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 FileInfo fileInfo(File, true);
282 // can this really happen?
283 if (!fileInfo.isOK())
286 fileInfo.modeString(szMode);
287 unsigned int nlink = fileInfo.getNumberOfLinks();
288 string user = lyxUserCache.find(fileInfo.getUid());
289 string group = lyxGroupCache.find(fileInfo.getGid());
291 time_t modtime = fileInfo.getModificationTime();
292 string Time = ctime(&modtime);
294 if (curTime > modtime + SIX_MONTH_SEC
295 || curTime < modtime + ONE_HOUR_SEC) {
296 // The file is fairly old or in the future. POSIX says
297 // the cutoff is 6 months old. Allow a 1 hour slop
298 // factor for what is considered "the future", to
299 // allow for NFS server/client clock disagreement.
300 // Show the year instead of the time of day.
302 Time.erase(15, string::npos);
304 Time.erase(16, string::npos);
307 string Buffer = string(szMode) + ' ' +
311 Time.substr(4, string::npos) + ' ';
313 Buffer += pDirEntry->d_name;
314 Buffer += fileInfo.typeIndicator();
316 if ((isLink = fileInfo.isLink())) {
319 if (LyXReadLink(File, Link)) {
323 // This gives the FileType of the file that
324 // is really pointed too after resolving all
325 // symlinks. This is not necessarily the same
326 // as the type of Link (which could again be a
327 // link). Is that intended?
329 fileInfo.newFile(File);
331 Buffer += fileInfo.typeIndicator();
337 // filters files according to pattern and type
338 if (fileInfo.isRegular()
340 || fileInfo.isBlock()
341 || fileInfo.isFifo()) {
342 if (!regexMatch(fname, pszMask))
344 } else if (!(isDir = fileInfo.isDir()))
349 // Note pszLsEntry is an string!
350 tmp.pszLsEntry = Buffer;
353 if (isDir) temp += '/';
356 // creates displayed name
357 temp = pDirEntry->d_name;
361 temp += fileInfo.typeIndicator();
362 tmp.pszDisplayed = temp;
364 direntries.push_back(tmp);
367 closedir(pDirectory);
370 sort(direntries.begin(), direntries.end(), comp_direntry());
372 // Add them to directory box
373 for (DirEntries::const_iterator cit = direntries.begin();
374 cit != direntries.end(); ++cit) {
375 string const temp = line + cit->pszDisplayed;
376 fl_add_browser_line(pFileDlgForm->List, temp.c_str());
378 fl_set_browser_topline(pFileDlgForm->List, iDepth);
379 fl_show_object(pFileDlgForm->List);
384 // SetDirectory: sets dialog current directory
385 void FileDialog::Private::SetDirectory(string const & Path)
393 tmp = MakeAbsPath(ExpandPath(Path), pszDirectory);
395 // must check the directory exists
396 DIR * pDirectory = ::opendir(tmp.c_str());
398 Alert::err_alert(_("Warning! Couldn't open directory."), tmp);
400 ::closedir(pDirectory);
406 // SetMask: sets dialog file mask
407 void FileDialog::Private::SetMask(string const & NewMask)
410 fl_set_input(pFileDlgForm->PatBox, pszMask.c_str());
414 // SetInfoLine: sets dialog information line
415 void FileDialog::Private::SetInfoLine(string const & Line)
418 fl_set_object_label(pFileDlgForm->FileInfo, pszInfoLine.c_str());
422 FileDialog::Private::Private()
424 pszDirectory = MakeAbsPath(string("."));
427 // Creates form if necessary.
429 pFileDlgForm = build_filedialog();
430 // Set callbacks. This means that we don't need a patch file
431 fl_set_object_callback(pFileDlgForm->DirBox,
432 C_LyXFileDlg_FileDlgCB, 0);
433 fl_set_object_callback(pFileDlgForm->PatBox,
434 C_LyXFileDlg_FileDlgCB, 1);
435 fl_set_object_callback(pFileDlgForm->List,
436 C_LyXFileDlg_FileDlgCB, 2);
437 fl_set_object_callback(pFileDlgForm->Filename,
438 C_LyXFileDlg_FileDlgCB, 3);
439 fl_set_object_callback(pFileDlgForm->Rescan,
440 C_LyXFileDlg_FileDlgCB, 10);
441 fl_set_object_callback(pFileDlgForm->Home,
442 C_LyXFileDlg_FileDlgCB, 11);
443 fl_set_object_callback(pFileDlgForm->User1,
444 C_LyXFileDlg_FileDlgCB, 12);
445 fl_set_object_callback(pFileDlgForm->User2,
446 C_LyXFileDlg_FileDlgCB, 13);
448 // Make sure pressing the close box doesn't crash LyX. (RvdK)
449 fl_set_form_atclose(pFileDlgForm->form,
450 C_LyXFileDlg_CancelCB, 0);
451 // Register doubleclick callback
452 fl_set_browser_dblclick_callback(pFileDlgForm->List,
453 C_LyXFileDlg_DoubleClickCB,
456 fl_hide_object(pFileDlgForm->User1);
457 fl_hide_object(pFileDlgForm->User2);
459 r_ = Dialogs::redrawGUI.connect(SigC::slot(this, &FileDialog::Private::redraw));
463 FileDialog::Private::~Private()
469 void FileDialog::Private::redraw()
471 if (pFileDlgForm->form && pFileDlgForm->form->visible)
472 fl_redraw_form(pFileDlgForm->form);
476 // SetButton: sets file selector user button action
477 void FileDialog::Private::SetButton(int iIndex, string const & pszName,
478 string const & pszPath)
484 pObject = pFileDlgForm->User1;
485 pTemp = &pszUserPath1;
486 } else if (iIndex == 1) {
487 pObject = pFileDlgForm->User2;
488 pTemp = &pszUserPath2;
491 if (!pszName.empty()) {
492 fl_set_object_label(pObject, idex(pszName.c_str()));
493 fl_set_button_shortcut(pObject, scex(pszName.c_str()), 1);
494 fl_show_object(pObject);
497 fl_hide_object(pObject);
503 // GetDirectory: gets last dialog directory
504 string const FileDialog::Private::GetDirectory() const
506 if (!pszDirectory.empty())
513 bool x_sync_kludge(bool ret)
515 XSync(fl_get_display(), false);
520 // RunDialog: handle dialog during file selection
521 bool FileDialog::Private::RunDialog()
523 force_cancel = false;
528 FL_OBJECT * pObject = fl_do_forms();
530 if (pObject == pFileDlgForm->Ready) {
532 return x_sync_kludge(true);
534 } else if (pObject == pFileDlgForm->Cancel
536 return x_sync_kludge(false);
539 return x_sync_kludge(true);
544 // XForms objects callback (static)
545 void FileDialog::Private::FileDlgCB(FL_OBJECT *, long lArgument)
547 if (!pCurrentDlg) return;
551 case 0: // get directory
552 pCurrentDlg->SetDirectory(fl_get_input(pFileDlgForm->DirBox));
553 pCurrentDlg->Reread();
557 pCurrentDlg->SetMask(fl_get_input(pFileDlgForm->PatBox));
558 pCurrentDlg->Reread();
562 pCurrentDlg->HandleListHit();
566 pCurrentDlg->SetDirectory(fl_get_input(pFileDlgForm->DirBox));
567 pCurrentDlg->SetMask(fl_get_input(pFileDlgForm->PatBox));
568 pCurrentDlg->Reread();
572 pCurrentDlg->SetDirectory(GetEnvPath("HOME"));
573 pCurrentDlg->SetMask(fl_get_input(pFileDlgForm->PatBox));
574 pCurrentDlg->Reread();
577 case 12: // user button 1
578 pCurrentDlg->SetDirectory(pCurrentDlg->pszUserPath1);
579 pCurrentDlg->SetMask(fl_get_input(pFileDlgForm
581 pCurrentDlg->Reread();
584 case 13: // user button 2
585 pCurrentDlg->SetDirectory(pCurrentDlg->pszUserPath2);
586 pCurrentDlg->SetMask(fl_get_input(pFileDlgForm
588 pCurrentDlg->Reread();
595 // Handle callback from list
596 void FileDialog::Private::HandleListHit()
599 int const iSelect = fl_get_browser(pFileDlgForm->List);
600 if (iSelect > iDepth) {
601 SetInfoLine(direntries[iSelect - iDepth - 1].pszLsEntry);
603 SetInfoLine(string());
608 // Callback for double click in list
609 void FileDialog::Private::DoubleClickCB(FL_OBJECT *, long)
611 if (pCurrentDlg->HandleDoubleClick())
612 // Simulate click on OK button
613 pCurrentDlg->Force(false);
617 // Handle double click from list
618 bool FileDialog::Private::HandleDoubleClick()
624 int const iSelect = fl_get_browser(pFileDlgForm->List);
625 if (iSelect > iDepth) {
626 pszTemp = direntries[iSelect - iDepth - 1].pszName;
627 SetInfoLine(direntries[iSelect - iDepth - 1].pszLsEntry);
628 if (!suffixIs(pszTemp, '/')) {
630 fl_set_input(pFileDlgForm->Filename, pszTemp.c_str());
632 } else if (iSelect != 0) {
633 SetInfoLine(string());
641 // builds new directory name
642 if (iSelect > iDepth) {
643 // Directory deeper down
644 // First, get directory with trailing /
645 Temp = fl_get_input(pFileDlgForm->DirBox);
646 if (!suffixIs(Temp, '/'))
650 // Directory higher up
652 for (int i = 0; i < iSelect; ++i) {
653 string piece = fl_get_browser_line(pFileDlgForm->List, i+1);
654 // The '+2' is here to count the '@b' (JMarc)
655 Temp += piece.substr(i + 2);
668 // Handle OK button call
669 bool FileDialog::Private::HandleOK()
672 string pszTemp = fl_get_input(pFileDlgForm->PatBox);
673 if (pszTemp != pszMask) {
679 // directory was changed
680 pszTemp = fl_get_input(pFileDlgForm->DirBox);
681 if (pszTemp!= pszDirectory) {
682 SetDirectory(pszTemp);
687 // Handle return from list
688 int const select = fl_get_browser(pFileDlgForm->List);
689 if (select > iDepth) {
690 string const temp = direntries[select - iDepth - 1].pszName;
691 if (!suffixIs(temp, '/')) {
692 // If user didn't type anything, use browser
694 fl_get_input(pFileDlgForm->Filename);
696 fl_set_input(pFileDlgForm->Filename, temp.c_str());
702 // Emulate a doubleclick
703 return HandleDoubleClick();
707 // Handle Cancel CB from WM close
708 int FileDialog::Private::CancelCB(FL_FORM *, void *)
710 // Simulate a click on the cancel button
711 pCurrentDlg->Force(true);
716 // Simulates a click on OK/Cancel
717 void FileDialog::Private::Force(bool cancel)
721 fl_set_button(pFileDlgForm->Cancel, 1);
724 fl_set_button(pFileDlgForm->Ready, 1);
726 // Start timer to break fl_do_forms loop soon
727 fl_set_timer(pFileDlgForm->timer, 0.1);
731 // Select: launches dialog and returns selected file
732 string const FileDialog::Private::Select(string const & title,
735 string const & suggested)
737 // handles new mask and path
749 // highlight the suggested file in the browser, if it exists.
751 string const filename = OnlyFilename(suggested);
752 if (!filename.empty()) {
754 i < fl_get_browser_maxline(pFileDlgForm->List); ++i) {
756 fl_get_browser_line(pFileDlgForm->List, i + 1);
757 s = strip(frontStrip(s));
765 if (sel != 0) fl_select_browser_line(pFileDlgForm->List, sel);
766 int const top = max(sel - 5, 1);
767 fl_set_browser_topline(pFileDlgForm->List, top);
769 // checks whether dialog can be started
770 if (pCurrentDlg) return string();
774 SetInfoLine(string());
775 fl_set_input(pFileDlgForm->Filename, suggested.c_str());
776 fl_set_button(pFileDlgForm->Cancel, 0);
777 fl_set_button(pFileDlgForm->Ready, 0);
778 fl_set_focus_object(pFileDlgForm->form, pFileDlgForm->Filename);
779 fl_deactivate_all_forms();
780 fl_show_form(pFileDlgForm->form,
781 FL_PLACE_MOUSE | FL_FREE_SIZE, 0,
786 fl_hide_form(pFileDlgForm->form);
787 fl_activate_all_forms();
790 // Returns filename or string() if no valid selection was made
791 if (!isOk || !fl_get_input(pFileDlgForm->Filename)[0]) return string();
793 pszFileName = fl_get_input(pFileDlgForm->Filename);
795 if (!AbsolutePath(pszFileName)) {
796 pszFileName = AddName(fl_get_input(pFileDlgForm->DirBox),