]> git.lyx.org Git - lyx.git/blob - src/Session.cpp
Rename files in src/support, step one.
[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 #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                         getline(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::clear()
235 {
236         // keep bookmark[0], the temporary one
237         bookmarks.resize(1);
238         bookmarks.resize(max_bookmarks + 1);
239 }
240
241
242 void BookmarksSection::read(istream & is)
243 {
244         string tmp;
245         do {
246                 char c = is.peek();
247                 if (c == '[')
248                         break;
249                 getline(is, tmp);
250                 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
251                         continue;
252
253                 try {
254                         // read bookmarks
255                         // idx, pit, pos, file\n
256                         unsigned int idx;
257                         pit_type pit;
258                         pos_type pos;
259                         string fname;
260                         istringstream itmp(tmp);
261                         itmp >> idx;
262                         itmp.ignore(2);  // ignore ", "
263                         itmp >> pit;
264                         itmp.ignore(2);  // ignore ", "
265                         itmp >> pos;
266                         itmp.ignore(2);  // ignore ", "
267                         getline(itmp, fname);
268                         if (!absolutePath(fname))
269                                 continue;
270                         FileName const file(fname);
271                         // only load valid bookmarks
272                         if (fs::exists(file.toFilesystemEncoding()) &&
273                             !fs::is_directory(file.toFilesystemEncoding()) &&
274                             idx <= max_bookmarks)
275                                 bookmarks[idx] = Bookmark(file, pit, pos, 0, 0);
276                         else
277                                 LYXERR(Debug::INIT) << "LyX: Warning: Ignore bookmark of file: " << fname << endl;
278                 } catch (...) {
279                         LYXERR(Debug::INIT) << "LyX: Warning: unknown Bookmark info: " << tmp << endl;
280                 }
281         } while (is.good());
282 }
283
284
285 void BookmarksSection::write(ostream & os) const
286 {
287         os << '\n' << sec_bookmarks << '\n';
288         for (size_t i = 1; i <= max_bookmarks; ++i) {
289                 if (isValid(i))
290                         os << i << ", "
291                            << bookmarks[i].bottom_pit << ", "
292                            << bookmarks[i].bottom_pos << ", "
293                            << bookmarks[i].filename << '\n';
294         }
295 }
296
297
298 void BookmarksSection::save(FileName const & fname, pit_type bottom_pit, pos_type bottom_pos,
299         int top_id, pos_type top_pos, unsigned int idx)
300 {
301         // silently ignore bookmarks when idx is out of range
302         if (idx <= max_bookmarks)
303                 bookmarks[idx] = Bookmark(fname, bottom_pit, bottom_pos, top_id, top_pos);
304 }
305
306
307 bool BookmarksSection::isValid(unsigned int i) const
308 {
309         return i <= max_bookmarks && !bookmarks[i].filename.empty();
310 }
311
312
313 BookmarksSection::Bookmark const & BookmarksSection::bookmark(unsigned int i) const
314 {
315         return bookmarks[i];
316 }
317
318
319 void ToolbarSection::read(istream & is)
320 {
321         string tmp;
322         do {
323                 char c = is.peek();
324                 if (c == '[')
325                         break;
326                 getline(is, tmp);
327                 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
328                         continue;
329
330                 try {
331                         // Read session info, saved as key/value pairs
332                         // would better yell if pos returns npos
333                         string::size_type pos = tmp.find_first_of(" = ");
334                         // silently ignore lines without " = "
335                         if (pos != string::npos) {
336                                 string key = tmp.substr(0, pos);
337                                 int state;
338                                 int location;
339                                 int posx;
340                                 int posy;
341                                 istringstream value(tmp.substr(pos + 3));
342                                 value >> state;
343                                 value >> location;
344                                 value >> posx;
345                                 value >> posy;
346                                 toolbars.push_back(boost::make_tuple(key, ToolbarInfo(state, location, posx, posy)));
347                         } else 
348                                 LYXERR(Debug::INIT) << "LyX: Warning: Ignore toolbar info: " << tmp << endl;
349                 } catch (...) {
350                         LYXERR(Debug::INIT) << "LyX: Warning: unknown Toolbar info: " << tmp << endl;
351                 }
352         } while (is.good());
353         // sort the toolbars by location, line and position
354         std::sort(toolbars.begin(), toolbars.end());
355 }
356
357
358 void ToolbarSection::write(ostream & os) const
359 {
360         os << '\n' << sec_toolbars << '\n';
361         for (ToolbarList::const_iterator tb = toolbars.begin();
362                 tb != toolbars.end(); ++tb) {
363                 os << tb->get<0>() << " = "
364                   << static_cast<int>(tb->get<1>().state) << " "
365                   << static_cast<int>(tb->get<1>().location) << " "
366                   << tb->get<1>().posx << " "
367                   << tb->get<1>().posy << '\n';
368         }
369 }
370
371
372 ToolbarSection::ToolbarInfo & ToolbarSection::load(string const & name)
373 {
374         for (ToolbarList::iterator tb = toolbars.begin();
375                 tb != toolbars.end(); ++tb)
376                 if (tb->get<0>() == name)
377                         return tb->get<1>();
378         // add a new item
379         toolbars.push_back(boost::make_tuple(name, ToolbarSection::ToolbarInfo()));
380         return toolbars.back().get<1>();
381 }
382
383
384 bool operator<(ToolbarSection::ToolbarItem const & a, ToolbarSection::ToolbarItem const & b)
385 {
386         ToolbarSection::ToolbarInfo lhs = a.get<1>();
387         ToolbarSection::ToolbarInfo rhs = b.get<1>();
388         // on if before off
389         if (lhs.state != rhs.state)
390                 return static_cast<int>(lhs.state) < static_cast<int>(rhs.state);
391         // order of dock does not really matter
392         if (lhs.location != rhs.location)
393                 return static_cast<int>(lhs.location) < static_cast<int>(rhs.location);
394         // if the same dock, the order depends on position
395         if (lhs.location == ToolbarSection::ToolbarInfo::TOP ||
396                 lhs.location == ToolbarSection::ToolbarInfo::BOTTOM)
397                 return lhs.posy < rhs.posy || (lhs.posy == rhs.posy && lhs.posx < rhs.posx);
398         else if (lhs.location == ToolbarSection::ToolbarInfo::LEFT ||
399                 lhs.location == ToolbarSection::ToolbarInfo::RIGHT)
400                 return lhs.posx < rhs.posx || (lhs.posx == rhs.posx && lhs.posy < rhs.posy);
401         else
402                 return true;
403 }
404
405
406 void SessionInfoSection::read(istream & is)
407 {
408         string tmp;
409         do {
410                 char c = is.peek();
411                 if (c == '[')
412                         break;
413                 getline(is, tmp);
414                 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
415                         continue;
416
417                 try {
418                         // Read session info, saved as key/value pairs
419                         // would better yell if pos returns npos
420                         string::size_type pos = tmp.find_first_of(" = ");
421                         // silently ignore lines without " = "
422                         if (pos != string::npos) {
423                                 string key = tmp.substr(0, pos);
424                                 string value = tmp.substr(pos + 3);
425                                 sessioninfo[key] = value;
426                         } else
427                                 LYXERR(Debug::INIT) << "LyX: Warning: Ignore session info: " << tmp << endl;
428                 } catch (...) {
429                         LYXERR(Debug::INIT) << "LyX: Warning: unknown Session info: " << tmp << endl;
430                 }
431         } while (is.good());
432 }
433
434
435 void SessionInfoSection::write(ostream & os) const
436 {
437         os << '\n' << sec_session << '\n';
438         for (MiscInfo::const_iterator val = sessioninfo.begin();
439                 val != sessioninfo.end(); ++val) {
440                 os << val->first << " = " << val->second << '\n';
441         }
442 }
443
444
445 void SessionInfoSection::save(string const & key, string const & value)
446 {
447         sessioninfo[key] = value;
448 }
449
450
451 string const SessionInfoSection::load(string const & key, bool release)
452 {
453         MiscInfo::const_iterator pos = sessioninfo.find(key);
454         string value;
455         if (pos != sessioninfo.end())
456                 value = pos->second;
457         if (release)
458                 sessioninfo.erase(key);
459         return value;
460 }
461
462
463 Session::Session(unsigned int num) :
464         last_files(num)
465 {
466         // locate the session file
467         // note that the session file name 'session' is hard-coded
468         session_file = FileName(addName(package().user_support().absFilename(), "session"));
469         //
470         readFile();
471 }
472
473
474 void Session::readFile()
475 {
476         // we will not complain if we can't find session_file nor will
477         // we issue a warning. (Lgb)
478         ifstream is(session_file.toFilesystemEncoding().c_str());
479         string tmp;
480
481         while (getline(is, tmp)) {
482                 // Ignore comments, empty line or line stats with ' '
483                 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
484                         continue;
485
486                 // Determine section id
487                 if (tmp == sec_lastfiles)
488                         lastFiles().read(is);
489                 else if (tmp == sec_lastopened)
490                         lastOpened().read(is);
491                 else if (tmp == sec_lastfilepos)
492                         lastFilePos().read(is);
493                 else if (tmp == sec_bookmarks)
494                         bookmarks().read(is);
495                 else if (tmp == sec_toolbars)
496                         toolbars().read(is);
497                 else if (tmp == sec_session)
498                         sessionInfo().read(is);
499                 else
500                         LYXERR(Debug::INIT) << "LyX: Warning: unknown Session section: " << tmp << endl;
501         }
502 }
503
504
505 void Session::writeFile() const
506 {
507         ofstream os(session_file.toFilesystemEncoding().c_str());
508         if (os) {
509                 os << "## Automatically generated lyx session file \n"
510                     << "## Editing this file manually may cause lyx to crash.\n";
511
512                 lastFiles().write(os);
513                 lastOpened().write(os);
514                 lastFilePos().write(os);
515                 bookmarks().write(os);
516                 toolbars().write(os);
517                 sessionInfo().write(os);
518         } else
519                 LYXERR(Debug::INIT) << "LyX: Warning: unable to save Session: "
520                        << session_file << endl;
521 }
522
523 }