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/lyxlib.h"
24 #include "support/lstrings.h"
25 #include "support/tostr.h"
26 #include "support/filetools.h"
28 #include "lyx_forms.h"
30 #include <boost/bind.hpp>
44 # define dirent direct
46 # include <sys/ndir.h>
60 using namespace lyx::support;
65 // six months, in seconds
66 long const SIX_MONTH_SEC = 6L * 30L * 24L * 60L * 60L;
68 long const ONE_HOUR_SEC = 60L * 60L;
73 int C_LyXFileDlg_CancelCB(FL_FORM *fl, void *xev)
75 return FileDialog::Private::CancelCB(fl, xev);
79 void C_LyXFileDlg_DoubleClickCB(FL_OBJECT * ob, long data)
81 FileDialog::Private::DoubleClickCB(ob, data);
85 void C_LyXFileDlg_FileDlgCB(FL_OBJECT * ob, long data)
87 FileDialog::Private::FileDlgCB(ob, data);
92 // *** User cache class implementation
93 /// User cache class definition
96 /// seeks user name from group ID
97 string const & find(uid_t ID) const {
98 Users::const_iterator cit = users.find(ID);
99 if (cit == users.end()) {
107 void add(uid_t ID) const;
109 typedef map<uid_t, string> Users;
115 void UserCache::add(uid_t ID) const
117 struct passwd const * entry = getpwuid(ID);
118 users[ID] = entry ? entry->pw_name : tostr(ID);
122 /// Group cache class definition
125 /// seeks group name from group ID
126 string const & find(gid_t ID) const ;
129 void add(gid_t ID) const;
131 typedef map<gid_t, string> Groups;
133 mutable Groups groups;
137 string const & GroupCache::find(gid_t ID) const
139 Groups::const_iterator cit = groups.find(ID);
140 if (cit == groups.end()) {
148 void GroupCache::add(gid_t ID) const
150 struct group const * entry = getgrgid(ID);
151 groups[ID] = entry ? entry->gr_name : tostr(ID);
155 UserCache lyxUserCache;
156 GroupCache lyxGroupCache;
158 // compares two LyXDirEntry objects content (used for sort)
159 class comp_direntry {
161 bool operator()(DirEntry const & r1, DirEntry const & r2) const
163 bool const r1d = suffixIs(r1.name_, '/');
164 bool const r2d = suffixIs(r2.name_, '/');
169 return r1.name_ < r2.name_;
178 // *** FileDialog::Private class implementation
181 FD_filedialog * FileDialog::Private::file_dlg_form_ = 0;
182 FileDialog::Private * FileDialog::Private::current_dlg_ = 0;
183 int FileDialog::Private::minw_ = 0;
184 int FileDialog::Private::minh_ = 0;
187 // Reread: updates dialog list to match class directory
188 void FileDialog::Private::Reread()
191 DIR * dir = ::opendir(directory_.c_str());
195 Alert::err_alert(_("Warning! Couldn't open directory."),
198 directory_ = getcwd();
199 dir = ::opendir(directory_.c_str());
202 // Clear the present namelist
203 dir_entries_.clear();
206 fl_hide_object(file_dlg_form_->List);
207 fl_clear_browser(file_dlg_form_->List);
208 fl_set_input(file_dlg_form_->DirBox, directory_.c_str());
210 // Splits complete directory name into directories and compute depth
214 string File = directory_;
216 File = split(File, Temp, '/');
218 while (!File.empty() || !Temp.empty()) {
219 string dline = "@b" + line + Temp + '/';
220 fl_add_browser_line(file_dlg_form_->List, dline.c_str());
221 File = split(File, Temp, '/');
226 // Parses all entries of the given subdirectory
227 time_t curTime = time(0);
229 while (dirent * entry = readdir(dir)) {
230 bool isLink = false, isDir = false;
232 // If the pattern doesn't start with a dot, skip hidden files
233 if (!mask_.empty() && mask_[0] != '.' &&
234 entry->d_name[0] == '.')
238 string fname = entry->d_name;
240 // Under all circumstances, "." and ".." are not wanted
241 if (fname == "." || fname == "..")
245 File = AddName(directory_, fname);
247 FileInfo fileInfo(File, true);
249 // can this really happen?
250 if (!fileInfo.isOK())
253 mode = fileInfo.modeString();
254 unsigned int const nlink = fileInfo.getNumberOfLinks();
255 string const user = lyxUserCache.find(fileInfo.getUid());
256 string const group = lyxGroupCache.find(fileInfo.getGid());
258 time_t modtime = fileInfo.getModificationTime();
259 string Time = ctime(&modtime);
261 if (curTime > modtime + SIX_MONTH_SEC
262 || curTime < modtime + ONE_HOUR_SEC) {
263 // The file is fairly old or in the future. POSIX says
264 // the cutoff is 6 months old. Allow a 1 hour slop
265 // factor for what is considered "the future", to
266 // allow for NFS server/client clock disagreement.
267 // Show the year instead of the time of day.
269 Time.erase(15, string::npos);
271 Time.erase(16, string::npos);
274 string buffer = mode + ' ' +
278 Time.substr(4, string::npos) + ' ';
280 buffer += entry->d_name;
281 buffer += fileInfo.typeIndicator();
283 isLink = fileInfo.isLink();
287 if (LyXReadLink(File, Link)) {
291 // This gives the FileType of the file that
292 // is really pointed too after resolving all
293 // symlinks. This is not necessarily the same
294 // as the type of Link (which could again be a
295 // link). Is that intended?
297 fileInfo.newFile(File);
299 buffer += fileInfo.typeIndicator();
305 // filters files according to pattern and type
306 if (fileInfo.isRegular()
308 || fileInfo.isBlock()
309 || fileInfo.isFifo()) {
310 if (!regexMatch(fname, mask_))
312 } else if (!(isDir = fileInfo.isDir()))
317 // Note ls_entry_ is an string!
318 tmp.ls_entry_ = buffer;
325 // creates displayed name
326 temp = entry->d_name;
330 temp += fileInfo.typeIndicator();
331 tmp.displayed_ = temp;
333 dir_entries_.push_back(tmp);
339 sort(dir_entries_.begin(), dir_entries_.end(), comp_direntry());
341 // Add them to directory box
342 for (DirEntries::const_iterator cit = dir_entries_.begin();
343 cit != dir_entries_.end(); ++cit) {
344 string const temp = line + cit->displayed_;
345 fl_add_browser_line(file_dlg_form_->List, temp.c_str());
347 fl_set_browser_topline(file_dlg_form_->List, depth_);
348 fl_show_object(file_dlg_form_->List);
353 // SetDirectory: sets dialog current directory
354 void FileDialog::Private::SetDirectory(string const & path)
360 tmp = MakeAbsPath(ExpandPath(path), directory_);
362 // must check the directory exists
363 DIR * dir = ::opendir(tmp.c_str());
367 Alert::err_alert(_("Warning! Couldn't open directory."), tmp);
376 // SetMask: sets dialog file mask
377 void FileDialog::Private::SetMask(string const & newmask)
380 fl_set_input(file_dlg_form_->PatBox, mask_.c_str());
384 // SetInfoLine: sets dialog information line
385 void FileDialog::Private::SetInfoLine(string const & line)
388 fl_set_object_label(file_dlg_form_->FileInfo, info_line_.c_str());
392 FileDialog::Private::Private()
394 directory_ = MakeAbsPath(string("."));
397 // Creates form if necessary.
398 if (!file_dlg_form_) {
399 file_dlg_form_ = build_filedialog(this);
400 minw_ = file_dlg_form_->form->w;
401 minh_ = file_dlg_form_->form->h;
402 // Set callbacks. This means that we don't need a patch file
403 fl_set_object_callback(file_dlg_form_->DirBox,
404 C_LyXFileDlg_FileDlgCB, 0);
405 fl_set_object_callback(file_dlg_form_->PatBox,
406 C_LyXFileDlg_FileDlgCB, 1);
407 fl_set_object_callback(file_dlg_form_->List,
408 C_LyXFileDlg_FileDlgCB, 2);
409 fl_set_object_callback(file_dlg_form_->Filename,
410 C_LyXFileDlg_FileDlgCB, 3);
411 fl_set_object_callback(file_dlg_form_->Rescan,
412 C_LyXFileDlg_FileDlgCB, 10);
413 fl_set_object_callback(file_dlg_form_->Home,
414 C_LyXFileDlg_FileDlgCB, 11);
415 fl_set_object_callback(file_dlg_form_->User1,
416 C_LyXFileDlg_FileDlgCB, 12);
417 fl_set_object_callback(file_dlg_form_->User2,
418 C_LyXFileDlg_FileDlgCB, 13);
420 // Make sure pressing the close box doesn't crash LyX. (RvdK)
421 fl_set_form_atclose(file_dlg_form_->form,
422 C_LyXFileDlg_CancelCB, 0);
423 // Register doubleclick callback
424 fl_set_browser_dblclick_callback(file_dlg_form_->List,
425 C_LyXFileDlg_DoubleClickCB,
428 fl_hide_object(file_dlg_form_->User1);
429 fl_hide_object(file_dlg_form_->User2);
431 r_ = Dialogs::redrawGUI().connect(boost::bind(&FileDialog::Private::redraw, this));
435 FileDialog::Private::~Private()
441 void FileDialog::Private::redraw()
443 if (file_dlg_form_->form && file_dlg_form_->form->visible)
444 fl_redraw_form(file_dlg_form_->form);
448 // SetButton: sets file selector user button action
449 void FileDialog::Private::SetButton(int index, string const & name,
456 ob = file_dlg_form_->User1;
458 } else if (index == 1) {
459 ob = file_dlg_form_->User2;
466 fl_set_object_label(ob, idex(name).c_str());
467 fl_set_button_shortcut(ob, scex(name).c_str(), 1);
477 // GetDirectory: gets last dialog directory
478 string const FileDialog::Private::GetDirectory() const
480 if (!directory_.empty())
487 bool x_sync_kludge(bool ret)
489 XSync(fl_get_display(), false);
494 // RunDialog: handle dialog during file selection
495 bool FileDialog::Private::RunDialog()
497 force_cancel_ = false;
502 FL_OBJECT * ob = fl_do_forms();
504 if (ob == file_dlg_form_->Ready) {
506 return x_sync_kludge(true);
508 } else if (ob == file_dlg_form_->Cancel || force_cancel_)
509 return x_sync_kludge(false);
512 return x_sync_kludge(true);
517 // XForms objects callback (static)
518 void FileDialog::Private::FileDlgCB(FL_OBJECT *, long arg)
525 case 0: // get directory
526 current_dlg_->SetDirectory(fl_get_input(file_dlg_form_->DirBox));
527 current_dlg_->Reread();
531 current_dlg_->SetMask(fl_get_input(file_dlg_form_->PatBox));
532 current_dlg_->Reread();
536 current_dlg_->HandleListHit();
540 current_dlg_->SetDirectory(fl_get_input(file_dlg_form_->DirBox));
541 current_dlg_->SetMask(fl_get_input(file_dlg_form_->PatBox));
542 current_dlg_->Reread();
546 current_dlg_->SetDirectory(GetEnvPath("HOME"));
547 current_dlg_->SetMask(fl_get_input(file_dlg_form_->PatBox));
548 current_dlg_->Reread();
551 case 12: // user button 1
552 current_dlg_->SetDirectory(current_dlg_->user_path1_);
553 current_dlg_->SetMask(fl_get_input(file_dlg_form_->PatBox));
554 current_dlg_->Reread();
557 case 13: // user button 2
558 current_dlg_->SetDirectory(current_dlg_->user_path2_);
559 current_dlg_->SetMask(fl_get_input(file_dlg_form_->PatBox));
560 current_dlg_->Reread();
567 // Handle callback from list
568 void FileDialog::Private::HandleListHit()
571 int const select_ = fl_get_browser(file_dlg_form_->List);
572 if (select_ > depth_)
573 SetInfoLine(dir_entries_[select_ - depth_ - 1].ls_entry_);
575 SetInfoLine(string());
579 // Callback for double click in list
580 void FileDialog::Private::DoubleClickCB(FL_OBJECT *, long)
582 // Simulate click on OK button
583 if (current_dlg_->HandleDoubleClick())
584 current_dlg_->Force(false);
588 // Handle double click from list
589 bool FileDialog::Private::HandleDoubleClick()
595 int const select_ = fl_get_browser(file_dlg_form_->List);
596 if (select_ > depth_) {
597 tmp = dir_entries_[select_ - depth_ - 1].name_;
598 SetInfoLine(dir_entries_[select_ - depth_ - 1].ls_entry_);
599 if (!suffixIs(tmp, '/')) {
601 fl_set_input(file_dlg_form_->Filename, tmp.c_str());
603 } else if (select_ != 0) {
604 SetInfoLine(string());
612 // builds new directory name
613 if (select_ > depth_) {
614 // Directory deeper down
615 // First, get directory with trailing /
616 Temp = fl_get_input(file_dlg_form_->DirBox);
617 if (!suffixIs(Temp, '/'))
621 // Directory higher up
623 for (int i = 0; i < select_; ++i) {
624 string piece = fl_get_browser_line(file_dlg_form_->List, i+1);
625 // The '+2' is here to count the '@b' (JMarc)
626 Temp += piece.substr(i + 2);
639 // Handle OK button call
640 bool FileDialog::Private::HandleOK()
643 string tmp = fl_get_input(file_dlg_form_->PatBox);
650 // directory was changed
651 tmp = fl_get_input(file_dlg_form_->DirBox);
652 if (tmp != directory_) {
658 // Handle return from list
659 int const select = fl_get_browser(file_dlg_form_->List);
660 if (select > depth_) {
661 string const temp = dir_entries_[select - depth_ - 1].name_;
662 if (!suffixIs(temp, '/')) {
663 // If user didn't type anything, use browser
664 string const name = fl_get_input(file_dlg_form_->Filename);
666 fl_set_input(file_dlg_form_->Filename, temp.c_str());
671 // Emulate a doubleclick
672 return HandleDoubleClick();
676 // Handle Cancel CB from WM close
677 int FileDialog::Private::CancelCB(FL_FORM *, void *)
679 // Simulate a click on the cancel button
680 current_dlg_->Force(true);
685 // Simulates a click on OK/Cancel
686 void FileDialog::Private::Force(bool cancel)
689 force_cancel_ = true;
690 fl_set_button(file_dlg_form_->Cancel, 1);
693 fl_set_button(file_dlg_form_->Ready, 1);
695 // Start timer to break fl_do_forms loop soon
696 fl_set_timer(file_dlg_form_->timer, 0.1);
700 // Select: launches dialog and returns selected file
701 string const FileDialog::Private::Select(string const & title,
704 string const & suggested)
706 // handles new mask and path
719 // highlight the suggested file in the browser, if it exists.
721 string const filename = OnlyFilename(suggested);
722 if (!filename.empty()) {
723 for (int i = 0; i < fl_get_browser_maxline(file_dlg_form_->List); ++i) {
724 string s = fl_get_browser_line(file_dlg_form_->List, i + 1);
734 fl_select_browser_line(file_dlg_form_->List, sel);
735 int const top = max(sel - 5, 1);
736 fl_set_browser_topline(file_dlg_form_->List, top);
738 // checks whether dialog can be started
744 SetInfoLine(string());
745 setEnabled(file_dlg_form_->Filename, true);
746 fl_set_input(file_dlg_form_->Filename, suggested.c_str());
747 fl_set_button(file_dlg_form_->Cancel, 0);
748 fl_set_button(file_dlg_form_->Ready, 0);
749 fl_set_focus_object(file_dlg_form_->form, file_dlg_form_->Filename);
750 fl_deactivate_all_forms();
751 // Prevent xforms crashing if the dialog gets too small by preventing
752 // it from being shrunk beyond a minimum size.
753 // calls to fl_set_form_minsize/maxsize apply only to the next
754 // fl_show_form(), so this comes first.
755 fl_set_form_minsize(file_dlg_form_->form, minw_, minh_);
757 fl_show_form(file_dlg_form_->form,
758 FL_PLACE_MOUSE | FL_FREE_SIZE, 0,
763 fl_hide_form(file_dlg_form_->form);
764 fl_activate_all_forms();
767 // Returns filename or string() if no valid selection was made
768 if (!isOk || !fl_get_input(file_dlg_form_->Filename)[0])
771 file_name_ = fl_get_input(file_dlg_form_->Filename);
773 if (!AbsolutePath(file_name_))
774 file_name_ = AddName(fl_get_input(file_dlg_form_->DirBox), file_name_);
779 // SelectDir: launches dialog and returns selected directory
780 string const FileDialog::Private::SelectDir(string const & title,
782 string const & suggested)
788 // handle case where path does not end with "/"
789 // remerge path+suggested and check if it is a valid path
790 if (!suggested.empty()) {
791 string tmp = suggested;
792 if (!suffixIs(tmp, '/'))
794 string full_path = path;
796 // check if this is really a directory
797 DIR * dir = ::opendir(full_path.c_str());
799 SetDirectory(full_path);
809 // checks whether dialog can be started
815 SetInfoLine(string());
816 fl_set_input(file_dlg_form_->Filename, "");
817 setEnabled(file_dlg_form_->Filename, false);
818 fl_set_button(file_dlg_form_->Cancel, 0);
819 fl_set_button(file_dlg_form_->Ready, 0);
820 fl_set_focus_object(file_dlg_form_->form, file_dlg_form_->DirBox);
821 fl_deactivate_all_forms();
822 fl_show_form(file_dlg_form_->form,
823 FL_PLACE_MOUSE | FL_FREE_SIZE, 0,
828 fl_hide_form(file_dlg_form_->form);
829 fl_activate_all_forms();
832 // Returns directory or string() if no valid selection was made
836 file_name_ = fl_get_input(file_dlg_form_->DirBox);