]> git.lyx.org Git - lyx.git/blob - src/Format.h
21721fa8e6c10cfcfb1a480b5aaf1c11bbfb06f6
[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 #include <map>
21 #include <ctime>
22
23 namespace lyx {
24
25 namespace support { class FileName; }
26
27 class Buffer;
28
29 class Format {
30 public:
31         /// Flags for some format properties
32         enum Flags {
33                 none = 0,
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.
37                 document = 1,
38                 /// Set if this format can contain vector graphics.
39                 vector = 2,
40                 /// This format should  appear in the File > Export menu
41                 export_menu = 4,
42                 /// This may be a compressed file but doesn't need decompression
43                 zipped_native = 8
44         };
45         ///
46         Format(std::string const & n, std::string const & e, std::string const & p,
47                std::string const & s, std::string const & v, std::string const & ed,
48                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 { return extensions_; }
69         ///
70         void setExtensions(std::string const & v);
71         ///
72         std::string const & prettyname() const { return prettyname_; }
73         ///
74         void setPrettyname(std::string 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         bool documentFormat() const { return flags_ & document; }
89         ///
90         bool vectorFormat() const { return flags_ & vector; }
91         ///
92         void setFlags(int v) { flags_ = v; }
93         ///
94         bool inExportMenu() const { return flags_ & export_menu; }
95         ///
96         bool zippedNative() const { return flags_ & zipped_native; }
97 private:
98         /// Internal name. Needs to be unique.
99         std::string name_;
100         /// Filename extensions, the first one being the default
101         mutable std::vector<std::string> extension_list_;
102         /// All filename extensions
103         std::string extensions_;
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         int flags_;
120 };
121
122
123 bool operator<(Format const & a, Format const & b);
124
125
126 ///
127 class Formats {
128 public:
129         ///
130         typedef std::vector<Format> FormatList;
131         ///
132         typedef FormatList::const_iterator const_iterator;
133         ///
134         Format const & get(FormatList::size_type i) const { return formatlist[i]; }
135         ///
136         Format & get(FormatList::size_type i) { return formatlist[i]; }
137         /// \returns format named \p name if it exists, otherwise 0
138         Format const * getFormat(std::string const & name) const;
139         /*!
140          * Get the format of \p filename from file contents or, if this
141          * fails, from file extension.
142          * \returns file format if it could be found, otherwise an empty
143          * string.
144          */
145         std::string getFormatFromFile(support::FileName const & filename) const;
146         /// Finds a format from a file extension. Returns string() if not found.
147         std::string getFormatFromExtension(std::string const & ext) const;
148         /** Returns true if the file referenced by \p filename is zipped and
149          ** needs to be unzipped for being handled
150          ** @note For natively zipped formats, such as dia/odg, this returns false.
151          **/
152         bool isZippedFile(support::FileName const & filename) const;
153         /// Set editor and/or viewer to "auto" for formats that can be
154         /// opened by the OS.
155         void setAutoOpen();
156         ///
157         int getNumber(std::string const & name) const;
158         ///
159         void add(std::string const & name);
160         ///
161         void add(std::string const & name, std::string const & extensions,
162                  std::string const & prettyname, std::string const & shortcut,
163                  std::string const & viewer, std::string const & editor,
164                  int flags);
165         ///
166         void erase(std::string const & name);
167         ///
168         void sort();
169         ///
170         void setViewer(std::string const & name, std::string const & command);
171         ///
172         void setEditor(std::string const & name, std::string const & command);
173         /// View the given file. Buffer used for DVI's paper orientation.
174         bool view(Buffer const & buffer, support::FileName const & filename,
175                   std::string const & format_name) const;
176         ///
177         bool edit(Buffer const & buffer, support::FileName const & filename,
178                   std::string const & format_name) const;
179         ///
180         docstring const prettyName(std::string const & name) const;
181         ///
182         std::string const extension(std::string const & name) const;
183         ///
184         std::string const extensions(std::string const & name) const;
185         ///
186         const_iterator begin() const { return formatlist.begin(); }
187         ///
188         const_iterator end() const { return formatlist.end(); }
189         ///
190         FormatList::size_type size() const { return formatlist.size(); }
191 private:
192         ///
193         FormatList formatlist;
194         /// Used to store last timestamp of file and whether it is (was) zipped
195         struct ZippedInfo {
196                 bool zipped; std::time_t timestamp;
197                 ZippedInfo(bool zipped, std::time_t timestamp)
198                 : zipped(zipped), timestamp(timestamp) { }
199         };
200         ///
201         mutable std::map<std::string, ZippedInfo> zipped_;
202 };
203
204 ///
205 std::string flavor2format(OutputParams::FLAVOR flavor);
206 // Not currently used.
207 // OutputParams::FLAVOR format2flavor(std::string fmt);
208
209 extern Formats formats;
210
211 extern Formats system_formats;
212
213
214 } // namespace lyx
215
216 #endif //FORMAT_H