]> git.lyx.org Git - lyx.git/blob - src/lyxvc.C
0a5c1e6173d3b24ca69418f18c39567bf75ba1d4
[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 using std::endl;
21
22 LyXVC::LyXVC()
23 {
24         vcs = 0;
25         browser = 0;
26         owner_ = 0;
27 }
28
29
30 LyXVC::~LyXVC()
31 {
32         if (browser) {
33                 if (browser->LaTeXLog->visible)
34                         fl_hide_form(browser->LaTeXLog);
35                 fl_free_form(browser->LaTeXLog);
36         }
37         delete vcs;
38 }
39
40
41 bool LyXVC::file_found_hook(string const & fn)
42 {
43         string found_file;
44         // Check if file is under RCS
45         if (!(found_file = RCS::find_file(fn)).empty()) {
46                 vcs = new RCS(found_file);
47                 vcs->owner(owner_);
48                 return true;
49         }
50         // Check if file is under CVS
51         if (!(found_file = CVS::find_file(fn)).empty()) {
52                 vcs = new CVS(found_file, fn);
53                 vcs->owner(owner_);
54                 return true;
55         }
56         // file is not under any VCS.
57         return false;
58 }
59
60
61 bool LyXVC::file_not_found_hook(string const & fn)
62 {
63         // Check if file is under RCS
64         if (!RCS::find_file(fn).empty())
65                 return true;
66         if (!CVS::find_file(fn).empty())
67                 return true;
68         return false;
69 }
70
71
72 void LyXVC::buffer(Buffer * buf)
73 {
74         owner_ = buf;
75 }
76
77
78 void LyXVC::registrer()
79 {
80         // it is very likely here that the vcs is not created yet...
81         // so... we use RCS as default, later this should perhaps be
82         // a lyxrc option.
83         if (!vcs) {
84                 vcs = new RCS(owner_->fileName());
85                 vcs->owner(owner_);
86         }
87         
88         // If the document is changed, we might want to save it
89         if (!vcs->owner()->isLyxClean() && 
90             AskQuestion(_("Changes in document:"),
91                         MakeDisplayPath(vcs->owner()->fileName(), 50),
92                         _("Save document and proceed?"))) {
93                 vcs->owner()->getUser()->owner()
94                         ->getLyXFunc()->Dispatch(LFUN_MENUWRITE);
95         }
96
97         // Maybe the save fails, or we answered "no". In both cases,
98         // the document will be dirty, and we abort.
99         if (!vcs->owner()->isLyxClean()) {
100                 return;
101         }
102
103         lyxerr[Debug::LYXVC] << "LyXVC: registrer" << endl;
104         pair<bool, string> tmp =
105                 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 }