2 * \file FormFiledialog.C
3 * Copyright 2001 the LyX Team
4 * Read the file COPYING
24 #include "lyx_gui_misc.h" // for WriteFSAlert
25 #include "support/FileInfo.h"
26 #include "support/lyxlib.h"
27 #include "support/lstrings.h"
29 #include "frontends/Dialogs.h"
37 # define NAMLEN(dirent) strlen((dirent)->d_name)
39 # define dirent direct
40 # define NAMLEN(dirent) (dirent)->d_namlen
42 # include <sys/ndir.h>
52 #if TIME_WITH_SYS_TIME
53 # include <sys/time.h>
57 # include <sys/time.h>
63 // FIXME: should be autoconfiscated
65 extern "C" int gettimeofday(struct timeval *, struct timezone *);
69 #pragma implementation
72 #include "support/filetools.h"
73 #include "FormFiledialog.h"
77 // six months, in seconds
78 long const SIX_MONTH_SEC = 6L * 30L * 24L * 60L * 60L;
80 long const ONE_HOUR_SEC = 60L * 60L;
85 int C_LyXFileDlg_CancelCB(FL_FORM *fl, void *xev)
87 return FileDialog::Private::CancelCB(fl, xev);
91 void C_LyXFileDlg_DoubleClickCB(FL_OBJECT * ob, long data)
93 FileDialog::Private::DoubleClickCB(ob, data);
97 void C_LyXFileDlg_FileDlgCB(FL_OBJECT * ob, long data)
99 FileDialog::Private::FileDlgCB(ob, data);
107 // *** User cache class implementation
108 /// User cache class definition
111 /// seeks user name from group ID
112 string const & find(uid_t ID) const {
113 Users::const_iterator cit = users.find(ID);
114 if (cit == users.end()) {
122 void add(uid_t ID) const;
124 typedef map<uid_t, string> Users;
130 void UserCache::add(uid_t ID) const
133 struct passwd * pEntry;
136 if ((pEntry = getpwuid(ID)))
137 pszNewName = pEntry->pw_name;
139 pszNewName = tostr(ID);
143 users[ID] = pszNewName;
147 /// Group cache class definition
150 /// seeks group name from group ID
151 string const & find(gid_t ID) const ;
154 void add(gid_t ID) const;
156 typedef map<gid_t, string> Groups;
158 mutable Groups groups;
162 string const & GroupCache::find(gid_t ID) const
164 Groups::const_iterator cit = groups.find(ID);
165 if (cit == groups.end()) {
173 void GroupCache::add(gid_t ID) const
176 struct group * pEntry;
179 if ((pEntry = getgrgid(ID))) pszNewName = pEntry->gr_name;
181 pszNewName = tostr(ID);
184 groups[ID] = pszNewName;
191 UserCache lyxUserCache;
192 GroupCache lyxGroupCache;
197 // compares two LyXDirEntry objects content (used for sort)
198 class comp_direntry {
200 int operator()(DirEntry const & r1,
201 DirEntry const & r2) const ;
203 int comp_direntry::operator()(DirEntry const & r1,
204 DirEntry const & r2) const {
205 bool r1d = suffixIs(r1.pszName, '/');
206 bool r2d = suffixIs(r2.pszName, '/');
207 if (r1d && !r2d) return 1;
208 if (!r1d && r2d) return 0;
209 return r1.pszName < r2.pszName;
213 // *** FileDialog::Private class implementation
216 FD_form_filedialog * FileDialog::Private::pFileDlgForm = 0;
217 FileDialog::Private * FileDialog::Private::pCurrentDlg = 0;
220 // Reread: updates dialog list to match class directory
221 void FileDialog::Private::Reread()
224 DIR * pDirectory = ::opendir(pszDirectory.c_str());
226 WriteFSAlert(_("Warning! Couldn't open directory."),
228 pszDirectory = lyx::getcwd();
229 pDirectory = ::opendir(pszDirectory.c_str());
232 // Clear the present namelist
236 fl_hide_object(pFileDlgForm->List);
237 fl_clear_browser(pFileDlgForm->List);
238 fl_set_input(pFileDlgForm->DirBox, pszDirectory.c_str());
240 // Splits complete directory name into directories and compute depth
245 string File = pszDirectory;
247 File = split(File, Temp, '/');
249 while (!File.empty() || !Temp.empty()) {
250 string dline = "@b"+line + Temp + '/';
251 fl_add_browser_line(pFileDlgForm->List, dline.c_str());
252 File = split(File, Temp, '/');
257 // Parses all entries of the given subdirectory
258 time_t curTime = time(0);
259 rewinddir(pDirectory);
260 struct dirent * pDirEntry;
261 while ((pDirEntry = readdir(pDirectory))) {
262 bool isLink = false, isDir = false;
264 // If the pattern doesn't start with a dot, skip hidden files
265 if (!pszMask.empty() && pszMask[0] != '.' &&
266 pDirEntry->d_name[0] == '.')
270 string fname = pDirEntry->d_name;
272 // Under all circumstances, "." and ".." are not wanted
273 if (fname == "." || fname == "..")
277 File = AddName(pszDirectory, fname);
279 fileInfo.newFile(File, true);
280 fileInfo.modeString(szMode);
281 unsigned int nlink = fileInfo.getNumberOfLinks();
282 string user = lyxUserCache.find(fileInfo.getUid());
283 string group = lyxGroupCache.find(fileInfo.getGid());
285 time_t modtime = fileInfo.getModificationTime();
286 string Time = ctime(&modtime);
288 if (curTime > fileInfo.getModificationTime() + SIX_MONTH_SEC
289 || curTime < fileInfo.getModificationTime()
291 // The file is fairly old or in the future. POSIX says
292 // the cutoff is 6 months old. Allow a 1 hour slop
293 // factor for what is considered "the future", to
294 // allow for NFS server/client clock disagreement.
295 // Show the year instead of the time of day.
297 Time.erase(15, string::npos);
299 Time.erase(16, string::npos);
302 string Buffer = string(szMode) + ' ' +
306 Time.substr(4, string::npos) + ' ';
308 Buffer += pDirEntry->d_name;
309 Buffer += fileInfo.typeIndicator();
311 if ((isLink = fileInfo.isLink())) {
314 if (LyXReadLink(File, Link)) {
318 // This gives the FileType of the file that
319 // is really pointed too after resolving all
320 // symlinks. This is not necessarily the same
321 // as the type of Link (which could again be a
322 // link). Is that intended?
324 fileInfo.newFile(File);
325 Buffer += fileInfo.typeIndicator();
329 // filters files according to pattern and type
330 if (fileInfo.isRegular()
332 || fileInfo.isBlock()
333 || fileInfo.isFifo()) {
334 if (!regexMatch(fname, pszMask))
336 } else if (!(isDir = fileInfo.isDir()))
341 // Note pszLsEntry is an string!
342 tmp.pszLsEntry = Buffer;
345 if (isDir) temp += '/';
348 // creates displayed name
349 temp = pDirEntry->d_name;
353 temp += fileInfo.typeIndicator();
354 tmp.pszDisplayed = temp;
356 direntries.push_back(tmp);
359 closedir(pDirectory);
362 sort(direntries.begin(), direntries.end(), comp_direntry());
364 // Add them to directory box
365 for (DirEntries::const_iterator cit = direntries.begin();
366 cit != direntries.end(); ++cit) {
367 string const temp = line + cit->pszDisplayed;
368 fl_add_browser_line(pFileDlgForm->List, temp.c_str());
370 fl_set_browser_topline(pFileDlgForm->List, iDepth);
371 fl_show_object(pFileDlgForm->List);
376 // SetDirectory: sets dialog current directory
377 void FileDialog::Private::SetDirectory(string const & Path)
381 if (!pszDirectory.empty()) {
382 string TempPath = ExpandPath(Path); // Expand ~/
383 TempPath = MakeAbsPath(TempPath, pszDirectory);
384 tmp = MakeAbsPath(TempPath);
386 tmp = MakeAbsPath(Path);
389 // must check the directory exists
390 DIR * pDirectory = ::opendir(tmp.c_str());
392 WriteFSAlert(_("Warning! Couldn't open directory."), tmp);
394 ::closedir(pDirectory);
400 // SetMask: sets dialog file mask
401 void FileDialog::Private::SetMask(string const & NewMask)
404 fl_set_input(pFileDlgForm->PatBox, pszMask.c_str());
408 // SetInfoLine: sets dialog information line
409 void FileDialog::Private::SetInfoLine(string const & Line)
412 fl_set_object_label(pFileDlgForm->FileInfo, pszInfoLine.c_str());
416 FileDialog::Private::Private()
418 pszDirectory = MakeAbsPath(string("."));
421 // Creates form if necessary.
423 pFileDlgForm = build_filedialog();
424 // Set callbacks. This means that we don't need a patch file
425 fl_set_object_callback(pFileDlgForm->DirBox,
426 C_LyXFileDlg_FileDlgCB, 0);
427 fl_set_object_callback(pFileDlgForm->PatBox,
428 C_LyXFileDlg_FileDlgCB, 1);
429 fl_set_object_callback(pFileDlgForm->List,
430 C_LyXFileDlg_FileDlgCB, 2);
431 fl_set_object_callback(pFileDlgForm->Filename,
432 C_LyXFileDlg_FileDlgCB, 3);
433 fl_set_object_callback(pFileDlgForm->Rescan,
434 C_LyXFileDlg_FileDlgCB, 10);
435 fl_set_object_callback(pFileDlgForm->Home,
436 C_LyXFileDlg_FileDlgCB, 11);
437 fl_set_object_callback(pFileDlgForm->User1,
438 C_LyXFileDlg_FileDlgCB, 12);
439 fl_set_object_callback(pFileDlgForm->User2,
440 C_LyXFileDlg_FileDlgCB, 13);
442 // Make sure pressing the close box doesn't crash LyX. (RvdK)
443 fl_set_form_atclose(pFileDlgForm->form,
444 C_LyXFileDlg_CancelCB, 0);
445 // Register doubleclick callback
446 fl_set_browser_dblclick_callback(pFileDlgForm->List,
447 C_LyXFileDlg_DoubleClickCB,
450 fl_hide_object(pFileDlgForm->User1);
451 fl_hide_object(pFileDlgForm->User2);
453 r_ = Dialogs::redrawGUI.connect(SigC::slot(this, &FileDialog::Private::redraw));
457 FileDialog::Private::~Private()
463 void FileDialog::Private::redraw()
465 if (pFileDlgForm->form && pFileDlgForm->form->visible)
466 fl_redraw_form(pFileDlgForm->form);
470 // SetButton: sets file selector user button action
471 void FileDialog::Private::SetButton(int iIndex, string const & pszName,
472 string const & pszPath)
478 pObject = pFileDlgForm->User1;
479 pTemp = &pszUserPath1;
480 } else if (iIndex == 1) {
481 pObject = pFileDlgForm->User2;
482 pTemp = &pszUserPath2;
485 if (!pszName.empty() && !pszPath.empty()) {
486 fl_set_object_label(pObject, pszName.c_str());
487 fl_show_object(pObject);
490 fl_hide_object(pObject);
496 // GetDirectory: gets last dialog directory
497 string const FileDialog::Private::GetDirectory() const
499 if (!pszDirectory.empty())
506 // RunDialog: handle dialog during file selection
507 bool FileDialog::Private::RunDialog()
509 force_cancel = false;
514 FL_OBJECT * pObject = fl_do_forms();
516 if (pObject == pFileDlgForm->Ready) {
519 } else if (pObject == pFileDlgForm->Cancel
528 // XForms objects callback (static)
529 void FileDialog::Private::FileDlgCB(FL_OBJECT *, long lArgument)
531 if (!pCurrentDlg) return;
535 case 0: // get directory
536 pCurrentDlg->SetDirectory(fl_get_input(pFileDlgForm->DirBox));
537 pCurrentDlg->Reread();
541 pCurrentDlg->SetMask(fl_get_input(pFileDlgForm->PatBox));
542 pCurrentDlg->Reread();
546 pCurrentDlg->HandleListHit();
550 pCurrentDlg->SetDirectory(fl_get_input(pFileDlgForm->DirBox));
551 pCurrentDlg->SetMask(fl_get_input(pFileDlgForm->PatBox));
552 pCurrentDlg->Reread();
556 pCurrentDlg->SetDirectory(GetEnvPath("HOME"));
557 pCurrentDlg->SetMask(fl_get_input(pFileDlgForm->PatBox));
558 pCurrentDlg->Reread();
561 case 12: // user button 1
562 if (!pCurrentDlg->pszUserPath1.empty()) {
563 pCurrentDlg->SetDirectory(pCurrentDlg->pszUserPath1);
564 pCurrentDlg->SetMask(fl_get_input(pFileDlgForm
566 pCurrentDlg->Reread();
570 case 13: // user button 2
571 if (!pCurrentDlg->pszUserPath2.empty()) {
572 pCurrentDlg->SetDirectory(pCurrentDlg->pszUserPath2);
573 pCurrentDlg->SetMask(fl_get_input(pFileDlgForm
575 pCurrentDlg->Reread();
583 // Handle callback from list
584 void FileDialog::Private::HandleListHit()
587 int const iSelect = fl_get_browser(pFileDlgForm->List);
588 if (iSelect > iDepth) {
589 SetInfoLine(direntries[iSelect - iDepth - 1].pszLsEntry);
591 SetInfoLine(string());
596 // Callback for double click in list
597 void FileDialog::Private::DoubleClickCB(FL_OBJECT *, long)
599 if (pCurrentDlg->HandleDoubleClick())
600 // Simulate click on OK button
601 pCurrentDlg->Force(false);
605 // Handle double click from list
606 bool FileDialog::Private::HandleDoubleClick()
612 int const iSelect = fl_get_browser(pFileDlgForm->List);
613 if (iSelect > iDepth) {
614 pszTemp = direntries[iSelect - iDepth - 1].pszName;
615 SetInfoLine(direntries[iSelect - iDepth - 1].pszLsEntry);
616 if (!suffixIs(pszTemp, '/')) {
618 fl_set_input(pFileDlgForm->Filename, pszTemp.c_str());
620 } else if (iSelect != 0) {
621 SetInfoLine(string());
629 // builds new directory name
630 if (iSelect > iDepth) {
631 // Directory deeper down
632 // First, get directory with trailing /
633 Temp = fl_get_input(pFileDlgForm->DirBox);
634 if (!suffixIs(Temp, '/'))
638 // Directory higher up
640 for (int i = 0; i < iSelect; ++i) {
641 string piece = fl_get_browser_line(pFileDlgForm->List, i+1);
642 // The '+2' is here to count the '@b' (JMarc)
643 Temp += piece.substr(i + 2);
656 // Handle OK button call
657 bool FileDialog::Private::HandleOK()
660 string pszTemp = fl_get_input(pFileDlgForm->PatBox);
661 if (pszTemp != pszMask) {
667 // directory was changed
668 pszTemp = fl_get_input(pFileDlgForm->DirBox);
669 if (pszTemp!= pszDirectory) {
670 SetDirectory(pszTemp);
675 // Handle return from list
676 int const select = fl_get_browser(pFileDlgForm->List);
677 if (select > iDepth) {
678 string const temp = direntries[select - iDepth - 1].pszName;
679 if (!suffixIs(temp, '/')) {
680 // If user didn't type anything, use browser
682 fl_get_input(pFileDlgForm->Filename);
684 fl_set_input(pFileDlgForm->Filename, temp.c_str());
690 // Emulate a doubleclick
691 return HandleDoubleClick();
695 // Handle Cancel CB from WM close
696 int FileDialog::Private::CancelCB(FL_FORM *, void *)
698 // Simulate a click on the cancel button
699 pCurrentDlg->Force(true);
704 // Simulates a click on OK/Cancel
705 void FileDialog::Private::Force(bool cancel)
709 fl_set_button(pFileDlgForm->Cancel, 1);
712 fl_set_button(pFileDlgForm->Ready, 1);
714 // Start timer to break fl_do_forms loop soon
715 fl_set_timer(pFileDlgForm->timer, 0.1);
719 // Select: launches dialog and returns selected file
720 string const FileDialog::Private::Select(string const & title, string const & path,
721 string const & mask, string const & suggested)
723 // handles new mask and path
735 // highlight the suggested file in the browser, if it exists.
737 string const filename = OnlyFilename(suggested);
738 if (!filename.empty()) {
740 i < fl_get_browser_maxline(pFileDlgForm->List); ++i) {
742 fl_get_browser_line(pFileDlgForm->List, i + 1);
743 s = strip(frontStrip(s));
751 if (sel != 0) fl_select_browser_line(pFileDlgForm->List, sel);
752 int const top = max(sel - 5, 1);
753 fl_set_browser_topline(pFileDlgForm->List, top);
755 // checks whether dialog can be started
756 if (pCurrentDlg) return string();
760 SetInfoLine(string());
761 fl_set_input(pFileDlgForm->Filename, suggested.c_str());
762 fl_set_button(pFileDlgForm->Cancel, 0);
763 fl_set_button(pFileDlgForm->Ready, 0);
764 fl_set_focus_object(pFileDlgForm->form, pFileDlgForm->Filename);
765 fl_deactivate_all_forms();
766 fl_show_form(pFileDlgForm->form,
767 FL_PLACE_MOUSE | FL_FREE_SIZE, 0,
772 fl_hide_form(pFileDlgForm->form);
773 fl_activate_all_forms();
776 // Returns filename or string() if no valid selection was made
777 if (!isOk || !fl_get_input(pFileDlgForm->Filename)[0]) return string();
779 pszFileName = fl_get_input(pFileDlgForm->Filename);
781 if (!AbsolutePath(pszFileName)) {
782 pszFileName = AddName(fl_get_input(pFileDlgForm->DirBox),