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 * ====================================================== */
25 #include "lyx_gui_misc.h" // CancelCloseCB
26 #include "support/FileInfo.h"
35 # define NAMLEN(dirent) strlen((dirent)->d_name)
37 # define dirent direct
38 # define NAMLEN(dirent) (dirent)->d_namlen
40 # include <sys/ndir.h>
50 #if TIME_WITH_SYS_TIME
51 # include <sys/time.h>
55 # include <sys/time.h>
62 extern "C" int gettimeofday(struct timeval *, struct timezone *);
63 #define remove(a) unlink(a)
67 #pragma implementation
70 #include "support/filetools.h"
73 // six months, in seconds
74 static const long SIX_MONTH_SEC = 6L * 30L * 24L * 60L * 60L;
75 static const long ONE_HOUR_SEC = 60L * 60L;
77 // *** User cache class implementation
78 /// User cache class definition
81 /// seeks user name from group ID
82 string const & find(uid_t ID) const {
83 Users::const_iterator cit = users.find(ID);
84 if (cit == users.end()) {
92 void add(uid_t ID) const;
94 typedef map<uid_t, string> Users;
99 void UserCache::add(uid_t ID) const
102 struct passwd * pEntry;
105 if ((pEntry = getpwuid(ID)))
106 pszNewName = pEntry->pw_name;
108 pszNewName = tostr(ID);
112 users[ID] = pszNewName;
116 /// Group cache class definition
119 /// seeks group name from group ID
120 string const & find(gid_t ID) const {
121 Groups::const_iterator cit = groups.find(ID);
122 if (cit == groups.end()) {
126 return (*cit).second;
130 void add(gid_t ID) const;
132 typedef map<gid_t, string> Groups;
134 mutable Groups groups;
137 void GroupCache::add(gid_t ID) const
140 struct group * pEntry;
143 if ((pEntry = getgrgid(ID))) pszNewName = pEntry->gr_name;
145 pszNewName = tostr(ID);
148 groups[ID] = pszNewName;
152 static UserCache lyxUserCache;
153 static GroupCache lyxGroupCache;
155 // some "C" wrappers around callbacks
156 extern "C" void C_LyXFileDlg_FileDlgCB(FL_OBJECT *, long lArgument);
157 extern "C" void C_LyXFileDlg_DoubleClickCB(FL_OBJECT *, long);
158 extern "C" int C_LyXFileDlg_CancelCB(FL_FORM *, void *);
161 // compares two LyXDirEntry objects content (used for sort)
162 class comp_direntry {
164 int operator()(LyXDirEntry const & r1,
165 LyXDirEntry const & r2) const {
166 bool r1d = suffixIs(r1.pszName, '/');
167 bool r2d = suffixIs(r2.pszName, '/');
168 if (r1d && !r2d) return 1;
169 if (!r1d && r2d) return 0;
170 return r1.pszName < r2.pszName;
175 // *** LyXFileDlg class implementation
178 FD_FileDlg * LyXFileDlg::pFileDlgForm = 0;
179 LyXFileDlg * LyXFileDlg::pCurrentDlg = 0;
182 // Reread: updates dialog list to match class directory
183 void LyXFileDlg::Reread()
186 DIR * pDirectory = opendir(pszDirectory.c_str());
188 WriteFSAlert(_("Warning! Couldn't open directory."),
190 pszDirectory = GetCWD();
191 pDirectory = opendir(pszDirectory.c_str());
194 // Clear the present namelist
198 fl_hide_object(pFileDlgForm->List);
199 fl_clear_browser(pFileDlgForm->List);
200 fl_set_input(pFileDlgForm->DirBox, pszDirectory.c_str());
202 // Splits complete directory name into directories and compute depth
207 string File = pszDirectory;
209 File = split(File, Temp, '/');
211 while (!File.empty() || !Temp.empty()) {
212 string dline = "@b"+line + Temp + '/';
213 fl_add_browser_line(pFileDlgForm->List, dline.c_str());
214 File = split(File, Temp, '/');
219 // Parses all entries of the given subdirectory
220 time_t curTime = time(0);
221 rewinddir(pDirectory);
222 struct dirent * pDirEntry;
223 while ((pDirEntry = readdir(pDirectory))) {
224 bool isLink = false, isDir = false;
226 // If the pattern doesn't start with a dot, skip hidden files
227 if (!pszMask.empty() && pszMask[0] != '.' &&
228 pDirEntry->d_name[0] == '.')
232 string fname = pDirEntry->d_name;
234 // Under all circumstances, "." and ".." are not wanted
235 if (fname == "." || fname == "..")
239 File = AddName(pszDirectory, fname);
241 fileInfo.newFile(File, true);
242 fileInfo.modeString(szMode);
243 unsigned int nlink = fileInfo.getNumberOfLinks();
244 string user = lyxUserCache.find(fileInfo.getUid());
245 string group = lyxGroupCache.find(fileInfo.getGid());
247 time_t modtime = fileInfo.getModificationTime();
248 string Time = ctime(&modtime);
250 if (curTime > fileInfo.getModificationTime() + SIX_MONTH_SEC
251 || curTime < fileInfo.getModificationTime()
253 // The file is fairly old or in the future. POSIX says
254 // the cutoff is 6 months old. Allow a 1 hour slop
255 // factor for what is considered "the future", to
256 // allow for NFS server/client clock disagreement.
257 // Show the year instead of the time of day.
259 Time.erase(15, string::npos);
261 Time.erase(16, string::npos);
264 string Buffer = string(szMode) + ' ' +
268 Time.substr(4, string::npos) + ' ';
270 Buffer += pDirEntry->d_name;
271 Buffer += fileInfo.typeIndicator();
273 if ((isLink = fileInfo.isLink())) {
276 if (LyXReadLink(File, Link)) {
280 // This gives the FileType of the file that
281 // is really pointed too after resolving all
282 // symlinks. This is not necessarily the same
283 // as the type of Link (which could again be a
284 // link). Is that intended?
286 fileInfo.newFile(File);
287 Buffer += fileInfo.typeIndicator();
291 // filters files according to pattern and type
292 if (fileInfo.isRegular()
294 || fileInfo.isBlock()
295 || fileInfo.isFifo()) {
296 if (!regexMatch(fname, pszMask))
298 } else if (!(isDir = fileInfo.isDir()))
303 // Note pszLsEntry is an string!
304 tmp.pszLsEntry = Buffer;
307 if (isDir) temp += '/';
310 // creates displayed name
311 temp = pDirEntry->d_name;
315 temp += fileInfo.typeIndicator();
316 tmp.pszDisplayed = temp;
318 direntries.push_back(tmp);
321 closedir(pDirectory);
324 sort(direntries.begin(), direntries.end(), comp_direntry());
326 // Add them to directory box
327 for (DirEntries::const_iterator cit = direntries.begin();
328 cit != direntries.end(); ++cit) {
329 string temp = line + (*cit).pszDisplayed;
330 fl_add_browser_line(pFileDlgForm->List, temp.c_str());
332 fl_set_browser_topline(pFileDlgForm->List, iDepth);
333 fl_show_object(pFileDlgForm->List);
338 // SetDirectory: sets dialog current directory
339 void LyXFileDlg::SetDirectory(string const & Path)
341 if (!pszDirectory.empty()) {
342 string TempPath = ExpandPath(Path); // Expand ~/
343 TempPath = MakeAbsPath(TempPath, pszDirectory);
344 pszDirectory = MakeAbsPath(TempPath);
345 } else pszDirectory = MakeAbsPath(Path);
349 // SetMask: sets dialog file mask
350 void LyXFileDlg::SetMask(string const & NewMask)
353 fl_set_input(pFileDlgForm->PatBox, pszMask.c_str());
357 // SetInfoLine: sets dialog information line
358 void LyXFileDlg::SetInfoLine(string const & Line)
361 fl_set_object_label(pFileDlgForm->FileInfo, pszInfoLine.c_str());
365 LyXFileDlg::LyXFileDlg()
367 pszDirectory = MakeAbsPath(string("."));
370 // Creates form if necessary.
372 pFileDlgForm = create_form_FileDlg();
373 // Set callbacks. This means that we don't need a patch file
374 fl_set_object_callback(pFileDlgForm->DirBox,
375 C_LyXFileDlg_FileDlgCB, 0);
376 fl_set_object_callback(pFileDlgForm->PatBox,
377 C_LyXFileDlg_FileDlgCB, 1);
378 fl_set_object_callback(pFileDlgForm->List,
379 C_LyXFileDlg_FileDlgCB, 2);
380 fl_set_object_callback(pFileDlgForm->Filename,
381 C_LyXFileDlg_FileDlgCB, 3);
382 fl_set_object_callback(pFileDlgForm->Rescan,
383 C_LyXFileDlg_FileDlgCB, 10);
384 fl_set_object_callback(pFileDlgForm->Home,
385 C_LyXFileDlg_FileDlgCB, 11);
386 fl_set_object_callback(pFileDlgForm->User1,
387 C_LyXFileDlg_FileDlgCB, 12);
388 fl_set_object_callback(pFileDlgForm->User2,
389 C_LyXFileDlg_FileDlgCB, 13);
391 // Make sure pressing the close box doesn't crash LyX. (RvdK)
392 fl_set_form_atclose(pFileDlgForm->FileDlg,
393 C_LyXFileDlg_CancelCB, 0);
394 // Register doubleclick callback
395 fl_set_browser_dblclick_callback(pFileDlgForm->List,
396 C_LyXFileDlg_DoubleClickCB,
399 fl_hide_object(pFileDlgForm->User1);
400 fl_hide_object(pFileDlgForm->User2);
404 // SetButton: sets file selector user button action
405 void LyXFileDlg::SetButton(int iIndex, string const & pszName,
406 string const & pszPath)
412 pObject = pFileDlgForm->User1;
413 pTemp = &pszUserPath1;
414 } else if (iIndex == 1) {
415 pObject = pFileDlgForm->User2;
416 pTemp = &pszUserPath2;
419 if (!pszName.empty() && !pszPath.empty()) {
420 fl_set_object_label(pObject, pszName.c_str());
421 fl_show_object(pObject);
424 fl_hide_object(pObject);
430 // GetDirectory: gets last dialog directory
431 string const LyXFileDlg::GetDirectory() const
433 if (!pszDirectory.empty())
440 // RunDialog: handle dialog during file selection
441 bool LyXFileDlg::RunDialog()
443 force_cancel = false;
449 FL_OBJECT * pObject = fl_do_forms();
451 if (pObject == pFileDlgForm->Ready) {
454 } else if (pObject == pFileDlgForm->Cancel
463 // XForms objects callback (static)
464 void LyXFileDlg::FileDlgCB(FL_OBJECT *, long lArgument)
466 if (!pCurrentDlg) return;
470 case 0: // get directory
471 pCurrentDlg->SetDirectory(fl_get_input(pFileDlgForm->DirBox));
472 pCurrentDlg->Reread();
476 pCurrentDlg->SetMask(fl_get_input(pFileDlgForm->PatBox));
477 pCurrentDlg->Reread();
481 pCurrentDlg->HandleListHit();
485 pCurrentDlg->SetDirectory(fl_get_input(pFileDlgForm->DirBox));
486 pCurrentDlg->SetMask(fl_get_input(pFileDlgForm->PatBox));
487 pCurrentDlg->Reread();
491 pCurrentDlg->SetDirectory(GetEnvPath("HOME"));
492 pCurrentDlg->SetMask(fl_get_input(pFileDlgForm->PatBox));
493 pCurrentDlg->Reread();
496 case 12: // user button 1
497 if (!pCurrentDlg->pszUserPath1.empty()) {
498 pCurrentDlg->SetDirectory(pCurrentDlg->pszUserPath1);
499 pCurrentDlg->SetMask(fl_get_input(pFileDlgForm
501 pCurrentDlg->Reread();
505 case 13: // user button 2
506 if (!pCurrentDlg->pszUserPath2.empty()) {
507 pCurrentDlg->SetDirectory(pCurrentDlg->pszUserPath2);
508 pCurrentDlg->SetMask(fl_get_input(pFileDlgForm
510 pCurrentDlg->Reread();
518 extern "C" void C_LyXFileDlg_FileDlgCB(FL_OBJECT * ob, long data)
520 LyXFileDlg::FileDlgCB(ob, data);
524 // Handle callback from list
525 void LyXFileDlg::HandleListHit()
528 int iSelect = fl_get_browser(pFileDlgForm->List);
529 if (iSelect > iDepth) {
530 SetInfoLine(direntries[iSelect - iDepth - 1].pszLsEntry);
532 SetInfoLine(string());
537 // Callback for double click in list
538 void LyXFileDlg::DoubleClickCB(FL_OBJECT *, long)
540 if (pCurrentDlg->HandleDoubleClick())
541 // Simulate click on OK button
542 pCurrentDlg->Force(false);
545 extern "C" void C_LyXFileDlg_DoubleClickCB(FL_OBJECT * ob, long data)
547 LyXFileDlg::DoubleClickCB(ob, data);
550 // Handle double click from list
551 bool LyXFileDlg::HandleDoubleClick()
557 int iSelect = fl_get_browser(pFileDlgForm->List);
558 if (iSelect > iDepth) {
559 pszTemp = direntries[iSelect - iDepth - 1].pszName;
560 SetInfoLine(direntries[iSelect - iDepth - 1].pszLsEntry);
561 if (!suffixIs(pszTemp, '/')) {
563 fl_set_input(pFileDlgForm->Filename, pszTemp.c_str());
565 } else if (iSelect != 0) {
566 SetInfoLine(string());
574 // builds new directory name
575 if (iSelect > iDepth) {
576 // Directory deeper down
577 // First, get directory with trailing /
578 Temp = fl_get_input(pFileDlgForm->DirBox);
579 if (!suffixIs(Temp, '/'))
583 // Directory higher up
585 for (int i = 0; i < iSelect; ++i) {
586 string piece = fl_get_browser_line(pFileDlgForm->List, i+1);
587 // The '+2' is here to count the '@b' (JMarc)
588 Temp += piece.substr(i + 2);
601 // Handle OK button call
602 bool LyXFileDlg::HandleOK()
605 string pszTemp = fl_get_input(pFileDlgForm->PatBox);
606 if (pszTemp!= pszMask) {
612 // directory was changed
613 pszTemp = fl_get_input(pFileDlgForm->DirBox);
614 if (pszTemp!= pszDirectory) {
615 SetDirectory(pszTemp);
620 // Handle return from list
621 int select = fl_get_browser(pFileDlgForm->List);
622 if (select > iDepth) {
623 string temp = direntries[select - iDepth - 1].pszName;
624 if (!suffixIs(temp, '/')) {
625 // If user didn't type anything, use browser
626 string name = fl_get_input(pFileDlgForm->Filename);
628 fl_set_input(pFileDlgForm->Filename, temp.c_str());
634 // Emulate a doubleclick
635 return HandleDoubleClick();
639 // Handle Cancel CB from WM close
640 int LyXFileDlg::CancelCB(FL_FORM *, void *)
642 // Simulate a click on the cancel button
643 pCurrentDlg->Force(true);
648 extern "C" int C_LyXFileDlg_CancelCB(FL_FORM *fl, void *xev)
650 return LyXFileDlg::CancelCB(fl, xev);
654 // Simulates a click on OK/Cancel
655 void LyXFileDlg::Force(bool cancel)
659 fl_set_button(pFileDlgForm->Cancel, 1);
662 fl_set_button(pFileDlgForm->Ready, 1);
664 // Start timer to break fl_do_forms loop soon
665 fl_set_timer(pFileDlgForm->timer, 0.1);
669 // Select: launches dialog and returns selected file
670 string const LyXFileDlg::Select(string const & title, string const & path,
671 string const & mask, string const & suggested)
673 // handles new mask and path
685 fl_select_browser_line(pFileDlgForm->List, 1);
686 fl_set_browser_topline(pFileDlgForm->List, 1);
689 // checks whether dialog can be started
690 if (pCurrentDlg) return string();
694 SetInfoLine (string());
695 fl_set_input(pFileDlgForm->Filename, suggested.c_str());
696 fl_set_button(pFileDlgForm->Cancel, 0);
697 fl_set_button(pFileDlgForm->Ready, 0);
698 fl_set_focus_object(pFileDlgForm->FileDlg, pFileDlgForm->Filename);
699 fl_deactivate_all_forms();
700 fl_show_form(pFileDlgForm->FileDlg, FL_PLACE_MOUSE | FL_FREE_SIZE,
701 FL_FULLBORDER, title.c_str());
705 fl_hide_form(pFileDlgForm->FileDlg);
706 fl_activate_all_forms();
709 // Returns filename or string() if no valid selection was made
710 if (!isOk || !fl_get_input(pFileDlgForm->Filename)[0]) return string();
712 pszFileName = fl_get_input(pFileDlgForm->Filename);
714 if (!AbsolutePath(pszFileName)) {
715 pszFileName = AddName(fl_get_input(pFileDlgForm->DirBox),