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
25 #include "frontends/Alert.h"
26 #include "support/FileInfo.h"
27 #include "support/lyxlib.h"
28 #include "support/lstrings.h"
30 #include "frontends/Dialogs.h"
31 #include "forms_gettext.h"
32 #include "xforms_helpers.h"
34 #include <boost/bind.hpp>
43 # define dirent direct
45 # include <sys/ndir.h>
56 #include "support/filetools.h"
57 #include "FormFiledialog.h"
58 #include "forms/form_filedialog.h"
59 #include FORMS_H_LOCATION
63 // six months, in seconds
64 long const SIX_MONTH_SEC = 6L * 30L * 24L * 60L * 60L;
66 long const ONE_HOUR_SEC = 60L * 60L;
71 int C_LyXFileDlg_CancelCB(FL_FORM *fl, void *xev)
73 return FileDialog::Private::CancelCB(fl, xev);
77 void C_LyXFileDlg_DoubleClickCB(FL_OBJECT * ob, long data)
79 FileDialog::Private::DoubleClickCB(ob, data);
83 void C_LyXFileDlg_FileDlgCB(FL_OBJECT * ob, long data)
85 FileDialog::Private::FileDlgCB(ob, data);
90 // *** User cache class implementation
91 /// User cache class definition
94 /// seeks user name from group ID
95 string const & find(uid_t ID) const {
96 Users::const_iterator cit = users.find(ID);
97 if (cit == users.end()) {
105 void add(uid_t ID) const;
107 typedef map<uid_t, string> Users;
113 void UserCache::add(uid_t ID) const
115 struct passwd const * entry = getpwuid(ID);
116 users[ID] = entry ? entry->pw_name : tostr(ID);
120 /// Group cache class definition
123 /// seeks group name from group ID
124 string const & find(gid_t ID) const ;
127 void add(gid_t ID) const;
129 typedef map<gid_t, string> Groups;
131 mutable Groups groups;
135 string const & GroupCache::find(gid_t ID) const
137 Groups::const_iterator cit = groups.find(ID);
138 if (cit == groups.end()) {
146 void GroupCache::add(gid_t ID) const
148 struct group const * entry = getgrgid(ID);
149 groups[ID] = entry ? entry->gr_name : tostr(ID);
153 UserCache lyxUserCache;
154 GroupCache lyxGroupCache;
156 // compares two LyXDirEntry objects content (used for sort)
157 class comp_direntry {
159 bool operator()(DirEntry const & r1, DirEntry const & r2) const
161 bool const r1d = suffixIs(r1.name_, '/');
162 bool const r2d = suffixIs(r2.name_, '/');
167 return r1.name_ < r2.name_;
176 // *** FileDialog::Private class implementation
179 FD_filedialog * FileDialog::Private::file_dlg_form_ = 0;
180 FileDialog::Private * FileDialog::Private::current_dlg_ = 0;
181 int FileDialog::Private::minw_ = 0;
182 int FileDialog::Private::minh_ = 0;
185 // Reread: updates dialog list to match class directory
186 void FileDialog::Private::Reread()
189 DIR * dir = ::opendir(directory_.c_str());
191 Alert::err_alert(_("Warning! Couldn't open directory."),
193 directory_ = lyx::getcwd();
194 dir = ::opendir(directory_.c_str());
197 // Clear the present namelist
198 dir_entries_.clear();
201 fl_hide_object(file_dlg_form_->List);
202 fl_clear_browser(file_dlg_form_->List);
203 fl_set_input(file_dlg_form_->DirBox, directory_.c_str());
205 // Splits complete directory name into directories and compute depth
209 string File = directory_;
211 File = split(File, Temp, '/');
213 while (!File.empty() || !Temp.empty()) {
214 string dline = "@b" + line + Temp + '/';
215 fl_add_browser_line(file_dlg_form_->List, dline.c_str());
216 File = split(File, Temp, '/');
221 // Parses all entries of the given subdirectory
222 time_t curTime = time(0);
224 while (dirent * entry = readdir(dir)) {
225 bool isLink = false, isDir = false;
227 // If the pattern doesn't start with a dot, skip hidden files
228 if (!mask_.empty() && mask_[0] != '.' &&
229 entry->d_name[0] == '.')
233 string fname = entry->d_name;
235 // Under all circumstances, "." and ".." are not wanted
236 if (fname == "." || fname == "..")
240 File = AddName(directory_, fname);
242 FileInfo fileInfo(File, true);
244 // can this really happen?
245 if (!fileInfo.isOK())
248 mode = fileInfo.modeString();
249 unsigned int const nlink = fileInfo.getNumberOfLinks();
250 string const user = lyxUserCache.find(fileInfo.getUid());
251 string const group = lyxGroupCache.find(fileInfo.getGid());
253 time_t modtime = fileInfo.getModificationTime();
254 string Time = ctime(&modtime);
256 if (curTime > modtime + SIX_MONTH_SEC
257 || curTime < modtime + ONE_HOUR_SEC) {
258 // The file is fairly old or in the future. POSIX says
259 // the cutoff is 6 months old. Allow a 1 hour slop
260 // factor for what is considered "the future", to
261 // allow for NFS server/client clock disagreement.
262 // Show the year instead of the time of day.
264 Time.erase(15, string::npos);
266 Time.erase(16, string::npos);
269 string buffer = mode + ' ' +
273 Time.substr(4, string::npos) + ' ';
275 buffer += entry->d_name;
276 buffer += fileInfo.typeIndicator();
278 isLink = fileInfo.isLink();
282 if (LyXReadLink(File, Link)) {
286 // This gives the FileType of the file that
287 // is really pointed too after resolving all
288 // symlinks. This is not necessarily the same
289 // as the type of Link (which could again be a
290 // link). Is that intended?
292 fileInfo.newFile(File);
294 buffer += fileInfo.typeIndicator();
300 // filters files according to pattern and type
301 if (fileInfo.isRegular()
303 || fileInfo.isBlock()
304 || fileInfo.isFifo()) {
305 if (!regexMatch(fname, mask_))
307 } else if (!(isDir = fileInfo.isDir()))
312 // Note ls_entry_ is an string!
313 tmp.ls_entry_ = buffer;
320 // creates displayed name
321 temp = entry->d_name;
325 temp += fileInfo.typeIndicator();
326 tmp.displayed_ = temp;
328 dir_entries_.push_back(tmp);
334 sort(dir_entries_.begin(), dir_entries_.end(), comp_direntry());
336 // Add them to directory box
337 for (DirEntries::const_iterator cit = dir_entries_.begin();
338 cit != dir_entries_.end(); ++cit) {
339 string const temp = line + cit->displayed_;
340 fl_add_browser_line(file_dlg_form_->List, temp.c_str());
342 fl_set_browser_topline(file_dlg_form_->List, depth_);
343 fl_show_object(file_dlg_form_->List);
348 // SetDirectory: sets dialog current directory
349 void FileDialog::Private::SetDirectory(string const & path)
355 tmp = MakeAbsPath(ExpandPath(path), directory_);
357 // must check the directory exists
358 DIR * dir = ::opendir(tmp.c_str());
360 Alert::err_alert(_("Warning! Couldn't open directory."), tmp);
368 // SetMask: sets dialog file mask
369 void FileDialog::Private::SetMask(string const & newmask)
372 fl_set_input(file_dlg_form_->PatBox, mask_.c_str());
376 // SetInfoLine: sets dialog information line
377 void FileDialog::Private::SetInfoLine(string const & line)
380 fl_set_object_label(file_dlg_form_->FileInfo, info_line_.c_str());
384 FileDialog::Private::Private(Dialogs & dia)
386 directory_ = MakeAbsPath(string("."));
389 // Creates form if necessary.
390 if (!file_dlg_form_) {
391 file_dlg_form_ = build_filedialog(this);
392 minw_ = file_dlg_form_->form->w;
393 minh_ = file_dlg_form_->form->h;
394 // Set callbacks. This means that we don't need a patch file
395 fl_set_object_callback(file_dlg_form_->DirBox,
396 C_LyXFileDlg_FileDlgCB, 0);
397 fl_set_object_callback(file_dlg_form_->PatBox,
398 C_LyXFileDlg_FileDlgCB, 1);
399 fl_set_object_callback(file_dlg_form_->List,
400 C_LyXFileDlg_FileDlgCB, 2);
401 fl_set_object_callback(file_dlg_form_->Filename,
402 C_LyXFileDlg_FileDlgCB, 3);
403 fl_set_object_callback(file_dlg_form_->Rescan,
404 C_LyXFileDlg_FileDlgCB, 10);
405 fl_set_object_callback(file_dlg_form_->Home,
406 C_LyXFileDlg_FileDlgCB, 11);
407 fl_set_object_callback(file_dlg_form_->User1,
408 C_LyXFileDlg_FileDlgCB, 12);
409 fl_set_object_callback(file_dlg_form_->User2,
410 C_LyXFileDlg_FileDlgCB, 13);
412 // Make sure pressing the close box doesn't crash LyX. (RvdK)
413 fl_set_form_atclose(file_dlg_form_->form,
414 C_LyXFileDlg_CancelCB, 0);
415 // Register doubleclick callback
416 fl_set_browser_dblclick_callback(file_dlg_form_->List,
417 C_LyXFileDlg_DoubleClickCB,
420 fl_hide_object(file_dlg_form_->User1);
421 fl_hide_object(file_dlg_form_->User2);
423 r_ = dia.redrawGUI().connect(boost::bind(&FileDialog::Private::redraw, this));
427 FileDialog::Private::~Private()
433 void FileDialog::Private::redraw()
435 if (file_dlg_form_->form && file_dlg_form_->form->visible)
436 fl_redraw_form(file_dlg_form_->form);
440 // SetButton: sets file selector user button action
441 void FileDialog::Private::SetButton(int index, string const & name,
448 ob = file_dlg_form_->User1;
450 } else if (index == 1) {
451 ob = file_dlg_form_->User2;
458 fl_set_object_label(ob, idex(name).c_str());
459 fl_set_button_shortcut(ob, scex(name).c_str(), 1);
469 // GetDirectory: gets last dialog directory
470 string const FileDialog::Private::GetDirectory() const
472 if (!directory_.empty())
479 bool x_sync_kludge(bool ret)
481 XSync(fl_get_display(), false);
486 // RunDialog: handle dialog during file selection
487 bool FileDialog::Private::RunDialog()
489 force_cancel_ = false;
494 FL_OBJECT * ob = fl_do_forms();
496 if (ob == file_dlg_form_->Ready) {
498 return x_sync_kludge(true);
500 } else if (ob == file_dlg_form_->Cancel || force_cancel_)
501 return x_sync_kludge(false);
504 return x_sync_kludge(true);
509 // XForms objects callback (static)
510 void FileDialog::Private::FileDlgCB(FL_OBJECT *, long arg)
517 case 0: // get directory
518 current_dlg_->SetDirectory(fl_get_input(file_dlg_form_->DirBox));
519 current_dlg_->Reread();
523 current_dlg_->SetMask(fl_get_input(file_dlg_form_->PatBox));
524 current_dlg_->Reread();
528 current_dlg_->HandleListHit();
532 current_dlg_->SetDirectory(fl_get_input(file_dlg_form_->DirBox));
533 current_dlg_->SetMask(fl_get_input(file_dlg_form_->PatBox));
534 current_dlg_->Reread();
538 current_dlg_->SetDirectory(GetEnvPath("HOME"));
539 current_dlg_->SetMask(fl_get_input(file_dlg_form_->PatBox));
540 current_dlg_->Reread();
543 case 12: // user button 1
544 current_dlg_->SetDirectory(current_dlg_->user_path1_);
545 current_dlg_->SetMask(fl_get_input(file_dlg_form_->PatBox));
546 current_dlg_->Reread();
549 case 13: // user button 2
550 current_dlg_->SetDirectory(current_dlg_->user_path2_);
551 current_dlg_->SetMask(fl_get_input(file_dlg_form_->PatBox));
552 current_dlg_->Reread();
559 // Handle callback from list
560 void FileDialog::Private::HandleListHit()
563 int const select_ = fl_get_browser(file_dlg_form_->List);
564 if (select_ > depth_)
565 SetInfoLine(dir_entries_[select_ - depth_ - 1].ls_entry_);
567 SetInfoLine(string());
571 // Callback for double click in list
572 void FileDialog::Private::DoubleClickCB(FL_OBJECT *, long)
574 // Simulate click on OK button
575 if (current_dlg_->HandleDoubleClick())
576 current_dlg_->Force(false);
580 // Handle double click from list
581 bool FileDialog::Private::HandleDoubleClick()
587 int const select_ = fl_get_browser(file_dlg_form_->List);
588 if (select_ > depth_) {
589 tmp = dir_entries_[select_ - depth_ - 1].name_;
590 SetInfoLine(dir_entries_[select_ - depth_ - 1].ls_entry_);
591 if (!suffixIs(tmp, '/')) {
593 fl_set_input(file_dlg_form_->Filename, tmp.c_str());
595 } else if (select_ != 0) {
596 SetInfoLine(string());
604 // builds new directory name
605 if (select_ > depth_) {
606 // Directory deeper down
607 // First, get directory with trailing /
608 Temp = fl_get_input(file_dlg_form_->DirBox);
609 if (!suffixIs(Temp, '/'))
613 // Directory higher up
615 for (int i = 0; i < select_; ++i) {
616 string piece = fl_get_browser_line(file_dlg_form_->List, i+1);
617 // The '+2' is here to count the '@b' (JMarc)
618 Temp += piece.substr(i + 2);
631 // Handle OK button call
632 bool FileDialog::Private::HandleOK()
635 string tmp = fl_get_input(file_dlg_form_->PatBox);
642 // directory was changed
643 tmp = fl_get_input(file_dlg_form_->DirBox);
644 if (tmp != directory_) {
650 // Handle return from list
651 int const select = fl_get_browser(file_dlg_form_->List);
652 if (select > depth_) {
653 string const temp = dir_entries_[select - depth_ - 1].name_;
654 if (!suffixIs(temp, '/')) {
655 // If user didn't type anything, use browser
656 string const name = fl_get_input(file_dlg_form_->Filename);
658 fl_set_input(file_dlg_form_->Filename, temp.c_str());
663 // Emulate a doubleclick
664 return HandleDoubleClick();
668 // Handle Cancel CB from WM close
669 int FileDialog::Private::CancelCB(FL_FORM *, void *)
671 // Simulate a click on the cancel button
672 current_dlg_->Force(true);
677 // Simulates a click on OK/Cancel
678 void FileDialog::Private::Force(bool cancel)
681 force_cancel_ = true;
682 fl_set_button(file_dlg_form_->Cancel, 1);
685 fl_set_button(file_dlg_form_->Ready, 1);
687 // Start timer to break fl_do_forms loop soon
688 fl_set_timer(file_dlg_form_->timer, 0.1);
692 // Select: launches dialog and returns selected file
693 string const FileDialog::Private::Select(string const & title,
696 string const & suggested)
698 // handles new mask and path
711 // highlight the suggested file in the browser, if it exists.
713 string const filename = OnlyFilename(suggested);
714 if (!filename.empty()) {
715 for (int i = 0; i < fl_get_browser_maxline(file_dlg_form_->List); ++i) {
716 string s = fl_get_browser_line(file_dlg_form_->List, i + 1);
726 fl_select_browser_line(file_dlg_form_->List, sel);
727 int const top = max(sel - 5, 1);
728 fl_set_browser_topline(file_dlg_form_->List, top);
730 // checks whether dialog can be started
736 SetInfoLine(string());
737 setEnabled(file_dlg_form_->Filename, true);
738 fl_set_input(file_dlg_form_->Filename, suggested.c_str());
739 fl_set_button(file_dlg_form_->Cancel, 0);
740 fl_set_button(file_dlg_form_->Ready, 0);
741 fl_set_focus_object(file_dlg_form_->form, file_dlg_form_->Filename);
742 fl_deactivate_all_forms();
743 // Prevent xforms crashing if the dialog gets too small by preventing
744 // it from being shrunk beyond a minimum size.
745 // calls to fl_set_form_minsize/maxsize apply only to the next
746 // fl_show_form(), so this comes first.
747 fl_set_form_minsize(file_dlg_form_->form, minw_, minh_);
749 fl_show_form(file_dlg_form_->form,
750 FL_PLACE_MOUSE | FL_FREE_SIZE, 0,
755 fl_hide_form(file_dlg_form_->form);
756 fl_activate_all_forms();
759 // Returns filename or string() if no valid selection was made
760 if (!isOk || !fl_get_input(file_dlg_form_->Filename)[0])
763 file_name_ = fl_get_input(file_dlg_form_->Filename);
765 if (!AbsolutePath(file_name_))
766 file_name_ = AddName(fl_get_input(file_dlg_form_->DirBox), file_name_);
771 // SelectDir: launches dialog and returns selected directory
772 string const FileDialog::Private::SelectDir(string const & title,
774 string const & suggested)
780 // handle case where path does not end with "/"
781 // remerge path+suggested and check if it is a valid path
782 if (!suggested.empty()) {
783 string tmp = suggested;
784 if (!suffixIs(tmp, '/'))
786 string full_path = path;
788 // check if this is really a directory
789 DIR * dir = ::opendir(full_path.c_str());
791 SetDirectory(full_path);
801 // checks whether dialog can be started
807 SetInfoLine(string());
808 fl_set_input(file_dlg_form_->Filename, "");
809 setEnabled(file_dlg_form_->Filename, false);
810 fl_set_button(file_dlg_form_->Cancel, 0);
811 fl_set_button(file_dlg_form_->Ready, 0);
812 fl_set_focus_object(file_dlg_form_->form, file_dlg_form_->DirBox);
813 fl_deactivate_all_forms();
814 fl_show_form(file_dlg_form_->form,
815 FL_PLACE_MOUSE | FL_FREE_SIZE, 0,
820 fl_hide_form(file_dlg_form_->form);
821 fl_activate_all_forms();
824 // Returns directory or string() if no valid selection was made
828 file_name_ = fl_get_input(file_dlg_form_->DirBox);