]> git.lyx.org Git - lyx.git/blob - src/LyXVC.cpp
Fix broken layout file syntax
[lyx.git] / src / LyXVC.cpp
1 /**
2  * \file LyXVC.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 Jean-Marc Lasgouttes
8  * \author Angus Leeming
9  * \author John Levon
10  * \author André Pönitz
11  * \author Allan Rae
12  *
13  * Full author contact details are available in file CREDITS.
14  */
15
16 #include <config.h>
17
18 #include "LyXVC.h"
19 #include "VCBackend.h"
20 #include "Buffer.h"
21
22 #include "frontends/alert.h"
23
24 #include "support/debug.h"
25 #include "support/filetools.h"
26 #include "support/gettext.h"
27 #include "support/lstrings.h"
28
29 using namespace std;
30 using namespace lyx::support;
31
32 namespace lyx {
33
34 namespace Alert = frontend::Alert;
35
36
37 LyXVC::LyXVC()
38 {
39         owner_ = 0;
40 }
41
42
43 // for the sake of boost::scoped_ptr
44 LyXVC::~LyXVC()
45 {}
46
47
48 bool LyXVC::fileInVC(FileName const & fn)
49 {
50         if (!RCS::findFile(fn).empty())
51                 return true;
52         if (!CVS::findFile(fn).empty())
53                 return true;
54         if (!SVN::findFile(fn).empty())
55                 return true;
56         return false;
57 }
58
59
60 bool LyXVC::file_found_hook(FileName const & fn)
61 {
62         FileName found_file;
63         // Check if file is under RCS
64         if (!(found_file = RCS::findFile(fn)).empty()) {
65                 vcs.reset(new RCS(found_file, owner_));
66                 return true;
67         }
68         // Check if file is under CVS
69         if (!(found_file = CVS::findFile(fn)).empty()) {
70                 vcs.reset(new CVS(found_file, owner_));
71                 return true;
72         }
73         // Check if file is under SVN
74         if (!(found_file = SVN::findFile(fn)).empty()) {
75                 vcs.reset(new SVN(found_file, owner_));
76                 return true;
77         }
78
79         // file is not under any VCS.
80         vcs.reset(0);
81         return false;
82 }
83
84
85 bool LyXVC::file_not_found_hook(FileName const & fn)
86 {
87         // Check if file is under RCS.
88         // This happens if we are trying to load non existent
89         // file on disk, but existent in ,v version.
90         bool foundRCS = !RCS::findFile(fn).empty();
91         bool foundCVS = foundRCS ? false : !CVS::findFile(fn).empty();
92         bool foundSVN = (foundRCS || foundCVS) ? false : !SVN::findFile(fn).empty();
93         if (foundRCS || foundCVS || foundSVN) {
94                 docstring const file = makeDisplayPath(fn.absFileName(), 20);
95                 docstring const text =
96                         bformat(_("Do you want to retrieve the document"
97                                                    " %1$s from version control?"), file);
98                 int const ret = Alert::prompt(_("Retrieve from version control?"),
99                         text, 0, 1, _("&Retrieve"), _("&Cancel"));
100
101                 if (ret == 0) {
102                         // Since the retrieve commands are implemented using
103                         // more general update commands we need to ensure that
104                         // we do not change an existing file by accident.
105                         if (fn.exists())
106                                 return false;
107                         if (foundRCS)
108                                 return RCS::retrieve(fn);
109                         else if (foundCVS)
110                                 return CVS::retrieve(fn);
111                         else
112                                 return SVN::retrieve(fn);
113                 }
114         }
115         return false;
116 }
117
118
119 void LyXVC::setBuffer(Buffer * buf)
120 {
121         owner_ = buf;
122 }
123
124
125 bool LyXVC::registrer()
126 {
127         FileName const filename = owner_->fileName();
128
129         // there must be a file to save
130         if (!filename.isReadableFile()) {
131                 Alert::error(_("Document not saved"),
132                              _("You must save the document "
133                                             "before it can be registered."));
134                 return false;
135         }
136
137         // it is very likely here that the vcs is not created yet...
138         if (!vcs) {
139                 //check in the root directory of the document
140                 FileName const cvs_entries(onlyPath(filename.absFileName()) + "/CVS/Entries");
141                 FileName const svn_entries(onlyPath(filename.absFileName()) + "/.svn/entries");
142
143                 if (svn_entries.isReadableFile()) {
144                         LYXERR(Debug::LYXVC, "LyXVC: registering "
145                                 << to_utf8(filename.displayName()) << " with SVN");
146                         vcs.reset(new SVN(cvs_entries, owner_));
147
148                 } else if (cvs_entries.isReadableFile()) {
149                         LYXERR(Debug::LYXVC, "LyXVC: registering "
150                                 << to_utf8(filename.displayName()) << " with CVS");
151                         vcs.reset(new CVS(cvs_entries, owner_));
152
153                 } else {
154                         LYXERR(Debug::LYXVC, "LyXVC: registering "
155                                 << to_utf8(filename.displayName()) << " with RCS");
156                         vcs.reset(new RCS(FileName(), owner_));
157                 }
158         }
159
160         LYXERR(Debug::LYXVC, "LyXVC: registrer");
161         docstring response;
162         bool ok = Alert::askForText(response, _("LyX VC: Initial description"),
163                         _("(no initial description)"));
164         if (!ok) {
165                 LYXERR(Debug::LYXVC, "LyXVC: user cancelled");
166                 vcs.reset(0);
167                 return false;
168         }
169         if (response.empty())
170                 response = _("(no initial description)");
171         vcs->registrer(to_utf8(response));
172         return true;
173 }
174
175
176 string LyXVC::checkIn()
177 {
178         LYXERR(Debug::LYXVC, "LyXVC: checkIn");
179         docstring empty(_("(no log message)"));
180         docstring response;
181         string log;
182         bool ok = true;
183         if (vcs->isCheckInWithConfirmation())
184                 ok = Alert::askForText(response, _("LyX VC: Log Message"));
185         if (ok) {
186                 if (response.empty())
187                         response = empty;
188                 //shell collisions
189                 response = subst(response, from_utf8("\""), from_utf8("\\\""));
190                 log = vcs->checkIn(to_utf8(response));
191
192                 // Reserve empty string for cancel button
193                 if (log.empty())
194                         log = to_utf8(empty);
195         } else {
196                 LYXERR(Debug::LYXVC, "LyXVC: user cancelled");
197         }
198         return log;
199 }
200
201
202 string LyXVC::checkOut()
203 {
204         //RCS allows checkOut only in ReadOnly mode
205         if (vcs->toggleReadOnlyEnabled() && !owner_->isReadonly())
206                 return string();
207
208         LYXERR(Debug::LYXVC, "LyXVC: checkOut");
209         return vcs->checkOut();
210 }
211
212
213 string LyXVC::repoUpdate()
214 {
215         LYXERR(Debug::LYXVC, "LyXVC: repoUpdate");
216         return vcs->repoUpdate();
217 }
218
219
220 string LyXVC::lockingToggle()
221 {
222         LYXERR(Debug::LYXVC, "LyXVC: toggle locking property");
223         return vcs->lockingToggle();
224 }
225
226
227 bool LyXVC::revert()
228 {
229         LYXERR(Debug::LYXVC, "LyXVC: revert");
230
231         docstring const file = owner_->fileName().displayName(20);
232         docstring text = bformat(_("Reverting to the stored version of the "
233                                 "document %1$s will lose all current changes.\n\n"
234                                 "Do you want to revert to the older version?"), file);
235         int ret = 0;
236         if (vcs->isRevertWithConfirmation())
237                 ret = Alert::prompt(_("Revert to stored version of document?"),
238                         text, 0, 1, _("&Revert"), _("&Cancel"));
239
240         return ret == 0 && vcs->revert();
241 }
242
243
244 void LyXVC::undoLast()
245 {
246         vcs->undoLast();
247 }
248
249
250 void LyXVC::toggleReadOnly()
251 {
252         if (!vcs->toggleReadOnlyEnabled())
253                 return;
254
255         switch (vcs->status()) {
256         case VCS::UNLOCKED:
257                 LYXERR(Debug::LYXVC, "LyXVC: toggle to locked");
258                 checkOut();
259                 break;
260         case VCS::LOCKED:
261                 LYXERR(Debug::LYXVC, "LyXVC: toggle to unlocked");
262                 checkIn();
263                 break;
264         case VCS::NOLOCKING:
265                 break;
266         }
267 }
268
269
270 bool LyXVC::inUse() const
271 {
272         if (vcs)
273                 return true;
274         return false;
275 }
276
277
278 string const LyXVC::versionString() const
279 {
280         return vcs->versionString();
281 }
282
283
284 bool LyXVC::locking() const
285 {
286         return vcs->status() != VCS::NOLOCKING;
287 }
288
289
290 string const LyXVC::getLogFile() const
291 {
292         if (!vcs)
293                 return string();
294
295         FileName const tmpf = FileName::tempName("lyxvclog");
296         if (tmpf.empty()) {
297                 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
298                 return string();
299         }
300         LYXERR(Debug::LYXVC, "Generating logfile " << tmpf);
301         vcs->getLog(tmpf);
302         return tmpf.absFileName();
303 }
304
305
306 string LyXVC::revisionInfo(RevisionInfo const info) const
307 {
308         if (!vcs)
309                 return string();
310
311         return vcs->revisionInfo(info);
312 }
313
314
315 bool LyXVC::checkOutEnabled() const
316 {
317         return vcs && vcs->checkOutEnabled();
318 }
319
320
321 bool LyXVC::checkInEnabled() const
322 {
323         return vcs && vcs->checkInEnabled();
324 }
325
326
327 bool LyXVC::lockingToggleEnabled() const
328 {
329         return vcs && vcs->lockingToggleEnabled();
330 }
331
332
333 bool LyXVC::undoLastEnabled() const
334 {
335         return vcs && vcs->undoLastEnabled();
336 }
337
338
339 bool LyXVC::repoUpdateEnabled() const
340 {
341         return vcs && vcs->repoUpdateEnabled();
342 }
343         
344         
345 bool LyXVC::prepareFileRevision(string const & rev, std::string & f)
346 {
347         return vcs && vcs->prepareFileRevision(rev, f);
348 }
349
350
351 bool LyXVC::prepareFileRevisionEnabled()
352 {
353         return vcs && vcs->prepareFileRevisionEnabled();
354 }
355
356 } // namespace lyx