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"
74 // six months, in seconds
75 static const long SIX_MONTH_SEC = 6L * 30L * 24L * 60L * 60L;
76 static const long ONE_HOUR_SEC = 60L * 60L;
78 // *** User cache class implementation
79 /// User cache class definition
82 /// seeks user name from group ID
83 string const & find(uid_t ID) const {
84 Users::const_iterator cit = users.find(ID);
85 if (cit == users.end()) {
93 void add(uid_t ID) const;
95 typedef map<uid_t, string> Users;
101 void UserCache::add(uid_t ID) const
104 struct passwd * pEntry;
107 if ((pEntry = getpwuid(ID)))
108 pszNewName = pEntry->pw_name;
110 pszNewName = tostr(ID);
114 users[ID] = pszNewName;
118 /// Group cache class definition
121 /// seeks group name from group ID
122 string const & find(gid_t ID) const ;
125 void add(gid_t ID) const;
127 typedef map<gid_t, string> Groups;
129 mutable Groups groups;
133 string const & GroupCache::find(gid_t ID) const
135 Groups::const_iterator cit = groups.find(ID);
136 if (cit == groups.end()) {
140 return (*cit).second;
144 void GroupCache::add(gid_t ID) const
147 struct group * pEntry;
150 if ((pEntry = getgrgid(ID))) pszNewName = pEntry->gr_name;
152 pszNewName = tostr(ID);
155 groups[ID] = pszNewName;
160 static UserCache lyxUserCache;
161 static GroupCache lyxGroupCache;
164 // compares two LyXDirEntry objects content (used for sort)
165 class comp_direntry {
167 int operator()(DirEntry const & r1,
168 DirEntry const & r2) const ;
170 int comp_direntry::operator()(DirEntry const & r1,
171 DirEntry const & r2) const {
172 bool r1d = suffixIs(r1.pszName, '/');
173 bool r2d = suffixIs(r2.pszName, '/');
174 if (r1d && !r2d) return 1;
175 if (!r1d && r2d) return 0;
176 return r1.pszName < r2.pszName;
180 // *** FileDialog::Private class implementation
183 FD_form_filedialog * FileDialog::Private::pFileDlgForm = 0;
184 FileDialog::Private * FileDialog::Private::pCurrentDlg = 0;
187 // Reread: updates dialog list to match class directory
188 void FileDialog::Private::Reread()
191 DIR * pDirectory = ::opendir(pszDirectory.c_str());
193 WriteFSAlert(_("Warning! Couldn't open directory."),
195 pszDirectory = lyx::getcwd();
196 pDirectory = ::opendir(pszDirectory.c_str());
199 // Clear the present namelist
203 fl_hide_object(pFileDlgForm->List);
204 fl_clear_browser(pFileDlgForm->List);
205 fl_set_input(pFileDlgForm->DirBox, pszDirectory.c_str());
207 // Splits complete directory name into directories and compute depth
212 string File = pszDirectory;
214 File = split(File, Temp, '/');
216 while (!File.empty() || !Temp.empty()) {
217 string dline = "@b"+line + Temp + '/';
218 fl_add_browser_line(pFileDlgForm->List, dline.c_str());
219 File = split(File, Temp, '/');
224 // Parses all entries of the given subdirectory
225 time_t curTime = time(0);
226 rewinddir(pDirectory);
227 struct dirent * pDirEntry;
228 while ((pDirEntry = readdir(pDirectory))) {
229 bool isLink = false, isDir = false;
231 // If the pattern doesn't start with a dot, skip hidden files
232 if (!pszMask.empty() && pszMask[0] != '.' &&
233 pDirEntry->d_name[0] == '.')
237 string fname = pDirEntry->d_name;
239 // Under all circumstances, "." and ".." are not wanted
240 if (fname == "." || fname == "..")
244 File = AddName(pszDirectory, fname);
246 fileInfo.newFile(File, true);
247 fileInfo.modeString(szMode);
248 unsigned int nlink = fileInfo.getNumberOfLinks();
249 string user = lyxUserCache.find(fileInfo.getUid());
250 string group = lyxGroupCache.find(fileInfo.getGid());
252 time_t modtime = fileInfo.getModificationTime();
253 string Time = ctime(&modtime);
255 if (curTime > fileInfo.getModificationTime() + SIX_MONTH_SEC
256 || curTime < fileInfo.getModificationTime()
258 // The file is fairly old or in the future. POSIX says
259 // the cutoff is 6 months old. Allow a 1 hour slop
260 // factor for what is considered "the future", to
261 // allow for NFS server/client clock disagreement.
262 // Show the year instead of the time of day.
264 Time.erase(15, string::npos);
266 Time.erase(16, string::npos);
269 string Buffer = string(szMode) + ' ' +
273 Time.substr(4, string::npos) + ' ';
275 Buffer += pDirEntry->d_name;
276 Buffer += fileInfo.typeIndicator();
278 if ((isLink = fileInfo.isLink())) {
281 if (LyXReadLink(File, Link)) {
285 // This gives the FileType of the file that
286 // is really pointed too after resolving all
287 // symlinks. This is not necessarily the same
288 // as the type of Link (which could again be a
289 // link). Is that intended?
291 fileInfo.newFile(File);
292 Buffer += fileInfo.typeIndicator();
296 // filters files according to pattern and type
297 if (fileInfo.isRegular()
299 || fileInfo.isBlock()
300 || fileInfo.isFifo()) {
301 if (!regexMatch(fname, pszMask))
303 } else if (!(isDir = fileInfo.isDir()))
308 // Note pszLsEntry is an string!
309 tmp.pszLsEntry = Buffer;
312 if (isDir) temp += '/';
315 // creates displayed name
316 temp = pDirEntry->d_name;
320 temp += fileInfo.typeIndicator();
321 tmp.pszDisplayed = temp;
323 direntries.push_back(tmp);
326 closedir(pDirectory);
329 sort(direntries.begin(), direntries.end(), comp_direntry());
331 // Add them to directory box
332 for (DirEntries::const_iterator cit = direntries.begin();
333 cit != direntries.end(); ++cit) {
334 string const temp = line + (*cit).pszDisplayed;
335 fl_add_browser_line(pFileDlgForm->List, temp.c_str());
337 fl_set_browser_topline(pFileDlgForm->List, iDepth);
338 fl_show_object(pFileDlgForm->List);
343 // SetDirectory: sets dialog current directory
344 void FileDialog::Private::SetDirectory(string const & Path)
346 if (!pszDirectory.empty()) {
347 string TempPath = ExpandPath(Path); // Expand ~/
348 TempPath = MakeAbsPath(TempPath, pszDirectory);
349 pszDirectory = MakeAbsPath(TempPath);
350 } else pszDirectory = MakeAbsPath(Path);
354 // SetMask: sets dialog file mask
355 void FileDialog::Private::SetMask(string const & NewMask)
358 fl_set_input(pFileDlgForm->PatBox, pszMask.c_str());
362 // SetInfoLine: sets dialog information line
363 void FileDialog::Private::SetInfoLine(string const & Line)
366 fl_set_object_label(pFileDlgForm->FileInfo, pszInfoLine.c_str());
370 FileDialog::Private::Private()
372 pszDirectory = MakeAbsPath(string("."));
375 // Creates form if necessary.
377 pFileDlgForm = build_filedialog();
378 // Set callbacks. This means that we don't need a patch file
379 fl_set_object_callback(pFileDlgForm->DirBox,
380 C_LyXFileDlg_FileDlgCB, 0);
381 fl_set_object_callback(pFileDlgForm->PatBox,
382 C_LyXFileDlg_FileDlgCB, 1);
383 fl_set_object_callback(pFileDlgForm->List,
384 C_LyXFileDlg_FileDlgCB, 2);
385 fl_set_object_callback(pFileDlgForm->Filename,
386 C_LyXFileDlg_FileDlgCB, 3);
387 fl_set_object_callback(pFileDlgForm->Rescan,
388 C_LyXFileDlg_FileDlgCB, 10);
389 fl_set_object_callback(pFileDlgForm->Home,
390 C_LyXFileDlg_FileDlgCB, 11);
391 fl_set_object_callback(pFileDlgForm->User1,
392 C_LyXFileDlg_FileDlgCB, 12);
393 fl_set_object_callback(pFileDlgForm->User2,
394 C_LyXFileDlg_FileDlgCB, 13);
396 // Make sure pressing the close box doesn't crash LyX. (RvdK)
397 fl_set_form_atclose(pFileDlgForm->form,
398 C_LyXFileDlg_CancelCB, 0);
399 // Register doubleclick callback
400 fl_set_browser_dblclick_callback(pFileDlgForm->List,
401 C_LyXFileDlg_DoubleClickCB,
404 fl_hide_object(pFileDlgForm->User1);
405 fl_hide_object(pFileDlgForm->User2);
407 r_ = Dialogs::redrawGUI.connect(SigC::slot(this, &FileDialog::Private::redraw));
411 FileDialog::Private::~Private()
417 void FileDialog::Private::redraw()
419 if (pFileDlgForm->form && pFileDlgForm->form->visible)
420 fl_redraw_form(pFileDlgForm->form);
424 // SetButton: sets file selector user button action
425 void FileDialog::Private::SetButton(int iIndex, string const & pszName,
426 string const & pszPath)
432 pObject = pFileDlgForm->User1;
433 pTemp = &pszUserPath1;
434 } else if (iIndex == 1) {
435 pObject = pFileDlgForm->User2;
436 pTemp = &pszUserPath2;
439 if (!pszName.empty() && !pszPath.empty()) {
440 fl_set_object_label(pObject, pszName.c_str());
441 fl_show_object(pObject);
444 fl_hide_object(pObject);
450 // GetDirectory: gets last dialog directory
451 string const FileDialog::Private::GetDirectory() const
453 if (!pszDirectory.empty())
460 // RunDialog: handle dialog during file selection
461 bool FileDialog::Private::RunDialog()
463 force_cancel = false;
468 FL_OBJECT * pObject = fl_do_forms();
470 if (pObject == pFileDlgForm->Ready) {
473 } else if (pObject == pFileDlgForm->Cancel
482 // XForms objects callback (static)
483 void FileDialog::Private::FileDlgCB(FL_OBJECT *, long lArgument)
485 if (!pCurrentDlg) return;
489 case 0: // get directory
490 pCurrentDlg->SetDirectory(fl_get_input(pFileDlgForm->DirBox));
491 pCurrentDlg->Reread();
495 pCurrentDlg->SetMask(fl_get_input(pFileDlgForm->PatBox));
496 pCurrentDlg->Reread();
500 pCurrentDlg->HandleListHit();
504 pCurrentDlg->SetDirectory(fl_get_input(pFileDlgForm->DirBox));
505 pCurrentDlg->SetMask(fl_get_input(pFileDlgForm->PatBox));
506 pCurrentDlg->Reread();
510 pCurrentDlg->SetDirectory(GetEnvPath("HOME"));
511 pCurrentDlg->SetMask(fl_get_input(pFileDlgForm->PatBox));
512 pCurrentDlg->Reread();
515 case 12: // user button 1
516 if (!pCurrentDlg->pszUserPath1.empty()) {
517 pCurrentDlg->SetDirectory(pCurrentDlg->pszUserPath1);
518 pCurrentDlg->SetMask(fl_get_input(pFileDlgForm
520 pCurrentDlg->Reread();
524 case 13: // user button 2
525 if (!pCurrentDlg->pszUserPath2.empty()) {
526 pCurrentDlg->SetDirectory(pCurrentDlg->pszUserPath2);
527 pCurrentDlg->SetMask(fl_get_input(pFileDlgForm
529 pCurrentDlg->Reread();
537 extern "C" void C_LyXFileDlg_FileDlgCB(FL_OBJECT * ob, long data)
539 FileDialog::Private::FileDlgCB(ob, data);
543 // Handle callback from list
544 void FileDialog::Private::HandleListHit()
547 int const iSelect = fl_get_browser(pFileDlgForm->List);
548 if (iSelect > iDepth) {
549 SetInfoLine(direntries[iSelect - iDepth - 1].pszLsEntry);
551 SetInfoLine(string());
556 // Callback for double click in list
557 void FileDialog::Private::DoubleClickCB(FL_OBJECT *, long)
559 if (pCurrentDlg->HandleDoubleClick())
560 // Simulate click on OK button
561 pCurrentDlg->Force(false);
565 extern "C" void C_LyXFileDlg_DoubleClickCB(FL_OBJECT * ob, long data)
567 FileDialog::Private::DoubleClickCB(ob, data);
571 // Handle double click from list
572 bool FileDialog::Private::HandleDoubleClick()
578 int const iSelect = fl_get_browser(pFileDlgForm->List);
579 if (iSelect > iDepth) {
580 pszTemp = direntries[iSelect - iDepth - 1].pszName;
581 SetInfoLine(direntries[iSelect - iDepth - 1].pszLsEntry);
582 if (!suffixIs(pszTemp, '/')) {
584 fl_set_input(pFileDlgForm->Filename, pszTemp.c_str());
586 } else if (iSelect != 0) {
587 SetInfoLine(string());
595 // builds new directory name
596 if (iSelect > iDepth) {
597 // Directory deeper down
598 // First, get directory with trailing /
599 Temp = fl_get_input(pFileDlgForm->DirBox);
600 if (!suffixIs(Temp, '/'))
604 // Directory higher up
606 for (int i = 0; i < iSelect; ++i) {
607 string piece = fl_get_browser_line(pFileDlgForm->List, i+1);
608 // The '+2' is here to count the '@b' (JMarc)
609 Temp += piece.substr(i + 2);
622 // Handle OK button call
623 bool FileDialog::Private::HandleOK()
626 string pszTemp = fl_get_input(pFileDlgForm->PatBox);
627 if (pszTemp != pszMask) {
633 // directory was changed
634 pszTemp = fl_get_input(pFileDlgForm->DirBox);
635 if (pszTemp!= pszDirectory) {
636 SetDirectory(pszTemp);
641 // Handle return from list
642 int const select = fl_get_browser(pFileDlgForm->List);
643 if (select > iDepth) {
644 string const temp = direntries[select - iDepth - 1].pszName;
645 if (!suffixIs(temp, '/')) {
646 // If user didn't type anything, use browser
648 fl_get_input(pFileDlgForm->Filename);
650 fl_set_input(pFileDlgForm->Filename, temp.c_str());
656 // Emulate a doubleclick
657 return HandleDoubleClick();
661 // Handle Cancel CB from WM close
662 int FileDialog::Private::CancelCB(FL_FORM *, void *)
664 // Simulate a click on the cancel button
665 pCurrentDlg->Force(true);
670 extern "C" int C_LyXFileDlg_CancelCB(FL_FORM *fl, void *xev)
672 return FileDialog::Private::CancelCB(fl, xev);
676 // Simulates a click on OK/Cancel
677 void FileDialog::Private::Force(bool cancel)
681 fl_set_button(pFileDlgForm->Cancel, 1);
684 fl_set_button(pFileDlgForm->Ready, 1);
686 // Start timer to break fl_do_forms loop soon
687 fl_set_timer(pFileDlgForm->timer, 0.1);
691 // Select: launches dialog and returns selected file
692 string const FileDialog::Private::Select(string const & title, string const & path,
693 string const & mask, string const & suggested)
695 // handles new mask and path
707 // highlight the suggested file in the browser, if it exists.
709 string const filename = OnlyFilename(suggested);
710 if (!filename.empty()) {
712 i < fl_get_browser_maxline(pFileDlgForm->List); ++i) {
714 fl_get_browser_line(pFileDlgForm->List, i + 1);
715 s = strip(frontStrip(s));
723 if (sel != 0) fl_select_browser_line(pFileDlgForm->List, sel);
724 int const top = max(sel - 5, 1);
725 fl_set_browser_topline(pFileDlgForm->List, top);
727 // checks whether dialog can be started
728 if (pCurrentDlg) return string();
732 SetInfoLine(string());
733 fl_set_input(pFileDlgForm->Filename, suggested.c_str());
734 fl_set_button(pFileDlgForm->Cancel, 0);
735 fl_set_button(pFileDlgForm->Ready, 0);
736 fl_set_focus_object(pFileDlgForm->form, pFileDlgForm->Filename);
737 fl_deactivate_all_forms();
738 fl_show_form(pFileDlgForm->form,
739 FL_PLACE_MOUSE | FL_FREE_SIZE, 0,
744 fl_hide_form(pFileDlgForm->form);
745 fl_activate_all_forms();
748 // Returns filename or string() if no valid selection was made
749 if (!isOk || !fl_get_input(pFileDlgForm->Filename)[0]) return string();
751 pszFileName = fl_get_input(pFileDlgForm->Filename);
753 if (!AbsolutePath(pszFileName)) {
754 pszFileName = AddName(fl_get_input(pFileDlgForm->DirBox),