]> git.lyx.org Git - lyx.git/blob - src/LyXVC.cpp
This should do it for the delimiters.
[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                 return true;
84         return false;
85 }
86
87
88 void LyXVC::setBuffer(Buffer * buf)
89 {
90         owner_ = buf;
91 }
92
93
94 bool LyXVC::registrer()
95 {
96         FileName const filename = owner_->fileName();
97
98         // there must be a file to save
99         if (!filename.isReadableFile()) {
100                 Alert::error(_("Document not saved"),
101                              _("You must save the document "
102                                             "before it can be registered."));
103                 return false;
104         }
105
106         // it is very likely here that the vcs is not created yet...
107         if (!vcs) {
108                 //check in the root directory of the document
109                 FileName const cvs_entries(onlyPath(filename.absFilename()) + "/CVS/Entries");
110                 FileName const svn_entries(onlyPath(filename.absFilename()) + "/.svn/entries");
111
112                 if (svn_entries.isReadableFile()) {
113                         LYXERR(Debug::LYXVC, "LyXVC: registering "
114                                 << to_utf8(filename.displayName()) << " with SVN");
115                         vcs.reset(new SVN(cvs_entries, filename));
116
117                 } else if (cvs_entries.isReadableFile()) {
118                         LYXERR(Debug::LYXVC, "LyXVC: registering "
119                                 << to_utf8(filename.displayName()) << " with CVS");
120                         vcs.reset(new CVS(cvs_entries, filename));
121
122                 } else {
123                         LYXERR(Debug::LYXVC, "LyXVC: registering "
124                                 << to_utf8(filename.displayName()) << " with RCS");
125                         vcs.reset(new RCS(FileName()));
126                 }
127
128                 vcs->owner(owner_);
129         }
130
131         LYXERR(Debug::LYXVC, "LyXVC: registrer");
132         docstring response;
133         bool ok = Alert::askForText(response, _("LyX VC: Initial description"),
134                         _("(no initial description)"));
135         if (!ok) {
136                 LYXERR(Debug::LYXVC, "LyXVC: user cancelled");
137                 return false;
138         }
139         if (response.empty())
140                 response = _("(no initial description)");
141         vcs->registrer(to_utf8(response));
142         return true;
143 }
144
145
146 string LyXVC::checkIn()
147 {
148         LYXERR(Debug::LYXVC, "LyXVC: checkIn");
149         docstring response;
150         string log;
151         bool ok = Alert::askForText(response, _("LyX VC: Log Message"));
152         if (ok) {
153                 if (response.empty())
154                         response = _("(no log message)");
155                 log = vcs->checkIn(to_utf8(response));
156         } else {
157                 LYXERR(Debug::LYXVC, "LyXVC: user cancelled");
158         }
159         return log;
160 }
161
162
163 string LyXVC::checkOut()
164 {
165         //RCS allows checkOut only in ReadOnly mode
166         if (vcs->toggleReadOnlyEnabled() && !owner_->isReadonly()) return string();
167
168         LYXERR(Debug::LYXVC, "LyXVC: checkOut");
169         return vcs->checkOut();
170 }
171
172
173 string LyXVC::repoUpdate()
174 {
175         LYXERR(Debug::LYXVC, "LyXVC: repoUpdate");
176         return vcs->repoUpdate();
177 }
178
179
180 string LyXVC::lockingToggle()
181 {
182         LYXERR(Debug::LYXVC, "LyXVC: toggle locking property");
183         return vcs->lockingToggle();
184 }
185
186
187 void LyXVC::revert()
188 {
189         LYXERR(Debug::LYXVC, "LyXVC: revert");
190
191         docstring const file = owner_->fileName().displayName(20);
192         docstring text = bformat(_("Reverting to the stored version of the "
193                                 "document %1$s will lose all current changes.\n\n"
194                                 "Do you want to revert to the older version?"), file);
195         int const ret = Alert::prompt(_("Revert to stored version of document?"),
196                 text, 0, 1, _("&Revert"), _("&Cancel"));
197
198         if (ret == 0)
199                 vcs->revert();
200 }
201
202
203 void LyXVC::undoLast()
204 {
205         vcs->undoLast();
206 }
207
208
209 void LyXVC::toggleReadOnly()
210 {
211         if (!vcs->toggleReadOnlyEnabled())
212                 return;
213
214         switch (vcs->status()) {
215         case VCS::UNLOCKED:
216                 LYXERR(Debug::LYXVC, "LyXVC: toggle to locked");
217                 checkOut();
218                 break;
219         case VCS::LOCKED:
220                 LYXERR(Debug::LYXVC, "LyXVC: toggle to unlocked");
221                 checkIn();
222                 break;
223         case VCS::NOLOCKING:
224                 break;
225         }
226 }
227
228
229 bool LyXVC::inUse()
230 {
231         if (vcs)
232                 return true;
233         return false;
234 }
235
236
237 //string const & LyXVC::version() const
238 //{
239 //      return vcs->version();
240 //}
241
242
243 string const LyXVC::versionString() const
244 {
245         return vcs->versionString();
246 }
247
248
249 string const & LyXVC::locker() const
250 {
251         return vcs->locker();
252 }
253
254
255 string const LyXVC::getLogFile() const
256 {
257         if (!vcs)
258                 return string();
259
260         FileName const tmpf = FileName::tempName("lyxvclog");
261         if (tmpf.empty()) {
262                 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
263                 return string();
264         }
265         LYXERR(Debug::LYXVC, "Generating logfile " << tmpf);
266         vcs->getLog(tmpf);
267         return tmpf.absFilename();
268 }
269
270
271 bool LyXVC::checkOutEnabled()
272 {
273         return vcs && vcs->checkOutEnabled();
274 }
275
276
277 bool LyXVC::checkInEnabled()
278 {
279         return vcs && vcs->checkInEnabled();
280 }
281
282
283 bool LyXVC::lockingToggleEnabled()
284 {
285         return vcs && vcs->lockingToggleEnabled();
286 }
287
288
289 bool LyXVC::undoLastEnabled()
290 {
291         return vcs && vcs->undoLastEnabled();
292 }
293
294
295 } // namespace lyx