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