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 * ====================================================== */
24 #include "lyx_gui_misc.h" // CancelCloseCB
25 #include "support/FileInfo.h"
34 # define NAMLEN(dirent) strlen((dirent)->d_name)
36 # define dirent direct
37 # define NAMLEN(dirent) (dirent)->d_namlen
39 # include <sys/ndir.h>
49 #if TIME_WITH_SYS_TIME
50 # include <sys/time.h>
54 # include <sys/time.h>
61 extern "C" int gettimeofday(struct timeval *, struct timezone *);
62 #define remove(a) unlink(a)
66 #pragma implementation
69 #include "support/filetools.h"
72 // six months, in seconds
73 static const long SIX_MONTH_SEC = 6L * 30L * 24L * 60L * 60L;
74 static const long ONE_HOUR_SEC = 60L * 60L;
76 // *** User cache class implementation
77 /// User cache class definition
80 /// seeks user name from group ID
81 string const & find(uid_t ID) const {
82 Users::const_iterator cit = users.find(ID);
83 if (cit == users.end()) {
91 void add(uid_t ID) const {
93 struct passwd * pEntry;
96 if ((pEntry = getpwuid(ID)))
97 pszNewName = pEntry->pw_name;
99 pszNewName = tostr(ID);
103 users[ID] = pszNewName;
106 typedef map<uid_t, string> Users;
112 /// Group cache class definition
115 /// seeks group name from group ID
116 string const & find(gid_t ID) const {
117 Groups::const_iterator cit = groups.find(ID);
118 if (cit == groups.end()) {
122 return (*cit).second;
126 void add(gid_t ID) const {
128 struct group * pEntry;
131 if ((pEntry = getgrgid(ID))) pszNewName = pEntry->gr_name;
133 pszNewName = tostr(ID);
136 groups[ID] = pszNewName;
139 typedef map<gid_t, string> Groups;
141 mutable Groups groups;
145 static UserCache lyxUserCache;
146 static GroupCache lyxGroupCache;
148 // some "C" wrappers around callbacks
149 extern "C" void C_LyXFileDlg_FileDlgCB(FL_OBJECT *, long lArgument);
150 extern "C" void C_LyXFileDlg_DoubleClickCB(FL_OBJECT *, long);
151 extern "C" int C_LyXFileDlg_CancelCB(FL_FORM *, void *);
154 // compares two LyXDirEntry objects content (used for sort)
155 class comp_direntry {
157 int operator()(LyXDirEntry const & r1,
158 LyXDirEntry const & r2) const {
159 bool r1d = suffixIs(r1.pszName, '/');
160 bool r2d = suffixIs(r2.pszName, '/');
161 if (r1d && !r2d) return 1;
162 if (!r1d && r2d) return 0;
163 return r1.pszName < r2.pszName;
168 // *** LyXFileDlg class implementation
171 FD_FileDlg * LyXFileDlg::pFileDlgForm = 0;
172 LyXFileDlg * LyXFileDlg::pCurrentDlg = 0;
175 // Reread: updates dialog list to match class directory
176 void LyXFileDlg::Reread()
179 DIR * pDirectory = opendir(pszDirectory.c_str());
181 WriteFSAlert(_("Warning! Couldn't open directory."),
183 pszDirectory = GetCWD();
184 pDirectory = opendir(pszDirectory.c_str());
187 // Clear the present namelist
191 fl_hide_object(pFileDlgForm->List);
192 fl_clear_browser(pFileDlgForm->List);
193 fl_set_input(pFileDlgForm->DirBox, pszDirectory.c_str());
195 // Splits complete directory name into directories and compute depth
200 string File = pszDirectory;
202 File = split(File, Temp, '/');
204 while (!File.empty() || !Temp.empty()) {
205 string dline = "@b"+line + Temp + '/';
206 fl_add_browser_line(pFileDlgForm->List, dline.c_str());
207 File = split(File, Temp, '/');
212 // Parses all entries of the given subdirectory
213 time_t curTime = time(0);
214 rewinddir(pDirectory);
215 struct dirent * pDirEntry;
216 while ((pDirEntry = readdir(pDirectory))) {
217 bool isLink = false, isDir = false;
219 // If the pattern doesn't start with a dot, skip hidden files
220 if (!pszMask.empty() && pszMask[0] != '.' &&
221 pDirEntry->d_name[0] == '.')
225 string fname = pDirEntry->d_name;
227 // Under all circumstances, "." and ".." are not wanted
228 if (fname == "." || fname == "..")
232 File = AddName(pszDirectory, fname);
234 fileInfo.newFile(File, true);
235 fileInfo.modeString(szMode);
236 unsigned int nlink = fileInfo.getNumberOfLinks();
237 string user = lyxUserCache.find(fileInfo.getUid());
238 string group = lyxGroupCache.find(fileInfo.getGid());
240 time_t modtime = fileInfo.getModificationTime();
241 string Time = ctime(&modtime);
243 if (curTime > fileInfo.getModificationTime() + SIX_MONTH_SEC
244 || curTime < fileInfo.getModificationTime()
246 // The file is fairly old or in the future. POSIX says
247 // the cutoff is 6 months old. Allow a 1 hour slop
248 // factor for what is considered "the future", to
249 // allow for NFS server/client clock disagreement.
250 // Show the year instead of the time of day.
252 Time.erase(15, string::npos);
254 Time.erase(16, string::npos);
257 string Buffer = string(szMode) + ' ' +
261 Time.substr(4, string::npos) + ' ';
263 Buffer += pDirEntry->d_name;
264 Buffer += fileInfo.typeIndicator();
266 if ((isLink = fileInfo.isLink())) {
269 if (LyXReadLink(File, Link)) {
273 // This gives the FileType of the file that
274 // is really pointed too after resolving all
275 // symlinks. This is not necessarily the same
276 // as the type of Link (which could again be a
277 // link). Is that intended?
279 fileInfo.newFile(File);
280 Buffer += fileInfo.typeIndicator();
284 // filters files according to pattern and type
285 if (fileInfo.isRegular()
287 || fileInfo.isBlock()
288 || fileInfo.isFifo()) {
289 if (!regexMatch(fname, pszMask))
291 } else if (!(isDir = fileInfo.isDir()))
296 // Note pszLsEntry is an string!
297 tmp.pszLsEntry = Buffer;
300 if (isDir) temp += '/';
303 // creates displayed name
304 temp = pDirEntry->d_name;
308 temp += fileInfo.typeIndicator();
309 tmp.pszDisplayed = temp;
311 direntries.push_back(tmp);
314 closedir(pDirectory);
317 sort(direntries.begin(), direntries.end(), comp_direntry());
319 // Add them to directory box
320 for (DirEntries::const_iterator cit = direntries.begin();
321 cit != direntries.end(); ++cit) {
322 string temp = line + (*cit).pszDisplayed;
323 fl_add_browser_line(pFileDlgForm->List, temp.c_str());
325 fl_set_browser_topline(pFileDlgForm->List, iDepth);
326 fl_show_object(pFileDlgForm->List);
331 // SetDirectory: sets dialog current directory
332 void LyXFileDlg::SetDirectory(string const & Path)
334 if (!pszDirectory.empty()) {
335 string TempPath = ExpandPath(Path); // Expand ~/
336 TempPath = MakeAbsPath(TempPath, pszDirectory);
337 pszDirectory = MakeAbsPath(TempPath);
338 } else pszDirectory = MakeAbsPath(Path);
342 // SetMask: sets dialog file mask
343 void LyXFileDlg::SetMask(string const & NewMask)
346 fl_set_input(pFileDlgForm->PatBox, pszMask.c_str());
350 // SetInfoLine: sets dialog information line
351 void LyXFileDlg::SetInfoLine(string const & Line)
354 fl_set_object_label(pFileDlgForm->FileInfo, pszInfoLine.c_str());
358 LyXFileDlg::LyXFileDlg()
360 pszDirectory = MakeAbsPath(string("."));
363 // Creates form if necessary.
365 pFileDlgForm = create_form_FileDlg();
366 // Set callbacks. This means that we don't need a patch file
367 fl_set_object_callback(pFileDlgForm->DirBox,
368 C_LyXFileDlg_FileDlgCB, 0);
369 fl_set_object_callback(pFileDlgForm->PatBox,
370 C_LyXFileDlg_FileDlgCB, 1);
371 fl_set_object_callback(pFileDlgForm->List,
372 C_LyXFileDlg_FileDlgCB, 2);
373 fl_set_object_callback(pFileDlgForm->Filename,
374 C_LyXFileDlg_FileDlgCB, 3);
375 fl_set_object_callback(pFileDlgForm->Rescan,
376 C_LyXFileDlg_FileDlgCB, 10);
377 fl_set_object_callback(pFileDlgForm->Home,
378 C_LyXFileDlg_FileDlgCB, 11);
379 fl_set_object_callback(pFileDlgForm->User1,
380 C_LyXFileDlg_FileDlgCB, 12);
381 fl_set_object_callback(pFileDlgForm->User2,
382 C_LyXFileDlg_FileDlgCB, 13);
384 // Make sure pressing the close box doesn't crash LyX. (RvdK)
385 fl_set_form_atclose(pFileDlgForm->FileDlg,
386 C_LyXFileDlg_CancelCB, 0);
387 // Register doubleclick callback
388 fl_set_browser_dblclick_callback(pFileDlgForm->List,
389 C_LyXFileDlg_DoubleClickCB,
392 fl_hide_object(pFileDlgForm->User1);
393 fl_hide_object(pFileDlgForm->User2);
397 // SetButton: sets file selector user button action
398 void LyXFileDlg::SetButton(int iIndex, string const & pszName,
399 string const & pszPath)
405 pObject = pFileDlgForm->User1;
406 pTemp = &pszUserPath1;
407 } else if (iIndex == 1) {
408 pObject = pFileDlgForm->User2;
409 pTemp = &pszUserPath2;
412 if (!pszName.empty() && !pszPath.empty()) {
413 fl_set_object_label(pObject, pszName.c_str());
414 fl_show_object(pObject);
417 fl_hide_object(pObject);
423 // GetDirectory: gets last dialog directory
424 string LyXFileDlg::GetDirectory() const
426 if (!pszDirectory.empty())
433 // RunDialog: handle dialog during file selection
434 bool LyXFileDlg::RunDialog()
436 force_cancel = false;
442 FL_OBJECT * pObject = fl_do_forms();
444 if (pObject == pFileDlgForm->Ready) {
447 } else if (pObject == pFileDlgForm->Cancel
456 // XForms objects callback (static)
457 void LyXFileDlg::FileDlgCB(FL_OBJECT *, long lArgument)
459 if (!pCurrentDlg) return;
463 case 0: // get directory
464 pCurrentDlg->SetDirectory(fl_get_input(pFileDlgForm->DirBox));
465 pCurrentDlg->Reread();
469 pCurrentDlg->SetMask(fl_get_input(pFileDlgForm->PatBox));
470 pCurrentDlg->Reread();
474 pCurrentDlg->HandleListHit();
478 pCurrentDlg->SetDirectory(fl_get_input(pFileDlgForm->DirBox));
479 pCurrentDlg->SetMask(fl_get_input(pFileDlgForm->PatBox));
480 pCurrentDlg->Reread();
484 pCurrentDlg->SetDirectory(GetEnvPath("HOME"));
485 pCurrentDlg->SetMask(fl_get_input(pFileDlgForm->PatBox));
486 pCurrentDlg->Reread();
489 case 12: // user button 1
490 if (!pCurrentDlg->pszUserPath1.empty()) {
491 pCurrentDlg->SetDirectory(pCurrentDlg->pszUserPath1);
492 pCurrentDlg->SetMask(fl_get_input(pFileDlgForm
494 pCurrentDlg->Reread();
498 case 13: // user button 2
499 if (!pCurrentDlg->pszUserPath2.empty()) {
500 pCurrentDlg->SetDirectory(pCurrentDlg->pszUserPath2);
501 pCurrentDlg->SetMask(fl_get_input(pFileDlgForm
503 pCurrentDlg->Reread();
510 extern "C" void C_LyXFileDlg_FileDlgCB(FL_OBJECT * ob, long data)
512 LyXFileDlg::FileDlgCB(ob, data);
516 // Handle callback from list
517 void LyXFileDlg::HandleListHit()
520 int iSelect = fl_get_browser(pFileDlgForm->List);
521 if (iSelect > iDepth) {
522 SetInfoLine(direntries[iSelect - iDepth - 1].pszLsEntry);
524 SetInfoLine(string());
529 // Callback for double click in list
530 void LyXFileDlg::DoubleClickCB(FL_OBJECT *, long)
532 if (pCurrentDlg->HandleDoubleClick())
533 // Simulate click on OK button
534 pCurrentDlg->Force(false);
537 extern "C" void C_LyXFileDlg_DoubleClickCB(FL_OBJECT * ob, long data)
539 LyXFileDlg::DoubleClickCB(ob, data);
542 // Handle double click from list
543 bool LyXFileDlg::HandleDoubleClick()
549 int iSelect = fl_get_browser(pFileDlgForm->List);
550 if (iSelect > iDepth) {
551 pszTemp = direntries[iSelect - iDepth - 1].pszName;
552 SetInfoLine(direntries[iSelect - iDepth - 1].pszLsEntry);
553 if (!suffixIs(pszTemp, '/')) {
555 fl_set_input(pFileDlgForm->Filename, pszTemp.c_str());
557 } else if (iSelect != 0) {
558 SetInfoLine(string());
566 // builds new directory name
567 if (iSelect > iDepth) {
568 // Directory deeper down
569 // First, get directory with trailing /
570 Temp = fl_get_input(pFileDlgForm->DirBox);
571 if (!suffixIs(Temp, '/'))
575 // Directory higher up
577 for (int i = 0; i < iSelect; ++i) {
578 string piece = fl_get_browser_line(pFileDlgForm->List, i+1);
579 // The '+2' is here to count the '@b' (JMarc)
580 Temp += piece.substr(i + 2);
593 // Handle OK button call
594 bool LyXFileDlg::HandleOK()
597 string pszTemp = fl_get_input(pFileDlgForm->PatBox);
598 if (pszTemp!= pszMask) {
604 // directory was changed
605 pszTemp = fl_get_input(pFileDlgForm->DirBox);
606 if (pszTemp!= pszDirectory) {
607 SetDirectory(pszTemp);
612 // Handle return from list
613 int select = fl_get_browser(pFileDlgForm->List);
614 if (select > iDepth) {
615 string temp = direntries[select - iDepth - 1].pszName;
616 if (!suffixIs(temp, '/')) {
617 // If user didn't type anything, use browser
618 string name = fl_get_input(pFileDlgForm->Filename);
620 fl_set_input(pFileDlgForm->Filename, temp.c_str());
626 // Emulate a doubleclick
627 return HandleDoubleClick();
631 // Handle Cancel CB from WM close
632 int LyXFileDlg::CancelCB(FL_FORM *, void *)
634 // Simulate a click on the cancel button
635 pCurrentDlg->Force(true);
640 extern "C" int C_LyXFileDlg_CancelCB(FL_FORM *fl, void *xev)
642 return LyXFileDlg::CancelCB(fl, xev);
646 // Simulates a click on OK/Cancel
647 void LyXFileDlg::Force(bool cancel)
651 fl_set_button(pFileDlgForm->Cancel, 1);
654 fl_set_button(pFileDlgForm->Ready, 1);
656 // Start timer to break fl_do_forms loop soon
657 fl_set_timer(pFileDlgForm->timer, 0.1);
661 // Select: launches dialog and returns selected file
662 string LyXFileDlg::Select(string const & title, string const & path,
663 string const & mask, string const & suggested)
665 // handles new mask and path
677 fl_select_browser_line(pFileDlgForm->List, 1);
678 fl_set_browser_topline(pFileDlgForm->List, 1);
681 // checks whether dialog can be started
682 if (pCurrentDlg) return string();
686 SetInfoLine (string());
687 fl_set_input(pFileDlgForm->Filename, suggested.c_str());
688 fl_set_button(pFileDlgForm->Cancel, 0);
689 fl_set_button(pFileDlgForm->Ready, 0);
690 fl_set_focus_object(pFileDlgForm->FileDlg, pFileDlgForm->Filename);
691 fl_deactivate_all_forms();
692 fl_show_form(pFileDlgForm->FileDlg, FL_PLACE_MOUSE | FL_FREE_SIZE,
693 FL_FULLBORDER, title.c_str());
697 fl_hide_form(pFileDlgForm->FileDlg);
698 fl_activate_all_forms();
701 // Returns filename or string() if no valid selection was made
702 if (!isOk || !fl_get_input(pFileDlgForm->Filename)[0]) return string();
704 pszFileName = fl_get_input(pFileDlgForm->Filename);
706 if (!AbsolutePath(pszFileName)) {
707 pszFileName = AddName(fl_get_input(pFileDlgForm->DirBox),