]> git.lyx.org Git - lyx.git/blob - src/session.h
* output_plaintext.C: cosmetics in comment: line length cannot be < 0
[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 <boost/utility.hpp>
20 #include <boost/tuple/tuple.hpp>
21
22 #include <string>
23 #include <deque>
24 #include <vector>
25 #include <map>
26
27 // used by at least frontends/qt2/QPref.C
28 const long maxlastfiles = 20;
29
30 /** This session file maintains
31   1. the latest documents loaded (lastfiles)
32   2. cursor positions of files closed (lastfilepos)
33   3. opened files when a lyx session is closed (lastopened)
34   4. bookmarks
35   5. general purpose session info in the form of key/value pairs
36  */
37 namespace lyx {
38
39 /** base class for all sections in the session file
40 */
41 class SessionSection : boost::noncopyable {
42
43 public:
44         ///
45         virtual ~SessionSection() {}
46
47         /// read section from std::istream
48         virtual void read(std::istream & is) = 0;
49
50         /// write to std::ostream
51         virtual void write(std::ostream & os) const = 0;
52 };
53
54
55 class LastFilesSection : SessionSection
56 {
57 public:
58         ///
59         typedef std::deque<support::FileName> LastFiles;
60
61 public:
62         ///
63         explicit LastFilesSection(unsigned int num = 4);
64
65         ///
66         void read(std::istream & is);
67
68         ///
69         void write(std::ostream & os) const;
70
71         /// Return lastfiles container (deque)
72         LastFiles const lastFiles() const { return lastfiles; }
73
74         /** Insert #file# into the lastfile dequeue.
75             This funtion inserts #file# into the last files list. If the file
76             already exists it is moved to the top of the list, else exist it
77             is placed on the top of the list. If the list is full the last
78             file in the list is popped from the end.
79             @param file the file to insert in the lastfile list.
80         */
81         void add(support::FileName const & file);
82
83 private:
84         /// Default number of lastfiles.
85         unsigned int const default_num_last_files;
86
87         /// Max number of lastfiles.
88         unsigned int const absolute_max_last_files;
89
90         /// a list of lastfiles
91         LastFiles lastfiles;
92
93         /// number of files in the lastfiles list.
94         unsigned int num_lastfiles;
95
96         /** Used by the constructor to set the number of stored last files.
97             @param num the number of lastfiles to set.
98         */
99         void setNumberOfLastFiles(unsigned int num);
100 };
101
102
103 class LastOpenedSection : SessionSection
104 {
105 public:
106         ///
107         typedef std::vector<support::FileName> LastOpened;
108
109 public:
110         ///
111         void read(std::istream & is);
112
113         ///
114         void write(std::ostream & os) const;
115
116         /// Return lastopened container (vector)
117         LastOpened const getfiles() const { return lastopened; }
118
119         /** add file to lastopened file list
120             @param file filename to add
121         */
122         void add(support::FileName const & file);
123
124         /** clear lastopened file list
125          */
126         void clear();
127
128 private:
129         /// a list of lastopened files
130         LastOpened lastopened;
131 };
132
133
134 class LastFilePosSection : SessionSection
135 {
136 public:
137         ///
138         typedef boost::tuple<pit_type, pos_type> FilePos;
139
140         ///
141         typedef std::map<support::FileName, FilePos> FilePosMap;
142
143 public:
144         ///
145         LastFilePosSection() : num_lastfilepos(100) {}
146
147         ///
148         void read(std::istream & is);
149
150         ///
151         void write(std::ostream & os) const;
152
153         /** add cursor position to the fname entry in the filepos map
154             @param fname file entry for which to save position information
155             @param pos position of the cursor when the file is closed.
156         */
157         void save(support::FileName const & fname, FilePos pos);
158
159         /** load saved cursor position from the fname entry in the filepos map
160             @param fname file entry for which to load position information
161         */
162         FilePos load(support::FileName const & fname) const;
163
164 private:
165         /// default number of lastfilepos to save */
166         unsigned int const num_lastfilepos;
167
168
169         /// a map of file positions
170         FilePosMap lastfilepos;
171 };
172
173
174 class BookmarksSection : SessionSection
175 {
176 public:
177         /// bookmarks
178         class Bookmark {
179         public:
180                 /// Filename
181                 support::FileName filename;
182                 /// Cursor pit, will be saved/restored by .lyx/session
183                 pit_type par_pit;
184                 /// Cursor paragraph Id, used to lcoate bookmarks for opened files
185                 int par_id;
186                 /// Cursor position within a paragraph
187                 pos_type par_pos;
188                 ///
189                 Bookmark() : par_id(0), par_pos(0) {}
190                 ///
191                 Bookmark(support::FileName const & f, pit_type pit, int id, pos_type pos)
192                         : filename(f), par_pit(pit), par_id(id), par_pos(pos) {}
193                 /// set bookmark par_id, this is because newly loaded bookmark
194                 /// may have zero par_id and par_pit can change during editing, see bug 3092
195                 void setPos(pit_type pit, int id) { 
196                         par_pit = pit;
197                         par_id = id;
198                 }
199         };
200
201         ///
202         typedef std::deque<Bookmark> BookmarkList;
203
204 public:
205         /// constructor, set max_bookmarks
206         /// allow 20 regular bookmarks
207         BookmarksSection() : bookmarks(0), max_bookmarks(20) {}
208
209         /// Save the current position as bookmark
210         /// if save==false, save to temp_bookmark
211         void save(support::FileName const & fname, pit_type pit, int par_id, pos_type par_pos, bool persistent);
212
213         /// return bookmark, return temp_bookmark if i==0
214         Bookmark const & bookmark(unsigned int i) const;
215
216         /// does the given bookmark have a saved position ?
217         bool isValid(unsigned int i) const;
218
219         ///
220         unsigned int size() const { return bookmarks.size(); }
221
222         /// clear all bookmarks
223         void clear() { bookmarks.clear(); }
224
225         ///
226         void read(std::istream & is);
227
228         ///
229         void write(std::ostream & os) const;
230
231         /** return bookmark list. Non-const container is used since
232                 bookmarks will be cleaned after use.
233         */
234         BookmarkList & load() { return bookmarks; }
235
236 private:
237         /// temp bookmark (previously saved_positions[0]), this is really ugly
238         /// c.f. ./frontends/controllers/ControlRef.C
239         /// FIXME: a separate LFUN may be a better solution
240         Bookmark temp_bookmark;
241
242         /// a list of bookmarks
243         BookmarkList bookmarks;
244
245         ///
246         unsigned int const max_bookmarks;
247 };
248
249
250 class ToolbarSection : SessionSection
251 {
252 public:
253         /// information about a toolbar, not all information can be
254         /// saved/restored by all frontends, but this class provides
255         /// a superset of things that can be managed by session.
256         class ToolbarInfo
257         {
258         public:
259                 ///
260                 ToolbarInfo() :
261                         state(ON), location(NOTSET) { }
262                 ///
263                 ToolbarInfo(int s, int loc) :
264                         state(static_cast<State>(s)), location(static_cast<Location>(loc)) { }
265
266         public:
267                 enum State {
268                         ON,
269                         OFF,
270                         AUTO
271                 };
272
273                 /// on/off/auto
274                 State state;
275
276                 /// location: this can be intepreted differently.
277                 enum Location {
278                         TOP,
279                         BOTTOM,
280                         LEFT,
281                         RIGHT,
282                         NOTSET
283                 };
284
285                 Location location;
286
287                 /// potentially, icons
288         };
289
290         /// info for each toolbar
291         typedef std::map<std::string, ToolbarInfo> ToolbarMap;
292
293 public:
294         ///
295         void read(std::istream & is);
296
297         ///
298         void write(std::ostream & os) const;
299
300         /// return reference to toolbar info, create a new one if needed
301         ToolbarInfo & load(std::string const & name);
302
303 private:
304         /// toolbar information
305         ToolbarMap toolbars;
306 };
307
308
309 class SessionInfoSection : SessionSection
310 {
311 public:
312         ///
313         typedef std::map<std::string, std::string> MiscInfo;
314
315 public:
316         ///
317         void read(std::istream & is);
318
319         ///
320         void write(std::ostream & os) const;
321
322         /** set session info
323                 @param key key of the value to store
324                 @param value value, a string without newline ('\n')
325         */
326         void save(std::string const & key, std::string const & value);
327
328         /** load session info
329                 @param key a key to extract value from the session file
330                 @param release whether or not clear the value. Default to true
331                         since most of such values are supposed to be used only once.
332         */
333         std::string const load(std::string const & key, bool release = true);
334
335 private:
336         /// a map to save session info
337         MiscInfo sessioninfo;
338 };
339
340
341 class Session : boost::noncopyable {
342
343 public:
344         /** Read the session file.
345             @param num length of lastfiles
346         */
347         explicit Session(unsigned int num = 4);
348
349         /** Write the session file.
350         */
351         void writeFile() const;
352
353         ///
354         LastFilesSection & lastFiles() { return last_files; }
355         
356         ///
357         LastFilesSection const & lastFiles() const { return last_files; }
358
359         ///
360         LastOpenedSection & lastOpened() { return last_opened; }
361
362         ///
363         LastOpenedSection const & lastOpened() const { return last_opened; }
364         
365         ///
366         LastFilePosSection & lastFilePos() { return last_file_pos; }
367         
368         ///
369         LastFilePosSection const & lastFilePos() const { return last_file_pos; }
370
371         ///
372         BookmarksSection & bookmarks() { return bookmarks_; }
373
374         ///
375         BookmarksSection const & bookmarks() const { return bookmarks_; }
376
377         ///
378         ToolbarSection & toolbars() { return toolbars_; }
379
380         ///
381         ToolbarSection const & toolbars() const { return toolbars_; }
382
383         ///
384         SessionInfoSection & sessionInfo() { return session_info; }
385
386         ///
387         SessionInfoSection const & sessionInfo() const { return session_info; }
388
389 private:
390         /// file to save session, determined in the constructor.
391         support::FileName session_file;
392
393         /** Read the session file.
394             Reads the #.lyx/session# at the beginning of the LyX session.
395             This will read the session file (usually #.lyx/session#).
396             @param file the file containing the session.
397         */
398         void readFile();
399
400         ///
401         LastFilesSection last_files;
402
403         ///
404         LastOpenedSection last_opened;
405
406         ///
407         LastFilePosSection last_file_pos;
408
409         ///
410         BookmarksSection bookmarks_;
411
412         ///
413         ToolbarSection toolbars_;
414
415         ///
416         SessionInfoSection session_info;
417 };
418
419 }
420
421 #endif