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"
64 using namespace lyx::support;
68 // six months, in seconds
69 long const SIX_MONTH_SEC = 6L * 30L * 24L * 60L * 60L;
71 long const ONE_HOUR_SEC = 60L * 60L;
76 int C_LyXFileDlg_CancelCB(FL_FORM *fl, void *xev)
78 return FileDialog::Private::CancelCB(fl, xev);
82 void C_LyXFileDlg_DoubleClickCB(FL_OBJECT * ob, long data)
84 FileDialog::Private::DoubleClickCB(ob, data);
88 void C_LyXFileDlg_FileDlgCB(FL_OBJECT * ob, long data)
90 FileDialog::Private::FileDlgCB(ob, data);
95 // *** User cache class implementation
96 /// User cache class definition
99 /// seeks user name from group ID
100 string const & find(uid_t ID) const {
101 Users::const_iterator cit = users.find(ID);
102 if (cit == users.end()) {
110 void add(uid_t ID) const;
112 typedef map<uid_t, string> Users;
118 void UserCache::add(uid_t ID) const
120 struct passwd const * entry = getpwuid(ID);
121 users[ID] = entry ? entry->pw_name : tostr(ID);
125 /// Group cache class definition
128 /// seeks group name from group ID
129 string const & find(gid_t ID) const ;
132 void add(gid_t ID) const;
134 typedef map<gid_t, string> Groups;
136 mutable Groups groups;
140 string const & GroupCache::find(gid_t ID) const
142 Groups::const_iterator cit = groups.find(ID);
143 if (cit == groups.end()) {
151 void GroupCache::add(gid_t ID) const
153 struct group const * entry = getgrgid(ID);
154 groups[ID] = entry ? entry->gr_name : tostr(ID);
158 UserCache lyxUserCache;
159 GroupCache lyxGroupCache;
161 // compares two LyXDirEntry objects content (used for sort)
162 class comp_direntry {
164 bool operator()(DirEntry const & r1, DirEntry const & r2) const
166 bool const r1d = suffixIs(r1.name_, '/');
167 bool const r2d = suffixIs(r2.name_, '/');
172 return r1.name_ < r2.name_;
181 // *** FileDialog::Private class implementation
184 FD_filedialog * FileDialog::Private::file_dlg_form_ = 0;
185 FileDialog::Private * FileDialog::Private::current_dlg_ = 0;
186 int FileDialog::Private::minw_ = 0;
187 int FileDialog::Private::minh_ = 0;
190 // Reread: updates dialog list to match class directory
191 void FileDialog::Private::Reread()
194 DIR * dir = ::opendir(directory_.c_str());
198 Alert::err_alert(_("Warning! Couldn't open directory."),
201 directory_ = getcwd();
202 dir = ::opendir(directory_.c_str());
205 // Clear the present namelist
206 dir_entries_.clear();
209 fl_hide_object(file_dlg_form_->List);
210 fl_clear_browser(file_dlg_form_->List);
211 fl_set_input(file_dlg_form_->DirBox, directory_.c_str());
213 // Splits complete directory name into directories and compute depth
217 string File = directory_;
219 File = split(File, Temp, '/');
221 while (!File.empty() || !Temp.empty()) {
222 string dline = "@b" + line + Temp + '/';
223 fl_add_browser_line(file_dlg_form_->List, dline.c_str());
224 File = split(File, Temp, '/');
229 // Parses all entries of the given subdirectory
230 time_t curTime = time(0);
232 while (dirent * entry = readdir(dir)) {
233 bool isLink = false, isDir = false;
235 // If the pattern doesn't start with a dot, skip hidden files
236 if (!mask_.empty() && mask_[0] != '.' &&
237 entry->d_name[0] == '.')
241 string fname = entry->d_name;
243 // Under all circumstances, "." and ".." are not wanted
244 if (fname == "." || fname == "..")
248 File = AddName(directory_, fname);
250 FileInfo fileInfo(File, true);
252 // can this really happen?
253 if (!fileInfo.isOK())
256 mode = fileInfo.modeString();
257 unsigned int const nlink = fileInfo.getNumberOfLinks();
258 string const user = lyxUserCache.find(fileInfo.getUid());
259 string const group = lyxGroupCache.find(fileInfo.getGid());
261 time_t modtime = fileInfo.getModificationTime();
262 string Time = ctime(&modtime);
264 if (curTime > modtime + SIX_MONTH_SEC
265 || curTime < modtime + ONE_HOUR_SEC) {
266 // The file is fairly old or in the future. POSIX says
267 // the cutoff is 6 months old. Allow a 1 hour slop
268 // factor for what is considered "the future", to
269 // allow for NFS server/client clock disagreement.
270 // Show the year instead of the time of day.
272 Time.erase(15, string::npos);
274 Time.erase(16, string::npos);
277 string buffer = mode + ' ' +
281 Time.substr(4, string::npos) + ' ';
283 buffer += entry->d_name;
284 buffer += fileInfo.typeIndicator();
286 isLink = fileInfo.isLink();
290 if (LyXReadLink(File, Link)) {
294 // This gives the FileType of the file that
295 // is really pointed too after resolving all
296 // symlinks. This is not necessarily the same
297 // as the type of Link (which could again be a
298 // link). Is that intended?
300 fileInfo.newFile(File);
302 buffer += fileInfo.typeIndicator();
308 // filters files according to pattern and type
309 if (fileInfo.isRegular()
311 || fileInfo.isBlock()
312 || fileInfo.isFifo()) {
313 if (!regexMatch(fname, mask_))
315 } else if (!(isDir = fileInfo.isDir()))
320 // Note ls_entry_ is an string!
321 tmp.ls_entry_ = buffer;
328 // creates displayed name
329 temp = entry->d_name;
333 temp += fileInfo.typeIndicator();
334 tmp.displayed_ = temp;
336 dir_entries_.push_back(tmp);
342 sort(dir_entries_.begin(), dir_entries_.end(), comp_direntry());
344 // Add them to directory box
345 for (DirEntries::const_iterator cit = dir_entries_.begin();
346 cit != dir_entries_.end(); ++cit) {
347 string const temp = line + cit->displayed_;
348 fl_add_browser_line(file_dlg_form_->List, temp.c_str());
350 fl_set_browser_topline(file_dlg_form_->List, depth_);
351 fl_show_object(file_dlg_form_->List);
356 // SetDirectory: sets dialog current directory
357 void FileDialog::Private::SetDirectory(string const & path)
363 tmp = MakeAbsPath(ExpandPath(path), directory_);
365 // must check the directory exists
366 DIR * dir = ::opendir(tmp.c_str());
370 Alert::err_alert(_("Warning! Couldn't open directory."), tmp);
379 // SetMask: sets dialog file mask
380 void FileDialog::Private::SetMask(string const & newmask)
383 fl_set_input(file_dlg_form_->PatBox, mask_.c_str());
387 // SetInfoLine: sets dialog information line
388 void FileDialog::Private::SetInfoLine(string const & line)
391 fl_set_object_label(file_dlg_form_->FileInfo, info_line_.c_str());
395 FileDialog::Private::Private()
397 directory_ = MakeAbsPath(string("."));
400 // Creates form if necessary.
401 if (!file_dlg_form_) {
402 file_dlg_form_ = build_filedialog(this);
403 minw_ = file_dlg_form_->form->w;
404 minh_ = file_dlg_form_->form->h;
405 // Set callbacks. This means that we don't need a patch file
406 fl_set_object_callback(file_dlg_form_->DirBox,
407 C_LyXFileDlg_FileDlgCB, 0);
408 fl_set_object_callback(file_dlg_form_->PatBox,
409 C_LyXFileDlg_FileDlgCB, 1);
410 fl_set_object_callback(file_dlg_form_->List,
411 C_LyXFileDlg_FileDlgCB, 2);
412 fl_set_object_callback(file_dlg_form_->Filename,
413 C_LyXFileDlg_FileDlgCB, 3);
414 fl_set_object_callback(file_dlg_form_->Rescan,
415 C_LyXFileDlg_FileDlgCB, 10);
416 fl_set_object_callback(file_dlg_form_->Home,
417 C_LyXFileDlg_FileDlgCB, 11);
418 fl_set_object_callback(file_dlg_form_->User1,
419 C_LyXFileDlg_FileDlgCB, 12);
420 fl_set_object_callback(file_dlg_form_->User2,
421 C_LyXFileDlg_FileDlgCB, 13);
423 // Make sure pressing the close box doesn't crash LyX. (RvdK)
424 fl_set_form_atclose(file_dlg_form_->form,
425 C_LyXFileDlg_CancelCB, 0);
426 // Register doubleclick callback
427 fl_set_browser_dblclick_callback(file_dlg_form_->List,
428 C_LyXFileDlg_DoubleClickCB,
431 fl_hide_object(file_dlg_form_->User1);
432 fl_hide_object(file_dlg_form_->User2);
434 r_ = Dialogs::redrawGUI().connect(boost::bind(&FileDialog::Private::redraw, this));
438 FileDialog::Private::~Private()
444 void FileDialog::Private::redraw()
446 if (file_dlg_form_->form && file_dlg_form_->form->visible)
447 fl_redraw_form(file_dlg_form_->form);
451 // SetButton: sets file selector user button action
452 void FileDialog::Private::SetButton(int index, string const & name,
459 ob = file_dlg_form_->User1;
461 } else if (index == 1) {
462 ob = file_dlg_form_->User2;
469 fl_set_object_label(ob, idex(name).c_str());
470 fl_set_button_shortcut(ob, scex(name).c_str(), 1);
480 // GetDirectory: gets last dialog directory
481 string const FileDialog::Private::GetDirectory() const
483 if (!directory_.empty())
490 bool x_sync_kludge(bool ret)
492 XSync(fl_get_display(), false);
497 // RunDialog: handle dialog during file selection
498 bool FileDialog::Private::RunDialog()
500 force_cancel_ = false;
505 FL_OBJECT * ob = fl_do_forms();
507 if (ob == file_dlg_form_->Ready) {
509 return x_sync_kludge(true);
511 } else if (ob == file_dlg_form_->Cancel || force_cancel_)
512 return x_sync_kludge(false);
515 return x_sync_kludge(true);
520 // XForms objects callback (static)
521 void FileDialog::Private::FileDlgCB(FL_OBJECT *, long arg)
528 case 0: // get directory
529 current_dlg_->SetDirectory(fl_get_input(file_dlg_form_->DirBox));
530 current_dlg_->Reread();
534 current_dlg_->SetMask(fl_get_input(file_dlg_form_->PatBox));
535 current_dlg_->Reread();
539 current_dlg_->HandleListHit();
543 current_dlg_->SetDirectory(fl_get_input(file_dlg_form_->DirBox));
544 current_dlg_->SetMask(fl_get_input(file_dlg_form_->PatBox));
545 current_dlg_->Reread();
549 current_dlg_->SetDirectory(GetEnvPath("HOME"));
550 current_dlg_->SetMask(fl_get_input(file_dlg_form_->PatBox));
551 current_dlg_->Reread();
554 case 12: // user button 1
555 current_dlg_->SetDirectory(current_dlg_->user_path1_);
556 current_dlg_->SetMask(fl_get_input(file_dlg_form_->PatBox));
557 current_dlg_->Reread();
560 case 13: // user button 2
561 current_dlg_->SetDirectory(current_dlg_->user_path2_);
562 current_dlg_->SetMask(fl_get_input(file_dlg_form_->PatBox));
563 current_dlg_->Reread();
570 // Handle callback from list
571 void FileDialog::Private::HandleListHit()
574 int const select_ = fl_get_browser(file_dlg_form_->List);
575 if (select_ > depth_)
576 SetInfoLine(dir_entries_[select_ - depth_ - 1].ls_entry_);
578 SetInfoLine(string());
582 // Callback for double click in list
583 void FileDialog::Private::DoubleClickCB(FL_OBJECT *, long)
585 // Simulate click on OK button
586 if (current_dlg_->HandleDoubleClick())
587 current_dlg_->Force(false);
591 // Handle double click from list
592 bool FileDialog::Private::HandleDoubleClick()
598 int const select_ = fl_get_browser(file_dlg_form_->List);
599 if (select_ > depth_) {
600 tmp = dir_entries_[select_ - depth_ - 1].name_;
601 SetInfoLine(dir_entries_[select_ - depth_ - 1].ls_entry_);
602 if (!suffixIs(tmp, '/')) {
604 fl_set_input(file_dlg_form_->Filename, tmp.c_str());
606 } else if (select_ != 0) {
607 SetInfoLine(string());
615 // builds new directory name
616 if (select_ > depth_) {
617 // Directory deeper down
618 // First, get directory with trailing /
619 Temp = fl_get_input(file_dlg_form_->DirBox);
620 if (!suffixIs(Temp, '/'))
624 // Directory higher up
626 for (int i = 0; i < select_; ++i) {
627 string piece = fl_get_browser_line(file_dlg_form_->List, i+1);
628 // The '+2' is here to count the '@b' (JMarc)
629 Temp += piece.substr(i + 2);
642 // Handle OK button call
643 bool FileDialog::Private::HandleOK()
646 string tmp = fl_get_input(file_dlg_form_->PatBox);
653 // directory was changed
654 tmp = fl_get_input(file_dlg_form_->DirBox);
655 if (tmp != directory_) {
661 // Handle return from list
662 int const select = fl_get_browser(file_dlg_form_->List);
663 if (select > depth_) {
664 string const temp = dir_entries_[select - depth_ - 1].name_;
665 if (!suffixIs(temp, '/')) {
666 // If user didn't type anything, use browser
667 string const name = fl_get_input(file_dlg_form_->Filename);
669 fl_set_input(file_dlg_form_->Filename, temp.c_str());
674 // Emulate a doubleclick
675 return HandleDoubleClick();
679 // Handle Cancel CB from WM close
680 int FileDialog::Private::CancelCB(FL_FORM *, void *)
682 // Simulate a click on the cancel button
683 current_dlg_->Force(true);
688 // Simulates a click on OK/Cancel
689 void FileDialog::Private::Force(bool cancel)
692 force_cancel_ = true;
693 fl_set_button(file_dlg_form_->Cancel, 1);
696 fl_set_button(file_dlg_form_->Ready, 1);
698 // Start timer to break fl_do_forms loop soon
699 fl_set_timer(file_dlg_form_->timer, 0.1);
703 // Select: launches dialog and returns selected file
704 string const FileDialog::Private::Select(string const & title,
707 string const & suggested)
709 // handles new mask and path
722 // highlight the suggested file in the browser, if it exists.
724 string const filename = OnlyFilename(suggested);
725 if (!filename.empty()) {
726 for (int i = 0; i < fl_get_browser_maxline(file_dlg_form_->List); ++i) {
727 string s = fl_get_browser_line(file_dlg_form_->List, i + 1);
737 fl_select_browser_line(file_dlg_form_->List, sel);
738 int const top = max(sel - 5, 1);
739 fl_set_browser_topline(file_dlg_form_->List, top);
741 // checks whether dialog can be started
747 SetInfoLine(string());
748 setEnabled(file_dlg_form_->Filename, true);
749 fl_set_input(file_dlg_form_->Filename, suggested.c_str());
750 fl_set_button(file_dlg_form_->Cancel, 0);
751 fl_set_button(file_dlg_form_->Ready, 0);
752 fl_set_focus_object(file_dlg_form_->form, file_dlg_form_->Filename);
753 fl_deactivate_all_forms();
754 // Prevent xforms crashing if the dialog gets too small by preventing
755 // it from being shrunk beyond a minimum size.
756 // calls to fl_set_form_minsize/maxsize apply only to the next
757 // fl_show_form(), so this comes first.
758 fl_set_form_minsize(file_dlg_form_->form, minw_, minh_);
760 fl_show_form(file_dlg_form_->form,
761 FL_PLACE_MOUSE | FL_FREE_SIZE, 0,
766 fl_hide_form(file_dlg_form_->form);
767 fl_activate_all_forms();
770 // Returns filename or string() if no valid selection was made
771 if (!isOk || !fl_get_input(file_dlg_form_->Filename)[0])
774 file_name_ = fl_get_input(file_dlg_form_->Filename);
776 if (!AbsolutePath(file_name_))
777 file_name_ = AddName(fl_get_input(file_dlg_form_->DirBox), file_name_);
782 // SelectDir: launches dialog and returns selected directory
783 string const FileDialog::Private::SelectDir(string const & title,
785 string const & suggested)
791 // handle case where path does not end with "/"
792 // remerge path+suggested and check if it is a valid path
793 if (!suggested.empty()) {
794 string tmp = suggested;
795 if (!suffixIs(tmp, '/'))
797 string full_path = path;
799 // check if this is really a directory
800 DIR * dir = ::opendir(full_path.c_str());
802 SetDirectory(full_path);
812 // checks whether dialog can be started
818 SetInfoLine(string());
819 fl_set_input(file_dlg_form_->Filename, "");
820 setEnabled(file_dlg_form_->Filename, false);
821 fl_set_button(file_dlg_form_->Cancel, 0);
822 fl_set_button(file_dlg_form_->Ready, 0);
823 fl_set_focus_object(file_dlg_form_->form, file_dlg_form_->DirBox);
824 fl_deactivate_all_forms();
825 fl_show_form(file_dlg_form_->form,
826 FL_PLACE_MOUSE | FL_FREE_SIZE, 0,
831 fl_hide_form(file_dlg_form_->form);
832 fl_activate_all_forms();
835 // Returns directory or string() if no valid selection was made
839 file_name_ = fl_get_input(file_dlg_form_->DirBox);