]> git.lyx.org Git - lyx.git/blob - src/LyXVC.cpp
Allow autoconf 2.69 and automake 1.12
[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::file_found_hook(FileName const & fn)
49 {
50         FileName found_file;
51         // Check if file is under RCS
52         if (!(found_file = RCS::findFile(fn)).empty()) {
53                 vcs.reset(new RCS(found_file));
54                 vcs->owner(owner_);
55                 return true;
56         }
57         // Check if file is under CVS
58         if (!(found_file = CVS::findFile(fn)).empty()) {
59                 vcs.reset(new CVS(found_file, fn));
60                 vcs->owner(owner_);
61                 return true;
62         }
63         // Check if file is under SVN
64         if (!(found_file = SVN::findFile(fn)).empty()) {
65                 vcs.reset(new SVN(found_file, fn));
66                 vcs->owner(owner_);
67                 return true;
68         }
69
70         // file is not under any VCS.
71         vcs.reset(0);
72         return false;
73 }
74
75
76 bool LyXVC::file_not_found_hook(FileName const & fn)
77 {
78         // Check if file is under RCS.
79         // This happens if we are trying to load non existent
80         // file on disk, but existent in ,v version.
81         // Seems there is no reasonable scenario for adding implementation
82         // of retrieve for cvs or svn.
83         if (!RCS::findFile(fn).empty()) {       
84                 docstring const file = makeDisplayPath(fn.absFileName(), 20);
85                 docstring const text =
86                         bformat(_("Do you want to retrieve the document"
87                                                    " %1$s from version control?"), file);
88                 int const ret = Alert::prompt(_("Retrieve from version control?"),
89                         text, 0, 1, _("&Retrieve"), _("&Cancel"));
90
91                 if (ret == 0) {
92                         // How can we know _how_ to do the checkout?
93                         // With the current VC support it has to be an RCS
94                         // file since CVS and SVN do not have special ,v files.
95                         RCS::retrieve(fn);
96                         return true;
97                 }
98         }
99         return false;
100 }
101
102
103 void LyXVC::setBuffer(Buffer * buf)
104 {
105         owner_ = buf;
106 }
107
108
109 bool LyXVC::registrer()
110 {
111         FileName const filename = owner_->fileName();
112
113         // there must be a file to save
114         if (!filename.isReadableFile()) {
115                 Alert::error(_("Document not saved"),
116                              _("You must save the document "
117                                             "before it can be registered."));
118                 return false;
119         }
120
121         // it is very likely here that the vcs is not created yet...
122         if (!vcs) {
123                 //check in the root directory of the document
124                 FileName const cvs_entries(onlyPath(filename.absFileName()) + "/CVS/Entries");
125                 FileName const svn_entries(onlyPath(filename.absFileName()) + "/.svn/entries");
126
127                 if (svn_entries.isReadableFile()) {
128                         LYXERR(Debug::LYXVC, "LyXVC: registering "
129                                 << to_utf8(filename.displayName()) << " with SVN");
130                         vcs.reset(new SVN(cvs_entries, filename));
131
132                 } else if (cvs_entries.isReadableFile()) {
133                         LYXERR(Debug::LYXVC, "LyXVC: registering "
134                                 << to_utf8(filename.displayName()) << " with CVS");
135                         vcs.reset(new CVS(cvs_entries, filename));
136
137                 } else {
138                         LYXERR(Debug::LYXVC, "LyXVC: registering "
139                                 << to_utf8(filename.displayName()) << " with RCS");
140                         vcs.reset(new RCS(FileName()));
141                 }
142
143                 vcs->owner(owner_);
144         }
145
146         LYXERR(Debug::LYXVC, "LyXVC: registrer");
147         docstring response;
148         bool ok = Alert::askForText(response, _("LyX VC: Initial description"),
149                         _("(no initial description)"));
150         if (!ok) {
151                 LYXERR(Debug::LYXVC, "LyXVC: user cancelled");
152                 vcs.reset(0);
153                 return false;
154         }
155         if (response.empty())
156                 response = _("(no initial description)");
157         vcs->registrer(to_utf8(response));
158         return true;
159 }
160
161
162 string LyXVC::checkIn()
163 {
164         LYXERR(Debug::LYXVC, "LyXVC: checkIn");
165         docstring empty(_("(no log message)"));
166         docstring response;
167         string log;
168         bool ok = true;
169         if (vcs->isCheckInWithConfirmation())
170                 ok = Alert::askForText(response, _("LyX VC: Log Message"));
171         if (ok) {
172                 if (response.empty())
173                         response = empty;
174                 //shell collisions
175                 response = subst(response, from_utf8("\""), from_utf8("\\\""));
176                 log = vcs->checkIn(to_utf8(response));
177
178                 // Reserve empty string for cancel button
179                 if (log.empty())
180                         log = to_utf8(empty);
181         } else {
182                 LYXERR(Debug::LYXVC, "LyXVC: user cancelled");
183         }
184         return log;
185 }
186
187
188 string LyXVC::checkOut()
189 {
190         //RCS allows checkOut only in ReadOnly mode
191         if (vcs->toggleReadOnlyEnabled() && !owner_->isReadonly())
192                 return string();
193
194         LYXERR(Debug::LYXVC, "LyXVC: checkOut");
195         return vcs->checkOut();
196 }
197
198
199 string LyXVC::repoUpdate()
200 {
201         LYXERR(Debug::LYXVC, "LyXVC: repoUpdate");
202         return vcs->repoUpdate();
203 }
204
205
206 string LyXVC::lockingToggle()
207 {
208         LYXERR(Debug::LYXVC, "LyXVC: toggle locking property");
209         return vcs->lockingToggle();
210 }
211
212
213 bool LyXVC::revert()
214 {
215         LYXERR(Debug::LYXVC, "LyXVC: revert");
216
217         docstring const file = owner_->fileName().displayName(20);
218         docstring text = bformat(_("Reverting to the stored version of the "
219                                 "document %1$s will lose all current changes.\n\n"
220                                 "Do you want to revert to the older version?"), file);
221         int ret = 0;
222         if (vcs->isRevertWithConfirmation())
223                 ret = Alert::prompt(_("Revert to stored version of document?"),
224                         text, 0, 1, _("&Revert"), _("&Cancel"));
225
226         return ret == 0 && vcs->revert();
227 }
228
229
230 void LyXVC::undoLast()
231 {
232         vcs->undoLast();
233 }
234
235
236 void LyXVC::toggleReadOnly()
237 {
238         if (!vcs->toggleReadOnlyEnabled())
239                 return;
240
241         switch (vcs->status()) {
242         case VCS::UNLOCKED:
243                 LYXERR(Debug::LYXVC, "LyXVC: toggle to locked");
244                 checkOut();
245                 break;
246         case VCS::LOCKED:
247                 LYXERR(Debug::LYXVC, "LyXVC: toggle to unlocked");
248                 checkIn();
249                 break;
250         case VCS::NOLOCKING:
251                 break;
252         }
253 }
254
255
256 bool LyXVC::inUse() const
257 {
258         if (vcs)
259                 return true;
260         return false;
261 }
262
263
264 string const LyXVC::versionString() const
265 {
266         return vcs->versionString();
267 }
268
269
270 bool LyXVC::locking() const
271 {
272         return vcs->status() != VCS::NOLOCKING;
273 }
274
275
276 string const LyXVC::getLogFile() const
277 {
278         if (!vcs)
279                 return string();
280
281         FileName const tmpf = FileName::tempName("lyxvclog");
282         if (tmpf.empty()) {
283                 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
284                 return string();
285         }
286         LYXERR(Debug::LYXVC, "Generating logfile " << tmpf);
287         vcs->getLog(tmpf);
288         return tmpf.absFileName();
289 }
290
291
292 string LyXVC::revisionInfo(RevisionInfo const info) const
293 {
294         if (!vcs)
295                 return string();
296
297         return vcs->revisionInfo(info);
298 }
299
300
301 bool LyXVC::checkOutEnabled() const
302 {
303         return vcs && vcs->checkOutEnabled();
304 }
305
306
307 bool LyXVC::checkInEnabled() const
308 {
309         return vcs && vcs->checkInEnabled();
310 }
311
312
313 bool LyXVC::lockingToggleEnabled() const
314 {
315         return vcs && vcs->lockingToggleEnabled();
316 }
317
318
319 bool LyXVC::undoLastEnabled() const
320 {
321         return vcs && vcs->undoLastEnabled();
322 }
323
324
325 bool LyXVC::repoUpdateEnabled() const
326 {
327         return vcs && vcs->repoUpdateEnabled();
328 }
329         
330         
331 bool LyXVC::prepareFileRevision(string const & rev, std::string & f)
332 {
333         return vcs && vcs->prepareFileRevision(rev, f);
334 }
335
336
337 bool LyXVC::prepareFileRevisionEnabled()
338 {
339         return vcs && vcs->prepareFileRevisionEnabled();
340 }
341
342 } // namespace lyx