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 #ifdef SIGC_CXX_NAMESPACES
79 // six months, in seconds
80 static const long SIX_MONTH_SEC = 6L * 30L * 24L * 60L * 60L;
81 static const long ONE_HOUR_SEC = 60L * 60L;
83 // *** User cache class implementation
84 /// User cache class definition
87 /// seeks user name from group ID
88 string const & find(uid_t ID) const {
89 Users::const_iterator cit = users.find(ID);
90 if (cit == users.end()) {
98 void add(uid_t ID) const;
100 typedef map<uid_t, string> Users;
106 void UserCache::add(uid_t ID) const
109 struct passwd * pEntry;
112 if ((pEntry = getpwuid(ID)))
113 pszNewName = pEntry->pw_name;
115 pszNewName = tostr(ID);
119 users[ID] = pszNewName;
123 /// Group cache class definition
126 /// seeks group name from group ID
127 string const & find(gid_t ID) const ;
130 void add(gid_t ID) const;
132 typedef map<gid_t, string> Groups;
134 mutable Groups groups;
138 string const & GroupCache::find(gid_t ID) const
140 Groups::const_iterator cit = groups.find(ID);
141 if (cit == groups.end()) {
145 return (*cit).second;
149 void GroupCache::add(gid_t ID) const
152 struct group * pEntry;
155 if ((pEntry = getgrgid(ID))) pszNewName = pEntry->gr_name;
157 pszNewName = tostr(ID);
160 groups[ID] = pszNewName;
165 static UserCache lyxUserCache;
166 static GroupCache lyxGroupCache;
169 // some "C" wrappers around callbacks
170 extern "C" void C_LyXFileDlg_FileDlgCB(FL_OBJECT *, long lArgument);
171 extern "C" void C_LyXFileDlg_DoubleClickCB(FL_OBJECT *, long);
172 extern "C" int C_LyXFileDlg_CancelCB(FL_FORM *, void *);
175 // compares two LyXDirEntry objects content (used for sort)
176 class comp_direntry {
178 int operator()(LyXDirEntry const & r1,
179 LyXDirEntry const & r2) const ;
181 int comp_direntry::operator()(LyXDirEntry const & r1,
182 LyXDirEntry const & r2) const {
183 bool r1d = suffixIs(r1.pszName, '/');
184 bool r2d = suffixIs(r2.pszName, '/');
185 if (r1d && !r2d) return 1;
186 if (!r1d && r2d) return 0;
187 return r1.pszName < r2.pszName;
191 // *** LyXFileDlg class implementation
194 FD_FileDlg * LyXFileDlg::pFileDlgForm = 0;
195 LyXFileDlg * LyXFileDlg::pCurrentDlg = 0;
198 // Reread: updates dialog list to match class directory
199 void LyXFileDlg::Reread()
202 DIR * pDirectory = ::opendir(pszDirectory.c_str());
204 WriteFSAlert(_("Warning! Couldn't open directory."),
206 pszDirectory = lyx::getcwd();
207 pDirectory = ::opendir(pszDirectory.c_str());
210 // Clear the present namelist
214 fl_hide_object(pFileDlgForm->List);
215 fl_clear_browser(pFileDlgForm->List);
216 fl_set_input(pFileDlgForm->DirBox, pszDirectory.c_str());
218 // Splits complete directory name into directories and compute depth
223 string File = pszDirectory;
225 File = split(File, Temp, '/');
227 while (!File.empty() || !Temp.empty()) {
228 string dline = "@b"+line + Temp + '/';
229 fl_add_browser_line(pFileDlgForm->List, dline.c_str());
230 File = split(File, Temp, '/');
235 // Parses all entries of the given subdirectory
236 time_t curTime = time(0);
237 rewinddir(pDirectory);
238 struct dirent * pDirEntry;
239 while ((pDirEntry = readdir(pDirectory))) {
240 bool isLink = false, isDir = false;
242 // If the pattern doesn't start with a dot, skip hidden files
243 if (!pszMask.empty() && pszMask[0] != '.' &&
244 pDirEntry->d_name[0] == '.')
248 string fname = pDirEntry->d_name;
250 // Under all circumstances, "." and ".." are not wanted
251 if (fname == "." || fname == "..")
255 File = AddName(pszDirectory, fname);
257 fileInfo.newFile(File, true);
258 fileInfo.modeString(szMode);
259 unsigned int nlink = fileInfo.getNumberOfLinks();
260 string user = lyxUserCache.find(fileInfo.getUid());
261 string group = lyxGroupCache.find(fileInfo.getGid());
263 time_t modtime = fileInfo.getModificationTime();
264 string Time = ctime(&modtime);
266 if (curTime > fileInfo.getModificationTime() + SIX_MONTH_SEC
267 || curTime < fileInfo.getModificationTime()
269 // The file is fairly old or in the future. POSIX says
270 // the cutoff is 6 months old. Allow a 1 hour slop
271 // factor for what is considered "the future", to
272 // allow for NFS server/client clock disagreement.
273 // Show the year instead of the time of day.
275 Time.erase(15, string::npos);
277 Time.erase(16, string::npos);
280 string Buffer = string(szMode) + ' ' +
284 Time.substr(4, string::npos) + ' ';
286 Buffer += pDirEntry->d_name;
287 Buffer += fileInfo.typeIndicator();
289 if ((isLink = fileInfo.isLink())) {
292 if (LyXReadLink(File, Link)) {
296 // This gives the FileType of the file that
297 // is really pointed too after resolving all
298 // symlinks. This is not necessarily the same
299 // as the type of Link (which could again be a
300 // link). Is that intended?
302 fileInfo.newFile(File);
303 Buffer += fileInfo.typeIndicator();
307 // filters files according to pattern and type
308 if (fileInfo.isRegular()
310 || fileInfo.isBlock()
311 || fileInfo.isFifo()) {
312 if (!regexMatch(fname, pszMask))
314 } else if (!(isDir = fileInfo.isDir()))
319 // Note pszLsEntry is an string!
320 tmp.pszLsEntry = Buffer;
323 if (isDir) temp += '/';
326 // creates displayed name
327 temp = pDirEntry->d_name;
331 temp += fileInfo.typeIndicator();
332 tmp.pszDisplayed = temp;
334 direntries.push_back(tmp);
337 closedir(pDirectory);
340 sort(direntries.begin(), direntries.end(), comp_direntry());
342 // Add them to directory box
343 for (DirEntries::const_iterator cit = direntries.begin();
344 cit != direntries.end(); ++cit) {
345 string const temp = line + (*cit).pszDisplayed;
346 fl_add_browser_line(pFileDlgForm->List, temp.c_str());
348 fl_set_browser_topline(pFileDlgForm->List, iDepth);
349 fl_show_object(pFileDlgForm->List);
354 // SetDirectory: sets dialog current directory
355 void LyXFileDlg::SetDirectory(string const & Path)
357 if (!pszDirectory.empty()) {
358 string TempPath = ExpandPath(Path); // Expand ~/
359 TempPath = MakeAbsPath(TempPath, pszDirectory);
360 pszDirectory = MakeAbsPath(TempPath);
361 } else pszDirectory = MakeAbsPath(Path);
365 // SetMask: sets dialog file mask
366 void LyXFileDlg::SetMask(string const & NewMask)
369 fl_set_input(pFileDlgForm->PatBox, pszMask.c_str());
373 // SetInfoLine: sets dialog information line
374 void LyXFileDlg::SetInfoLine(string const & Line)
377 fl_set_object_label(pFileDlgForm->FileInfo, pszInfoLine.c_str());
381 LyXFileDlg::LyXFileDlg()
383 pszDirectory = MakeAbsPath(string("."));
386 // Creates form if necessary.
388 pFileDlgForm = create_form_FileDlg();
389 // Set callbacks. This means that we don't need a patch file
390 fl_set_object_callback(pFileDlgForm->DirBox,
391 C_LyXFileDlg_FileDlgCB, 0);
392 fl_set_object_callback(pFileDlgForm->PatBox,
393 C_LyXFileDlg_FileDlgCB, 1);
394 fl_set_object_callback(pFileDlgForm->List,
395 C_LyXFileDlg_FileDlgCB, 2);
396 fl_set_object_callback(pFileDlgForm->Filename,
397 C_LyXFileDlg_FileDlgCB, 3);
398 fl_set_object_callback(pFileDlgForm->Rescan,
399 C_LyXFileDlg_FileDlgCB, 10);
400 fl_set_object_callback(pFileDlgForm->Home,
401 C_LyXFileDlg_FileDlgCB, 11);
402 fl_set_object_callback(pFileDlgForm->User1,
403 C_LyXFileDlg_FileDlgCB, 12);
404 fl_set_object_callback(pFileDlgForm->User2,
405 C_LyXFileDlg_FileDlgCB, 13);
407 // Make sure pressing the close box doesn't crash LyX. (RvdK)
408 fl_set_form_atclose(pFileDlgForm->FileDlg,
409 C_LyXFileDlg_CancelCB, 0);
410 // Register doubleclick callback
411 fl_set_browser_dblclick_callback(pFileDlgForm->List,
412 C_LyXFileDlg_DoubleClickCB,
415 fl_hide_object(pFileDlgForm->User1);
416 fl_hide_object(pFileDlgForm->User2);
418 r_ = Dialogs::redrawGUI.connect(slot(this, &LyXFileDlg::redraw));
422 LyXFileDlg::~LyXFileDlg()
428 void LyXFileDlg::redraw()
430 if (pFileDlgForm->FileDlg && pFileDlgForm->FileDlg->visible)
431 fl_redraw_form(pFileDlgForm->FileDlg);
435 // SetButton: sets file selector user button action
436 void LyXFileDlg::SetButton(int iIndex, string const & pszName,
437 string const & pszPath)
443 pObject = pFileDlgForm->User1;
444 pTemp = &pszUserPath1;
445 } else if (iIndex == 1) {
446 pObject = pFileDlgForm->User2;
447 pTemp = &pszUserPath2;
450 if (!pszName.empty() && !pszPath.empty()) {
451 fl_set_object_label(pObject, pszName.c_str());
452 fl_show_object(pObject);
455 fl_hide_object(pObject);
461 // GetDirectory: gets last dialog directory
462 string const LyXFileDlg::GetDirectory() const
464 if (!pszDirectory.empty())
471 // RunDialog: handle dialog during file selection
472 bool LyXFileDlg::RunDialog()
474 force_cancel = false;
479 FL_OBJECT * pObject = fl_do_forms();
481 if (pObject == pFileDlgForm->Ready) {
484 } else if (pObject == pFileDlgForm->Cancel
493 // XForms objects callback (static)
494 void LyXFileDlg::FileDlgCB(FL_OBJECT *, long lArgument)
496 if (!pCurrentDlg) return;
500 case 0: // get directory
501 pCurrentDlg->SetDirectory(fl_get_input(pFileDlgForm->DirBox));
502 pCurrentDlg->Reread();
506 pCurrentDlg->SetMask(fl_get_input(pFileDlgForm->PatBox));
507 pCurrentDlg->Reread();
511 pCurrentDlg->HandleListHit();
515 pCurrentDlg->SetDirectory(fl_get_input(pFileDlgForm->DirBox));
516 pCurrentDlg->SetMask(fl_get_input(pFileDlgForm->PatBox));
517 pCurrentDlg->Reread();
521 pCurrentDlg->SetDirectory(GetEnvPath("HOME"));
522 pCurrentDlg->SetMask(fl_get_input(pFileDlgForm->PatBox));
523 pCurrentDlg->Reread();
526 case 12: // user button 1
527 if (!pCurrentDlg->pszUserPath1.empty()) {
528 pCurrentDlg->SetDirectory(pCurrentDlg->pszUserPath1);
529 pCurrentDlg->SetMask(fl_get_input(pFileDlgForm
531 pCurrentDlg->Reread();
535 case 13: // user button 2
536 if (!pCurrentDlg->pszUserPath2.empty()) {
537 pCurrentDlg->SetDirectory(pCurrentDlg->pszUserPath2);
538 pCurrentDlg->SetMask(fl_get_input(pFileDlgForm
540 pCurrentDlg->Reread();
548 extern "C" void C_LyXFileDlg_FileDlgCB(FL_OBJECT * ob, long data)
550 LyXFileDlg::FileDlgCB(ob, data);
554 // Handle callback from list
555 void LyXFileDlg::HandleListHit()
558 int const iSelect = fl_get_browser(pFileDlgForm->List);
559 if (iSelect > iDepth) {
560 SetInfoLine(direntries[iSelect - iDepth - 1].pszLsEntry);
562 SetInfoLine(string());
567 // Callback for double click in list
568 void LyXFileDlg::DoubleClickCB(FL_OBJECT *, long)
570 if (pCurrentDlg->HandleDoubleClick())
571 // Simulate click on OK button
572 pCurrentDlg->Force(false);
576 extern "C" void C_LyXFileDlg_DoubleClickCB(FL_OBJECT * ob, long data)
578 LyXFileDlg::DoubleClickCB(ob, data);
582 // Handle double click from list
583 bool LyXFileDlg::HandleDoubleClick()
589 int const iSelect = fl_get_browser(pFileDlgForm->List);
590 if (iSelect > iDepth) {
591 pszTemp = direntries[iSelect - iDepth - 1].pszName;
592 SetInfoLine(direntries[iSelect - iDepth - 1].pszLsEntry);
593 if (!suffixIs(pszTemp, '/')) {
595 fl_set_input(pFileDlgForm->Filename, pszTemp.c_str());
597 } else if (iSelect != 0) {
598 SetInfoLine(string());
606 // builds new directory name
607 if (iSelect > iDepth) {
608 // Directory deeper down
609 // First, get directory with trailing /
610 Temp = fl_get_input(pFileDlgForm->DirBox);
611 if (!suffixIs(Temp, '/'))
615 // Directory higher up
617 for (int i = 0; i < iSelect; ++i) {
618 string piece = fl_get_browser_line(pFileDlgForm->List, i+1);
619 // The '+2' is here to count the '@b' (JMarc)
620 Temp += piece.substr(i + 2);
633 // Handle OK button call
634 bool LyXFileDlg::HandleOK()
637 string pszTemp = fl_get_input(pFileDlgForm->PatBox);
638 if (pszTemp != pszMask) {
644 // directory was changed
645 pszTemp = fl_get_input(pFileDlgForm->DirBox);
646 if (pszTemp!= pszDirectory) {
647 SetDirectory(pszTemp);
652 // Handle return from list
653 int const select = fl_get_browser(pFileDlgForm->List);
654 if (select > iDepth) {
655 string const temp = direntries[select - iDepth - 1].pszName;
656 if (!suffixIs(temp, '/')) {
657 // If user didn't type anything, use browser
659 fl_get_input(pFileDlgForm->Filename);
661 fl_set_input(pFileDlgForm->Filename, temp.c_str());
667 // Emulate a doubleclick
668 return HandleDoubleClick();
672 // Handle Cancel CB from WM close
673 int LyXFileDlg::CancelCB(FL_FORM *, void *)
675 // Simulate a click on the cancel button
676 pCurrentDlg->Force(true);
681 extern "C" int C_LyXFileDlg_CancelCB(FL_FORM *fl, void *xev)
683 return LyXFileDlg::CancelCB(fl, xev);
687 // Simulates a click on OK/Cancel
688 void LyXFileDlg::Force(bool cancel)
692 fl_set_button(pFileDlgForm->Cancel, 1);
695 fl_set_button(pFileDlgForm->Ready, 1);
697 // Start timer to break fl_do_forms loop soon
698 fl_set_timer(pFileDlgForm->timer, 0.1);
702 // Select: launches dialog and returns selected file
703 string const LyXFileDlg::Select(string const & title, string const & path,
704 string const & mask, string const & suggested)
706 // handles new mask and path
718 // highlight the suggested file in the browser, if it exists.
720 string const filename = OnlyFilename(suggested);
721 if (!filename.empty()) {
723 i < fl_get_browser_maxline(pFileDlgForm->List); ++i) {
725 fl_get_browser_line(pFileDlgForm->List, i + 1);
726 s = strip(frontStrip(s));
734 if (sel != 0) fl_select_browser_line(pFileDlgForm->List, sel);
735 int const top = max(sel - 5, 1);
736 fl_set_browser_topline(pFileDlgForm->List, top);
738 // checks whether dialog can be started
739 if (pCurrentDlg) return string();
743 SetInfoLine(string());
744 fl_set_input(pFileDlgForm->Filename, suggested.c_str());
745 fl_set_button(pFileDlgForm->Cancel, 0);
746 fl_set_button(pFileDlgForm->Ready, 0);
747 fl_set_focus_object(pFileDlgForm->FileDlg, pFileDlgForm->Filename);
748 fl_deactivate_all_forms();
749 fl_show_form(pFileDlgForm->FileDlg,
750 FL_PLACE_MOUSE | FL_FREE_SIZE, FL_TRANSIENT,
755 fl_hide_form(pFileDlgForm->FileDlg);
756 fl_activate_all_forms();
759 // Returns filename or string() if no valid selection was made
760 if (!isOk || !fl_get_input(pFileDlgForm->Filename)[0]) return string();
762 pszFileName = fl_get_input(pFileDlgForm->Filename);
764 if (!AbsolutePath(pszFileName)) {
765 pszFileName = AddName(fl_get_input(pFileDlgForm->DirBox),