2 * \file FormFiledialog.C
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
9 * Full author contact details are available in file CREDITS.
14 #include "FormFiledialog.h"
15 #include "forms/form_filedialog.h"
17 #include "forms_gettext.h"
18 #include "xforms_helpers.h"
20 #include "frontends/Dialogs.h"
22 #include "support/FileInfo.h"
23 #include "support/filetools.h"
24 #include "support/globbing.h"
25 #include "support/lstrings.h"
26 #include "support/lyxlib.h"
27 #include "support/tostr.h"
29 #include "lyx_forms.h"
31 #include <boost/bind.hpp>
32 #include <boost/regex.hpp>
46 # define dirent direct
48 # include <sys/ndir.h>
58 using lyx::support::AbsolutePath;
59 using lyx::support::AddName;
60 using lyx::support::ExpandPath;
61 using lyx::support::FileFilterList;
62 using lyx::support::FileInfo;
63 using lyx::support::getcwd;
64 using lyx::support::GetEnvPath;
65 using lyx::support::LyXReadLink;
66 using lyx::support::MakeAbsPath;
67 using lyx::support::OnlyFilename;
68 using lyx::support::split;
69 using lyx::support::subst;
70 using lyx::support::suffixIs;
71 using lyx::support::trim;
82 // six months, in seconds
83 long const SIX_MONTH_SEC = 6L * 30L * 24L * 60L * 60L;
85 long const ONE_HOUR_SEC = 60L * 60L;
90 int C_LyXFileDlg_CancelCB(FL_FORM *fl, void *xev)
92 return FileDialog::Private::CancelCB(fl, xev);
96 void C_LyXFileDlg_DoubleClickCB(FL_OBJECT * ob, long data)
98 FileDialog::Private::DoubleClickCB(ob, data);
102 void C_LyXFileDlg_FileDlgCB(FL_OBJECT * ob, long data)
104 FileDialog::Private::FileDlgCB(ob, data);
109 // *** User cache class implementation
110 /// User cache class definition
113 /// seeks user name from group ID
114 string const & find(uid_t ID) const {
115 Users::const_iterator cit = users.find(ID);
116 if (cit == users.end()) {
124 void add(uid_t ID) const;
126 typedef map<uid_t, string> Users;
132 void UserCache::add(uid_t ID) const
134 struct passwd const * entry = getpwuid(ID);
135 users[ID] = entry ? entry->pw_name : tostr(int(ID));
139 /// Group cache class definition
142 /// seeks group name from group ID
143 string const & find(gid_t ID) const ;
146 void add(gid_t ID) const;
148 typedef map<gid_t, string> Groups;
150 mutable Groups groups;
154 string const & GroupCache::find(gid_t ID) const
156 Groups::const_iterator cit = groups.find(ID);
157 if (cit == groups.end()) {
165 void GroupCache::add(gid_t ID) const
167 struct group const * entry = getgrgid(ID);
168 groups[ID] = entry ? entry->gr_name : tostr(int(ID));
172 UserCache lyxUserCache;
173 GroupCache lyxGroupCache;
175 // compares two LyXDirEntry objects content (used for sort)
176 class comp_direntry : public std::binary_function<DirEntry, DirEntry, bool> {
178 bool operator()(DirEntry const & r1, DirEntry const & r2) const
180 bool const r1d = suffixIs(r1.name_, '/');
181 bool const r2d = suffixIs(r2.name_, '/');
186 return r1.name_ < r2.name_;
195 // *** FileDialog::Private class implementation
198 FD_filedialog * FileDialog::Private::file_dlg_form_ = 0;
199 FileDialog::Private * FileDialog::Private::current_dlg_ = 0;
200 int FileDialog::Private::minw_ = 0;
201 int FileDialog::Private::minh_ = 0;
204 // Reread: updates dialog list to match class directory
205 void FileDialog::Private::Reread()
208 DIR * dir = ::opendir(directory_.c_str());
212 Alert::err_alert(_("Warning! Couldn't open directory."),
215 directory_ = getcwd();
216 dir = ::opendir(directory_.c_str());
219 // Clear the present namelist
220 dir_entries_.clear();
223 fl_hide_object(file_dlg_form_->List);
224 fl_clear_browser(file_dlg_form_->List);
225 fl_set_input(file_dlg_form_->DirBox, directory_.c_str());
227 // Splits complete directory name into directories and compute depth
231 string File = directory_;
233 File = split(File, Temp, '/');
235 while (!File.empty() || !Temp.empty()) {
236 string dline = "@b" + line + Temp + '/';
237 fl_add_browser_line(file_dlg_form_->List, dline.c_str());
238 File = split(File, Temp, '/');
243 vector<string> const glob_matches =
244 lyx::support::expand_globs(mask_, directory_);
246 time_t curTime = time(0);
248 while (dirent * entry = readdir(dir)) {
249 bool isLink = false, isDir = false;
251 // If the pattern doesn't start with a dot, skip hidden files
252 if (!mask_.empty() && mask_[0] != '.' &&
253 entry->d_name[0] == '.')
257 string fname = entry->d_name;
259 // Under all circumstances, "." and ".." are not wanted
260 if (fname == "." || fname == "..")
264 File = AddName(directory_, fname);
266 FileInfo fileInfo(File, true);
268 // can this really happen?
269 if (!fileInfo.isOK())
272 mode = fileInfo.modeString();
273 unsigned int const nlink = fileInfo.getNumberOfLinks();
274 string const user = lyxUserCache.find(fileInfo.getUid());
275 string const group = lyxGroupCache.find(fileInfo.getGid());
277 time_t modtime = fileInfo.getModificationTime();
278 string Time = ctime(&modtime);
280 if (curTime > modtime + SIX_MONTH_SEC
281 || curTime < modtime + ONE_HOUR_SEC) {
282 // The file is fairly old or in the future. POSIX says
283 // the cutoff is 6 months old. Allow a 1 hour slop
284 // factor for what is considered "the future", to
285 // allow for NFS server/client clock disagreement.
286 // Show the year instead of the time of day.
288 Time.erase(15, string::npos);
290 Time.erase(16, string::npos);
293 string buffer = mode + ' ' +
297 Time.substr(4, string::npos) + ' ';
299 buffer += entry->d_name;
300 buffer += fileInfo.typeIndicator();
302 isLink = fileInfo.isLink();
306 if (LyXReadLink(File, Link)) {
310 // This gives the FileType of the file that
311 // is really pointed to after resolving all
312 // symlinks. This is not necessarily the same
313 // as the type of Link (which could again be a
314 // link). Is that intended?
316 fileInfo.newFile(File);
318 buffer += fileInfo.typeIndicator();
324 // filters files according to pattern and type
325 if (fileInfo.isRegular()
327 || fileInfo.isBlock()
328 || fileInfo.isFifo()) {
329 typedef vector<string>::const_iterator viterator;
330 viterator gbegin = glob_matches.begin();
331 viterator const gend = glob_matches.end();
332 if (std::find(gbegin, gend, fname) == gend)
334 } else if (!(isDir = fileInfo.isDir()))
339 // Note ls_entry_ is an string!
340 tmp.ls_entry_ = buffer;
347 // creates displayed name
348 temp = entry->d_name;
352 temp += fileInfo.typeIndicator();
353 tmp.displayed_ = temp;
355 dir_entries_.push_back(tmp);
361 sort(dir_entries_.begin(), dir_entries_.end(), comp_direntry());
363 // Add them to directory box
364 for (DirEntries::const_iterator cit = dir_entries_.begin();
365 cit != dir_entries_.end(); ++cit) {
366 string const temp = line + cit->displayed_;
367 fl_add_browser_line(file_dlg_form_->List, temp.c_str());
369 fl_set_browser_topline(file_dlg_form_->List, depth_);
370 fl_show_object(file_dlg_form_->List);
375 // SetDirectory: sets dialog current directory
376 void FileDialog::Private::SetDirectory(string const & path)
382 tmp = MakeAbsPath(ExpandPath(path), directory_);
384 // must check the directory exists
385 DIR * dir = ::opendir(tmp.c_str());
389 Alert::err_alert(_("Warning! Couldn't open directory."), tmp);
398 void FileDialog::Private::SetFilters(string const & mask)
400 SetFilters(FileFilterList(mask));
404 void FileDialog::Private::SetFilters(FileFilterList const & filters)
406 // Just take the first one for now.
407 mask_ = filters.filters()[0].globs();
408 fl_set_input(file_dlg_form_->PatBox, mask_.c_str());
412 // SetInfoLine: sets dialog information line
413 void FileDialog::Private::SetInfoLine(string const & line)
416 fl_set_object_label(file_dlg_form_->FileInfo, info_line_.c_str());
420 FileDialog::Private::Private()
422 directory_ = MakeAbsPath(string("."));
424 // Creates form if necessary.
425 if (!file_dlg_form_) {
426 file_dlg_form_ = build_filedialog(this);
427 minw_ = file_dlg_form_->form->w;
428 minh_ = file_dlg_form_->form->h;
429 // Set callbacks. This means that we don't need a patch file
430 fl_set_object_callback(file_dlg_form_->DirBox,
431 C_LyXFileDlg_FileDlgCB, 0);
432 fl_set_object_callback(file_dlg_form_->PatBox,
433 C_LyXFileDlg_FileDlgCB, 1);
434 fl_set_object_callback(file_dlg_form_->List,
435 C_LyXFileDlg_FileDlgCB, 2);
436 fl_set_object_callback(file_dlg_form_->Filename,
437 C_LyXFileDlg_FileDlgCB, 3);
438 fl_set_object_callback(file_dlg_form_->Rescan,
439 C_LyXFileDlg_FileDlgCB, 10);
440 fl_set_object_callback(file_dlg_form_->Home,
441 C_LyXFileDlg_FileDlgCB, 11);
442 fl_set_object_callback(file_dlg_form_->User1,
443 C_LyXFileDlg_FileDlgCB, 12);
444 fl_set_object_callback(file_dlg_form_->User2,
445 C_LyXFileDlg_FileDlgCB, 13);
447 // Make sure pressing the close box doesn't crash LyX. (RvdK)
448 fl_set_form_atclose(file_dlg_form_->form,
449 C_LyXFileDlg_CancelCB, 0);
450 // Register doubleclick callback
451 fl_set_browser_dblclick_callback(file_dlg_form_->List,
452 C_LyXFileDlg_DoubleClickCB,
455 fl_hide_object(file_dlg_form_->User1);
456 fl_hide_object(file_dlg_form_->User2);
458 r_ = Dialogs::redrawGUI().connect(boost::bind(&FileDialog::Private::redraw, this));
462 FileDialog::Private::~Private()
468 void FileDialog::Private::redraw()
470 if (file_dlg_form_->form && file_dlg_form_->form->visible)
471 fl_redraw_form(file_dlg_form_->form);
475 // SetButton: sets file selector user button action
476 void FileDialog::Private::SetButton(int index, string const & name,
483 ob = file_dlg_form_->User1;
485 } else if (index == 1) {
486 ob = file_dlg_form_->User2;
493 fl_set_object_label(ob, idex(name).c_str());
494 fl_set_button_shortcut(ob, scex(name).c_str(), 1);
504 // GetDirectory: gets last dialog directory
505 string const FileDialog::Private::GetDirectory() const
507 if (!directory_.empty())
514 bool x_sync_kludge(bool ret)
516 XSync(fl_get_display(), false);
521 // RunDialog: handle dialog during file selection
522 bool FileDialog::Private::RunDialog()
524 force_cancel_ = false;
529 FL_OBJECT * ob = fl_do_forms();
531 if (ob == file_dlg_form_->Ready) {
533 return x_sync_kludge(true);
535 } else if (ob == file_dlg_form_->Cancel || force_cancel_)
536 return x_sync_kludge(false);
539 return x_sync_kludge(true);
544 // XForms objects callback (static)
545 void FileDialog::Private::FileDlgCB(FL_OBJECT *, long arg)
552 case 0: // get directory
553 current_dlg_->SetDirectory(fl_get_input(file_dlg_form_->DirBox));
554 current_dlg_->Reread();
558 current_dlg_->SetFilters(fl_get_input(file_dlg_form_->PatBox));
559 current_dlg_->Reread();
563 current_dlg_->HandleListHit();
567 current_dlg_->SetDirectory(fl_get_input(file_dlg_form_->DirBox));
568 current_dlg_->SetFilters(fl_get_input(file_dlg_form_->PatBox));
569 current_dlg_->Reread();
573 current_dlg_->SetDirectory(GetEnvPath("HOME"));
574 current_dlg_->SetFilters(fl_get_input(file_dlg_form_->PatBox));
575 current_dlg_->Reread();
578 case 12: // user button 1
579 current_dlg_->SetDirectory(current_dlg_->user_path1_);
580 current_dlg_->SetFilters(fl_get_input(file_dlg_form_->PatBox));
581 current_dlg_->Reread();
584 case 13: // user button 2
585 current_dlg_->SetDirectory(current_dlg_->user_path2_);
586 current_dlg_->SetFilters(fl_get_input(file_dlg_form_->PatBox));
587 current_dlg_->Reread();
594 // Handle callback from list
595 void FileDialog::Private::HandleListHit()
598 int const select_ = fl_get_browser(file_dlg_form_->List);
599 if (select_ > depth_)
600 SetInfoLine(dir_entries_[select_ - depth_ - 1].ls_entry_);
602 SetInfoLine(string());
606 // Callback for double click in list
607 void FileDialog::Private::DoubleClickCB(FL_OBJECT *, long)
609 // Simulate click on OK button
610 if (current_dlg_->HandleDoubleClick())
611 current_dlg_->Force(false);
615 // Handle double click from list
616 bool FileDialog::Private::HandleDoubleClick()
622 int const select_ = fl_get_browser(file_dlg_form_->List);
623 if (select_ > depth_) {
624 tmp = dir_entries_[select_ - depth_ - 1].name_;
625 SetInfoLine(dir_entries_[select_ - depth_ - 1].ls_entry_);
626 if (!suffixIs(tmp, '/')) {
628 fl_set_input(file_dlg_form_->Filename, tmp.c_str());
630 } else if (select_ != 0) {
631 SetInfoLine(string());
639 // builds new directory name
640 if (select_ > depth_) {
641 // Directory deeper down
642 // First, get directory with trailing /
643 Temp = fl_get_input(file_dlg_form_->DirBox);
644 if (!suffixIs(Temp, '/'))
648 // Directory higher up
650 for (int i = 0; i < select_; ++i) {
651 string piece = fl_get_browser_line(file_dlg_form_->List, i+1);
652 // The '+2' is here to count the '@b' (JMarc)
653 Temp += piece.substr(i + 2);
666 // Handle OK button call
667 bool FileDialog::Private::HandleOK()
670 string tmp = fl_get_input(file_dlg_form_->PatBox);
677 // directory was changed
678 tmp = fl_get_input(file_dlg_form_->DirBox);
679 if (tmp != directory_) {
685 // Handle return from list
686 int const select = fl_get_browser(file_dlg_form_->List);
687 if (select > depth_) {
688 string const temp = dir_entries_[select - depth_ - 1].name_;
689 if (!suffixIs(temp, '/')) {
690 // If user didn't type anything, use browser
691 string const name = fl_get_input(file_dlg_form_->Filename);
693 fl_set_input(file_dlg_form_->Filename, temp.c_str());
698 // Emulate a doubleclick
699 return HandleDoubleClick();
703 // Handle Cancel CB from WM close
704 int FileDialog::Private::CancelCB(FL_FORM *, void *)
706 // Simulate a click on the cancel button
707 current_dlg_->Force(true);
712 // Simulates a click on OK/Cancel
713 void FileDialog::Private::Force(bool cancel)
716 force_cancel_ = true;
717 fl_set_button(file_dlg_form_->Cancel, 1);
720 fl_set_button(file_dlg_form_->Ready, 1);
722 // Start timer to break fl_do_forms loop soon
723 fl_set_timer(file_dlg_form_->timer, 0.1);
727 // Select: launches dialog and returns selected file
728 string const FileDialog::Private::Select(string const & title,
730 FileFilterList const & filters,
731 string const & suggested)
733 // handles new mask and path
738 // highlight the suggested file in the browser, if it exists.
740 string const filename = OnlyFilename(suggested);
741 if (!filename.empty()) {
742 for (int i = 0; i < fl_get_browser_maxline(file_dlg_form_->List); ++i) {
743 string s = fl_get_browser_line(file_dlg_form_->List, i + 1);
753 fl_select_browser_line(file_dlg_form_->List, sel);
754 int const top = max(sel - 5, 1);
755 fl_set_browser_topline(file_dlg_form_->List, top);
757 // checks whether dialog can be started
763 SetInfoLine(string());
764 setEnabled(file_dlg_form_->Filename, true);
765 fl_set_input(file_dlg_form_->Filename, suggested.c_str());
766 fl_set_button(file_dlg_form_->Cancel, 0);
767 fl_set_button(file_dlg_form_->Ready, 0);
768 fl_set_focus_object(file_dlg_form_->form, file_dlg_form_->Filename);
769 fl_deactivate_all_forms();
770 // Prevent xforms crashing if the dialog gets too small by preventing
771 // it from being shrunk beyond a minimum size.
772 // calls to fl_set_form_minsize/maxsize apply only to the next
773 // fl_show_form(), so this comes first.
774 fl_set_form_minsize(file_dlg_form_->form, minw_, minh_);
776 fl_show_form(file_dlg_form_->form,
777 FL_PLACE_MOUSE | FL_FREE_SIZE, 0,
780 bool const isOk = RunDialog();
782 fl_hide_form(file_dlg_form_->form);
783 fl_activate_all_forms();
786 // Returns filename or string() if no valid selection was made
787 if (!isOk || !fl_get_input(file_dlg_form_->Filename)[0])
790 file_name_ = fl_get_input(file_dlg_form_->Filename);
792 if (!AbsolutePath(file_name_))
793 file_name_ = AddName(fl_get_input(file_dlg_form_->DirBox), file_name_);
798 // SelectDir: launches dialog and returns selected directory
799 string const FileDialog::Private::SelectDir(string const & title,
801 string const & suggested)
807 // handle case where path does not end with "/"
808 // remerge path+suggested and check if it is a valid path
809 if (!suggested.empty()) {
810 string tmp = suggested;
811 if (!suffixIs(tmp, '/'))
813 string full_path = path;
815 // check if this is really a directory
816 DIR * dir = ::opendir(full_path.c_str());
818 SetDirectory(full_path);
828 // checks whether dialog can be started
834 SetInfoLine(string());
835 fl_set_input(file_dlg_form_->Filename, "");
836 setEnabled(file_dlg_form_->Filename, false);
837 fl_set_button(file_dlg_form_->Cancel, 0);
838 fl_set_button(file_dlg_form_->Ready, 0);
839 fl_set_focus_object(file_dlg_form_->form, file_dlg_form_->DirBox);
840 fl_deactivate_all_forms();
841 fl_show_form(file_dlg_form_->form,
842 FL_PLACE_MOUSE | FL_FREE_SIZE, 0,
847 fl_hide_form(file_dlg_form_->form);
848 fl_activate_all_forms();
851 // Returns directory or string() if no valid selection was made
855 file_name_ = fl_get_input(file_dlg_form_->DirBox);