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 * ======================================================*/
21 #include "lyx_gui_misc.h" // CancelCloseCB
22 #include "support/FileInfo.h"
31 # define NAMLEN(dirent) strlen((dirent)->d_name)
33 # define dirent direct
34 # define NAMLEN(dirent) (dirent)->d_namlen
36 # include <sys/ndir.h>
46 #if TIME_WITH_SYS_TIME
47 # include <sys/time.h>
51 # include <sys/time.h>
58 extern "C" int gettimeofday(struct timeval *,struct timezone *);
59 #define remove(a) unlink(a)
63 #pragma implementation
66 #include "support/filetools.h"
68 #include "definitions.h"
70 static const long SIX_MONTH_SEC = 6L * 30L * 24L * 60L * 60L; // six months, in seconds
71 static const long ONE_HOUR_SEC = 60L * 60L;
73 // *** User cache class implementation
75 // global instance (user cache root)
76 UserCache lyxUserCache = UserCache(string(),0,0);
79 // Add: creates a new user entry
80 UserCache * UserCache::Add(uid_t ID)
83 struct passwd * pEntry;
86 if ((pEntry = getpwuid(ID)))
87 pszNewName = pEntry->pw_name;
89 pszNewName = tostr(ID);
93 return new UserCache(pszNewName, ID, pRoot);
97 UserCache::UserCache(string const & pszName, uid_t ID, UserCache * pRoot)
102 pNext = pRoot->pNext;
110 this->pszName = pszName;
115 UserCache::~UserCache()
117 if (pNext) delete pNext;
121 // Find: seeks user name from user ID
122 string UserCache::Find(uid_t ID)
124 if ((!pszName.empty()) && (this->ID == ID)) return pszName;
125 if (pNext) return pNext->Find(ID);
127 return pRoot->Add(ID)->pszName;
131 // *** Group cache class implementation
133 // global instance (group cache root)
134 GroupCache lyxGroupCache = GroupCache(string(),0,0);
136 // Add: creates a new group entry
137 GroupCache * GroupCache::Add(gid_t ID)
140 struct group * pEntry;
143 if ((pEntry = getgrgid(ID))) pszNewName = pEntry->gr_name;
145 pszNewName = tostr(ID);
149 return new GroupCache(pszNewName, ID, pRoot);
153 GroupCache::GroupCache(string const & pszName, gid_t ID, GroupCache * pRoot)
158 pNext = pRoot->pNext;
166 this->pszName = pszName;
171 GroupCache::~GroupCache()
173 if (pNext) delete pNext;
177 // Find: seeks group name from group ID
178 string GroupCache::Find(gid_t ID)
180 if ((!pszName.empty()) && (this->ID == ID)) return pszName;
181 if (pNext) return pNext->Find(ID);
183 return pRoot->Add(ID)->pszName;
186 // *** LyXDirEntry internal structure implementation
188 // ldeCompProc: compares two LyXDirEntry objects content (used for qsort)
189 int LyXDirEntry::ldeCompProc(const LyXDirEntry * r1,
190 const LyXDirEntry * r2)
192 bool r1d = suffixIs(r1->pszName, '/');
193 bool r2d = suffixIs(r2->pszName, '/');
194 if (r1d && !r2d) return -1;
195 if (!r1d && r2d) return 1;
196 return r1->pszName.compare(r2->pszName);
199 // *** LyXFileDlg class implementation
202 FD_FileDlg * LyXFileDlg::pFileDlgForm = 0;
203 LyXFileDlg * LyXFileDlg::pCurrentDlg = 0;
206 // Reread: updates dialog list to match class directory
207 void LyXFileDlg::Reread()
211 struct dirent * pDirEntry;
219 pDirectory = opendir(pszDirectory.c_str());
221 WriteFSAlert(_("Warning! Couldn't open directory."),
223 pszDirectory = GetCWD();
224 pDirectory = opendir(pszDirectory.c_str());
227 // Clear the present namelist
229 delete [] pCurrentNames;
234 fl_hide_object(pFileDlgForm->List);
235 fl_clear_browser(pFileDlgForm->List);
236 fl_set_input(pFileDlgForm->DirBox, pszDirectory.c_str());
238 // Splits complete directory name into directories and compute depth
243 File = split(File, Temp, '/');
245 while (!File.empty() || !Temp.empty()) {
246 string dline = "@b"+line + Temp + '/';
247 fl_add_browser_line(pFileDlgForm->List, dline.c_str());
248 File = split(File, Temp, '/');
253 // Allocate names array
255 rewinddir(pDirectory);
256 while ((readdir(pDirectory))) ++iNumNames;
257 pCurrentNames = new LyXDirEntry[iNumNames];
259 // Parses all entries of the given subdirectory
261 time_t curTime = time(0);
262 rewinddir(pDirectory);
263 while ((pDirEntry = readdir(pDirectory))) {
265 bool isLink = false, isDir = false;
267 // If the pattern doesn't start with a dot, skip hidden files
268 if (!pszMask.empty() && pszMask[0] != '.' &&
269 pDirEntry->d_name[0] == '.')
273 string fname = pDirEntry->d_name;
275 // Under all circumstances, "." and ".." are not wanted
276 if (fname == "." || fname == "..")
280 File = AddName(pszDirectory, fname);
282 fileInfo.newFile(File, true);
283 fileInfo.modeString(szMode);
284 unsigned int nlink = fileInfo.getNumberOfLinks();
285 string user = lyxUserCache.Find(fileInfo.getUid());
286 string group = lyxGroupCache.Find(fileInfo.getGid());
288 time_t modtime = fileInfo.getModificationTime();
289 Time = ctime(&modtime);
291 if (curTime > fileInfo.getModificationTime() + SIX_MONTH_SEC
292 || curTime < fileInfo.getModificationTime()
294 // The file is fairly old or in the future. POSIX says
295 // the cutoff is 6 months old. Allow a 1 hour slop
296 // factor for what is considered "the future", to
297 // allow for NFS server/client clock disagreement.
298 // Show the year instead of the time of day.
300 Time.erase(15, string::npos);
302 Time.erase(16, string::npos);
305 Buffer = string(szMode) + ' ' +
309 Time.substr(4, string::npos) + ' ';
311 Buffer += pDirEntry->d_name;
312 Buffer += fileInfo.typeIndicator();
314 if ((isLink = fileInfo.isLink())) {
317 if (LyXReadLink(File,Link)) {
321 // This gives the FileType of the file that
322 // is really pointed too after resolving all
323 // symlinks. This is not necessarily the same
324 // as the type of Link (which could again be a
325 // link). Is that intended?
327 fileInfo.newFile(File);
328 Buffer += fileInfo.typeIndicator();
332 // filters files according to pattern and type
333 if (fileInfo.isRegular()
335 || fileInfo.isBlock()
336 || fileInfo.isFifo()) {
337 if (!regexMatch(fname, pszMask))
339 } else if (!(isDir = fileInfo.isDir()))
342 // Note pszLsEntry is an string!
343 pCurrentNames[iNumNames].pszLsEntry = Buffer;
347 if (isDir) temp += '/';
348 pCurrentNames[iNumNames].pszName = temp;
350 // creates displayed name
351 temp = pDirEntry->d_name;
355 temp += fileInfo.typeIndicator();
357 pCurrentNames[iNumNames++].pszDisplayed = temp;
360 closedir(pDirectory);
363 qsort(pCurrentNames, iNumNames, sizeof(LyXDirEntry),
364 (int (*)(const void *, const void *))LyXDirEntry::ldeCompProc);
366 // Add them to directory box
367 for (i = 0; i < iNumNames; ++i) {
368 string temp = line + pCurrentNames[i].pszDisplayed;
369 fl_add_browser_line(pFileDlgForm->List, temp.c_str());
371 fl_set_browser_topline(pFileDlgForm->List,iDepth);
372 fl_show_object(pFileDlgForm->List);
377 // SetDirectory: sets dialog current directory
378 void LyXFileDlg::SetDirectory(string const & Path)
380 if (!pszDirectory.empty()) {
381 string TempPath = ExpandPath(Path); // Expand ~/
382 TempPath = MakeAbsPath(TempPath, pszDirectory);
383 pszDirectory = MakeAbsPath(TempPath);
384 } else pszDirectory = MakeAbsPath(Path);
388 // SetMask: sets dialog file mask
389 void LyXFileDlg::SetMask(string const & NewMask)
392 fl_set_input(pFileDlgForm->PatBox, pszMask.c_str());
396 // SetInfoLine: sets dialog information line
397 void LyXFileDlg::SetInfoLine(string const & Line)
400 fl_set_object_label(pFileDlgForm->FileInfo, pszInfoLine.c_str());
404 LyXFileDlg::LyXFileDlg()
407 pszDirectory = MakeAbsPath(string("."));
410 // Creates form if necessary.
412 pFileDlgForm = create_form_FileDlg();
413 // Set callbacks. This means that we don't need a patch file
414 fl_set_object_callback(pFileDlgForm->DirBox,
415 LyXFileDlg::FileDlgCB,0);
416 fl_set_object_callback(pFileDlgForm->PatBox,
417 LyXFileDlg::FileDlgCB,1);
418 fl_set_object_callback(pFileDlgForm->List,
419 LyXFileDlg::FileDlgCB,2);
420 fl_set_object_callback(pFileDlgForm->Filename,
421 LyXFileDlg::FileDlgCB,3);
422 fl_set_object_callback(pFileDlgForm->Rescan,
423 LyXFileDlg::FileDlgCB,10);
424 fl_set_object_callback(pFileDlgForm->Home,
425 LyXFileDlg::FileDlgCB,11);
426 fl_set_object_callback(pFileDlgForm->User1,
427 LyXFileDlg::FileDlgCB,12);
428 fl_set_object_callback(pFileDlgForm->User2,
429 LyXFileDlg::FileDlgCB,13);
431 // Make sure pressing the close box doesn't crash LyX. (RvdK)
432 fl_set_form_atclose(pFileDlgForm->FileDlg, CancelCB, 0);
433 // Register doubleclick callback
434 fl_set_browser_dblclick_callback(pFileDlgForm->List,
437 fl_hide_object(pFileDlgForm->User1);
438 fl_hide_object(pFileDlgForm->User2);
442 LyXFileDlg::~LyXFileDlg()
444 // frees directory entries
446 delete [] pCurrentNames;
451 // SetButton: sets file selector user button action
452 void LyXFileDlg::SetButton(int iIndex, string const & pszName,
453 string const & pszPath)
459 pObject = pFileDlgForm->User1;
460 pTemp = &pszUserPath1;
461 } else if (iIndex == 1) {
462 pObject = pFileDlgForm->User2;
463 pTemp = &pszUserPath2;
466 if (!pszName.empty() && !pszPath.empty()) {
467 fl_set_object_label(pObject, pszName.c_str());
468 fl_show_object(pObject);
471 fl_hide_object(pObject);
477 // GetDirectory: gets last dialog directory
478 string LyXFileDlg::GetDirectory()
480 if (!pszDirectory.empty())
487 // RunDialog: handle dialog during file selection
488 bool LyXFileDlg::RunDialog()
490 force_cancel = false;
496 FL_OBJECT * pObject = fl_do_forms();
498 if (pObject == pFileDlgForm->Ready) {
501 } else if (pObject == pFileDlgForm->Cancel
510 // XForms objects callback (static)
511 void LyXFileDlg::FileDlgCB(FL_OBJECT *, long lArgument)
513 if (!pCurrentDlg) return;
517 case 0: // get directory
518 pCurrentDlg->SetDirectory(fl_get_input(pFileDlgForm->DirBox));
519 pCurrentDlg->Reread();
523 pCurrentDlg->SetMask(fl_get_input(pFileDlgForm->PatBox));
524 pCurrentDlg->Reread();
528 pCurrentDlg->HandleListHit();
532 pCurrentDlg->SetDirectory(fl_get_input(pFileDlgForm->DirBox));
533 pCurrentDlg->SetMask(fl_get_input(pFileDlgForm->PatBox));
534 pCurrentDlg->Reread();
538 pCurrentDlg->SetDirectory(GetEnvPath("HOME"));
539 pCurrentDlg->SetMask(fl_get_input(pFileDlgForm->PatBox));
540 pCurrentDlg->Reread();
543 case 12: // user button 1
544 if (!pCurrentDlg->pszUserPath1.empty()) {
545 pCurrentDlg->SetDirectory(pCurrentDlg->pszUserPath1);
546 pCurrentDlg->SetMask(fl_get_input(pFileDlgForm->PatBox));
547 pCurrentDlg->Reread();
551 case 13: // user button 2
552 if (!pCurrentDlg->pszUserPath2.empty()) {
553 pCurrentDlg->SetDirectory(pCurrentDlg->pszUserPath2);
554 pCurrentDlg->SetMask(fl_get_input(pFileDlgForm->PatBox));
555 pCurrentDlg->Reread();
563 // Handle callback from list
564 void LyXFileDlg::HandleListHit()
567 int iSelect = fl_get_browser(pFileDlgForm->List);
568 if (iSelect > iDepth) {
569 SetInfoLine(pCurrentNames[iSelect - iDepth - 1].pszLsEntry);
571 SetInfoLine(string());
576 // Callback for double click in list
577 void LyXFileDlg::DoubleClickCB(FL_OBJECT *, long)
579 if (pCurrentDlg->HandleDoubleClick())
580 // Simulate click on OK button
581 pCurrentDlg->Force(false);
585 // Handle double click from list
586 bool LyXFileDlg::HandleDoubleClick()
594 iSelect = fl_get_browser(pFileDlgForm->List);
595 if (iSelect > iDepth) {
596 pszTemp = pCurrentNames[iSelect - iDepth - 1].pszName;
597 SetInfoLine(pCurrentNames[iSelect - iDepth - 1].pszLsEntry);
598 if (!suffixIs(pszTemp, '/')) {
600 fl_set_input(pFileDlgForm->Filename, pszTemp.c_str());
602 } else if (iSelect !=0) {
603 SetInfoLine(string());
613 // builds new directory name
614 if (iSelect > iDepth) {
615 // Directory deeper down
616 // First, get directory with trailing /
617 Temp = fl_get_input(pFileDlgForm->DirBox);
618 if (!suffixIs(Temp, '/'))
622 // Directory higher up
624 for (i = 0; i < iSelect; ++i) {
625 string piece = fl_get_browser_line(pFileDlgForm->List, i+1);
626 // The '+2' is here to count the '@b' (JMarc)
627 Temp += piece.substr(i + 2);
640 // Handle OK button call
641 bool LyXFileDlg::HandleOK()
646 pszTemp = fl_get_input(pFileDlgForm->PatBox);
647 if (pszTemp!=pszMask) {
653 // directory was changed
654 pszTemp = fl_get_input(pFileDlgForm->DirBox);
655 if (pszTemp!=pszDirectory) {
656 SetDirectory(pszTemp);
661 // Handle return from list
662 int select = fl_get_browser(pFileDlgForm->List);
663 if (select > iDepth) {
664 string temp = pCurrentNames[select - iDepth - 1].pszName;
665 if (!suffixIs(temp, '/')) {
666 // If user didn't type anything, use browser
667 string name = fl_get_input(pFileDlgForm->Filename);
669 fl_set_input(pFileDlgForm->Filename, temp.c_str());
675 // Emulate a doubleclick
676 return HandleDoubleClick();
680 // Handle Cancel CB from WM close
681 int LyXFileDlg::CancelCB(FL_FORM *, void *)
683 // Simulate a click on the cancel button
684 pCurrentDlg->Force(true);
689 // Simulates a click on OK/Cancel
690 void LyXFileDlg::Force(bool cancel)
694 fl_set_button(pFileDlgForm->Cancel, 1);
697 fl_set_button(pFileDlgForm->Ready, 1);
699 // Start timer to break fl_do_forms loop soon
700 fl_set_timer(pFileDlgForm->timer, 0.1);
704 // Select: launches dialog and returns selected file
705 string LyXFileDlg::Select(string const & title, string const & path,
706 string const & mask, string const & suggested)
710 // handles new mask and path
722 fl_select_browser_line(pFileDlgForm->List, 1);
723 fl_set_browser_topline(pFileDlgForm->List, 1);
726 // checks whether dialog can be started
727 if (pCurrentDlg) return string();
731 SetInfoLine (string());
732 fl_set_input(pFileDlgForm->Filename, suggested.c_str());
733 fl_set_button(pFileDlgForm->Cancel, 0);
734 fl_set_button(pFileDlgForm->Ready, 0);
735 fl_set_focus_object(pFileDlgForm->FileDlg, pFileDlgForm->Filename);
736 fl_deactivate_all_forms();
737 fl_show_form(pFileDlgForm->FileDlg, FL_PLACE_MOUSE | FL_FREE_SIZE,
738 FL_FULLBORDER, title.c_str());
742 fl_hide_form(pFileDlgForm->FileDlg);
743 fl_activate_all_forms();
746 // Returns filename or string() if no valid selection was made
747 if (!isOk || !fl_get_input(pFileDlgForm->Filename)[0]) return string();
749 pszFileName = fl_get_input(pFileDlgForm->Filename);
751 if (!AbsolutePath(pszFileName)) {
752 pszFileName = AddName(fl_get_input(pFileDlgForm->DirBox),