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
26 #include "forms_gettext.h"
27 #include "xforms_helpers.h"
29 #include "support/FileInfo.h"
30 #include "support/lyxlib.h"
31 #include "support/lstrings.h"
32 #include "support/tostr.h"
33 #include "support/filetools.h"
35 #include "frontends/Alert.h"
36 #include "frontends/Dialogs.h"
38 #include <boost/bind.hpp>
47 # define dirent direct
49 # include <sys/ndir.h>
60 #include "FormFiledialog.h"
61 #include "forms/form_filedialog.h"
62 #include "lyx_forms.h"
66 // six months, in seconds
67 long const SIX_MONTH_SEC = 6L * 30L * 24L * 60L * 60L;
69 long const ONE_HOUR_SEC = 60L * 60L;
74 int C_LyXFileDlg_CancelCB(FL_FORM *fl, void *xev)
76 return FileDialog::Private::CancelCB(fl, xev);
80 void C_LyXFileDlg_DoubleClickCB(FL_OBJECT * ob, long data)
82 FileDialog::Private::DoubleClickCB(ob, data);
86 void C_LyXFileDlg_FileDlgCB(FL_OBJECT * ob, long data)
88 FileDialog::Private::FileDlgCB(ob, data);
93 // *** User cache class implementation
94 /// User cache class definition
97 /// seeks user name from group ID
98 string const & find(uid_t ID) const {
99 Users::const_iterator cit = users.find(ID);
100 if (cit == users.end()) {
108 void add(uid_t ID) const;
110 typedef map<uid_t, string> Users;
116 void UserCache::add(uid_t ID) const
118 struct passwd const * entry = getpwuid(ID);
119 users[ID] = entry ? entry->pw_name : tostr(ID);
123 /// Group cache class definition
126 /// seeks group name from group ID
127 string const & find(gid_t ID) const ;
130 void add(gid_t ID) const;
132 typedef map<gid_t, string> Groups;
134 mutable Groups groups;
138 string const & GroupCache::find(gid_t ID) const
140 Groups::const_iterator cit = groups.find(ID);
141 if (cit == groups.end()) {
149 void GroupCache::add(gid_t ID) const
151 struct group const * entry = getgrgid(ID);
152 groups[ID] = entry ? entry->gr_name : tostr(ID);
156 UserCache lyxUserCache;
157 GroupCache lyxGroupCache;
159 // compares two LyXDirEntry objects content (used for sort)
160 class comp_direntry {
162 bool operator()(DirEntry const & r1, DirEntry const & r2) const
164 bool const r1d = suffixIs(r1.name_, '/');
165 bool const r2d = suffixIs(r2.name_, '/');
170 return r1.name_ < r2.name_;
179 // *** FileDialog::Private class implementation
182 FD_filedialog * FileDialog::Private::file_dlg_form_ = 0;
183 FileDialog::Private * FileDialog::Private::current_dlg_ = 0;
184 int FileDialog::Private::minw_ = 0;
185 int FileDialog::Private::minh_ = 0;
188 // Reread: updates dialog list to match class directory
189 void FileDialog::Private::Reread()
192 DIR * dir = ::opendir(directory_.c_str());
196 Alert::err_alert(_("Warning! Couldn't open directory."),
199 directory_ = lyx::getcwd();
200 dir = ::opendir(directory_.c_str());
203 // Clear the present namelist
204 dir_entries_.clear();
207 fl_hide_object(file_dlg_form_->List);
208 fl_clear_browser(file_dlg_form_->List);
209 fl_set_input(file_dlg_form_->DirBox, directory_.c_str());
211 // Splits complete directory name into directories and compute depth
215 string File = directory_;
217 File = split(File, Temp, '/');
219 while (!File.empty() || !Temp.empty()) {
220 string dline = "@b" + line + Temp + '/';
221 fl_add_browser_line(file_dlg_form_->List, dline.c_str());
222 File = split(File, Temp, '/');
227 // Parses all entries of the given subdirectory
228 time_t curTime = time(0);
230 while (dirent * entry = readdir(dir)) {
231 bool isLink = false, isDir = false;
233 // If the pattern doesn't start with a dot, skip hidden files
234 if (!mask_.empty() && mask_[0] != '.' &&
235 entry->d_name[0] == '.')
239 string fname = entry->d_name;
241 // Under all circumstances, "." and ".." are not wanted
242 if (fname == "." || fname == "..")
246 File = AddName(directory_, fname);
248 FileInfo fileInfo(File, true);
250 // can this really happen?
251 if (!fileInfo.isOK())
254 mode = fileInfo.modeString();
255 unsigned int const nlink = fileInfo.getNumberOfLinks();
256 string const user = lyxUserCache.find(fileInfo.getUid());
257 string const group = lyxGroupCache.find(fileInfo.getGid());
259 time_t modtime = fileInfo.getModificationTime();
260 string Time = ctime(&modtime);
262 if (curTime > modtime + SIX_MONTH_SEC
263 || curTime < modtime + ONE_HOUR_SEC) {
264 // The file is fairly old or in the future. POSIX says
265 // the cutoff is 6 months old. Allow a 1 hour slop
266 // factor for what is considered "the future", to
267 // allow for NFS server/client clock disagreement.
268 // Show the year instead of the time of day.
270 Time.erase(15, string::npos);
272 Time.erase(16, string::npos);
275 string buffer = mode + ' ' +
279 Time.substr(4, string::npos) + ' ';
281 buffer += entry->d_name;
282 buffer += fileInfo.typeIndicator();
284 isLink = fileInfo.isLink();
288 if (LyXReadLink(File, Link)) {
292 // This gives the FileType of the file that
293 // is really pointed too after resolving all
294 // symlinks. This is not necessarily the same
295 // as the type of Link (which could again be a
296 // link). Is that intended?
298 fileInfo.newFile(File);
300 buffer += fileInfo.typeIndicator();
306 // filters files according to pattern and type
307 if (fileInfo.isRegular()
309 || fileInfo.isBlock()
310 || fileInfo.isFifo()) {
311 if (!regexMatch(fname, mask_))
313 } else if (!(isDir = fileInfo.isDir()))
318 // Note ls_entry_ is an string!
319 tmp.ls_entry_ = buffer;
326 // creates displayed name
327 temp = entry->d_name;
331 temp += fileInfo.typeIndicator();
332 tmp.displayed_ = temp;
334 dir_entries_.push_back(tmp);
340 sort(dir_entries_.begin(), dir_entries_.end(), comp_direntry());
342 // Add them to directory box
343 for (DirEntries::const_iterator cit = dir_entries_.begin();
344 cit != dir_entries_.end(); ++cit) {
345 string const temp = line + cit->displayed_;
346 fl_add_browser_line(file_dlg_form_->List, temp.c_str());
348 fl_set_browser_topline(file_dlg_form_->List, depth_);
349 fl_show_object(file_dlg_form_->List);
354 // SetDirectory: sets dialog current directory
355 void FileDialog::Private::SetDirectory(string const & path)
361 tmp = MakeAbsPath(ExpandPath(path), directory_);
363 // must check the directory exists
364 DIR * dir = ::opendir(tmp.c_str());
368 Alert::err_alert(_("Warning! Couldn't open directory."), tmp);
377 // SetMask: sets dialog file mask
378 void FileDialog::Private::SetMask(string const & newmask)
381 fl_set_input(file_dlg_form_->PatBox, mask_.c_str());
385 // SetInfoLine: sets dialog information line
386 void FileDialog::Private::SetInfoLine(string const & line)
389 fl_set_object_label(file_dlg_form_->FileInfo, info_line_.c_str());
393 FileDialog::Private::Private()
395 directory_ = MakeAbsPath(string("."));
398 // Creates form if necessary.
399 if (!file_dlg_form_) {
400 file_dlg_form_ = build_filedialog(this);
401 minw_ = file_dlg_form_->form->w;
402 minh_ = file_dlg_form_->form->h;
403 // Set callbacks. This means that we don't need a patch file
404 fl_set_object_callback(file_dlg_form_->DirBox,
405 C_LyXFileDlg_FileDlgCB, 0);
406 fl_set_object_callback(file_dlg_form_->PatBox,
407 C_LyXFileDlg_FileDlgCB, 1);
408 fl_set_object_callback(file_dlg_form_->List,
409 C_LyXFileDlg_FileDlgCB, 2);
410 fl_set_object_callback(file_dlg_form_->Filename,
411 C_LyXFileDlg_FileDlgCB, 3);
412 fl_set_object_callback(file_dlg_form_->Rescan,
413 C_LyXFileDlg_FileDlgCB, 10);
414 fl_set_object_callback(file_dlg_form_->Home,
415 C_LyXFileDlg_FileDlgCB, 11);
416 fl_set_object_callback(file_dlg_form_->User1,
417 C_LyXFileDlg_FileDlgCB, 12);
418 fl_set_object_callback(file_dlg_form_->User2,
419 C_LyXFileDlg_FileDlgCB, 13);
421 // Make sure pressing the close box doesn't crash LyX. (RvdK)
422 fl_set_form_atclose(file_dlg_form_->form,
423 C_LyXFileDlg_CancelCB, 0);
424 // Register doubleclick callback
425 fl_set_browser_dblclick_callback(file_dlg_form_->List,
426 C_LyXFileDlg_DoubleClickCB,
429 fl_hide_object(file_dlg_form_->User1);
430 fl_hide_object(file_dlg_form_->User2);
432 r_ = Dialogs::redrawGUI().connect(boost::bind(&FileDialog::Private::redraw, this));
436 FileDialog::Private::~Private()
442 void FileDialog::Private::redraw()
444 if (file_dlg_form_->form && file_dlg_form_->form->visible)
445 fl_redraw_form(file_dlg_form_->form);
449 // SetButton: sets file selector user button action
450 void FileDialog::Private::SetButton(int index, string const & name,
457 ob = file_dlg_form_->User1;
459 } else if (index == 1) {
460 ob = file_dlg_form_->User2;
467 fl_set_object_label(ob, idex(name).c_str());
468 fl_set_button_shortcut(ob, scex(name).c_str(), 1);
478 // GetDirectory: gets last dialog directory
479 string const FileDialog::Private::GetDirectory() const
481 if (!directory_.empty())
488 bool x_sync_kludge(bool ret)
490 XSync(fl_get_display(), false);
495 // RunDialog: handle dialog during file selection
496 bool FileDialog::Private::RunDialog()
498 force_cancel_ = false;
503 FL_OBJECT * ob = fl_do_forms();
505 if (ob == file_dlg_form_->Ready) {
507 return x_sync_kludge(true);
509 } else if (ob == file_dlg_form_->Cancel || force_cancel_)
510 return x_sync_kludge(false);
513 return x_sync_kludge(true);
518 // XForms objects callback (static)
519 void FileDialog::Private::FileDlgCB(FL_OBJECT *, long arg)
526 case 0: // get directory
527 current_dlg_->SetDirectory(fl_get_input(file_dlg_form_->DirBox));
528 current_dlg_->Reread();
532 current_dlg_->SetMask(fl_get_input(file_dlg_form_->PatBox));
533 current_dlg_->Reread();
537 current_dlg_->HandleListHit();
541 current_dlg_->SetDirectory(fl_get_input(file_dlg_form_->DirBox));
542 current_dlg_->SetMask(fl_get_input(file_dlg_form_->PatBox));
543 current_dlg_->Reread();
547 current_dlg_->SetDirectory(GetEnvPath("HOME"));
548 current_dlg_->SetMask(fl_get_input(file_dlg_form_->PatBox));
549 current_dlg_->Reread();
552 case 12: // user button 1
553 current_dlg_->SetDirectory(current_dlg_->user_path1_);
554 current_dlg_->SetMask(fl_get_input(file_dlg_form_->PatBox));
555 current_dlg_->Reread();
558 case 13: // user button 2
559 current_dlg_->SetDirectory(current_dlg_->user_path2_);
560 current_dlg_->SetMask(fl_get_input(file_dlg_form_->PatBox));
561 current_dlg_->Reread();
568 // Handle callback from list
569 void FileDialog::Private::HandleListHit()
572 int const select_ = fl_get_browser(file_dlg_form_->List);
573 if (select_ > depth_)
574 SetInfoLine(dir_entries_[select_ - depth_ - 1].ls_entry_);
576 SetInfoLine(string());
580 // Callback for double click in list
581 void FileDialog::Private::DoubleClickCB(FL_OBJECT *, long)
583 // Simulate click on OK button
584 if (current_dlg_->HandleDoubleClick())
585 current_dlg_->Force(false);
589 // Handle double click from list
590 bool FileDialog::Private::HandleDoubleClick()
596 int const select_ = fl_get_browser(file_dlg_form_->List);
597 if (select_ > depth_) {
598 tmp = dir_entries_[select_ - depth_ - 1].name_;
599 SetInfoLine(dir_entries_[select_ - depth_ - 1].ls_entry_);
600 if (!suffixIs(tmp, '/')) {
602 fl_set_input(file_dlg_form_->Filename, tmp.c_str());
604 } else if (select_ != 0) {
605 SetInfoLine(string());
613 // builds new directory name
614 if (select_ > depth_) {
615 // Directory deeper down
616 // First, get directory with trailing /
617 Temp = fl_get_input(file_dlg_form_->DirBox);
618 if (!suffixIs(Temp, '/'))
622 // Directory higher up
624 for (int i = 0; i < select_; ++i) {
625 string piece = fl_get_browser_line(file_dlg_form_->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 FileDialog::Private::HandleOK()
644 string tmp = fl_get_input(file_dlg_form_->PatBox);
651 // directory was changed
652 tmp = fl_get_input(file_dlg_form_->DirBox);
653 if (tmp != directory_) {
659 // Handle return from list
660 int const select = fl_get_browser(file_dlg_form_->List);
661 if (select > depth_) {
662 string const temp = dir_entries_[select - depth_ - 1].name_;
663 if (!suffixIs(temp, '/')) {
664 // If user didn't type anything, use browser
665 string const name = fl_get_input(file_dlg_form_->Filename);
667 fl_set_input(file_dlg_form_->Filename, temp.c_str());
672 // Emulate a doubleclick
673 return HandleDoubleClick();
677 // Handle Cancel CB from WM close
678 int FileDialog::Private::CancelCB(FL_FORM *, void *)
680 // Simulate a click on the cancel button
681 current_dlg_->Force(true);
686 // Simulates a click on OK/Cancel
687 void FileDialog::Private::Force(bool cancel)
690 force_cancel_ = true;
691 fl_set_button(file_dlg_form_->Cancel, 1);
694 fl_set_button(file_dlg_form_->Ready, 1);
696 // Start timer to break fl_do_forms loop soon
697 fl_set_timer(file_dlg_form_->timer, 0.1);
701 // Select: launches dialog and returns selected file
702 string const FileDialog::Private::Select(string const & title,
705 string const & suggested)
707 // handles new mask and path
720 // highlight the suggested file in the browser, if it exists.
722 string const filename = OnlyFilename(suggested);
723 if (!filename.empty()) {
724 for (int i = 0; i < fl_get_browser_maxline(file_dlg_form_->List); ++i) {
725 string s = fl_get_browser_line(file_dlg_form_->List, i + 1);
735 fl_select_browser_line(file_dlg_form_->List, sel);
736 int const top = max(sel - 5, 1);
737 fl_set_browser_topline(file_dlg_form_->List, top);
739 // checks whether dialog can be started
745 SetInfoLine(string());
746 setEnabled(file_dlg_form_->Filename, true);
747 fl_set_input(file_dlg_form_->Filename, suggested.c_str());
748 fl_set_button(file_dlg_form_->Cancel, 0);
749 fl_set_button(file_dlg_form_->Ready, 0);
750 fl_set_focus_object(file_dlg_form_->form, file_dlg_form_->Filename);
751 fl_deactivate_all_forms();
752 // Prevent xforms crashing if the dialog gets too small by preventing
753 // it from being shrunk beyond a minimum size.
754 // calls to fl_set_form_minsize/maxsize apply only to the next
755 // fl_show_form(), so this comes first.
756 fl_set_form_minsize(file_dlg_form_->form, minw_, minh_);
758 fl_show_form(file_dlg_form_->form,
759 FL_PLACE_MOUSE | FL_FREE_SIZE, 0,
764 fl_hide_form(file_dlg_form_->form);
765 fl_activate_all_forms();
768 // Returns filename or string() if no valid selection was made
769 if (!isOk || !fl_get_input(file_dlg_form_->Filename)[0])
772 file_name_ = fl_get_input(file_dlg_form_->Filename);
774 if (!AbsolutePath(file_name_))
775 file_name_ = AddName(fl_get_input(file_dlg_form_->DirBox), file_name_);
780 // SelectDir: launches dialog and returns selected directory
781 string const FileDialog::Private::SelectDir(string const & title,
783 string const & suggested)
789 // handle case where path does not end with "/"
790 // remerge path+suggested and check if it is a valid path
791 if (!suggested.empty()) {
792 string tmp = suggested;
793 if (!suffixIs(tmp, '/'))
795 string full_path = path;
797 // check if this is really a directory
798 DIR * dir = ::opendir(full_path.c_str());
800 SetDirectory(full_path);
810 // checks whether dialog can be started
816 SetInfoLine(string());
817 fl_set_input(file_dlg_form_->Filename, "");
818 setEnabled(file_dlg_form_->Filename, false);
819 fl_set_button(file_dlg_form_->Cancel, 0);
820 fl_set_button(file_dlg_form_->Ready, 0);
821 fl_set_focus_object(file_dlg_form_->form, file_dlg_form_->DirBox);
822 fl_deactivate_all_forms();
823 fl_show_form(file_dlg_form_->form,
824 FL_PLACE_MOUSE | FL_FREE_SIZE, 0,
829 fl_hide_form(file_dlg_form_->form);
830 fl_activate_all_forms();
833 // Returns directory or string() if no valid selection was made
837 file_name_ = fl_get_input(file_dlg_form_->DirBox);