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