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