]> git.lyx.org Git - lyx.git/blob - src/Session.h
Andre's s/getTextClass/textClass/ cleanup.
[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 ToolbarSection : SessionSection
267 {
268 public:
269         /// information about a toolbar, not all information can be
270         /// saved/restored by all frontends, but this class provides
271         /// a superset of things that can be managed by session.
272         class ToolbarInfo
273         {
274         public:
275                 ///
276                 ToolbarInfo()
277                         : state(ON), location(NOTSET), posx(0), posy(0)
278                 {}
279                 ///
280                 //ToolbarInfo(int s, int loc, int x = 0, int y = 0)
281         //              : state(State(s)), location(Location(loc)), posx(x), posy(y)
282         //      {}
283
284         public:
285                 enum State {
286                         ON,
287                         OFF,
288                         AUTO
289                 };
290
291                 /// on/off/auto
292                 State state;
293
294                 /// location: this can be intepreted differently.
295                 enum Location {
296                         TOP,
297                         BOTTOM,
298                         LEFT,
299                         RIGHT,
300                         NOTSET
301                 };
302
303                 Location location;
304
305                 /// x-position of the toolbar
306                 int posx;
307
308                 /// y-position of the toolbar
309                 int posy;
310
311                 /// potentially, icons
312         };
313
314         struct ToolbarItem {
315                 std::string key;
316                 ToolbarInfo info;
317         };
318
319         /// info for each toolbar
320         typedef std::vector<ToolbarItem> ToolbarList;
321
322
323 public:
324         ///
325         void read(std::istream & is);
326
327         ///
328         void write(std::ostream & os) const;
329
330         /// return reference to toolbar info, create a new one if needed
331         ToolbarInfo & load(std::string const & name);
332
333         /// toolbar begin
334         ToolbarList::const_iterator begin() { return toolbars.begin(); }
335
336         /// toolbar end
337         ToolbarList::const_iterator end() { return toolbars.end(); }
338
339 private:
340         /// toolbar information
341         ToolbarList toolbars;
342 };
343
344 /// comparison operator to sort toolbars, the rules are:
345 ///         ON before OFF
346 ///     TOP < BOTTOM < LEFT < RIGHT
347 ///     Line at each side
348 ///     order in each line
349 bool operator<(ToolbarSection::ToolbarItem const & a,
350         ToolbarSection::ToolbarItem const & b);
351
352
353 class SessionInfoSection : SessionSection
354 {
355 public:
356         ///
357         typedef std::map<std::string, std::string> MiscInfo;
358
359 public:
360         ///
361         void read(std::istream & is);
362
363         ///
364         void write(std::ostream & os) const;
365
366         /** set session info
367                 @param key key of the value to store
368                 @param value value, a string without newline ('\n')
369         */
370         void save(std::string const & key, std::string const & value);
371
372         /** load session info
373                 @param key a key to extract value from the session file
374                 @param release whether or not clear the value. Default to true
375                         since most of such values are supposed to be used only once.
376         */
377         std::string const load(std::string const & key, bool release = true);
378
379 private:
380         /// a map to save session info
381         MiscInfo sessioninfo;
382 };
383
384
385 class Session
386 {
387 public:
388         /// Read the session file.  @param num length of lastfiles
389         explicit Session(unsigned int num = 4);
390         /// Write the session file.
391         void writeFile() const;
392         ///
393         LastFilesSection & lastFiles() { return last_files; }
394         ///
395         LastFilesSection const & lastFiles() const { return last_files; }
396         ///
397         LastOpenedSection & lastOpened() { return last_opened; }
398         ///
399         LastOpenedSection const & lastOpened() const { return last_opened; }
400         ///
401         LastFilePosSection & lastFilePos() { return last_file_pos; }
402         ///
403         LastFilePosSection const & lastFilePos() const { return last_file_pos; }
404         ///
405         BookmarksSection & bookmarks() { return bookmarks_; }
406         ///
407         BookmarksSection const & bookmarks() const { return bookmarks_; }
408         ///
409         ToolbarSection & toolbars() { return toolbars_; }
410         ///
411         ToolbarSection const & toolbars() const { return toolbars_; }
412         ///
413         SessionInfoSection & sessionInfo() { return session_info; }
414         ///
415         SessionInfoSection const & sessionInfo() const { return session_info; }
416
417 private:
418         /// uncopiable
419         Session(Session const &);
420         void operator=(Session const &);
421
422         /// file to save session, determined in the constructor.
423         support::FileName session_file;
424
425         /** Read the session file.
426             Reads the #.lyx/session# at the beginning of the LyX session.
427             This will read the session file (usually #.lyx/session#).
428             @param file the file containing the session.
429         */
430         void readFile();
431
432         ///
433         LastFilesSection last_files;
434         ///
435         LastOpenedSection last_opened;
436         ///
437         LastFilePosSection last_file_pos;
438         ///
439         BookmarksSection bookmarks_;
440         ///
441         ToolbarSection toolbars_;
442         ///
443         SessionInfoSection session_info;
444 };
445
446 }
447
448 #endif