2 /* This file is part of
3 * ======================================================
5 * LyX, The Document Processor
7 * Copyright 1995 Matthias Ettrich
8 * Copyright 1995-2000 The LyX Team.
10 * ====================================================== */
26 #include "lyx_gui_misc.h" // CancelCloseCB
27 #include "support/FileInfo.h"
28 #include "support/lyxlib.h"
30 #include "frontends/Dialogs.h"
38 # define NAMLEN(dirent) strlen((dirent)->d_name)
40 # define dirent direct
41 # define NAMLEN(dirent) (dirent)->d_namlen
43 # include <sys/ndir.h>
53 #if TIME_WITH_SYS_TIME
54 # include <sys/time.h>
58 # include <sys/time.h>
65 extern "C" int gettimeofday(struct timeval *, struct timezone *);
69 #pragma implementation
72 #include "support/filetools.h"
75 // six months, in seconds
76 static const long SIX_MONTH_SEC = 6L * 30L * 24L * 60L * 60L;
77 static const long ONE_HOUR_SEC = 60L * 60L;
79 // *** User cache class implementation
80 /// User cache class definition
83 /// seeks user name from group ID
84 string const & find(uid_t ID) const {
85 Users::const_iterator cit = users.find(ID);
86 if (cit == users.end()) {
94 void add(uid_t ID) const;
96 typedef map<uid_t, string> Users;
102 void UserCache::add(uid_t ID) const
105 struct passwd * pEntry;
108 if ((pEntry = getpwuid(ID)))
109 pszNewName = pEntry->pw_name;
111 pszNewName = tostr(ID);
115 users[ID] = pszNewName;
119 /// Group cache class definition
122 /// seeks group name from group ID
123 string const & find(gid_t ID) const ;
126 void add(gid_t ID) const;
128 typedef map<gid_t, string> Groups;
130 mutable Groups groups;
134 string const & GroupCache::find(gid_t ID) const
136 Groups::const_iterator cit = groups.find(ID);
137 if (cit == groups.end()) {
141 return (*cit).second;
145 void GroupCache::add(gid_t ID) const
148 struct group * pEntry;
151 if ((pEntry = getgrgid(ID))) pszNewName = pEntry->gr_name;
153 pszNewName = tostr(ID);
156 groups[ID] = pszNewName;
161 static UserCache lyxUserCache;
162 static GroupCache lyxGroupCache;
165 // some "C" wrappers around callbacks
166 extern "C" void C_LyXFileDlg_FileDlgCB(FL_OBJECT *, long lArgument);
167 extern "C" void C_LyXFileDlg_DoubleClickCB(FL_OBJECT *, long);
168 extern "C" int C_LyXFileDlg_CancelCB(FL_FORM *, void *);
171 // compares two LyXDirEntry objects content (used for sort)
172 class comp_direntry {
174 int operator()(LyXDirEntry const & r1,
175 LyXDirEntry const & r2) const ;
177 int comp_direntry::operator()(LyXDirEntry const & r1,
178 LyXDirEntry const & r2) const {
179 bool r1d = suffixIs(r1.pszName, '/');
180 bool r2d = suffixIs(r2.pszName, '/');
181 if (r1d && !r2d) return 1;
182 if (!r1d && r2d) return 0;
183 return r1.pszName < r2.pszName;
187 // *** LyXFileDlg class implementation
190 FD_FileDlg * LyXFileDlg::pFileDlgForm = 0;
191 LyXFileDlg * LyXFileDlg::pCurrentDlg = 0;
194 // Reread: updates dialog list to match class directory
195 void LyXFileDlg::Reread()
198 DIR * pDirectory = ::opendir(pszDirectory.c_str());
200 WriteFSAlert(_("Warning! Couldn't open directory."),
202 pszDirectory = lyx::getcwd();
203 pDirectory = ::opendir(pszDirectory.c_str());
206 // Clear the present namelist
210 fl_hide_object(pFileDlgForm->List);
211 fl_clear_browser(pFileDlgForm->List);
212 fl_set_input(pFileDlgForm->DirBox, pszDirectory.c_str());
214 // Splits complete directory name into directories and compute depth
219 string File = pszDirectory;
221 File = split(File, Temp, '/');
223 while (!File.empty() || !Temp.empty()) {
224 string dline = "@b"+line + Temp + '/';
225 fl_add_browser_line(pFileDlgForm->List, dline.c_str());
226 File = split(File, Temp, '/');
231 // Parses all entries of the given subdirectory
232 time_t curTime = time(0);
233 rewinddir(pDirectory);
234 struct dirent * pDirEntry;
235 while ((pDirEntry = readdir(pDirectory))) {
236 bool isLink = false, isDir = false;
238 // If the pattern doesn't start with a dot, skip hidden files
239 if (!pszMask.empty() && pszMask[0] != '.' &&
240 pDirEntry->d_name[0] == '.')
244 string fname = pDirEntry->d_name;
246 // Under all circumstances, "." and ".." are not wanted
247 if (fname == "." || fname == "..")
251 File = AddName(pszDirectory, fname);
253 fileInfo.newFile(File, true);
254 fileInfo.modeString(szMode);
255 unsigned int nlink = fileInfo.getNumberOfLinks();
256 string user = lyxUserCache.find(fileInfo.getUid());
257 string group = lyxGroupCache.find(fileInfo.getGid());
259 time_t modtime = fileInfo.getModificationTime();
260 string Time = ctime(&modtime);
262 if (curTime > fileInfo.getModificationTime() + SIX_MONTH_SEC
263 || curTime < fileInfo.getModificationTime()
265 // The file is fairly old or in the future. POSIX says
266 // the cutoff is 6 months old. Allow a 1 hour slop
267 // factor for what is considered "the future", to
268 // allow for NFS server/client clock disagreement.
269 // Show the year instead of the time of day.
271 Time.erase(15, string::npos);
273 Time.erase(16, string::npos);
276 string Buffer = string(szMode) + ' ' +
280 Time.substr(4, string::npos) + ' ';
282 Buffer += pDirEntry->d_name;
283 Buffer += fileInfo.typeIndicator();
285 if ((isLink = fileInfo.isLink())) {
288 if (LyXReadLink(File, Link)) {
292 // This gives the FileType of the file that
293 // is really pointed too after resolving all
294 // symlinks. This is not necessarily the same
295 // as the type of Link (which could again be a
296 // link). Is that intended?
298 fileInfo.newFile(File);
299 Buffer += fileInfo.typeIndicator();
303 // filters files according to pattern and type
304 if (fileInfo.isRegular()
306 || fileInfo.isBlock()
307 || fileInfo.isFifo()) {
308 if (!regexMatch(fname, pszMask))
310 } else if (!(isDir = fileInfo.isDir()))
315 // Note pszLsEntry is an string!
316 tmp.pszLsEntry = Buffer;
319 if (isDir) temp += '/';
322 // creates displayed name
323 temp = pDirEntry->d_name;
327 temp += fileInfo.typeIndicator();
328 tmp.pszDisplayed = temp;
330 direntries.push_back(tmp);
333 closedir(pDirectory);
336 sort(direntries.begin(), direntries.end(), comp_direntry());
338 // Add them to directory box
339 for (DirEntries::const_iterator cit = direntries.begin();
340 cit != direntries.end(); ++cit) {
341 string const temp = line + (*cit).pszDisplayed;
342 fl_add_browser_line(pFileDlgForm->List, temp.c_str());
344 fl_set_browser_topline(pFileDlgForm->List, iDepth);
345 fl_show_object(pFileDlgForm->List);
350 // SetDirectory: sets dialog current directory
351 void LyXFileDlg::SetDirectory(string const & Path)
353 if (!pszDirectory.empty()) {
354 string TempPath = ExpandPath(Path); // Expand ~/
355 TempPath = MakeAbsPath(TempPath, pszDirectory);
356 pszDirectory = MakeAbsPath(TempPath);
357 } else pszDirectory = MakeAbsPath(Path);
361 // SetMask: sets dialog file mask
362 void LyXFileDlg::SetMask(string const & NewMask)
365 fl_set_input(pFileDlgForm->PatBox, pszMask.c_str());
369 // SetInfoLine: sets dialog information line
370 void LyXFileDlg::SetInfoLine(string const & Line)
373 fl_set_object_label(pFileDlgForm->FileInfo, pszInfoLine.c_str());
377 LyXFileDlg::LyXFileDlg()
379 pszDirectory = MakeAbsPath(string("."));
382 // Creates form if necessary.
384 pFileDlgForm = create_form_FileDlg();
385 // Set callbacks. This means that we don't need a patch file
386 fl_set_object_callback(pFileDlgForm->DirBox,
387 C_LyXFileDlg_FileDlgCB, 0);
388 fl_set_object_callback(pFileDlgForm->PatBox,
389 C_LyXFileDlg_FileDlgCB, 1);
390 fl_set_object_callback(pFileDlgForm->List,
391 C_LyXFileDlg_FileDlgCB, 2);
392 fl_set_object_callback(pFileDlgForm->Filename,
393 C_LyXFileDlg_FileDlgCB, 3);
394 fl_set_object_callback(pFileDlgForm->Rescan,
395 C_LyXFileDlg_FileDlgCB, 10);
396 fl_set_object_callback(pFileDlgForm->Home,
397 C_LyXFileDlg_FileDlgCB, 11);
398 fl_set_object_callback(pFileDlgForm->User1,
399 C_LyXFileDlg_FileDlgCB, 12);
400 fl_set_object_callback(pFileDlgForm->User2,
401 C_LyXFileDlg_FileDlgCB, 13);
403 // Make sure pressing the close box doesn't crash LyX. (RvdK)
404 fl_set_form_atclose(pFileDlgForm->FileDlg,
405 C_LyXFileDlg_CancelCB, 0);
406 // Register doubleclick callback
407 fl_set_browser_dblclick_callback(pFileDlgForm->List,
408 C_LyXFileDlg_DoubleClickCB,
411 fl_hide_object(pFileDlgForm->User1);
412 fl_hide_object(pFileDlgForm->User2);
414 r_ = Dialogs::redrawGUI.connect(SigC::slot(this, &LyXFileDlg::redraw));
418 LyXFileDlg::~LyXFileDlg()
424 void LyXFileDlg::redraw()
426 if (pFileDlgForm->FileDlg && pFileDlgForm->FileDlg->visible)
427 fl_redraw_form(pFileDlgForm->FileDlg);
431 // SetButton: sets file selector user button action
432 void LyXFileDlg::SetButton(int iIndex, string const & pszName,
433 string const & pszPath)
439 pObject = pFileDlgForm->User1;
440 pTemp = &pszUserPath1;
441 } else if (iIndex == 1) {
442 pObject = pFileDlgForm->User2;
443 pTemp = &pszUserPath2;
446 if (!pszName.empty() && !pszPath.empty()) {
447 fl_set_object_label(pObject, pszName.c_str());
448 fl_show_object(pObject);
451 fl_hide_object(pObject);
457 // GetDirectory: gets last dialog directory
458 string const LyXFileDlg::GetDirectory() const
460 if (!pszDirectory.empty())
467 // RunDialog: handle dialog during file selection
468 bool LyXFileDlg::RunDialog()
470 force_cancel = false;
475 FL_OBJECT * pObject = fl_do_forms();
477 if (pObject == pFileDlgForm->Ready) {
480 } else if (pObject == pFileDlgForm->Cancel
489 // XForms objects callback (static)
490 void LyXFileDlg::FileDlgCB(FL_OBJECT *, long lArgument)
492 if (!pCurrentDlg) return;
496 case 0: // get directory
497 pCurrentDlg->SetDirectory(fl_get_input(pFileDlgForm->DirBox));
498 pCurrentDlg->Reread();
502 pCurrentDlg->SetMask(fl_get_input(pFileDlgForm->PatBox));
503 pCurrentDlg->Reread();
507 pCurrentDlg->HandleListHit();
511 pCurrentDlg->SetDirectory(fl_get_input(pFileDlgForm->DirBox));
512 pCurrentDlg->SetMask(fl_get_input(pFileDlgForm->PatBox));
513 pCurrentDlg->Reread();
517 pCurrentDlg->SetDirectory(GetEnvPath("HOME"));
518 pCurrentDlg->SetMask(fl_get_input(pFileDlgForm->PatBox));
519 pCurrentDlg->Reread();
522 case 12: // user button 1
523 if (!pCurrentDlg->pszUserPath1.empty()) {
524 pCurrentDlg->SetDirectory(pCurrentDlg->pszUserPath1);
525 pCurrentDlg->SetMask(fl_get_input(pFileDlgForm
527 pCurrentDlg->Reread();
531 case 13: // user button 2
532 if (!pCurrentDlg->pszUserPath2.empty()) {
533 pCurrentDlg->SetDirectory(pCurrentDlg->pszUserPath2);
534 pCurrentDlg->SetMask(fl_get_input(pFileDlgForm
536 pCurrentDlg->Reread();
544 extern "C" void C_LyXFileDlg_FileDlgCB(FL_OBJECT * ob, long data)
546 LyXFileDlg::FileDlgCB(ob, data);
550 // Handle callback from list
551 void LyXFileDlg::HandleListHit()
554 int const iSelect = fl_get_browser(pFileDlgForm->List);
555 if (iSelect > iDepth) {
556 SetInfoLine(direntries[iSelect - iDepth - 1].pszLsEntry);
558 SetInfoLine(string());
563 // Callback for double click in list
564 void LyXFileDlg::DoubleClickCB(FL_OBJECT *, long)
566 if (pCurrentDlg->HandleDoubleClick())
567 // Simulate click on OK button
568 pCurrentDlg->Force(false);
572 extern "C" void C_LyXFileDlg_DoubleClickCB(FL_OBJECT * ob, long data)
574 LyXFileDlg::DoubleClickCB(ob, data);
578 // Handle double click from list
579 bool LyXFileDlg::HandleDoubleClick()
585 int const iSelect = fl_get_browser(pFileDlgForm->List);
586 if (iSelect > iDepth) {
587 pszTemp = direntries[iSelect - iDepth - 1].pszName;
588 SetInfoLine(direntries[iSelect - iDepth - 1].pszLsEntry);
589 if (!suffixIs(pszTemp, '/')) {
591 fl_set_input(pFileDlgForm->Filename, pszTemp.c_str());
593 } else if (iSelect != 0) {
594 SetInfoLine(string());
602 // builds new directory name
603 if (iSelect > iDepth) {
604 // Directory deeper down
605 // First, get directory with trailing /
606 Temp = fl_get_input(pFileDlgForm->DirBox);
607 if (!suffixIs(Temp, '/'))
611 // Directory higher up
613 for (int i = 0; i < iSelect; ++i) {
614 string piece = fl_get_browser_line(pFileDlgForm->List, i+1);
615 // The '+2' is here to count the '@b' (JMarc)
616 Temp += piece.substr(i + 2);
629 // Handle OK button call
630 bool LyXFileDlg::HandleOK()
633 string pszTemp = fl_get_input(pFileDlgForm->PatBox);
634 if (pszTemp != pszMask) {
640 // directory was changed
641 pszTemp = fl_get_input(pFileDlgForm->DirBox);
642 if (pszTemp!= pszDirectory) {
643 SetDirectory(pszTemp);
648 // Handle return from list
649 int const select = fl_get_browser(pFileDlgForm->List);
650 if (select > iDepth) {
651 string const temp = direntries[select - iDepth - 1].pszName;
652 if (!suffixIs(temp, '/')) {
653 // If user didn't type anything, use browser
655 fl_get_input(pFileDlgForm->Filename);
657 fl_set_input(pFileDlgForm->Filename, temp.c_str());
663 // Emulate a doubleclick
664 return HandleDoubleClick();
668 // Handle Cancel CB from WM close
669 int LyXFileDlg::CancelCB(FL_FORM *, void *)
671 // Simulate a click on the cancel button
672 pCurrentDlg->Force(true);
677 extern "C" int C_LyXFileDlg_CancelCB(FL_FORM *fl, void *xev)
679 return LyXFileDlg::CancelCB(fl, xev);
683 // Simulates a click on OK/Cancel
684 void LyXFileDlg::Force(bool cancel)
688 fl_set_button(pFileDlgForm->Cancel, 1);
691 fl_set_button(pFileDlgForm->Ready, 1);
693 // Start timer to break fl_do_forms loop soon
694 fl_set_timer(pFileDlgForm->timer, 0.1);
698 // Select: launches dialog and returns selected file
699 string const LyXFileDlg::Select(string const & title, string const & path,
700 string const & mask, string const & suggested)
702 // handles new mask and path
714 // highlight the suggested file in the browser, if it exists.
716 string const filename = OnlyFilename(suggested);
717 if (!filename.empty()) {
719 i < fl_get_browser_maxline(pFileDlgForm->List); ++i) {
721 fl_get_browser_line(pFileDlgForm->List, i + 1);
722 s = strip(frontStrip(s));
730 if (sel != 0) fl_select_browser_line(pFileDlgForm->List, sel);
731 int const top = max(sel - 5, 1);
732 fl_set_browser_topline(pFileDlgForm->List, top);
734 // checks whether dialog can be started
735 if (pCurrentDlg) return string();
739 SetInfoLine(string());
740 fl_set_input(pFileDlgForm->Filename, suggested.c_str());
741 fl_set_button(pFileDlgForm->Cancel, 0);
742 fl_set_button(pFileDlgForm->Ready, 0);
743 fl_set_focus_object(pFileDlgForm->FileDlg, pFileDlgForm->Filename);
744 fl_deactivate_all_forms();
745 fl_show_form(pFileDlgForm->FileDlg,
746 FL_PLACE_MOUSE | FL_FREE_SIZE, FL_TRANSIENT,
751 fl_hide_form(pFileDlgForm->FileDlg);
752 fl_activate_all_forms();
755 // Returns filename or string() if no valid selection was made
756 if (!isOk || !fl_get_input(pFileDlgForm->Filename)[0]) return string();
758 pszFileName = fl_get_input(pFileDlgForm->Filename);
760 if (!AbsolutePath(pszFileName)) {
761 pszFileName = AddName(fl_get_input(pFileDlgForm->DirBox),