]> git.lyx.org Git - lyx.git/blob - src/Session.cpp
Paragraph:
[lyx.git] / src / Session.cpp
1 /**
2  * \file Session.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author Bo Peng
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "Session.h"
15
16 #include "support/debug.h"
17 #include "support/FileNameList.h"
18 #include "support/filetools.h"
19 #include "support/Package.h"
20
21 #include <fstream>
22 #include <sstream>
23 #include <algorithm>
24 #include <iterator>
25
26 using namespace std;
27 using namespace lyx::support;
28
29 namespace {
30
31 string const sec_lastfiles = "[recent files]";
32 string const sec_lastfilepos = "[cursor positions]";
33 string const sec_lastopened = "[last opened files]";
34 string const sec_bookmarks = "[bookmarks]";
35 string const sec_session = "[session info]";
36 string const sec_toolbars = "[toolbars]";
37
38 } // anon namespace
39
40
41 namespace lyx {
42
43
44 LastFilesSection::LastFilesSection(unsigned int num) :
45         default_num_last_files(4),
46         absolute_max_last_files(100)
47 {
48         setNumberOfLastFiles(num);
49 }
50
51
52 void LastFilesSection::read(istream & is)
53 {
54         string tmp;
55         do {
56                 char c = is.peek();
57                 if (c == '[')
58                         break;
59                 getline(is, tmp);
60                 FileName const file(tmp);
61                 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ' || !file.isAbsolute())
62                         continue;
63
64                 // read lastfiles
65                 if (file.exists() && !file.isDirectory()
66                     && lastfiles.size() < num_lastfiles)
67                         lastfiles.push_back(file);
68                 else
69                         LYXERR(Debug::INIT, "LyX: Warning: Ignore last file: " << tmp);
70         } while (is.good());
71 }
72
73
74 void LastFilesSection::write(ostream & os) const
75 {
76         os << '\n' << sec_lastfiles << '\n';
77         copy(lastfiles.begin(), lastfiles.end(),
78              ostream_iterator<FileName>(os, "\n"));
79 }
80
81
82 void LastFilesSection::add(FileName const & file)
83 {
84         // If file already exist, delete it and reinsert at front.
85         LastFiles::iterator it = find(lastfiles.begin(), lastfiles.end(), file);
86         if (it != lastfiles.end())
87                 lastfiles.erase(it);
88         lastfiles.push_front(file);
89         if (lastfiles.size() > num_lastfiles)
90                 lastfiles.pop_back();
91 }
92
93
94 void LastFilesSection::setNumberOfLastFiles(unsigned int no)
95 {
96         if (0 < no && no <= absolute_max_last_files)
97                 num_lastfiles = no;
98         else {
99                 LYXERR(Debug::INIT, "LyX: session: too many last files\n"
100                         << "\tdefault (=" << default_num_last_files << ") used.");
101                 num_lastfiles = default_num_last_files;
102         }
103 }
104
105
106 void LastOpenedSection::read(istream & /*is*/)
107 {
108         lastopened.clear();
109         FileNameList list;// = theApp()->fileNameListFromSession("last_opened");
110         for (size_t i = 0; i != list.size(); ++i) {
111                 FileName const & file = list[i];
112                 if (!file.isAbsolute() || !file.exists() || file.isDirectory())
113                         LYXERR(Debug::INIT, "Warning: invalid last opened file: " << file);
114                 else
115                         lastopened.push_back(file);
116         }
117 }
118
119
120 void LastOpenedSection::write(ostream & /*os*/) const
121 {
122         //theApp()->toSession(lastopened);
123 }
124
125
126 void LastOpenedSection::add(FileName const & file)
127 {
128         lastopened.push_back(file);
129 }
130
131
132 void LastOpenedSection::clear()
133 {
134         lastopened.clear();
135 }
136
137
138 void LastFilePosSection::read(istream & is)
139 {
140         string tmp;
141         do {
142                 char c = is.peek();
143                 if (c == '[')
144                         break;
145                 getline(is, tmp);
146                 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
147                         continue;
148
149                 try {
150                         // read lastfilepos
151                         // pos, file\n
152                         FilePos filepos;
153                         string fname;
154                         istringstream itmp(tmp);
155                         itmp >> filepos.pit;
156                         itmp.ignore(2);  // ignore ", "
157                         itmp >> filepos.pos;
158                         itmp.ignore(2);  // ignore ", "
159                         getline(itmp, fname);
160                         FileName const file(fname);
161                         if (!file.isAbsolute())
162                                 continue;
163                         if (file.exists() && !file.isDirectory()
164                             && lastfilepos.size() < num_lastfilepos)
165                                 lastfilepos[file] = filepos;
166                         else
167                                 LYXERR(Debug::INIT, "LyX: Warning: Ignore pos of last file: " << fname);
168                 } catch (...) {
169                         LYXERR(Debug::INIT, "LyX: Warning: unknown pos of last file: " << tmp);
170                 }
171         } while (is.good());
172 }
173
174
175 void LastFilePosSection::write(ostream & os) const
176 {
177         os << '\n' << sec_lastfilepos << '\n';
178         for (FilePosMap::const_iterator file = lastfilepos.begin();
179                 file != lastfilepos.end(); ++file) {
180                 os << file->second.pit << ", " << file->second.pos << ", "
181                    << file->first << '\n';
182         }
183 }
184
185
186 void LastFilePosSection::save(FileName const & fname, FilePos const & pos)
187 {
188         lastfilepos[fname] = pos;
189 }
190
191
192 LastFilePosSection::FilePos LastFilePosSection::load(FileName const & fname) const
193 {
194         FilePosMap::const_iterator entry = lastfilepos.find(fname);
195         // Has position information, return it.
196         if (entry != lastfilepos.end())
197                 return entry->second;
198         // Not found, return the first paragraph
199         return FilePos();
200 }
201
202
203 void BookmarksSection::clear()
204 {
205         // keep bookmark[0], the temporary one
206         bookmarks.resize(1);
207         bookmarks.resize(max_bookmarks + 1);
208 }
209
210
211 void BookmarksSection::read(istream & is)
212 {
213         string tmp;
214         do {
215                 char c = is.peek();
216                 if (c == '[')
217                         break;
218                 getline(is, tmp);
219                 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
220                         continue;
221
222                 try {
223                         // read bookmarks
224                         // idx, pit, pos, file\n
225                         unsigned int idx;
226                         pit_type pit;
227                         pos_type pos;
228                         string fname;
229                         istringstream itmp(tmp);
230                         itmp >> idx;
231                         itmp.ignore(2);  // ignore ", "
232                         itmp >> pit;
233                         itmp.ignore(2);  // ignore ", "
234                         itmp >> pos;
235                         itmp.ignore(2);  // ignore ", "
236                         getline(itmp, fname);
237                         FileName const file(fname);
238                         if (!file.isAbsolute())
239                                 continue;
240                         // only load valid bookmarks
241                         if (file.exists() && !file.isDirectory() && idx <= max_bookmarks)
242                                 bookmarks[idx] = Bookmark(file, pit, pos, 0, 0);
243                         else
244                                 LYXERR(Debug::INIT, "LyX: Warning: Ignore bookmark of file: " << fname);
245                 } catch (...) {
246                         LYXERR(Debug::INIT, "LyX: Warning: unknown Bookmark info: " << tmp);
247                 }
248         } while (is.good());
249 }
250
251
252 void BookmarksSection::write(ostream & os) const
253 {
254         os << '\n' << sec_bookmarks << '\n';
255         for (size_t i = 1; i <= max_bookmarks; ++i) {
256                 if (isValid(i))
257                         os << i << ", "
258                            << bookmarks[i].bottom_pit << ", "
259                            << bookmarks[i].bottom_pos << ", "
260                            << bookmarks[i].filename << '\n';
261         }
262 }
263
264
265 void BookmarksSection::save(FileName const & fname,
266         pit_type bottom_pit, pos_type bottom_pos,
267         int top_id, pos_type top_pos, unsigned int idx)
268 {
269         // silently ignore bookmarks when idx is out of range
270         if (idx <= max_bookmarks)
271                 bookmarks[idx] = Bookmark(fname, bottom_pit, bottom_pos, top_id, top_pos);
272 }
273
274
275 bool BookmarksSection::isValid(unsigned int i) const
276 {
277         return i <= max_bookmarks && !bookmarks[i].filename.empty();
278 }
279
280
281 BookmarksSection::Bookmark const & BookmarksSection::bookmark(unsigned int i) const
282 {
283         return bookmarks[i];
284 }
285
286
287 Session::Session(unsigned int num) :
288         last_files(num)
289 {
290         // locate the session file
291         // note that the session file name 'session' is hard-coded
292         session_file = FileName(addName(package().user_support().absFilename(), "session"));
293         //
294         readFile();
295 }
296
297
298 void Session::readFile()
299 {
300         // we will not complain if we can't find session_file nor will
301         // we issue a warning. (Lgb)
302         ifstream is(session_file.toFilesystemEncoding().c_str());
303         string tmp;
304
305         while (getline(is, tmp)) {
306                 // Ignore comments, empty line or line stats with ' '
307                 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
308                         continue;
309
310                 // Determine section id
311                 if (tmp == sec_lastfiles)
312                         lastFiles().read(is);
313                 else if (tmp == sec_lastopened)
314                         lastOpened().read(is);
315                 else if (tmp == sec_lastfilepos)
316                         lastFilePos().read(is);
317                 else if (tmp == sec_bookmarks)
318                         bookmarks().read(is);
319                 else
320                         LYXERR(Debug::INIT, "LyX: Warning: unknown Session section: " << tmp);
321         }
322 }
323
324
325 void Session::writeFile() const
326 {
327         ofstream os(session_file.toFilesystemEncoding().c_str());
328         if (os) {
329                 os << "## Automatically generated lyx session file \n"
330                     << "## Editing this file manually may cause lyx to crash.\n";
331
332                 lastFiles().write(os);
333                 lastOpened().write(os);
334                 lastFilePos().write(os);
335                 bookmarks().write(os);
336         } else
337                 LYXERR(Debug::INIT, "LyX: Warning: unable to save Session: "
338                        << session_file);
339 }
340
341 }