4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
9 * Full author contact details are available in file CREDITS.
15 #include "support/strfwd.h"
16 #include "support/trivstring.h"
23 namespace support { class FileName; }
27 enum class Flavor : int;
31 /// Flags for some format properties
34 /// Set if this format is a document format (as opposed to
35 /// e.g. image formats).
36 /// Some formats are both (e.g. pdf), they have this flag set.
38 /// Set if this format can contain vector graphics.
40 /// This format should appear in the File > Export menu
42 /// This may be a compressed file but doesn't need decompression
44 /// This format should not appear in a menu at all
48 Format(std::string const & n, std::string const & e, docstring const & p,
49 std::string const & s, std::string const & v, std::string const & ed,
50 std::string const & m, int);
53 /// Is \p ext a valid filename extension for this format?
54 bool hasExtension(std::string const & ext) const;
55 /// Tell whether this format is a child format.
56 /// Child formats inherit settings like the viewer from their parent.
57 bool isChildFormat() const;
58 /// Name fo the parent format
59 std::string const parentFormat() const;
61 std::string const name() const { return name_; }
63 void setName(std::string const & v) { name_ = v; }
65 std::string const extension() const
67 return extension_list_.empty() ? empty_string() : extension_list_[0];
70 std::string const extensions() const;
72 void setExtensions(std::string const & v);
74 docstring const prettyname() const { return prettyname_; }
76 void setPrettyname(docstring const & v) { prettyname_ = v; }
78 std::string const shortcut() const { return shortcut_; }
80 void setShortcut(std::string const & v) { shortcut_ = v; }
82 std::string const viewer() const { return viewer_; }
84 void setViewer(std::string const & v) { viewer_ = v; }
86 std::string const editor() const { return editor_; }
88 void setEditor(std::string const & v) { editor_ = v; }
90 std::string const mime() const { return mime_; }
92 void setMime(std::string const & m) { mime_ = m; }
94 bool documentFormat() const { return flags_ & document; }
96 bool vectorFormat() const { return flags_ & vector; }
98 void setFlags(int v) { flags_ = v; }
100 bool inExportMenu() const { return flags_ & export_menu; }
102 bool noMenu() const { return flags_ & no_menu; }
104 bool zippedNative() const { return flags_ & zipped_native; }
106 static bool formatSorter(Format const * lhs, Format const * rhs);
109 /// Internal name. Needs to be unique.
111 /// Filename extensions, the first one being the default
112 std::vector<std::string> extension_list_;
113 /// Name presented to the user. Needs to be unique.
114 trivdocstring prettyname_;
115 /// Keyboard shortcut for the View and Export menu.
116 trivstring shortcut_;
118 * Viewer for this format. Needs to be in the PATH or an absolute
120 * This format cannot be viewed if \c viewer_ is empty.
121 * If it is \c auto the default viewer of the OS for this format is
125 /// Editor for this format. \sa viewer_.
128 * Full MIME type, e.g. "text/x-tex".
129 * Only types listed by the shared MIME database of freedesktop.org
131 * This field may be empty, but it must be unique across all formats
140 bool operator<(Format const & a, Format const & b);
147 typedef std::vector<Format> FormatList;
149 typedef FormatList::const_iterator const_iterator;
151 Format const & get(FormatList::size_type i) const { return formatlist_[i]; }
153 Format & get(FormatList::size_type i) { return formatlist_[i]; }
154 /// \returns format named \p name if it exists, otherwise 0
155 Format const * getFormat(std::string const & name) const;
156 /// \returns format named \p name if it exists, otherwise 0
157 Format * getFormat(std::string const & name);
159 * Get the format of \p filename from file contents or, if this
160 * fails, from file extension.
161 * \returns file format if it could be found, otherwise an empty
163 * This function is expensive.
165 std::string getFormatFromFile(support::FileName const & filename) const;
166 /// Finds a format from a file extension. Returns string() if not found.
167 std::string getFormatFromExtension(std::string const & ext) const;
168 /** Returns true if the file referenced by \p filename is zipped and
169 ** needs to be unzipped for being handled
170 ** @note For natively zipped formats, such as dia/odg, this returns false.
172 bool isZippedFile(support::FileName const & filename) const;
173 /// check for zipped file format
174 static bool isZippedFileFormat(std::string const & format);
175 /// check for PostScript file format
176 static bool isPostScriptFileFormat(std::string const & format);
177 /// Set editor and/or viewer to "auto" for formats that can be
178 /// opened by the OS.
181 int getNumber(std::string const & name) const;
183 void add(std::string const & name);
185 void add(std::string const & name, std::string const & extensions,
186 docstring const & prettyname, std::string const & shortcut,
187 std::string const & viewer, std::string const & editor,
188 std::string const & mime, int flags);
190 void erase(std::string const & name);
194 void setViewer(std::string const & name, std::string const & command);
196 void setEditor(std::string const & name, std::string const & command);
197 /// View the given file. Buffer used for DVI's paper orientation.
198 bool view(Buffer const & buffer, support::FileName const & filename,
199 std::string const & format_name) const;
201 bool edit(Buffer const & buffer, support::FileName const & filename,
202 std::string const & format_name) const;
204 docstring const prettyName(std::string const & name) const;
206 std::string const extension(std::string const & name) const;
208 std::string const extensions(std::string const & name) const;
210 const_iterator begin() const { return formatlist_.begin(); }
212 const_iterator end() const { return formatlist_.end(); }
214 bool empty() const { return formatlist_.empty(); }
216 FormatList::size_type size() const { return formatlist_.size(); }
219 FormatList formatlist_;
223 std::string flavor2format(Flavor flavor);
224 // Not currently used.
225 // Flavor format2flavor(std::string fmt);
227 /// The global instance.
228 /// Implementation is in LyX.cpp.
229 extern Formats & theFormats();
231 /// The global copy after reading lyxrc.defaults.
232 /// Implementation is in LyX.cpp.
233 extern Formats & theSystemFormats();