]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/helper_funcs.C
7d9fdb1614a1f85dfdd159852d1e2b4845bde52c
[lyx.git] / src / frontends / controllers / helper_funcs.C
1 /**
2  * \file helper_funcs.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Jean-Marc Lasgouttes
7  * \author Angus Leeming
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12
13 #include <config.h>
14
15 #include "helper_funcs.h"
16
17 #include "gettext.h"
18
19 #include "frontends/Alert.h"
20 #include "frontends/FileDialog.h"
21
22 #include "support/filetools.h"
23 #include "support/path_defines.h"
24
25 using std::pair;
26 using std::vector;
27 using std::string;
28
29 // sorry this is just a temporary hack we should include vspace.h! (Jug)
30 extern const char * stringFromUnit(int);
31
32 namespace lyx {
33
34 using support::AddName;
35 using support::ChangeExtension;
36 using support::FileFilterList;
37 using support::GetExtension;
38 using support::LibFileSearch;
39 using support::MakeAbsPath;
40 using support::MakeRelPath;
41 using support::OnlyFilename;
42 using support::OnlyPath;
43 using support::prefixIs;
44 using support::system_lyxdir;
45 using support::user_lyxdir;
46
47 namespace frontend {
48
49
50 string const browseFile(string const & filename,
51                         string const & title,
52                         FileFilterList const & filters,
53                         bool save,
54                         pair<string,string> const & dir1,
55                         pair<string,string> const & dir2)
56 {
57         string lastPath(".");
58         if (!filename.empty())
59                 lastPath = OnlyPath(filename);
60
61         FileDialog fileDlg(title, LFUN_SELECT_FILE_SYNC, dir1, dir2);
62
63         FileDialog::Result result;
64
65         while (true) {
66                 if (save)
67                         result = fileDlg.save(lastPath, filters,
68                                 OnlyFilename(filename));
69                 else
70                         result = fileDlg.open(lastPath, filters,
71                                 OnlyFilename(filename));
72
73                 if (result.second.empty())
74                         return result.second;
75
76                 lastPath = OnlyPath(result.second);
77
78                 if (result.second.find_first_of("#~$% ") == string::npos)
79                         break;
80
81                 Alert::error(_("Invalid filename"),
82                         _("Filename can't contain any "
83                         "of these characters:\n"
84                         "space, '#', '~', '$' or '%'."));
85         }
86
87         return result.second;
88 }
89
90
91 string const browseRelFile(string const & filename,
92                            string const & refpath,
93                            string const & title,
94                            FileFilterList const & filters,
95                            bool save,
96                            pair<string,string> const & dir1,
97                            pair<string,string> const & dir2)
98 {
99         string const fname = MakeAbsPath(filename, refpath);
100
101         string const outname = browseFile(fname, title, filters, save,
102                                           dir1, dir2);
103         string const reloutname = MakeRelPath(outname, refpath);
104         if (prefixIs(reloutname, "../"))
105                 return outname;
106         else
107                 return reloutname;
108 }
109
110
111
112 string const browseLibFile(string const & dir,
113                            string const & name,
114                            string const & ext,
115                            string const & title,
116                            FileFilterList const & filters)
117 {
118         pair<string,string> const dir1(_("System files|#S#s"),
119                                        AddName(system_lyxdir(), dir));
120
121         pair<string,string> const dir2(_("User files|#U#u"),
122                                        AddName(user_lyxdir(), dir));
123
124         string const result = browseFile(LibFileSearch(dir, name, ext), title,
125                                    filters, false, dir1, dir2);
126
127         // remove the extension if it is the default one
128         string noextresult;
129         if (GetExtension(result) == ext)
130                 noextresult = ChangeExtension(result, string());
131         else
132                 noextresult = result;
133
134         // remove the directory, if it is the default one
135         string const file = OnlyFilename(noextresult);
136         if (LibFileSearch(dir, file, ext) == result)
137                 return file;
138         else
139                 return noextresult;
140 }
141
142
143 string const browseDir(string const & pathname,
144                         string const & title,
145                         pair<string,string> const & dir1,
146                         pair<string,string> const & dir2)
147 {
148         string lastPath(".");
149         if (!pathname.empty())
150                 lastPath = OnlyPath(pathname);
151
152         FileDialog fileDlg(title, LFUN_SELECT_FILE_SYNC, dir1, dir2);
153
154         FileDialog::Result result;
155
156         while (true) {
157                 result = fileDlg.opendir(lastPath,
158                                 OnlyFilename(pathname));
159
160                 if (result.second.empty())
161                         return result.second;
162
163                 lastPath = OnlyPath(result.second);
164
165                 if (result.second.find_first_of("#~$% ") == string::npos)
166                         break;
167
168                 Alert::error(_("Invalid filename"),
169                         _("Filename can't contain any "
170                         "of these characters:\n"
171                         "space, '#', '~', '$' or '%'."));
172         }
173
174         return result.second;
175 }
176
177
178 vector<string> const getLatexUnits()
179 {
180         vector<string> units;
181         char const * str;
182         for (int i = 0; (str = stringFromUnit(i)); ++i)
183             units.push_back(str);
184
185         return units;
186 }
187
188 } // namespace frontend
189 } // namespace lyx