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());
193 Alert::err_alert(_("Warning! Couldn't open directory."),
196 directory_ = lyx::getcwd();
197 dir = ::opendir(directory_.c_str());
200 // Clear the present namelist
201 dir_entries_.clear();
204 fl_hide_object(file_dlg_form_->List);
205 fl_clear_browser(file_dlg_form_->List);
206 fl_set_input(file_dlg_form_->DirBox, directory_.c_str());
208 // Splits complete directory name into directories and compute depth
212 string File = directory_;
214 File = split(File, Temp, '/');
216 while (!File.empty() || !Temp.empty()) {
217 string dline = "@b" + line + Temp + '/';
218 fl_add_browser_line(file_dlg_form_->List, dline.c_str());
219 File = split(File, Temp, '/');
224 // Parses all entries of the given subdirectory
225 time_t curTime = time(0);
227 while (dirent * entry = readdir(dir)) {
228 bool isLink = false, isDir = false;
230 // If the pattern doesn't start with a dot, skip hidden files
231 if (!mask_.empty() && mask_[0] != '.' &&
232 entry->d_name[0] == '.')
236 string fname = entry->d_name;
238 // Under all circumstances, "." and ".." are not wanted
239 if (fname == "." || fname == "..")
243 File = AddName(directory_, fname);
245 FileInfo fileInfo(File, true);
247 // can this really happen?
248 if (!fileInfo.isOK())
251 mode = fileInfo.modeString();
252 unsigned int const nlink = fileInfo.getNumberOfLinks();
253 string const user = lyxUserCache.find(fileInfo.getUid());
254 string const group = lyxGroupCache.find(fileInfo.getGid());
256 time_t modtime = fileInfo.getModificationTime();
257 string Time = ctime(&modtime);
259 if (curTime > modtime + SIX_MONTH_SEC
260 || curTime < modtime + ONE_HOUR_SEC) {
261 // The file is fairly old or in the future. POSIX says
262 // the cutoff is 6 months old. Allow a 1 hour slop
263 // factor for what is considered "the future", to
264 // allow for NFS server/client clock disagreement.
265 // Show the year instead of the time of day.
267 Time.erase(15, string::npos);
269 Time.erase(16, string::npos);
272 string buffer = mode + ' ' +
276 Time.substr(4, string::npos) + ' ';
278 buffer += entry->d_name;
279 buffer += fileInfo.typeIndicator();
281 isLink = fileInfo.isLink();
285 if (LyXReadLink(File, Link)) {
289 // This gives the FileType of the file that
290 // is really pointed too after resolving all
291 // symlinks. This is not necessarily the same
292 // as the type of Link (which could again be a
293 // link). Is that intended?
295 fileInfo.newFile(File);
297 buffer += fileInfo.typeIndicator();
303 // filters files according to pattern and type
304 if (fileInfo.isRegular()
306 || fileInfo.isBlock()
307 || fileInfo.isFifo()) {
308 if (!regexMatch(fname, mask_))
310 } else if (!(isDir = fileInfo.isDir()))
315 // Note ls_entry_ is an string!
316 tmp.ls_entry_ = buffer;
323 // creates displayed name
324 temp = entry->d_name;
328 temp += fileInfo.typeIndicator();
329 tmp.displayed_ = temp;
331 dir_entries_.push_back(tmp);
337 sort(dir_entries_.begin(), dir_entries_.end(), comp_direntry());
339 // Add them to directory box
340 for (DirEntries::const_iterator cit = dir_entries_.begin();
341 cit != dir_entries_.end(); ++cit) {
342 string const temp = line + cit->displayed_;
343 fl_add_browser_line(file_dlg_form_->List, temp.c_str());
345 fl_set_browser_topline(file_dlg_form_->List, depth_);
346 fl_show_object(file_dlg_form_->List);
351 // SetDirectory: sets dialog current directory
352 void FileDialog::Private::SetDirectory(string const & path)
358 tmp = MakeAbsPath(ExpandPath(path), directory_);
360 // must check the directory exists
361 DIR * dir = ::opendir(tmp.c_str());
365 Alert::err_alert(_("Warning! Couldn't open directory."), tmp);
374 // SetMask: sets dialog file mask
375 void FileDialog::Private::SetMask(string const & newmask)
378 fl_set_input(file_dlg_form_->PatBox, mask_.c_str());
382 // SetInfoLine: sets dialog information line
383 void FileDialog::Private::SetInfoLine(string const & line)
386 fl_set_object_label(file_dlg_form_->FileInfo, info_line_.c_str());
390 FileDialog::Private::Private()
392 directory_ = MakeAbsPath(string("."));
395 // Creates form if necessary.
396 if (!file_dlg_form_) {
397 file_dlg_form_ = build_filedialog(this);
398 minw_ = file_dlg_form_->form->w;
399 minh_ = file_dlg_form_->form->h;
400 // Set callbacks. This means that we don't need a patch file
401 fl_set_object_callback(file_dlg_form_->DirBox,
402 C_LyXFileDlg_FileDlgCB, 0);
403 fl_set_object_callback(file_dlg_form_->PatBox,
404 C_LyXFileDlg_FileDlgCB, 1);
405 fl_set_object_callback(file_dlg_form_->List,
406 C_LyXFileDlg_FileDlgCB, 2);
407 fl_set_object_callback(file_dlg_form_->Filename,
408 C_LyXFileDlg_FileDlgCB, 3);
409 fl_set_object_callback(file_dlg_form_->Rescan,
410 C_LyXFileDlg_FileDlgCB, 10);
411 fl_set_object_callback(file_dlg_form_->Home,
412 C_LyXFileDlg_FileDlgCB, 11);
413 fl_set_object_callback(file_dlg_form_->User1,
414 C_LyXFileDlg_FileDlgCB, 12);
415 fl_set_object_callback(file_dlg_form_->User2,
416 C_LyXFileDlg_FileDlgCB, 13);
418 // Make sure pressing the close box doesn't crash LyX. (RvdK)
419 fl_set_form_atclose(file_dlg_form_->form,
420 C_LyXFileDlg_CancelCB, 0);
421 // Register doubleclick callback
422 fl_set_browser_dblclick_callback(file_dlg_form_->List,
423 C_LyXFileDlg_DoubleClickCB,
426 fl_hide_object(file_dlg_form_->User1);
427 fl_hide_object(file_dlg_form_->User2);
429 r_ = Dialogs::redrawGUI().connect(boost::bind(&FileDialog::Private::redraw, this));
433 FileDialog::Private::~Private()
439 void FileDialog::Private::redraw()
441 if (file_dlg_form_->form && file_dlg_form_->form->visible)
442 fl_redraw_form(file_dlg_form_->form);
446 // SetButton: sets file selector user button action
447 void FileDialog::Private::SetButton(int index, string const & name,
454 ob = file_dlg_form_->User1;
456 } else if (index == 1) {
457 ob = file_dlg_form_->User2;
464 fl_set_object_label(ob, idex(name).c_str());
465 fl_set_button_shortcut(ob, scex(name).c_str(), 1);
475 // GetDirectory: gets last dialog directory
476 string const FileDialog::Private::GetDirectory() const
478 if (!directory_.empty())
485 bool x_sync_kludge(bool ret)
487 XSync(fl_get_display(), false);
492 // RunDialog: handle dialog during file selection
493 bool FileDialog::Private::RunDialog()
495 force_cancel_ = false;
500 FL_OBJECT * ob = fl_do_forms();
502 if (ob == file_dlg_form_->Ready) {
504 return x_sync_kludge(true);
506 } else if (ob == file_dlg_form_->Cancel || force_cancel_)
507 return x_sync_kludge(false);
510 return x_sync_kludge(true);
515 // XForms objects callback (static)
516 void FileDialog::Private::FileDlgCB(FL_OBJECT *, long arg)
523 case 0: // get directory
524 current_dlg_->SetDirectory(fl_get_input(file_dlg_form_->DirBox));
525 current_dlg_->Reread();
529 current_dlg_->SetMask(fl_get_input(file_dlg_form_->PatBox));
530 current_dlg_->Reread();
534 current_dlg_->HandleListHit();
538 current_dlg_->SetDirectory(fl_get_input(file_dlg_form_->DirBox));
539 current_dlg_->SetMask(fl_get_input(file_dlg_form_->PatBox));
540 current_dlg_->Reread();
544 current_dlg_->SetDirectory(GetEnvPath("HOME"));
545 current_dlg_->SetMask(fl_get_input(file_dlg_form_->PatBox));
546 current_dlg_->Reread();
549 case 12: // user button 1
550 current_dlg_->SetDirectory(current_dlg_->user_path1_);
551 current_dlg_->SetMask(fl_get_input(file_dlg_form_->PatBox));
552 current_dlg_->Reread();
555 case 13: // user button 2
556 current_dlg_->SetDirectory(current_dlg_->user_path2_);
557 current_dlg_->SetMask(fl_get_input(file_dlg_form_->PatBox));
558 current_dlg_->Reread();
565 // Handle callback from list
566 void FileDialog::Private::HandleListHit()
569 int const select_ = fl_get_browser(file_dlg_form_->List);
570 if (select_ > depth_)
571 SetInfoLine(dir_entries_[select_ - depth_ - 1].ls_entry_);
573 SetInfoLine(string());
577 // Callback for double click in list
578 void FileDialog::Private::DoubleClickCB(FL_OBJECT *, long)
580 // Simulate click on OK button
581 if (current_dlg_->HandleDoubleClick())
582 current_dlg_->Force(false);
586 // Handle double click from list
587 bool FileDialog::Private::HandleDoubleClick()
593 int const select_ = fl_get_browser(file_dlg_form_->List);
594 if (select_ > depth_) {
595 tmp = dir_entries_[select_ - depth_ - 1].name_;
596 SetInfoLine(dir_entries_[select_ - depth_ - 1].ls_entry_);
597 if (!suffixIs(tmp, '/')) {
599 fl_set_input(file_dlg_form_->Filename, tmp.c_str());
601 } else if (select_ != 0) {
602 SetInfoLine(string());
610 // builds new directory name
611 if (select_ > depth_) {
612 // Directory deeper down
613 // First, get directory with trailing /
614 Temp = fl_get_input(file_dlg_form_->DirBox);
615 if (!suffixIs(Temp, '/'))
619 // Directory higher up
621 for (int i = 0; i < select_; ++i) {
622 string piece = fl_get_browser_line(file_dlg_form_->List, i+1);
623 // The '+2' is here to count the '@b' (JMarc)
624 Temp += piece.substr(i + 2);
637 // Handle OK button call
638 bool FileDialog::Private::HandleOK()
641 string tmp = fl_get_input(file_dlg_form_->PatBox);
648 // directory was changed
649 tmp = fl_get_input(file_dlg_form_->DirBox);
650 if (tmp != directory_) {
656 // Handle return from list
657 int const select = fl_get_browser(file_dlg_form_->List);
658 if (select > depth_) {
659 string const temp = dir_entries_[select - depth_ - 1].name_;
660 if (!suffixIs(temp, '/')) {
661 // If user didn't type anything, use browser
662 string const name = fl_get_input(file_dlg_form_->Filename);
664 fl_set_input(file_dlg_form_->Filename, temp.c_str());
669 // Emulate a doubleclick
670 return HandleDoubleClick();
674 // Handle Cancel CB from WM close
675 int FileDialog::Private::CancelCB(FL_FORM *, void *)
677 // Simulate a click on the cancel button
678 current_dlg_->Force(true);
683 // Simulates a click on OK/Cancel
684 void FileDialog::Private::Force(bool cancel)
687 force_cancel_ = true;
688 fl_set_button(file_dlg_form_->Cancel, 1);
691 fl_set_button(file_dlg_form_->Ready, 1);
693 // Start timer to break fl_do_forms loop soon
694 fl_set_timer(file_dlg_form_->timer, 0.1);
698 // Select: launches dialog and returns selected file
699 string const FileDialog::Private::Select(string const & title,
702 string const & suggested)
704 // handles new mask and path
717 // highlight the suggested file in the browser, if it exists.
719 string const filename = OnlyFilename(suggested);
720 if (!filename.empty()) {
721 for (int i = 0; i < fl_get_browser_maxline(file_dlg_form_->List); ++i) {
722 string s = fl_get_browser_line(file_dlg_form_->List, i + 1);
732 fl_select_browser_line(file_dlg_form_->List, sel);
733 int const top = max(sel - 5, 1);
734 fl_set_browser_topline(file_dlg_form_->List, top);
736 // checks whether dialog can be started
742 SetInfoLine(string());
743 setEnabled(file_dlg_form_->Filename, true);
744 fl_set_input(file_dlg_form_->Filename, suggested.c_str());
745 fl_set_button(file_dlg_form_->Cancel, 0);
746 fl_set_button(file_dlg_form_->Ready, 0);
747 fl_set_focus_object(file_dlg_form_->form, file_dlg_form_->Filename);
748 fl_deactivate_all_forms();
749 // Prevent xforms crashing if the dialog gets too small by preventing
750 // it from being shrunk beyond a minimum size.
751 // calls to fl_set_form_minsize/maxsize apply only to the next
752 // fl_show_form(), so this comes first.
753 fl_set_form_minsize(file_dlg_form_->form, minw_, minh_);
755 fl_show_form(file_dlg_form_->form,
756 FL_PLACE_MOUSE | FL_FREE_SIZE, 0,
761 fl_hide_form(file_dlg_form_->form);
762 fl_activate_all_forms();
765 // Returns filename or string() if no valid selection was made
766 if (!isOk || !fl_get_input(file_dlg_form_->Filename)[0])
769 file_name_ = fl_get_input(file_dlg_form_->Filename);
771 if (!AbsolutePath(file_name_))
772 file_name_ = AddName(fl_get_input(file_dlg_form_->DirBox), file_name_);
777 // SelectDir: launches dialog and returns selected directory
778 string const FileDialog::Private::SelectDir(string const & title,
780 string const & suggested)
786 // handle case where path does not end with "/"
787 // remerge path+suggested and check if it is a valid path
788 if (!suggested.empty()) {
789 string tmp = suggested;
790 if (!suffixIs(tmp, '/'))
792 string full_path = path;
794 // check if this is really a directory
795 DIR * dir = ::opendir(full_path.c_str());
797 SetDirectory(full_path);
807 // checks whether dialog can be started
813 SetInfoLine(string());
814 fl_set_input(file_dlg_form_->Filename, "");
815 setEnabled(file_dlg_form_->Filename, false);
816 fl_set_button(file_dlg_form_->Cancel, 0);
817 fl_set_button(file_dlg_form_->Ready, 0);
818 fl_set_focus_object(file_dlg_form_->form, file_dlg_form_->DirBox);
819 fl_deactivate_all_forms();
820 fl_show_form(file_dlg_form_->form,
821 FL_PLACE_MOUSE | FL_FREE_SIZE, 0,
826 fl_hide_form(file_dlg_form_->form);
827 fl_activate_all_forms();
830 // Returns directory or string() if no valid selection was made
834 file_name_ = fl_get_input(file_dlg_form_->DirBox);