]> git.lyx.org Git - lyx.git/blob - src/buffer_funcs.cpp
prepare Qt 5.6 builds
[lyx.git] / src / buffer_funcs.cpp
1 /**
2  * \file buffer_funcs.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 Alfredo Braunstein
8  *
9  * Full author contact details are available in file CREDITS.
10  *
11  */
12
13 #include <config.h>
14
15 #include "buffer_funcs.h"
16 #include "Buffer.h"
17 #include "BufferList.h"
18 #include "BufferParams.h"
19 #include "DocIterator.h"
20 #include "Counters.h"
21 #include "ErrorList.h"
22 #include "Floating.h"
23 #include "FloatList.h"
24 #include "InsetList.h"
25 #include "Language.h"
26 #include "LaTeX.h"
27 #include "Layout.h"
28 #include "LyX.h"
29 #include "LyXVC.h"
30 #include "TextClass.h"
31 #include "Paragraph.h"
32 #include "ParagraphList.h"
33 #include "ParagraphParameters.h"
34 #include "ParIterator.h"
35 #include "TexRow.h"
36 #include "Text.h"
37 #include "TocBackend.h"
38
39 #include "frontends/alert.h"
40
41 #include "insets/InsetBibitem.h"
42 #include "insets/InsetInclude.h"
43
44 #include "support/lassert.h"
45 #include "support/convert.h"
46 #include "support/debug.h"
47 #include "support/filetools.h"
48 #include "support/gettext.h"
49 #include "support/lstrings.h"
50 #include "support/mutex.h"
51 #include "support/textutils.h"
52
53 using namespace std;
54 using namespace lyx::support;
55
56 namespace lyx {
57
58 namespace Alert = frontend::Alert;
59
60
61 Buffer * checkAndLoadLyXFile(FileName const & filename, bool const acceptDirty)
62 {
63         // File already open?
64         Buffer * checkBuffer = theBufferList().getBuffer(filename);
65         if (checkBuffer) {
66                 // Sometimes (when setting the master buffer from a child)
67                 // we accept a dirty buffer right away (otherwise we'd get
68                 // an infinite loop (bug 5514).
69                 // We also accept a dirty buffer when the document has not
70                 // yet been saved to disk.
71                 if (checkBuffer->isClean() || acceptDirty || !filename.exists())
72                         return checkBuffer;
73                 docstring const file = makeDisplayPath(filename.absFileName(), 20);
74                 docstring const text = bformat(_(
75                                 "The document %1$s is already loaded and has unsaved changes.\n"
76                                 "Do you want to abandon your changes and reload the version on disk?"), file);
77                 int res = Alert::prompt(_("Reload saved document?"),
78                                         text, 2, 2,  _("Yes, &Reload"), _("No, &Keep Changes"), _("&Cancel"));
79                 switch (res) {
80                         case 0: {
81                                 // reload the document
82                                 if (checkBuffer->reload() != Buffer::ReadSuccess)
83                                         return 0;
84                                 return checkBuffer;
85                         }
86                         case 1:
87                                 // keep changes
88                                 return checkBuffer;
89                         case 2:
90                                 // cancel
91                                 return 0;
92                 }
93         }
94
95         bool const exists = filename.exists();
96         bool const tryVC = exists ? false : LyXVC::fileInVC(filename);
97         if (exists || tryVC) {
98                 if (exists && !filename.isReadableFile()) {
99                         docstring text = bformat(_("The file %1$s exists but is not "
100                                 "readable by the current user."),
101                                 from_utf8(filename.absFileName()));
102                         Alert::error(_("File not readable!"), text);
103                         return 0;
104                 }
105                 Buffer * b = theBufferList().newBuffer(filename.absFileName());
106                 if (!b) {
107                         // Buffer creation is not possible.
108                         return 0;
109                 }
110                 if (b->loadLyXFile() != Buffer::ReadSuccess) {
111                         // do not save an emergency file when releasing the buffer
112                         b->markClean();
113                         theBufferList().release(b);
114                         return 0;
115                 }
116                 return b;
117         }
118
119         docstring text = bformat(_("The document %1$s does not yet "
120                 "exist.\n\nDo you want to create a new document?"),
121                 from_utf8(filename.absFileName()));
122         if (!Alert::prompt(_("Create new document?"),
123                         text, 0, 1, _("&Create"), _("Cancel")))
124                 return newFile(filename.absFileName(), string(), true);
125
126         return 0;
127 }
128
129
130 // FIXME newFile() should probably be a member method of Application...
131 Buffer * newFile(string const & filename, string const & templatename,
132                  bool is_named)
133 {
134         // get a free buffer
135         Buffer * b = theBufferList().newBuffer(filename);
136         if (!b)
137                 // Buffer creation is not possible.
138                 return 0;
139
140         FileName tname;
141         // use defaults.lyx as a default template if it exists.
142         if (templatename.empty())
143                 tname = libFileSearch("templates", "defaults.lyx");
144         else
145                 tname = makeAbsPath(templatename);
146
147         if (!tname.empty()) {
148                 if (b->loadThisLyXFile(tname) != Buffer::ReadSuccess) {
149                         docstring const file = makeDisplayPath(tname.absFileName(), 50);
150                         docstring const text  = bformat(
151                                 _("The specified document template\n%1$s\ncould not be read."),
152                                 file);
153                         Alert::error(_("Could not read template"), text);
154                         theBufferList().release(b);
155                         return 0;
156                 }
157         }
158
159         if (is_named)
160                 // in this case, the user chose the filename, so we
161                 // assume that she really does want this file.
162                 b->markDirty();
163         else
164                 b->setUnnamed();
165
166         b->setReadonly(false);
167         b->setFullyLoaded(true);
168
169         return b;
170 }
171
172
173 Buffer * newUnnamedFile(FileName const & path, string const & prefix,
174                                                 string const & templatename)
175 {
176         static map<string, int> file_number;
177         static Mutex mutex;
178
179         Mutex::Locker locker(&mutex);
180         FileName filename;
181
182         do {
183                 filename.set(path, 
184                         prefix + convert<string>(++file_number[prefix]) + ".lyx");
185         }
186         while (theBufferList().exists(filename) || filename.isReadableFile());
187                 
188         return newFile(filename.absFileName(), templatename, false);
189 }
190
191
192 Buffer * loadIfNeeded(FileName const & fname)
193 {
194         Buffer * buffer = theBufferList().getBuffer(fname);
195         if (!buffer) {
196                 if (!fname.exists() && !LyXVC::fileInVC(fname))
197                         return 0;
198
199                 buffer = theBufferList().newBuffer(fname.absFileName());
200                 if (!buffer)
201                         // Buffer creation is not possible.
202                         return 0;
203
204                 if (buffer->loadLyXFile() != Buffer::ReadSuccess) {
205                         //close the buffer we just opened
206                         theBufferList().release(buffer);
207                         return 0;
208                 }
209         }
210         return buffer;
211 }
212
213
214 } // namespace lyx