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