LyX 1.6.0 release candidate 4 (rc4)
[lyx.git] / src / LyXVC.cpp
blob5b510ae0f3ac07c7176c5dc3ca602c074aabf893
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.
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
13 * Full author contact details are available in file CREDITS.
16 #include <config.h>
18 #include "LyXVC.h"
19 #include "VCBackend.h"
20 #include "Buffer.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"
29 using namespace std;
30 using namespace lyx::support;
32 namespace lyx {
34 namespace Alert = frontend::Alert;
37 LyXVC::LyXVC()
39 owner_ = 0;
43 // for the sake of boost::scoped_ptr
44 LyXVC::~LyXVC()
48 bool LyXVC::file_found_hook(FileName const & fn)
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;
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;
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;
70 // file is not under any VCS.
71 return false;
75 bool LyXVC::file_not_found_hook(FileName const & fn)
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;
88 void LyXVC::setBuffer(Buffer * buf)
90 owner_ = buf;
94 void LyXVC::registrer()
96 FileName const filename = owner_->fileName();
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;
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");
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));
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));
122 } else {
123 LYXERR(Debug::LYXVC, "LyXVC: registering "
124 << to_utf8(filename.displayName()) << " with RCS");
125 vcs.reset(new RCS(filename));
128 vcs->owner(owner_);
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 || response.empty()) {
136 // should we insist on checking response.empty()?
137 LYXERR(Debug::LYXVC, "LyXVC: user cancelled");
138 return;
141 vcs->registrer(to_utf8(response));
145 string LyXVC::checkIn()
147 LYXERR(Debug::LYXVC, "LyXVC: checkIn");
148 docstring response;
149 string log;
150 bool ok = Alert::askForText(response, _("LyX VC: Log Message"));
151 if (ok) {
152 if (response.empty())
153 response = _("(no log message)");
154 log = vcs->checkIn(to_utf8(response));
155 } else {
156 LYXERR(Debug::LYXVC, "LyXVC: user cancelled");
158 return log;
162 string LyXVC::checkOut()
164 //RCS allows checkOut only in ReadOnly mode
165 if (vcs->toggleReadOnlyEnabled() && !owner_->isReadonly()) return string();
167 LYXERR(Debug::LYXVC, "LyXVC: checkOut");
168 return vcs->checkOut();
172 void LyXVC::revert()
174 LYXERR(Debug::LYXVC, "LyXVC: revert");
176 docstring const file = owner_->fileName().displayName(20);
177 docstring text = bformat(_("Reverting to the stored version of the "
178 "document %1$s will lose all current changes.\n\n"
179 "Do you want to revert to the older version?"), file);
180 int const ret = Alert::prompt(_("Revert to stored version of document?"),
181 text, 0, 1, _("&Revert"), _("&Cancel"));
183 if (ret == 0)
184 vcs->revert();
188 void LyXVC::undoLast()
190 vcs->undoLast();
194 void LyXVC::toggleReadOnly()
196 if (!vcs->toggleReadOnlyEnabled())
197 return;
199 switch (vcs->status()) {
200 case VCS::UNLOCKED:
201 LYXERR(Debug::LYXVC, "LyXVC: toggle to locked");
202 checkOut();
203 break;
204 case VCS::LOCKED:
205 LYXERR(Debug::LYXVC, "LyXVC: toggle to unlocked");
206 checkIn();
207 break;
212 bool LyXVC::inUse()
214 if (vcs)
215 return true;
216 return false;
220 //string const & LyXVC::version() const
222 // return vcs->version();
226 string const LyXVC::versionString() const
228 return vcs->versionString();
232 string const & LyXVC::locker() const
234 return vcs->locker();
238 string const LyXVC::getLogFile() const
240 if (!vcs)
241 return string();
243 FileName const tmpf = FileName::tempName("lyxvclog");
244 if (tmpf.empty()) {
245 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
246 return string();
248 LYXERR(Debug::LYXVC, "Generating logfile " << tmpf);
249 vcs->getLog(tmpf);
250 return tmpf.absFilename();
254 bool LyXVC::checkOutEnabled()
256 return vcs && vcs->checkOutEnabled();
260 bool LyXVC::checkInEnabled()
262 return vcs && vcs->checkInEnabled();
266 bool LyXVC::undoLastEnabled()
268 return vcs && vcs->undoLastEnabled();
272 } // namespace lyx