]> git.lyx.org Git - lyx.git/blob - src/lyxvc.C
31aca999eac6133cc80efc98b1b3f600969f4c49
[lyx.git] / src / lyxvc.C
1 #include <config.h>
2
3 #ifdef __GNUG__
4 #pragma implementation
5 #endif
6
7 #include FORMS_H_LOCATION
8 #include "lyxvc.h"
9 #include "debug.h"
10 #include "lyx_gui_misc.h"
11 #include "bufferlist.h"
12 #include "support/syscall.h"
13 #include "support/path.h"
14 #include "support/filetools.h"
15 #include "support/FileInfo.h"
16 #include "gettext.h"
17 #include "LyXView.h"
18 #include "lyxfunc.h"
19 #include "latexoptions.h"
20
21 extern BufferList bufferlist;
22 extern void MenuWrite(Buffer *);
23
24 #if 0
25 extern bool gsworking();
26 #endif
27
28 LyXVC::LyXVC()
29 {
30         backend = UNKNOWN_VCS;
31         _owner = 0;
32         browser = 0;
33 }
34
35
36 LyXVC::~LyXVC()
37 {
38         if (browser) {
39                 if (browser->LaTeXLog->visible)
40                         fl_hide_form(browser->LaTeXLog);
41                 fl_free_form(browser->LaTeXLog);
42         }
43 }
44
45
46 bool LyXVC::file_found_hook(string const & fn)
47 {
48         string tmp(fn);
49         FileInfo f;
50         // Check if *,v exists.
51         tmp += ",v";
52         lyxerr[Debug::LYXVC] << "Checking if file is under vc: "
53                              << tmp << endl;
54         if (f.newFile(tmp).readable()) {
55                 lyxerr[Debug::LYXVC] << "Yes it is under vc." << endl;
56                 master = tmp;
57                 backend = RCS_VCS;
58                 scanMaster();
59                 return true;
60         } else {
61                 // Check if RCS/*,v exists.
62                 tmp = AddName(AddPath(OnlyPath(fn), "RCS"), fn);
63                 tmp += ",v";
64                 lyxerr[Debug::LYXVC] << "Checking if file is under vc: "
65                                      << tmp << endl;
66                 if (f.newFile(tmp).readable()) {
67                         lyxerr[Debug::LYXVC] << "Yes it is under vc."<< endl;
68                         master = tmp;
69                         backend = RCS_VCS;
70                         scanMaster();
71                         return true;
72                 }
73         }
74         // If either one, return true
75
76         // file is not under any VCS.
77         return false;
78 }
79
80
81 bool LyXVC::file_not_found_hook(string const &)
82 {
83         // file is not under any VCS.
84         return false;
85 }
86
87
88 void LyXVC::scanMaster()
89 {
90         lyxerr[Debug::LYXVC] << "LyXVC: This file is a VC file." << endl;
91
92         LyXLex lex(0, 0);
93         lex.setFile(master);
94
95         string token;
96         bool read_enough = false;
97         while (lex.IsOK() && !read_enough) {
98                 lex.next();
99                 token = lex.GetString();
100                 
101                 lyxerr[Debug::LYXVC] <<"LyXVC::scanMaster: current lex text: `"
102                                      << token << "'" << endl;
103
104                 if (token.empty())
105                         continue;
106                 else if (token == "head") {
107                         // get version here
108                         lex.next();
109                         string tmv = strip(lex.GetString(), ';');
110                         version = tmv;
111                 } else if (contains(token, "access")
112                            || contains(token, "symbols")
113                            || contains(token, "strict")) {
114                         // nothing
115                 } else if (contains(token, "locks")) {
116                         // get locker here
117                         if (contains(token, ";")) {
118                                 locker = "Unlocked";
119                                 vcstat = UNLOCKED;
120                                 continue;
121                         }
122                         string tmpt, s1, s2;
123                         do {
124                                 lex.next();
125                                 s1 = strip(tmpt = lex.GetString(), ';');
126                                 // tmp is now in the format <user>:<version>
127                                 s1 = split(s1, s2, ':');
128                                 // s2 is user, and s1 is version
129                                 if (s1 == version) {
130                                         locker = s2;
131                                         vcstat = LOCKED;
132                                         break;
133                                 }
134                         } while (!contains(tmpt, ";"));
135                         
136                 } else if (token == "comment") {
137                         // we don't need to read any further than this.
138                         read_enough = true;
139                 } else {
140                         // unexpected
141                         lyxerr[Debug::LYXVC]
142                                 << "LyXVC::scanMaster(): unexpected token"
143                                 << endl;
144                 }
145         }
146 }
147
148
149 void LyXVC::setBuffer(Buffer *buf)
150 {
151         _owner = buf;
152 }
153
154
155 //
156 // I will probably add some backend_xxxx functions later to perform the
157 // version control system specific commands. Something like:
158 // void backend_revert(<params>) {
159 //        if (backend == "RCS") {
160 //        } else if (backend == "CVS") {
161 //        } else if (backend == "SCCS") {
162 //        }
163 //
164 // But for 0.12 we will only support RCS.
165 //
166
167 void LyXVC::registrer()
168 {
169         // If the document is changed, we might want to save it
170         if (!_owner->isLyxClean() && 
171             AskQuestion(_("Changes in document:"),
172                         MakeDisplayPath(_owner->getFileName(),50),
173                         _("Save document and proceed?"))) {
174                 MenuWrite(_owner);
175         }
176
177         // Maybe the save fails, or we answered "no". In both cases,
178         // the document will be dirty, and we abort.
179         if (!_owner->isLyxClean()) {
180                 return;
181         }
182
183         lyxerr[Debug::LYXVC] << "LyXVC: registrer" << endl;
184         string tmp = askForText(_("LyX VC: Initial description"),
185                                  _("(no initial description)"));
186         if (tmp.empty()) {
187                 lyxerr[Debug::LYXVC] << "LyXVC: user cancelled" << endl;
188                 WriteAlert(_("Info"), _("This document has NOT been registered."));
189                 return;
190         }
191         string cmd = "ci -q -u -i -t-\"";
192         cmd += tmp;
193         cmd += "\" \"";
194         cmd += OnlyFilename(_owner->getFileName());
195         cmd += "\"";
196         doVCCommand(cmd);
197         _owner->getUser()->getOwner()->getLyXFunc()->Dispatch("buffer-reload");
198 }
199
200
201 void LyXVC::checkIn()
202 {
203         // If the document is changed, we might want to save it
204         if (!_owner->isLyxClean() && 
205             AskQuestion(_("Changes in document:"),
206                         MakeDisplayPath(_owner->getFileName(),50),
207                         _("Save document and proceed?"))) {
208                 MenuWrite(_owner);
209         }
210
211         // Maybe the save fails, or we answered "no". In both cases,
212         // the document will be dirty, and we abort.
213         if (!_owner->isLyxClean()) {
214                 return;
215         }
216
217         lyxerr[Debug::LYXVC] << "LyXVC: checkIn" << endl;
218         _owner->getUser()->getOwner()->getLyXFunc()->Dispatch(LFUN_MENUWRITE);
219         string tmp = askForText(_("LyX VC: Log Message"));
220         if (tmp.empty()) tmp = "(no log msg)";
221         doVCCommand("ci -q -u -m\"" + tmp + "\" \""
222                     + OnlyFilename(_owner->getFileName()) + "\"");
223         _owner->getUser()->getOwner()->getLyXFunc()->Dispatch("buffer-reload");
224 }
225
226
227 void LyXVC::checkOut()
228 {
229         lyxerr[Debug::LYXVC] << "LyXVC: checkOut" << endl;
230         if (!_owner->isLyxClean() 
231             && !AskQuestion(_("Changes in document:"),
232                            MakeDisplayPath(_owner->getFileName(),50),
233                            _("Ignore changes and proceed with check out?"))) {
234                 return;
235         }
236
237         _owner->markLyxClean();
238         doVCCommand("co -q -l \""
239                     + OnlyFilename(_owner->getFileName()) + "\"");
240         _owner->getUser()->getOwner()->getLyXFunc()->Dispatch("buffer-reload");
241 }
242
243
244 void LyXVC::revert()
245 {
246         lyxerr[Debug::LYXVC] << "LyXVC: revert" << endl;
247         // Here we should check if the buffer is dirty. And if it is
248         // we should warn the user that reverting will discard all
249         // changes made since the last check in.
250         if (AskQuestion(_("When you revert, you will loose all changes made"),
251                         _("to the document since the last check in."),
252                         _("Do you still want to do it?"))) {
253                 
254                 doVCCommand("co -f -u" + getVersion() + " \""
255                             + OnlyFilename(_owner->getFileName()) + "\"");
256                 // We ignore changes and just reload!
257                 _owner->markLyxClean();
258                 _owner->getUser()->getOwner()->
259                         getLyXFunc()->Dispatch("buffer-reload");
260         }
261 }
262
263
264 void LyXVC::undoLast()
265 {
266         lyxerr[Debug::LYXVC] << "LyXVC: undoLast" << endl;
267         doVCCommand("rcs -o" + getVersion() + " \""
268                     + OnlyFilename(_owner->getFileName()) + "\"");
269 }
270
271
272 void LyXVC::toggleReadOnly()
273 {
274         switch (vcstat) {
275         case UNLOCKED:
276                 checkOut();
277                 break;
278         case LOCKED:
279                 checkIn();
280                 break;
281         }
282 }
283
284
285 bool LyXVC::inUse()
286 {
287         if (!master.empty())
288                 return true;
289         return false;
290 }
291
292
293 string const LyXVC::getVersion() const
294 {
295         return version;
296 }
297
298
299 string const LyXVC::getLocker() const
300 {
301         return locker;
302 }
303
304 // This is a hack anyway so I'll put it here in the mean time.
305 void LyXVC::logClose(FL_OBJECT *obj, long)
306 {
307         LyXVC *This = (LyXVC*)obj->form->u_vdata;
308         fl_hide_form(This->browser->LaTeXLog);
309 }
310
311 // and, hack over hack, here is a C wrapper :)
312 extern "C" void C_LyXVC_logClose(FL_OBJECT *ob, long data)
313 {
314         LyXVC::logClose(ob, data);
315 }
316
317
318 void LyXVC::logUpdate(FL_OBJECT *obj, long)
319 {
320         LyXVC *This = (LyXVC*)obj->form->u_vdata;
321         This->showLog();
322 }
323
324 extern "C" void C_LyXVC_logUpdate(FL_OBJECT *ob, long data)
325 {
326         LyXVC::logUpdate(ob, data);
327 }
328
329
330 void LyXVC::viewLog(string const & fil)
331 {
332         static int ow = -1, oh;
333
334         if (!browser) {
335                 FL_OBJECT *obj;
336                 browser = (FD_LaTeXLog *) fl_calloc(1, sizeof(*browser));
337                 
338                 browser->LaTeXLog = fl_bgn_form(FL_NO_BOX, 470, 380);
339                 browser->LaTeXLog->u_vdata = (void*)this;
340                 obj = fl_add_box(FL_UP_BOX, 0, 0, 470, 380, "");
341                 browser->browser_latexlog = fl_add_browser(FL_NORMAL_BROWSER, 10, 10, 450, 320, "");
342                 obj = fl_add_button(FL_RETURN_BUTTON, 270, 340, 90, 30, _("Close"));
343                 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
344                 fl_set_object_callback(obj, C_LyXVC_logClose, 0);
345                 obj = fl_add_button(FL_NORMAL_BUTTON,370,340,90,30,
346                                     idex(_("Update|#Uu")));
347                 fl_set_button_shortcut(obj,scex(_("Update|#Uu")),1);
348                 fl_set_object_lsize(obj,FL_NORMAL_SIZE);
349                 fl_set_object_callback(obj, C_LyXVC_logUpdate,0);
350                 fl_end_form();
351                 fl_set_form_atclose(browser->LaTeXLog, CancelCloseBoxCB, 0);
352         }
353
354         if (!fl_load_browser(browser->browser_latexlog, fil.c_str()))
355                 fl_add_browser_line(browser->browser_latexlog, _("No RCS History!"));
356         
357         if (browser->LaTeXLog->visible) {
358                 fl_raise_form(browser->LaTeXLog);
359         } else {
360                 fl_show_form(browser->LaTeXLog,
361                              FL_PLACE_MOUSE | FL_FREE_SIZE, FL_FULLBORDER,
362                              _("RCS History"));
363                 if (ow < 0) {
364                         ow = browser->LaTeXLog->w;
365                         oh = browser->LaTeXLog->h;
366                 }
367                 fl_set_form_minsize(browser->LaTeXLog, ow, oh);
368         }
369 }
370
371
372 void LyXVC::showLog()
373 {
374         // This I really don't like, but we'll look at this problem
375         // in 0.13. Then we can make a clean solution.
376 #if 0
377         if (gsworking()) {
378                 WriteAlert(_("Sorry, can't do this while"
379                              " pictures are being rendered."),
380                            _("Please wait a few seconds for"
381                              " this to finish and try again."),
382                            _("(or kill runaway gs processes"
383                              " by hand and try again.)"));
384                 return;
385         }
386         extern pid_t isp_pid; // from spellchecker.C
387         if(isp_pid != -1) {
388                 WriteAlert(_("Can't do this while the"
389                              " spellchecker is running."),
390                            _("Stop the spellchecker first."));
391                 return;
392         }
393 #endif
394         string tmpf = tmpnam(0);
395         doVCCommand("rlog \""
396                     + OnlyFilename(_owner->getFileName()) + "\" > " + tmpf);
397         viewLog(tmpf);
398         unlink(tmpf.c_str());
399 }
400
401
402 int LyXVC::doVCCommand(string const & cmd)
403 {
404         lyxerr[Debug::LYXVC] << "doVCCommand: " << cmd << endl;
405         Systemcalls one;
406         Path p(_owner->filepath);
407         int ret = one.startscript(Systemcalls::System, cmd);
408         return ret;
409 }