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