]> git.lyx.org Git - lyx.git/blob - src/Session.cpp
6cdf1f724842558011503954a8b084eba43299f5
[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         lastopened.push_back(lof);
154 }
155
156
157 void LastOpenedSection::clear()
158 {
159         lastopened.clear();
160 }
161
162
163 void LastFilePosSection::read(istream & is)
164 {
165         string tmp;
166         do {
167                 char c = is.peek();
168                 if (c == '[')
169                         break;
170                 getline(is, tmp);
171                 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
172                         continue;
173
174                 try {
175                         // read lastfilepos
176                         // pos, file\n
177                         FilePos filepos;
178                         string fname;
179                         istringstream itmp(tmp);
180                         itmp >> filepos.pit;
181                         itmp.ignore(2);  // ignore ", "
182                         itmp >> filepos.pos;
183                         itmp.ignore(2);  // ignore ", "
184                         getline(itmp, fname);
185                         if (!FileName::isAbsolute(fname))
186                                 continue;
187                         FileName const file(fname);
188                         if (file.exists() && !file.isDirectory()
189                             && lastfilepos.size() < num_lastfilepos)
190                                 lastfilepos[file] = filepos;
191                         else
192                                 LYXERR(Debug::INIT, "LyX: Warning: Ignore pos of last file: " << fname);
193                 } catch (...) {
194                         LYXERR(Debug::INIT, "LyX: Warning: unknown pos of last file: " << tmp);
195                 }
196         } while (is.good());
197 }
198
199
200 void LastFilePosSection::write(ostream & os) const
201 {
202         os << '\n' << sec_lastfilepos << '\n';
203         for (FilePosMap::const_iterator file = lastfilepos.begin();
204                 file != lastfilepos.end(); ++file) {
205                 os << file->second.pit << ", " << file->second.pos << ", "
206                    << file->first << '\n';
207         }
208 }
209
210
211 void LastFilePosSection::save(FileName const & fname, FilePos const & pos)
212 {
213         lastfilepos[fname] = pos;
214 }
215
216
217 LastFilePosSection::FilePos LastFilePosSection::load(FileName const & fname) const
218 {
219         FilePosMap::const_iterator entry = lastfilepos.find(fname);
220         // Has position information, return it.
221         if (entry != lastfilepos.end())
222                 return entry->second;
223         // Not found, return the first paragraph
224         return FilePos();
225 }
226
227
228 void BookmarksSection::clear()
229 {
230         // keep bookmark[0], the temporary one
231         bookmarks.resize(1);
232         bookmarks.resize(max_bookmarks + 1);
233 }
234
235
236 void BookmarksSection::read(istream & is)
237 {
238         string tmp;
239         do {
240                 char c = is.peek();
241                 if (c == '[')
242                         break;
243                 getline(is, tmp);
244                 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
245                         continue;
246
247                 try {
248                         // read bookmarks
249                         // idx, pit, pos, file\n
250                         unsigned int idx;
251                         pit_type pit;
252                         pos_type pos;
253                         string fname;
254                         istringstream itmp(tmp);
255                         itmp >> idx;
256                         itmp.ignore(2);  // ignore ", "
257                         itmp >> pit;
258                         itmp.ignore(2);  // ignore ", "
259                         itmp >> pos;
260                         itmp.ignore(2);  // ignore ", "
261                         getline(itmp, fname);
262                         if (!FileName::isAbsolute(fname))
263                                 continue;
264                         FileName const file(fname);
265                         // only load valid bookmarks
266                         if (file.exists() && !file.isDirectory() && idx <= max_bookmarks)
267                                 bookmarks[idx] = Bookmark(file, pit, pos, 0, 0);
268                         else
269                                 LYXERR(Debug::INIT, "LyX: Warning: Ignore bookmark of file: " << fname);
270                 } catch (...) {
271                         LYXERR(Debug::INIT, "LyX: Warning: unknown Bookmark info: " << tmp);
272                 }
273         } while (is.good());
274 }
275
276
277 void BookmarksSection::write(ostream & os) const
278 {
279         os << '\n' << sec_bookmarks << '\n';
280         for (size_t i = 0; i <= max_bookmarks; ++i) {
281                 if (isValid(i))
282                         os << i << ", "
283                            << bookmarks[i].bottom_pit << ", "
284                            << bookmarks[i].bottom_pos << ", "
285                            << bookmarks[i].filename << '\n';
286         }
287 }
288
289
290 void BookmarksSection::save(FileName const & fname,
291         pit_type bottom_pit, pos_type bottom_pos,
292         int top_id, pos_type top_pos, unsigned int idx)
293 {
294         // silently ignore bookmarks when idx is out of range
295         if (idx <= max_bookmarks)
296                 bookmarks[idx] = Bookmark(fname, bottom_pit, bottom_pos, top_id, top_pos);
297 }
298
299
300 bool BookmarksSection::isValid(unsigned int i) const
301 {
302         return i <= max_bookmarks && !bookmarks[i].filename.empty();
303 }
304
305
306 bool BookmarksSection::hasValid() const
307 {
308         for (size_t i = 1; i <= size(); ++i) {
309                 if (isValid(i))
310                         return true;
311         }
312         return false;
313 }
314
315
316 BookmarksSection::Bookmark const & BookmarksSection::bookmark(unsigned int i) const
317 {
318         return bookmarks[i];
319 }
320
321
322 LastCommandsSection::LastCommandsSection(unsigned int num) :
323         default_num_last_commands(30),
324         absolute_max_last_commands(100)
325 {
326         setNumberOfLastCommands(num);
327 }
328
329         
330 void LastCommandsSection::read(istream & is)
331 {
332         string tmp;
333         do {
334                 char c = is.peek();
335                 if (c == '[')
336                         break;
337                 getline(is, tmp);
338                 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
339                         continue;
340
341                 lastcommands.push_back(tmp);
342         } while (is.good());
343 }
344
345
346 void LastCommandsSection::write(ostream & os) const
347 {
348         os << '\n' << sec_lastcommands << '\n';
349         copy(lastcommands.begin(), lastcommands.end(),
350                 ostream_iterator<std::string>(os, "\n"));
351 }
352
353
354 void LastCommandsSection::setNumberOfLastCommands(unsigned int no)
355 {
356         if (0 < no && no <= absolute_max_last_commands)
357                 num_lastcommands = no;
358         else {
359                 LYXERR(Debug::INIT, "LyX: session: too many last commands\n"
360                         << "\tdefault (=" << default_num_last_commands << ") used.");
361                 num_lastcommands = default_num_last_commands;
362         }
363 }
364
365
366 void LastCommandsSection::add(std::string const & string)
367 {
368         lastcommands.push_back(string);
369 }
370
371
372 void LastCommandsSection::clear()
373 {
374         lastcommands.clear();
375 }
376
377
378 Session::Session(unsigned int num_last_files, unsigned int num_last_commands) :
379         last_files(num_last_files), last_commands(num_last_commands)
380 {
381         // locate the session file
382         // note that the session file name 'session' is hard-coded
383         session_file = FileName(addName(package().user_support().absFileName(), "session"));
384         //
385         readFile();
386 }
387
388
389 void Session::readFile()
390 {
391         // we will not complain if we can't find session_file nor will
392         // we issue a warning. (Lgb)
393         ifstream is(session_file.toFilesystemEncoding().c_str());
394         string tmp;
395
396         while (getline(is, tmp)) {
397                 // Ignore comments, empty line or line stats with ' '
398                 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
399                         continue;
400
401                 // Determine section id
402                 if (tmp == sec_lastfiles)
403                         lastFiles().read(is);
404                 else if (tmp == sec_lastopened)
405                         lastOpened().read(is);
406                 else if (tmp == sec_lastfilepos)
407                         lastFilePos().read(is);
408                 else if (tmp == sec_bookmarks)
409                         bookmarks().read(is);
410                 else if (tmp == sec_lastcommands)
411                         lastCommands().read(is);
412
413                 else
414                         LYXERR(Debug::INIT, "LyX: Warning: unknown Session section: " << tmp);
415         }
416 }
417
418
419 void Session::writeFile() const
420 {
421         ofstream os(session_file.toFilesystemEncoding().c_str());
422         if (os) {
423                 os << "## Automatically generated lyx session file \n"
424                     << "## Editing this file manually may cause lyx to crash.\n";
425
426                 lastFiles().write(os);
427                 lastOpened().write(os);
428                 lastFilePos().write(os);
429                 lastCommands().write(os);
430                 bookmarks().write(os);
431         } else
432                 LYXERR(Debug::INIT, "LyX: Warning: unable to save Session: "
433                        << session_file);
434 }
435
436 }