]> git.lyx.org Git - lyx.git/blob - src/session.h
New bookmarks implementation:
[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         /// bookmarks
177         class Bookmark {
178         public:
179                 /// Filename
180                 std::string filename;
181                 /// Cursor paragraph Id
182                 int par_id;
183                 /// Cursor position
184                 pos_type par_pos;
185                 ///
186                 Bookmark() : par_id(0), par_pos(0) {}
187                 ///
188                 Bookmark(std::string const & f, int id, pos_type pos)
189                         : filename(f), par_id(id), par_pos(pos) {}
190         };
191
192         ///
193         typedef std::deque<Bookmark> BookmarkList;
194
195 public:
196         /// constructor, set max_bookmarks
197         /// allow 20 regular bookmarks
198         BookmarksSection::BookmarksSection() : max_bookmarks(20), bookmarks(0) {}
199
200         /// Save the current position as bookmark
201         /// if save==false, save to temp_bookmark
202         void save(std::string const & fname, int par_id, pos_type par_pos, bool persistent);
203
204         /// return bookmark, return temp_bookmark if i==0
205         Bookmark const & bookmark(unsigned int i) const;
206
207         /// does the given bookmark have a saved position ?
208         bool isValid(unsigned int i) const;
209
210         ///
211         unsigned int size() const { return bookmarks.size(); }
212
213         /// clear all bookmarks
214         void clear() { bookmarks.clear(); }
215
216         ///
217         void read(std::istream & is);
218
219         ///
220         void write(std::ostream & os) const;
221
222         /** return bookmark list. Non-const container is used since
223                 bookmarks will be cleaned after use.
224         */
225         BookmarkList & load() { return bookmarks; }
226
227 private:
228         /// temp bookmark (previously saved_positions[0]), this is really ugly
229         /// c.f. ./frontends/controllers/ControlRef.C
230         /// FIXME: a separate LFUN may be a better solution
231         Bookmark temp_bookmark;
232
233         /// a list of bookmarks
234         BookmarkList bookmarks;
235
236         ///
237         unsigned int const max_bookmarks;
238 };
239
240
241 class SessionInfoSection : SessionSection
242 {
243 public:
244         ///
245         typedef std::map<std::string, std::string> MiscInfo;
246
247 public:
248         ///
249         void read(std::istream & is);
250
251         ///
252         void write(std::ostream & os) const;
253
254         /** set session info
255                 @param key key of the value to store
256                 @param value value, a string without newline ('\n')
257         */
258         void save(std::string const & key, std::string const & value);
259
260         /** load session info
261                 @param key a key to extract value from the session file
262                 @param release whether or not clear the value. Default to true
263                         since most of such values are supposed to be used only once.
264         */
265         std::string const load(std::string const & key, bool release = true);
266
267 private:
268         /// a map to save session info
269         MiscInfo sessioninfo;
270 };
271
272
273 class Session : boost::noncopyable {
274
275 public:
276         /** Read the session file.
277             @param num length of lastfiles
278         */
279         explicit Session(unsigned int num = 4);
280
281         /** Write the session file.
282         */
283         void writeFile() const;
284
285         ///
286         LastFilesSection & lastFiles() { return last_files; }
287         
288         ///
289         LastFilesSection const & lastFiles() const { return last_files; }
290
291         ///
292         LastOpenedSection & lastOpened() { return last_opened; }
293
294         ///
295         LastOpenedSection const & lastOpened() const { return last_opened; }
296         
297         ///
298         LastFilePosSection & lastFilePos() { return last_file_pos; }
299         
300         ///
301         LastFilePosSection const & lastFilePos() const { return last_file_pos; }
302
303         ///
304         BookmarksSection & bookmarks() { return bookmarks_; }
305
306         ///
307         BookmarksSection const & bookmarks() const { return bookmarks_; }
308
309         ///
310         SessionInfoSection & sessionInfo() { return session_info; }
311
312         ///
313         SessionInfoSection const & sessionInfo() const { return session_info; }
314
315 private:
316         /// file to save session, determined in the constructor.
317         std::string session_file;
318
319         /** Read the session file.
320             Reads the #.lyx/session# at the beginning of the LyX session.
321             This will read the session file (usually #.lyx/session#).
322             @param file the file containing the session.
323         */
324         void readFile();
325
326         ///
327         LastFilesSection last_files;
328
329         ///
330         LastOpenedSection last_opened;
331
332         ///
333         LastFilePosSection last_file_pos;
334
335         ///
336         BookmarksSection bookmarks_;
337
338         ///
339         SessionInfoSection session_info;
340 };
341
342 }
343
344 #endif