]> git.lyx.org Git - lyx.git/blob - src/lyxvc.C
This commit saves the need to check for lyx::use_gui in a number of places.
[lyx.git] / src / lyxvc.C
1 /**
2  * \file lyxvc.C
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 "vc-backend.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 using lyx::support::bformat;
30 using lyx::support::isFileReadable;
31 using lyx::support::makeDisplayPath;
32 using lyx::support::tempName;
33
34 using lyx::docstring;
35
36 using std::endl;
37 using std::string;
38 using std::pair;
39
40 namespace Alert = lyx::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(string const & fn)
55 {
56         string found_file;
57         // Check if file is under RCS
58         if (!(found_file = RCS::find_file(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::find_file(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(string const & fn)
75 {
76         // Check if file is under RCS
77         if (!RCS::find_file(fn).empty())
78                 return true;
79         if (!CVS::find_file(fn).empty())
80                 return true;
81         return false;
82 }
83
84
85 void LyXVC::buffer(Buffer * buf)
86 {
87         owner_ = buf;
88 }
89
90
91 void LyXVC::registrer()
92 {
93         string const filename = owner_->fileName();
94
95         // there must be a file to save
96         if (!isFileReadable(filename)) {
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                 string const cvs_entries = "CVS/Entries";
106
107                 if (isFileReadable(cvs_entries)) {
108                         lyxerr[Debug::LYXVC]
109                                 << "LyXVC: registering "
110                                 << lyx::to_utf8(makeDisplayPath(filename))
111                                 << " with CVS" << endl;
112                         vcs.reset(new CVS(cvs_entries, filename));
113
114                 } else {
115                         lyxerr[Debug::LYXVC]
116                                 << "LyXVC: registering "
117                                 << lyx::to_utf8(makeDisplayPath(filename))
118                                 << " with RCS" << endl;
119                         vcs.reset(new RCS(filename));
120                 }
121
122                 vcs->owner(owner_);
123         }
124
125         lyxerr[Debug::LYXVC] << "LyXVC: registrer" << endl;
126         pair<bool, docstring> tmp =
127                 Alert::askForText(_("LyX VC: Initial description"),
128                            _("(no initial description)"));
129         if (!tmp.first || tmp.second.empty()) {
130                 // should we insist on checking tmp.second.empty()?
131                 lyxerr[Debug::LYXVC] << "LyXVC: user cancelled" << endl;
132                 return;
133         }
134
135         vcs->registrer(lyx::to_utf8(tmp.second));
136 }
137
138
139 void LyXVC::checkIn()
140 {
141
142         lyxerr[Debug::LYXVC] << "LyXVC: checkIn" << endl;
143         pair<bool, docstring> tmp = Alert::askForText(_("LyX VC: Log Message"));
144         if (tmp.first) {
145                 if (tmp.second.empty()) {
146                         tmp.second = _("(no log message)");
147                 }
148                 vcs->checkIn(lyx::to_utf8(tmp.second));
149         } else {
150                 lyxerr[Debug::LYXVC] << "LyXVC: user cancelled" << endl;
151         }
152 }
153
154
155 void LyXVC::checkOut()
156 {
157         lyxerr[Debug::LYXVC] << "LyXVC: checkOut" << endl;
158
159         vcs->checkOut();
160 }
161
162
163 void LyXVC::revert()
164 {
165         lyxerr[Debug::LYXVC] << "LyXVC: revert" << endl;
166
167         docstring const file = makeDisplayPath(owner_->fileName(), 20);
168         docstring text = bformat(_("Reverting to the stored version of the "
169                 "document %1$s will lose all current changes.\n\n"
170                                              "Do you want to revert to the saved version?"), file);
171         int const ret = Alert::prompt(_("Revert to stored version of document?"),
172                 text, 0, 1, _("&Revert"), _("&Cancel"));
173
174         if (ret == 0)
175                 vcs->revert();
176 }
177
178
179 void LyXVC::undoLast()
180 {
181         vcs->undoLast();
182 }
183
184
185 void LyXVC::toggleReadOnly()
186 {
187         switch (vcs->status()) {
188         case VCS::UNLOCKED:
189                 lyxerr[Debug::LYXVC] << "LyXVC: toggle to locked" << endl;
190                 checkOut();
191                 break;
192         case VCS::LOCKED:
193                 lyxerr[Debug::LYXVC] << "LyXVC: toggle to unlocked" << endl;
194                 checkIn();
195                 break;
196         }
197 }
198
199
200 bool LyXVC::inUse()
201 {
202         if (vcs) return true;
203         return false;
204 }
205
206
207 //string const & LyXVC::version() const
208 //{
209 //      return vcs->version();
210 //}
211
212 string const LyXVC::versionString() const
213 {
214         return vcs->versionString();
215 }
216
217
218 string const & LyXVC::locker() const
219 {
220         return vcs->locker();
221 }
222
223
224 string const LyXVC::getLogFile() const
225 {
226         if (!vcs)
227                 return string();
228
229         string tmpf = tempName(string(), "lyxvclog");
230         if (tmpf.empty()) {
231                 lyxerr[Debug::LYXVC] << "Could not generate logfile "
232                                      << tmpf << endl;
233                 return string();
234         }
235         lyxerr[Debug::LYXVC] << "Generating logfile " << tmpf << endl;
236         vcs->getLog(tmpf);
237         return tmpf;
238 }