]> git.lyx.org Git - lyx.git/blob - src/lyxvc.C
introduce namespace lyx::support
[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 namespace lyx::support;
20
21 using std::endl;
22 using std::pair;
23
24
25 /* WARNING: Several of the vcs-> methods end up
26  * deleting this object via BufferView::reload() !
27  */
28
29 LyXVC::LyXVC()
30 {
31         vcs = 0;
32         owner_ = 0;
33 }
34
35
36 LyXVC::~LyXVC()
37 {
38         delete vcs;
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 & fn)
63 {
64         // Check if file is under RCS
65         if (!RCS::find_file(fn).empty())
66                 return true;
67         if (!CVS::find_file(fn).empty())
68                 return true;
69         return false;
70 }
71
72
73 void LyXVC::buffer(Buffer * buf)
74 {
75         owner_ = buf;
76 }
77
78
79 bool LyXVC::ensureClean()
80 {
81         if (owner_->isClean())
82                 return true;
83
84         string const file = MakeDisplayPath(owner_->fileName(), 30);
85         string text = bformat(_("The document %1$s has unsaved changes.\n\n"
86                 "Do you want to save the document?"), file);
87         int const ret = Alert::prompt(_("Save changed document?"),
88                 text, 0, 1, _("&Save"), _("&Cancel"));
89
90         if (ret == 0) {
91                 vcs->owner()->getUser()->owner()->dispatch(FuncRequest(LFUN_MENUWRITE));
92         }
93
94         return owner_->isClean();
95 }
96
97
98 void LyXVC::registrer()
99 {
100         string const filename = owner_->fileName();
101
102         // there must be a file to save
103         if (!IsFileReadable(filename)) {
104                 Alert::error(_("Document not saved"),
105                         _("You must save the document "
106                           "before it can be registered."));
107                 return;
108         }
109
110         // it is very likely here that the vcs is not created yet...
111         if (!vcs) {
112                 string const cvs_entries = "CVS/Entries";
113
114                 if (IsFileReadable(cvs_entries)) {
115                         lyxerr[Debug::LYXVC]
116                                 << "LyXVC: registering "
117                                 << MakeDisplayPath(filename)
118                                 << " with CVS" << endl;
119                         vcs = new CVS(cvs_entries, filename);
120
121                 } else {
122                         lyxerr[Debug::LYXVC]
123                                 << "LyXVC: registering "
124                                 << MakeDisplayPath(filename)
125                                 << " with RCS" << endl;
126                         vcs = new RCS(filename);
127                 }
128
129                 vcs->owner(owner_);
130         }
131
132         // Maybe the save fails, or we answered "no". In both cases,
133         // the document will be dirty, and we abort.
134         if (!ensureClean())
135                 return;
136
137         lyxerr[Debug::LYXVC] << "LyXVC: registrer" << endl;
138         pair<bool, string> tmp =
139                 Alert::askForText(_("LyX VC: Initial description"),
140                            _("(no initial description)"));
141         if (!tmp.first || tmp.second.empty()) {
142                 // should we insist on checking tmp.second.empty()?
143                 lyxerr[Debug::LYXVC] << "LyXVC: user cancelled" << endl;
144                 return;
145         }
146
147         vcs->registrer(tmp.second);
148 }
149
150
151 void LyXVC::checkIn()
152 {
153         // If the document is changed, we might want to save it
154         if (!vcs->owner()->isClean()) {
155                 vcs->owner()->getUser()->owner()
156                         ->dispatch(FuncRequest(LFUN_MENUWRITE));
157         }
158
159         // Maybe the save fails, or we answered "no". In both cases,
160         // the document will be dirty, and we abort.
161         if (!vcs->owner()->isClean())
162                 return;
163
164         lyxerr[Debug::LYXVC] << "LyXVC: checkIn" << endl;
165         pair<bool, string> tmp = Alert::askForText(_("LyX VC: Log Message"));
166         if (tmp.first) {
167                 if (tmp.second.empty()) {
168                         tmp.second = _("(no log message)");
169                 }
170                 vcs->checkIn(tmp.second);
171         } else {
172                 lyxerr[Debug::LYXVC] << "LyXVC: user cancelled" << endl;
173         }
174 }
175
176
177 void LyXVC::checkOut()
178 {
179         lyxerr[Debug::LYXVC] << "LyXVC: checkOut" << endl;
180         if (!ensureClean())
181                 return;
182
183         vcs->checkOut();
184 }
185
186
187 void LyXVC::revert()
188 {
189         lyxerr[Debug::LYXVC] << "LyXVC: revert" << endl;
190
191         string const file = MakeDisplayPath(owner_->fileName(), 20);
192         string text = bformat(_("Reverting to the stored version of the "
193                 "document %1$s will lose all current changes.\n\n"
194                 "Do you want to revert to the saved version?"), file);
195         int const ret = Alert::prompt(_("Revert to stored version of document?"),
196                 text, 0, 1, _("&Revert"), _("&Cancel"));
197
198         if (ret == 0)
199                 vcs->revert();
200 }
201
202
203 void LyXVC::undoLast()
204 {
205         vcs->undoLast();
206 }
207
208
209 void LyXVC::toggleReadOnly()
210 {
211         switch (vcs->status()) {
212         case VCS::UNLOCKED:
213                 lyxerr[Debug::LYXVC] << "LyXVC: toggle to locked" << endl;
214                 checkOut();
215                 break;
216         case VCS::LOCKED:
217                 lyxerr[Debug::LYXVC] << "LyXVC: toggle to unlocked" << endl;
218                 checkIn();
219                 break;
220         }
221 }
222
223
224 bool LyXVC::inUse()
225 {
226         if (vcs) return true;
227         return false;
228 }
229
230
231 //string const & LyXVC::version() const
232 //{
233 //      return vcs->version();
234 //}
235
236 string const LyXVC::versionString() const
237 {
238         return vcs->versionString();
239 }
240
241
242 string const & LyXVC::locker() const
243 {
244         return vcs->locker();
245 }
246
247
248 string const LyXVC::getLogFile() const
249 {
250         if (!vcs)
251                 return string();
252
253         string tmpf = tempName(string(), "lyxvclog");
254         lyxerr[Debug::LYXVC] << "Generating logfile " << tmpf << endl;
255         vcs->getLog(tmpf);
256         return tmpf;
257 }