]> git.lyx.org Git - lyx.git/blob - src/LyXVC.cpp
4f6492e2830494fed1c7bbc54c632668cd7f2fed
[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::rename(FileName const & fn)
177 {
178         LYXERR(Debug::LYXVC, "LyXVC: rename");
179         if (!vcs || fileInVC(fn))
180                 return string();
181         docstring response;
182         bool ok = Alert::askForText(response, _("LyX VC: Log message"),
183                         _("(no log message)"));
184         if (!ok) {
185                 LYXERR(Debug::LYXVC, "LyXVC: user cancelled");
186                 return string();
187         }
188         if (response.empty())
189                 response = _("(no log message)");
190         string ret = vcs->rename(fn, to_utf8(response));
191         return ret;
192 }
193
194
195 string LyXVC::copy(FileName const & fn)
196 {
197         LYXERR(Debug::LYXVC, "LyXVC: copy");
198         if (!vcs || fileInVC(fn))
199                 return string();
200         docstring response;
201         bool ok = Alert::askForText(response, _("LyX VC: Log message"),
202                         _("(no log message)"));
203         if (!ok) {
204                 LYXERR(Debug::LYXVC, "LyXVC: user cancelled");
205                 return string();
206         }
207         if (response.empty())
208                 response = _("(no log message)");
209         string ret = vcs->copy(fn, to_utf8(response));
210         return ret;
211 }
212
213
214 LyXVC::CommandResult LyXVC::checkIn(string & log)
215 {
216         LYXERR(Debug::LYXVC, "LyXVC: checkIn");
217         if (!vcs)
218                 return ErrorBefore;
219         docstring empty(_("(no log message)"));
220         docstring response;
221         bool ok = true;
222         if (vcs->isCheckInWithConfirmation())
223                 ok = Alert::askForText(response, _("LyX VC: Log Message"));
224         if (ok) {
225                 if (response.empty())
226                         response = empty;
227                 //shell collisions
228                 response = subst(response, from_ascii("\""), from_ascii("\\\""));
229                 return vcs->checkIn(to_utf8(response), log);
230         } else {
231                 LYXERR(Debug::LYXVC, "LyXVC: user cancelled");
232                 return Cancelled;
233         }
234 }
235
236
237 string LyXVC::checkOut()
238 {
239         if (!vcs)
240                 return string();
241         //RCS allows checkOut only in ReadOnly mode
242         if (vcs->toggleReadOnlyEnabled() && !owner_->isReadonly())
243                 return string();
244
245         LYXERR(Debug::LYXVC, "LyXVC: checkOut");
246         return vcs->checkOut();
247 }
248
249
250 string LyXVC::repoUpdate()
251 {
252         LYXERR(Debug::LYXVC, "LyXVC: repoUpdate");
253         if (!vcs)
254                 return string();
255         return vcs->repoUpdate();
256 }
257
258
259 string LyXVC::lockingToggle()
260 {
261         LYXERR(Debug::LYXVC, "LyXVC: toggle locking property");
262         if (!vcs)
263                 return string();
264         return vcs->lockingToggle();
265 }
266
267
268 bool LyXVC::revert()
269 {
270         LYXERR(Debug::LYXVC, "LyXVC: revert");
271         if (!vcs)
272                 return false;
273
274         docstring const file = owner_->fileName().displayName(20);
275         docstring text = bformat(_("Reverting to the stored version of the "
276                                 "document %1$s will lose all current changes.\n\n"
277                                 "Do you want to revert to the older version?"), file);
278         int ret = 0;
279         if (vcs->isRevertWithConfirmation())
280                 ret = Alert::prompt(_("Revert to stored version of document?"),
281                         text, 0, 1, _("&Revert"), _("&Cancel"));
282
283         return ret == 0 && vcs->revert();
284 }
285
286
287 void LyXVC::undoLast()
288 {
289         if (!vcs)
290                 return;
291         vcs->undoLast();
292 }
293
294
295 string LyXVC::toggleReadOnly()
296 {
297         if (!vcs)
298                 return string();
299         if (!vcs->toggleReadOnlyEnabled())
300                 return string();
301
302         switch (vcs->status()) {
303         case VCS::UNLOCKED:
304                 LYXERR(Debug::LYXVC, "LyXVC: toggle to locked");
305                 return checkOut();
306         case VCS::LOCKED: {
307                 LYXERR(Debug::LYXVC, "LyXVC: toggle to unlocked");
308                 string log;
309                 if (checkIn(log) != Success)
310                         return string();
311                 return log;
312         }
313         case VCS::NOLOCKING:
314         case VCS::UNVERSIONED:
315                 break;
316         }
317         return string();
318 }
319
320
321 bool LyXVC::inUse() const
322 {
323         if (vcs)
324                 return vcs->status() != VCS::UNVERSIONED;
325         return false;
326 }
327
328
329 string const LyXVC::versionString() const
330 {
331         if (!vcs)
332                 return string();
333         return vcs->versionString();
334 }
335
336
337 bool LyXVC::locking() const
338 {
339         if (!vcs)
340                 return false;
341         return vcs->status() != VCS::NOLOCKING;
342 }
343
344
345 string const LyXVC::getLogFile() const
346 {
347         if (!vcs)
348                 return string();
349
350         FileName const tmpf = FileName::tempName("lyxvclog");
351         if (tmpf.empty()) {
352                 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
353                 return string();
354         }
355         LYXERR(Debug::LYXVC, "Generating logfile " << tmpf);
356         vcs->getLog(tmpf);
357         return tmpf.absFileName();
358 }
359
360
361 string LyXVC::revisionInfo(RevisionInfo const info) const
362 {
363         if (!vcs)
364                 return string();
365
366         return vcs->revisionInfo(info);
367 }
368
369
370 bool LyXVC::renameEnabled() const
371 {
372         if (!inUse())
373                 return false;
374         return vcs->renameEnabled();
375 }
376
377
378 bool LyXVC::copyEnabled() const
379 {
380         return inUse();
381 }
382
383
384 bool LyXVC::checkOutEnabled() const
385 {
386         return vcs && vcs->checkOutEnabled();
387 }
388
389
390 bool LyXVC::checkInEnabled() const
391 {
392         return vcs && vcs->checkInEnabled();
393 }
394
395
396 bool LyXVC::isCheckInWithConfirmation() const
397 {
398         return vcs && vcs->isCheckInWithConfirmation();
399 }
400
401
402 bool LyXVC::lockingToggleEnabled() const
403 {
404         return vcs && vcs->lockingToggleEnabled();
405 }
406
407
408 bool LyXVC::undoLastEnabled() const
409 {
410         return vcs && vcs->undoLastEnabled();
411 }
412
413
414 bool LyXVC::repoUpdateEnabled() const
415 {
416         return vcs && vcs->repoUpdateEnabled();
417 }
418         
419         
420 bool LyXVC::prepareFileRevision(string const & rev, std::string & f)
421 {
422         return vcs && vcs->prepareFileRevision(rev, f);
423 }
424
425
426 bool LyXVC::prepareFileRevisionEnabled()
427 {
428         return vcs && vcs->prepareFileRevisionEnabled();
429 }
430
431 } // namespace lyx