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 {
43 /// read section from std::istream
44 virtual void read(std::istream & is) = 0;
46 /// write to std::ostream
47 virtual void write(std::ostream & os) const = 0;
51 class LastFilesSection : SessionSection
55 typedef std::deque<std::string> LastFiles;
59 explicit LastFilesSection(unsigned int num = 4);
62 void read(std::istream & is);
65 void write(std::ostream & os) const;
67 /// Return lastfiles container (deque)
68 LastFiles const lastFiles() const { return lastfiles; }
70 /** Insert #file# into the lastfile dequeue.
71 This funtion inserts #file# into the last files list. If the file
72 already exists it is moved to the top of the list, else exist it
73 is placed on the top of the list. If the list is full the last
74 file in the list is popped from the end.
75 @param file the file to insert in the lastfile list.
77 void add(std::string const & file);
80 /// Default number of lastfiles.
81 unsigned int const default_num_last_files;
83 /// Max number of lastfiles.
84 unsigned int const absolute_max_last_files;
86 /// a list of lastfiles
89 /// number of files in the lastfiles list.
90 unsigned int num_lastfiles;
92 /** Used by the constructor to set the number of stored last files.
93 @param num the number of lastfiles to set.
95 void setNumberOfLastFiles(unsigned int num);
99 class LastOpenedSection : SessionSection
103 typedef std::vector<std::string> LastOpened;
107 void read(std::istream & is);
110 void write(std::ostream & os) const;
112 /// Return lastopened container (vector)
113 LastOpened const getfiles() const { return lastopened; }
115 /** add file to lastopened file list
116 @param file filename to add
118 void add(std::string const & file);
120 /** clear lastopened file list
125 /// a list of lastopened files
126 LastOpened lastopened;
130 class LastFilePosSection : SessionSection
134 typedef boost::tuple<pit_type, pos_type> FilePos;
137 typedef std::map<std::string, FilePos> FilePosMap;
141 LastFilePosSection() : num_lastfilepos(100) {}
144 void read(std::istream & is);
147 void write(std::ostream & os) const;
149 /** add cursor position to the fname entry in the filepos map
150 @param fname file entry for which to save position information
151 @param pos position of the cursor when the file is closed.
153 void save(std::string const & fname, FilePos pos);
155 /** load saved cursor position from the fname entry in the filepos map
156 @param fname file entry for which to load position information
158 FilePos load(std::string const & fname) const;
161 /// default number of lastfilepos to save */
162 unsigned int const num_lastfilepos;
165 /// a map of file positions
166 FilePosMap lastfilepos;
170 class BookmarksSection : SessionSection
174 typedef boost::tuple<unsigned int, std::string, unsigned int, pos_type> Bookmark;
177 typedef std::vector<Bookmark> BookmarkList;
181 void read(std::istream & is);
184 void write(std::ostream & os) const;
187 @bookmark bookmark to be saved
189 void save(Bookmark const & bookmark);
191 /** return bookmark list. Non-const container is used since
192 bookmarks will be cleaned after use.
194 BookmarkList & load() { return bookmarks; }
197 /// a list of bookmarks
198 BookmarkList bookmarks;
202 class SessionInfoSection : SessionSection
206 typedef std::map<std::string, std::string> MiscInfo;
210 void read(std::istream & is);
213 void write(std::ostream & os) const;
216 @param key key of the value to store
217 @param value value, a string without newline ('\n')
219 void save(std::string const & key, std::string const & value);
221 /** load session info
222 @param key a key to extract value from the session file
223 @param release whether or not clear the value. Default to true
224 since most of such values are supposed to be used only once.
226 std::string const load(std::string const & key, bool release = true);
229 /// a map to save session info
230 MiscInfo sessioninfo;
234 class Session : boost::noncopyable {
237 /** Read the session file.
238 @param num length of lastfiles
240 explicit Session(unsigned int num = 4);
242 /** Write the session file.
244 void writeFile() const;
247 LastFilesSection & LastFiles() { return last_files; }
250 LastFilesSection const & LastFiles() const { return last_files; }
253 LastOpenedSection & LastOpened() { return last_opened; }
256 LastOpenedSection const & LastOpened() const { return last_opened; }
259 LastFilePosSection & LastFilePos() { return last_file_pos; }
262 LastFilePosSection const & LastFilePos() const { return last_file_pos; }
265 BookmarksSection & Bookmarks() { return bookmarks; }
268 BookmarksSection const & Bookmarks() const { return bookmarks; }
271 SessionInfoSection & SessionInfo() { return session_info; }
274 SessionInfoSection const & SessionInfo() const { return session_info; }
277 /// file to save session, determined in the constructor.
278 std::string session_file;
280 /** Read the session file.
281 Reads the #.lyx/session# at the beginning of the LyX session.
282 This will read the session file (usually #.lyx/session#).
283 @param file the file containing the session.
288 LastFilesSection last_files;
291 LastOpenedSection last_opened;
294 LastFilePosSection last_file_pos;
297 BookmarksSection bookmarks;
300 SessionInfoSection session_info;