]> git.lyx.org Git - lyx.git/blob - src/session.h
do not define boost::throw_exceptions if we are compiling with exceptions
[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 class Session : boost::noncopyable {
39
40 public:
41         ///
42         typedef boost::tuple<lyx::pit_type, lyx::pos_type> FilePos;
43         ///
44         typedef std::map<std::string, FilePos> FilePosMap;
45         ///
46         typedef std::deque<std::string> LastFiles;
47         ///
48         typedef std::vector<std::string> LastOpened;
49         ///
50         typedef boost::tuple<unsigned int, std::string, unsigned int, lyx::pos_type> Bookmark;
51         ///
52         typedef std::vector<Bookmark> BookmarkList;
53         ///
54         typedef std::map<std::string, std::string> MiscInfo;
55
56 public:
57         /** Read the session file.
58             @param num length of lastfiles
59         */
60         explicit Session(unsigned int num = 4);
61
62         /** Write the session file.
63         */
64         void writeFile() const;
65         
66         /** Insert #file# into the lastfile dequeue.
67             This funtion inserts #file# into the last files list. If the file
68             already exists it is moved to the top of the list, else exist it
69             is placed on the top of the list. If the list is full the last
70             file in the list is popped from the end.
71             @param file the file to insert in the lastfile list.
72         */
73         void addLastFile(std::string const & file);
74         
75         /** add cursor position to the fname entry in the filepos map
76             @param fname file entry for which to save position information
77             @param pos position of the cursor when the file is closed.
78         */
79         void saveFilePosition(std::string const & fname, FilePos pos);
80         
81         /** clear lastopened file list
82          */
83         void clearLastOpenedFiles();
84         
85         /** set lastopened file list
86             @param files filenames of a list of opened files 
87         */
88         void setLastOpenedFiles(std::vector<std::string> const & files);
89         
90         /** load saved cursor position from the fname entry in the filepos map
91             @param fname file entry for which to load position information
92         */
93         FilePos loadFilePosition(std::string const & fname) const;
94         
95         /// Return lastfiles container (deque)
96         LastFiles const lastFiles() const { return lastfiles; }
97         
98         /// Return lastopened container (vector)
99         LastOpened const lastOpenedFiles() const { return lastopened; }
100
101         /** save a bookmark
102                 @bookmark bookmark to be saved
103         */
104         void saveBookmark(Bookmark const & bookmark);
105         
106         /** return bookmark list. Non-const container is used since 
107                 bookmarks will be cleaned after use.
108         */
109         BookmarkList & loadBookmarks() { return bookmarks; }
110
111         /** set session info 
112                 @param key key of the value to store
113                 @param value value, a string without newline ('\n')
114         */
115         void saveSessionInfo(std::string const & key, std::string const & value);
116
117         /** load session info
118                 @param key a key to extract value from the session file 
119                 @param release whether or not clear the value. Default to true
120                         since most of such values are supposed to be used only once.
121         */
122         std::string const loadSessionInfo(std::string const & key, bool release = true);        
123 private:
124         /// Default number of lastfiles.
125         unsigned int const default_num_last_files;
126
127         /// Max number of lastfiles.
128         unsigned int const absolute_max_last_files;
129
130         /// default number of lastfilepos to save */
131         unsigned int const num_lastfilepos;
132
133         /// file to save session, determined in the constructor.
134         std::string session_file;
135         
136         /// a list of lastfiles
137         LastFiles lastfiles;
138
139         /// a list of bookmarks
140         BookmarkList bookmarks;
141
142         /// a map to save session info
143         MiscInfo sessioninfo;
144         
145         /// number of files in the lastfiles list.
146         unsigned int num_lastfiles;
147         
148         /// a map of file positions
149         FilePosMap lastfilepos;
150         
151         /// a list of lastopened files
152         LastOpened lastopened;
153         
154         /** Read the session file.
155             Reads the #.lyx/session# at the beginning of the LyX session.
156             This will read the session file (usually #.lyx/session#). 
157             @param file the file containing the session.
158         */
159         void readFile();
160
161         /** Used by the constructor to set the number of stored last files.
162             @param num the number of lastfiles to set.
163         */
164         void setNumberOfLastFiles(unsigned int num);
165 };
166
167 }
168
169 #endif