3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
7 * \author Jean-Marc Lasgouttes
8 * \author Angus Leeming
10 * \author André Pönitz
13 * Full author contact details are available in file CREDITS.
19 #include "VCBackend.h"
22 #include "frontends/alert.h"
24 #include "support/debug.h"
25 #include "support/filetools.h"
26 #include "support/gettext.h"
27 #include "support/lstrings.h"
30 using namespace lyx::support;
34 namespace Alert = frontend::Alert;
43 // for the sake of boost::scoped_ptr
48 bool LyXVC::fileInVC(FileName const & fn)
50 if (!RCS::findFile(fn).empty())
52 if (!CVS::findFile(fn).empty())
54 if (!SVN::findFile(fn).empty())
60 bool LyXVC::file_found_hook(FileName const & fn)
63 // Check if file is under RCS
64 if (!(found_file = RCS::findFile(fn)).empty()) {
65 vcs.reset(new RCS(found_file, owner_));
68 // Check if file is under CVS
69 if (!(found_file = CVS::findFile(fn)).empty()) {
70 vcs.reset(new CVS(found_file, owner_));
73 // Check if file is under SVN
74 if (!(found_file = SVN::findFile(fn)).empty()) {
75 vcs.reset(new SVN(found_file, owner_));
79 // file is not under any VCS.
85 bool LyXVC::file_not_found_hook(FileName const & fn)
87 // Check if file is under RCS.
88 // This happens if we are trying to load non existent
89 // file on disk, but existent in ,v version.
90 bool foundRCS = !RCS::findFile(fn).empty();
91 bool foundCVS = foundRCS ? false : !CVS::findFile(fn).empty();
92 bool foundSVN = (foundRCS || foundCVS) ? false : !SVN::findFile(fn).empty();
93 if (foundRCS || foundCVS || foundSVN) {
94 docstring const file = makeDisplayPath(fn.absFileName(), 20);
95 docstring const text =
96 bformat(_("Do you want to retrieve the document"
97 " %1$s from version control?"), file);
98 int const ret = Alert::prompt(_("Retrieve from version control?"),
99 text, 0, 1, _("&Retrieve"), _("&Cancel"));
102 // Since the retrieve commands are implemented using
103 // more general update commands we need to ensure that
104 // we do not change an existing file by accident.
108 return RCS::retrieve(fn);
110 return CVS::retrieve(fn);
112 return SVN::retrieve(fn);
119 void LyXVC::setBuffer(Buffer * buf)
125 bool LyXVC::registrer()
127 FileName const filename = owner_->fileName();
129 // there must be a file to save
130 if (!filename.isReadableFile()) {
131 Alert::error(_("Document not saved"),
132 _("You must save the document "
133 "before it can be registered."));
137 // it is very likely here that the vcs is not created yet...
139 //check in the root directory of the document
140 FileName const cvs_entries(onlyPath(filename.absFileName()) + "/CVS/Entries");
141 FileName const svn_entries(onlyPath(filename.absFileName()) + "/.svn/entries");
143 if (svn_entries.isReadableFile()) {
144 LYXERR(Debug::LYXVC, "LyXVC: registering "
145 << to_utf8(filename.displayName()) << " with SVN");
146 vcs.reset(new SVN(cvs_entries, owner_));
148 } else if (cvs_entries.isReadableFile()) {
149 LYXERR(Debug::LYXVC, "LyXVC: registering "
150 << to_utf8(filename.displayName()) << " with CVS");
151 vcs.reset(new CVS(cvs_entries, owner_));
154 LYXERR(Debug::LYXVC, "LyXVC: registering "
155 << to_utf8(filename.displayName()) << " with RCS");
156 vcs.reset(new RCS(FileName(), owner_));
160 LYXERR(Debug::LYXVC, "LyXVC: registrer");
162 bool ok = Alert::askForText(response, _("LyX VC: Initial description"),
163 _("(no initial description)"));
165 LYXERR(Debug::LYXVC, "LyXVC: user cancelled");
169 if (response.empty())
170 response = _("(no initial description)");
171 vcs->registrer(to_utf8(response));
176 string LyXVC::rename(FileName const & fn)
178 LYXERR(Debug::LYXVC, "LyXVC: rename");
179 if (!vcs || fileInVC(fn))
182 bool ok = Alert::askForText(response, _("LyX VC: Log message"),
183 _("(no log message)"));
185 LYXERR(Debug::LYXVC, "LyXVC: user cancelled");
188 if (response.empty())
189 response = _("(no log message)");
190 string ret = vcs->rename(fn, to_utf8(response));
195 string LyXVC::copy(FileName const & fn)
197 LYXERR(Debug::LYXVC, "LyXVC: copy");
198 if (!vcs || fileInVC(fn))
201 bool ok = Alert::askForText(response, _("LyX VC: Log message"),
202 _("(no log message)"));
204 LYXERR(Debug::LYXVC, "LyXVC: user cancelled");
207 if (response.empty())
208 response = _("(no log message)");
209 string ret = vcs->copy(fn, to_utf8(response));
214 LyXVC::CommandResult LyXVC::checkIn(string & log)
216 LYXERR(Debug::LYXVC, "LyXVC: checkIn");
219 docstring empty(_("(no log message)"));
222 if (vcs->isCheckInWithConfirmation())
223 ok = Alert::askForText(response, _("LyX VC: Log Message"));
225 if (response.empty())
228 response = subst(response, from_ascii("\""), from_ascii("\\\""));
229 return vcs->checkIn(to_utf8(response), log);
231 LYXERR(Debug::LYXVC, "LyXVC: user cancelled");
237 string LyXVC::checkOut()
241 //RCS allows checkOut only in ReadOnly mode
242 if (vcs->toggleReadOnlyEnabled() && !owner_->isReadonly())
245 LYXERR(Debug::LYXVC, "LyXVC: checkOut");
246 return vcs->checkOut();
250 string LyXVC::repoUpdate()
252 LYXERR(Debug::LYXVC, "LyXVC: repoUpdate");
255 return vcs->repoUpdate();
259 string LyXVC::lockingToggle()
261 LYXERR(Debug::LYXVC, "LyXVC: toggle locking property");
264 return vcs->lockingToggle();
270 LYXERR(Debug::LYXVC, "LyXVC: revert");
274 docstring const file = owner_->fileName().displayName(20);
275 docstring text = bformat(_("Reverting to the stored version of the "
276 "document %1$s will lose all current changes.\n\n"
277 "Do you want to revert to the older version?"), file);
279 if (vcs->isRevertWithConfirmation())
280 ret = Alert::prompt(_("Revert to stored version of document?"),
281 text, 0, 1, _("&Revert"), _("&Cancel"));
283 return ret == 0 && vcs->revert();
287 void LyXVC::undoLast()
295 string LyXVC::toggleReadOnly()
299 if (!vcs->toggleReadOnlyEnabled())
302 switch (vcs->status()) {
304 LYXERR(Debug::LYXVC, "LyXVC: toggle to locked");
307 LYXERR(Debug::LYXVC, "LyXVC: toggle to unlocked");
309 if (checkIn(log) != Success)
314 case VCS::UNVERSIONED:
321 bool LyXVC::inUse() const
324 return vcs->status() != VCS::UNVERSIONED;
329 string const LyXVC::versionString() const
333 return vcs->versionString();
337 bool LyXVC::locking() const
341 return vcs->status() != VCS::NOLOCKING;
345 string const LyXVC::getLogFile() const
350 FileName const tmpf = FileName::tempName("lyxvclog");
352 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
355 LYXERR(Debug::LYXVC, "Generating logfile " << tmpf);
357 return tmpf.absFileName();
361 string LyXVC::revisionInfo(RevisionInfo const info) const
366 return vcs->revisionInfo(info);
370 bool LyXVC::renameEnabled() const
374 return vcs->renameEnabled();
378 bool LyXVC::copyEnabled() const
384 bool LyXVC::checkOutEnabled() const
386 return vcs && vcs->checkOutEnabled();
390 bool LyXVC::checkInEnabled() const
392 return vcs && vcs->checkInEnabled();
396 bool LyXVC::isCheckInWithConfirmation() const
398 return vcs && vcs->isCheckInWithConfirmation();
402 bool LyXVC::lockingToggleEnabled() const
404 return vcs && vcs->lockingToggleEnabled();
408 bool LyXVC::undoLastEnabled() const
410 return vcs && vcs->undoLastEnabled();
414 bool LyXVC::repoUpdateEnabled() const
416 return vcs && vcs->repoUpdateEnabled();
420 bool LyXVC::prepareFileRevision(string const & rev, std::string & f)
422 return vcs && vcs->prepareFileRevision(rev, f);
426 bool LyXVC::prepareFileRevisionEnabled()
428 return vcs && vcs->prepareFileRevisionEnabled();