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