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