4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
7 * \author Lars Gullik Bjønnes
10 * Full author contact details are available in file CREDITS.
16 #include <support/types.h>
18 #include <boost/utility.hpp>
19 #include <boost/tuple/tuple.hpp>
26 // used by at least frontends/qt2/QPref.C
27 const long maxlastfiles = 20;
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)
34 5. general purpose session info in the form of key/value pairs
38 /** base class for all sections in the session file
40 class SessionSection : boost::noncopyable {
44 virtual ~SessionSection() {}
46 /// read section from std::istream
47 virtual void read(std::istream & is) = 0;
49 /// write to std::ostream
50 virtual void write(std::ostream & os) const = 0;
54 class LastFilesSection : SessionSection
58 typedef std::deque<std::string> LastFiles;
62 explicit LastFilesSection(unsigned int num = 4);
65 void read(std::istream & is);
68 void write(std::ostream & os) const;
70 /// Return lastfiles container (deque)
71 LastFiles const lastFiles() const { return lastfiles; }
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.
80 void add(std::string const & file);
83 /// Default number of lastfiles.
84 unsigned int const default_num_last_files;
86 /// Max number of lastfiles.
87 unsigned int const absolute_max_last_files;
89 /// a list of lastfiles
92 /// number of files in the lastfiles list.
93 unsigned int num_lastfiles;
95 /** Used by the constructor to set the number of stored last files.
96 @param num the number of lastfiles to set.
98 void setNumberOfLastFiles(unsigned int num);
102 class LastOpenedSection : SessionSection
106 typedef std::vector<std::string> LastOpened;
110 void read(std::istream & is);
113 void write(std::ostream & os) const;
115 /// Return lastopened container (vector)
116 LastOpened const getfiles() const { return lastopened; }
118 /** add file to lastopened file list
119 @param file filename to add
121 void add(std::string const & file);
123 /** clear lastopened file list
128 /// a list of lastopened files
129 LastOpened lastopened;
133 class LastFilePosSection : SessionSection
137 typedef boost::tuple<pit_type, pos_type> FilePos;
140 typedef std::map<std::string, FilePos> FilePosMap;
144 LastFilePosSection() : num_lastfilepos(100) {}
147 void read(std::istream & is);
150 void write(std::ostream & os) const;
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.
156 void save(std::string const & fname, FilePos pos);
158 /** load saved cursor position from the fname entry in the filepos map
159 @param fname file entry for which to load position information
161 FilePos load(std::string const & fname) const;
164 /// default number of lastfilepos to save */
165 unsigned int const num_lastfilepos;
168 /// a map of file positions
169 FilePosMap lastfilepos;
173 class BookmarksSection : SessionSection
180 std::string filename;
181 /// Cursor paragraph Id
186 Bookmark() : par_id(0), par_pos(0) {}
188 Bookmark(std::string const & f, int id, pos_type pos)
189 : filename(f), par_id(id), par_pos(pos) {}
193 typedef std::deque<Bookmark> BookmarkList;
196 /// constructor, set max_bookmarks
197 /// allow 20 regular bookmarks
198 BookmarksSection() : bookmarks(0), max_bookmarks(20) {}
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);
204 /// return bookmark, return temp_bookmark if i==0
205 Bookmark const & bookmark(unsigned int i) const;
207 /// does the given bookmark have a saved position ?
208 bool isValid(unsigned int i) const;
211 unsigned int size() const { return bookmarks.size(); }
213 /// clear all bookmarks
214 void clear() { bookmarks.clear(); }
217 void read(std::istream & is);
220 void write(std::ostream & os) const;
222 /** return bookmark list. Non-const container is used since
223 bookmarks will be cleaned after use.
225 BookmarkList & load() { return bookmarks; }
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;
233 /// a list of bookmarks
234 BookmarkList bookmarks;
237 unsigned int const max_bookmarks;
241 class ToolbarSection : SessionSection
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.
252 state(ON), location(NOTSET) { }
254 ToolbarInfo(int s, int loc) :
255 state(static_cast<State>(s)), location(static_cast<Location>(loc)) { }
267 /// location: this can be intepreted differently.
278 /// potentially, icons
281 /// info for each toolbar
282 typedef std::map<std::string, ToolbarInfo> ToolbarMap;
286 void read(std::istream & is);
289 void write(std::ostream & os) const;
291 /// return reference to toolbar info, create a new one if needed
292 ToolbarInfo & load(std::string const & name);
295 /// toolbar information
300 class SessionInfoSection : SessionSection
304 typedef std::map<std::string, std::string> MiscInfo;
308 void read(std::istream & is);
311 void write(std::ostream & os) const;
314 @param key key of the value to store
315 @param value value, a string without newline ('\n')
317 void save(std::string const & key, std::string const & value);
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.
324 std::string const load(std::string const & key, bool release = true);
327 /// a map to save session info
328 MiscInfo sessioninfo;
332 class Session : boost::noncopyable {
335 /** Read the session file.
336 @param num length of lastfiles
338 explicit Session(unsigned int num = 4);
340 /** Write the session file.
342 void writeFile() const;
345 LastFilesSection & lastFiles() { return last_files; }
348 LastFilesSection const & lastFiles() const { return last_files; }
351 LastOpenedSection & lastOpened() { return last_opened; }
354 LastOpenedSection const & lastOpened() const { return last_opened; }
357 LastFilePosSection & lastFilePos() { return last_file_pos; }
360 LastFilePosSection const & lastFilePos() const { return last_file_pos; }
363 BookmarksSection & bookmarks() { return bookmarks_; }
366 BookmarksSection const & bookmarks() const { return bookmarks_; }
369 ToolbarSection & toolbars() { return toolbars_; }
372 ToolbarSection const & toolbars() const { return toolbars_; }
375 SessionInfoSection & sessionInfo() { return session_info; }
378 SessionInfoSection const & sessionInfo() const { return session_info; }
381 /// file to save session, determined in the constructor.
382 std::string session_file;
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.
392 LastFilesSection last_files;
395 LastOpenedSection last_opened;
398 LastFilePosSection last_file_pos;
401 BookmarksSection bookmarks_;
404 ToolbarSection toolbars_;
407 SessionInfoSection session_info;