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