]> git.lyx.org Git - lyx.git/blob - src/lyxvc.C
redraw fix 1.
[lyx.git] / src / lyxvc.C
1 #include <config.h>
2
3 #ifdef __GNUG__
4 #pragma implementation
5 #endif
6
7 #include "lyxvc.h"
8 #include "vc-backend.h"
9 #include "debug.h"
10 #include "buffer.h"
11 #include "BufferView.h"
12 #include "lyxfunc.h"
13 #include "gettext.h"
14
15 #include "frontends/Alert.h"
16 #include "frontends/LyXView.h"
17
18 #include "support/filetools.h"
19 #include "support/lyxlib.h"
20
21 #include <unistd.h>
22
23 using std::endl;
24 using std::pair;
25
26 LyXVC::LyXVC()
27 {
28         vcs = 0;
29         owner_ = 0;
30 }
31
32
33 LyXVC::~LyXVC()
34 {
35         delete vcs;
36 }
37
38
39 bool LyXVC::file_found_hook(string const & fn)
40 {
41         string found_file;
42         // Check if file is under RCS
43         if (!(found_file = RCS::find_file(fn)).empty()) {
44                 vcs = new RCS(found_file);
45                 vcs->owner(owner_);
46                 return true;
47         }
48         // Check if file is under CVS
49         if (!(found_file = CVS::find_file(fn)).empty()) {
50                 vcs = new CVS(found_file, fn);
51                 vcs->owner(owner_);
52                 return true;
53         }
54         // file is not under any VCS.
55         return false;
56 }
57
58
59 bool LyXVC::file_not_found_hook(string const & fn)
60 {
61         // Check if file is under RCS
62         if (!RCS::find_file(fn).empty())
63                 return true;
64         if (!CVS::find_file(fn).empty())
65                 return true;
66         return false;
67 }
68
69
70 void LyXVC::buffer(Buffer * buf)
71 {
72         owner_ = buf;
73 }
74
75
76 void LyXVC::registrer()
77 {
78         string const filename = owner_->fileName();
79  
80         // there must be a file to save
81         if (!IsFileReadable(filename)) {
82                 Alert::alert(_("File not saved"),
83                         _("You must save the file"),
84                         _("before it can be registered."));
85                 return;
86         }
87
88         // it is very likely here that the vcs is not created yet...
89         if (!vcs) {
90                 string const cvs_entries = "CVS/Entries";
91
92                 if (IsFileReadable(cvs_entries)) {
93                         lyxerr[Debug::LYXVC]
94                                 << "LyXVC: registering "
95                                 << MakeDisplayPath(filename)
96                                 << " with CVS" << endl;
97                         vcs = new CVS(cvs_entries, filename);
98
99                 } else {
100                         lyxerr[Debug::LYXVC]
101                                 << "LyXVC: registering "
102                                 << MakeDisplayPath(filename)
103                                 << " with RCS" << endl;
104                         vcs = new RCS(filename);
105                 }
106
107                 vcs->owner(owner_);
108         }
109
110         // If the document is changed, we might want to save it
111         if (!vcs->owner()->isLyxClean() &&
112             Alert::askQuestion(_("Changes in document:"),
113                         MakeDisplayPath(filename, 50),
114                         _("Save document and proceed?"))) {
115                 vcs->owner()->getUser()->owner()
116                         ->getLyXFunc()->dispatch(LFUN_MENUWRITE);
117         }
118
119         // Maybe the save fails, or we answered "no". In both cases,
120         // the document will be dirty, and we abort.
121         if (!vcs->owner()->isLyxClean()) {
122                 return;
123         }
124
125         lyxerr[Debug::LYXVC] << "LyXVC: registrer" << endl;
126         pair<bool, string> tmp =
127                 Alert::askForText(_("LyX VC: Initial description"),
128                            _("(no initial description)"));
129         if (!tmp.first || tmp.second.empty()) {
130                 // should we insist on checking tmp.second.empty()?
131                 lyxerr[Debug::LYXVC] << "LyXVC: user cancelled" << endl;
132                 Alert::alert(_("Info"),
133                            _("This document has NOT been registered."));
134                 return;
135         }
136
137         vcs->registrer(tmp.second);
138 }
139
140
141 void LyXVC::checkIn()
142 {
143         // If the document is changed, we might want to save it
144         if (!vcs->owner()->isLyxClean() &&
145             Alert::askQuestion(_("Changes in document:"),
146                         MakeDisplayPath(vcs->owner()->fileName(), 50),
147                         _("Save document and proceed?"))) {
148                 vcs->owner()->getUser()->owner()
149                         ->getLyXFunc()->dispatch(LFUN_MENUWRITE);
150         }
151
152         // Maybe the save fails, or we answered "no". In both cases,
153         // the document will be dirty, and we abort.
154         if (!vcs->owner()->isLyxClean()) {
155                 return;
156         }
157
158         lyxerr[Debug::LYXVC] << "LyXVC: checkIn" << endl;
159         pair<bool, string> tmp = Alert::askForText(_("LyX VC: Log Message"));
160         if (tmp.first) {
161                 if (tmp.second.empty()) {
162                         tmp.second = _("(no log message)");
163                 }
164                 vcs->checkIn(tmp.second);
165         } else {
166                 lyxerr[Debug::LYXVC] << "LyXVC: user cancelled" << endl;
167         }
168 }
169
170
171 void LyXVC::checkOut()
172 {
173         lyxerr[Debug::LYXVC] << "LyXVC: checkOut" << endl;
174         if (!vcs->owner()->isLyxClean()
175             && !Alert::askQuestion(_("Changes in document:"),
176                            MakeDisplayPath(vcs->owner()->fileName(), 50),
177                            _("Ignore changes and proceed with check out?"))) {
178                 return;
179         }
180
181         vcs->checkOut();
182
183 }
184
185
186 void LyXVC::revert()
187 {
188         lyxerr[Debug::LYXVC] << "LyXVC: revert" << endl;
189         // Here we should check if the buffer is dirty. And if it is
190         // we should warn the user that reverting will discard all
191         // changes made since the last check in.
192         if (Alert::askQuestion(_("When you revert, you will loose all changes made"),
193                         _("to the document since the last check in."),
194                         _("Do you still want to do it?"))) {
195
196                 vcs->revert();
197         }
198 }
199
200
201 void LyXVC::undoLast()
202 {
203         vcs->undoLast();
204 }
205
206
207 void LyXVC::toggleReadOnly()
208 {
209         switch (vcs->status()) {
210         case VCS::UNLOCKED:
211                 lyxerr[Debug::LYXVC] << "LyXVC: toggle to locked" << endl;
212                 checkOut();
213                 break;
214         case VCS::LOCKED:
215                 lyxerr[Debug::LYXVC] << "LyXVC: toggle to unlocked" << endl;
216                 checkIn();
217                 break;
218         }
219 }
220
221
222 bool LyXVC::inUse()
223 {
224         if (vcs) return true;
225         return false;
226 }
227
228
229 //string const & LyXVC::version() const
230 //{
231 //      return vcs->version();
232 //}
233
234 string const LyXVC::versionString() const
235 {
236         return vcs->versionString();
237 }
238
239
240 string const & LyXVC::locker() const
241 {
242         return vcs->locker();
243 }
244
245
246 const string LyXVC::getLogFile() const
247 {
248         if (!vcs)
249                 return string();
250
251         string tmpf = lyx::tempName(string(), "lyxvclog");
252         lyxerr[Debug::LYXVC] << "Generating logfile " << tmpf << endl;
253         vcs->getLog(tmpf);
254         return tmpf;
255 }