]> git.lyx.org Git - lyx.git/blob - src/Session.h
772027ec80c4c340de12093e267641870c2ee677
[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/FileName.h"
17 #include "support/types.h"
18
19 #include <string>
20 #include <deque>
21 #include <vector>
22 #include <map>
23
24 // used by at least frontends/qt4/GuiPref.cpp
25 const long maxlastfiles = 20;
26
27 /** This session file maintains
28   1. the latest documents loaded (lastfiles)
29   2. cursor positions of files closed (lastfilepos)
30   3. opened files when a lyx session is closed (lastopened)
31   4. bookmarks
32   5. general purpose session info in the form of key/value pairs
33  */
34 namespace lyx {
35
36 /* base class for all sections in the session file
37 */
38 class SessionSection
39 {
40 public:
41         ///
42         SessionSection() {}
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 private:
53         /// uncopiable
54         SessionSection(SessionSection const &);
55         void operator=(SessionSection const &);
56 };
57
58
59 class LastFilesSection : SessionSection
60 {
61 public:
62         ///
63         typedef std::deque<support::FileName> LastFiles;
64
65 public:
66         ///
67         explicit LastFilesSection(unsigned int num = 4);
68
69         ///
70         void read(std::istream & is);
71
72         ///
73         void write(std::ostream & os) const;
74
75         /// Return lastfiles container (deque)
76         LastFiles const lastFiles() const { return lastfiles; }
77
78         /** Insert #file# into the lastfile dequeue.
79             This funtion inserts #file# into the last files list. If the file
80             already exists it is moved to the top of the list, else exist it
81             is placed on the top of the list. If the list is full the last
82             file in the list is popped from the end.
83             @param file the file to insert in the lastfile list.
84         */
85         void add(support::FileName const & file);
86
87 private:
88         /// Default number of lastfiles.
89         unsigned int const default_num_last_files;
90
91         /// Max number of lastfiles.
92         unsigned int const absolute_max_last_files;
93
94         /// a list of lastfiles
95         LastFiles lastfiles;
96
97         /// number of files in the lastfiles list.
98         unsigned int num_lastfiles;
99
100         /** Used by the constructor to set the number of stored last files.
101             @param num the number of lastfiles to set.
102         */
103         void setNumberOfLastFiles(unsigned int num);
104 };
105
106
107 class LastOpenedSection : SessionSection
108 {
109 public:
110         ///
111         typedef std::vector<support::FileName> LastOpened;
112
113 public:
114         ///
115         void read(std::istream & is);
116
117         ///
118         void write(std::ostream & os) const;
119
120         /// Return lastopened container (vector)
121         LastOpened const getfiles() const { return lastopened; }
122
123         /** add file to lastopened file list
124             @param file filename to add
125         */
126         void add(support::FileName const & file);
127
128         /** clear lastopened file list
129          */
130         void clear();
131
132 private:
133         /// a list of lastopened files
134         LastOpened lastopened;
135 };
136
137
138 class LastFilePosSection : SessionSection
139 {
140 public:
141         ///
142         struct FilePos {
143                 FilePos() : pit(0), pos(0) {}
144                 pit_type pit;
145                 pos_type pos;
146         };
147
148         ///
149         typedef std::map<support::FileName, FilePos> FilePosMap;
150
151 public:
152         ///
153         LastFilePosSection() : num_lastfilepos(100) {}
154
155         ///
156         void read(std::istream & is);
157
158         ///
159         void write(std::ostream & os) const;
160
161         /** add cursor position to the fname entry in the filepos map
162             @param fname file entry for which to save position information
163             @param pos position of the cursor when the file is closed.
164         */
165         void save(support::FileName const & fname, FilePos const & pos);
166
167         /** load saved cursor position from the fname entry in the filepos map
168             @param fname file entry for which to load position information
169         */
170         FilePos load(support::FileName const & fname) const;
171
172 private:
173         /// default number of lastfilepos to save */
174         unsigned int const num_lastfilepos;
175
176
177         /// a map of file positions
178         FilePosMap lastfilepos;
179 };
180
181
182 class BookmarksSection : SessionSection
183 {
184 public:
185         /// A bookmark is composed of three parts
186         /// 1. filename
187         /// 2. bottom (whole document) level pit and pos, used to (inaccurately) save/restore a bookmark
188         /// 3. top level id and pos, used to accurately locate bookmark when lyx is running
189         /// top and bottom level information sometimes needs to be sync'ed. In particular,
190         /// top_id is determined when a bookmark is restored from session; and
191         /// bottom_pit and bottom_pos are determined from top_id when a bookmark
192         /// is save to session. (What a mess! :-)
193         ///
194         /// TODO: bottom level pit and pos will be replaced by StableDocIterator
195         class Bookmark {
196         public:
197                 /// Filename
198                 support::FileName filename;
199                 /// Bottom level cursor pit, will be saved/restored by .lyx/session
200                 pit_type bottom_pit;
201                 /// Bottom level cursor position, will be saved/restore by .lyx/session
202                 pos_type bottom_pos;
203                 /// Top level cursor id, used to lcoate bookmarks for opened files
204                 int top_id;
205                 /// Top level cursor position within a paragraph
206                 pos_type top_pos;
207                 ///
208                 Bookmark() : bottom_pit(0), bottom_pos(0), top_id(0), top_pos(0) {}
209                 ///
210                 Bookmark(support::FileName const & f, pit_type pit, pos_type pos, int id, pos_type tpos)
211                         : filename(f), bottom_pit(pit), bottom_pos(pos), top_id(id), top_pos(tpos) {}
212                 /// set bookmark top_id, this is because newly loaded bookmark
213                 /// may have zero par_id and par_pit can change during editing, see bug 3092
214                 void updatePos(pit_type pit, pos_type pos, int id) {
215                         bottom_pit = pit;
216                         bottom_pos = pos;
217                         top_id = id;
218                 }
219         };
220
221         ///
222         typedef std::vector<Bookmark> BookmarkList;
223
224 public:
225         /// constructor, set max_bookmarks
226         /// allow 9 regular bookmarks, bookmark 0 is temporary
227         BookmarksSection() : bookmarks(10), max_bookmarks(9) {}
228
229         /// Save the current position as bookmark
230         void save(support::FileName const & fname, pit_type bottom_pit, pos_type bottom_pos,
231                 int top_id, pos_type top_pos, unsigned int idx);
232
233         /// return bookmark 0-9, bookmark 0 is the temporary bookmark
234         Bookmark const & bookmark(unsigned int i) const;
235
236         /// does the given bookmark have a saved position ?
237         bool isValid(unsigned int i) const;
238
239         ///
240         unsigned int size() const { return max_bookmarks; }
241
242         /// clear all bookmarks
243         void clear();
244
245         ///
246         void read(std::istream & is);
247
248         ///
249         void write(std::ostream & os) const;
250
251         /** return bookmark list. Non-const container is used since
252                 bookmarks will be cleaned after use.
253         */
254         BookmarkList & load() { return bookmarks; }
255
256 private:
257
258         /// a list of bookmarks
259         BookmarkList bookmarks;
260
261         ///
262         unsigned int const max_bookmarks;
263 };
264
265
266 class SessionInfoSection : SessionSection
267 {
268 public:
269         ///
270         typedef std::map<std::string, std::string> MiscInfo;
271
272 public:
273         ///
274         void read(std::istream & is);
275
276         ///
277         void write(std::ostream & os) const;
278
279         /** set session info
280                 @param key key of the value to store
281                 @param value value, a string without newline ('\n')
282         */
283         void save(std::string const & key, std::string const & value);
284
285         /** load session info
286                 @param key a key to extract value from the session file
287                 @param release whether or not clear the value. Default to true
288                         since most of such values are supposed to be used only once.
289         */
290         std::string const load(std::string const & key, bool release = true);
291
292 private:
293         /// a map to save session info
294         MiscInfo sessioninfo;
295 };
296
297
298 class Session
299 {
300 public:
301         /// Read the session file.  @param num length of lastfiles
302         explicit Session(unsigned int num = 4);
303         /// Write the session file.
304         void writeFile() const;
305         ///
306         LastFilesSection & lastFiles() { return last_files; }
307         ///
308         LastFilesSection const & lastFiles() const { return last_files; }
309         ///
310         LastOpenedSection & lastOpened() { return last_opened; }
311         ///
312         LastOpenedSection const & lastOpened() const { return last_opened; }
313         ///
314         LastFilePosSection & lastFilePos() { return last_file_pos; }
315         ///
316         LastFilePosSection const & lastFilePos() const { return last_file_pos; }
317         ///
318         BookmarksSection & bookmarks() { return bookmarks_; }
319         ///
320         BookmarksSection const & bookmarks() const { return bookmarks_; }
321         ///
322         SessionInfoSection & sessionInfo() { return session_info; }
323         ///
324         SessionInfoSection const & sessionInfo() const { return session_info; }
325
326 private:
327         friend class LyX;
328         /// uncopiable
329         Session(Session const &);
330         void operator=(Session const &);
331
332         /// file to save session, determined in the constructor.
333         support::FileName session_file;
334
335         /** Read the session file.
336             Reads the #.lyx/session# at the beginning of the LyX session.
337             This will read the session file (usually #.lyx/session#).
338             @param file the file containing the session.
339         */
340         void readFile();
341
342         ///
343         LastFilesSection last_files;
344         ///
345         LastOpenedSection last_opened;
346         ///
347         LastFilePosSection last_file_pos;
348         ///
349         BookmarksSection bookmarks_;
350         ///
351         SessionInfoSection session_info;
352 };
353
354 /// This is a singleton class. Get the instance.
355 /// Implemented in LyX.cpp.
356 Session & theSession();
357
358 } // lyx
359
360 #endif