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.
302 Time.erase(15, string::npos);
304 Time.erase(16, string::npos);
307 Buffer = string(szMode) + ' ' +
311 Time.substr(4, string::npos) + ' ';
313 Buffer += pDirEntry->d_name;
314 Buffer += fileInfo.typeIndicator();
316 if ((isLink = fileInfo.isLink())) {
319 if (LyXReadLink(File,Link)) {
323 // This gives the FileType of the file that
324 // is really pointed too after resolving all
325 // symlinks. This is not necessarily the same
326 // as the type of Link (which could again be a
327 // link). Is that intended?
329 fileInfo.newFile(File);
330 Buffer += fileInfo.typeIndicator();
334 // filters files according to pattern and type
335 if (fileInfo.isRegular()
337 || fileInfo.isBlock()
338 || fileInfo.isFifo()) {
339 if (!regexMatch(fname, pszMask))
341 } else if (!(isDir = fileInfo.isDir()))
344 // Note pszLsEntry is an string!
345 pCurrentNames[iNumNames].pszLsEntry = Buffer;
349 if (isDir) temp += '/';
350 pCurrentNames[iNumNames].pszName = temp;
352 // creates displayed name
353 temp = pDirEntry->d_name;
357 temp += fileInfo.typeIndicator();
359 pCurrentNames[iNumNames++].pszDisplayed = temp;
362 closedir(pDirectory);
365 qsort(pCurrentNames, iNumNames, sizeof(LyXDirEntry),
366 (int (*)(const void *, const void *))LyXDirEntry::ldeCompProc);
368 // Add them to directory box
369 for (i = 0; i < iNumNames; ++i) {
370 string temp = line + pCurrentNames[i].pszDisplayed;
371 fl_add_browser_line(pFileDlgForm->List, temp.c_str());
373 fl_set_browser_topline(pFileDlgForm->List,iDepth);
374 fl_show_object(pFileDlgForm->List);
379 // SetDirectory: sets dialog current directory
380 void LyXFileDlg::SetDirectory(string const & Path)
382 if (!pszDirectory.empty()) {
383 string TempPath = ExpandPath(Path); // Expand ~/
384 TempPath = MakeAbsPath(TempPath, pszDirectory);
385 pszDirectory = MakeAbsPath(TempPath);
386 } else pszDirectory = MakeAbsPath(Path);
390 // SetMask: sets dialog file mask
391 void LyXFileDlg::SetMask(string const & NewMask)
394 fl_set_input(pFileDlgForm->PatBox, pszMask.c_str());
398 // SetInfoLine: sets dialog information line
399 void LyXFileDlg::SetInfoLine(string const & Line)
402 fl_set_object_label(pFileDlgForm->FileInfo, pszInfoLine.c_str());
406 LyXFileDlg::LyXFileDlg()
409 pszDirectory = MakeAbsPath(string("."));
412 // Creates form if necessary.
414 pFileDlgForm = create_form_FileDlg();
415 // Set callbacks. This means that we don't need a patch file
416 fl_set_object_callback(pFileDlgForm->DirBox,
417 LyXFileDlg::FileDlgCB,0);
418 fl_set_object_callback(pFileDlgForm->PatBox,
419 LyXFileDlg::FileDlgCB,1);
420 fl_set_object_callback(pFileDlgForm->List,
421 LyXFileDlg::FileDlgCB,2);
422 fl_set_object_callback(pFileDlgForm->Filename,
423 LyXFileDlg::FileDlgCB,3);
424 fl_set_object_callback(pFileDlgForm->Rescan,
425 LyXFileDlg::FileDlgCB,10);
426 fl_set_object_callback(pFileDlgForm->Home,
427 LyXFileDlg::FileDlgCB,11);
428 fl_set_object_callback(pFileDlgForm->User1,
429 LyXFileDlg::FileDlgCB,12);
430 fl_set_object_callback(pFileDlgForm->User2,
431 LyXFileDlg::FileDlgCB,13);
433 // Make sure pressing the close box doesn't crash LyX. (RvdK)
434 fl_set_form_atclose(pFileDlgForm->FileDlg, CancelCB, 0);
435 // Register doubleclick callback
436 fl_set_browser_dblclick_callback(pFileDlgForm->List,
439 fl_hide_object(pFileDlgForm->User1);
440 fl_hide_object(pFileDlgForm->User2);
444 LyXFileDlg::~LyXFileDlg()
446 // frees directory entries
448 delete [] pCurrentNames;
453 // SetButton: sets file selector user button action
454 void LyXFileDlg::SetButton(int iIndex, string const & pszName,
455 string const & pszPath)
461 pObject = pFileDlgForm->User1;
462 pTemp = &pszUserPath1;
463 } else if (iIndex == 1) {
464 pObject = pFileDlgForm->User2;
465 pTemp = &pszUserPath2;
468 if (!pszName.empty() && !pszPath.empty()) {
469 fl_set_object_label(pObject, pszName.c_str());
470 fl_show_object(pObject);
473 fl_hide_object(pObject);
479 // GetDirectory: gets last dialog directory
480 string LyXFileDlg::GetDirectory()
482 if (!pszDirectory.empty())
489 // RunDialog: handle dialog during file selection
490 bool LyXFileDlg::RunDialog()
492 force_cancel = false;
498 FL_OBJECT * pObject = fl_do_forms();
500 if (pObject == pFileDlgForm->Ready) {
503 } else if (pObject == pFileDlgForm->Cancel
512 // XForms objects callback (static)
513 void LyXFileDlg::FileDlgCB(FL_OBJECT *, long lArgument)
515 if (!pCurrentDlg) return;
519 case 0: // get directory
520 pCurrentDlg->SetDirectory(fl_get_input(pFileDlgForm->DirBox));
521 pCurrentDlg->Reread();
525 pCurrentDlg->SetMask(fl_get_input(pFileDlgForm->PatBox));
526 pCurrentDlg->Reread();
530 pCurrentDlg->HandleListHit();
534 pCurrentDlg->SetDirectory(fl_get_input(pFileDlgForm->DirBox));
535 pCurrentDlg->SetMask(fl_get_input(pFileDlgForm->PatBox));
536 pCurrentDlg->Reread();
540 pCurrentDlg->SetDirectory(GetEnvPath("HOME"));
541 pCurrentDlg->SetMask(fl_get_input(pFileDlgForm->PatBox));
542 pCurrentDlg->Reread();
545 case 12: // user button 1
546 if (!pCurrentDlg->pszUserPath1.empty()) {
547 pCurrentDlg->SetDirectory(pCurrentDlg->pszUserPath1);
548 pCurrentDlg->SetMask(fl_get_input(pFileDlgForm->PatBox));
549 pCurrentDlg->Reread();
553 case 13: // user button 2
554 if (!pCurrentDlg->pszUserPath2.empty()) {
555 pCurrentDlg->SetDirectory(pCurrentDlg->pszUserPath2);
556 pCurrentDlg->SetMask(fl_get_input(pFileDlgForm->PatBox));
557 pCurrentDlg->Reread();
565 // Handle callback from list
566 void LyXFileDlg::HandleListHit()
569 int iSelect = fl_get_browser(pFileDlgForm->List);
570 if (iSelect > iDepth) {
571 SetInfoLine(pCurrentNames[iSelect - iDepth - 1].pszLsEntry);
573 SetInfoLine(string());
578 // Callback for double click in list
579 void LyXFileDlg::DoubleClickCB(FL_OBJECT *, long)
581 if (pCurrentDlg->HandleDoubleClick())
582 // Simulate click on OK button
583 pCurrentDlg->Force(false);
587 // Handle double click from list
588 bool LyXFileDlg::HandleDoubleClick()
596 iSelect = fl_get_browser(pFileDlgForm->List);
597 if (iSelect > iDepth) {
598 pszTemp = pCurrentNames[iSelect - iDepth - 1].pszName;
599 SetInfoLine(pCurrentNames[iSelect - iDepth - 1].pszLsEntry);
600 if (!suffixIs(pszTemp, '/')) {
602 fl_set_input(pFileDlgForm->Filename, pszTemp.c_str());
604 } else if (iSelect !=0) {
605 SetInfoLine(string());
615 // builds new directory name
616 if (iSelect > iDepth) {
617 // Directory deeper down
618 // First, get directory with trailing /
619 Temp = fl_get_input(pFileDlgForm->DirBox);
620 if (!suffixIs(Temp, '/'))
624 // Directory higher up
626 for (i = 0; i < iSelect; ++i) {
627 string piece = fl_get_browser_line(pFileDlgForm->List, i+1);
628 // The '+2' is here to count the '@b' (JMarc)
629 Temp += piece.substr(i + 2);
642 // Handle OK button call
643 bool LyXFileDlg::HandleOK()
648 pszTemp = fl_get_input(pFileDlgForm->PatBox);
649 if (pszTemp!=pszMask) {
655 // directory was changed
656 pszTemp = fl_get_input(pFileDlgForm->DirBox);
657 if (pszTemp!=pszDirectory) {
658 SetDirectory(pszTemp);
663 // Handle return from list
664 int select = fl_get_browser(pFileDlgForm->List);
665 if (select > iDepth) {
666 string temp = pCurrentNames[select - iDepth - 1].pszName;
667 if (!suffixIs(temp, '/')) {
668 // If user didn't type anything, use browser
669 string name = fl_get_input(pFileDlgForm->Filename);
671 fl_set_input(pFileDlgForm->Filename, temp.c_str());
677 // Emulate a doubleclick
678 return HandleDoubleClick();
682 // Handle Cancel CB from WM close
683 int LyXFileDlg::CancelCB(FL_FORM *, void *)
685 // Simulate a click on the cancel button
686 pCurrentDlg->Force(true);
691 // Simulates a click on OK/Cancel
692 void LyXFileDlg::Force(bool cancel)
696 fl_set_button(pFileDlgForm->Cancel, 1);
699 fl_set_button(pFileDlgForm->Ready, 1);
701 // Start timer to break fl_do_forms loop soon
702 fl_set_timer(pFileDlgForm->timer, 0.1);
706 // Select: launches dialog and returns selected file
707 string LyXFileDlg::Select(string const & title, string const & path,
708 string const & mask, string const & suggested)
712 // handles new mask and path
724 fl_select_browser_line(pFileDlgForm->List, 1);
725 fl_set_browser_topline(pFileDlgForm->List, 1);
728 // checks whether dialog can be started
729 if (pCurrentDlg) return string();
733 SetInfoLine (string());
734 fl_set_input(pFileDlgForm->Filename, suggested.c_str());
735 fl_set_button(pFileDlgForm->Cancel, 0);
736 fl_set_button(pFileDlgForm->Ready, 0);
737 fl_set_focus_object(pFileDlgForm->FileDlg, pFileDlgForm->Filename);
738 fl_deactivate_all_forms();
739 fl_show_form(pFileDlgForm->FileDlg, FL_PLACE_MOUSE | FL_FREE_SIZE,
740 FL_FULLBORDER, title.c_str());
744 fl_hide_form(pFileDlgForm->FileDlg);
745 fl_activate_all_forms();
748 // Returns filename or string() if no valid selection was made
749 if (!isOk || !fl_get_input(pFileDlgForm->Filename)[0]) return string();
751 pszFileName = fl_get_input(pFileDlgForm->Filename);
753 if (!AbsolutePath(pszFileName)) {
754 pszFileName = AddName(fl_get_input(pFileDlgForm->DirBox),