]> git.lyx.org Git - lyx.git/blob - src/session.h
hopefully fix tex2lyx linking.
[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() : bookmarks(0), max_bookmarks(20) {}
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 ToolbarSection : SessionSection
242 {
243 public:
244         /// information about a toolbar, not all information can be
245         /// saved/restored by all frontends, but this class provides
246         /// a superset of things that can be managed by session.
247         class ToolbarInfo
248         {
249         public:
250                 ///
251                 ToolbarInfo() :
252                         state(ON), location(NOTSET) { }
253                 ///
254                 ToolbarInfo(int s, int loc) :
255                         state(static_cast<State>(s)), location(static_cast<Location>(loc)) { }
256
257         public:
258                 enum State {
259                         ON,
260                         OFF,
261                         AUTO
262                 };
263
264                 /// on/off/auto
265                 State state;
266
267                 /// location: this can be intepreted differently.
268                 enum Location {
269                         TOP,
270                         BOTTOM,
271                         LEFT,
272                         RIGHT,
273                         NOTSET
274                 };
275
276                 Location location;
277
278                 /// potentially, icons
279         };
280
281         /// info for each toolbar
282         typedef std::map<std::string, ToolbarInfo> ToolbarMap;
283
284 public:
285         ///
286         void read(std::istream & is);
287
288         ///
289         void write(std::ostream & os) const;
290
291         /// return reference to toolbar info, create a new one if needed
292         ToolbarInfo & load(std::string const & name);
293
294 private:
295         /// toolbar information
296         ToolbarMap toolbars;
297 };
298
299
300 class SessionInfoSection : SessionSection
301 {
302 public:
303         ///
304         typedef std::map<std::string, std::string> MiscInfo;
305
306 public:
307         ///
308         void read(std::istream & is);
309
310         ///
311         void write(std::ostream & os) const;
312
313         /** set session info
314                 @param key key of the value to store
315                 @param value value, a string without newline ('\n')
316         */
317         void save(std::string const & key, std::string const & value);
318
319         /** load session info
320                 @param key a key to extract value from the session file
321                 @param release whether or not clear the value. Default to true
322                         since most of such values are supposed to be used only once.
323         */
324         std::string const load(std::string const & key, bool release = true);
325
326 private:
327         /// a map to save session info
328         MiscInfo sessioninfo;
329 };
330
331
332 class Session : boost::noncopyable {
333
334 public:
335         /** Read the session file.
336             @param num length of lastfiles
337         */
338         explicit Session(unsigned int num = 4);
339
340         /** Write the session file.
341         */
342         void writeFile() const;
343
344         ///
345         LastFilesSection & lastFiles() { return last_files; }
346         
347         ///
348         LastFilesSection const & lastFiles() const { return last_files; }
349
350         ///
351         LastOpenedSection & lastOpened() { return last_opened; }
352
353         ///
354         LastOpenedSection const & lastOpened() const { return last_opened; }
355         
356         ///
357         LastFilePosSection & lastFilePos() { return last_file_pos; }
358         
359         ///
360         LastFilePosSection const & lastFilePos() const { return last_file_pos; }
361
362         ///
363         BookmarksSection & bookmarks() { return bookmarks_; }
364
365         ///
366         BookmarksSection const & bookmarks() const { return bookmarks_; }
367
368         ///
369         ToolbarSection & toolbars() { return toolbars_; }
370
371         ///
372         ToolbarSection const & toolbars() const { return toolbars_; }
373
374         ///
375         SessionInfoSection & sessionInfo() { return session_info; }
376
377         ///
378         SessionInfoSection const & sessionInfo() const { return session_info; }
379
380 private:
381         /// file to save session, determined in the constructor.
382         std::string session_file;
383
384         /** Read the session file.
385             Reads the #.lyx/session# at the beginning of the LyX session.
386             This will read the session file (usually #.lyx/session#).
387             @param file the file containing the session.
388         */
389         void readFile();
390
391         ///
392         LastFilesSection last_files;
393
394         ///
395         LastOpenedSection last_opened;
396
397         ///
398         LastFilePosSection last_file_pos;
399
400         ///
401         BookmarksSection bookmarks_;
402
403         ///
404         ToolbarSection toolbars_;
405
406         ///
407         SessionInfoSection session_info;
408 };
409
410 }
411
412 #endif