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 #pragma implementation
59 #include "support/filetools.h"
60 #include "FormFiledialog.h"
61 #include "forms/form_filedialog.h"
62 #include FORMS_H_LOCATION
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());
194 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());
363 Alert::err_alert(_("Warning! Couldn't open directory."), tmp);
371 // SetMask: sets dialog file mask
372 void FileDialog::Private::SetMask(string const & newmask)
375 fl_set_input(file_dlg_form_->PatBox, mask_.c_str());
379 // SetInfoLine: sets dialog information line
380 void FileDialog::Private::SetInfoLine(string const & line)
383 fl_set_object_label(file_dlg_form_->FileInfo, info_line_.c_str());
387 FileDialog::Private::Private(Dialogs & dia)
389 directory_ = MakeAbsPath(string("."));
392 // Creates form if necessary.
393 if (!file_dlg_form_) {
394 file_dlg_form_ = build_filedialog(this);
395 minw_ = file_dlg_form_->form->w;
396 minh_ = file_dlg_form_->form->h;
397 // Set callbacks. This means that we don't need a patch file
398 fl_set_object_callback(file_dlg_form_->DirBox,
399 C_LyXFileDlg_FileDlgCB, 0);
400 fl_set_object_callback(file_dlg_form_->PatBox,
401 C_LyXFileDlg_FileDlgCB, 1);
402 fl_set_object_callback(file_dlg_form_->List,
403 C_LyXFileDlg_FileDlgCB, 2);
404 fl_set_object_callback(file_dlg_form_->Filename,
405 C_LyXFileDlg_FileDlgCB, 3);
406 fl_set_object_callback(file_dlg_form_->Rescan,
407 C_LyXFileDlg_FileDlgCB, 10);
408 fl_set_object_callback(file_dlg_form_->Home,
409 C_LyXFileDlg_FileDlgCB, 11);
410 fl_set_object_callback(file_dlg_form_->User1,
411 C_LyXFileDlg_FileDlgCB, 12);
412 fl_set_object_callback(file_dlg_form_->User2,
413 C_LyXFileDlg_FileDlgCB, 13);
415 // Make sure pressing the close box doesn't crash LyX. (RvdK)
416 fl_set_form_atclose(file_dlg_form_->form,
417 C_LyXFileDlg_CancelCB, 0);
418 // Register doubleclick callback
419 fl_set_browser_dblclick_callback(file_dlg_form_->List,
420 C_LyXFileDlg_DoubleClickCB,
423 fl_hide_object(file_dlg_form_->User1);
424 fl_hide_object(file_dlg_form_->User2);
426 r_ = dia.redrawGUI().connect(boost::bind(&FileDialog::Private::redraw, this));
430 FileDialog::Private::~Private()
436 void FileDialog::Private::redraw()
438 if (file_dlg_form_->form && file_dlg_form_->form->visible)
439 fl_redraw_form(file_dlg_form_->form);
443 // SetButton: sets file selector user button action
444 void FileDialog::Private::SetButton(int index, string const & name,
451 ob = file_dlg_form_->User1;
453 } else if (index == 1) {
454 ob = file_dlg_form_->User2;
461 fl_set_object_label(ob, idex(name).c_str());
462 fl_set_button_shortcut(ob, scex(name).c_str(), 1);
472 // GetDirectory: gets last dialog directory
473 string const FileDialog::Private::GetDirectory() const
475 if (!directory_.empty())
482 bool x_sync_kludge(bool ret)
484 XSync(fl_get_display(), false);
489 // RunDialog: handle dialog during file selection
490 bool FileDialog::Private::RunDialog()
492 force_cancel_ = false;
497 FL_OBJECT * ob = fl_do_forms();
499 if (ob == file_dlg_form_->Ready) {
501 return x_sync_kludge(true);
503 } else if (ob == file_dlg_form_->Cancel || force_cancel_)
504 return x_sync_kludge(false);
507 return x_sync_kludge(true);
512 // XForms objects callback (static)
513 void FileDialog::Private::FileDlgCB(FL_OBJECT *, long arg)
520 case 0: // get directory
521 current_dlg_->SetDirectory(fl_get_input(file_dlg_form_->DirBox));
522 current_dlg_->Reread();
526 current_dlg_->SetMask(fl_get_input(file_dlg_form_->PatBox));
527 current_dlg_->Reread();
531 current_dlg_->HandleListHit();
535 current_dlg_->SetDirectory(fl_get_input(file_dlg_form_->DirBox));
536 current_dlg_->SetMask(fl_get_input(file_dlg_form_->PatBox));
537 current_dlg_->Reread();
541 current_dlg_->SetDirectory(GetEnvPath("HOME"));
542 current_dlg_->SetMask(fl_get_input(file_dlg_form_->PatBox));
543 current_dlg_->Reread();
546 case 12: // user button 1
547 current_dlg_->SetDirectory(current_dlg_->user_path1_);
548 current_dlg_->SetMask(fl_get_input(file_dlg_form_->PatBox));
549 current_dlg_->Reread();
552 case 13: // user button 2
553 current_dlg_->SetDirectory(current_dlg_->user_path2_);
554 current_dlg_->SetMask(fl_get_input(file_dlg_form_->PatBox));
555 current_dlg_->Reread();
562 // Handle callback from list
563 void FileDialog::Private::HandleListHit()
566 int const select_ = fl_get_browser(file_dlg_form_->List);
567 if (select_ > depth_)
568 SetInfoLine(dir_entries_[select_ - depth_ - 1].ls_entry_);
570 SetInfoLine(string());
574 // Callback for double click in list
575 void FileDialog::Private::DoubleClickCB(FL_OBJECT *, long)
577 // Simulate click on OK button
578 if (current_dlg_->HandleDoubleClick())
579 current_dlg_->Force(false);
583 // Handle double click from list
584 bool FileDialog::Private::HandleDoubleClick()
590 int const select_ = fl_get_browser(file_dlg_form_->List);
591 if (select_ > depth_) {
592 tmp = dir_entries_[select_ - depth_ - 1].name_;
593 SetInfoLine(dir_entries_[select_ - depth_ - 1].ls_entry_);
594 if (!suffixIs(tmp, '/')) {
596 fl_set_input(file_dlg_form_->Filename, tmp.c_str());
598 } else if (select_ != 0) {
599 SetInfoLine(string());
607 // builds new directory name
608 if (select_ > depth_) {
609 // Directory deeper down
610 // First, get directory with trailing /
611 Temp = fl_get_input(file_dlg_form_->DirBox);
612 if (!suffixIs(Temp, '/'))
616 // Directory higher up
618 for (int i = 0; i < select_; ++i) {
619 string piece = fl_get_browser_line(file_dlg_form_->List, i+1);
620 // The '+2' is here to count the '@b' (JMarc)
621 Temp += piece.substr(i + 2);
634 // Handle OK button call
635 bool FileDialog::Private::HandleOK()
638 string tmp = fl_get_input(file_dlg_form_->PatBox);
645 // directory was changed
646 tmp = fl_get_input(file_dlg_form_->DirBox);
647 if (tmp != directory_) {
653 // Handle return from list
654 int const select = fl_get_browser(file_dlg_form_->List);
655 if (select > depth_) {
656 string const temp = dir_entries_[select - depth_ - 1].name_;
657 if (!suffixIs(temp, '/')) {
658 // If user didn't type anything, use browser
659 string const name = fl_get_input(file_dlg_form_->Filename);
661 fl_set_input(file_dlg_form_->Filename, temp.c_str());
666 // Emulate a doubleclick
667 return HandleDoubleClick();
671 // Handle Cancel CB from WM close
672 int FileDialog::Private::CancelCB(FL_FORM *, void *)
674 // Simulate a click on the cancel button
675 current_dlg_->Force(true);
680 // Simulates a click on OK/Cancel
681 void FileDialog::Private::Force(bool cancel)
684 force_cancel_ = true;
685 fl_set_button(file_dlg_form_->Cancel, 1);
688 fl_set_button(file_dlg_form_->Ready, 1);
690 // Start timer to break fl_do_forms loop soon
691 fl_set_timer(file_dlg_form_->timer, 0.1);
695 // Select: launches dialog and returns selected file
696 string const FileDialog::Private::Select(string const & title,
699 string const & suggested)
701 // handles new mask and path
714 // highlight the suggested file in the browser, if it exists.
716 string const filename = OnlyFilename(suggested);
717 if (!filename.empty()) {
718 for (int i = 0; i < fl_get_browser_maxline(file_dlg_form_->List); ++i) {
719 string s = fl_get_browser_line(file_dlg_form_->List, i + 1);
729 fl_select_browser_line(file_dlg_form_->List, sel);
730 int const top = max(sel - 5, 1);
731 fl_set_browser_topline(file_dlg_form_->List, top);
733 // checks whether dialog can be started
739 SetInfoLine(string());
740 setEnabled(file_dlg_form_->Filename, true);
741 fl_set_input(file_dlg_form_->Filename, suggested.c_str());
742 fl_set_button(file_dlg_form_->Cancel, 0);
743 fl_set_button(file_dlg_form_->Ready, 0);
744 fl_set_focus_object(file_dlg_form_->form, file_dlg_form_->Filename);
745 fl_deactivate_all_forms();
746 // Prevent xforms crashing if the dialog gets too small by preventing
747 // it from being shrunk beyond a minimum size.
748 // calls to fl_set_form_minsize/maxsize apply only to the next
749 // fl_show_form(), so this comes first.
750 fl_set_form_minsize(file_dlg_form_->form, minw_, minh_);
752 fl_show_form(file_dlg_form_->form,
753 FL_PLACE_MOUSE | FL_FREE_SIZE, 0,
758 fl_hide_form(file_dlg_form_->form);
759 fl_activate_all_forms();
762 // Returns filename or string() if no valid selection was made
763 if (!isOk || !fl_get_input(file_dlg_form_->Filename)[0])
766 file_name_ = fl_get_input(file_dlg_form_->Filename);
768 if (!AbsolutePath(file_name_))
769 file_name_ = AddName(fl_get_input(file_dlg_form_->DirBox), file_name_);
774 // SelectDir: launches dialog and returns selected directory
775 string const FileDialog::Private::SelectDir(string const & title,
777 string const & suggested)
783 // handle case where path does not end with "/"
784 // remerge path+suggested and check if it is a valid path
785 if (!suggested.empty()) {
786 string tmp = suggested;
787 if (!suffixIs(tmp, '/'))
789 string full_path = path;
791 // check if this is really a directory
792 DIR * dir = ::opendir(full_path.c_str());
794 SetDirectory(full_path);
804 // checks whether dialog can be started
810 SetInfoLine(string());
811 fl_set_input(file_dlg_form_->Filename, "");
812 setEnabled(file_dlg_form_->Filename, false);
813 fl_set_button(file_dlg_form_->Cancel, 0);
814 fl_set_button(file_dlg_form_->Ready, 0);
815 fl_set_focus_object(file_dlg_form_->form, file_dlg_form_->DirBox);
816 fl_deactivate_all_forms();
817 fl_show_form(file_dlg_form_->form,
818 FL_PLACE_MOUSE | FL_FREE_SIZE, 0,
823 fl_hide_form(file_dlg_form_->form);
824 fl_activate_all_forms();
827 // Returns directory or string() if no valid selection was made
831 file_name_ = fl_get_input(file_dlg_form_->DirBox);