This commit was manufactured by cvs2svn to create tag
[lyx.git] / src / lyxvc.C
blobe4ca83683ebd70ba49987d0ac799ecf57db99347
1 #include <config.h>
3 #ifdef __GNUG__
4 #pragma implementation
5 #endif
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"
21 extern BufferList bufferlist;
22 extern void MenuWrite(Buffer *);
25 LyXVC::LyXVC()
27         backend = UNKNOWN_VCS;
28         _owner = 0;
29         browser = 0;
33 LyXVC::~LyXVC()
35         if (browser) {
36                 if (browser->LaTeXLog->visible)
37                         fl_hide_form(browser->LaTeXLog);
38                 fl_free_form(browser->LaTeXLog);
39         }
43 bool LyXVC::file_found_hook(string const & fn)
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
73         // file is not under any VCS.
74         return false;
78 bool LyXVC::file_not_found_hook(string const &)
80         // file is not under any VCS.
81         return false;
85 void LyXVC::scanMaster()
87         lyxerr[Debug::LYXVC] << "LyXVC: This file is a VC file." << endl;
89         LyXLex lex(0, 0);
90         lex.setFile(master);
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;
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         }
146 void LyXVC::setBuffer(Buffer * buf)
148         _owner = buf;
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 //        }
161 // But for 0.12 we will only support RCS.
164 void LyXVC::registrer()
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         }
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         }
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");
198 void LyXVC::checkIn()
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         }
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         }
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");
224 void LyXVC::checkOut()
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         }
234         _owner->markLyxClean();
235         doVCCommand("co -q -l \""
236                     + OnlyFilename(_owner->getFileName()) + "\"");
237         _owner->getUser()->getOwner()->getLyXFunc()->Dispatch("buffer-reload");
241 void LyXVC::revert()
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         }
261 void LyXVC::undoLast()
263         lyxerr[Debug::LYXVC] << "LyXVC: undoLast" << endl;
264         doVCCommand("rcs -o" + getVersion() + " \""
265                     + OnlyFilename(_owner->getFileName()) + "\"");
269 void LyXVC::toggleReadOnly()
271         switch (vcstat) {
272         case UNLOCKED:
273                 checkOut();
274                 break;
275         case LOCKED:
276                 checkIn();
277                 break;
278         }
282 bool LyXVC::inUse()
284         if (!master.empty())
285                 return true;
286         return false;
290 string const LyXVC::getVersion() const
292         return version;
296 string const LyXVC::getLocker() const
298         return locker;
301 // This is a hack anyway so I'll put it here in the mean time.
302 void LyXVC::logClose(FL_OBJECT * obj, long)
304         LyXVC * This = static_cast<LyXVC*>(obj->form->u_vdata);
305         fl_hide_form(This->browser->LaTeXLog);
308 // and, hack over hack, here is a C wrapper :)
309 extern "C" void C_LyXVC_logClose(FL_OBJECT * ob, long data)
311         LyXVC::logClose(ob, data);
315 void LyXVC::logUpdate(FL_OBJECT * obj, long)
317         LyXVC * This = static_cast<LyXVC*>(obj->form->u_vdata);
318         This->showLog();
321 extern "C" void C_LyXVC_logUpdate(FL_OBJECT *ob, long data)
323         LyXVC::logUpdate(ob, data);
327 void LyXVC::viewLog(string const & fil)
329         static int ow = -1, oh;
331         if (!browser) {
332                 FL_OBJECT * obj;
333                 browser = (FD_LaTeXLog *) fl_calloc(1, sizeof(*browser));
334                 
335                 browser->LaTeXLog = fl_bgn_form(FL_NO_BOX, 470, 380);
336                 browser->LaTeXLog->u_vdata = this;
337                 obj = fl_add_box(FL_UP_BOX, 0, 0, 470, 380, "");
338                 browser->browser_latexlog = fl_add_browser(FL_NORMAL_BROWSER, 10, 10, 450, 320, "");
339                 obj = fl_add_button(FL_RETURN_BUTTON, 270, 340, 90, 30, _("Close"));
340                 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
341                 fl_set_object_callback(obj, C_LyXVC_logClose, 0);
342                 obj = fl_add_button(FL_NORMAL_BUTTON,370,340,90,30,
343                                     idex(_("Update|#Uu")));
344                 fl_set_button_shortcut(obj,scex(_("Update|#Uu")),1);
345                 fl_set_object_lsize(obj,FL_NORMAL_SIZE);
346                 fl_set_object_callback(obj, C_LyXVC_logUpdate,0);
347                 fl_end_form();
348                 fl_set_form_atclose(browser->LaTeXLog, CancelCloseBoxCB, 0);
349         }
351         if (!fl_load_browser(browser->browser_latexlog, fil.c_str()))
352                 fl_add_browser_line(browser->browser_latexlog, _("No RCS History!"));
353         
354         if (browser->LaTeXLog->visible) {
355                 fl_raise_form(browser->LaTeXLog);
356         } else {
357                 fl_show_form(browser->LaTeXLog,
358                              FL_PLACE_MOUSE | FL_FREE_SIZE, FL_FULLBORDER,
359                              _("RCS History"));
360                 if (ow < 0) {
361                         ow = browser->LaTeXLog->w;
362                         oh = browser->LaTeXLog->h;
363                 }
364                 fl_set_form_minsize(browser->LaTeXLog, ow, oh);
365         }
369 void LyXVC::showLog()
371         string tmpf = tmpnam(0);
372         doVCCommand("rlog \""
373                     + OnlyFilename(_owner->getFileName()) + "\" > " + tmpf);
374         viewLog(tmpf);
375         unlink(tmpf.c_str());
379 int LyXVC::doVCCommand(string const & cmd)
381         lyxerr[Debug::LYXVC] << "doVCCommand: " << cmd << endl;
382         Systemcalls one;
383         Path p(_owner->filepath);
384         int ret = one.startscript(Systemcalls::System, cmd);
385         return ret;