]> git.lyx.org Git - lyx.git/blob - src/LyXVC.cpp
* add PreBabelPreamble to Language definition (fixes #4786).
[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 bool 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         return ret == 0 && vcs->revert();
225 }
226
227
228 void LyXVC::undoLast()
229 {
230         vcs->undoLast();
231 }
232
233
234 void LyXVC::toggleReadOnly()
235 {
236         if (!vcs->toggleReadOnlyEnabled())
237                 return;
238
239         switch (vcs->status()) {
240         case VCS::UNLOCKED:
241                 LYXERR(Debug::LYXVC, "LyXVC: toggle to locked");
242                 checkOut();
243                 break;
244         case VCS::LOCKED:
245                 LYXERR(Debug::LYXVC, "LyXVC: toggle to unlocked");
246                 checkIn();
247                 break;
248         case VCS::NOLOCKING:
249                 break;
250         }
251 }
252
253
254 bool LyXVC::inUse() const
255 {
256         if (vcs)
257                 return true;
258         return false;
259 }
260
261
262 string const LyXVC::versionString() const
263 {
264         return vcs->versionString();
265 }
266
267
268 bool LyXVC::locking() const
269 {
270         return vcs->status() != VCS::NOLOCKING;
271 }
272
273
274 string const LyXVC::getLogFile() const
275 {
276         if (!vcs)
277                 return string();
278
279         FileName const tmpf = FileName::tempName("lyxvclog");
280         if (tmpf.empty()) {
281                 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
282                 return string();
283         }
284         LYXERR(Debug::LYXVC, "Generating logfile " << tmpf);
285         vcs->getLog(tmpf);
286         return tmpf.absFileName();
287 }
288
289
290 string LyXVC::revisionInfo(RevisionInfo const info) const
291 {
292         if (!vcs)
293                 return string();
294
295         return vcs->revisionInfo(info);
296 }
297
298
299 bool LyXVC::checkOutEnabled() const
300 {
301         return vcs && vcs->checkOutEnabled();
302 }
303
304
305 bool LyXVC::checkInEnabled() const
306 {
307         return vcs && vcs->checkInEnabled();
308 }
309
310
311 bool LyXVC::lockingToggleEnabled() const
312 {
313         return vcs && vcs->lockingToggleEnabled();
314 }
315
316
317 bool LyXVC::undoLastEnabled() const
318 {
319         return vcs && vcs->undoLastEnabled();
320 }
321
322
323 bool LyXVC::repoUpdateEnabled() const
324 {
325         return vcs && vcs->repoUpdateEnabled();
326 }
327         
328         
329 bool LyXVC::prepareFileRevision(string const & rev, std::string & f)
330 {
331         return vcs && vcs->prepareFileRevision(rev, f);
332 }
333
334
335 bool LyXVC::prepareFileRevisionEnabled()
336 {
337         return vcs && vcs->prepareFileRevisionEnabled();
338 }
339
340 } // namespace lyx