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