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