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 LyXVC::CommandResult LyXVC::checkIn(string & log)
178 LYXERR(Debug::LYXVC, "LyXVC: checkIn");
181 docstring empty(_("(no log message)"));
184 if (vcs->isCheckInWithConfirmation())
185 ok = Alert::askForText(response, _("LyX VC: Log Message"));
187 if (response.empty())
190 response = subst(response, from_ascii("\""), from_ascii("\\\""));
191 return vcs->checkIn(to_utf8(response), log);
193 LYXERR(Debug::LYXVC, "LyXVC: user cancelled");
199 string LyXVC::checkOut()
203 //RCS allows checkOut only in ReadOnly mode
204 if (vcs->toggleReadOnlyEnabled() && !owner_->isReadonly())
207 LYXERR(Debug::LYXVC, "LyXVC: checkOut");
208 return vcs->checkOut();
212 string LyXVC::repoUpdate()
214 LYXERR(Debug::LYXVC, "LyXVC: repoUpdate");
217 return vcs->repoUpdate();
221 string LyXVC::lockingToggle()
223 LYXERR(Debug::LYXVC, "LyXVC: toggle locking property");
226 return vcs->lockingToggle();
232 LYXERR(Debug::LYXVC, "LyXVC: revert");
236 docstring const file = owner_->fileName().displayName(20);
237 docstring text = bformat(_("Reverting to the stored version of the "
238 "document %1$s will lose all current changes.\n\n"
239 "Do you want to revert to the older version?"), file);
241 if (vcs->isRevertWithConfirmation())
242 ret = Alert::prompt(_("Revert to stored version of document?"),
243 text, 0, 1, _("&Revert"), _("&Cancel"));
245 return ret == 0 && vcs->revert();
249 void LyXVC::undoLast()
257 string LyXVC::toggleReadOnly()
261 if (!vcs->toggleReadOnlyEnabled())
264 switch (vcs->status()) {
266 LYXERR(Debug::LYXVC, "LyXVC: toggle to locked");
269 LYXERR(Debug::LYXVC, "LyXVC: toggle to unlocked");
271 if (checkIn(log) != Success)
282 bool LyXVC::inUse() const
290 string const LyXVC::versionString() const
294 return vcs->versionString();
298 bool LyXVC::locking() const
302 return vcs->status() != VCS::NOLOCKING;
306 string const LyXVC::getLogFile() const
311 FileName const tmpf = FileName::tempName("lyxvclog");
313 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
316 LYXERR(Debug::LYXVC, "Generating logfile " << tmpf);
318 return tmpf.absFileName();
322 string LyXVC::revisionInfo(RevisionInfo const info) const
327 return vcs->revisionInfo(info);
331 bool LyXVC::checkOutEnabled() const
333 return vcs && vcs->checkOutEnabled();
337 bool LyXVC::checkInEnabled() const
339 return vcs && vcs->checkInEnabled();
343 bool LyXVC::lockingToggleEnabled() const
345 return vcs && vcs->lockingToggleEnabled();
349 bool LyXVC::undoLastEnabled() const
351 return vcs && vcs->undoLastEnabled();
355 bool LyXVC::repoUpdateEnabled() const
357 return vcs && vcs->repoUpdateEnabled();
361 bool LyXVC::prepareFileRevision(string const & rev, std::string & f)
363 return vcs && vcs->prepareFileRevision(rev, f);
367 bool LyXVC::prepareFileRevisionEnabled()
369 return vcs && vcs->prepareFileRevisionEnabled();