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