]> git.lyx.org Git - lyx.git/blob - src/lyxvc.C
reverse last change
[lyx.git] / src / lyxvc.C
1 #include <config.h>
2
3 #ifdef __GNUG__
4 #pragma implementation
5 #endif
6
7 #include "lyxvc.h"
8 #include "vc-backend.h"
9 #include "debug.h"
10 #include "buffer.h"
11 #include "BufferView.h"
12 #include "gettext.h"
13 #include "funcrequest.h"
14
15 #include "frontends/Alert.h"
16 #include "frontends/LyXView.h"
17
18 #include "support/filetools.h"
19 #include "support/lyxlib.h"
20
21 #include <unistd.h>
22
23 using std::endl;
24 using std::pair;
25
26 LyXVC::LyXVC()
27 {
28         vcs = 0;
29         owner_ = 0;
30 }
31
32
33 LyXVC::~LyXVC()
34 {
35         delete vcs;
36 }
37
38
39 bool LyXVC::file_found_hook(string const & fn)
40 {
41         string found_file;
42         // Check if file is under RCS
43         if (!(found_file = RCS::find_file(fn)).empty()) {
44                 vcs = new RCS(found_file);
45                 vcs->owner(owner_);
46                 return true;
47         }
48         // Check if file is under CVS
49         if (!(found_file = CVS::find_file(fn)).empty()) {
50                 vcs = new CVS(found_file, fn);
51                 vcs->owner(owner_);
52                 return true;
53         }
54         // file is not under any VCS.
55         return false;
56 }
57
58
59 bool LyXVC::file_not_found_hook(string const & fn)
60 {
61         // Check if file is under RCS
62         if (!RCS::find_file(fn).empty())
63                 return true;
64         if (!CVS::find_file(fn).empty())
65                 return true;
66         return false;
67 }
68
69
70 void LyXVC::buffer(Buffer * buf)
71 {
72         owner_ = buf;
73 }
74
75
76 void LyXVC::registrer()
77 {
78         string const filename = owner_->fileName();
79
80         // there must be a file to save
81         if (!IsFileReadable(filename)) {
82                 Alert::alert(_("File not saved"),
83                         _("You must save the file"),
84                         _("before it can be registered."));
85                 return;
86         }
87
88         // it is very likely here that the vcs is not created yet...
89         if (!vcs) {
90                 string const cvs_entries = "CVS/Entries";
91
92                 if (IsFileReadable(cvs_entries)) {
93                         lyxerr[Debug::LYXVC]
94                                 << "LyXVC: registering "
95                                 << MakeDisplayPath(filename)
96                                 << " with CVS" << endl;
97                         vcs = new CVS(cvs_entries, filename);
98
99                 } else {
100                         lyxerr[Debug::LYXVC]
101                                 << "LyXVC: registering "
102                                 << MakeDisplayPath(filename)
103                                 << " with RCS" << endl;
104                         vcs = new RCS(filename);
105                 }
106
107                 vcs->owner(owner_);
108         }
109
110         // If the document is changed, we might want to save it
111         if (!vcs->owner()->isClean() &&
112             Alert::askQuestion(_("Changes in document:"),
113                         MakeDisplayPath(filename, 50),
114                         _("Save document and proceed?"))) {
115                 vcs->owner()->getUser()->owner()
116                         ->dispatch(FuncRequest(LFUN_MENUWRITE));
117         }
118
119         // Maybe the save fails, or we answered "no". In both cases,
120         // the document will be dirty, and we abort.
121         if (!vcs->owner()->isClean())
122                 return;
123
124         lyxerr[Debug::LYXVC] << "LyXVC: registrer" << endl;
125         pair<bool, string> tmp =
126                 Alert::askForText(_("LyX VC: Initial description"),
127                            _("(no initial description)"));
128         if (!tmp.first || tmp.second.empty()) {
129                 // should we insist on checking tmp.second.empty()?
130                 lyxerr[Debug::LYXVC] << "LyXVC: user cancelled" << endl;
131                 Alert::alert(_("Info"),
132                            _("This document has NOT been registered."));
133                 return;
134         }
135
136         vcs->registrer(tmp.second);
137 }
138
139
140 void LyXVC::checkIn()
141 {
142         // If the document is changed, we might want to save it
143         if (!vcs->owner()->isClean() &&
144             Alert::askQuestion(_("Changes in document:"),
145                         MakeDisplayPath(vcs->owner()->fileName(), 50),
146                         _("Save document and proceed?"))) {
147                 vcs->owner()->getUser()->owner()
148                         ->dispatch(FuncRequest(LFUN_MENUWRITE));
149         }
150
151         // Maybe the save fails, or we answered "no". In both cases,
152         // the document will be dirty, and we abort.
153         if (!vcs->owner()->isClean())
154                 return;
155
156         lyxerr[Debug::LYXVC] << "LyXVC: checkIn" << endl;
157         pair<bool, string> tmp = Alert::askForText(_("LyX VC: Log Message"));
158         if (tmp.first) {
159                 if (tmp.second.empty()) {
160                         tmp.second = _("(no log message)");
161                 }
162                 vcs->checkIn(tmp.second);
163         } else {
164                 lyxerr[Debug::LYXVC] << "LyXVC: user cancelled" << endl;
165         }
166 }
167
168
169 void LyXVC::checkOut()
170 {
171         lyxerr[Debug::LYXVC] << "LyXVC: checkOut" << endl;
172         if (!vcs->owner()->isClean()
173             && !Alert::askQuestion(_("Changes in document:"),
174                            MakeDisplayPath(vcs->owner()->fileName(), 50),
175                            _("Ignore changes and proceed with check out?"))) {
176                 return;
177         }
178
179         vcs->checkOut();
180
181 }
182
183
184 void LyXVC::revert()
185 {
186         lyxerr[Debug::LYXVC] << "LyXVC: revert" << endl;
187         // Here we should check if the buffer is dirty. And if it is
188         // we should warn the user that reverting will discard all
189         // changes made since the last check in.
190         if (Alert::askQuestion(_("When you revert, you will loose all changes made"),
191                         _("to the document since the last check in."),
192                         _("Do you still want to do it?"))) {
193
194                 vcs->revert();
195         }
196 }
197
198
199 void LyXVC::undoLast()
200 {
201         vcs->undoLast();
202 }
203
204
205 void LyXVC::toggleReadOnly()
206 {
207         switch (vcs->status()) {
208         case VCS::UNLOCKED:
209                 lyxerr[Debug::LYXVC] << "LyXVC: toggle to locked" << endl;
210                 checkOut();
211                 break;
212         case VCS::LOCKED:
213                 lyxerr[Debug::LYXVC] << "LyXVC: toggle to unlocked" << endl;
214                 checkIn();
215                 break;
216         }
217 }
218
219
220 bool LyXVC::inUse()
221 {
222         if (vcs) return true;
223         return false;
224 }
225
226
227 //string const & LyXVC::version() const
228 //{
229 //      return vcs->version();
230 //}
231
232 string const LyXVC::versionString() const
233 {
234         return vcs->versionString();
235 }
236
237
238 string const & LyXVC::locker() const
239 {
240         return vcs->locker();
241 }
242
243
244 const string LyXVC::getLogFile() const
245 {
246         if (!vcs)
247                 return string();
248
249         string tmpf = lyx::tempName(string(), "lyxvclog");
250         lyxerr[Debug::LYXVC] << "Generating logfile " << tmpf << endl;
251         vcs->getLog(tmpf);
252         return tmpf;
253 }