]> git.lyx.org Git - lyx.git/blob - src/session.C
* src/session.C: robustify session::read (lyx will start with corrupted session...
[lyx.git] / src / session.C
1 /**
2  * \file session.C
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 #include "debug.h"
16 #include "support/package.h"
17 #include "support/filetools.h"
18
19 #include <boost/filesystem/operations.hpp>
20
21 #include <fstream>
22 #include <sstream>
23 #include <algorithm>
24 #include <iterator>
25
26 using lyx::support::addName;
27 using lyx::support::package;
28
29 namespace fs = boost::filesystem;
30
31 using std::vector;
32 using std::getline;
33 using std::string;
34 using std::ifstream;
35 using std::ofstream;
36 using std::istream;
37 using std::ostream;
38 using std::endl;
39 using std::istringstream;
40 using std::copy;
41 using std::find;
42 using std::ostream_iterator;
43
44 namespace {
45
46 string const sec_lastfiles = "[recent files]";
47 string const sec_lastfilepos = "[cursor positions]";
48 string const sec_lastopened = "[last opened files]";
49 string const sec_bookmarks = "[bookmarks]";
50 string const sec_session = "[session info]";
51 string const sec_toolbars = "[toolbars]";
52
53 } // anon namespace
54
55
56 namespace lyx {
57
58 LastFilesSection::LastFilesSection(unsigned int num) :
59         default_num_last_files(4),
60         absolute_max_last_files(100)
61 {
62         setNumberOfLastFiles(num);
63 }
64
65
66 void LastFilesSection::read(istream & is)
67 {
68         string tmp;
69         do {
70                 char c = is.peek();
71                 if (c == '[')
72                         break;
73                 getline(is, tmp);
74                 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
75                         continue;
76                 
77                 // read lastfiles
78                 if (fs::exists(tmp) && !fs::is_directory(tmp) && lastfiles.size() < num_lastfiles)
79                         lastfiles.push_back(tmp);
80                 else 
81                         lyxerr << "LyX: Warning: Ignore last file: " << tmp << endl;
82         } while (is.good());
83 }
84
85
86 void LastFilesSection::write(ostream & os) const
87 {
88         os << '\n' << sec_lastfiles << '\n';
89         copy(lastfiles.begin(), lastfiles.end(),
90              ostream_iterator<string>(os, "\n"));
91 }
92
93
94 void LastFilesSection::add(string const & file)
95 {
96         // If file already exist, delete it and reinsert at front.
97         LastFiles::iterator it = find(lastfiles.begin(), lastfiles.end(), file);
98         if (it != lastfiles.end())
99                 lastfiles.erase(it);
100         lastfiles.push_front(file);
101         if (lastfiles.size() > num_lastfiles)
102                 lastfiles.pop_back();
103 }
104
105
106 void LastFilesSection::setNumberOfLastFiles(unsigned int no)
107 {
108         if (0 < no && no <= absolute_max_last_files)
109                 num_lastfiles = no;
110         else {
111                 lyxerr << "LyX: session: too many last files\n"
112                        << "\tdefault (=" << default_num_last_files
113                        << ") used." << endl;
114                 num_lastfiles = default_num_last_files;
115         }
116 }
117
118
119 void LastOpenedSection::read(istream & is)
120 {
121         string tmp;
122         do {
123                 char c = is.peek();
124                 if (c == '[')
125                         break;
126                 getline(is, tmp);
127                 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
128                         continue;
129
130                 if (fs::exists(tmp) && !fs::is_directory(tmp))
131                         lastopened.push_back(tmp);
132                 else
133                         lyxerr << "LyX: Warning: Ignore last opened file: " << tmp << endl;
134         } while (is.good());
135 }
136
137
138 void LastOpenedSection::write(ostream & os) const
139 {
140         os << '\n' << sec_lastopened << '\n';
141         copy(lastopened.begin(), lastopened.end(),
142              ostream_iterator<string>(os, "\n"));
143 }
144
145
146 void LastOpenedSection::add(string const & file)
147 {
148         lastopened.push_back(file);
149 }
150
151
152 void LastOpenedSection::clear()
153 {
154         lastopened.clear();
155 }
156
157
158 void LastFilePosSection::read(istream & is)
159 {
160         string tmp;
161         do {
162                 char c = is.peek();
163                 if (c == '[')
164                         break;
165                 getline(is, tmp);
166                 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
167                         continue;
168
169                 try {
170                         // read lastfilepos
171                         // pos, file\n
172                         pit_type pit;
173                         pos_type pos;
174                         string fname;
175                         istringstream itmp(tmp);
176                         itmp >> pit;
177                         itmp.ignore(2);  // ignore ", "
178                         itmp >> pos;
179                         itmp.ignore(2);  // ignore ", "
180                         itmp >> fname;
181                         if (fs::exists(fname) && !fs::is_directory(fname) && lastfilepos.size() < num_lastfilepos)
182                                 lastfilepos[fname] = boost::tie(pit, pos);
183                         else
184                                 lyxerr << "LyX: Warning: Ignore pos of last file: " << fname << endl;
185                 } catch (...) {
186                         lyxerr << "LyX: Warning: unknown pos of last file: " << tmp << endl;
187                 }
188         } while (is.good());
189 }
190
191
192 void LastFilePosSection::write(ostream & os) const
193 {
194         os << '\n' << sec_lastfilepos << '\n';
195         for (FilePosMap::const_iterator file = lastfilepos.begin();
196                 file != lastfilepos.end(); ++file) {
197                 os << file->second.get<0>() << ", "
198                     << file->second.get<1>() << ", "
199                     << file->first << '\n';
200         }
201 }
202
203
204 void LastFilePosSection::save(string const & fname, FilePos pos)
205 {
206         lastfilepos[fname] = pos;
207 }
208
209
210 LastFilePosSection::FilePos LastFilePosSection::load(string const & fname) const
211 {
212         FilePosMap::const_iterator entry = lastfilepos.find(fname);
213         // Has position information, return it.
214         if (entry != lastfilepos.end())
215                 return entry->second;
216         // Not found, return the first paragraph
217         else
218                 return 0;
219 }
220
221
222 void BookmarksSection::read(istream & is)
223 {
224         string tmp;
225         do {
226                 char c = is.peek();
227                 if (c == '[')
228                         break;
229                 getline(is, tmp);
230                 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
231                         continue;
232
233                 try {
234                         // read bookmarks
235                         // id, pos, file\n
236                         unsigned int id;
237                         pos_type pos;
238                         string fname;
239                         istringstream itmp(tmp);
240                         itmp >> id;
241                         itmp.ignore(2);  // ignore ", "
242                         itmp >> pos;
243                         itmp.ignore(2);  // ignore ", "
244                         itmp >> fname;
245                         // only load valid bookmarks
246                         if (fs::exists(fname) && !fs::is_directory(fname) && bookmarks.size() < max_bookmarks)
247                                 bookmarks.push_back(Bookmark(fname, id, pos));
248                         else 
249                                 lyxerr << "LyX: Warning: Ignore bookmark of file: " << fname << endl;
250                 } catch (...) {
251                         lyxerr << "LyX: Warning: unknown Bookmark info: " << tmp << endl;
252                 }
253         } while (is.good());
254 }
255
256
257 void BookmarksSection::write(ostream & os) const
258 {
259         os << '\n' << sec_bookmarks << '\n';
260         for (size_t i = 0; i < bookmarks.size(); ++i) {
261                 os << bookmarks[i].par_id << ", "
262                    << bookmarks[i].par_pos << ", "
263                    << bookmarks[i].filename << '\n';
264         }
265 }
266
267
268 void BookmarksSection::save(std::string const & fname, int par_id, pos_type par_pos, bool persistent)
269 {
270         if (persistent) {
271                 bookmarks.push_front(Bookmark(fname, par_id, par_pos));
272                 if (bookmarks.size() > max_bookmarks)
273                         bookmarks.pop_back();
274                 }
275         else
276                 temp_bookmark = Bookmark(fname, par_id, par_pos);
277 }
278
279
280 bool BookmarksSection::isValid(unsigned int i) const
281 {
282         // i == 0, or in the queue
283         return i <= bookmarks.size();
284 }
285
286
287 BookmarksSection::Bookmark const & BookmarksSection::bookmark(unsigned int i) const
288 {
289         if (i == 0)
290                 return temp_bookmark;
291         else
292                 return bookmarks[i-1];
293 }
294
295
296 void ToolbarSection::read(istream & is)
297 {
298         string tmp;
299         do {
300                 char c = is.peek();
301                 if (c == '[')
302                         break;
303                 getline(is, tmp);
304                 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
305                         continue;
306
307                 try {
308                         // Read session info, saved as key/value pairs
309                         // would better yell if pos returns npos
310                         string::size_type pos = tmp.find_first_of(" = ");
311                         // silently ignore lines without " = "
312                         if (pos != string::npos) {
313                                 string key = tmp.substr(0, pos);
314                                 int state;
315                                 int location;
316                                 istringstream value(tmp.substr(pos + 3));
317                                 value >> state;
318                                 value.ignore(1); // ignore " "
319                                 value >> location;
320                                 toolbars[key] = ToolbarInfo(state, location);
321                         } else 
322                                 lyxerr << "LyX: Warning: Ignore toolbar info: " << tmp << endl;
323                 } catch (...) {
324                         lyxerr << "LyX: Warning: unknown Toolbar info: " << tmp << endl;
325                 }
326         } while (is.good());
327 }
328
329
330 void ToolbarSection::write(ostream & os) const
331 {
332         os << '\n' << sec_toolbars << '\n';
333         for (ToolbarMap::const_iterator tb = toolbars.begin();
334                 tb != toolbars.end(); ++tb) {
335                 os << tb->first << " = "
336                   << static_cast<int>(tb->second.state) << " "
337                   << static_cast<int>(tb->second.location) << '\n';
338         }
339 }
340
341
342 ToolbarSection::ToolbarInfo & ToolbarSection::load(string const & name)
343 {
344         return toolbars[name];
345 }
346
347
348 void SessionInfoSection::read(istream & is)
349 {
350         string tmp;
351         do {
352                 char c = is.peek();
353                 if (c == '[')
354                         break;
355                 getline(is, tmp);
356                 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
357                         continue;
358
359                 try {
360                         // Read session info, saved as key/value pairs
361                         // would better yell if pos returns npos
362                         string::size_type pos = tmp.find_first_of(" = ");
363                         // silently ignore lines without " = "
364                         if (pos != string::npos) {
365                                 string key = tmp.substr(0, pos);
366                                 string value = tmp.substr(pos + 3);
367                                 sessioninfo[key] = value;
368                         } else
369                                 lyxerr << "LyX: Warning: Ignore session info: " << tmp << endl;
370                 } catch (...) {
371                         lyxerr << "LyX: Warning: unknown Session info: " << tmp << endl;
372                 }
373         } while (is.good());
374 }
375
376
377 void SessionInfoSection::write(ostream & os) const
378 {
379         os << '\n' << sec_session << '\n';
380         for (MiscInfo::const_iterator val = sessioninfo.begin();
381                 val != sessioninfo.end(); ++val) {
382                 os << val->first << " = " << val->second << '\n';
383         }
384 }
385
386
387 void SessionInfoSection::save(string const & key, string const & value)
388 {
389         sessioninfo[key] = value;
390 }
391
392
393 string const SessionInfoSection::load(string const & key, bool release)
394 {
395         MiscInfo::const_iterator pos = sessioninfo.find(key);
396         string value;
397         if (pos != sessioninfo.end())
398                 value = pos->second;
399         if (release)
400                 sessioninfo.erase(key);
401         return value;
402 }
403
404
405 Session::Session(unsigned int num) :
406         last_files(num)
407 {
408         // locate the session file
409         // note that the session file name 'session' is hard-coded
410         session_file = addName(package().user_support(), "session");
411         //
412         readFile();
413 }
414
415
416 void Session::readFile()
417 {
418         // we will not complain if we can't find session_file nor will
419         // we issue a warning. (Lgb)
420         ifstream is(session_file.c_str());
421         string tmp;
422
423         while (getline(is, tmp)) {
424                 // Ignore comments, empty line or line stats with ' '
425                 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
426                         continue;
427
428                 // Determine section id
429                 if (tmp == sec_lastfiles)
430                         lastFiles().read(is);
431                 else if (tmp == sec_lastopened)
432                         lastOpened().read(is);
433                 else if (tmp == sec_lastfilepos)
434                         lastFilePos().read(is);
435                 else if (tmp == sec_bookmarks)
436                         bookmarks().read(is);
437                 else if (tmp == sec_toolbars)
438                         toolbars().read(is);
439                 else if (tmp == sec_session)
440                         sessionInfo().read(is);
441                 else
442                         lyxerr << "LyX: Warning: unknown Session section: " << tmp << endl;
443         }
444 }
445
446
447 void Session::writeFile() const
448 {
449         ofstream os(session_file.c_str());
450         if (os) {
451                 os << "## Automatically generated lyx session file \n"
452                     << "## Editing this file manually may cause lyx to crash.\n";
453
454                 lastFiles().write(os);
455                 lastOpened().write(os);
456                 lastFilePos().write(os);
457                 bookmarks().write(os);
458                 toolbars().write(os);
459                 sessionInfo().write(os);
460         } else
461                 lyxerr << "LyX: Warning: unable to save Session: "
462                        << session_file << endl;
463 }
464
465 }