]> git.lyx.org Git - lyx.git/blob - src/LyXVC.cpp
Enable OK/Apply buttons when resetting to class defaults.
[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 #include "support/TempFile.h"
29
30 using namespace std;
31 using namespace lyx::support;
32
33 namespace lyx {
34
35 namespace Alert = frontend::Alert;
36
37
38 LyXVC::LyXVC()
39 {
40         owner_ = 0;
41 }
42
43
44 // for the sake of boost::scoped_ptr
45 LyXVC::~LyXVC()
46 {}
47
48
49 docstring LyXVC::vcstatus() const
50 {
51         if (!vcs)
52                 return docstring();
53         if (locking())
54                 return bformat(_("%1$s lock"), from_ascii(vcs->vcname()));
55         else
56                 return from_ascii(vcs->vcname());
57 }
58
59
60 bool LyXVC::fileInVC(FileName const & fn)
61 {
62         if (!RCS::findFile(fn).empty())
63                 return true;
64         if (!CVS::findFile(fn).empty())
65                 return true;
66         if (!SVN::findFile(fn).empty())
67                 return true;
68         if (!GIT::findFile(fn).empty())
69                 return true;
70         return false;
71 }
72
73
74 bool LyXVC::file_found_hook(FileName const & fn)
75 {
76         FileName found_file;
77         // Check if file is under RCS
78         if (!(found_file = RCS::findFile(fn)).empty()) {
79                 vcs.reset(new RCS(found_file, owner_));
80                 return true;
81         }
82         // Check if file is under CVS
83         if (!(found_file = CVS::findFile(fn)).empty()) {
84                 vcs.reset(new CVS(found_file, owner_));
85                 return true;
86         }
87         // Check if file is under SVN
88         if (!(found_file = SVN::findFile(fn)).empty()) {
89                 vcs.reset(new SVN(found_file, owner_));
90                 return true;
91         }
92         // Check if file is under GIT
93         if (!(found_file = GIT::findFile(fn)).empty()) {
94                 vcs.reset(new GIT(found_file, owner_));
95                 return true;
96         }
97
98         // file is not under any VCS.
99         vcs.reset(0);
100         return false;
101 }
102
103
104 bool LyXVC::file_not_found_hook(FileName const & fn)
105 {
106         // Check if file is under RCS.
107         // This happens if we are trying to load non existent
108         // file on disk, but existent in ,v version.
109         bool foundRCS = !RCS::findFile(fn).empty();
110         bool foundCVS = foundRCS ? false : !CVS::findFile(fn).empty();
111         bool foundSVN = (foundRCS || foundCVS) ? false : !SVN::findFile(fn).empty();
112         bool foundGIT = (foundRCS || foundCVS || foundSVN) ? false : !GIT::findFile(fn).empty();
113         if (foundRCS || foundCVS || foundSVN || foundGIT) {
114                 docstring const file = makeDisplayPath(fn.absFileName(), 20);
115                 docstring const text =
116                         bformat(_("Do you want to retrieve the document"
117                                                    " %1$s from version control?"), file);
118                 int const ret = Alert::prompt(_("Retrieve from version control?"),
119                         text, 0, 1, _("&Retrieve"), _("&Cancel"));
120
121                 if (ret == 0) {
122                         // Since the retrieve commands are implemented using
123                         // more general update commands we need to ensure that
124                         // we do not change an existing file by accident.
125                         if (fn.exists())
126                                 return false;
127                         if (foundRCS)
128                                 return RCS::retrieve(fn);
129                         else if (foundCVS)
130                                 return CVS::retrieve(fn);
131                         else if (foundSVN)
132                                 return SVN::retrieve(fn);
133                         else
134                                 return GIT::retrieve(fn);
135                 }
136         }
137         return false;
138 }
139
140
141 void LyXVC::setBuffer(Buffer * buf)
142 {
143         owner_ = buf;
144 }
145
146
147 bool LyXVC::registrer()
148 {
149         FileName const filename = owner_->fileName();
150
151         // there must be a file to save
152         if (!filename.isReadableFile()) {
153                 Alert::error(_("Document not saved"),
154                              _("You must save the document "
155                                             "before it can be registered."));
156                 return false;
157         }
158
159         // it is very likely here that the vcs is not created yet...
160         if (!vcs) {
161                 //check in the root directory of the document
162                 FileName const cvs_entries(onlyPath(filename.absFileName()) + "/CVS/Entries");
163                 FileName const svn_entries(onlyPath(filename.absFileName()) + "/.svn/entries");
164                 FileName const git_index(onlyPath(filename.absFileName()) + "/.git/index");
165
166                 if (git_index.isReadableFile()) {
167                         LYXERR(Debug::LYXVC, "LyXVC: registering "
168                                 << to_utf8(filename.displayName()) << " with GIT");
169                         vcs.reset(new GIT(git_index, owner_));
170
171                 } else if (svn_entries.isReadableFile()) {
172                         LYXERR(Debug::LYXVC, "LyXVC: registering "
173                                 << to_utf8(filename.displayName()) << " with SVN");
174                         vcs.reset(new SVN(svn_entries, owner_));
175
176                 } else if (cvs_entries.isReadableFile()) {
177                         LYXERR(Debug::LYXVC, "LyXVC: registering "
178                                 << to_utf8(filename.displayName()) << " with CVS");
179                         vcs.reset(new CVS(cvs_entries, owner_));
180
181                 } else {
182                         LYXERR(Debug::LYXVC, "LyXVC: registering "
183                                 << to_utf8(filename.displayName()) << " with RCS");
184                         vcs.reset(new RCS(FileName(), owner_));
185                 }
186         }
187
188         LYXERR(Debug::LYXVC, "LyXVC: registrer");
189         docstring response;
190         bool ok = Alert::askForText(response, _("LyX VC: Initial description"),
191                         _("(no initial description)"));
192         if (!ok) {
193                 LYXERR(Debug::LYXVC, "LyXVC: user cancelled");
194                 vcs.reset(0);
195                 return false;
196         }
197         if (response.empty())
198                 response = _("(no initial description)");
199         vcs->registrer(to_utf8(response));
200         return true;
201 }
202
203
204 string LyXVC::rename(FileName const & fn)
205 {
206         LYXERR(Debug::LYXVC, "LyXVC: rename");
207         if (!vcs || fileInVC(fn))
208                 return string();
209         docstring response;
210         bool ok = Alert::askForText(response, _("LyX VC: Log message"),
211                         _("(no log message)"));
212         if (!ok) {
213                 LYXERR(Debug::LYXVC, "LyXVC: user cancelled");
214                 return string();
215         }
216         if (response.empty())
217                 response = _("(no log message)");
218         string ret = vcs->rename(fn, to_utf8(response));
219         return ret;
220 }
221
222
223 string LyXVC::copy(FileName const & fn)
224 {
225         LYXERR(Debug::LYXVC, "LyXVC: copy");
226         if (!vcs || fileInVC(fn))
227                 return string();
228         docstring response;
229         bool ok = Alert::askForText(response, _("LyX VC: Log message"),
230                         _("(no log message)"));
231         if (!ok) {
232                 LYXERR(Debug::LYXVC, "LyXVC: user cancelled");
233                 return string();
234         }
235         if (response.empty())
236                 response = _("(no log message)");
237         string ret = vcs->copy(fn, to_utf8(response));
238         return ret;
239 }
240
241
242 LyXVC::CommandResult LyXVC::checkIn(string & log)
243 {
244         LYXERR(Debug::LYXVC, "LyXVC: checkIn");
245         if (!vcs)
246                 return ErrorBefore;
247         docstring empty(_("(no log message)"));
248         docstring response;
249         bool ok = true;
250         if (vcs->isCheckInWithConfirmation())
251                 ok = Alert::askForText(response, _("LyX VC: Log Message"));
252         if (ok) {
253                 if (response.empty())
254                         response = empty;
255                 //shell collisions
256                 response = subst(response, from_ascii("\""), from_ascii("\\\""));
257                 return vcs->checkIn(to_utf8(response), log);
258         } else {
259                 LYXERR(Debug::LYXVC, "LyXVC: user cancelled");
260                 return Cancelled;
261         }
262 }
263
264
265 string LyXVC::checkOut()
266 {
267         if (!vcs)
268                 return string();
269         //RCS allows checkOut only in ReadOnly mode
270         if (vcs->toggleReadOnlyEnabled() && !owner_->isReadonly())
271                 return string();
272
273         LYXERR(Debug::LYXVC, "LyXVC: checkOut");
274         return vcs->checkOut();
275 }
276
277
278 string LyXVC::repoUpdate()
279 {
280         LYXERR(Debug::LYXVC, "LyXVC: repoUpdate");
281         if (!vcs)
282                 return string();
283         return vcs->repoUpdate();
284 }
285
286
287 string LyXVC::lockingToggle()
288 {
289         LYXERR(Debug::LYXVC, "LyXVC: toggle locking property");
290         if (!vcs)
291                 return string();
292         return vcs->lockingToggle();
293 }
294
295
296 bool LyXVC::revert()
297 {
298         LYXERR(Debug::LYXVC, "LyXVC: revert");
299         if (!vcs)
300                 return false;
301
302         docstring const file = owner_->fileName().displayName(20);
303         docstring text = bformat(_("Reverting to the stored version of the "
304                                 "document %1$s will lose all current changes.\n\n"
305                                 "Do you want to revert to the older version?"), file);
306         int ret = 0;
307         if (vcs->isRevertWithConfirmation())
308                 ret = Alert::prompt(_("Revert to stored version of document?"),
309                         text, 0, 1, _("&Revert"), _("&Cancel"));
310
311         return ret == 0 && vcs->revert();
312 }
313
314
315 void LyXVC::undoLast()
316 {
317         if (!vcs)
318                 return;
319         vcs->undoLast();
320 }
321
322
323 string LyXVC::toggleReadOnly()
324 {
325         if (!vcs)
326                 return string();
327         if (!vcs->toggleReadOnlyEnabled())
328                 return string();
329
330         switch (vcs->status()) {
331         case VCS::UNLOCKED:
332                 LYXERR(Debug::LYXVC, "LyXVC: toggle to locked");
333                 return checkOut();
334         case VCS::LOCKED: {
335                 LYXERR(Debug::LYXVC, "LyXVC: toggle to unlocked");
336                 string log;
337                 if (checkIn(log) != VCSuccess)
338                         return string();
339                 return log;
340         }
341         case VCS::NOLOCKING:
342                 Buffer * b = vcs->owner();
343                 bool const newstate = !b->isReadonly();
344                 string result = "LyXVC: toggle to ";
345                 result += (newstate ? "readonly" : "readwrite");
346                 LYXERR(Debug::LYXVC, result);
347                 b->setReadonly(newstate);
348                 return result;
349         }
350         return string();
351 }
352
353
354 bool LyXVC::inUse() const
355 {
356         if (vcs)
357                 return true;
358         return false;
359 }
360
361
362 string const LyXVC::versionString() const
363 {
364         if (!vcs)
365                 return string();
366         return vcs->versionString();
367 }
368
369
370 bool LyXVC::locking() const
371 {
372         if (!vcs)
373                 return false;
374         return vcs->status() != VCS::NOLOCKING;
375 }
376
377
378 string const LyXVC::getLogFile() const
379 {
380         if (!vcs)
381                 return string();
382
383         TempFile tempfile("lyxvclog");
384         tempfile.setAutoRemove(false);
385         FileName const tmpf = tempfile.name();
386         if (tmpf.empty()) {
387                 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
388                 return string();
389         }
390         LYXERR(Debug::LYXVC, "Generating logfile " << tmpf);
391         vcs->getLog(tmpf);
392         return tmpf.absFileName();
393 }
394
395
396 string LyXVC::revisionInfo(RevisionInfo const info) const
397 {
398         if (!vcs)
399                 return string();
400
401         return vcs->revisionInfo(info);
402 }
403
404
405 bool LyXVC::renameEnabled() const
406 {
407         if (!inUse())
408                 return false;
409         return vcs->renameEnabled();
410 }
411
412
413 bool LyXVC::copyEnabled() const
414 {
415         if (!inUse())
416                 return false;
417         return vcs->copyEnabled();
418 }
419
420
421 bool LyXVC::checkOutEnabled() const
422 {
423         return vcs && vcs->checkOutEnabled();
424 }
425
426
427 bool LyXVC::checkInEnabled() const
428 {
429         return vcs && vcs->checkInEnabled();
430 }
431
432
433 bool LyXVC::isCheckInWithConfirmation() const
434 {
435         return vcs && vcs->isCheckInWithConfirmation();
436 }
437
438
439 bool LyXVC::lockingToggleEnabled() const
440 {
441         return vcs && vcs->lockingToggleEnabled();
442 }
443
444
445 bool LyXVC::undoLastEnabled() const
446 {
447         return vcs && vcs->undoLastEnabled();
448 }
449
450
451 bool LyXVC::repoUpdateEnabled() const
452 {
453         return vcs && vcs->repoUpdateEnabled();
454 }
455         
456         
457 bool LyXVC::prepareFileRevision(string const & rev, std::string & f)
458 {
459         return vcs && vcs->prepareFileRevision(rev, f);
460 }
461
462
463 bool LyXVC::prepareFileRevisionEnabled()
464 {
465         return vcs && vcs->prepareFileRevisionEnabled();
466 }
467
468 } // namespace lyx