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