]> git.lyx.org Git - lyx.git/blob - src/Format.h
Allow to hide formats from menus
[lyx.git] / src / Format.h
1 // -*- C++ -*-
2 /**
3  * \file Format.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Dekel Tsur
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #ifndef FORMAT_H
13 #define FORMAT_H
14
15 #include "support/docstring.h"
16 #include "support/trivstring.h"
17
18 #include "OutputParams.h"
19
20 #include <vector>
21
22 namespace lyx {
23
24 namespace support { class FileName; }
25
26 class Buffer;
27
28 class Format {
29 public:
30         /// Flags for some format properties
31         enum Flags {
32                 none = 0,
33                 /// Set if this format is a document format (as opposed to
34                 /// e.g. image formats).
35                 /// Some formats are both (e.g. pdf), they have this flag set.
36                 document = 1,
37                 /// Set if this format can contain vector graphics.
38                 vector = 2,
39                 /// This format should  appear in the File > Export menu
40                 export_menu = 4,
41                 /// This may be a compressed file but doesn't need decompression
42                 zipped_native = 8,
43                 /// This format should not appear in a menu at all
44                 no_menu = 16
45         };
46         ///
47         Format(std::string const & n, std::string const & e, docstring const & p,
48                std::string const & s, std::string const & v, std::string const & ed,
49                std::string const & m, int);
50         ///
51         bool dummy() const;
52         /// Is \p ext a valid filename extension for this format?
53         bool hasExtension(std::string const & ext) const;
54         /// Tell whether this format is a child format.
55         /// Child formats inherit settings like the viewer from their parent.
56         bool isChildFormat() const;
57         /// Name fo the parent format
58         std::string const parentFormat() const;
59         ///
60         std::string const name() const { return name_; }
61         ///
62         void setName(std::string const & v) { name_ = v; }
63         ///
64         std::string const extension() const
65         {
66                 return extension_list_.empty() ? empty_string() : extension_list_[0];
67         }
68         ///
69         std::string const extensions() const;
70         ///
71         void setExtensions(std::string const & v);
72         ///
73         docstring const prettyname() const { return prettyname_; }
74         ///
75         void setPrettyname(docstring const & v) { prettyname_ = v; }
76         ///
77         std::string const shortcut() const { return shortcut_; }
78         ///
79         void setShortcut(std::string const & v) { shortcut_ = v; }
80         ///
81         std::string const viewer() const { return viewer_; }
82         ///
83         void setViewer(std::string const & v) { viewer_ = v; }
84         ///
85         std::string const editor() const { return editor_; }
86         ///
87         void setEditor(std::string const & v) { editor_ = v; }
88         ///
89         std::string const mime() const { return mime_; }
90         ///
91         void setMime(std::string const & m) { mime_ = m; }
92         ///
93         bool documentFormat() const { return flags_ & document; }
94         ///
95         bool vectorFormat() const { return flags_ & vector; }
96         ///
97         void setFlags(int v) { flags_ = v; }
98         ///
99         bool inExportMenu() const { return flags_ & export_menu; }
100         ///
101         bool noMenu() const { return flags_ & no_menu; }
102         ///
103         bool zippedNative() const { return flags_ & zipped_native; }
104         ///
105         static bool formatSorter(Format const * lhs, Format const * rhs);
106
107 private:
108         /// Internal name. Needs to be unique.
109         trivstring name_;
110         /// Filename extensions, the first one being the default
111         std::vector<std::string> extension_list_;
112         /// Name presented to the user. Needs to be unique.
113         trivdocstring prettyname_;
114         /// Keyboard shortcut for the View and Export menu.
115         trivstring shortcut_;
116         /*!
117          * Viewer for this format. Needs to be in the PATH or an absolute
118          * filename.
119          * This format cannot be viewed if \c viewer_ is empty.
120          * If it is \c auto the default viewer of the OS for this format is
121          * used.
122          */
123         trivstring viewer_;
124         /// Editor for this format. \sa viewer_.
125         trivstring editor_;
126         /*!
127          * Full MIME type, e.g. "text/x-tex".
128          * Only types listed by the shared MIME database of freedesktop.org
129          * should be added.
130          * This field may be empty, but it must be unique across all formats
131          * if it is set.
132          */
133         trivstring mime_;
134         ///
135         int flags_;
136 };
137
138
139 bool operator<(Format const & a, Format const & b);
140
141
142 ///
143 class Formats {
144 public:
145         ///
146         typedef std::vector<Format> FormatList;
147         ///
148         typedef FormatList::const_iterator const_iterator;
149         ///
150         Format const & get(FormatList::size_type i) const { return formatlist_[i]; }
151         ///
152         Format & get(FormatList::size_type i) { return formatlist_[i]; }
153         /// \returns format named \p name if it exists, otherwise 0
154         Format const * getFormat(std::string const & name) const;
155         /*!
156          * Get the format of \p filename from file contents or, if this
157          * fails, from file extension.
158          * \returns file format if it could be found, otherwise an empty
159          * string.
160          * This function is expensive.
161          */
162         std::string getFormatFromFile(support::FileName const & filename) const;
163         /// Finds a format from a file extension. Returns string() if not found.
164         std::string getFormatFromExtension(std::string const & ext) const;
165         /** Returns true if the file referenced by \p filename is zipped and
166          ** needs to be unzipped for being handled
167          ** @note For natively zipped formats, such as dia/odg, this returns false.
168          **/
169         bool isZippedFile(support::FileName const & filename) const;
170         /// check for zipped file format
171         static bool isZippedFileFormat(std::string const & format);
172         /// check for PostScript file format
173         static bool isPostScriptFileFormat(std::string const & format);
174         /// Set editor and/or viewer to "auto" for formats that can be
175         /// opened by the OS.
176         void setAutoOpen();
177         ///
178         int getNumber(std::string const & name) const;
179         ///
180         void add(std::string const & name);
181         ///
182         void add(std::string const & name, std::string const & extensions,
183                  docstring const & prettyname, std::string const & shortcut,
184                  std::string const & viewer, std::string const & editor,
185                  std::string const & mime, int flags);
186         ///
187         void erase(std::string const & name);
188         ///
189         void sort();
190         ///
191         void setViewer(std::string const & name, std::string const & command);
192         ///
193         void setEditor(std::string const & name, std::string const & command);
194         /// View the given file. Buffer used for DVI's paper orientation.
195         bool view(Buffer const & buffer, support::FileName const & filename,
196                   std::string const & format_name) const;
197         ///
198         bool edit(Buffer const & buffer, support::FileName const & filename,
199                   std::string const & format_name) const;
200         ///
201         docstring const prettyName(std::string const & name) const;
202         ///
203         std::string const extension(std::string const & name) const;
204         ///
205         std::string const extensions(std::string const & name) const;
206         ///
207         const_iterator begin() const { return formatlist_.begin(); }
208         ///
209         const_iterator end() const { return formatlist_.end(); }
210         ///
211         bool empty() const { return formatlist_.empty(); }
212         ///
213         FormatList::size_type size() const { return formatlist_.size(); }
214 private:
215         ///
216         FormatList formatlist_;
217 };
218
219 ///
220 std::string flavor2format(OutputParams::FLAVOR flavor);
221 // Not currently used.
222 // OutputParams::FLAVOR format2flavor(std::string fmt);
223
224 /// The global instance.
225 /// Implementation is in LyX.cpp.
226 extern Formats & theFormats();
227
228 /// The global copy after reading lyxrc.defaults.
229 /// Implementation is in LyX.cpp.
230 extern Formats & theSystemFormats();
231
232 } // namespace lyx
233
234 #endif //FORMAT_H