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"
33 #include <boost/bind.hpp>
42 # define dirent direct
44 # include <sys/ndir.h>
55 #pragma implementation
58 #include "support/filetools.h"
59 #include "FormFiledialog.h"
60 #include "forms/form_filedialog.h"
61 #include FORMS_H_LOCATION
65 // six months, in seconds
66 long const SIX_MONTH_SEC = 6L * 30L * 24L * 60L * 60L;
68 long const ONE_HOUR_SEC = 60L * 60L;
73 int C_LyXFileDlg_CancelCB(FL_FORM *fl, void *xev)
75 return FileDialog::Private::CancelCB(fl, xev);
79 void C_LyXFileDlg_DoubleClickCB(FL_OBJECT * ob, long data)
81 FileDialog::Private::DoubleClickCB(ob, data);
85 void C_LyXFileDlg_FileDlgCB(FL_OBJECT * ob, long data)
87 FileDialog::Private::FileDlgCB(ob, data);
92 // *** User cache class implementation
93 /// User cache class definition
96 /// seeks user name from group ID
97 string const & find(uid_t ID) const {
98 Users::const_iterator cit = users.find(ID);
99 if (cit == users.end()) {
107 void add(uid_t ID) const;
109 typedef map<uid_t, string> Users;
115 void UserCache::add(uid_t ID) const
117 struct passwd const * entry = getpwuid(ID);
118 users[ID] = entry ? entry->pw_name : tostr(ID);
122 /// Group cache class definition
125 /// seeks group name from group ID
126 string const & find(gid_t ID) const ;
129 void add(gid_t ID) const;
131 typedef map<gid_t, string> Groups;
133 mutable Groups groups;
137 string const & GroupCache::find(gid_t ID) const
139 Groups::const_iterator cit = groups.find(ID);
140 if (cit == groups.end()) {
148 void GroupCache::add(gid_t ID) const
150 struct group const * entry = getgrgid(ID);
151 groups[ID] = entry ? entry->gr_name : tostr(ID);
155 UserCache lyxUserCache;
156 GroupCache lyxGroupCache;
158 // compares two LyXDirEntry objects content (used for sort)
159 class comp_direntry {
161 bool operator()(DirEntry const & r1, DirEntry const & r2) const
163 bool const r1d = suffixIs(r1.name_, '/');
164 bool const r2d = suffixIs(r2.name_, '/');
169 return r1.name_ < r2.name_;
178 // *** FileDialog::Private class implementation
181 FD_filedialog * FileDialog::Private::file_dlg_form_ = 0;
182 FileDialog::Private * FileDialog::Private::current_dlg_ = 0;
185 // Reread: updates dialog list to match class directory
186 void FileDialog::Private::Reread()
189 DIR * dir = ::opendir(directory_.c_str());
191 Alert::err_alert(_("Warning! Couldn't open directory."),
193 directory_ = lyx::getcwd();
194 dir = ::opendir(directory_.c_str());
197 // Clear the present namelist
198 dir_entries_.clear();
201 fl_hide_object(file_dlg_form_->List);
202 fl_clear_browser(file_dlg_form_->List);
203 fl_set_input(file_dlg_form_->DirBox, directory_.c_str());
205 // Splits complete directory name into directories and compute depth
209 string File = directory_;
211 File = split(File, Temp, '/');
213 while (!File.empty() || !Temp.empty()) {
214 string dline = "@b" + line + Temp + '/';
215 fl_add_browser_line(file_dlg_form_->List, dline.c_str());
216 File = split(File, Temp, '/');
221 // Parses all entries of the given subdirectory
222 time_t curTime = time(0);
224 while (dirent * entry = readdir(dir)) {
225 bool isLink = false, isDir = false;
227 // If the pattern doesn't start with a dot, skip hidden files
228 if (!mask_.empty() && mask_[0] != '.' &&
229 entry->d_name[0] == '.')
233 string fname = entry->d_name;
235 // Under all circumstances, "." and ".." are not wanted
236 if (fname == "." || fname == "..")
240 File = AddName(directory_, fname);
242 FileInfo fileInfo(File, true);
244 // can this really happen?
245 if (!fileInfo.isOK())
248 mode = fileInfo.modeString();
249 unsigned int const nlink = fileInfo.getNumberOfLinks();
250 string const user = lyxUserCache.find(fileInfo.getUid());
251 string const group = lyxGroupCache.find(fileInfo.getGid());
253 time_t modtime = fileInfo.getModificationTime();
254 string Time = ctime(&modtime);
256 if (curTime > modtime + SIX_MONTH_SEC
257 || curTime < modtime + ONE_HOUR_SEC) {
258 // The file is fairly old or in the future. POSIX says
259 // the cutoff is 6 months old. Allow a 1 hour slop
260 // factor for what is considered "the future", to
261 // allow for NFS server/client clock disagreement.
262 // Show the year instead of the time of day.
264 Time.erase(15, string::npos);
266 Time.erase(16, string::npos);
269 string buffer = mode + ' ' +
273 Time.substr(4, string::npos) + ' ';
275 buffer += entry->d_name;
276 buffer += fileInfo.typeIndicator();
278 isLink = fileInfo.isLink();
282 if (LyXReadLink(File, Link)) {
286 // This gives the FileType of the file that
287 // is really pointed too after resolving all
288 // symlinks. This is not necessarily the same
289 // as the type of Link (which could again be a
290 // link). Is that intended?
292 fileInfo.newFile(File);
294 buffer += fileInfo.typeIndicator();
300 // filters files according to pattern and type
301 if (fileInfo.isRegular()
303 || fileInfo.isBlock()
304 || fileInfo.isFifo()) {
305 if (!regexMatch(fname, mask_))
307 } else if (!(isDir = fileInfo.isDir()))
312 // Note ls_entry_ is an string!
313 tmp.ls_entry_ = buffer;
320 // creates displayed name
321 temp = entry->d_name;
325 temp += fileInfo.typeIndicator();
326 tmp.displayed_ = temp;
328 dir_entries_.push_back(tmp);
334 sort(dir_entries_.begin(), dir_entries_.end(), comp_direntry());
336 // Add them to directory box
337 for (DirEntries::const_iterator cit = dir_entries_.begin();
338 cit != dir_entries_.end(); ++cit) {
339 string const temp = line + cit->displayed_;
340 fl_add_browser_line(file_dlg_form_->List, temp.c_str());
342 fl_set_browser_topline(file_dlg_form_->List, depth_);
343 fl_show_object(file_dlg_form_->List);
348 // SetDirectory: sets dialog current directory
349 void FileDialog::Private::SetDirectory(string const & path)
355 tmp = MakeAbsPath(ExpandPath(path), directory_);
357 // must check the directory exists
358 DIR * dir = ::opendir(tmp.c_str());
360 Alert::err_alert(_("Warning! Couldn't open directory."), tmp);
368 // SetMask: sets dialog file mask
369 void FileDialog::Private::SetMask(string const & newmask)
372 fl_set_input(file_dlg_form_->PatBox, mask_.c_str());
376 // SetInfoLine: sets dialog information line
377 void FileDialog::Private::SetInfoLine(string const & line)
380 fl_set_object_label(file_dlg_form_->FileInfo, info_line_.c_str());
384 FileDialog::Private::Private(Dialogs & dia)
386 directory_ = MakeAbsPath(string("."));
389 // Creates form if necessary.
390 if (!file_dlg_form_) {
391 file_dlg_form_ = build_filedialog(this);
392 // Set callbacks. This means that we don't need a patch file
393 fl_set_object_callback(file_dlg_form_->DirBox,
394 C_LyXFileDlg_FileDlgCB, 0);
395 fl_set_object_callback(file_dlg_form_->PatBox,
396 C_LyXFileDlg_FileDlgCB, 1);
397 fl_set_object_callback(file_dlg_form_->List,
398 C_LyXFileDlg_FileDlgCB, 2);
399 fl_set_object_callback(file_dlg_form_->Filename,
400 C_LyXFileDlg_FileDlgCB, 3);
401 fl_set_object_callback(file_dlg_form_->Rescan,
402 C_LyXFileDlg_FileDlgCB, 10);
403 fl_set_object_callback(file_dlg_form_->Home,
404 C_LyXFileDlg_FileDlgCB, 11);
405 fl_set_object_callback(file_dlg_form_->User1,
406 C_LyXFileDlg_FileDlgCB, 12);
407 fl_set_object_callback(file_dlg_form_->User2,
408 C_LyXFileDlg_FileDlgCB, 13);
410 // Make sure pressing the close box doesn't crash LyX. (RvdK)
411 fl_set_form_atclose(file_dlg_form_->form,
412 C_LyXFileDlg_CancelCB, 0);
413 // Register doubleclick callback
414 fl_set_browser_dblclick_callback(file_dlg_form_->List,
415 C_LyXFileDlg_DoubleClickCB,
418 fl_hide_object(file_dlg_form_->User1);
419 fl_hide_object(file_dlg_form_->User2);
421 r_ = dia.redrawGUI().connect(boost::bind(&FileDialog::Private::redraw, this));
425 FileDialog::Private::~Private()
431 void FileDialog::Private::redraw()
433 if (file_dlg_form_->form && file_dlg_form_->form->visible)
434 fl_redraw_form(file_dlg_form_->form);
438 // SetButton: sets file selector user button action
439 void FileDialog::Private::SetButton(int index, string const & name,
446 ob = file_dlg_form_->User1;
448 } else if (index == 1) {
449 ob = file_dlg_form_->User2;
456 fl_set_object_label(ob, idex(name.c_str()));
457 fl_set_button_shortcut(ob, scex(name.c_str()), 1);
467 // GetDirectory: gets last dialog directory
468 string const FileDialog::Private::GetDirectory() const
470 if (!directory_.empty())
477 bool x_sync_kludge(bool ret)
479 XSync(fl_get_display(), false);
484 // RunDialog: handle dialog during file selection
485 bool FileDialog::Private::RunDialog()
487 force_cancel_ = false;
492 FL_OBJECT * ob = fl_do_forms();
494 if (ob == file_dlg_form_->Ready) {
496 return x_sync_kludge(true);
498 } else if (ob == file_dlg_form_->Cancel || force_cancel_)
499 return x_sync_kludge(false);
502 return x_sync_kludge(true);
507 // XForms objects callback (static)
508 void FileDialog::Private::FileDlgCB(FL_OBJECT *, long arg)
515 case 0: // get directory
516 current_dlg_->SetDirectory(fl_get_input(file_dlg_form_->DirBox));
517 current_dlg_->Reread();
521 current_dlg_->SetMask(fl_get_input(file_dlg_form_->PatBox));
522 current_dlg_->Reread();
526 current_dlg_->HandleListHit();
530 current_dlg_->SetDirectory(fl_get_input(file_dlg_form_->DirBox));
531 current_dlg_->SetMask(fl_get_input(file_dlg_form_->PatBox));
532 current_dlg_->Reread();
536 current_dlg_->SetDirectory(GetEnvPath("HOME"));
537 current_dlg_->SetMask(fl_get_input(file_dlg_form_->PatBox));
538 current_dlg_->Reread();
541 case 12: // user button 1
542 current_dlg_->SetDirectory(current_dlg_->user_path1_);
543 current_dlg_->SetMask(fl_get_input(file_dlg_form_->PatBox));
544 current_dlg_->Reread();
547 case 13: // user button 2
548 current_dlg_->SetDirectory(current_dlg_->user_path2_);
549 current_dlg_->SetMask(fl_get_input(file_dlg_form_->PatBox));
550 current_dlg_->Reread();
557 // Handle callback from list
558 void FileDialog::Private::HandleListHit()
561 int const select_ = fl_get_browser(file_dlg_form_->List);
562 if (select_ > depth_)
563 SetInfoLine(dir_entries_[select_ - depth_ - 1].ls_entry_);
565 SetInfoLine(string());
569 // Callback for double click in list
570 void FileDialog::Private::DoubleClickCB(FL_OBJECT *, long)
572 // Simulate click on OK button
573 if (current_dlg_->HandleDoubleClick())
574 current_dlg_->Force(false);
578 // Handle double click from list
579 bool FileDialog::Private::HandleDoubleClick()
585 int const select_ = fl_get_browser(file_dlg_form_->List);
586 if (select_ > depth_) {
587 tmp = dir_entries_[select_ - depth_ - 1].name_;
588 SetInfoLine(dir_entries_[select_ - depth_ - 1].ls_entry_);
589 if (!suffixIs(tmp, '/')) {
591 fl_set_input(file_dlg_form_->Filename, tmp.c_str());
593 } else if (select_ != 0) {
594 SetInfoLine(string());
602 // builds new directory name
603 if (select_ > depth_) {
604 // Directory deeper down
605 // First, get directory with trailing /
606 Temp = fl_get_input(file_dlg_form_->DirBox);
607 if (!suffixIs(Temp, '/'))
611 // Directory higher up
613 for (int i = 0; i < select_; ++i) {
614 string piece = fl_get_browser_line(file_dlg_form_->List, i+1);
615 // The '+2' is here to count the '@b' (JMarc)
616 Temp += piece.substr(i + 2);
629 // Handle OK button call
630 bool FileDialog::Private::HandleOK()
633 string tmp = fl_get_input(file_dlg_form_->PatBox);
640 // directory was changed
641 tmp = fl_get_input(file_dlg_form_->DirBox);
642 if (tmp != directory_) {
648 // Handle return from list
649 int const select = fl_get_browser(file_dlg_form_->List);
650 if (select > depth_) {
651 string const temp = dir_entries_[select - depth_ - 1].name_;
652 if (!suffixIs(temp, '/')) {
653 // If user didn't type anything, use browser
654 string const name = fl_get_input(file_dlg_form_->Filename);
656 fl_set_input(file_dlg_form_->Filename, temp.c_str());
661 // Emulate a doubleclick
662 return HandleDoubleClick();
666 // Handle Cancel CB from WM close
667 int FileDialog::Private::CancelCB(FL_FORM *, void *)
669 // Simulate a click on the cancel button
670 current_dlg_->Force(true);
675 // Simulates a click on OK/Cancel
676 void FileDialog::Private::Force(bool cancel)
679 force_cancel_ = true;
680 fl_set_button(file_dlg_form_->Cancel, 1);
683 fl_set_button(file_dlg_form_->Ready, 1);
685 // Start timer to break fl_do_forms loop soon
686 fl_set_timer(file_dlg_form_->timer, 0.1);
690 // Select: launches dialog and returns selected file
691 string const FileDialog::Private::Select(string const & title,
694 string const & suggested)
696 // handles new mask and path
709 // highlight the suggested file in the browser, if it exists.
711 string const filename = OnlyFilename(suggested);
712 if (!filename.empty()) {
713 for (int i = 0; i < fl_get_browser_maxline(file_dlg_form_->List); ++i) {
714 string s = fl_get_browser_line(file_dlg_form_->List, i + 1);
724 fl_select_browser_line(file_dlg_form_->List, sel);
725 int const top = max(sel - 5, 1);
726 fl_set_browser_topline(file_dlg_form_->List, top);
728 // checks whether dialog can be started
734 SetInfoLine(string());
735 fl_set_input(file_dlg_form_->Filename, suggested.c_str());
736 fl_set_button(file_dlg_form_->Cancel, 0);
737 fl_set_button(file_dlg_form_->Ready, 0);
738 fl_set_focus_object(file_dlg_form_->form, file_dlg_form_->Filename);
739 fl_deactivate_all_forms();
740 fl_show_form(file_dlg_form_->form,
741 FL_PLACE_MOUSE | FL_FREE_SIZE, 0,
746 fl_hide_form(file_dlg_form_->form);
747 fl_activate_all_forms();
750 // Returns filename or string() if no valid selection was made
751 if (!isOk || !fl_get_input(file_dlg_form_->Filename)[0])
754 file_name_ = fl_get_input(file_dlg_form_->Filename);
756 if (!AbsolutePath(file_name_))
757 file_name_ = AddName(fl_get_input(file_dlg_form_->DirBox), file_name_);