2 * \file FormFiledialog.C
3 * Copyright 2001 the LyX Team
4 * Read the file COPYING
24 #include "frontends/Alert.h"
25 #include "support/FileInfo.h"
26 #include "support/lyxlib.h"
27 #include "support/lstrings.h"
29 #include "frontends/Dialogs.h"
30 #include "xforms_helpers.h"
39 # define NAMLEN(dirent) strlen((dirent)->d_name)
41 # define dirent direct
42 # define NAMLEN(dirent) (dirent)->d_namlen
44 # include <sys/ndir.h>
54 #if TIME_WITH_SYS_TIME
55 # include <sys/time.h>
59 # include <sys/time.h>
65 // FIXME: should be autoconfiscated
67 extern "C" int gettimeofday(struct timeval *, struct timezone *);
71 #pragma implementation
74 #include "support/filetools.h"
75 #include "FormFiledialog.h"
79 // six months, in seconds
80 long const SIX_MONTH_SEC = 6L * 30L * 24L * 60L * 60L;
82 long const ONE_HOUR_SEC = 60L * 60L;
87 int C_LyXFileDlg_CancelCB(FL_FORM *fl, void *xev)
89 return FileDialog::Private::CancelCB(fl, xev);
93 void C_LyXFileDlg_DoubleClickCB(FL_OBJECT * ob, long data)
95 FileDialog::Private::DoubleClickCB(ob, data);
99 void C_LyXFileDlg_FileDlgCB(FL_OBJECT * ob, long data)
101 FileDialog::Private::FileDlgCB(ob, data);
109 // *** User cache class implementation
110 /// User cache class definition
113 /// seeks user name from group ID
114 string const & find(uid_t ID) const {
115 Users::const_iterator cit = users.find(ID);
116 if (cit == users.end()) {
124 void add(uid_t ID) const;
126 typedef map<uid_t, string> Users;
132 void UserCache::add(uid_t ID) const
135 struct passwd * pEntry;
138 if ((pEntry = getpwuid(ID)))
139 pszNewName = pEntry->pw_name;
141 pszNewName = tostr(ID);
145 users[ID] = pszNewName;
149 /// Group cache class definition
152 /// seeks group name from group ID
153 string const & find(gid_t ID) const ;
156 void add(gid_t ID) const;
158 typedef map<gid_t, string> Groups;
160 mutable Groups groups;
164 string const & GroupCache::find(gid_t ID) const
166 Groups::const_iterator cit = groups.find(ID);
167 if (cit == groups.end()) {
175 void GroupCache::add(gid_t ID) const
178 struct group * pEntry;
181 if ((pEntry = getgrgid(ID))) pszNewName = pEntry->gr_name;
183 pszNewName = tostr(ID);
186 groups[ID] = pszNewName;
193 UserCache lyxUserCache;
194 GroupCache lyxGroupCache;
199 // compares two LyXDirEntry objects content (used for sort)
200 class comp_direntry {
202 int operator()(DirEntry const & r1,
203 DirEntry const & r2) const ;
205 int comp_direntry::operator()(DirEntry const & r1,
206 DirEntry const & r2) const {
207 bool r1d = suffixIs(r1.pszName, '/');
208 bool r2d = suffixIs(r2.pszName, '/');
209 if (r1d && !r2d) return 1;
210 if (!r1d && r2d) return 0;
211 return r1.pszName < r2.pszName;
215 // *** FileDialog::Private class implementation
218 FD_form_filedialog * FileDialog::Private::pFileDlgForm = 0;
219 FileDialog::Private * FileDialog::Private::pCurrentDlg = 0;
222 // Reread: updates dialog list to match class directory
223 void FileDialog::Private::Reread()
226 DIR * pDirectory = ::opendir(pszDirectory.c_str());
228 Alert::err_alert(_("Warning! Couldn't open directory."),
230 pszDirectory = lyx::getcwd();
231 pDirectory = ::opendir(pszDirectory.c_str());
234 // Clear the present namelist
238 fl_hide_object(pFileDlgForm->List);
239 fl_clear_browser(pFileDlgForm->List);
240 fl_set_input(pFileDlgForm->DirBox, pszDirectory.c_str());
242 // Splits complete directory name into directories and compute depth
246 string File = pszDirectory;
248 File = split(File, Temp, '/');
250 while (!File.empty() || !Temp.empty()) {
251 string dline = "@b"+line + Temp + '/';
252 fl_add_browser_line(pFileDlgForm->List, dline.c_str());
253 File = split(File, Temp, '/');
258 // Parses all entries of the given subdirectory
259 time_t curTime = time(0);
260 rewinddir(pDirectory);
261 struct dirent * pDirEntry;
262 while ((pDirEntry = readdir(pDirectory))) {
263 bool isLink = false, isDir = false;
265 // If the pattern doesn't start with a dot, skip hidden files
266 if (!pszMask.empty() && pszMask[0] != '.' &&
267 pDirEntry->d_name[0] == '.')
271 string fname = pDirEntry->d_name;
273 // Under all circumstances, "." and ".." are not wanted
274 if (fname == "." || fname == "..")
278 File = AddName(pszDirectory, fname);
280 // FIXME: we don't get this file exists/stattable
281 FileInfo fileInfo(File, true);
282 fileInfo.modeString(szMode);
283 unsigned int nlink = fileInfo.getNumberOfLinks();
284 string user = lyxUserCache.find(fileInfo.getUid());
285 string group = lyxGroupCache.find(fileInfo.getGid());
287 time_t modtime = fileInfo.getModificationTime();
288 string Time = ctime(&modtime);
290 if (curTime > modtime + SIX_MONTH_SEC
291 || curTime < modtime + ONE_HOUR_SEC) {
292 // The file is fairly old or in the future. POSIX says
293 // the cutoff is 6 months old. Allow a 1 hour slop
294 // factor for what is considered "the future", to
295 // allow for NFS server/client clock disagreement.
296 // Show the year instead of the time of day.
298 Time.erase(15, string::npos);
300 Time.erase(16, string::npos);
303 string Buffer = string(szMode) + ' ' +
307 Time.substr(4, string::npos) + ' ';
309 Buffer += pDirEntry->d_name;
310 Buffer += fileInfo.typeIndicator();
312 if ((isLink = fileInfo.isLink())) {
315 if (LyXReadLink(File, Link)) {
319 // This gives the FileType of the file that
320 // is really pointed too after resolving all
321 // symlinks. This is not necessarily the same
322 // as the type of Link (which could again be a
323 // link). Is that intended?
325 fileInfo.newFile(File);
326 Buffer += fileInfo.typeIndicator();
330 // filters files according to pattern and type
331 if (fileInfo.isRegular()
333 || fileInfo.isBlock()
334 || fileInfo.isFifo()) {
335 if (!regexMatch(fname, pszMask))
337 } else if (!(isDir = fileInfo.isDir()))
342 // Note pszLsEntry is an string!
343 tmp.pszLsEntry = Buffer;
346 if (isDir) temp += '/';
349 // creates displayed name
350 temp = pDirEntry->d_name;
354 temp += fileInfo.typeIndicator();
355 tmp.pszDisplayed = temp;
357 direntries.push_back(tmp);
360 closedir(pDirectory);
363 sort(direntries.begin(), direntries.end(), comp_direntry());
365 // Add them to directory box
366 for (DirEntries::const_iterator cit = direntries.begin();
367 cit != direntries.end(); ++cit) {
368 string const temp = line + cit->pszDisplayed;
369 fl_add_browser_line(pFileDlgForm->List, temp.c_str());
371 fl_set_browser_topline(pFileDlgForm->List, iDepth);
372 fl_show_object(pFileDlgForm->List);
377 // SetDirectory: sets dialog current directory
378 void FileDialog::Private::SetDirectory(string const & Path)
386 tmp = MakeAbsPath(ExpandPath(Path), pszDirectory);
388 // must check the directory exists
389 DIR * pDirectory = ::opendir(tmp.c_str());
391 Alert::err_alert(_("Warning! Couldn't open directory."), tmp);
393 ::closedir(pDirectory);
399 // SetMask: sets dialog file mask
400 void FileDialog::Private::SetMask(string const & NewMask)
403 fl_set_input(pFileDlgForm->PatBox, pszMask.c_str());
407 // SetInfoLine: sets dialog information line
408 void FileDialog::Private::SetInfoLine(string const & Line)
411 fl_set_object_label(pFileDlgForm->FileInfo, pszInfoLine.c_str());
415 FileDialog::Private::Private()
417 pszDirectory = MakeAbsPath(string("."));
420 // Creates form if necessary.
422 pFileDlgForm = build_filedialog();
423 // Set callbacks. This means that we don't need a patch file
424 fl_set_object_callback(pFileDlgForm->DirBox,
425 C_LyXFileDlg_FileDlgCB, 0);
426 fl_set_object_callback(pFileDlgForm->PatBox,
427 C_LyXFileDlg_FileDlgCB, 1);
428 fl_set_object_callback(pFileDlgForm->List,
429 C_LyXFileDlg_FileDlgCB, 2);
430 fl_set_object_callback(pFileDlgForm->Filename,
431 C_LyXFileDlg_FileDlgCB, 3);
432 fl_set_object_callback(pFileDlgForm->Rescan,
433 C_LyXFileDlg_FileDlgCB, 10);
434 fl_set_object_callback(pFileDlgForm->Home,
435 C_LyXFileDlg_FileDlgCB, 11);
436 fl_set_object_callback(pFileDlgForm->User1,
437 C_LyXFileDlg_FileDlgCB, 12);
438 fl_set_object_callback(pFileDlgForm->User2,
439 C_LyXFileDlg_FileDlgCB, 13);
441 // Make sure pressing the close box doesn't crash LyX. (RvdK)
442 fl_set_form_atclose(pFileDlgForm->form,
443 C_LyXFileDlg_CancelCB, 0);
444 // Register doubleclick callback
445 fl_set_browser_dblclick_callback(pFileDlgForm->List,
446 C_LyXFileDlg_DoubleClickCB,
449 fl_hide_object(pFileDlgForm->User1);
450 fl_hide_object(pFileDlgForm->User2);
452 r_ = Dialogs::redrawGUI.connect(SigC::slot(this, &FileDialog::Private::redraw));
456 FileDialog::Private::~Private()
462 void FileDialog::Private::redraw()
464 if (pFileDlgForm->form && pFileDlgForm->form->visible)
465 fl_redraw_form(pFileDlgForm->form);
469 // SetButton: sets file selector user button action
470 void FileDialog::Private::SetButton(int iIndex, string const & pszName,
471 string const & pszPath)
477 pObject = pFileDlgForm->User1;
478 pTemp = &pszUserPath1;
479 } else if (iIndex == 1) {
480 pObject = pFileDlgForm->User2;
481 pTemp = &pszUserPath2;
484 if (!pszName.empty()) {
485 fl_set_object_label(pObject, idex(pszName.c_str()));
486 fl_set_button_shortcut(pObject, scex(pszName.c_str()), 1);
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 pCurrentDlg->SetDirectory(pCurrentDlg->pszUserPath1);
563 pCurrentDlg->SetMask(fl_get_input(pFileDlgForm
565 pCurrentDlg->Reread();
568 case 13: // user button 2
569 pCurrentDlg->SetDirectory(pCurrentDlg->pszUserPath2);
570 pCurrentDlg->SetMask(fl_get_input(pFileDlgForm
572 pCurrentDlg->Reread();
579 // Handle callback from list
580 void FileDialog::Private::HandleListHit()
583 int const iSelect = fl_get_browser(pFileDlgForm->List);
584 if (iSelect > iDepth) {
585 SetInfoLine(direntries[iSelect - iDepth - 1].pszLsEntry);
587 SetInfoLine(string());
592 // Callback for double click in list
593 void FileDialog::Private::DoubleClickCB(FL_OBJECT *, long)
595 if (pCurrentDlg->HandleDoubleClick())
596 // Simulate click on OK button
597 pCurrentDlg->Force(false);
601 // Handle double click from list
602 bool FileDialog::Private::HandleDoubleClick()
608 int const iSelect = fl_get_browser(pFileDlgForm->List);
609 if (iSelect > iDepth) {
610 pszTemp = direntries[iSelect - iDepth - 1].pszName;
611 SetInfoLine(direntries[iSelect - iDepth - 1].pszLsEntry);
612 if (!suffixIs(pszTemp, '/')) {
614 fl_set_input(pFileDlgForm->Filename, pszTemp.c_str());
616 } else if (iSelect != 0) {
617 SetInfoLine(string());
625 // builds new directory name
626 if (iSelect > iDepth) {
627 // Directory deeper down
628 // First, get directory with trailing /
629 Temp = fl_get_input(pFileDlgForm->DirBox);
630 if (!suffixIs(Temp, '/'))
634 // Directory higher up
636 for (int i = 0; i < iSelect; ++i) {
637 string piece = fl_get_browser_line(pFileDlgForm->List, i+1);
638 // The '+2' is here to count the '@b' (JMarc)
639 Temp += piece.substr(i + 2);
652 // Handle OK button call
653 bool FileDialog::Private::HandleOK()
656 string pszTemp = fl_get_input(pFileDlgForm->PatBox);
657 if (pszTemp != pszMask) {
663 // directory was changed
664 pszTemp = fl_get_input(pFileDlgForm->DirBox);
665 if (pszTemp!= pszDirectory) {
666 SetDirectory(pszTemp);
671 // Handle return from list
672 int const select = fl_get_browser(pFileDlgForm->List);
673 if (select > iDepth) {
674 string const temp = direntries[select - iDepth - 1].pszName;
675 if (!suffixIs(temp, '/')) {
676 // If user didn't type anything, use browser
678 fl_get_input(pFileDlgForm->Filename);
680 fl_set_input(pFileDlgForm->Filename, temp.c_str());
686 // Emulate a doubleclick
687 return HandleDoubleClick();
691 // Handle Cancel CB from WM close
692 int FileDialog::Private::CancelCB(FL_FORM *, void *)
694 // Simulate a click on the cancel button
695 pCurrentDlg->Force(true);
700 // Simulates a click on OK/Cancel
701 void FileDialog::Private::Force(bool cancel)
705 fl_set_button(pFileDlgForm->Cancel, 1);
708 fl_set_button(pFileDlgForm->Ready, 1);
710 // Start timer to break fl_do_forms loop soon
711 fl_set_timer(pFileDlgForm->timer, 0.1);
715 // Select: launches dialog and returns selected file
716 string const FileDialog::Private::Select(string const & title,
719 string const & suggested)
721 // handles new mask and path
733 // highlight the suggested file in the browser, if it exists.
735 string const filename = OnlyFilename(suggested);
736 if (!filename.empty()) {
738 i < fl_get_browser_maxline(pFileDlgForm->List); ++i) {
740 fl_get_browser_line(pFileDlgForm->List, i + 1);
741 s = strip(frontStrip(s));
749 if (sel != 0) fl_select_browser_line(pFileDlgForm->List, sel);
750 int const top = max(sel - 5, 1);
751 fl_set_browser_topline(pFileDlgForm->List, top);
753 // checks whether dialog can be started
754 if (pCurrentDlg) return string();
758 SetInfoLine(string());
759 fl_set_input(pFileDlgForm->Filename, suggested.c_str());
760 fl_set_button(pFileDlgForm->Cancel, 0);
761 fl_set_button(pFileDlgForm->Ready, 0);
762 fl_set_focus_object(pFileDlgForm->form, pFileDlgForm->Filename);
763 fl_deactivate_all_forms();
764 fl_show_form(pFileDlgForm->form,
765 FL_PLACE_MOUSE | FL_FREE_SIZE, 0,
770 fl_hide_form(pFileDlgForm->form);
771 fl_activate_all_forms();
774 // Returns filename or string() if no valid selection was made
775 if (!isOk || !fl_get_input(pFileDlgForm->Filename)[0]) return string();
777 pszFileName = fl_get_input(pFileDlgForm->Filename);
779 if (!AbsolutePath(pszFileName)) {
780 pszFileName = AddName(fl_get_input(pFileDlgForm->DirBox),