]> git.lyx.org Git - lyx.git/blob - src/Session.cpp
Bulk cleanup/fix incorrect annotation at the end of namespaces.
[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 string const sec_authfiles = "[auth files]";
38
39 } // namespace
40
41
42 namespace lyx {
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                 if (tmp.empty() || tmp[0] == '#' || tmp[0] == ' ' || !FileName::isAbsolute(tmp))
61                         continue;
62
63                 // read lastfiles
64                 FileName const file(tmp);
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.insert(lastfiles.begin(), 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         string tmp;
109         do {
110                 char c = is.peek();
111                 if (c == '[')
112                         break;
113                 getline(is, tmp);
114                 if (tmp.empty() || tmp[0] == '#' || tmp[0] == ' ')
115                         continue;
116
117                 try {
118                         LastOpenedFile lof;
119                         istringstream itmp(tmp);
120                         itmp >> lof.active;
121                         itmp.ignore(2);  // ignore ", "
122                         string fname;
123                         getline(itmp, fname);
124                         if (!FileName::isAbsolute(fname))
125                                 continue;
126
127                         FileName const file(fname);
128                         if (file.exists() && !file.isDirectory()) {
129                                 lof.file_name = file;
130                                 lastopened.push_back(lof);
131                         } else {
132                                 LYXERR(Debug::INIT,
133                                         "LyX: Warning: Ignore last opened file: " << tmp);
134                         }
135                 } catch (...) {
136                         LYXERR(Debug::INIT,
137                                 "LyX: Warning: unknown state of last opened file: " << tmp);
138                 }
139         } while (is.good());
140 }
141
142
143 void LastOpenedSection::write(ostream & os) const
144 {
145         os << '\n' << sec_lastopened << '\n';
146         for (size_t i = 0; i < lastopened.size(); ++i)
147                 os << lastopened[i].active << ", " << lastopened[i].file_name << '\n';
148 }
149
150
151 void LastOpenedSection::add(FileName const & file, bool active)
152 {
153         LastOpenedFile lof(file, active);
154         // check if file is already recorded (this can happen
155         // with multiple buffer views). We do only record each
156         // file once, since we cannot restore multiple views
157         // currently, we even crash in some cases (see #9483).
158         // FIXME: Add session support for multiple views of
159         //        the same buffer (split-view etc.).
160         for (size_t i = 0; i < lastopened.size(); ++i) {
161                 if (lastopened[i].file_name == file)
162                         return;
163         }
164         lastopened.push_back(lof);
165 }
166
167
168 void LastOpenedSection::clear()
169 {
170         lastopened.clear();
171 }
172
173
174 void LastFilePosSection::read(istream & is)
175 {
176         string tmp;
177         do {
178                 char c = is.peek();
179                 if (c == '[')
180                         break;
181                 getline(is, tmp);
182                 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
183                         continue;
184
185                 try {
186                         // read lastfilepos
187                         // pos, file\n
188                         FilePos filepos;
189                         string fname;
190                         istringstream itmp(tmp);
191                         itmp >> filepos.pit;
192                         itmp.ignore(2);  // ignore ", "
193                         itmp >> filepos.pos;
194                         itmp.ignore(2);  // ignore ", "
195                         getline(itmp, fname);
196                         if (!FileName::isAbsolute(fname))
197                                 continue;
198                         FileName const file(fname);
199                         if (file.exists() && !file.isDirectory()
200                             && lastfilepos.size() < num_lastfilepos)
201                                 lastfilepos[file] = filepos;
202                         else
203                                 LYXERR(Debug::INIT, "LyX: Warning: Ignore pos of last file: " << fname);
204                 } catch (...) {
205                         LYXERR(Debug::INIT, "LyX: Warning: unknown pos of last file: " << tmp);
206                 }
207         } while (is.good());
208 }
209
210
211 void LastFilePosSection::write(ostream & os) const
212 {
213         os << '\n' << sec_lastfilepos << '\n';
214         for (FilePosMap::const_iterator file = lastfilepos.begin();
215                 file != lastfilepos.end(); ++file) {
216                 os << file->second.pit << ", " << file->second.pos << ", "
217                    << file->first << '\n';
218         }
219 }
220
221
222 void LastFilePosSection::save(FileName const & fname, FilePos const & pos)
223 {
224         lastfilepos[fname] = pos;
225 }
226
227
228 LastFilePosSection::FilePos LastFilePosSection::load(FileName const & fname) const
229 {
230         FilePosMap::const_iterator entry = lastfilepos.find(fname);
231         // Has position information, return it.
232         if (entry != lastfilepos.end())
233                 return entry->second;
234         // Not found, return the first paragraph
235         return FilePos();
236 }
237
238
239 void BookmarksSection::clear()
240 {
241         // keep bookmark[0], the temporary one
242         bookmarks.resize(1);
243         bookmarks.resize(max_bookmarks + 1);
244 }
245
246
247 void BookmarksSection::read(istream & is)
248 {
249         string tmp;
250         do {
251                 char c = is.peek();
252                 if (c == '[')
253                         break;
254                 getline(is, tmp);
255                 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
256                         continue;
257
258                 try {
259                         // read bookmarks
260                         // idx, pit, pos, file\n
261                         unsigned int idx;
262                         pit_type pit;
263                         pos_type pos;
264                         string fname;
265                         istringstream itmp(tmp);
266                         itmp >> idx;
267                         itmp.ignore(2);  // ignore ", "
268                         itmp >> pit;
269                         itmp.ignore(2);  // ignore ", "
270                         itmp >> pos;
271                         itmp.ignore(2);  // ignore ", "
272                         getline(itmp, fname);
273                         if (!FileName::isAbsolute(fname))
274                                 continue;
275                         FileName const file(fname);
276                         // only load valid bookmarks
277                         if (file.exists() && !file.isDirectory() && idx <= max_bookmarks)
278                                 bookmarks[idx] = Bookmark(file, pit, pos, 0, 0);
279                         else
280                                 LYXERR(Debug::INIT, "LyX: Warning: Ignore bookmark of file: " << fname);
281                 } catch (...) {
282                         LYXERR(Debug::INIT, "LyX: Warning: unknown Bookmark info: " << tmp);
283                 }
284         } while (is.good());
285 }
286
287
288 void BookmarksSection::write(ostream & os) const
289 {
290         os << '\n' << sec_bookmarks << '\n';
291         for (size_t i = 0; i <= max_bookmarks; ++i) {
292                 if (isValid(i))
293                         os << i << ", "
294                            << bookmarks[i].bottom_pit << ", "
295                            << bookmarks[i].bottom_pos << ", "
296                            << bookmarks[i].filename << '\n';
297         }
298 }
299
300
301 void BookmarksSection::save(FileName const & fname,
302         pit_type bottom_pit, pos_type bottom_pos,
303         int top_id, pos_type top_pos, unsigned int idx)
304 {
305         // silently ignore bookmarks when idx is out of range
306         if (idx <= max_bookmarks)
307                 bookmarks[idx] = Bookmark(fname, bottom_pit, bottom_pos, top_id, top_pos);
308 }
309
310
311 bool BookmarksSection::isValid(unsigned int i) const
312 {
313         return i <= max_bookmarks && !bookmarks[i].filename.empty();
314 }
315
316
317 bool BookmarksSection::hasValid() const
318 {
319         for (size_t i = 1; i <= size(); ++i) {
320                 if (isValid(i))
321                         return true;
322         }
323         return false;
324 }
325
326
327 BookmarksSection::Bookmark const & BookmarksSection::bookmark(unsigned int i) const
328 {
329         return bookmarks[i];
330 }
331
332
333 LastCommandsSection::LastCommandsSection(unsigned int num) :
334         default_num_last_commands(30),
335         absolute_max_last_commands(100)
336 {
337         setNumberOfLastCommands(num);
338 }
339
340
341 void LastCommandsSection::read(istream & is)
342 {
343         string tmp;
344         do {
345                 char c = is.peek();
346                 if (c == '[')
347                         break;
348                 getline(is, tmp);
349                 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
350                         continue;
351
352                 lastcommands.push_back(tmp);
353         } while (is.good());
354 }
355
356
357 void LastCommandsSection::write(ostream & os) const
358 {
359         os << '\n' << sec_lastcommands << '\n';
360         copy(lastcommands.begin(), lastcommands.end(),
361                 ostream_iterator<std::string>(os, "\n"));
362 }
363
364
365 void LastCommandsSection::setNumberOfLastCommands(unsigned int no)
366 {
367         if (0 < no && no <= absolute_max_last_commands)
368                 num_lastcommands = no;
369         else {
370                 LYXERR(Debug::INIT, "LyX: session: too many last commands\n"
371                         << "\tdefault (=" << default_num_last_commands << ") used.");
372                 num_lastcommands = default_num_last_commands;
373         }
374 }
375
376
377 void LastCommandsSection::add(std::string const & string)
378 {
379         lastcommands.push_back(string);
380 }
381
382
383 void LastCommandsSection::clear()
384 {
385         lastcommands.clear();
386 }
387
388
389 Session::Session(unsigned int num_last_files, unsigned int num_last_commands) :
390         last_files(num_last_files), last_commands(num_last_commands)
391 {
392         // locate the session file
393         // note that the session file name 'session' is hard-coded
394         session_file = FileName(addName(package().user_support().absFileName(), "session"));
395         //
396         readFile();
397 }
398
399
400 void Session::readFile()
401 {
402         // we will not complain if we can't find session_file nor will
403         // we issue a warning. (Lgb)
404         ifstream is(session_file.toFilesystemEncoding().c_str());
405         string tmp;
406
407         while (getline(is, tmp)) {
408                 // Ignore comments, empty line or line stats with ' '
409                 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
410                         continue;
411
412                 // Determine section id
413                 if (tmp == sec_lastfiles)
414                         lastFiles().read(is);
415                 else if (tmp == sec_lastopened)
416                         lastOpened().read(is);
417                 else if (tmp == sec_lastfilepos)
418                         lastFilePos().read(is);
419                 else if (tmp == sec_bookmarks)
420                         bookmarks().read(is);
421                 else if (tmp == sec_lastcommands)
422                         lastCommands().read(is);
423                 else if (tmp == sec_authfiles)
424                         authFiles().read(is);
425
426                 else
427                         LYXERR(Debug::INIT, "LyX: Warning: unknown Session section: " << tmp);
428         }
429 }
430
431
432 void Session::writeFile() const
433 {
434         ofstream os(session_file.toFilesystemEncoding().c_str());
435         if (os) {
436                 os << "## Automatically generated lyx session file \n"
437                     << "## Editing this file manually may cause lyx to crash.\n";
438
439                 lastFiles().write(os);
440                 lastOpened().write(os);
441                 lastFilePos().write(os);
442                 lastCommands().write(os);
443                 bookmarks().write(os);
444                 authFiles().write(os);
445         } else
446                 LYXERR(Debug::INIT, "LyX: Warning: unable to save Session: "
447                        << session_file);
448 }
449
450
451 AuthFilesSection::AuthFilesSection() {  }
452
453
454 void AuthFilesSection::read(istream & is)
455 {
456         string tmp;
457         do {
458                 char c = is.peek();
459                 if (c == '[')
460                         break;
461                 getline(is, tmp);
462                 if (tmp.empty() || tmp[0] == '#' || tmp[0] == ' ' || !FileName::isAbsolute(tmp))
463                         continue;
464
465                 // read lastfiles
466                 FileName const file(tmp);
467                 if (file.exists() && !file.isDirectory())
468                         auth_files_.insert(tmp);
469                 else
470                         LYXERR(Debug::INIT, "LyX: Warning: Ignore auth file: " << tmp);
471         } while (is.good());
472 }
473
474
475 void AuthFilesSection::write(ostream & os) const
476 {
477         os << '\n' << sec_authfiles << '\n';
478         copy(auth_files_.begin(), auth_files_.end(),
479              ostream_iterator<std::string>(os, "\n"));
480 }
481
482
483 } // namespace lyx