2 * \file FormFiledialog.C
3 * Copyright 2001 the LyX Team
4 * Read the file COPYING
7 * \author John Levon, moz@compsoc.man.ac.uk
23 #include "frontends/Alert.h"
24 #include "support/FileInfo.h"
25 #include "support/lyxlib.h"
26 #include "support/lstrings.h"
28 #include "frontends/Dialogs.h"
29 #include "forms_gettext.h"
31 #include <boost/bind.hpp>
40 # define dirent direct
42 # include <sys/ndir.h>
53 #pragma implementation
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;
183 // Reread: updates dialog list to match class directory
184 void FileDialog::Private::Reread()
187 DIR * dir = ::opendir(directory_.c_str());
189 Alert::err_alert(_("Warning! Couldn't open directory."),
191 directory_ = lyx::getcwd();
192 dir = ::opendir(directory_.c_str());
195 // Clear the present namelist
196 dir_entries_.clear();
199 fl_hide_object(file_dlg_form_->List);
200 fl_clear_browser(file_dlg_form_->List);
201 fl_set_input(file_dlg_form_->DirBox, directory_.c_str());
203 // Splits complete directory name into directories and compute depth
207 string File = directory_;
209 File = split(File, Temp, '/');
211 while (!File.empty() || !Temp.empty()) {
212 string dline = "@b" + line + Temp + '/';
213 fl_add_browser_line(file_dlg_form_->List, dline.c_str());
214 File = split(File, Temp, '/');
219 // Parses all entries of the given subdirectory
220 time_t curTime = time(0);
222 while (dirent * entry = readdir(dir)) {
223 bool isLink = false, isDir = false;
225 // If the pattern doesn't start with a dot, skip hidden files
226 if (!mask_.empty() && mask_[0] != '.' &&
227 entry->d_name[0] == '.')
231 string fname = entry->d_name;
233 // Under all circumstances, "." and ".." are not wanted
234 if (fname == "." || fname == "..")
238 File = AddName(directory_, fname);
240 FileInfo fileInfo(File, true);
242 // can this really happen?
243 if (!fileInfo.isOK())
246 mode = fileInfo.modeString();
247 unsigned int const nlink = fileInfo.getNumberOfLinks();
248 string const user = lyxUserCache.find(fileInfo.getUid());
249 string const group = lyxGroupCache.find(fileInfo.getGid());
251 time_t modtime = fileInfo.getModificationTime();
252 string Time = ctime(&modtime);
254 if (curTime > modtime + SIX_MONTH_SEC
255 || curTime < modtime + ONE_HOUR_SEC) {
256 // The file is fairly old or in the future. POSIX says
257 // the cutoff is 6 months old. Allow a 1 hour slop
258 // factor for what is considered "the future", to
259 // allow for NFS server/client clock disagreement.
260 // Show the year instead of the time of day.
262 Time.erase(15, string::npos);
264 Time.erase(16, string::npos);
267 string buffer = mode + ' ' +
271 Time.substr(4, string::npos) + ' ';
273 buffer += entry->d_name;
274 buffer += fileInfo.typeIndicator();
276 isLink = fileInfo.isLink();
280 if (LyXReadLink(File, Link)) {
284 // This gives the FileType of the file that
285 // is really pointed too after resolving all
286 // symlinks. This is not necessarily the same
287 // as the type of Link (which could again be a
288 // link). Is that intended?
290 fileInfo.newFile(File);
292 buffer += fileInfo.typeIndicator();
298 // filters files according to pattern and type
299 if (fileInfo.isRegular()
301 || fileInfo.isBlock()
302 || fileInfo.isFifo()) {
303 if (!regexMatch(fname, mask_))
305 } else if (!(isDir = fileInfo.isDir()))
310 // Note ls_entry_ is an string!
311 tmp.ls_entry_ = buffer;
318 // creates displayed name
319 temp = entry->d_name;
323 temp += fileInfo.typeIndicator();
324 tmp.displayed_ = temp;
326 dir_entries_.push_back(tmp);
332 sort(dir_entries_.begin(), dir_entries_.end(), comp_direntry());
334 // Add them to directory box
335 for (DirEntries::const_iterator cit = dir_entries_.begin();
336 cit != dir_entries_.end(); ++cit) {
337 string const temp = line + cit->displayed_;
338 fl_add_browser_line(file_dlg_form_->List, temp.c_str());
340 fl_set_browser_topline(file_dlg_form_->List, depth_);
341 fl_show_object(file_dlg_form_->List);
346 // SetDirectory: sets dialog current directory
347 void FileDialog::Private::SetDirectory(string const & path)
353 tmp = MakeAbsPath(ExpandPath(path), directory_);
355 // must check the directory exists
356 DIR * dir = ::opendir(tmp.c_str());
358 Alert::err_alert(_("Warning! Couldn't open directory."), tmp);
366 // SetMask: sets dialog file mask
367 void FileDialog::Private::SetMask(string const & newmask)
370 fl_set_input(file_dlg_form_->PatBox, mask_.c_str());
374 // SetInfoLine: sets dialog information line
375 void FileDialog::Private::SetInfoLine(string const & line)
378 fl_set_object_label(file_dlg_form_->FileInfo, info_line_.c_str());
382 FileDialog::Private::Private(Dialogs & dia)
384 directory_ = MakeAbsPath(string("."));
387 // Creates form if necessary.
388 if (!file_dlg_form_) {
389 file_dlg_form_ = build_filedialog(this);
390 // Set callbacks. This means that we don't need a patch file
391 fl_set_object_callback(file_dlg_form_->DirBox,
392 C_LyXFileDlg_FileDlgCB, 0);
393 fl_set_object_callback(file_dlg_form_->PatBox,
394 C_LyXFileDlg_FileDlgCB, 1);
395 fl_set_object_callback(file_dlg_form_->List,
396 C_LyXFileDlg_FileDlgCB, 2);
397 fl_set_object_callback(file_dlg_form_->Filename,
398 C_LyXFileDlg_FileDlgCB, 3);
399 fl_set_object_callback(file_dlg_form_->Rescan,
400 C_LyXFileDlg_FileDlgCB, 10);
401 fl_set_object_callback(file_dlg_form_->Home,
402 C_LyXFileDlg_FileDlgCB, 11);
403 fl_set_object_callback(file_dlg_form_->User1,
404 C_LyXFileDlg_FileDlgCB, 12);
405 fl_set_object_callback(file_dlg_form_->User2,
406 C_LyXFileDlg_FileDlgCB, 13);
408 // Make sure pressing the close box doesn't crash LyX. (RvdK)
409 fl_set_form_atclose(file_dlg_form_->form,
410 C_LyXFileDlg_CancelCB, 0);
411 // Register doubleclick callback
412 fl_set_browser_dblclick_callback(file_dlg_form_->List,
413 C_LyXFileDlg_DoubleClickCB,
416 fl_hide_object(file_dlg_form_->User1);
417 fl_hide_object(file_dlg_form_->User2);
419 r_ = dia.redrawGUI.connect(boost::bind(&FileDialog::Private::redraw, this));
423 FileDialog::Private::~Private()
429 void FileDialog::Private::redraw()
431 if (file_dlg_form_->form && file_dlg_form_->form->visible)
432 fl_redraw_form(file_dlg_form_->form);
436 // SetButton: sets file selector user button action
437 void FileDialog::Private::SetButton(int index, string const & name,
444 ob = file_dlg_form_->User1;
446 } else if (index == 1) {
447 ob = file_dlg_form_->User2;
454 fl_set_object_label(ob, idex(name.c_str()));
455 fl_set_button_shortcut(ob, scex(name.c_str()), 1);
465 // GetDirectory: gets last dialog directory
466 string const FileDialog::Private::GetDirectory() const
468 if (!directory_.empty())
475 bool x_sync_kludge(bool ret)
477 XSync(fl_get_display(), false);
482 // RunDialog: handle dialog during file selection
483 bool FileDialog::Private::RunDialog()
485 force_cancel_ = false;
490 FL_OBJECT * ob = fl_do_forms();
492 if (ob == file_dlg_form_->Ready) {
494 return x_sync_kludge(true);
496 } else if (ob == file_dlg_form_->Cancel || force_cancel_)
497 return x_sync_kludge(false);
500 return x_sync_kludge(true);
505 // XForms objects callback (static)
506 void FileDialog::Private::FileDlgCB(FL_OBJECT *, long arg)
513 case 0: // get directory
514 current_dlg_->SetDirectory(fl_get_input(file_dlg_form_->DirBox));
515 current_dlg_->Reread();
519 current_dlg_->SetMask(fl_get_input(file_dlg_form_->PatBox));
520 current_dlg_->Reread();
524 current_dlg_->HandleListHit();
528 current_dlg_->SetDirectory(fl_get_input(file_dlg_form_->DirBox));
529 current_dlg_->SetMask(fl_get_input(file_dlg_form_->PatBox));
530 current_dlg_->Reread();
534 current_dlg_->SetDirectory(GetEnvPath("HOME"));
535 current_dlg_->SetMask(fl_get_input(file_dlg_form_->PatBox));
536 current_dlg_->Reread();
539 case 12: // user button 1
540 current_dlg_->SetDirectory(current_dlg_->user_path1_);
541 current_dlg_->SetMask(fl_get_input(file_dlg_form_->PatBox));
542 current_dlg_->Reread();
545 case 13: // user button 2
546 current_dlg_->SetDirectory(current_dlg_->user_path2_);
547 current_dlg_->SetMask(fl_get_input(file_dlg_form_->PatBox));
548 current_dlg_->Reread();
555 // Handle callback from list
556 void FileDialog::Private::HandleListHit()
559 int const select_ = fl_get_browser(file_dlg_form_->List);
560 if (select_ > depth_)
561 SetInfoLine(dir_entries_[select_ - depth_ - 1].ls_entry_);
563 SetInfoLine(string());
567 // Callback for double click in list
568 void FileDialog::Private::DoubleClickCB(FL_OBJECT *, long)
570 // Simulate click on OK button
571 if (current_dlg_->HandleDoubleClick())
572 current_dlg_->Force(false);
576 // Handle double click from list
577 bool FileDialog::Private::HandleDoubleClick()
583 int const select_ = fl_get_browser(file_dlg_form_->List);
584 if (select_ > depth_) {
585 tmp = dir_entries_[select_ - depth_ - 1].name_;
586 SetInfoLine(dir_entries_[select_ - depth_ - 1].ls_entry_);
587 if (!suffixIs(tmp, '/')) {
589 fl_set_input(file_dlg_form_->Filename, tmp.c_str());
591 } else if (select_ != 0) {
592 SetInfoLine(string());
600 // builds new directory name
601 if (select_ > depth_) {
602 // Directory deeper down
603 // First, get directory with trailing /
604 Temp = fl_get_input(file_dlg_form_->DirBox);
605 if (!suffixIs(Temp, '/'))
609 // Directory higher up
611 for (int i = 0; i < select_; ++i) {
612 string piece = fl_get_browser_line(file_dlg_form_->List, i+1);
613 // The '+2' is here to count the '@b' (JMarc)
614 Temp += piece.substr(i + 2);
627 // Handle OK button call
628 bool FileDialog::Private::HandleOK()
631 string tmp = fl_get_input(file_dlg_form_->PatBox);
638 // directory was changed
639 tmp = fl_get_input(file_dlg_form_->DirBox);
640 if (tmp != directory_) {
646 // Handle return from list
647 int const select = fl_get_browser(file_dlg_form_->List);
648 if (select > depth_) {
649 string const temp = dir_entries_[select - depth_ - 1].name_;
650 if (!suffixIs(temp, '/')) {
651 // If user didn't type anything, use browser
652 string const name = fl_get_input(file_dlg_form_->Filename);
654 fl_set_input(file_dlg_form_->Filename, temp.c_str());
659 // Emulate a doubleclick
660 return HandleDoubleClick();
664 // Handle Cancel CB from WM close
665 int FileDialog::Private::CancelCB(FL_FORM *, void *)
667 // Simulate a click on the cancel button
668 current_dlg_->Force(true);
673 // Simulates a click on OK/Cancel
674 void FileDialog::Private::Force(bool cancel)
677 force_cancel_ = true;
678 fl_set_button(file_dlg_form_->Cancel, 1);
681 fl_set_button(file_dlg_form_->Ready, 1);
683 // Start timer to break fl_do_forms loop soon
684 fl_set_timer(file_dlg_form_->timer, 0.1);
688 // Select: launches dialog and returns selected file
689 string const FileDialog::Private::Select(string const & title,
692 string const & suggested)
694 // handles new mask and path
707 // highlight the suggested file in the browser, if it exists.
709 string const filename = OnlyFilename(suggested);
710 if (!filename.empty()) {
711 for (int i = 0; i < fl_get_browser_maxline(file_dlg_form_->List); ++i) {
712 string s = fl_get_browser_line(file_dlg_form_->List, i + 1);
722 fl_select_browser_line(file_dlg_form_->List, sel);
723 int const top = max(sel - 5, 1);
724 fl_set_browser_topline(file_dlg_form_->List, top);
726 // checks whether dialog can be started
732 SetInfoLine(string());
733 fl_set_input(file_dlg_form_->Filename, suggested.c_str());
734 fl_set_button(file_dlg_form_->Cancel, 0);
735 fl_set_button(file_dlg_form_->Ready, 0);
736 fl_set_focus_object(file_dlg_form_->form, file_dlg_form_->Filename);
737 fl_deactivate_all_forms();
738 fl_show_form(file_dlg_form_->form,
739 FL_PLACE_MOUSE | FL_FREE_SIZE, 0,
744 fl_hide_form(file_dlg_form_->form);
745 fl_activate_all_forms();
748 // Returns filename or string() if no valid selection was made
749 if (!isOk || !fl_get_input(file_dlg_form_->Filename)[0])
752 file_name_ = fl_get_input(file_dlg_form_->Filename);
754 if (!AbsolutePath(file_name_))
755 file_name_ = AddName(fl_get_input(file_dlg_form_->DirBox), file_name_);