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)
379 if (!pszDirectory.empty()) {
380 string TempPath = ExpandPath(Path); // Expand ~/
381 TempPath = MakeAbsPath(TempPath, pszDirectory);
382 pszDirectory = MakeAbsPath(TempPath);
383 } else pszDirectory = MakeAbsPath(Path);
387 // SetMask: sets dialog file mask
388 void FileDialog::Private::SetMask(string const & NewMask)
391 fl_set_input(pFileDlgForm->PatBox, pszMask.c_str());
395 // SetInfoLine: sets dialog information line
396 void FileDialog::Private::SetInfoLine(string const & Line)
399 fl_set_object_label(pFileDlgForm->FileInfo, pszInfoLine.c_str());
403 FileDialog::Private::Private()
405 pszDirectory = MakeAbsPath(string("."));
408 // Creates form if necessary.
410 pFileDlgForm = build_filedialog();
411 // Set callbacks. This means that we don't need a patch file
412 fl_set_object_callback(pFileDlgForm->DirBox,
413 C_LyXFileDlg_FileDlgCB, 0);
414 fl_set_object_callback(pFileDlgForm->PatBox,
415 C_LyXFileDlg_FileDlgCB, 1);
416 fl_set_object_callback(pFileDlgForm->List,
417 C_LyXFileDlg_FileDlgCB, 2);
418 fl_set_object_callback(pFileDlgForm->Filename,
419 C_LyXFileDlg_FileDlgCB, 3);
420 fl_set_object_callback(pFileDlgForm->Rescan,
421 C_LyXFileDlg_FileDlgCB, 10);
422 fl_set_object_callback(pFileDlgForm->Home,
423 C_LyXFileDlg_FileDlgCB, 11);
424 fl_set_object_callback(pFileDlgForm->User1,
425 C_LyXFileDlg_FileDlgCB, 12);
426 fl_set_object_callback(pFileDlgForm->User2,
427 C_LyXFileDlg_FileDlgCB, 13);
429 // Make sure pressing the close box doesn't crash LyX. (RvdK)
430 fl_set_form_atclose(pFileDlgForm->form,
431 C_LyXFileDlg_CancelCB, 0);
432 // Register doubleclick callback
433 fl_set_browser_dblclick_callback(pFileDlgForm->List,
434 C_LyXFileDlg_DoubleClickCB,
437 fl_hide_object(pFileDlgForm->User1);
438 fl_hide_object(pFileDlgForm->User2);
440 r_ = Dialogs::redrawGUI.connect(SigC::slot(this, &FileDialog::Private::redraw));
444 FileDialog::Private::~Private()
450 void FileDialog::Private::redraw()
452 if (pFileDlgForm->form && pFileDlgForm->form->visible)
453 fl_redraw_form(pFileDlgForm->form);
457 // SetButton: sets file selector user button action
458 void FileDialog::Private::SetButton(int iIndex, string const & pszName,
459 string const & pszPath)
465 pObject = pFileDlgForm->User1;
466 pTemp = &pszUserPath1;
467 } else if (iIndex == 1) {
468 pObject = pFileDlgForm->User2;
469 pTemp = &pszUserPath2;
472 if (!pszName.empty() && !pszPath.empty()) {
473 fl_set_object_label(pObject, pszName.c_str());
474 fl_show_object(pObject);
477 fl_hide_object(pObject);
483 // GetDirectory: gets last dialog directory
484 string const FileDialog::Private::GetDirectory() const
486 if (!pszDirectory.empty())
493 // RunDialog: handle dialog during file selection
494 bool FileDialog::Private::RunDialog()
496 force_cancel = false;
501 FL_OBJECT * pObject = fl_do_forms();
503 if (pObject == pFileDlgForm->Ready) {
506 } else if (pObject == pFileDlgForm->Cancel
515 // XForms objects callback (static)
516 void FileDialog::Private::FileDlgCB(FL_OBJECT *, long lArgument)
518 if (!pCurrentDlg) return;
522 case 0: // get directory
523 pCurrentDlg->SetDirectory(fl_get_input(pFileDlgForm->DirBox));
524 pCurrentDlg->Reread();
528 pCurrentDlg->SetMask(fl_get_input(pFileDlgForm->PatBox));
529 pCurrentDlg->Reread();
533 pCurrentDlg->HandleListHit();
537 pCurrentDlg->SetDirectory(fl_get_input(pFileDlgForm->DirBox));
538 pCurrentDlg->SetMask(fl_get_input(pFileDlgForm->PatBox));
539 pCurrentDlg->Reread();
543 pCurrentDlg->SetDirectory(GetEnvPath("HOME"));
544 pCurrentDlg->SetMask(fl_get_input(pFileDlgForm->PatBox));
545 pCurrentDlg->Reread();
548 case 12: // user button 1
549 if (!pCurrentDlg->pszUserPath1.empty()) {
550 pCurrentDlg->SetDirectory(pCurrentDlg->pszUserPath1);
551 pCurrentDlg->SetMask(fl_get_input(pFileDlgForm
553 pCurrentDlg->Reread();
557 case 13: // user button 2
558 if (!pCurrentDlg->pszUserPath2.empty()) {
559 pCurrentDlg->SetDirectory(pCurrentDlg->pszUserPath2);
560 pCurrentDlg->SetMask(fl_get_input(pFileDlgForm
562 pCurrentDlg->Reread();
570 // Handle callback from list
571 void FileDialog::Private::HandleListHit()
574 int const iSelect = fl_get_browser(pFileDlgForm->List);
575 if (iSelect > iDepth) {
576 SetInfoLine(direntries[iSelect - iDepth - 1].pszLsEntry);
578 SetInfoLine(string());
583 // Callback for double click in list
584 void FileDialog::Private::DoubleClickCB(FL_OBJECT *, long)
586 if (pCurrentDlg->HandleDoubleClick())
587 // Simulate click on OK button
588 pCurrentDlg->Force(false);
592 // Handle double click from list
593 bool FileDialog::Private::HandleDoubleClick()
599 int const iSelect = fl_get_browser(pFileDlgForm->List);
600 if (iSelect > iDepth) {
601 pszTemp = direntries[iSelect - iDepth - 1].pszName;
602 SetInfoLine(direntries[iSelect - iDepth - 1].pszLsEntry);
603 if (!suffixIs(pszTemp, '/')) {
605 fl_set_input(pFileDlgForm->Filename, pszTemp.c_str());
607 } else if (iSelect != 0) {
608 SetInfoLine(string());
616 // builds new directory name
617 if (iSelect > iDepth) {
618 // Directory deeper down
619 // First, get directory with trailing /
620 Temp = fl_get_input(pFileDlgForm->DirBox);
621 if (!suffixIs(Temp, '/'))
625 // Directory higher up
627 for (int i = 0; i < iSelect; ++i) {
628 string piece = fl_get_browser_line(pFileDlgForm->List, i+1);
629 // The '+2' is here to count the '@b' (JMarc)
630 Temp += piece.substr(i + 2);
643 // Handle OK button call
644 bool FileDialog::Private::HandleOK()
647 string pszTemp = fl_get_input(pFileDlgForm->PatBox);
648 if (pszTemp != pszMask) {
654 // directory was changed
655 pszTemp = fl_get_input(pFileDlgForm->DirBox);
656 if (pszTemp!= pszDirectory) {
657 SetDirectory(pszTemp);
662 // Handle return from list
663 int const select = fl_get_browser(pFileDlgForm->List);
664 if (select > iDepth) {
665 string const temp = direntries[select - iDepth - 1].pszName;
666 if (!suffixIs(temp, '/')) {
667 // If user didn't type anything, use browser
669 fl_get_input(pFileDlgForm->Filename);
671 fl_set_input(pFileDlgForm->Filename, temp.c_str());
677 // Emulate a doubleclick
678 return HandleDoubleClick();
682 // Handle Cancel CB from WM close
683 int FileDialog::Private::CancelCB(FL_FORM *, void *)
685 // Simulate a click on the cancel button
686 pCurrentDlg->Force(true);
691 // Simulates a click on OK/Cancel
692 void FileDialog::Private::Force(bool cancel)
696 fl_set_button(pFileDlgForm->Cancel, 1);
699 fl_set_button(pFileDlgForm->Ready, 1);
701 // Start timer to break fl_do_forms loop soon
702 fl_set_timer(pFileDlgForm->timer, 0.1);
706 // Select: launches dialog and returns selected file
707 string const FileDialog::Private::Select(string const & title, string const & path,
708 string const & mask, string const & suggested)
710 // handles new mask and path
722 // highlight the suggested file in the browser, if it exists.
724 string const filename = OnlyFilename(suggested);
725 if (!filename.empty()) {
727 i < fl_get_browser_maxline(pFileDlgForm->List); ++i) {
729 fl_get_browser_line(pFileDlgForm->List, i + 1);
730 s = strip(frontStrip(s));
738 if (sel != 0) fl_select_browser_line(pFileDlgForm->List, sel);
739 int const top = max(sel - 5, 1);
740 fl_set_browser_topline(pFileDlgForm->List, top);
742 // checks whether dialog can be started
743 if (pCurrentDlg) return string();
747 SetInfoLine(string());
748 fl_set_input(pFileDlgForm->Filename, suggested.c_str());
749 fl_set_button(pFileDlgForm->Cancel, 0);
750 fl_set_button(pFileDlgForm->Ready, 0);
751 fl_set_focus_object(pFileDlgForm->form, pFileDlgForm->Filename);
752 fl_deactivate_all_forms();
753 fl_show_form(pFileDlgForm->form,
754 FL_PLACE_MOUSE | FL_FREE_SIZE, 0,
759 fl_hide_form(pFileDlgForm->form);
760 fl_activate_all_forms();
763 // Returns filename or string() if no valid selection was made
764 if (!isOk || !fl_get_input(pFileDlgForm->Filename)[0]) return string();
766 pszFileName = fl_get_input(pFileDlgForm->Filename);
768 if (!AbsolutePath(pszFileName)) {
769 pszFileName = AddName(fl_get_input(pFileDlgForm->DirBox),