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