2 * \file FormFiledialog.C
3 * Read the file COPYING
8 * Full author contact details are available in file CREDITS
24 #include "frontends/Alert.h"
25 #include "support/FileInfo.h"
26 #include "support/lyxlib.h"
27 #include "support/lstrings.h"
29 #include "frontends/Dialogs.h"
30 #include "forms_gettext.h"
32 #include <boost/bind.hpp>
41 # define dirent direct
43 # include <sys/ndir.h>
54 #pragma implementation
57 #include "support/filetools.h"
58 #include "FormFiledialog.h"
59 #include "forms/form_filedialog.h"
60 #include FORMS_H_LOCATION
64 // six months, in seconds
65 long const SIX_MONTH_SEC = 6L * 30L * 24L * 60L * 60L;
67 long const ONE_HOUR_SEC = 60L * 60L;
72 int C_LyXFileDlg_CancelCB(FL_FORM *fl, void *xev)
74 return FileDialog::Private::CancelCB(fl, xev);
78 void C_LyXFileDlg_DoubleClickCB(FL_OBJECT * ob, long data)
80 FileDialog::Private::DoubleClickCB(ob, data);
84 void C_LyXFileDlg_FileDlgCB(FL_OBJECT * ob, long data)
86 FileDialog::Private::FileDlgCB(ob, data);
91 // *** User cache class implementation
92 /// User cache class definition
95 /// seeks user name from group ID
96 string const & find(uid_t ID) const {
97 Users::const_iterator cit = users.find(ID);
98 if (cit == users.end()) {
106 void add(uid_t ID) const;
108 typedef map<uid_t, string> Users;
114 void UserCache::add(uid_t ID) const
116 struct passwd const * entry = getpwuid(ID);
117 users[ID] = entry ? entry->pw_name : tostr(ID);
121 /// Group cache class definition
124 /// seeks group name from group ID
125 string const & find(gid_t ID) const ;
128 void add(gid_t ID) const;
130 typedef map<gid_t, string> Groups;
132 mutable Groups groups;
136 string const & GroupCache::find(gid_t ID) const
138 Groups::const_iterator cit = groups.find(ID);
139 if (cit == groups.end()) {
147 void GroupCache::add(gid_t ID) const
149 struct group const * entry = getgrgid(ID);
150 groups[ID] = entry ? entry->gr_name : tostr(ID);
154 UserCache lyxUserCache;
155 GroupCache lyxGroupCache;
157 // compares two LyXDirEntry objects content (used for sort)
158 class comp_direntry {
160 bool operator()(DirEntry const & r1, DirEntry const & r2) const
162 bool const r1d = suffixIs(r1.name_, '/');
163 bool const r2d = suffixIs(r2.name_, '/');
168 return r1.name_ < r2.name_;
177 // *** FileDialog::Private class implementation
180 FD_filedialog * FileDialog::Private::file_dlg_form_ = 0;
181 FileDialog::Private * FileDialog::Private::current_dlg_ = 0;
184 // Reread: updates dialog list to match class directory
185 void FileDialog::Private::Reread()
188 DIR * dir = ::opendir(directory_.c_str());
190 Alert::err_alert(_("Warning! Couldn't open directory."),
192 directory_ = lyx::getcwd();
193 dir = ::opendir(directory_.c_str());
196 // Clear the present namelist
197 dir_entries_.clear();
200 fl_hide_object(file_dlg_form_->List);
201 fl_clear_browser(file_dlg_form_->List);
202 fl_set_input(file_dlg_form_->DirBox, directory_.c_str());
204 // Splits complete directory name into directories and compute depth
208 string File = directory_;
210 File = split(File, Temp, '/');
212 while (!File.empty() || !Temp.empty()) {
213 string dline = "@b" + line + Temp + '/';
214 fl_add_browser_line(file_dlg_form_->List, dline.c_str());
215 File = split(File, Temp, '/');
220 // Parses all entries of the given subdirectory
221 time_t curTime = time(0);
223 while (dirent * entry = readdir(dir)) {
224 bool isLink = false, isDir = false;
226 // If the pattern doesn't start with a dot, skip hidden files
227 if (!mask_.empty() && mask_[0] != '.' &&
228 entry->d_name[0] == '.')
232 string fname = entry->d_name;
234 // Under all circumstances, "." and ".." are not wanted
235 if (fname == "." || fname == "..")
239 File = AddName(directory_, fname);
241 FileInfo fileInfo(File, true);
243 // can this really happen?
244 if (!fileInfo.isOK())
247 mode = fileInfo.modeString();
248 unsigned int const nlink = fileInfo.getNumberOfLinks();
249 string const user = lyxUserCache.find(fileInfo.getUid());
250 string const group = lyxGroupCache.find(fileInfo.getGid());
252 time_t modtime = fileInfo.getModificationTime();
253 string Time = ctime(&modtime);
255 if (curTime > modtime + SIX_MONTH_SEC
256 || curTime < modtime + ONE_HOUR_SEC) {
257 // The file is fairly old or in the future. POSIX says
258 // the cutoff is 6 months old. Allow a 1 hour slop
259 // factor for what is considered "the future", to
260 // allow for NFS server/client clock disagreement.
261 // Show the year instead of the time of day.
263 Time.erase(15, string::npos);
265 Time.erase(16, string::npos);
268 string buffer = mode + ' ' +
272 Time.substr(4, string::npos) + ' ';
274 buffer += entry->d_name;
275 buffer += fileInfo.typeIndicator();
277 isLink = fileInfo.isLink();
281 if (LyXReadLink(File, Link)) {
285 // This gives the FileType of the file that
286 // is really pointed too after resolving all
287 // symlinks. This is not necessarily the same
288 // as the type of Link (which could again be a
289 // link). Is that intended?
291 fileInfo.newFile(File);
293 buffer += fileInfo.typeIndicator();
299 // filters files according to pattern and type
300 if (fileInfo.isRegular()
302 || fileInfo.isBlock()
303 || fileInfo.isFifo()) {
304 if (!regexMatch(fname, mask_))
306 } else if (!(isDir = fileInfo.isDir()))
311 // Note ls_entry_ is an string!
312 tmp.ls_entry_ = buffer;
319 // creates displayed name
320 temp = entry->d_name;
324 temp += fileInfo.typeIndicator();
325 tmp.displayed_ = temp;
327 dir_entries_.push_back(tmp);
333 sort(dir_entries_.begin(), dir_entries_.end(), comp_direntry());
335 // Add them to directory box
336 for (DirEntries::const_iterator cit = dir_entries_.begin();
337 cit != dir_entries_.end(); ++cit) {
338 string const temp = line + cit->displayed_;
339 fl_add_browser_line(file_dlg_form_->List, temp.c_str());
341 fl_set_browser_topline(file_dlg_form_->List, depth_);
342 fl_show_object(file_dlg_form_->List);
347 // SetDirectory: sets dialog current directory
348 void FileDialog::Private::SetDirectory(string const & path)
354 tmp = MakeAbsPath(ExpandPath(path), directory_);
356 // must check the directory exists
357 DIR * dir = ::opendir(tmp.c_str());
359 Alert::err_alert(_("Warning! Couldn't open directory."), tmp);
367 // SetMask: sets dialog file mask
368 void FileDialog::Private::SetMask(string const & newmask)
371 fl_set_input(file_dlg_form_->PatBox, mask_.c_str());
375 // SetInfoLine: sets dialog information line
376 void FileDialog::Private::SetInfoLine(string const & line)
379 fl_set_object_label(file_dlg_form_->FileInfo, info_line_.c_str());
383 FileDialog::Private::Private(Dialogs & dia)
385 directory_ = MakeAbsPath(string("."));
388 // Creates form if necessary.
389 if (!file_dlg_form_) {
390 file_dlg_form_ = build_filedialog(this);
391 // Set callbacks. This means that we don't need a patch file
392 fl_set_object_callback(file_dlg_form_->DirBox,
393 C_LyXFileDlg_FileDlgCB, 0);
394 fl_set_object_callback(file_dlg_form_->PatBox,
395 C_LyXFileDlg_FileDlgCB, 1);
396 fl_set_object_callback(file_dlg_form_->List,
397 C_LyXFileDlg_FileDlgCB, 2);
398 fl_set_object_callback(file_dlg_form_->Filename,
399 C_LyXFileDlg_FileDlgCB, 3);
400 fl_set_object_callback(file_dlg_form_->Rescan,
401 C_LyXFileDlg_FileDlgCB, 10);
402 fl_set_object_callback(file_dlg_form_->Home,
403 C_LyXFileDlg_FileDlgCB, 11);
404 fl_set_object_callback(file_dlg_form_->User1,
405 C_LyXFileDlg_FileDlgCB, 12);
406 fl_set_object_callback(file_dlg_form_->User2,
407 C_LyXFileDlg_FileDlgCB, 13);
409 // Make sure pressing the close box doesn't crash LyX. (RvdK)
410 fl_set_form_atclose(file_dlg_form_->form,
411 C_LyXFileDlg_CancelCB, 0);
412 // Register doubleclick callback
413 fl_set_browser_dblclick_callback(file_dlg_form_->List,
414 C_LyXFileDlg_DoubleClickCB,
417 fl_hide_object(file_dlg_form_->User1);
418 fl_hide_object(file_dlg_form_->User2);
420 r_ = dia.redrawGUI().connect(boost::bind(&FileDialog::Private::redraw, this));
424 FileDialog::Private::~Private()
430 void FileDialog::Private::redraw()
432 if (file_dlg_form_->form && file_dlg_form_->form->visible)
433 fl_redraw_form(file_dlg_form_->form);
437 // SetButton: sets file selector user button action
438 void FileDialog::Private::SetButton(int index, string const & name,
445 ob = file_dlg_form_->User1;
447 } else if (index == 1) {
448 ob = file_dlg_form_->User2;
455 fl_set_object_label(ob, idex(name.c_str()));
456 fl_set_button_shortcut(ob, scex(name.c_str()), 1);
466 // GetDirectory: gets last dialog directory
467 string const FileDialog::Private::GetDirectory() const
469 if (!directory_.empty())
476 bool x_sync_kludge(bool ret)
478 XSync(fl_get_display(), false);
483 // RunDialog: handle dialog during file selection
484 bool FileDialog::Private::RunDialog()
486 force_cancel_ = false;
491 FL_OBJECT * ob = fl_do_forms();
493 if (ob == file_dlg_form_->Ready) {
495 return x_sync_kludge(true);
497 } else if (ob == file_dlg_form_->Cancel || force_cancel_)
498 return x_sync_kludge(false);
501 return x_sync_kludge(true);
506 // XForms objects callback (static)
507 void FileDialog::Private::FileDlgCB(FL_OBJECT *, long arg)
514 case 0: // get directory
515 current_dlg_->SetDirectory(fl_get_input(file_dlg_form_->DirBox));
516 current_dlg_->Reread();
520 current_dlg_->SetMask(fl_get_input(file_dlg_form_->PatBox));
521 current_dlg_->Reread();
525 current_dlg_->HandleListHit();
529 current_dlg_->SetDirectory(fl_get_input(file_dlg_form_->DirBox));
530 current_dlg_->SetMask(fl_get_input(file_dlg_form_->PatBox));
531 current_dlg_->Reread();
535 current_dlg_->SetDirectory(GetEnvPath("HOME"));
536 current_dlg_->SetMask(fl_get_input(file_dlg_form_->PatBox));
537 current_dlg_->Reread();
540 case 12: // user button 1
541 current_dlg_->SetDirectory(current_dlg_->user_path1_);
542 current_dlg_->SetMask(fl_get_input(file_dlg_form_->PatBox));
543 current_dlg_->Reread();
546 case 13: // user button 2
547 current_dlg_->SetDirectory(current_dlg_->user_path2_);
548 current_dlg_->SetMask(fl_get_input(file_dlg_form_->PatBox));
549 current_dlg_->Reread();
556 // Handle callback from list
557 void FileDialog::Private::HandleListHit()
560 int const select_ = fl_get_browser(file_dlg_form_->List);
561 if (select_ > depth_)
562 SetInfoLine(dir_entries_[select_ - depth_ - 1].ls_entry_);
564 SetInfoLine(string());
568 // Callback for double click in list
569 void FileDialog::Private::DoubleClickCB(FL_OBJECT *, long)
571 // Simulate click on OK button
572 if (current_dlg_->HandleDoubleClick())
573 current_dlg_->Force(false);
577 // Handle double click from list
578 bool FileDialog::Private::HandleDoubleClick()
584 int const select_ = fl_get_browser(file_dlg_form_->List);
585 if (select_ > depth_) {
586 tmp = dir_entries_[select_ - depth_ - 1].name_;
587 SetInfoLine(dir_entries_[select_ - depth_ - 1].ls_entry_);
588 if (!suffixIs(tmp, '/')) {
590 fl_set_input(file_dlg_form_->Filename, tmp.c_str());
592 } else if (select_ != 0) {
593 SetInfoLine(string());
601 // builds new directory name
602 if (select_ > depth_) {
603 // Directory deeper down
604 // First, get directory with trailing /
605 Temp = fl_get_input(file_dlg_form_->DirBox);
606 if (!suffixIs(Temp, '/'))
610 // Directory higher up
612 for (int i = 0; i < select_; ++i) {
613 string piece = fl_get_browser_line(file_dlg_form_->List, i+1);
614 // The '+2' is here to count the '@b' (JMarc)
615 Temp += piece.substr(i + 2);
628 // Handle OK button call
629 bool FileDialog::Private::HandleOK()
632 string tmp = fl_get_input(file_dlg_form_->PatBox);
639 // directory was changed
640 tmp = fl_get_input(file_dlg_form_->DirBox);
641 if (tmp != directory_) {
647 // Handle return from list
648 int const select = fl_get_browser(file_dlg_form_->List);
649 if (select > depth_) {
650 string const temp = dir_entries_[select - depth_ - 1].name_;
651 if (!suffixIs(temp, '/')) {
652 // If user didn't type anything, use browser
653 string const name = fl_get_input(file_dlg_form_->Filename);
655 fl_set_input(file_dlg_form_->Filename, temp.c_str());
660 // Emulate a doubleclick
661 return HandleDoubleClick();
665 // Handle Cancel CB from WM close
666 int FileDialog::Private::CancelCB(FL_FORM *, void *)
668 // Simulate a click on the cancel button
669 current_dlg_->Force(true);
674 // Simulates a click on OK/Cancel
675 void FileDialog::Private::Force(bool cancel)
678 force_cancel_ = true;
679 fl_set_button(file_dlg_form_->Cancel, 1);
682 fl_set_button(file_dlg_form_->Ready, 1);
684 // Start timer to break fl_do_forms loop soon
685 fl_set_timer(file_dlg_form_->timer, 0.1);
689 // Select: launches dialog and returns selected file
690 string const FileDialog::Private::Select(string const & title,
693 string const & suggested)
695 // handles new mask and path
708 // highlight the suggested file in the browser, if it exists.
710 string const filename = OnlyFilename(suggested);
711 if (!filename.empty()) {
712 for (int i = 0; i < fl_get_browser_maxline(file_dlg_form_->List); ++i) {
713 string s = fl_get_browser_line(file_dlg_form_->List, i + 1);
723 fl_select_browser_line(file_dlg_form_->List, sel);
724 int const top = max(sel - 5, 1);
725 fl_set_browser_topline(file_dlg_form_->List, top);
727 // checks whether dialog can be started
733 SetInfoLine(string());
734 fl_set_input(file_dlg_form_->Filename, suggested.c_str());
735 fl_set_button(file_dlg_form_->Cancel, 0);
736 fl_set_button(file_dlg_form_->Ready, 0);
737 fl_set_focus_object(file_dlg_form_->form, file_dlg_form_->Filename);
738 fl_deactivate_all_forms();
739 fl_show_form(file_dlg_form_->form,
740 FL_PLACE_MOUSE | FL_FREE_SIZE, 0,
745 fl_hide_form(file_dlg_form_->form);
746 fl_activate_all_forms();
749 // Returns filename or string() if no valid selection was made
750 if (!isOk || !fl_get_input(file_dlg_form_->Filename)[0])
753 file_name_ = fl_get_input(file_dlg_form_->Filename);
755 if (!AbsolutePath(file_name_))
756 file_name_ = AddName(fl_get_input(file_dlg_form_->DirBox), file_name_);