]> git.lyx.org Git - lyx.git/blob - src/session.h
* Painter.h:
[lyx.git] / src / session.h
1 // -*- C++ -*-
2 /**
3  * \file session.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Lars Gullik Bjønnes
8  * \author Bo Peng
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #ifndef SESSION_H
14 #define SESSION_H
15
16 #include <support/types.h>
17
18 #include <boost/utility.hpp>
19 #include <boost/tuple/tuple.hpp>
20
21 #include <string>
22 #include <deque>
23 #include <vector>
24 #include <map>
25
26 // used by at least frontends/qt2/QPref.C
27 const long maxlastfiles = 20;
28
29 /** This session file maintains
30   1. the latest documents loaded (lastfiles)
31   2. cursor positions of files closed (lastfilepos)
32   3. opened files when a lyx session is closed (lastopened)
33   4. bookmarks
34   5. general purpose session info in the form of key/value pairs
35  */
36 namespace lyx {
37
38 /** base class for all sections in the session file
39 */
40 class SessionSection : boost::noncopyable {
41
42 public:
43         ///
44         virtual ~SessionSection() {}
45
46         /// read section from std::istream
47         virtual void read(std::istream & is) = 0;
48
49         /// write to std::ostream
50         virtual void write(std::ostream & os) const = 0;
51 };
52
53
54 class LastFilesSection : SessionSection
55 {
56 public:
57         ///
58         typedef std::deque<std::string> LastFiles;
59
60 public:
61         ///
62         explicit LastFilesSection(unsigned int num = 4);
63
64         ///
65         void read(std::istream & is);
66
67         ///
68         void write(std::ostream & os) const;
69
70         /// Return lastfiles container (deque)
71         LastFiles const lastFiles() const { return lastfiles; }
72
73         /** Insert #file# into the lastfile dequeue.
74             This funtion inserts #file# into the last files list. If the file
75             already exists it is moved to the top of the list, else exist it
76             is placed on the top of the list. If the list is full the last
77             file in the list is popped from the end.
78             @param file the file to insert in the lastfile list.
79         */
80         void add(std::string const & file);
81
82 private:
83         /// Default number of lastfiles.
84         unsigned int const default_num_last_files;
85
86         /// Max number of lastfiles.
87         unsigned int const absolute_max_last_files;
88
89         /// a list of lastfiles
90         LastFiles lastfiles;
91
92         /// number of files in the lastfiles list.
93         unsigned int num_lastfiles;
94
95         /** Used by the constructor to set the number of stored last files.
96             @param num the number of lastfiles to set.
97         */
98         void setNumberOfLastFiles(unsigned int num);
99 };
100
101
102 class LastOpenedSection : SessionSection
103 {
104 public:
105         ///
106         typedef std::vector<std::string> LastOpened;
107
108 public:
109         ///
110         void read(std::istream & is);
111
112         ///
113         void write(std::ostream & os) const;
114
115         /// Return lastopened container (vector)
116         LastOpened const getfiles() const { return lastopened; }
117
118         /** add file to lastopened file list
119             @param file filename to add
120         */
121         void add(std::string const & file);
122
123         /** clear lastopened file list
124          */
125         void clear();
126
127 private:
128         /// a list of lastopened files
129         LastOpened lastopened;
130 };
131
132
133 class LastFilePosSection : SessionSection
134 {
135 public:
136         ///
137         typedef boost::tuple<pit_type, pos_type> FilePos;
138
139         ///
140         typedef std::map<std::string, FilePos> FilePosMap;
141
142 public:
143         ///
144         LastFilePosSection() : num_lastfilepos(100) {}
145
146         ///
147         void read(std::istream & is);
148
149         ///
150         void write(std::ostream & os) const;
151
152         /** add cursor position to the fname entry in the filepos map
153             @param fname file entry for which to save position information
154             @param pos position of the cursor when the file is closed.
155         */
156         void save(std::string const & fname, FilePos pos);
157
158         /** load saved cursor position from the fname entry in the filepos map
159             @param fname file entry for which to load position information
160         */
161         FilePos load(std::string const & fname) const;
162
163 private:
164         /// default number of lastfilepos to save */
165         unsigned int const num_lastfilepos;
166
167
168         /// a map of file positions
169         FilePosMap lastfilepos;
170 };
171
172
173 class BookmarksSection : SessionSection
174 {
175 public:
176         ///
177         typedef boost::tuple<unsigned int, std::string, unsigned int, pos_type> Bookmark;
178
179         ///
180         typedef std::vector<Bookmark> BookmarkList;
181
182 public:
183         ///
184         void read(std::istream & is);
185
186         ///
187         void write(std::ostream & os) const;
188
189         /** save a bookmark
190                 @bookmark bookmark to be saved
191         */
192         void save(Bookmark const & bookmark);
193
194         /** return bookmark list. Non-const container is used since
195                 bookmarks will be cleaned after use.
196         */
197         BookmarkList & load() { return bookmarks; }
198
199 private:
200         /// a list of bookmarks
201         BookmarkList bookmarks;
202 };
203
204
205 class SessionInfoSection : SessionSection
206 {
207 public:
208         ///
209         typedef std::map<std::string, std::string> MiscInfo;
210
211 public:
212         ///
213         void read(std::istream & is);
214
215         ///
216         void write(std::ostream & os) const;
217
218         /** set session info
219                 @param key key of the value to store
220                 @param value value, a string without newline ('\n')
221         */
222         void save(std::string const & key, std::string const & value);
223
224         /** load session info
225                 @param key a key to extract value from the session file
226                 @param release whether or not clear the value. Default to true
227                         since most of such values are supposed to be used only once.
228         */
229         std::string const load(std::string const & key, bool release = true);
230
231 private:
232         /// a map to save session info
233         MiscInfo sessioninfo;
234 };
235
236
237 class Session : boost::noncopyable {
238
239 public:
240         /** Read the session file.
241             @param num length of lastfiles
242         */
243         explicit Session(unsigned int num = 4);
244
245         /** Write the session file.
246         */
247         void writeFile() const;
248
249         ///
250         LastFilesSection & lastFiles() { return last_files; }
251         
252         ///
253         LastFilesSection const & lastFiles() const { return last_files; }
254
255         ///
256         LastOpenedSection & lastOpened() { return last_opened; }
257
258         ///
259         LastOpenedSection const & lastOpened() const { return last_opened; }
260         
261         ///
262         LastFilePosSection & lastFilePos() { return last_file_pos; }
263         
264         ///
265         LastFilePosSection const & lastFilePos() const { return last_file_pos; }
266
267         ///
268         BookmarksSection & bookmarks() { return bookmarks_; }
269
270         ///
271         BookmarksSection const & bookmarks() const { return bookmarks_; }
272
273         ///
274         SessionInfoSection & sessionInfo() { return session_info; }
275
276         ///
277         SessionInfoSection const & sessionInfo() const { return session_info; }
278
279 private:
280         /// file to save session, determined in the constructor.
281         std::string session_file;
282
283         /** Read the session file.
284             Reads the #.lyx/session# at the beginning of the LyX session.
285             This will read the session file (usually #.lyx/session#).
286             @param file the file containing the session.
287         */
288         void readFile();
289
290         ///
291         LastFilesSection last_files;
292
293         ///
294         LastOpenedSection last_opened;
295
296         ///
297         LastFilePosSection last_file_pos;
298
299         ///
300         BookmarksSection bookmarks_;
301
302         ///
303         SessionInfoSection session_info;
304 };
305
306 }
307
308 #endif