]> git.lyx.org Git - lyx.git/blob - src/lyxvc.C
3097069d6652395bf4ad3c250e130d8188bdf171
[lyx.git] / src / lyxvc.C
1 #include <config.h>
2
3 #ifdef __GNUG__
4 #pragma implementation
5 #endif
6
7 #include <unistd.h>
8
9 #include FORMS_H_LOCATION
10 #include "lyxvc.h"
11 #include "vc-backend.h"
12 #include "debug.h"
13 #include "lyx_gui_misc.h"
14 #include "buffer.h"
15 #include "gettext.h"
16 #include "support/filetools.h"
17 #include "lyxfunc.h"
18 #include "LyXView.h"
19
20
21 LyXVC::LyXVC()
22 {
23         vcs = 0;
24         browser = 0;
25         owner_ = 0;
26 }
27
28
29 LyXVC::~LyXVC()
30 {
31         if (browser) {
32                 if (browser->LaTeXLog->visible)
33                         fl_hide_form(browser->LaTeXLog);
34                 fl_free_form(browser->LaTeXLog);
35         }
36         if (vcs) {
37                 delete vcs;
38         }
39 }
40
41
42 bool LyXVC::file_found_hook(string const & fn)
43 {
44         string found_file;
45         // Check if file is under RCS
46         if (!(found_file = RCS::find_file(fn)).empty()) {
47                 vcs = new RCS(found_file);
48                 vcs->owner(owner_);
49                 return true;
50         }
51         // Check if file is under CVS
52         if (!(found_file = CVS::find_file(fn)).empty()) {
53                 vcs = new CVS(found_file, fn);
54                 vcs->owner(owner_);
55                 return true;
56         }
57         // file is not under any VCS.
58         return false;
59 }
60
61
62 bool LyXVC::file_not_found_hook(string const & fn)
63 {
64         // Check if file is under RCS
65         if (!RCS::find_file(fn).empty())
66                 return true;
67         if (!CVS::find_file(fn).empty())
68                 return true;
69         return false;
70 }
71
72
73 void LyXVC::buffer(Buffer * buf)
74 {
75         owner_ = buf;
76 }
77
78
79 void LyXVC::registrer()
80 {
81         // it is very likely here that the vcs is not created yet...
82         // so... we use RCS as default, later this should perhaps be
83         // a lyxrc option.
84         if (!vcs) {
85                 vcs = new RCS(owner_->fileName());
86                 vcs->owner(owner_);
87         }
88         
89         // If the document is changed, we might want to save it
90         if (!vcs->owner()->isLyxClean() && 
91             AskQuestion(_("Changes in document:"),
92                         MakeDisplayPath(vcs->owner()->fileName(), 50),
93                         _("Save document and proceed?"))) {
94                 vcs->owner()->getUser()->owner()
95                         ->getLyXFunc()->Dispatch(LFUN_MENUWRITE);
96         }
97
98         // Maybe the save fails, or we answered "no". In both cases,
99         // the document will be dirty, and we abort.
100         if (!vcs->owner()->isLyxClean()) {
101                 return;
102         }
103
104         lyxerr[Debug::LYXVC] << "LyXVC: registrer" << endl;
105         pair<bool, string> tmp = askForText(_("LyX VC: Initial description"),
106                                             _("(no initial description)"));
107         if (!tmp.first || tmp.second.empty()) {
108                 // should we insist on checking tmp.second.empty()?
109                 lyxerr[Debug::LYXVC] << "LyXVC: user cancelled" << endl;
110                 WriteAlert(_("Info"),
111                            _("This document has NOT been registered."));
112                 return;
113         }
114         
115         vcs->registrer(tmp.second);
116 }
117
118
119 void LyXVC::checkIn()
120 {
121         // If the document is changed, we might want to save it
122         if (!vcs->owner()->isLyxClean() && 
123             AskQuestion(_("Changes in document:"),
124                         MakeDisplayPath(vcs->owner()->fileName(), 50),
125                         _("Save document and proceed?"))) {
126                 vcs->owner()->getUser()->owner()
127                         ->getLyXFunc()->Dispatch(LFUN_MENUWRITE);
128         }
129
130         // Maybe the save fails, or we answered "no". In both cases,
131         // the document will be dirty, and we abort.
132         if (!vcs->owner()->isLyxClean()) {
133                 return;
134         }
135
136         lyxerr[Debug::LYXVC] << "LyXVC: checkIn" << endl;
137         pair<bool, string> tmp = askForText(_("LyX VC: Log Message"));
138         if (tmp.first) {
139                 if (tmp.second.empty()) {
140                         tmp.second = _("(no log message)");
141                 }
142                 vcs->checkIn(tmp.second);
143         } else {
144                 lyxerr[Debug::LYXVC] << "LyXVC: user cancelled" << endl;
145         }
146 }
147
148
149 void LyXVC::checkOut()
150 {
151         lyxerr[Debug::LYXVC] << "LyXVC: checkOut" << endl;
152         if (!vcs->owner()->isLyxClean() 
153             && !AskQuestion(_("Changes in document:"),
154                            MakeDisplayPath(vcs->owner()->fileName(), 50),
155                            _("Ignore changes and proceed with check out?"))) {
156                 return;
157         }
158
159         vcs->checkOut();
160         
161 }
162
163
164 void LyXVC::revert()
165 {
166         lyxerr[Debug::LYXVC] << "LyXVC: revert" << endl;
167         // Here we should check if the buffer is dirty. And if it is
168         // we should warn the user that reverting will discard all
169         // changes made since the last check in.
170         if (AskQuestion(_("When you revert, you will loose all changes made"),
171                         _("to the document since the last check in."),
172                         _("Do you still want to do it?"))) {
173
174                 vcs->revert();
175         }
176 }
177
178
179 void LyXVC::undoLast()
180 {
181         vcs->undoLast();
182 }
183
184
185 void LyXVC::toggleReadOnly()
186 {
187         switch (vcs->stat()) {
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
213 string const & LyXVC::locker() const
214 {
215         return vcs->locker();
216 }
217
218
219 // This is a hack anyway so I'll put it here in the mean time.
220 void LyXVC::logClose(FL_OBJECT * obj, long)
221 {
222         LyXVC * This = static_cast<LyXVC*>(obj->form->u_vdata);
223         fl_hide_form(This->browser->LaTeXLog);
224 }
225
226
227 // and, hack over hack, here is a C wrapper :)
228 extern "C" void C_LyXVC_logClose(FL_OBJECT * ob, long data)
229 {
230         LyXVC::logClose(ob, data);
231 }
232
233
234 void LyXVC::logUpdate(FL_OBJECT * obj, long)
235 {
236         LyXVC * This = static_cast<LyXVC*>(obj->form->u_vdata);
237         This->showLog();
238 }
239
240 extern "C" void C_LyXVC_logUpdate(FL_OBJECT *ob, long data)
241 {
242         LyXVC::logUpdate(ob, data);
243 }
244
245
246 void LyXVC::viewLog(string const & fil)
247 {
248         static int ow = -1, oh;
249
250         if (!browser) {
251                 FL_OBJECT * obj;
252                 browser = (FD_LaTeXLog *) fl_calloc(1, sizeof(*browser));
253                 
254                 browser->LaTeXLog = fl_bgn_form(FL_NO_BOX, 470, 380);
255                 browser->LaTeXLog->u_vdata = this;
256                 obj = fl_add_box(FL_UP_BOX, 0, 0, 470, 380, "");
257                 browser->browser_latexlog = fl_add_browser(FL_NORMAL_BROWSER,
258                                                            10, 10,
259                                                            450, 320, "");
260                 obj = fl_add_button(FL_RETURN_BUTTON, 270, 340, 90, 30,
261                                     _("Close"));
262                 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
263                 fl_set_object_callback(obj, C_LyXVC_logClose, 0);
264                 obj = fl_add_button(FL_NORMAL_BUTTON, 370, 340, 90, 30,
265                                     idex(_("Update|#Uu")));
266                 fl_set_button_shortcut(obj, scex(_("Update|#Uu")), 1);
267                 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
268                 fl_set_object_callback(obj, C_LyXVC_logUpdate, 0);
269                 fl_end_form();
270                 fl_set_form_atclose(browser->LaTeXLog, CancelCloseBoxCB, 0);
271         }
272
273         if (!fl_load_browser(browser->browser_latexlog, fil.c_str()))
274                 fl_add_browser_line(browser->browser_latexlog,
275                                     _("No VC History!"));
276         
277         if (browser->LaTeXLog->visible) {
278                 fl_raise_form(browser->LaTeXLog);
279         } else {
280                 fl_show_form(browser->LaTeXLog,
281                              FL_PLACE_MOUSE | FL_FREE_SIZE, FL_FULLBORDER,
282                              _("VC History"));
283                 if (ow < 0) {
284                         ow = browser->LaTeXLog->w;
285                         oh = browser->LaTeXLog->h;
286                 }
287                 fl_set_form_minsize(browser->LaTeXLog, ow, oh);
288         }
289 }
290
291
292 void LyXVC::showLog()
293 {
294         string tmpf = tmpnam(0);
295         vcs->getLog(tmpf);
296         viewLog(tmpf);
297         unlink(tmpf.c_str());
298 }