]> git.lyx.org Git - lyx.git/blob - src/lyxvc.C
hopefully fix tex2lyx linking.
[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
30 namespace lyx {
31
32 using support::bformat;
33 using support::isFileReadable;
34 using support::makeDisplayPath;
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(string const & fn)
56 {
57         string 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(string 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::buffer(Buffer * buf)
87 {
88         owner_ = buf;
89 }
90
91
92 void LyXVC::registrer()
93 {
94         string const filename = owner_->fileName();
95
96         // there must be a file to save
97         if (!isFileReadable(filename)) {
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                 string const cvs_entries = "CVS/Entries";
107
108                 if (isFileReadable(cvs_entries)) {
109                         lyxerr[Debug::LYXVC]
110                                 << "LyXVC: registering "
111                                 << to_utf8(makeDisplayPath(filename))
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(makeDisplayPath(filename))
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 = makeDisplayPath(owner_->fileName(), 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) return true;
204         return false;
205 }
206
207
208 //string const & LyXVC::version() const
209 //{
210 //      return vcs->version();
211 //}
212
213 string const LyXVC::versionString() const
214 {
215         return vcs->versionString();
216 }
217
218
219 string const & LyXVC::locker() const
220 {
221         return vcs->locker();
222 }
223
224
225 string const LyXVC::getLogFile() const
226 {
227         if (!vcs)
228                 return string();
229
230         string tmpf = tempName(string(), "lyxvclog");
231         if (tmpf.empty()) {
232                 lyxerr[Debug::LYXVC] << "Could not generate logfile "
233                                      << tmpf << endl;
234                 return string();
235         }
236         lyxerr[Debug::LYXVC] << "Generating logfile " << tmpf << endl;
237         vcs->getLog(tmpf);
238         return tmpf;
239 }
240
241
242 } // namespace lyx