]> git.lyx.org Git - lyx.git/blob - src/LyXVC.cpp
2e316d6af7fa28a08c6225af65cf986d1d4f024a
[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 "support/debug.h"
21 #include "Buffer.h"
22 #include "support/gettext.h"
23
24 #include "frontends/alert.h"
25
26 #include "support/filetools.h"
27 #include "support/lstrings.h"
28 #include "support/lyxlib.h"
29
30
31 namespace lyx {
32
33 using support::bformat;
34 using support::FileName;
35 using support::makeAbsPath;
36 using support::tempName;
37
38 using std::string;
39
40 namespace Alert = frontend::Alert;
41
42
43 LyXVC::LyXVC()
44 {
45         owner_ = 0;
46 }
47
48
49 // for the sake of boost::scoped_ptr
50 LyXVC::~LyXVC()
51 {}
52
53
54 bool LyXVC::file_found_hook(FileName const & fn)
55 {
56         FileName found_file;
57         // Check if file is under RCS
58         if (!(found_file = RCS::findFile(fn)).empty()) {
59                 vcs.reset(new RCS(found_file));
60                 vcs->owner(owner_);
61                 return true;
62         }
63         // Check if file is under CVS
64         if (!(found_file = CVS::findFile(fn)).empty()) {
65                 vcs.reset(new CVS(found_file, fn));
66                 vcs->owner(owner_);
67                 return true;
68         }
69         // file is not under any VCS.
70         return false;
71 }
72
73
74 bool LyXVC::file_not_found_hook(FileName const & fn)
75 {
76         // Check if file is under RCS
77         if (!RCS::findFile(fn).empty())
78                 return true;
79         if (!CVS::findFile(fn).empty())
80                 return true;
81         return false;
82 }
83
84
85 void LyXVC::setBuffer(Buffer * buf)
86 {
87         owner_ = buf;
88 }
89
90
91 void LyXVC::registrer()
92 {
93         FileName const filename = owner_->fileName();
94
95         // there must be a file to save
96         if (!filename.isReadableFile()) {
97                 Alert::error(_("Document not saved"),
98                              _("You must save the document "
99                                             "before it can be registered."));
100                 return;
101         }
102
103         // it is very likely here that the vcs is not created yet...
104         if (!vcs) {
105                 FileName const cvs_entries(makeAbsPath("CVS/Entries"));
106
107                 if (cvs_entries.isReadableFile()) {
108                         LYXERR(Debug::LYXVC, "LyXVC: registering "
109                                 << to_utf8(filename.displayName()) << " with CVS");
110                         vcs.reset(new CVS(cvs_entries, filename));
111
112                 } else {
113                         LYXERR(Debug::LYXVC, "LyXVC: registering "
114                                 << to_utf8(filename.displayName()) << " with RCS");
115                         vcs.reset(new RCS(filename));
116                 }
117
118                 vcs->owner(owner_);
119         }
120
121         LYXERR(Debug::LYXVC, "LyXVC: registrer");
122         docstring response;
123         bool ok = Alert::askForText(response, _("LyX VC: Initial description"),
124                         _("(no initial description)"));
125         if (!ok || response.empty()) {
126                 // should we insist on checking response.empty()?
127                 LYXERR(Debug::LYXVC, "LyXVC: user cancelled");
128                 return;
129         }
130
131         vcs->registrer(to_utf8(response));
132 }
133
134
135 void LyXVC::checkIn()
136 {
137         LYXERR(Debug::LYXVC, "LyXVC: checkIn");
138         docstring response;
139         bool ok = Alert::askForText(response, _("LyX VC: Log Message"));
140         if (ok) {
141                 if (response.empty())
142                         response = _("(no log message)");
143                 vcs->checkIn(to_utf8(response));
144         } else {
145                 LYXERR(Debug::LYXVC, "LyXVC: user cancelled");
146         }
147 }
148
149
150 void LyXVC::checkOut()
151 {
152         LYXERR(Debug::LYXVC, "LyXVC: checkOut");
153         vcs->checkOut();
154 }
155
156
157 void LyXVC::revert()
158 {
159         LYXERR(Debug::LYXVC, "LyXVC: revert");
160
161         docstring const file = owner_->fileName().displayName(20);
162         docstring text = bformat(_("Reverting to the stored version of the "
163                 "document %1$s will lose all current changes.\n\n"
164                                              "Do you want to revert to the saved version?"), file);
165         int const ret = Alert::prompt(_("Revert to stored version of document?"),
166                 text, 0, 1, _("&Revert"), _("&Cancel"));
167
168         if (ret == 0)
169                 vcs->revert();
170 }
171
172
173 void LyXVC::undoLast()
174 {
175         vcs->undoLast();
176 }
177
178
179 void LyXVC::toggleReadOnly()
180 {
181         switch (vcs->status()) {
182         case VCS::UNLOCKED:
183                 LYXERR(Debug::LYXVC, "LyXVC: toggle to locked");
184                 checkOut();
185                 break;
186         case VCS::LOCKED:
187                 LYXERR(Debug::LYXVC, "LyXVC: toggle to unlocked");
188                 checkIn();
189                 break;
190         }
191 }
192
193
194 bool LyXVC::inUse()
195 {
196         if (vcs)
197                 return true;
198         return false;
199 }
200
201
202 //string const & LyXVC::version() const
203 //{
204 //      return vcs->version();
205 //}
206
207
208 string const LyXVC::versionString() const
209 {
210         return vcs->versionString();
211 }
212
213
214 string const & LyXVC::locker() const
215 {
216         return vcs->locker();
217 }
218
219
220 string const LyXVC::getLogFile() const
221 {
222         if (!vcs)
223                 return string();
224
225         FileName const tmpf(tempName(FileName(), "lyxvclog"));
226         if (tmpf.empty()) {
227                 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
228                 return string();
229         }
230         LYXERR(Debug::LYXVC, "Generating logfile " << tmpf);
231         vcs->getLog(tmpf);
232         return tmpf.absFilename();
233 }
234
235
236 } // namespace lyx