+ CvsStatus status = getStatus();
+ switch (status) {
+ case UpToDate:
+ if (vcstatus != NOLOCKING)
+ return 0 == unedit();
+ break;
+ case NeedsMerge:
+ case NeedsCheckout:
+ case LocallyModified: {
+ FileName f(owner_->absFileName());
+ f.removeFile();
+ update(File, FileName());
+ owner_->markClean();
+ break;
+ }
+ case LocallyAdded: {
+ docstring const file = owner_->fileName().displayName(20);
+ frontend::Alert::error(_("Revision control error."),
+ bformat(_("The document %1$s is not in repository.\n"
+ "You have to check in the first revision before you can revert."),
+ file)) ;
+ return false;
+ }
+ default: {
+ docstring const file = owner_->fileName().displayName(20);
+ frontend::Alert::error(_("Revision control error."),
+ bformat(_("Cannot revert document %1$s to repository version.\n"
+ "The status '%2$s' is unexpected."),
+ file, toString(status)));
+ return false;
+ }
+ }
+ return true;
+}
+
+
+void CVS::undoLast()
+{
+ // merge the current with the previous version
+ // in a reverse patch kind of way, so that the
+ // result is to revert the last changes.
+ lyxerr << "Sorry, not implemented." << endl;
+}
+
+
+bool CVS::undoLastEnabled()
+{
+ return false;
+}
+
+
+void CVS::getLog(FileName const & tmpf)
+{
+ doVCCommandWithOutput("cvs log " + getTarget(File),
+ FileName(owner_->filePath()),
+ tmpf);
+}
+
+
+bool CVS::toggleReadOnlyEnabled()
+{
+ return false;
+}
+
+
+string CVS::revisionInfo(LyXVC::RevisionInfo const info)
+{
+ if (!version_.empty()) {
+ getRevisionInfo();
+ switch (info) {
+ case LyXVC::File:
+ return version_;
+ case LyXVC::Author:
+ return rev_author_cache_;
+ case LyXVC::Date:
+ return rev_date_cache_;
+ case LyXVC::Time:
+ return rev_time_cache_;
+ default: ;
+ }
+ }
+ return string();
+}
+
+
+bool CVS::prepareFileRevision(string const & revis, string & f)
+{
+ string rev = revis;
+ if (!VCS::makeRCSRevision(version_, rev))
+ return false;
+
+ FileName tmpf = FileName::tempName("lyxvcrev_" + rev + "_");
+ if (tmpf.empty()) {
+ LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
+ return false;
+ }
+
+ doVCCommandWithOutput("cvs update -p -r" + rev + " "
+ + getTarget(File),
+ FileName(owner_->filePath()), tmpf);
+ if (tmpf.isFileEmpty())
+ return false;
+
+ f = tmpf.absFileName();
+ return true;
+}
+
+
+bool CVS::prepareFileRevisionEnabled()
+{
+ return true;
+}
+
+
+/////////////////////////////////////////////////////////////////////
+//
+// SVN
+//
+/////////////////////////////////////////////////////////////////////
+
+SVN::SVN(FileName const & m, FileName const & f)
+{
+ owner_ = 0;
+ master_ = m;
+ file_ = f;
+ locked_mode_ = 0;
+ scanMaster();
+}
+
+
+FileName const SVN::findFile(FileName const & file)
+{
+ // First we look for the .svn/entries in the same dir
+ // where we have file.
+ FileName const entries(onlyPath(file.absFileName()) + "/.svn/entries");
+ string const tmpf = onlyFileName(file.absFileName());
+ LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under svn in `" << entries
+ << "' for `" << tmpf << '\'');
+ if (entries.isReadableFile()) {
+ // Ok we are at least in a SVN dir. Parse the .svn/entries
+ // and see if we can find this file. We do a fast and
+ // dirty parse here.
+ ifstream ifs(entries.toFilesystemEncoding().c_str());
+ string line, oldline;
+ while (getline(ifs, line)) {
+ if (line == "dir" || line == "file")
+ LYXERR(Debug::LYXVC, "\tEntries: " << oldline);
+ if (oldline == tmpf && line == "file")
+ return entries;
+ oldline = line;
+ }
+ }
+ return FileName();
+}
+
+
+void SVN::scanMaster()
+{
+ // vcstatus code is somewhat superflous, until we want
+ // to implement read-only toggle for svn.
+ vcstatus = NOLOCKING;
+ if (checkLockMode()) {
+ if (isLocked()) {
+ vcstatus = LOCKED;
+ } else {
+ vcstatus = UNLOCKED;
+ }
+ }
+}
+
+
+bool SVN::checkLockMode()
+{
+ FileName tmpf = FileName::tempName("lyxvcout");
+ if (tmpf.empty()){
+ LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
+ return N_("Error: Could not generate logfile.");
+ }
+
+ LYXERR(Debug::LYXVC, "Detecting locking mode...");
+ if (doVCCommandCall("svn proplist " + quoteName(file_.onlyFileName())
+ + " > " + quoteName(tmpf.toFilesystemEncoding()),
+ file_.onlyPath()))
+ return false;
+
+ ifstream ifs(tmpf.toFilesystemEncoding().c_str());
+ string line;
+ bool ret = false;
+
+ while (ifs) {
+ getline(ifs, line);
+ LYXERR(Debug::LYXVC, line);
+ if (contains(line, "svn:needs-lock"))
+ ret = true;
+ }
+ LYXERR(Debug::LYXVC, "Locking enabled: " << ret);
+ ifs.close();
+ locked_mode_ = ret;
+ return ret;
+
+}
+
+
+bool SVN::isLocked() const
+{
+ file_.refresh();
+ return !file_.isReadOnly();
+}
+
+
+void SVN::registrer(string const & /*msg*/)
+{
+ doVCCommand("svn add -q " + quoteName(onlyFileName(owner_->absFileName())),
+ FileName(owner_->filePath()));
+}
+
+
+string SVN::checkIn(string const & msg)
+{
+ FileName tmpf = FileName::tempName("lyxvcout");
+ if (tmpf.empty()){
+ LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
+ return N_("Error: Could not generate logfile.");
+ }
+
+ doVCCommand("svn commit -m \"" + msg + "\" "
+ + quoteName(onlyFileName(owner_->absFileName()))
+ + " > " + quoteName(tmpf.toFilesystemEncoding()),
+ FileName(owner_->filePath()));
+
+ string log;
+ string res = scanLogFile(tmpf, log);
+ if (!res.empty())
+ frontend::Alert::error(_("Revision control error."),
+ _("Error when committing to repository.\n"
+ "You have to manually resolve the problem.\n"
+ "LyX will reopen the document after you press OK."));
+ else
+ fileLock(false, tmpf, log);
+
+ tmpf.erase();
+ return log.empty() ? string() : "SVN: " + log;
+}
+
+
+bool SVN::checkInEnabled()
+{
+ if (locked_mode_)
+ return isLocked();
+ else
+ return true;
+}
+
+
+bool SVN::isCheckInWithConfirmation()
+{
+ // FIXME one day common getDiff and perhaps OpMode for all backends
+
+ FileName tmpf = FileName::tempName("lyxvcout");
+ if (tmpf.empty()) {
+ LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
+ return true;
+ }
+
+ doVCCommandCall("svn diff " + quoteName(owner_->absFileName())
+ + " > " + quoteName(tmpf.toFilesystemEncoding()),
+ FileName(owner_->filePath()));
+
+ docstring diff = tmpf.fileContents("UTF-8");
+ tmpf.erase();
+
+ if (diff.empty())
+ return false;
+
+ return true;
+}
+
+
+// FIXME Correctly return code should be checked instead of this.
+// This would need another solution than just plain startscript.
+// Hint from Andre': QProcess::readAllStandardError()...
+string SVN::scanLogFile(FileName const & f, string & status)
+{
+ ifstream ifs(f.toFilesystemEncoding().c_str());
+ string line;
+
+ while (ifs) {
+ getline(ifs, line);
+ LYXERR(Debug::LYXVC, line << "\n");
+ if (!line.empty())
+ status += line + "; ";
+ if (prefixIs(line, "C ") || prefixIs(line, "CU ")
+ || contains(line, "Commit failed")) {
+ ifs.close();
+ return line;
+ }
+ if (contains(line, "svn:needs-lock")) {
+ ifs.close();
+ return line;
+ }
+ }
+ ifs.close();
+ return string();
+}
+
+
+void SVN::fileLock(bool lock, FileName const & tmpf, string &status)
+{
+ if (!locked_mode_ || (isLocked() == lock))
+ return;
+
+ string const arg = lock ? "lock " : "unlock ";
+ doVCCommand("svn "+ arg + quoteName(onlyFileName(owner_->absFileName()))
+ + " > " + quoteName(tmpf.toFilesystemEncoding()),
+ FileName(owner_->filePath()));
+
+ // Lock error messages go unfortunately on stderr and are unreachible this way.
+ ifstream ifs(tmpf.toFilesystemEncoding().c_str());
+ string line;
+ while (ifs) {
+ getline(ifs, line);
+ if (!line.empty()) status += line + "; ";
+ }
+ ifs.close();
+
+ if (!isLocked() && lock)
+ frontend::Alert::error(_("Revision control error."),
+ _("Error while acquiring write lock.\n"
+ "Another user is most probably editing\n"
+ "the current document now!\n"
+ "Also check the access to the repository."));
+ if (isLocked() && !lock)
+ frontend::Alert::error(_("Revision control error."),
+ _("Error while releasing write lock.\n"
+ "Check the access to the repository."));
+}
+
+
+string SVN::checkOut()
+{
+ FileName tmpf = FileName::tempName("lyxvcout");
+ if (tmpf.empty()) {
+ LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
+ return N_("Error: Could not generate logfile.");
+ }
+
+ doVCCommand("svn update --non-interactive " + quoteName(onlyFileName(owner_->absFileName()))
+ + " > " + quoteName(tmpf.toFilesystemEncoding()),
+ FileName(owner_->filePath()));
+
+ string log;
+ string const res = scanLogFile(tmpf, log);
+ if (!res.empty())
+ frontend::Alert::error(_("Revision control error."),
+ bformat(_("Error when updating from repository.\n"
+ "You have to manually resolve the conflicts NOW!\n'%1$s'.\n\n"
+ "After pressing OK, LyX will try to reopen the resolved document."),
+ from_local8bit(res)));
+
+ fileLock(true, tmpf, log);
+
+ tmpf.erase();
+ return log.empty() ? string() : "SVN: " + log;
+}
+
+
+bool SVN::checkOutEnabled()
+{
+ if (locked_mode_)
+ return !isLocked();
+ else
+ return true;
+}
+
+
+string SVN::repoUpdate()
+{
+ FileName tmpf = FileName::tempName("lyxvcout");
+ if (tmpf.empty()) {
+ LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
+ return N_("Error: Could not generate logfile.");
+ }
+
+ doVCCommand("svn diff " + quoteName(owner_->filePath())
+ + " > " + quoteName(tmpf.toFilesystemEncoding()),
+ FileName(owner_->filePath()));
+ docstring res = tmpf.fileContents("UTF-8");
+ if (!res.empty()) {
+ LYXERR(Debug::LYXVC, "Diff detected:\n" << res);
+ docstring const file = from_utf8(owner_->filePath());
+ docstring text = bformat(_("There were detected changes "
+ "in the working directory:\n%1$s\n\n"
+ "In case of file conflict version of the local directory files "
+ "will be preferred."
+ "\n\nContinue?"), file);
+ int ret = frontend::Alert::prompt(_("Changes detected"),
+ text, 0, 1, _("&Yes"), _("&No"), _("View &Log ..."));
+ if (ret == 2 ) {
+ dispatch(FuncRequest(LFUN_DIALOG_SHOW, "file " + tmpf.absFileName()));
+ ret = frontend::Alert::prompt(_("Changes detected"),
+ text, 0, 1, _("&Yes"), _("&No"));
+ hideDialogs("file", 0);
+ }
+ if (ret == 1 ) {
+ tmpf.erase();
+ return string();
+ }
+ }
+
+ // Reverting looks too harsh, see bug #6255.
+ // doVCCommand("svn revert -R " + quoteName(owner_->filePath())
+ // + " > " + quoteName(tmpf.toFilesystemEncoding()),
+ // FileName(owner_->filePath()));
+ // res = "Revert log:\n" + tmpf.fileContents("UTF-8");
+ doVCCommand("svn update --accept mine-full " + quoteName(owner_->filePath())
+ + " > " + quoteName(tmpf.toFilesystemEncoding()),
+ FileName(owner_->filePath()));
+ res += "Update log:\n" + tmpf.fileContents("UTF-8");
+
+ LYXERR(Debug::LYXVC, res);
+ tmpf.erase();
+ return to_utf8(res);
+}
+
+
+bool SVN::repoUpdateEnabled()
+{
+ return true;
+}
+
+
+string SVN::lockingToggle()
+{
+ FileName tmpf = FileName::tempName("lyxvcout");
+ if (tmpf.empty()) {
+ LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
+ return N_("Error: Could not generate logfile.");
+ }
+
+ int ret = doVCCommand("svn proplist " + quoteName(onlyFileName(owner_->absFileName()))
+ + " > " + quoteName(tmpf.toFilesystemEncoding()),
+ FileName(owner_->filePath()));
+ if (ret)
+ return string();