2 * \file FormFiledialog.C
3 * Copyright 2001 the LyX Team
4 * Read the file COPYING
7 * \author John Levon, moz@compsoc.man.ac.uk
22 #include "frontends/Alert.h"
23 #include "support/FileInfo.h"
24 #include "support/lyxlib.h"
25 #include "support/lstrings.h"
27 #include "frontends/Dialogs.h"
28 #include "forms_gettext.h"
30 #include <boost/bind.hpp>
39 # define dirent direct
41 # include <sys/ndir.h>
52 #pragma implementation
55 #include "support/filetools.h"
56 #include "FormFiledialog.h"
57 #include "forms/form_filedialog.h"
58 #include FORMS_H_LOCATION
62 // six months, in seconds
63 long const SIX_MONTH_SEC = 6L * 30L * 24L * 60L * 60L;
65 long const ONE_HOUR_SEC = 60L * 60L;
70 int C_LyXFileDlg_CancelCB(FL_FORM *fl, void *xev)
72 return FileDialog::Private::CancelCB(fl, xev);
76 void C_LyXFileDlg_DoubleClickCB(FL_OBJECT * ob, long data)
78 FileDialog::Private::DoubleClickCB(ob, data);
82 void C_LyXFileDlg_FileDlgCB(FL_OBJECT * ob, long data)
84 FileDialog::Private::FileDlgCB(ob, data);
89 // *** User cache class implementation
90 /// User cache class definition
93 /// seeks user name from group ID
94 string const & find(uid_t ID) const {
95 Users::const_iterator cit = users.find(ID);
96 if (cit == users.end()) {
104 void add(uid_t ID) const;
106 typedef map<uid_t, string> Users;
112 void UserCache::add(uid_t ID) const
114 struct passwd const * entry = getpwuid(ID);
115 users[ID] = entry ? entry->pw_name : tostr(ID);
119 /// Group cache class definition
122 /// seeks group name from group ID
123 string const & find(gid_t ID) const ;
126 void add(gid_t ID) const;
128 typedef map<gid_t, string> Groups;
130 mutable Groups groups;
134 string const & GroupCache::find(gid_t ID) const
136 Groups::const_iterator cit = groups.find(ID);
137 if (cit == groups.end()) {
145 void GroupCache::add(gid_t ID) const
147 struct group const * entry = getgrgid(ID);
148 groups[ID] = entry ? entry->gr_name : tostr(ID);
152 UserCache lyxUserCache;
153 GroupCache lyxGroupCache;
155 // compares two LyXDirEntry objects content (used for sort)
156 class comp_direntry {
158 bool operator()(DirEntry const & r1, DirEntry const & r2) const
160 bool const r1d = suffixIs(r1.name_, '/');
161 bool const r2d = suffixIs(r2.name_, '/');
166 return r1.name_ < r2.name_;
175 // *** FileDialog::Private class implementation
178 FD_filedialog * FileDialog::Private::file_dlg_form_ = 0;
179 FileDialog::Private * FileDialog::Private::current_dlg_ = 0;
182 // Reread: updates dialog list to match class directory
183 void FileDialog::Private::Reread()
186 DIR * dir = ::opendir(directory_.c_str());
188 Alert::err_alert(_("Warning! Couldn't open directory."),
190 directory_ = lyx::getcwd();
191 dir = ::opendir(directory_.c_str());
194 // Clear the present namelist
195 dir_entries_.clear();
198 fl_hide_object(file_dlg_form_->List);
199 fl_clear_browser(file_dlg_form_->List);
200 fl_set_input(file_dlg_form_->DirBox, directory_.c_str());
202 // Splits complete directory name into directories and compute depth
206 string File = directory_;
208 File = split(File, Temp, '/');
210 while (!File.empty() || !Temp.empty()) {
211 string dline = "@b" + line + Temp + '/';
212 fl_add_browser_line(file_dlg_form_->List, dline.c_str());
213 File = split(File, Temp, '/');
218 // Parses all entries of the given subdirectory
219 time_t curTime = time(0);
221 while (dirent * entry = readdir(dir)) {
222 bool isLink = false, isDir = false;
224 // If the pattern doesn't start with a dot, skip hidden files
225 if (!mask_.empty() && mask_[0] != '.' &&
226 entry->d_name[0] == '.')
230 string fname = entry->d_name;
232 // Under all circumstances, "." and ".." are not wanted
233 if (fname == "." || fname == "..")
237 File = AddName(directory_, fname);
239 FileInfo fileInfo(File, true);
241 // can this really happen?
242 if (!fileInfo.isOK())
245 mode = fileInfo.modeString();
246 unsigned int const nlink = fileInfo.getNumberOfLinks();
247 string const user = lyxUserCache.find(fileInfo.getUid());
248 string const group = lyxGroupCache.find(fileInfo.getGid());
250 time_t modtime = fileInfo.getModificationTime();
251 string Time = ctime(&modtime);
253 if (curTime > modtime + SIX_MONTH_SEC
254 || curTime < modtime + ONE_HOUR_SEC) {
255 // The file is fairly old or in the future. POSIX says
256 // the cutoff is 6 months old. Allow a 1 hour slop
257 // factor for what is considered "the future", to
258 // allow for NFS server/client clock disagreement.
259 // Show the year instead of the time of day.
261 Time.erase(15, string::npos);
263 Time.erase(16, string::npos);
266 string buffer = mode + ' ' +
270 Time.substr(4, string::npos) + ' ';
272 buffer += entry->d_name;
273 buffer += fileInfo.typeIndicator();
275 isLink = fileInfo.isLink();
279 if (LyXReadLink(File, Link)) {
283 // This gives the FileType of the file that
284 // is really pointed too after resolving all
285 // symlinks. This is not necessarily the same
286 // as the type of Link (which could again be a
287 // link). Is that intended?
289 fileInfo.newFile(File);
291 buffer += fileInfo.typeIndicator();
297 // filters files according to pattern and type
298 if (fileInfo.isRegular()
300 || fileInfo.isBlock()
301 || fileInfo.isFifo()) {
302 if (!regexMatch(fname, mask_))
304 } else if (!(isDir = fileInfo.isDir()))
309 // Note ls_entry_ is an string!
310 tmp.ls_entry_ = buffer;
317 // creates displayed name
318 temp = entry->d_name;
322 temp += fileInfo.typeIndicator();
323 tmp.displayed_ = temp;
325 dir_entries_.push_back(tmp);
331 sort(dir_entries_.begin(), dir_entries_.end(), comp_direntry());
333 // Add them to directory box
334 for (DirEntries::const_iterator cit = dir_entries_.begin();
335 cit != dir_entries_.end(); ++cit) {
336 string const temp = line + cit->displayed_;
337 fl_add_browser_line(file_dlg_form_->List, temp.c_str());
339 fl_set_browser_topline(file_dlg_form_->List, depth_);
340 fl_show_object(file_dlg_form_->List);
345 // SetDirectory: sets dialog current directory
346 void FileDialog::Private::SetDirectory(string const & path)
352 tmp = MakeAbsPath(ExpandPath(path), directory_);
354 // must check the directory exists
355 DIR * dir = ::opendir(tmp.c_str());
357 Alert::err_alert(_("Warning! Couldn't open directory."), tmp);
365 // SetMask: sets dialog file mask
366 void FileDialog::Private::SetMask(string const & newmask)
369 fl_set_input(file_dlg_form_->PatBox, mask_.c_str());
373 // SetInfoLine: sets dialog information line
374 void FileDialog::Private::SetInfoLine(string const & line)
377 fl_set_object_label(file_dlg_form_->FileInfo, info_line_.c_str());
381 FileDialog::Private::Private(Dialogs & dia)
383 directory_ = MakeAbsPath(string("."));
386 // Creates form if necessary.
387 if (!file_dlg_form_) {
388 file_dlg_form_ = build_filedialog(this);
389 // Set callbacks. This means that we don't need a patch file
390 fl_set_object_callback(file_dlg_form_->DirBox,
391 C_LyXFileDlg_FileDlgCB, 0);
392 fl_set_object_callback(file_dlg_form_->PatBox,
393 C_LyXFileDlg_FileDlgCB, 1);
394 fl_set_object_callback(file_dlg_form_->List,
395 C_LyXFileDlg_FileDlgCB, 2);
396 fl_set_object_callback(file_dlg_form_->Filename,
397 C_LyXFileDlg_FileDlgCB, 3);
398 fl_set_object_callback(file_dlg_form_->Rescan,
399 C_LyXFileDlg_FileDlgCB, 10);
400 fl_set_object_callback(file_dlg_form_->Home,
401 C_LyXFileDlg_FileDlgCB, 11);
402 fl_set_object_callback(file_dlg_form_->User1,
403 C_LyXFileDlg_FileDlgCB, 12);
404 fl_set_object_callback(file_dlg_form_->User2,
405 C_LyXFileDlg_FileDlgCB, 13);
407 // Make sure pressing the close box doesn't crash LyX. (RvdK)
408 fl_set_form_atclose(file_dlg_form_->form,
409 C_LyXFileDlg_CancelCB, 0);
410 // Register doubleclick callback
411 fl_set_browser_dblclick_callback(file_dlg_form_->List,
412 C_LyXFileDlg_DoubleClickCB,
415 fl_hide_object(file_dlg_form_->User1);
416 fl_hide_object(file_dlg_form_->User2);
418 r_ = dia.redrawGUI.connect(boost::bind(&FileDialog::Private::redraw, this));
422 FileDialog::Private::~Private()
428 void FileDialog::Private::redraw()
430 if (file_dlg_form_->form && file_dlg_form_->form->visible)
431 fl_redraw_form(file_dlg_form_->form);
435 // SetButton: sets file selector user button action
436 void FileDialog::Private::SetButton(int index, string const & name,
443 ob = file_dlg_form_->User1;
445 } else if (index == 1) {
446 ob = file_dlg_form_->User2;
453 fl_set_object_label(ob, idex(name.c_str()));
454 fl_set_button_shortcut(ob, scex(name.c_str()), 1);
464 // GetDirectory: gets last dialog directory
465 string const FileDialog::Private::GetDirectory() const
467 if (!directory_.empty())
474 bool x_sync_kludge(bool ret)
476 XSync(fl_get_display(), false);
481 // RunDialog: handle dialog during file selection
482 bool FileDialog::Private::RunDialog()
484 force_cancel_ = false;
489 FL_OBJECT * ob = fl_do_forms();
491 if (ob == file_dlg_form_->Ready) {
493 return x_sync_kludge(true);
495 } else if (ob == file_dlg_form_->Cancel || force_cancel_)
496 return x_sync_kludge(false);
499 return x_sync_kludge(true);
504 // XForms objects callback (static)
505 void FileDialog::Private::FileDlgCB(FL_OBJECT *, long arg)
512 case 0: // get directory
513 current_dlg_->SetDirectory(fl_get_input(file_dlg_form_->DirBox));
514 current_dlg_->Reread();
518 current_dlg_->SetMask(fl_get_input(file_dlg_form_->PatBox));
519 current_dlg_->Reread();
523 current_dlg_->HandleListHit();
527 current_dlg_->SetDirectory(fl_get_input(file_dlg_form_->DirBox));
528 current_dlg_->SetMask(fl_get_input(file_dlg_form_->PatBox));
529 current_dlg_->Reread();
533 current_dlg_->SetDirectory(GetEnvPath("HOME"));
534 current_dlg_->SetMask(fl_get_input(file_dlg_form_->PatBox));
535 current_dlg_->Reread();
538 case 12: // user button 1
539 current_dlg_->SetDirectory(current_dlg_->user_path1_);
540 current_dlg_->SetMask(fl_get_input(file_dlg_form_->PatBox));
541 current_dlg_->Reread();
544 case 13: // user button 2
545 current_dlg_->SetDirectory(current_dlg_->user_path2_);
546 current_dlg_->SetMask(fl_get_input(file_dlg_form_->PatBox));
547 current_dlg_->Reread();
554 // Handle callback from list
555 void FileDialog::Private::HandleListHit()
558 int const select_ = fl_get_browser(file_dlg_form_->List);
559 if (select_ > depth_)
560 SetInfoLine(dir_entries_[select_ - depth_ - 1].ls_entry_);
562 SetInfoLine(string());
566 // Callback for double click in list
567 void FileDialog::Private::DoubleClickCB(FL_OBJECT *, long)
569 // Simulate click on OK button
570 if (current_dlg_->HandleDoubleClick())
571 current_dlg_->Force(false);
575 // Handle double click from list
576 bool FileDialog::Private::HandleDoubleClick()
582 int const select_ = fl_get_browser(file_dlg_form_->List);
583 if (select_ > depth_) {
584 tmp = dir_entries_[select_ - depth_ - 1].name_;
585 SetInfoLine(dir_entries_[select_ - depth_ - 1].ls_entry_);
586 if (!suffixIs(tmp, '/')) {
588 fl_set_input(file_dlg_form_->Filename, tmp.c_str());
590 } else if (select_ != 0) {
591 SetInfoLine(string());
599 // builds new directory name
600 if (select_ > depth_) {
601 // Directory deeper down
602 // First, get directory with trailing /
603 Temp = fl_get_input(file_dlg_form_->DirBox);
604 if (!suffixIs(Temp, '/'))
608 // Directory higher up
610 for (int i = 0; i < select_; ++i) {
611 string piece = fl_get_browser_line(file_dlg_form_->List, i+1);
612 // The '+2' is here to count the '@b' (JMarc)
613 Temp += piece.substr(i + 2);
626 // Handle OK button call
627 bool FileDialog::Private::HandleOK()
630 string tmp = fl_get_input(file_dlg_form_->PatBox);
637 // directory was changed
638 tmp = fl_get_input(file_dlg_form_->DirBox);
639 if (tmp != directory_) {
645 // Handle return from list
646 int const select = fl_get_browser(file_dlg_form_->List);
647 if (select > depth_) {
648 string const temp = dir_entries_[select - depth_ - 1].name_;
649 if (!suffixIs(temp, '/')) {
650 // If user didn't type anything, use browser
651 string const name = fl_get_input(file_dlg_form_->Filename);
653 fl_set_input(file_dlg_form_->Filename, temp.c_str());
658 // Emulate a doubleclick
659 return HandleDoubleClick();
663 // Handle Cancel CB from WM close
664 int FileDialog::Private::CancelCB(FL_FORM *, void *)
666 // Simulate a click on the cancel button
667 current_dlg_->Force(true);
672 // Simulates a click on OK/Cancel
673 void FileDialog::Private::Force(bool cancel)
676 force_cancel_ = true;
677 fl_set_button(file_dlg_form_->Cancel, 1);
680 fl_set_button(file_dlg_form_->Ready, 1);
682 // Start timer to break fl_do_forms loop soon
683 fl_set_timer(file_dlg_form_->timer, 0.1);
687 // Select: launches dialog and returns selected file
688 string const FileDialog::Private::Select(string const & title,
691 string const & suggested)
693 // handles new mask and path
706 // highlight the suggested file in the browser, if it exists.
708 string const filename = OnlyFilename(suggested);
709 if (!filename.empty()) {
710 for (int i = 0; i < fl_get_browser_maxline(file_dlg_form_->List); ++i) {
711 string s = fl_get_browser_line(file_dlg_form_->List, i + 1);
712 s = strip(frontStrip(s));
721 fl_select_browser_line(file_dlg_form_->List, sel);
722 int const top = max(sel - 5, 1);
723 fl_set_browser_topline(file_dlg_form_->List, top);
725 // checks whether dialog can be started
731 SetInfoLine(string());
732 fl_set_input(file_dlg_form_->Filename, suggested.c_str());
733 fl_set_button(file_dlg_form_->Cancel, 0);
734 fl_set_button(file_dlg_form_->Ready, 0);
735 fl_set_focus_object(file_dlg_form_->form, file_dlg_form_->Filename);
736 fl_deactivate_all_forms();
737 fl_show_form(file_dlg_form_->form,
738 FL_PLACE_MOUSE | FL_FREE_SIZE, 0,
743 fl_hide_form(file_dlg_form_->form);
744 fl_activate_all_forms();
747 // Returns filename or string() if no valid selection was made
748 if (!isOk || !fl_get_input(file_dlg_form_->Filename)[0])
751 file_name_ = fl_get_input(file_dlg_form_->Filename);
753 if (!AbsolutePath(file_name_))
754 file_name_ = AddName(fl_get_input(file_dlg_form_->DirBox), file_name_);