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