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"
28 #include "frontends/Dialogs.h"
36 # define NAMLEN(dirent) strlen((dirent)->d_name)
38 # define dirent direct
39 # define NAMLEN(dirent) (dirent)->d_namlen
41 # include <sys/ndir.h>
51 #if TIME_WITH_SYS_TIME
52 # include <sys/time.h>
56 # include <sys/time.h>
62 // FIXME: should be autoconfiscated
64 extern "C" int gettimeofday(struct timeval *, struct timezone *);
68 #pragma implementation
71 #include "support/filetools.h"
72 #include "FormFiledialog.h"
76 // six months, in seconds
77 long const SIX_MONTH_SEC = 6L * 30L * 24L * 60L * 60L;
79 long const ONE_HOUR_SEC = 60L * 60L;
84 // *** User cache class implementation
85 /// User cache class definition
88 /// seeks user name from group ID
89 string const & find(uid_t ID) const {
90 Users::const_iterator cit = users.find(ID);
91 if (cit == users.end()) {
99 void add(uid_t ID) const;
101 typedef map<uid_t, string> Users;
107 void UserCache::add(uid_t ID) const
110 struct passwd * pEntry;
113 if ((pEntry = getpwuid(ID)))
114 pszNewName = pEntry->pw_name;
116 pszNewName = tostr(ID);
120 users[ID] = pszNewName;
124 /// Group cache class definition
127 /// seeks group name from group ID
128 string const & find(gid_t ID) const ;
131 void add(gid_t ID) const;
133 typedef map<gid_t, string> Groups;
135 mutable Groups groups;
139 string const & GroupCache::find(gid_t ID) const
141 Groups::const_iterator cit = groups.find(ID);
142 if (cit == groups.end()) {
146 return (*cit).second;
150 void GroupCache::add(gid_t ID) const
153 struct group * pEntry;
156 if ((pEntry = getgrgid(ID))) pszNewName = pEntry->gr_name;
158 pszNewName = tostr(ID);
161 groups[ID] = pszNewName;
168 UserCache lyxUserCache;
169 GroupCache lyxGroupCache;
174 // compares two LyXDirEntry objects content (used for sort)
175 class comp_direntry {
177 int operator()(DirEntry const & r1,
178 DirEntry const & r2) const ;
180 int comp_direntry::operator()(DirEntry const & r1,
181 DirEntry const & r2) const {
182 bool r1d = suffixIs(r1.pszName, '/');
183 bool r2d = suffixIs(r2.pszName, '/');
184 if (r1d && !r2d) return 1;
185 if (!r1d && r2d) return 0;
186 return r1.pszName < r2.pszName;
190 // *** FileDialog::Private class implementation
193 FD_form_filedialog * FileDialog::Private::pFileDlgForm = 0;
194 FileDialog::Private * FileDialog::Private::pCurrentDlg = 0;
197 // Reread: updates dialog list to match class directory
198 void FileDialog::Private::Reread()
201 DIR * pDirectory = ::opendir(pszDirectory.c_str());
203 WriteFSAlert(_("Warning! Couldn't open directory."),
205 pszDirectory = lyx::getcwd();
206 pDirectory = ::opendir(pszDirectory.c_str());
209 // Clear the present namelist
213 fl_hide_object(pFileDlgForm->List);
214 fl_clear_browser(pFileDlgForm->List);
215 fl_set_input(pFileDlgForm->DirBox, pszDirectory.c_str());
217 // Splits complete directory name into directories and compute depth
222 string File = pszDirectory;
224 File = split(File, Temp, '/');
226 while (!File.empty() || !Temp.empty()) {
227 string dline = "@b"+line + Temp + '/';
228 fl_add_browser_line(pFileDlgForm->List, dline.c_str());
229 File = split(File, Temp, '/');
234 // Parses all entries of the given subdirectory
235 time_t curTime = time(0);
236 rewinddir(pDirectory);
237 struct dirent * pDirEntry;
238 while ((pDirEntry = readdir(pDirectory))) {
239 bool isLink = false, isDir = false;
241 // If the pattern doesn't start with a dot, skip hidden files
242 if (!pszMask.empty() && pszMask[0] != '.' &&
243 pDirEntry->d_name[0] == '.')
247 string fname = pDirEntry->d_name;
249 // Under all circumstances, "." and ".." are not wanted
250 if (fname == "." || fname == "..")
254 File = AddName(pszDirectory, fname);
256 fileInfo.newFile(File, true);
257 fileInfo.modeString(szMode);
258 unsigned int nlink = fileInfo.getNumberOfLinks();
259 string user = lyxUserCache.find(fileInfo.getUid());
260 string group = lyxGroupCache.find(fileInfo.getGid());
262 time_t modtime = fileInfo.getModificationTime();
263 string Time = ctime(&modtime);
265 if (curTime > fileInfo.getModificationTime() + SIX_MONTH_SEC
266 || curTime < fileInfo.getModificationTime()
268 // The file is fairly old or in the future. POSIX says
269 // the cutoff is 6 months old. Allow a 1 hour slop
270 // factor for what is considered "the future", to
271 // allow for NFS server/client clock disagreement.
272 // Show the year instead of the time of day.
274 Time.erase(15, string::npos);
276 Time.erase(16, string::npos);
279 string Buffer = string(szMode) + ' ' +
283 Time.substr(4, string::npos) + ' ';
285 Buffer += pDirEntry->d_name;
286 Buffer += fileInfo.typeIndicator();
288 if ((isLink = fileInfo.isLink())) {
291 if (LyXReadLink(File, Link)) {
295 // This gives the FileType of the file that
296 // is really pointed too after resolving all
297 // symlinks. This is not necessarily the same
298 // as the type of Link (which could again be a
299 // link). Is that intended?
301 fileInfo.newFile(File);
302 Buffer += fileInfo.typeIndicator();
306 // filters files according to pattern and type
307 if (fileInfo.isRegular()
309 || fileInfo.isBlock()
310 || fileInfo.isFifo()) {
311 if (!regexMatch(fname, pszMask))
313 } else if (!(isDir = fileInfo.isDir()))
318 // Note pszLsEntry is an string!
319 tmp.pszLsEntry = Buffer;
322 if (isDir) temp += '/';
325 // creates displayed name
326 temp = pDirEntry->d_name;
330 temp += fileInfo.typeIndicator();
331 tmp.pszDisplayed = temp;
333 direntries.push_back(tmp);
336 closedir(pDirectory);
339 sort(direntries.begin(), direntries.end(), comp_direntry());
341 // Add them to directory box
342 for (DirEntries::const_iterator cit = direntries.begin();
343 cit != direntries.end(); ++cit) {
344 string const temp = line + (*cit).pszDisplayed;
345 fl_add_browser_line(pFileDlgForm->List, temp.c_str());
347 fl_set_browser_topline(pFileDlgForm->List, iDepth);
348 fl_show_object(pFileDlgForm->List);
353 // SetDirectory: sets dialog current directory
354 void FileDialog::Private::SetDirectory(string const & Path)
356 if (!pszDirectory.empty()) {
357 string TempPath = ExpandPath(Path); // Expand ~/
358 TempPath = MakeAbsPath(TempPath, pszDirectory);
359 pszDirectory = MakeAbsPath(TempPath);
360 } else pszDirectory = MakeAbsPath(Path);
364 // SetMask: sets dialog file mask
365 void FileDialog::Private::SetMask(string const & NewMask)
368 fl_set_input(pFileDlgForm->PatBox, pszMask.c_str());
372 // SetInfoLine: sets dialog information line
373 void FileDialog::Private::SetInfoLine(string const & Line)
376 fl_set_object_label(pFileDlgForm->FileInfo, pszInfoLine.c_str());
380 FileDialog::Private::Private()
382 pszDirectory = MakeAbsPath(string("."));
385 // Creates form if necessary.
387 pFileDlgForm = build_filedialog();
388 // Set callbacks. This means that we don't need a patch file
389 fl_set_object_callback(pFileDlgForm->DirBox,
390 C_LyXFileDlg_FileDlgCB, 0);
391 fl_set_object_callback(pFileDlgForm->PatBox,
392 C_LyXFileDlg_FileDlgCB, 1);
393 fl_set_object_callback(pFileDlgForm->List,
394 C_LyXFileDlg_FileDlgCB, 2);
395 fl_set_object_callback(pFileDlgForm->Filename,
396 C_LyXFileDlg_FileDlgCB, 3);
397 fl_set_object_callback(pFileDlgForm->Rescan,
398 C_LyXFileDlg_FileDlgCB, 10);
399 fl_set_object_callback(pFileDlgForm->Home,
400 C_LyXFileDlg_FileDlgCB, 11);
401 fl_set_object_callback(pFileDlgForm->User1,
402 C_LyXFileDlg_FileDlgCB, 12);
403 fl_set_object_callback(pFileDlgForm->User2,
404 C_LyXFileDlg_FileDlgCB, 13);
406 // Make sure pressing the close box doesn't crash LyX. (RvdK)
407 fl_set_form_atclose(pFileDlgForm->form,
408 C_LyXFileDlg_CancelCB, 0);
409 // Register doubleclick callback
410 fl_set_browser_dblclick_callback(pFileDlgForm->List,
411 C_LyXFileDlg_DoubleClickCB,
414 fl_hide_object(pFileDlgForm->User1);
415 fl_hide_object(pFileDlgForm->User2);
417 r_ = Dialogs::redrawGUI.connect(SigC::slot(this, &FileDialog::Private::redraw));
421 FileDialog::Private::~Private()
427 void FileDialog::Private::redraw()
429 if (pFileDlgForm->form && pFileDlgForm->form->visible)
430 fl_redraw_form(pFileDlgForm->form);
434 // SetButton: sets file selector user button action
435 void FileDialog::Private::SetButton(int iIndex, string const & pszName,
436 string const & pszPath)
442 pObject = pFileDlgForm->User1;
443 pTemp = &pszUserPath1;
444 } else if (iIndex == 1) {
445 pObject = pFileDlgForm->User2;
446 pTemp = &pszUserPath2;
449 if (!pszName.empty() && !pszPath.empty()) {
450 fl_set_object_label(pObject, pszName.c_str());
451 fl_show_object(pObject);
454 fl_hide_object(pObject);
460 // GetDirectory: gets last dialog directory
461 string const FileDialog::Private::GetDirectory() const
463 if (!pszDirectory.empty())
470 // RunDialog: handle dialog during file selection
471 bool FileDialog::Private::RunDialog()
473 force_cancel = false;
478 FL_OBJECT * pObject = fl_do_forms();
480 if (pObject == pFileDlgForm->Ready) {
483 } else if (pObject == pFileDlgForm->Cancel
492 // XForms objects callback (static)
493 void FileDialog::Private::FileDlgCB(FL_OBJECT *, long lArgument)
495 if (!pCurrentDlg) return;
499 case 0: // get directory
500 pCurrentDlg->SetDirectory(fl_get_input(pFileDlgForm->DirBox));
501 pCurrentDlg->Reread();
505 pCurrentDlg->SetMask(fl_get_input(pFileDlgForm->PatBox));
506 pCurrentDlg->Reread();
510 pCurrentDlg->HandleListHit();
514 pCurrentDlg->SetDirectory(fl_get_input(pFileDlgForm->DirBox));
515 pCurrentDlg->SetMask(fl_get_input(pFileDlgForm->PatBox));
516 pCurrentDlg->Reread();
520 pCurrentDlg->SetDirectory(GetEnvPath("HOME"));
521 pCurrentDlg->SetMask(fl_get_input(pFileDlgForm->PatBox));
522 pCurrentDlg->Reread();
525 case 12: // user button 1
526 if (!pCurrentDlg->pszUserPath1.empty()) {
527 pCurrentDlg->SetDirectory(pCurrentDlg->pszUserPath1);
528 pCurrentDlg->SetMask(fl_get_input(pFileDlgForm
530 pCurrentDlg->Reread();
534 case 13: // user button 2
535 if (!pCurrentDlg->pszUserPath2.empty()) {
536 pCurrentDlg->SetDirectory(pCurrentDlg->pszUserPath2);
537 pCurrentDlg->SetMask(fl_get_input(pFileDlgForm
539 pCurrentDlg->Reread();
547 extern "C" void C_LyXFileDlg_FileDlgCB(FL_OBJECT * ob, long data)
549 FileDialog::Private::FileDlgCB(ob, data);
553 // Handle callback from list
554 void FileDialog::Private::HandleListHit()
557 int const iSelect = fl_get_browser(pFileDlgForm->List);
558 if (iSelect > iDepth) {
559 SetInfoLine(direntries[iSelect - iDepth - 1].pszLsEntry);
561 SetInfoLine(string());
566 // Callback for double click in list
567 void FileDialog::Private::DoubleClickCB(FL_OBJECT *, long)
569 if (pCurrentDlg->HandleDoubleClick())
570 // Simulate click on OK button
571 pCurrentDlg->Force(false);
575 extern "C" void C_LyXFileDlg_DoubleClickCB(FL_OBJECT * ob, long data)
577 FileDialog::Private::DoubleClickCB(ob, data);
581 // Handle double click from list
582 bool FileDialog::Private::HandleDoubleClick()
588 int const iSelect = fl_get_browser(pFileDlgForm->List);
589 if (iSelect > iDepth) {
590 pszTemp = direntries[iSelect - iDepth - 1].pszName;
591 SetInfoLine(direntries[iSelect - iDepth - 1].pszLsEntry);
592 if (!suffixIs(pszTemp, '/')) {
594 fl_set_input(pFileDlgForm->Filename, pszTemp.c_str());
596 } else if (iSelect != 0) {
597 SetInfoLine(string());
605 // builds new directory name
606 if (iSelect > iDepth) {
607 // Directory deeper down
608 // First, get directory with trailing /
609 Temp = fl_get_input(pFileDlgForm->DirBox);
610 if (!suffixIs(Temp, '/'))
614 // Directory higher up
616 for (int i = 0; i < iSelect; ++i) {
617 string piece = fl_get_browser_line(pFileDlgForm->List, i+1);
618 // The '+2' is here to count the '@b' (JMarc)
619 Temp += piece.substr(i + 2);
632 // Handle OK button call
633 bool FileDialog::Private::HandleOK()
636 string pszTemp = fl_get_input(pFileDlgForm->PatBox);
637 if (pszTemp != pszMask) {
643 // directory was changed
644 pszTemp = fl_get_input(pFileDlgForm->DirBox);
645 if (pszTemp!= pszDirectory) {
646 SetDirectory(pszTemp);
651 // Handle return from list
652 int const select = fl_get_browser(pFileDlgForm->List);
653 if (select > iDepth) {
654 string const temp = direntries[select - iDepth - 1].pszName;
655 if (!suffixIs(temp, '/')) {
656 // If user didn't type anything, use browser
658 fl_get_input(pFileDlgForm->Filename);
660 fl_set_input(pFileDlgForm->Filename, temp.c_str());
666 // Emulate a doubleclick
667 return HandleDoubleClick();
671 // Handle Cancel CB from WM close
672 int FileDialog::Private::CancelCB(FL_FORM *, void *)
674 // Simulate a click on the cancel button
675 pCurrentDlg->Force(true);
680 extern "C" int C_LyXFileDlg_CancelCB(FL_FORM *fl, void *xev)
682 return FileDialog::Private::CancelCB(fl, xev);
686 // Simulates a click on OK/Cancel
687 void FileDialog::Private::Force(bool cancel)
691 fl_set_button(pFileDlgForm->Cancel, 1);
694 fl_set_button(pFileDlgForm->Ready, 1);
696 // Start timer to break fl_do_forms loop soon
697 fl_set_timer(pFileDlgForm->timer, 0.1);
701 // Select: launches dialog and returns selected file
702 string const FileDialog::Private::Select(string const & title, string const & path,
703 string const & mask, string const & suggested)
705 // handles new mask and path
717 // highlight the suggested file in the browser, if it exists.
719 string const filename = OnlyFilename(suggested);
720 if (!filename.empty()) {
722 i < fl_get_browser_maxline(pFileDlgForm->List); ++i) {
724 fl_get_browser_line(pFileDlgForm->List, i + 1);
725 s = strip(frontStrip(s));
733 if (sel != 0) fl_select_browser_line(pFileDlgForm->List, sel);
734 int const top = max(sel - 5, 1);
735 fl_set_browser_topline(pFileDlgForm->List, top);
737 // checks whether dialog can be started
738 if (pCurrentDlg) return string();
742 SetInfoLine(string());
743 fl_set_input(pFileDlgForm->Filename, suggested.c_str());
744 fl_set_button(pFileDlgForm->Cancel, 0);
745 fl_set_button(pFileDlgForm->Ready, 0);
746 fl_set_focus_object(pFileDlgForm->form, pFileDlgForm->Filename);
747 fl_deactivate_all_forms();
748 fl_show_form(pFileDlgForm->form,
749 FL_PLACE_MOUSE | FL_FREE_SIZE, 0,
754 fl_hide_form(pFileDlgForm->form);
755 fl_activate_all_forms();
758 // Returns filename or string() if no valid selection was made
759 if (!isOk || !fl_get_input(pFileDlgForm->Filename)[0]) return string();
761 pszFileName = fl_get_input(pFileDlgForm->Filename);
763 if (!AbsolutePath(pszFileName)) {
764 pszFileName = AddName(fl_get_input(pFileDlgForm->DirBox),