2 /* This file is part of
3 * ======================================================
5 * LyX, The Document Processor
7 * Copyright 1995 Matthias Ettrich
8 * Copyright 1995-2000 The LyX Team.
10 * ====================================================== */
25 #include "lyx_gui_misc.h" // CancelCloseCB
26 #include "support/FileInfo.h"
35 # define NAMLEN(dirent) strlen((dirent)->d_name)
37 # define dirent direct
38 # define NAMLEN(dirent) (dirent)->d_namlen
40 # include <sys/ndir.h>
50 #if TIME_WITH_SYS_TIME
51 # include <sys/time.h>
55 # include <sys/time.h>
62 extern "C" int gettimeofday(struct timeval *, struct timezone *);
66 #pragma implementation
69 #include "support/filetools.h"
72 // six months, in seconds
73 static const long SIX_MONTH_SEC = 6L * 30L * 24L * 60L * 60L;
74 static const long ONE_HOUR_SEC = 60L * 60L;
76 // *** User cache class implementation
77 /// User cache class definition
80 /// seeks user name from group ID
81 string const & find(uid_t ID) const {
82 Users::const_iterator cit = users.find(ID);
83 if (cit == users.end()) {
91 void add(uid_t ID) const;
93 typedef map<uid_t, string> Users;
98 void UserCache::add(uid_t ID) const
101 struct passwd * pEntry;
104 if ((pEntry = getpwuid(ID)))
105 pszNewName = pEntry->pw_name;
107 pszNewName = tostr(ID);
111 users[ID] = pszNewName;
115 /// Group cache class definition
118 /// seeks group name from group ID
119 string const & find(gid_t ID) const ;
122 void add(gid_t ID) const;
124 typedef map<gid_t, string> Groups;
126 mutable Groups groups;
129 string const & GroupCache::find(gid_t ID) const
131 Groups::const_iterator cit = groups.find(ID);
132 if (cit == groups.end()) {
136 return (*cit).second;
138 void GroupCache::add(gid_t ID) const
141 struct group * pEntry;
144 if ((pEntry = getgrgid(ID))) pszNewName = pEntry->gr_name;
146 pszNewName = tostr(ID);
149 groups[ID] = pszNewName;
153 static UserCache lyxUserCache;
154 static GroupCache lyxGroupCache;
156 // some "C" wrappers around callbacks
157 extern "C" void C_LyXFileDlg_FileDlgCB(FL_OBJECT *, long lArgument);
158 extern "C" void C_LyXFileDlg_DoubleClickCB(FL_OBJECT *, long);
159 extern "C" int C_LyXFileDlg_CancelCB(FL_FORM *, void *);
162 // compares two LyXDirEntry objects content (used for sort)
163 class comp_direntry {
165 int operator()(LyXDirEntry const & r1,
166 LyXDirEntry const & r2) const ;
168 int comp_direntry::operator()(LyXDirEntry const & r1,
169 LyXDirEntry const & r2) const {
170 bool r1d = suffixIs(r1.pszName, '/');
171 bool r2d = suffixIs(r2.pszName, '/');
172 if (r1d && !r2d) return 1;
173 if (!r1d && r2d) return 0;
174 return r1.pszName < r2.pszName;
178 // *** LyXFileDlg class implementation
181 FD_FileDlg * LyXFileDlg::pFileDlgForm = 0;
182 LyXFileDlg * LyXFileDlg::pCurrentDlg = 0;
185 // Reread: updates dialog list to match class directory
186 void LyXFileDlg::Reread()
189 DIR * pDirectory = ::opendir(pszDirectory.c_str());
191 WriteFSAlert(_("Warning! Couldn't open directory."),
193 pszDirectory = GetCWD();
194 pDirectory = ::opendir(pszDirectory.c_str());
197 // Clear the present namelist
201 fl_hide_object(pFileDlgForm->List);
202 fl_clear_browser(pFileDlgForm->List);
203 fl_set_input(pFileDlgForm->DirBox, pszDirectory.c_str());
205 // Splits complete directory name into directories and compute depth
210 string File = pszDirectory;
212 File = split(File, Temp, '/');
214 while (!File.empty() || !Temp.empty()) {
215 string dline = "@b"+line + Temp + '/';
216 fl_add_browser_line(pFileDlgForm->List, dline.c_str());
217 File = split(File, Temp, '/');
222 // Parses all entries of the given subdirectory
223 time_t curTime = time(0);
224 rewinddir(pDirectory);
225 struct dirent * pDirEntry;
226 while ((pDirEntry = readdir(pDirectory))) {
227 bool isLink = false, isDir = false;
229 // If the pattern doesn't start with a dot, skip hidden files
230 if (!pszMask.empty() && pszMask[0] != '.' &&
231 pDirEntry->d_name[0] == '.')
235 string fname = pDirEntry->d_name;
237 // Under all circumstances, "." and ".." are not wanted
238 if (fname == "." || fname == "..")
242 File = AddName(pszDirectory, fname);
244 fileInfo.newFile(File, true);
245 fileInfo.modeString(szMode);
246 unsigned int nlink = fileInfo.getNumberOfLinks();
247 string user = lyxUserCache.find(fileInfo.getUid());
248 string group = lyxGroupCache.find(fileInfo.getGid());
250 time_t modtime = fileInfo.getModificationTime();
251 string Time = ctime(&modtime);
253 if (curTime > fileInfo.getModificationTime() + SIX_MONTH_SEC
254 || curTime < fileInfo.getModificationTime()
256 // The file is fairly old or in the future. POSIX says
257 // the cutoff is 6 months old. Allow a 1 hour slop
258 // factor for what is considered "the future", to
259 // allow for NFS server/client clock disagreement.
260 // Show the year instead of the time of day.
262 Time.erase(15, string::npos);
264 Time.erase(16, string::npos);
267 string Buffer = string(szMode) + ' ' +
271 Time.substr(4, string::npos) + ' ';
273 Buffer += pDirEntry->d_name;
274 Buffer += fileInfo.typeIndicator();
276 if ((isLink = fileInfo.isLink())) {
279 if (LyXReadLink(File, Link)) {
283 // This gives the FileType of the file that
284 // is really pointed too after resolving all
285 // symlinks. This is not necessarily the same
286 // as the type of Link (which could again be a
287 // link). Is that intended?
289 fileInfo.newFile(File);
290 Buffer += fileInfo.typeIndicator();
294 // filters files according to pattern and type
295 if (fileInfo.isRegular()
297 || fileInfo.isBlock()
298 || fileInfo.isFifo()) {
299 if (!regexMatch(fname, pszMask))
301 } else if (!(isDir = fileInfo.isDir()))
306 // Note pszLsEntry is an string!
307 tmp.pszLsEntry = Buffer;
310 if (isDir) temp += '/';
313 // creates displayed name
314 temp = pDirEntry->d_name;
318 temp += fileInfo.typeIndicator();
319 tmp.pszDisplayed = temp;
321 direntries.push_back(tmp);
324 closedir(pDirectory);
327 sort(direntries.begin(), direntries.end(), comp_direntry());
329 // Add them to directory box
330 for (DirEntries::const_iterator cit = direntries.begin();
331 cit != direntries.end(); ++cit) {
332 string temp = line + (*cit).pszDisplayed;
333 fl_add_browser_line(pFileDlgForm->List, temp.c_str());
335 fl_set_browser_topline(pFileDlgForm->List, iDepth);
336 fl_show_object(pFileDlgForm->List);
341 // SetDirectory: sets dialog current directory
342 void LyXFileDlg::SetDirectory(string const & Path)
344 if (!pszDirectory.empty()) {
345 string TempPath = ExpandPath(Path); // Expand ~/
346 TempPath = MakeAbsPath(TempPath, pszDirectory);
347 pszDirectory = MakeAbsPath(TempPath);
348 } else pszDirectory = MakeAbsPath(Path);
352 // SetMask: sets dialog file mask
353 void LyXFileDlg::SetMask(string const & NewMask)
356 fl_set_input(pFileDlgForm->PatBox, pszMask.c_str());
360 // SetInfoLine: sets dialog information line
361 void LyXFileDlg::SetInfoLine(string const & Line)
364 fl_set_object_label(pFileDlgForm->FileInfo, pszInfoLine.c_str());
368 LyXFileDlg::LyXFileDlg()
370 pszDirectory = MakeAbsPath(string("."));
373 // Creates form if necessary.
375 pFileDlgForm = create_form_FileDlg();
376 // Set callbacks. This means that we don't need a patch file
377 fl_set_object_callback(pFileDlgForm->DirBox,
378 C_LyXFileDlg_FileDlgCB, 0);
379 fl_set_object_callback(pFileDlgForm->PatBox,
380 C_LyXFileDlg_FileDlgCB, 1);
381 fl_set_object_callback(pFileDlgForm->List,
382 C_LyXFileDlg_FileDlgCB, 2);
383 fl_set_object_callback(pFileDlgForm->Filename,
384 C_LyXFileDlg_FileDlgCB, 3);
385 fl_set_object_callback(pFileDlgForm->Rescan,
386 C_LyXFileDlg_FileDlgCB, 10);
387 fl_set_object_callback(pFileDlgForm->Home,
388 C_LyXFileDlg_FileDlgCB, 11);
389 fl_set_object_callback(pFileDlgForm->User1,
390 C_LyXFileDlg_FileDlgCB, 12);
391 fl_set_object_callback(pFileDlgForm->User2,
392 C_LyXFileDlg_FileDlgCB, 13);
394 // Make sure pressing the close box doesn't crash LyX. (RvdK)
395 fl_set_form_atclose(pFileDlgForm->FileDlg,
396 C_LyXFileDlg_CancelCB, 0);
397 // Register doubleclick callback
398 fl_set_browser_dblclick_callback(pFileDlgForm->List,
399 C_LyXFileDlg_DoubleClickCB,
402 fl_hide_object(pFileDlgForm->User1);
403 fl_hide_object(pFileDlgForm->User2);
407 // SetButton: sets file selector user button action
408 void LyXFileDlg::SetButton(int iIndex, string const & pszName,
409 string const & pszPath)
415 pObject = pFileDlgForm->User1;
416 pTemp = &pszUserPath1;
417 } else if (iIndex == 1) {
418 pObject = pFileDlgForm->User2;
419 pTemp = &pszUserPath2;
422 if (!pszName.empty() && !pszPath.empty()) {
423 fl_set_object_label(pObject, pszName.c_str());
424 fl_show_object(pObject);
427 fl_hide_object(pObject);
433 // GetDirectory: gets last dialog directory
434 string const LyXFileDlg::GetDirectory() const
436 if (!pszDirectory.empty())
443 // RunDialog: handle dialog during file selection
444 bool LyXFileDlg::RunDialog()
446 force_cancel = false;
452 FL_OBJECT * pObject = fl_do_forms();
454 if (pObject == pFileDlgForm->Ready) {
457 } else if (pObject == pFileDlgForm->Cancel
466 // XForms objects callback (static)
467 void LyXFileDlg::FileDlgCB(FL_OBJECT *, long lArgument)
469 if (!pCurrentDlg) return;
473 case 0: // get directory
474 pCurrentDlg->SetDirectory(fl_get_input(pFileDlgForm->DirBox));
475 pCurrentDlg->Reread();
479 pCurrentDlg->SetMask(fl_get_input(pFileDlgForm->PatBox));
480 pCurrentDlg->Reread();
484 pCurrentDlg->HandleListHit();
488 pCurrentDlg->SetDirectory(fl_get_input(pFileDlgForm->DirBox));
489 pCurrentDlg->SetMask(fl_get_input(pFileDlgForm->PatBox));
490 pCurrentDlg->Reread();
494 pCurrentDlg->SetDirectory(GetEnvPath("HOME"));
495 pCurrentDlg->SetMask(fl_get_input(pFileDlgForm->PatBox));
496 pCurrentDlg->Reread();
499 case 12: // user button 1
500 if (!pCurrentDlg->pszUserPath1.empty()) {
501 pCurrentDlg->SetDirectory(pCurrentDlg->pszUserPath1);
502 pCurrentDlg->SetMask(fl_get_input(pFileDlgForm
504 pCurrentDlg->Reread();
508 case 13: // user button 2
509 if (!pCurrentDlg->pszUserPath2.empty()) {
510 pCurrentDlg->SetDirectory(pCurrentDlg->pszUserPath2);
511 pCurrentDlg->SetMask(fl_get_input(pFileDlgForm
513 pCurrentDlg->Reread();
521 extern "C" void C_LyXFileDlg_FileDlgCB(FL_OBJECT * ob, long data)
523 LyXFileDlg::FileDlgCB(ob, data);
527 // Handle callback from list
528 void LyXFileDlg::HandleListHit()
531 int iSelect = fl_get_browser(pFileDlgForm->List);
532 if (iSelect > iDepth) {
533 SetInfoLine(direntries[iSelect - iDepth - 1].pszLsEntry);
535 SetInfoLine(string());
540 // Callback for double click in list
541 void LyXFileDlg::DoubleClickCB(FL_OBJECT *, long)
543 if (pCurrentDlg->HandleDoubleClick())
544 // Simulate click on OK button
545 pCurrentDlg->Force(false);
548 extern "C" void C_LyXFileDlg_DoubleClickCB(FL_OBJECT * ob, long data)
550 LyXFileDlg::DoubleClickCB(ob, data);
553 // Handle double click from list
554 bool LyXFileDlg::HandleDoubleClick()
560 int iSelect = fl_get_browser(pFileDlgForm->List);
561 if (iSelect > iDepth) {
562 pszTemp = direntries[iSelect - iDepth - 1].pszName;
563 SetInfoLine(direntries[iSelect - iDepth - 1].pszLsEntry);
564 if (!suffixIs(pszTemp, '/')) {
566 fl_set_input(pFileDlgForm->Filename, pszTemp.c_str());
568 } else if (iSelect != 0) {
569 SetInfoLine(string());
577 // builds new directory name
578 if (iSelect > iDepth) {
579 // Directory deeper down
580 // First, get directory with trailing /
581 Temp = fl_get_input(pFileDlgForm->DirBox);
582 if (!suffixIs(Temp, '/'))
586 // Directory higher up
588 for (int i = 0; i < iSelect; ++i) {
589 string piece = fl_get_browser_line(pFileDlgForm->List, i+1);
590 // The '+2' is here to count the '@b' (JMarc)
591 Temp += piece.substr(i + 2);
604 // Handle OK button call
605 bool LyXFileDlg::HandleOK()
608 string pszTemp = fl_get_input(pFileDlgForm->PatBox);
609 if (pszTemp!= pszMask) {
615 // directory was changed
616 pszTemp = fl_get_input(pFileDlgForm->DirBox);
617 if (pszTemp!= pszDirectory) {
618 SetDirectory(pszTemp);
623 // Handle return from list
624 int select = fl_get_browser(pFileDlgForm->List);
625 if (select > iDepth) {
626 string temp = direntries[select - iDepth - 1].pszName;
627 if (!suffixIs(temp, '/')) {
628 // If user didn't type anything, use browser
629 string name = fl_get_input(pFileDlgForm->Filename);
631 fl_set_input(pFileDlgForm->Filename, temp.c_str());
637 // Emulate a doubleclick
638 return HandleDoubleClick();
642 // Handle Cancel CB from WM close
643 int LyXFileDlg::CancelCB(FL_FORM *, void *)
645 // Simulate a click on the cancel button
646 pCurrentDlg->Force(true);
651 extern "C" int C_LyXFileDlg_CancelCB(FL_FORM *fl, void *xev)
653 return LyXFileDlg::CancelCB(fl, xev);
657 // Simulates a click on OK/Cancel
658 void LyXFileDlg::Force(bool cancel)
662 fl_set_button(pFileDlgForm->Cancel, 1);
665 fl_set_button(pFileDlgForm->Ready, 1);
667 // Start timer to break fl_do_forms loop soon
668 fl_set_timer(pFileDlgForm->timer, 0.1);
672 // Select: launches dialog and returns selected file
673 string const LyXFileDlg::Select(string const & title, string const & path,
674 string const & mask, string const & suggested)
676 // handles new mask and path
688 fl_select_browser_line(pFileDlgForm->List, 1);
689 fl_set_browser_topline(pFileDlgForm->List, 1);
692 // checks whether dialog can be started
693 if (pCurrentDlg) return string();
697 SetInfoLine (string());
698 fl_set_input(pFileDlgForm->Filename, suggested.c_str());
699 fl_set_button(pFileDlgForm->Cancel, 0);
700 fl_set_button(pFileDlgForm->Ready, 0);
701 fl_set_focus_object(pFileDlgForm->FileDlg, pFileDlgForm->Filename);
702 fl_deactivate_all_forms();
703 fl_show_form(pFileDlgForm->FileDlg, FL_PLACE_MOUSE | FL_FREE_SIZE,
704 FL_FULLBORDER, title.c_str());
708 fl_hide_form(pFileDlgForm->FileDlg);
709 fl_activate_all_forms();
712 // Returns filename or string() if no valid selection was made
713 if (!isOk || !fl_get_input(pFileDlgForm->Filename)[0]) return string();
715 pszFileName = fl_get_input(pFileDlgForm->Filename);
717 if (!AbsolutePath(pszFileName)) {
718 pszFileName = AddName(fl_get_input(pFileDlgForm->DirBox),