3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
7 * \author Alfredo Braunstein
9 * Full author contact details are available in file CREDITS
15 #include "buffer_funcs.h"
16 #include "bufferlist.h"
19 #include "vc-backend.h"
21 #include "ParagraphList.h"
22 #include "paragraph.h"
24 #include "frontends/Alert.h"
26 #include "support/filetools.h"
27 #include "support/FileInfo.h"
28 #include "support/lyxlib.h"
31 extern BufferList bufferlist;
36 bool readFile(Buffer * b, string const & s)
39 string e = OnlyPath(s);
41 // File information about normal file
44 if (!fileInfo.exist()) {
45 string const file = MakeDisplayPath(s, 50);
46 string text = bformat(_("The specified document\n%1$s"
47 "\ncould not be read."), file);
48 Alert::error(_("Could not read document"), text);
52 // Check if emergency save file exists and is newer.
53 e += OnlyFilename(s) + ".emergency";
54 FileInfo fileInfoE(e);
56 bool use_emergency = false;
58 if (fileInfoE.exist() && fileInfo.exist()) {
59 if (fileInfoE.getModificationTime()
60 > fileInfo.getModificationTime()) {
61 string const file = MakeDisplayPath(s, 20);
62 string text = bformat(_("An emergency save of the document %1$s exists.\n"
63 "\nRecover emergency save?"), file);
64 int const ret = Alert::prompt(_("Load emergency save?"),
65 text, 0, 1, _("&Recover"), _("&Load Original"));
69 // the file is not saved if we load the
78 // Now check if autosave file is newer.
82 FileInfo fileInfoA(a);
83 if (fileInfoA.exist() && fileInfo.exist()) {
84 if (fileInfoA.getModificationTime()
85 > fileInfo.getModificationTime()) {
86 string const file = MakeDisplayPath(s, 20);
87 string text = bformat(_("The backup of the document %1$s is newer.\n\n"
88 "Load the backup instead?"), file);
89 int const ret = Alert::prompt(_("Load backup?"),
90 text, 0, 1, _("&Load backup"), _("Load &original"));
94 // the file is not saved if we load the
98 // Here, we should delete the autosave
104 // not sure if this is the correct place to begin LyXLex
108 return b->readFile(lex, ts);
116 bool loadLyXFile(Buffer * b, string const & s)
118 switch (IsFileWriteable(s)) {
120 b->setReadonly(true);
123 if (readFile(b, s)) {
124 b->lyxvc.file_found_hook(s);
129 string const file = MakeDisplayPath(s, 20);
130 // Here we probably should run
131 if (LyXVC::file_not_found_hook(s)) {
132 string text = bformat(_("Do you want to retrieve the document"
133 " %1$s from version control?"), file);
134 int const ret = Alert::prompt(_("Retrieve from version control?"),
135 text, 0, 1, _("&Retrieve"), _("&Cancel"));
138 // How can we know _how_ to do the checkout?
139 // With the current VC support it has to be,
140 // a RCS file since CVS do not have special ,v files.
142 return loadLyXFile(b, s);
151 Buffer * newFile(string const & filename, string const & templatename,
155 Buffer * b = bufferlist.newBuffer(filename);
158 // use defaults.lyx as a default template if it exists.
159 if (templatename.empty())
160 tname = LibFileSearch("templates", "defaults.lyx");
162 tname = templatename;
164 if (!tname.empty()) {
165 bool templateok = false;
169 if (b->readFile(lex, tname)) {
174 string const file = MakeDisplayPath(tname, 50);
175 string text = bformat(_("The specified document template\n%1$s\n"
176 "could not be read."), file);
177 Alert::error(_("Could not read template"), text);
178 // no template, start with empty buffer
179 b->paragraphs.push_back(Paragraph());
180 b->paragraphs.begin()->layout(b->params.getLyXTextClass().defaultLayout());
182 } else { // start with empty buffer
183 b->paragraphs.push_back(Paragraph());
184 b->paragraphs.begin()->layout(b->params.getLyXTextClass().defaultLayout());
189 b->setFileName(filename);
192 b->setReadonly(false);
193 b->updateDocLang(b->params.language);