2 * \file FormFiledialog.C
3 * Copyright 2001 the LyX Team
4 * Read the file COPYING
7 * \author John Levon, moz@compsoc.man.ac.uk
24 #include "frontends/Alert.h"
25 #include "support/FileInfo.h"
26 #include "support/lyxlib.h"
27 #include "support/lstrings.h"
29 #include "frontends/Dialogs.h"
30 #include "forms_gettext.h"
32 #include <boost/bind.hpp>
41 # define dirent direct
43 # include <sys/ndir.h>
54 #pragma implementation
57 #include "support/filetools.h"
58 #include "FormFiledialog.h"
59 #include "forms/form_filedialog.h"
60 #include FORMS_H_LOCATION
64 // six months, in seconds
65 long const SIX_MONTH_SEC = 6L * 30L * 24L * 60L * 60L;
67 long const ONE_HOUR_SEC = 60L * 60L;
72 int C_LyXFileDlg_CancelCB(FL_FORM *fl, void *xev)
74 return FileDialog::Private::CancelCB(fl, xev);
78 void C_LyXFileDlg_DoubleClickCB(FL_OBJECT * ob, long data)
80 FileDialog::Private::DoubleClickCB(ob, data);
84 void C_LyXFileDlg_FileDlgCB(FL_OBJECT * ob, long data)
86 FileDialog::Private::FileDlgCB(ob, data);
91 // *** User cache class implementation
92 /// User cache class definition
95 /// seeks user name from group ID
96 string const & find(uid_t ID) const {
97 Users::const_iterator cit = users.find(ID);
98 if (cit == users.end()) {
106 void add(uid_t ID) const;
108 typedef map<uid_t, string> Users;
114 void UserCache::add(uid_t ID) const
116 struct passwd const * entry = getpwuid(ID);
117 users[ID] = entry ? entry->pw_name : tostr(ID);
121 /// Group cache class definition
124 /// seeks group name from group ID
125 string const & find(gid_t ID) const ;
128 void add(gid_t ID) const;
130 typedef map<gid_t, string> Groups;
132 mutable Groups groups;
136 string const & GroupCache::find(gid_t ID) const
138 Groups::const_iterator cit = groups.find(ID);
139 if (cit == groups.end()) {
147 void GroupCache::add(gid_t ID) const
149 struct group const * entry = getgrgid(ID);
150 groups[ID] = entry ? entry->gr_name : tostr(ID);
154 UserCache lyxUserCache;
155 GroupCache lyxGroupCache;
157 // compares two LyXDirEntry objects content (used for sort)
158 class comp_direntry {
160 bool operator()(DirEntry const & r1, DirEntry const & r2) const
162 bool const r1d = suffixIs(r1.name_, '/');
163 bool const r2d = suffixIs(r2.name_, '/');
168 return r1.name_ < r2.name_;
177 // *** FileDialog::Private class implementation
180 FD_filedialog * FileDialog::Private::file_dlg_form_ = 0;
181 FileDialog::Private * FileDialog::Private::current_dlg_ = 0;
184 // Reread: updates dialog list to match class directory
185 void FileDialog::Private::Reread()
188 DIR * dir = ::opendir(directory_.c_str());
190 Alert::err_alert(_("Warning! Couldn't open directory."),
192 directory_ = lyx::getcwd();
193 dir = ::opendir(directory_.c_str());
196 // Clear the present namelist
197 dir_entries_.clear();
200 fl_hide_object(file_dlg_form_->List);
201 fl_clear_browser(file_dlg_form_->List);
202 fl_set_input(file_dlg_form_->DirBox, directory_.c_str());
204 // Splits complete directory name into directories and compute depth
208 string File = directory_;
210 File = split(File, Temp, '/');
212 while (!File.empty() || !Temp.empty()) {
213 string dline = "@b"+line + Temp + '/';
214 fl_add_browser_line(file_dlg_form_->List, dline.c_str());
215 File = split(File, Temp, '/');
220 // Parses all entries of the given subdirectory
221 time_t curTime = time(0);
223 while (dirent * pDirEntry = readdir(dir)) {
224 bool isLink = false, isDir = false;
226 // If the pattern doesn't start with a dot, skip hidden files
227 if (!mask_.empty() && mask_[0] != '.' &&
228 pDirEntry->d_name[0] == '.')
232 string fname = pDirEntry->d_name;
234 // Under all circumstances, "." and ".." are not wanted
235 if (fname == "." || fname == "..")
239 File = AddName(directory_, fname);
241 FileInfo fileInfo(File, true);
243 // can this really happen?
244 if (!fileInfo.isOK())
247 fileInfo.modeString(szMode);
248 unsigned int nlink = fileInfo.getNumberOfLinks();
249 string user = lyxUserCache.find(fileInfo.getUid());
250 string group = lyxGroupCache.find(fileInfo.getGid());
252 time_t modtime = fileInfo.getModificationTime();
253 string Time = ctime(&modtime);
255 if (curTime > modtime + SIX_MONTH_SEC
256 || curTime < modtime + ONE_HOUR_SEC) {
257 // The file is fairly old or in the future. POSIX says
258 // the cutoff is 6 months old. Allow a 1 hour slop
259 // factor for what is considered "the future", to
260 // allow for NFS server/client clock disagreement.
261 // Show the year instead of the time of day.
263 Time.erase(15, string::npos);
265 Time.erase(16, string::npos);
268 string Buffer = string(szMode) + ' ' +
272 Time.substr(4, string::npos) + ' ';
274 Buffer += pDirEntry->d_name;
275 Buffer += fileInfo.typeIndicator();
277 if ((isLink = fileInfo.isLink())) {
280 if (LyXReadLink(File, Link)) {
284 // This gives the FileType of the file that
285 // is really pointed too after resolving all
286 // symlinks. This is not necessarily the same
287 // as the type of Link (which could again be a
288 // link). Is that intended?
290 fileInfo.newFile(File);
292 Buffer += fileInfo.typeIndicator();
298 // filters files according to pattern and type
299 if (fileInfo.isRegular()
301 || fileInfo.isBlock()
302 || fileInfo.isFifo()) {
303 if (!regexMatch(fname, mask_))
305 } else if (!(isDir = fileInfo.isDir()))
310 // Note ls_entry_ is an string!
311 tmp.ls_entry_ = Buffer;
314 if (isDir) temp += '/';
317 // creates displayed name
318 temp = pDirEntry->d_name;
322 temp += fileInfo.typeIndicator();
323 tmp.displayed_ = temp;
325 dir_entries_.push_back(tmp);
331 sort(dir_entries_.begin(), dir_entries_.end(), comp_direntry());
333 // Add them to directory box
334 for (DirEntries::const_iterator cit = dir_entries_.begin();
335 cit != dir_entries_.end(); ++cit) {
336 string const temp = line + cit->displayed_;
337 fl_add_browser_line(file_dlg_form_->List, temp.c_str());
339 fl_set_browser_topline(file_dlg_form_->List, depth_);
340 fl_show_object(file_dlg_form_->List);
345 // SetDirectory: sets dialog current directory
346 void FileDialog::Private::SetDirectory(string const & path)
352 tmp = MakeAbsPath(ExpandPath(path), directory_);
354 // must check the directory exists
355 DIR * dir = ::opendir(tmp.c_str());
357 Alert::err_alert(_("Warning! Couldn't open directory."), tmp);
365 // SetMask: sets dialog file mask
366 void FileDialog::Private::SetMask(string const & newmask)
369 fl_set_input(file_dlg_form_->PatBox, mask_.c_str());
373 // SetInfoLine: sets dialog information line
374 void FileDialog::Private::SetInfoLine(string const & line)
377 fl_set_object_label(file_dlg_form_->FileInfo, info_line_.c_str());
381 FileDialog::Private::Private(Dialogs & dia)
383 directory_ = MakeAbsPath(string("."));
386 // Creates form if necessary.
387 if (!file_dlg_form_) {
388 file_dlg_form_ = build_filedialog(this);
389 // Set callbacks. This means that we don't need a patch file
390 fl_set_object_callback(file_dlg_form_->DirBox,
391 C_LyXFileDlg_FileDlgCB, 0);
392 fl_set_object_callback(file_dlg_form_->PatBox,
393 C_LyXFileDlg_FileDlgCB, 1);
394 fl_set_object_callback(file_dlg_form_->List,
395 C_LyXFileDlg_FileDlgCB, 2);
396 fl_set_object_callback(file_dlg_form_->Filename,
397 C_LyXFileDlg_FileDlgCB, 3);
398 fl_set_object_callback(file_dlg_form_->Rescan,
399 C_LyXFileDlg_FileDlgCB, 10);
400 fl_set_object_callback(file_dlg_form_->Home,
401 C_LyXFileDlg_FileDlgCB, 11);
402 fl_set_object_callback(file_dlg_form_->User1,
403 C_LyXFileDlg_FileDlgCB, 12);
404 fl_set_object_callback(file_dlg_form_->User2,
405 C_LyXFileDlg_FileDlgCB, 13);
407 // Make sure pressing the close box doesn't crash LyX. (RvdK)
408 fl_set_form_atclose(file_dlg_form_->form,
409 C_LyXFileDlg_CancelCB, 0);
410 // Register doubleclick callback
411 fl_set_browser_dblclick_callback(file_dlg_form_->List,
412 C_LyXFileDlg_DoubleClickCB,
415 fl_hide_object(file_dlg_form_->User1);
416 fl_hide_object(file_dlg_form_->User2);
418 r_ = dia.redrawGUI.connect(boost::bind(&FileDialog::Private::redraw, this));
422 FileDialog::Private::~Private()
428 void FileDialog::Private::redraw()
430 if (file_dlg_form_->form && file_dlg_form_->form->visible)
431 fl_redraw_form(file_dlg_form_->form);
435 // SetButton: sets file selector user button action
436 void FileDialog::Private::SetButton(int iIndex, string const & name_,
437 string const & pszPath)
443 ob = file_dlg_form_->User1;
444 pTemp = &user_path1_;
445 } else if (iIndex == 1) {
446 ob = file_dlg_form_->User2;
447 pTemp = &user_path2_;
450 if (!name_.empty()) {
451 fl_set_object_label(ob, idex(name_.c_str()));
452 fl_set_button_shortcut(ob, scex(name_.c_str()), 1);
462 // GetDirectory: gets last dialog directory
463 string const FileDialog::Private::GetDirectory() const
465 if (!directory_.empty())
472 bool x_sync_kludge(bool ret)
474 XSync(fl_get_display(), false);
479 // RunDialog: handle dialog during file selection
480 bool FileDialog::Private::RunDialog()
482 force_cancel_ = false;
487 FL_OBJECT * ob = fl_do_forms();
489 if (ob == file_dlg_form_->Ready) {
491 return x_sync_kludge(true);
493 } else if (ob == file_dlg_form_->Cancel
495 return x_sync_kludge(false);
498 return x_sync_kludge(true);
503 // XForms objects callback (static)
504 void FileDialog::Private::FileDlgCB(FL_OBJECT *, long arg)
511 case 0: // get directory
512 current_dlg_->SetDirectory(fl_get_input(file_dlg_form_->DirBox));
513 current_dlg_->Reread();
517 current_dlg_->SetMask(fl_get_input(file_dlg_form_->PatBox));
518 current_dlg_->Reread();
522 current_dlg_->HandleListHit();
526 current_dlg_->SetDirectory(fl_get_input(file_dlg_form_->DirBox));
527 current_dlg_->SetMask(fl_get_input(file_dlg_form_->PatBox));
528 current_dlg_->Reread();
532 current_dlg_->SetDirectory(GetEnvPath("HOME"));
533 current_dlg_->SetMask(fl_get_input(file_dlg_form_->PatBox));
534 current_dlg_->Reread();
537 case 12: // user button 1
538 current_dlg_->SetDirectory(current_dlg_->user_path1_);
539 current_dlg_->SetMask(fl_get_input(file_dlg_form_->PatBox));
540 current_dlg_->Reread();
543 case 13: // user button 2
544 current_dlg_->SetDirectory(current_dlg_->user_path2_);
545 current_dlg_->SetMask(fl_get_input(file_dlg_form_->PatBox));
546 current_dlg_->Reread();
553 // Handle callback from list
554 void FileDialog::Private::HandleListHit()
557 int const select_ = fl_get_browser(file_dlg_form_->List);
558 if (select_ > depth_)
559 SetInfoLine(dir_entries_[select_ - depth_ - 1].ls_entry_);
561 SetInfoLine(string());
565 // Callback for double click in list
566 void FileDialog::Private::DoubleClickCB(FL_OBJECT *, long)
568 // Simulate click on OK button
569 if (current_dlg_->HandleDoubleClick())
570 current_dlg_->Force(false);
574 // Handle double click from list
575 bool FileDialog::Private::HandleDoubleClick()
581 int const select_ = fl_get_browser(file_dlg_form_->List);
582 if (select_ > depth_) {
583 tmp = dir_entries_[select_ - depth_ - 1].name_;
584 SetInfoLine(dir_entries_[select_ - depth_ - 1].ls_entry_);
585 if (!suffixIs(tmp, '/')) {
587 fl_set_input(file_dlg_form_->Filename, tmp.c_str());
589 } else if (select_ != 0) {
590 SetInfoLine(string());
598 // builds new directory name
599 if (select_ > depth_) {
600 // Directory deeper down
601 // First, get directory with trailing /
602 Temp = fl_get_input(file_dlg_form_->DirBox);
603 if (!suffixIs(Temp, '/'))
607 // Directory higher up
609 for (int i = 0; i < select_; ++i) {
610 string piece = fl_get_browser_line(file_dlg_form_->List, i+1);
611 // The '+2' is here to count the '@b' (JMarc)
612 Temp += piece.substr(i + 2);
625 // Handle OK button call
626 bool FileDialog::Private::HandleOK()
629 string tmp = fl_get_input(file_dlg_form_->PatBox);
636 // directory was changed
637 tmp = fl_get_input(file_dlg_form_->DirBox);
638 if (tmp != directory_) {
644 // Handle return from list
645 int const select = fl_get_browser(file_dlg_form_->List);
646 if (select > depth_) {
647 string const temp = dir_entries_[select - depth_ - 1].name_;
648 if (!suffixIs(temp, '/')) {
649 // If user didn't type anything, use browser
650 string const name = fl_get_input(file_dlg_form_->Filename);
652 fl_set_input(file_dlg_form_->Filename, temp.c_str());
657 // Emulate a doubleclick
658 return HandleDoubleClick();
662 // Handle Cancel CB from WM close
663 int FileDialog::Private::CancelCB(FL_FORM *, void *)
665 // Simulate a click on the cancel button
666 current_dlg_->Force(true);
671 // Simulates a click on OK/Cancel
672 void FileDialog::Private::Force(bool cancel)
675 force_cancel_ = true;
676 fl_set_button(file_dlg_form_->Cancel, 1);
679 fl_set_button(file_dlg_form_->Ready, 1);
681 // Start timer to break fl_do_forms loop soon
682 fl_set_timer(file_dlg_form_->timer, 0.1);
686 // Select: launches dialog and returns selected file
687 string const FileDialog::Private::Select(string const & title,
690 string const & suggested)
692 // handles new mask and path
705 // highlight the suggested file in the browser, if it exists.
707 string const filename = OnlyFilename(suggested);
708 if (!filename.empty()) {
709 for (int i = 0; i < fl_get_browser_maxline(file_dlg_form_->List); ++i) {
710 string s = fl_get_browser_line(file_dlg_form_->List, i + 1);
711 s = strip(frontStrip(s));
719 if (sel != 0) fl_select_browser_line(file_dlg_form_->List, sel);
720 int const top = max(sel - 5, 1);
721 fl_set_browser_topline(file_dlg_form_->List, top);
723 // checks whether dialog can be started
729 SetInfoLine(string());
730 fl_set_input(file_dlg_form_->Filename, suggested.c_str());
731 fl_set_button(file_dlg_form_->Cancel, 0);
732 fl_set_button(file_dlg_form_->Ready, 0);
733 fl_set_focus_object(file_dlg_form_->form, file_dlg_form_->Filename);
734 fl_deactivate_all_forms();
735 fl_show_form(file_dlg_form_->form,
736 FL_PLACE_MOUSE | FL_FREE_SIZE, 0,
741 fl_hide_form(file_dlg_form_->form);
742 fl_activate_all_forms();
745 // Returns filename or string() if no valid selection was made
746 if (!isOk || !fl_get_input(file_dlg_form_->Filename)[0])
749 file_name_ = fl_get_input(file_dlg_form_->Filename);
751 if (!AbsolutePath(file_name_))
752 file_name_ = AddName(fl_get_input(file_dlg_form_->DirBox), file_name_);