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 * ====================================================== */
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 typedef map<uid_t, string> Users;
98 void UserCache::add(uid_t ID) const
101 struct passwd * pEntry;
104 if ((pEntry = getpwuid(ID)))
105 pszNewName = pEntry->pw_name;
107 pszNewName = tostr(ID);
111 users[ID] = pszNewName;
115 /// Group cache class definition
118 /// seeks group name from group ID
119 string const & find(gid_t ID) const {
120 Groups::const_iterator cit = groups.find(ID);
121 if (cit == groups.end()) {
125 return (*cit).second;
129 void add(gid_t ID) const;
131 typedef map<gid_t, string> Groups;
133 mutable Groups groups;
136 void GroupCache::add(gid_t ID) const
139 struct group * pEntry;
142 if ((pEntry = getgrgid(ID))) pszNewName = pEntry->gr_name;
144 pszNewName = tostr(ID);
147 groups[ID] = pszNewName;
151 static UserCache lyxUserCache;
152 static GroupCache lyxGroupCache;
154 // some "C" wrappers around callbacks
155 extern "C" void C_LyXFileDlg_FileDlgCB(FL_OBJECT *, long lArgument);
156 extern "C" void C_LyXFileDlg_DoubleClickCB(FL_OBJECT *, long);
157 extern "C" int C_LyXFileDlg_CancelCB(FL_FORM *, void *);
160 // compares two LyXDirEntry objects content (used for sort)
161 class comp_direntry {
163 int operator()(LyXDirEntry const & r1,
164 LyXDirEntry const & r2) const {
165 bool r1d = suffixIs(r1.pszName, '/');
166 bool r2d = suffixIs(r2.pszName, '/');
167 if (r1d && !r2d) return 1;
168 if (!r1d && r2d) return 0;
169 return r1.pszName < r2.pszName;
174 // *** LyXFileDlg class implementation
177 FD_FileDlg * LyXFileDlg::pFileDlgForm = 0;
178 LyXFileDlg * LyXFileDlg::pCurrentDlg = 0;
181 // Reread: updates dialog list to match class directory
182 void LyXFileDlg::Reread()
185 DIR * pDirectory = opendir(pszDirectory.c_str());
187 WriteFSAlert(_("Warning! Couldn't open directory."),
189 pszDirectory = GetCWD();
190 pDirectory = opendir(pszDirectory.c_str());
193 // Clear the present namelist
197 fl_hide_object(pFileDlgForm->List);
198 fl_clear_browser(pFileDlgForm->List);
199 fl_set_input(pFileDlgForm->DirBox, pszDirectory.c_str());
201 // Splits complete directory name into directories and compute depth
206 string File = pszDirectory;
208 File = split(File, Temp, '/');
210 while (!File.empty() || !Temp.empty()) {
211 string dline = "@b"+line + Temp + '/';
212 fl_add_browser_line(pFileDlgForm->List, dline.c_str());
213 File = split(File, Temp, '/');
218 // Parses all entries of the given subdirectory
219 time_t curTime = time(0);
220 rewinddir(pDirectory);
221 struct dirent * pDirEntry;
222 while ((pDirEntry = readdir(pDirectory))) {
223 bool isLink = false, isDir = false;
225 // If the pattern doesn't start with a dot, skip hidden files
226 if (!pszMask.empty() && pszMask[0] != '.' &&
227 pDirEntry->d_name[0] == '.')
231 string fname = pDirEntry->d_name;
233 // Under all circumstances, "." and ".." are not wanted
234 if (fname == "." || fname == "..")
238 File = AddName(pszDirectory, fname);
240 fileInfo.newFile(File, true);
241 fileInfo.modeString(szMode);
242 unsigned int nlink = fileInfo.getNumberOfLinks();
243 string user = lyxUserCache.find(fileInfo.getUid());
244 string group = lyxGroupCache.find(fileInfo.getGid());
246 time_t modtime = fileInfo.getModificationTime();
247 string Time = ctime(&modtime);
249 if (curTime > fileInfo.getModificationTime() + SIX_MONTH_SEC
250 || curTime < fileInfo.getModificationTime()
252 // The file is fairly old or in the future. POSIX says
253 // the cutoff is 6 months old. Allow a 1 hour slop
254 // factor for what is considered "the future", to
255 // allow for NFS server/client clock disagreement.
256 // Show the year instead of the time of day.
258 Time.erase(15, string::npos);
260 Time.erase(16, string::npos);
263 string Buffer = string(szMode) + ' ' +
267 Time.substr(4, string::npos) + ' ';
269 Buffer += pDirEntry->d_name;
270 Buffer += fileInfo.typeIndicator();
272 if ((isLink = fileInfo.isLink())) {
275 if (LyXReadLink(File, Link)) {
279 // This gives the FileType of the file that
280 // is really pointed too after resolving all
281 // symlinks. This is not necessarily the same
282 // as the type of Link (which could again be a
283 // link). Is that intended?
285 fileInfo.newFile(File);
286 Buffer += fileInfo.typeIndicator();
290 // filters files according to pattern and type
291 if (fileInfo.isRegular()
293 || fileInfo.isBlock()
294 || fileInfo.isFifo()) {
295 if (!regexMatch(fname, pszMask))
297 } else if (!(isDir = fileInfo.isDir()))
302 // Note pszLsEntry is an string!
303 tmp.pszLsEntry = Buffer;
306 if (isDir) temp += '/';
309 // creates displayed name
310 temp = pDirEntry->d_name;
314 temp += fileInfo.typeIndicator();
315 tmp.pszDisplayed = temp;
317 direntries.push_back(tmp);
320 closedir(pDirectory);
323 sort(direntries.begin(), direntries.end(), comp_direntry());
325 // Add them to directory box
326 for (DirEntries::const_iterator cit = direntries.begin();
327 cit != direntries.end(); ++cit) {
328 string temp = line + (*cit).pszDisplayed;
329 fl_add_browser_line(pFileDlgForm->List, temp.c_str());
331 fl_set_browser_topline(pFileDlgForm->List, iDepth);
332 fl_show_object(pFileDlgForm->List);
337 // SetDirectory: sets dialog current directory
338 void LyXFileDlg::SetDirectory(string const & Path)
340 if (!pszDirectory.empty()) {
341 string TempPath = ExpandPath(Path); // Expand ~/
342 TempPath = MakeAbsPath(TempPath, pszDirectory);
343 pszDirectory = MakeAbsPath(TempPath);
344 } else pszDirectory = MakeAbsPath(Path);
348 // SetMask: sets dialog file mask
349 void LyXFileDlg::SetMask(string const & NewMask)
352 fl_set_input(pFileDlgForm->PatBox, pszMask.c_str());
356 // SetInfoLine: sets dialog information line
357 void LyXFileDlg::SetInfoLine(string const & Line)
360 fl_set_object_label(pFileDlgForm->FileInfo, pszInfoLine.c_str());
364 LyXFileDlg::LyXFileDlg()
366 pszDirectory = MakeAbsPath(string("."));
369 // Creates form if necessary.
371 pFileDlgForm = create_form_FileDlg();
372 // Set callbacks. This means that we don't need a patch file
373 fl_set_object_callback(pFileDlgForm->DirBox,
374 C_LyXFileDlg_FileDlgCB, 0);
375 fl_set_object_callback(pFileDlgForm->PatBox,
376 C_LyXFileDlg_FileDlgCB, 1);
377 fl_set_object_callback(pFileDlgForm->List,
378 C_LyXFileDlg_FileDlgCB, 2);
379 fl_set_object_callback(pFileDlgForm->Filename,
380 C_LyXFileDlg_FileDlgCB, 3);
381 fl_set_object_callback(pFileDlgForm->Rescan,
382 C_LyXFileDlg_FileDlgCB, 10);
383 fl_set_object_callback(pFileDlgForm->Home,
384 C_LyXFileDlg_FileDlgCB, 11);
385 fl_set_object_callback(pFileDlgForm->User1,
386 C_LyXFileDlg_FileDlgCB, 12);
387 fl_set_object_callback(pFileDlgForm->User2,
388 C_LyXFileDlg_FileDlgCB, 13);
390 // Make sure pressing the close box doesn't crash LyX. (RvdK)
391 fl_set_form_atclose(pFileDlgForm->FileDlg,
392 C_LyXFileDlg_CancelCB, 0);
393 // Register doubleclick callback
394 fl_set_browser_dblclick_callback(pFileDlgForm->List,
395 C_LyXFileDlg_DoubleClickCB,
398 fl_hide_object(pFileDlgForm->User1);
399 fl_hide_object(pFileDlgForm->User2);
403 // SetButton: sets file selector user button action
404 void LyXFileDlg::SetButton(int iIndex, string const & pszName,
405 string const & pszPath)
411 pObject = pFileDlgForm->User1;
412 pTemp = &pszUserPath1;
413 } else if (iIndex == 1) {
414 pObject = pFileDlgForm->User2;
415 pTemp = &pszUserPath2;
418 if (!pszName.empty() && !pszPath.empty()) {
419 fl_set_object_label(pObject, pszName.c_str());
420 fl_show_object(pObject);
423 fl_hide_object(pObject);
429 // GetDirectory: gets last dialog directory
430 string LyXFileDlg::GetDirectory() const
432 if (!pszDirectory.empty())
439 // RunDialog: handle dialog during file selection
440 bool LyXFileDlg::RunDialog()
442 force_cancel = false;
448 FL_OBJECT * pObject = fl_do_forms();
450 if (pObject == pFileDlgForm->Ready) {
453 } else if (pObject == pFileDlgForm->Cancel
462 // XForms objects callback (static)
463 void LyXFileDlg::FileDlgCB(FL_OBJECT *, long lArgument)
465 if (!pCurrentDlg) return;
469 case 0: // get directory
470 pCurrentDlg->SetDirectory(fl_get_input(pFileDlgForm->DirBox));
471 pCurrentDlg->Reread();
475 pCurrentDlg->SetMask(fl_get_input(pFileDlgForm->PatBox));
476 pCurrentDlg->Reread();
480 pCurrentDlg->HandleListHit();
484 pCurrentDlg->SetDirectory(fl_get_input(pFileDlgForm->DirBox));
485 pCurrentDlg->SetMask(fl_get_input(pFileDlgForm->PatBox));
486 pCurrentDlg->Reread();
490 pCurrentDlg->SetDirectory(GetEnvPath("HOME"));
491 pCurrentDlg->SetMask(fl_get_input(pFileDlgForm->PatBox));
492 pCurrentDlg->Reread();
495 case 12: // user button 1
496 if (!pCurrentDlg->pszUserPath1.empty()) {
497 pCurrentDlg->SetDirectory(pCurrentDlg->pszUserPath1);
498 pCurrentDlg->SetMask(fl_get_input(pFileDlgForm
500 pCurrentDlg->Reread();
504 case 13: // user button 2
505 if (!pCurrentDlg->pszUserPath2.empty()) {
506 pCurrentDlg->SetDirectory(pCurrentDlg->pszUserPath2);
507 pCurrentDlg->SetMask(fl_get_input(pFileDlgForm
509 pCurrentDlg->Reread();
517 extern "C" void C_LyXFileDlg_FileDlgCB(FL_OBJECT * ob, long data)
519 LyXFileDlg::FileDlgCB(ob, data);
523 // Handle callback from list
524 void LyXFileDlg::HandleListHit()
527 int iSelect = fl_get_browser(pFileDlgForm->List);
528 if (iSelect > iDepth) {
529 SetInfoLine(direntries[iSelect - iDepth - 1].pszLsEntry);
531 SetInfoLine(string());
536 // Callback for double click in list
537 void LyXFileDlg::DoubleClickCB(FL_OBJECT *, long)
539 if (pCurrentDlg->HandleDoubleClick())
540 // Simulate click on OK button
541 pCurrentDlg->Force(false);
544 extern "C" void C_LyXFileDlg_DoubleClickCB(FL_OBJECT * ob, long data)
546 LyXFileDlg::DoubleClickCB(ob, data);
549 // Handle double click from list
550 bool LyXFileDlg::HandleDoubleClick()
556 int iSelect = fl_get_browser(pFileDlgForm->List);
557 if (iSelect > iDepth) {
558 pszTemp = direntries[iSelect - iDepth - 1].pszName;
559 SetInfoLine(direntries[iSelect - iDepth - 1].pszLsEntry);
560 if (!suffixIs(pszTemp, '/')) {
562 fl_set_input(pFileDlgForm->Filename, pszTemp.c_str());
564 } else if (iSelect != 0) {
565 SetInfoLine(string());
573 // builds new directory name
574 if (iSelect > iDepth) {
575 // Directory deeper down
576 // First, get directory with trailing /
577 Temp = fl_get_input(pFileDlgForm->DirBox);
578 if (!suffixIs(Temp, '/'))
582 // Directory higher up
584 for (int i = 0; i < iSelect; ++i) {
585 string piece = fl_get_browser_line(pFileDlgForm->List, i+1);
586 // The '+2' is here to count the '@b' (JMarc)
587 Temp += piece.substr(i + 2);
600 // Handle OK button call
601 bool LyXFileDlg::HandleOK()
604 string pszTemp = fl_get_input(pFileDlgForm->PatBox);
605 if (pszTemp!= pszMask) {
611 // directory was changed
612 pszTemp = fl_get_input(pFileDlgForm->DirBox);
613 if (pszTemp!= pszDirectory) {
614 SetDirectory(pszTemp);
619 // Handle return from list
620 int select = fl_get_browser(pFileDlgForm->List);
621 if (select > iDepth) {
622 string temp = direntries[select - iDepth - 1].pszName;
623 if (!suffixIs(temp, '/')) {
624 // If user didn't type anything, use browser
625 string name = fl_get_input(pFileDlgForm->Filename);
627 fl_set_input(pFileDlgForm->Filename, temp.c_str());
633 // Emulate a doubleclick
634 return HandleDoubleClick();
638 // Handle Cancel CB from WM close
639 int LyXFileDlg::CancelCB(FL_FORM *, void *)
641 // Simulate a click on the cancel button
642 pCurrentDlg->Force(true);
647 extern "C" int C_LyXFileDlg_CancelCB(FL_FORM *fl, void *xev)
649 return LyXFileDlg::CancelCB(fl, xev);
653 // Simulates a click on OK/Cancel
654 void LyXFileDlg::Force(bool cancel)
658 fl_set_button(pFileDlgForm->Cancel, 1);
661 fl_set_button(pFileDlgForm->Ready, 1);
663 // Start timer to break fl_do_forms loop soon
664 fl_set_timer(pFileDlgForm->timer, 0.1);
668 // Select: launches dialog and returns selected file
669 string LyXFileDlg::Select(string const & title, string const & path,
670 string const & mask, string const & suggested)
672 // handles new mask and path
684 fl_select_browser_line(pFileDlgForm->List, 1);
685 fl_set_browser_topline(pFileDlgForm->List, 1);
688 // checks whether dialog can be started
689 if (pCurrentDlg) return string();
693 SetInfoLine (string());
694 fl_set_input(pFileDlgForm->Filename, suggested.c_str());
695 fl_set_button(pFileDlgForm->Cancel, 0);
696 fl_set_button(pFileDlgForm->Ready, 0);
697 fl_set_focus_object(pFileDlgForm->FileDlg, pFileDlgForm->Filename);
698 fl_deactivate_all_forms();
699 fl_show_form(pFileDlgForm->FileDlg, FL_PLACE_MOUSE | FL_FREE_SIZE,
700 FL_FULLBORDER, title.c_str());
704 fl_hide_form(pFileDlgForm->FileDlg);
705 fl_activate_all_forms();
708 // Returns filename or string() if no valid selection was made
709 if (!isOk || !fl_get_input(pFileDlgForm->Filename)[0]) return string();
711 pszFileName = fl_get_input(pFileDlgForm->Filename);
713 if (!AbsolutePath(pszFileName)) {
714 pszFileName = AddName(fl_get_input(pFileDlgForm->DirBox),