]> git.lyx.org Git - lyx.git/blob - src/lyxvc.C
4ab17afc9caa0b037cede8b6d88c26e695b6810e
[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 using std::pair;
22
23 LyXVC::LyXVC()
24 {
25         vcs = 0;
26         browser = 0;
27         owner_ = 0;
28 }
29
30
31 LyXVC::~LyXVC()
32 {
33         if (browser) {
34                 if (browser->LaTeXLog->visible)
35                         fl_hide_form(browser->LaTeXLog);
36                 fl_free_form(browser->LaTeXLog);
37         }
38         delete vcs;
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 =
106                 askForText(_("LyX VC: Initial description"),
107                            _("(no initial description)"));
108         if (!tmp.first || tmp.second.empty()) {
109                 // should we insist on checking tmp.second.empty()?
110                 lyxerr[Debug::LYXVC] << "LyXVC: user cancelled" << endl;
111                 WriteAlert(_("Info"),
112                            _("This document has NOT been registered."));
113                 return;
114         }
115         
116         vcs->registrer(tmp.second);
117 }
118
119
120 void LyXVC::checkIn()
121 {
122         // If the document is changed, we might want to save it
123         if (!vcs->owner()->isLyxClean() && 
124             AskQuestion(_("Changes in document:"),
125                         MakeDisplayPath(vcs->owner()->fileName(), 50),
126                         _("Save document and proceed?"))) {
127                 vcs->owner()->getUser()->owner()
128                         ->getLyXFunc()->Dispatch(LFUN_MENUWRITE);
129         }
130
131         // Maybe the save fails, or we answered "no". In both cases,
132         // the document will be dirty, and we abort.
133         if (!vcs->owner()->isLyxClean()) {
134                 return;
135         }
136
137         lyxerr[Debug::LYXVC] << "LyXVC: checkIn" << endl;
138         pair<bool, string> tmp = askForText(_("LyX VC: Log Message"));
139         if (tmp.first) {
140                 if (tmp.second.empty()) {
141                         tmp.second = _("(no log message)");
142                 }
143                 vcs->checkIn(tmp.second);
144         } else {
145                 lyxerr[Debug::LYXVC] << "LyXVC: user cancelled" << endl;
146         }
147 }
148
149
150 void LyXVC::checkOut()
151 {
152         lyxerr[Debug::LYXVC] << "LyXVC: checkOut" << endl;
153         if (!vcs->owner()->isLyxClean() 
154             && !AskQuestion(_("Changes in document:"),
155                            MakeDisplayPath(vcs->owner()->fileName(), 50),
156                            _("Ignore changes and proceed with check out?"))) {
157                 return;
158         }
159
160         vcs->checkOut();
161         
162 }
163
164
165 void LyXVC::revert()
166 {
167         lyxerr[Debug::LYXVC] << "LyXVC: revert" << endl;
168         // Here we should check if the buffer is dirty. And if it is
169         // we should warn the user that reverting will discard all
170         // changes made since the last check in.
171         if (AskQuestion(_("When you revert, you will loose all changes made"),
172                         _("to the document since the last check in."),
173                         _("Do you still want to do it?"))) {
174
175                 vcs->revert();
176         }
177 }
178
179
180 void LyXVC::undoLast()
181 {
182         vcs->undoLast();
183 }
184
185
186 void LyXVC::toggleReadOnly()
187 {
188         switch (vcs->status()) {
189         case VCS::UNLOCKED:
190                 lyxerr[Debug::LYXVC] << "LyXVC: toggle to locked" << endl;
191                 checkOut();
192                 break;
193         case VCS::LOCKED:
194                 lyxerr[Debug::LYXVC] << "LyXVC: toggle to unlocked" << endl;
195                 checkIn();
196                 break;
197         }
198 }
199
200
201 bool LyXVC::inUse()
202 {
203         if (vcs) return true;
204         return false;
205 }
206
207
208 string const & LyXVC::version() const
209 {
210         return vcs->version();
211 }
212
213
214 string const & LyXVC::locker() const
215 {
216         return vcs->locker();
217 }
218
219
220 // This is a hack anyway so I'll put it here in the mean time.
221 void LyXVC::logClose(FL_OBJECT * obj, long)
222 {
223         LyXVC * This = static_cast<LyXVC*>(obj->form->u_vdata);
224         fl_hide_form(This->browser->LaTeXLog);
225 }
226
227
228 // and, hack over hack, here is a C wrapper :)
229 extern "C" void C_LyXVC_logClose(FL_OBJECT * ob, long data)
230 {
231         LyXVC::logClose(ob, data);
232 }
233
234
235 void LyXVC::logUpdate(FL_OBJECT * obj, long)
236 {
237         LyXVC * This = static_cast<LyXVC*>(obj->form->u_vdata);
238         This->showLog();
239 }
240
241 extern "C" void C_LyXVC_logUpdate(FL_OBJECT *ob, long data)
242 {
243         LyXVC::logUpdate(ob, data);
244 }
245
246
247 void LyXVC::viewLog(string const & fil)
248 {
249         static int ow = -1, oh;
250
251         if (!browser) {
252                 FL_OBJECT * obj;
253                 browser = (FD_LaTeXLog *) fl_calloc(1, sizeof(*browser));
254                 
255                 browser->LaTeXLog = fl_bgn_form(FL_NO_BOX, 470, 380);
256                 browser->LaTeXLog->u_vdata = this;
257                 obj = fl_add_box(FL_UP_BOX, 0, 0, 470, 380, "");
258                 browser->browser_latexlog = fl_add_browser(FL_NORMAL_BROWSER,
259                                                            10, 10,
260                                                            450, 320, "");
261                 obj = fl_add_button(FL_RETURN_BUTTON, 270, 340, 90, 30,
262                                     _("Close"));
263                 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
264                 fl_set_object_callback(obj, C_LyXVC_logClose, 0);
265                 obj = fl_add_button(FL_NORMAL_BUTTON, 370, 340, 90, 30,
266                                     idex(_("Update|#Uu")));
267                 fl_set_button_shortcut(obj, scex(_("Update|#Uu")), 1);
268                 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
269                 fl_set_object_callback(obj, C_LyXVC_logUpdate, 0);
270                 fl_end_form();
271                 fl_set_form_atclose(browser->LaTeXLog, CancelCloseBoxCB, 0);
272         }
273
274         if (!fl_load_browser(browser->browser_latexlog, fil.c_str()))
275                 fl_add_browser_line(browser->browser_latexlog,
276                                     _("No VC History!"));
277         
278         if (browser->LaTeXLog->visible) {
279                 fl_raise_form(browser->LaTeXLog);
280         } else {
281                 fl_show_form(browser->LaTeXLog,
282                              FL_PLACE_MOUSE | FL_FREE_SIZE, FL_FULLBORDER,
283                              _("VC History"));
284                 if (ow < 0) {
285                         ow = browser->LaTeXLog->w;
286                         oh = browser->LaTeXLog->h;
287                 }
288                 fl_set_form_minsize(browser->LaTeXLog, ow, oh);
289         }
290 }
291
292
293 void LyXVC::showLog()
294 {
295         string tmpf = tmpnam(0);
296         vcs->getLog(tmpf);
297         viewLog(tmpf);
298         lyx::unlink(tmpf);
299 }