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