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
177 typedef boost::tuple<unsigned int, std::string, unsigned int, pos_type> Bookmark;
180 typedef std::vector<Bookmark> BookmarkList;
184 void read(std::istream & is);
187 void write(std::ostream & os) const;
190 @bookmark bookmark to be saved
192 void save(Bookmark const & bookmark);
194 /** return bookmark list. Non-const container is used since
195 bookmarks will be cleaned after use.
197 BookmarkList & load() { return bookmarks; }
200 /// a list of bookmarks
201 BookmarkList bookmarks;
205 class SessionInfoSection : SessionSection
209 typedef std::map<std::string, std::string> MiscInfo;
213 void read(std::istream & is);
216 void write(std::ostream & os) const;
219 @param key key of the value to store
220 @param value value, a string without newline ('\n')
222 void save(std::string const & key, std::string const & value);
224 /** load session info
225 @param key a key to extract value from the session file
226 @param release whether or not clear the value. Default to true
227 since most of such values are supposed to be used only once.
229 std::string const load(std::string const & key, bool release = true);
232 /// a map to save session info
233 MiscInfo sessioninfo;
237 class Session : boost::noncopyable {
240 /** Read the session file.
241 @param num length of lastfiles
243 explicit Session(unsigned int num = 4);
245 /** Write the session file.
247 void writeFile() const;
250 LastFilesSection & LastFiles() { return last_files; }
253 LastFilesSection const & LastFiles() const { return last_files; }
256 LastOpenedSection & LastOpened() { return last_opened; }
259 LastOpenedSection const & LastOpened() const { return last_opened; }
262 LastFilePosSection & LastFilePos() { return last_file_pos; }
265 LastFilePosSection const & LastFilePos() const { return last_file_pos; }
268 BookmarksSection & Bookmarks() { return bookmarks; }
271 BookmarksSection const & Bookmarks() const { return bookmarks; }
274 SessionInfoSection & SessionInfo() { return session_info; }
277 SessionInfoSection const & SessionInfo() const { return session_info; }
280 /// file to save session, determined in the constructor.
281 std::string session_file;
283 /** Read the session file.
284 Reads the #.lyx/session# at the beginning of the LyX session.
285 This will read the session file (usually #.lyx/session#).
286 @param file the file containing the session.
291 LastFilesSection last_files;
294 LastOpenedSection last_opened;
297 LastFilePosSection last_file_pos;
300 BookmarksSection bookmarks;
303 SessionInfoSection session_info;