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