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