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