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