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