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