2 /* This file is part of
3 * ======================================================
5 * LyX, The Document Processor
7 * Copyright 1995 Matthias Ettrich
8 * Copyright 1995-1999 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 struct passwd * pEntry;
97 if ((pEntry = getpwuid(ID)))
98 pszNewName = pEntry->pw_name;
100 pszNewName = tostr(ID);
104 users[ID] = pszNewName;
107 typedef map<uid_t, string> Users;
113 /// Group cache class definition
116 /// seeks group name from group ID
117 string const & find(gid_t ID) const {
118 Groups::const_iterator cit = groups.find(ID);
119 if (cit == groups.end()) {
123 return (*cit).second;
127 void add(gid_t ID) const {
129 struct group * pEntry;
132 if ((pEntry = getgrgid(ID))) pszNewName = pEntry->gr_name;
134 pszNewName = tostr(ID);
137 groups[ID] = pszNewName;
140 typedef map<gid_t, string> Groups;
142 mutable Groups groups;
146 static UserCache lyxUserCache;
147 static GroupCache lyxGroupCache;
149 // some "C" wrappers around callbacks
150 extern "C" void C_LyXFileDlg_FileDlgCB(FL_OBJECT *, long lArgument);
151 extern "C" void C_LyXFileDlg_DoubleClickCB(FL_OBJECT *, long);
152 extern "C" int C_LyXFileDlg_CancelCB(FL_FORM *, void *);
155 // compares two LyXDirEntry objects content (used for sort)
156 class comp_direntry {
158 int operator()(LyXDirEntry const & r1,
159 LyXDirEntry const & r2) const {
160 bool r1d = suffixIs(r1.pszName, '/');
161 bool r2d = suffixIs(r2.pszName, '/');
162 if (r1d && !r2d) return 1;
163 if (!r1d && r2d) return 0;
164 return r1.pszName < r2.pszName;
169 // *** LyXFileDlg class implementation
172 FD_FileDlg * LyXFileDlg::pFileDlgForm = 0;
173 LyXFileDlg * LyXFileDlg::pCurrentDlg = 0;
176 // Reread: updates dialog list to match class directory
177 void LyXFileDlg::Reread()
180 DIR * pDirectory = opendir(pszDirectory.c_str());
182 WriteFSAlert(_("Warning! Couldn't open directory."),
184 pszDirectory = GetCWD();
185 pDirectory = opendir(pszDirectory.c_str());
188 // Clear the present namelist
192 fl_hide_object(pFileDlgForm->List);
193 fl_clear_browser(pFileDlgForm->List);
194 fl_set_input(pFileDlgForm->DirBox, pszDirectory.c_str());
196 // Splits complete directory name into directories and compute depth
201 string File = pszDirectory;
203 File = split(File, Temp, '/');
205 while (!File.empty() || !Temp.empty()) {
206 string dline = "@b"+line + Temp + '/';
207 fl_add_browser_line(pFileDlgForm->List, dline.c_str());
208 File = split(File, Temp, '/');
213 // Parses all entries of the given subdirectory
214 time_t curTime = time(0);
215 rewinddir(pDirectory);
216 struct dirent * pDirEntry;
217 while ((pDirEntry = readdir(pDirectory))) {
218 bool isLink = false, isDir = false;
220 // If the pattern doesn't start with a dot, skip hidden files
221 if (!pszMask.empty() && pszMask[0] != '.' &&
222 pDirEntry->d_name[0] == '.')
226 string fname = pDirEntry->d_name;
228 // Under all circumstances, "." and ".." are not wanted
229 if (fname == "." || fname == "..")
233 File = AddName(pszDirectory, fname);
235 fileInfo.newFile(File, true);
236 fileInfo.modeString(szMode);
237 unsigned int nlink = fileInfo.getNumberOfLinks();
238 string user = lyxUserCache.find(fileInfo.getUid());
239 string group = lyxGroupCache.find(fileInfo.getGid());
241 time_t modtime = fileInfo.getModificationTime();
242 string Time = ctime(&modtime);
244 if (curTime > fileInfo.getModificationTime() + SIX_MONTH_SEC
245 || curTime < fileInfo.getModificationTime()
247 // The file is fairly old or in the future. POSIX says
248 // the cutoff is 6 months old. Allow a 1 hour slop
249 // factor for what is considered "the future", to
250 // allow for NFS server/client clock disagreement.
251 // Show the year instead of the time of day.
253 Time.erase(15, string::npos);
255 Time.erase(16, string::npos);
258 string Buffer = string(szMode) + ' ' +
262 Time.substr(4, string::npos) + ' ';
264 Buffer += pDirEntry->d_name;
265 Buffer += fileInfo.typeIndicator();
267 if ((isLink = fileInfo.isLink())) {
270 if (LyXReadLink(File, Link)) {
274 // This gives the FileType of the file that
275 // is really pointed too after resolving all
276 // symlinks. This is not necessarily the same
277 // as the type of Link (which could again be a
278 // link). Is that intended?
280 fileInfo.newFile(File);
281 Buffer += fileInfo.typeIndicator();
285 // filters files according to pattern and type
286 if (fileInfo.isRegular()
288 || fileInfo.isBlock()
289 || fileInfo.isFifo()) {
290 if (!regexMatch(fname, pszMask))
292 } else if (!(isDir = fileInfo.isDir()))
297 // Note pszLsEntry is an string!
298 tmp.pszLsEntry = Buffer;
301 if (isDir) temp += '/';
304 // creates displayed name
305 temp = pDirEntry->d_name;
309 temp += fileInfo.typeIndicator();
310 tmp.pszDisplayed = temp;
312 direntries.push_back(tmp);
315 closedir(pDirectory);
318 sort(direntries.begin(), direntries.end(), comp_direntry());
320 // Add them to directory box
321 for (DirEntries::const_iterator cit = direntries.begin();
322 cit != direntries.end(); ++cit) {
323 string temp = line + (*cit).pszDisplayed;
324 fl_add_browser_line(pFileDlgForm->List, temp.c_str());
326 fl_set_browser_topline(pFileDlgForm->List, iDepth);
327 fl_show_object(pFileDlgForm->List);
332 // SetDirectory: sets dialog current directory
333 void LyXFileDlg::SetDirectory(string const & Path)
335 if (!pszDirectory.empty()) {
336 string TempPath = ExpandPath(Path); // Expand ~/
337 TempPath = MakeAbsPath(TempPath, pszDirectory);
338 pszDirectory = MakeAbsPath(TempPath);
339 } else pszDirectory = MakeAbsPath(Path);
343 // SetMask: sets dialog file mask
344 void LyXFileDlg::SetMask(string const & NewMask)
347 fl_set_input(pFileDlgForm->PatBox, pszMask.c_str());
351 // SetInfoLine: sets dialog information line
352 void LyXFileDlg::SetInfoLine(string const & Line)
355 fl_set_object_label(pFileDlgForm->FileInfo, pszInfoLine.c_str());
359 LyXFileDlg::LyXFileDlg()
361 pszDirectory = MakeAbsPath(string("."));
364 // Creates form if necessary.
366 pFileDlgForm = create_form_FileDlg();
367 // Set callbacks. This means that we don't need a patch file
368 fl_set_object_callback(pFileDlgForm->DirBox,
369 C_LyXFileDlg_FileDlgCB, 0);
370 fl_set_object_callback(pFileDlgForm->PatBox,
371 C_LyXFileDlg_FileDlgCB, 1);
372 fl_set_object_callback(pFileDlgForm->List,
373 C_LyXFileDlg_FileDlgCB, 2);
374 fl_set_object_callback(pFileDlgForm->Filename,
375 C_LyXFileDlg_FileDlgCB, 3);
376 fl_set_object_callback(pFileDlgForm->Rescan,
377 C_LyXFileDlg_FileDlgCB, 10);
378 fl_set_object_callback(pFileDlgForm->Home,
379 C_LyXFileDlg_FileDlgCB, 11);
380 fl_set_object_callback(pFileDlgForm->User1,
381 C_LyXFileDlg_FileDlgCB, 12);
382 fl_set_object_callback(pFileDlgForm->User2,
383 C_LyXFileDlg_FileDlgCB, 13);
385 // Make sure pressing the close box doesn't crash LyX. (RvdK)
386 fl_set_form_atclose(pFileDlgForm->FileDlg,
387 C_LyXFileDlg_CancelCB, 0);
388 // Register doubleclick callback
389 fl_set_browser_dblclick_callback(pFileDlgForm->List,
390 C_LyXFileDlg_DoubleClickCB,
393 fl_hide_object(pFileDlgForm->User1);
394 fl_hide_object(pFileDlgForm->User2);
398 // SetButton: sets file selector user button action
399 void LyXFileDlg::SetButton(int iIndex, string const & pszName,
400 string const & pszPath)
406 pObject = pFileDlgForm->User1;
407 pTemp = &pszUserPath1;
408 } else if (iIndex == 1) {
409 pObject = pFileDlgForm->User2;
410 pTemp = &pszUserPath2;
413 if (!pszName.empty() && !pszPath.empty()) {
414 fl_set_object_label(pObject, pszName.c_str());
415 fl_show_object(pObject);
418 fl_hide_object(pObject);
424 // GetDirectory: gets last dialog directory
425 string LyXFileDlg::GetDirectory() const
427 if (!pszDirectory.empty())
434 // RunDialog: handle dialog during file selection
435 bool LyXFileDlg::RunDialog()
437 force_cancel = false;
443 FL_OBJECT * pObject = fl_do_forms();
445 if (pObject == pFileDlgForm->Ready) {
448 } else if (pObject == pFileDlgForm->Cancel
457 // XForms objects callback (static)
458 void LyXFileDlg::FileDlgCB(FL_OBJECT *, long lArgument)
460 if (!pCurrentDlg) return;
464 case 0: // get directory
465 pCurrentDlg->SetDirectory(fl_get_input(pFileDlgForm->DirBox));
466 pCurrentDlg->Reread();
470 pCurrentDlg->SetMask(fl_get_input(pFileDlgForm->PatBox));
471 pCurrentDlg->Reread();
475 pCurrentDlg->HandleListHit();
479 pCurrentDlg->SetDirectory(fl_get_input(pFileDlgForm->DirBox));
480 pCurrentDlg->SetMask(fl_get_input(pFileDlgForm->PatBox));
481 pCurrentDlg->Reread();
485 pCurrentDlg->SetDirectory(GetEnvPath("HOME"));
486 pCurrentDlg->SetMask(fl_get_input(pFileDlgForm->PatBox));
487 pCurrentDlg->Reread();
490 case 12: // user button 1
491 if (!pCurrentDlg->pszUserPath1.empty()) {
492 pCurrentDlg->SetDirectory(pCurrentDlg->pszUserPath1);
493 pCurrentDlg->SetMask(fl_get_input(pFileDlgForm
495 pCurrentDlg->Reread();
499 case 13: // user button 2
500 if (!pCurrentDlg->pszUserPath2.empty()) {
501 pCurrentDlg->SetDirectory(pCurrentDlg->pszUserPath2);
502 pCurrentDlg->SetMask(fl_get_input(pFileDlgForm
504 pCurrentDlg->Reread();
511 extern "C" void C_LyXFileDlg_FileDlgCB(FL_OBJECT * ob, long data)
513 LyXFileDlg::FileDlgCB(ob, data);
517 // Handle callback from list
518 void LyXFileDlg::HandleListHit()
521 int iSelect = fl_get_browser(pFileDlgForm->List);
522 if (iSelect > iDepth) {
523 SetInfoLine(direntries[iSelect - iDepth - 1].pszLsEntry);
525 SetInfoLine(string());
530 // Callback for double click in list
531 void LyXFileDlg::DoubleClickCB(FL_OBJECT *, long)
533 if (pCurrentDlg->HandleDoubleClick())
534 // Simulate click on OK button
535 pCurrentDlg->Force(false);
538 extern "C" void C_LyXFileDlg_DoubleClickCB(FL_OBJECT * ob, long data)
540 LyXFileDlg::DoubleClickCB(ob, data);
543 // Handle double click from list
544 bool LyXFileDlg::HandleDoubleClick()
550 int iSelect = fl_get_browser(pFileDlgForm->List);
551 if (iSelect > iDepth) {
552 pszTemp = direntries[iSelect - iDepth - 1].pszName;
553 SetInfoLine(direntries[iSelect - iDepth - 1].pszLsEntry);
554 if (!suffixIs(pszTemp, '/')) {
556 fl_set_input(pFileDlgForm->Filename, pszTemp.c_str());
558 } else if (iSelect != 0) {
559 SetInfoLine(string());
567 // builds new directory name
568 if (iSelect > iDepth) {
569 // Directory deeper down
570 // First, get directory with trailing /
571 Temp = fl_get_input(pFileDlgForm->DirBox);
572 if (!suffixIs(Temp, '/'))
576 // Directory higher up
578 for (int i = 0; i < iSelect; ++i) {
579 string piece = fl_get_browser_line(pFileDlgForm->List, i+1);
580 // The '+2' is here to count the '@b' (JMarc)
581 Temp += piece.substr(i + 2);
594 // Handle OK button call
595 bool LyXFileDlg::HandleOK()
598 string pszTemp = fl_get_input(pFileDlgForm->PatBox);
599 if (pszTemp!= pszMask) {
605 // directory was changed
606 pszTemp = fl_get_input(pFileDlgForm->DirBox);
607 if (pszTemp!= pszDirectory) {
608 SetDirectory(pszTemp);
613 // Handle return from list
614 int select = fl_get_browser(pFileDlgForm->List);
615 if (select > iDepth) {
616 string temp = direntries[select - iDepth - 1].pszName;
617 if (!suffixIs(temp, '/')) {
618 // If user didn't type anything, use browser
619 string name = fl_get_input(pFileDlgForm->Filename);
621 fl_set_input(pFileDlgForm->Filename, temp.c_str());
627 // Emulate a doubleclick
628 return HandleDoubleClick();
632 // Handle Cancel CB from WM close
633 int LyXFileDlg::CancelCB(FL_FORM *, void *)
635 // Simulate a click on the cancel button
636 pCurrentDlg->Force(true);
641 extern "C" int C_LyXFileDlg_CancelCB(FL_FORM *fl, void *xev)
643 return LyXFileDlg::CancelCB(fl, xev);
647 // Simulates a click on OK/Cancel
648 void LyXFileDlg::Force(bool cancel)
652 fl_set_button(pFileDlgForm->Cancel, 1);
655 fl_set_button(pFileDlgForm->Ready, 1);
657 // Start timer to break fl_do_forms loop soon
658 fl_set_timer(pFileDlgForm->timer, 0.1);
662 // Select: launches dialog and returns selected file
663 string LyXFileDlg::Select(string const & title, string const & path,
664 string const & mask, string const & suggested)
666 // handles new mask and path
678 fl_select_browser_line(pFileDlgForm->List, 1);
679 fl_set_browser_topline(pFileDlgForm->List, 1);
682 // checks whether dialog can be started
683 if (pCurrentDlg) return string();
687 SetInfoLine (string());
688 fl_set_input(pFileDlgForm->Filename, suggested.c_str());
689 fl_set_button(pFileDlgForm->Cancel, 0);
690 fl_set_button(pFileDlgForm->Ready, 0);
691 fl_set_focus_object(pFileDlgForm->FileDlg, pFileDlgForm->Filename);
692 fl_deactivate_all_forms();
693 fl_show_form(pFileDlgForm->FileDlg, FL_PLACE_MOUSE | FL_FREE_SIZE,
694 FL_FULLBORDER, title.c_str());
698 fl_hide_form(pFileDlgForm->FileDlg);
699 fl_activate_all_forms();
702 // Returns filename or string() if no valid selection was made
703 if (!isOk || !fl_get_input(pFileDlgForm->Filename)[0]) return string();
705 pszFileName = fl_get_input(pFileDlgForm->Filename);
707 if (!AbsolutePath(pszFileName)) {
708 pszFileName = AddName(fl_get_input(pFileDlgForm->DirBox),