]> git.lyx.org Git - lyx.git/blob - src/lyxvc.C
the doxygen patch
[lyx.git] / src / lyxvc.C
1 #include <config.h>
2
3 #include "lyxvc.h"
4 #include "vc-backend.h"
5 #include "debug.h"
6 #include "buffer.h"
7 #include "BufferView.h"
8 #include "gettext.h"
9 #include "funcrequest.h"
10
11 #include "frontends/Alert.h"
12 #include "frontends/LyXView.h"
13
14 #include "support/filetools.h"
15 #include "support/lyxlib.h"
16
17 #include <unistd.h>
18
19 using std::endl;
20 using std::pair;
21
22
23 /* WARNING: Several of the vcs-> methods end up
24  * deleting this object via BufferView::reload() !
25  */
26
27 LyXVC::LyXVC()
28 {
29         vcs = 0;
30         owner_ = 0;
31 }
32
33
34 LyXVC::~LyXVC()
35 {
36         delete vcs;
37 }
38
39
40 bool LyXVC::file_found_hook(string const & fn)
41 {
42         string found_file;
43         // Check if file is under RCS
44         if (!(found_file = RCS::find_file(fn)).empty()) {
45                 vcs = new RCS(found_file);
46                 vcs->owner(owner_);
47                 return true;
48         }
49         // Check if file is under CVS
50         if (!(found_file = CVS::find_file(fn)).empty()) {
51                 vcs = new CVS(found_file, fn);
52                 vcs->owner(owner_);
53                 return true;
54         }
55         // file is not under any VCS.
56         return false;
57 }
58
59
60 bool LyXVC::file_not_found_hook(string const & fn)
61 {
62         // Check if file is under RCS
63         if (!RCS::find_file(fn).empty())
64                 return true;
65         if (!CVS::find_file(fn).empty())
66                 return true;
67         return false;
68 }
69
70
71 void LyXVC::buffer(Buffer * buf)
72 {
73         owner_ = buf;
74 }
75
76
77 bool LyXVC::ensureClean()
78 {
79         if (owner_->isClean())
80                 return true;
81
82         string const file = MakeDisplayPath(owner_->fileName(), 30);
83         string text = bformat(_("The document %1$s has unsaved changes.\n\n"
84                 "Do you want to save the document?"), file);
85         int const ret = Alert::prompt(_("Save changed document?"),
86                 text, 0, 1, _("&Save"), _("&Cancel"));
87
88         if (ret == 0) {
89                 vcs->owner()->getUser()->owner()->dispatch(FuncRequest(LFUN_MENUWRITE));
90         }
91
92         return owner_->isClean();
93 }
94
95
96 void LyXVC::registrer()
97 {
98         string const filename = owner_->fileName();
99
100         // there must be a file to save
101         if (!IsFileReadable(filename)) {
102                 Alert::error(_("Document not saved"),
103                         _("You must save the document "
104                           "before it can be registered."));
105                 return;
106         }
107
108         // it is very likely here that the vcs is not created yet...
109         if (!vcs) {
110                 string const cvs_entries = "CVS/Entries";
111
112                 if (IsFileReadable(cvs_entries)) {
113                         lyxerr[Debug::LYXVC]
114                                 << "LyXVC: registering "
115                                 << MakeDisplayPath(filename)
116                                 << " with CVS" << endl;
117                         vcs = new CVS(cvs_entries, filename);
118
119                 } else {
120                         lyxerr[Debug::LYXVC]
121                                 << "LyXVC: registering "
122                                 << MakeDisplayPath(filename)
123                                 << " with RCS" << endl;
124                         vcs = new RCS(filename);
125                 }
126
127                 vcs->owner(owner_);
128         }
129
130         // Maybe the save fails, or we answered "no". In both cases,
131         // the document will be dirty, and we abort.
132         if (!ensureClean())
133                 return;
134
135         lyxerr[Debug::LYXVC] << "LyXVC: registrer" << endl;
136         pair<bool, string> tmp =
137                 Alert::askForText(_("LyX VC: Initial description"),
138                            _("(no initial description)"));
139         if (!tmp.first || tmp.second.empty()) {
140                 // should we insist on checking tmp.second.empty()?
141                 lyxerr[Debug::LYXVC] << "LyXVC: user cancelled" << endl;
142                 return;
143         }
144
145         vcs->registrer(tmp.second);
146 }
147
148
149 void LyXVC::checkIn()
150 {
151         // If the document is changed, we might want to save it
152         if (!vcs->owner()->isClean()) {
153                 vcs->owner()->getUser()->owner()
154                         ->dispatch(FuncRequest(LFUN_MENUWRITE));
155         }
156
157         // Maybe the save fails, or we answered "no". In both cases,
158         // the document will be dirty, and we abort.
159         if (!vcs->owner()->isClean())
160                 return;
161
162         lyxerr[Debug::LYXVC] << "LyXVC: checkIn" << endl;
163         pair<bool, string> tmp = Alert::askForText(_("LyX VC: Log Message"));
164         if (tmp.first) {
165                 if (tmp.second.empty()) {
166                         tmp.second = _("(no log message)");
167                 }
168                 vcs->checkIn(tmp.second);
169         } else {
170                 lyxerr[Debug::LYXVC] << "LyXVC: user cancelled" << endl;
171         }
172 }
173
174
175 void LyXVC::checkOut()
176 {
177         lyxerr[Debug::LYXVC] << "LyXVC: checkOut" << endl;
178         if (!ensureClean())
179                 return;
180
181         vcs->checkOut();
182 }
183
184
185 void LyXVC::revert()
186 {
187         lyxerr[Debug::LYXVC] << "LyXVC: revert" << endl;
188
189         string const file = MakeDisplayPath(owner_->fileName(), 20);
190         string text = bformat(_("Reverting to the stored version of the "
191                 "document %1$s will lose all current changes.\n\n"
192                 "Do you want to revert to the saved version?"), file);
193         int const ret = Alert::prompt(_("Revert to stored version of document?"),
194                 text, 0, 1, _("&Revert"), _("&Cancel"));
195
196         if (ret == 0)
197                 vcs->revert();
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 string const 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 }