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