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