]> git.lyx.org Git - lyx.git/blob - src/lyxvc.C
38d9278dd793f627af57e426f1c386313bda761b
[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 "gettext.h"
13 #include "LyXView.h"
14 #include "lyxfunc.h"
15
16 #include "frontends/Alert.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         // it is very likely here that the vcs is not created yet...
79         // so... we use RCS as default, later this should perhaps be
80         // a lyxrc option.
81         if (!vcs) {
82                 vcs = new RCS(owner_->fileName());
83                 vcs->owner(owner_);
84         }
85
86         // If the document is changed, we might want to save it
87         if (!vcs->owner()->isLyxClean() &&
88             Alert::askQuestion(_("Changes in document:"),
89                         MakeDisplayPath(vcs->owner()->fileName(), 50),
90                         _("Save document and proceed?"))) {
91                 vcs->owner()->getUser()->owner()
92                         ->getLyXFunc()->dispatch(LFUN_MENUWRITE);
93         }
94
95         // Maybe the save fails, or we answered "no". In both cases,
96         // the document will be dirty, and we abort.
97         if (!vcs->owner()->isLyxClean()) {
98                 return;
99         }
100
101         lyxerr[Debug::LYXVC] << "LyXVC: registrer" << endl;
102         pair<bool, string> tmp =
103                 Alert::askForText(_("LyX VC: Initial description"),
104                            _("(no initial description)"));
105         if (!tmp.first || tmp.second.empty()) {
106                 // should we insist on checking tmp.second.empty()?
107                 lyxerr[Debug::LYXVC] << "LyXVC: user cancelled" << endl;
108                 Alert::alert(_("Info"),
109                            _("This document has NOT been registered."));
110                 return;
111         }
112
113         vcs->registrer(tmp.second);
114 }
115
116
117 void LyXVC::checkIn()
118 {
119         // If the document is changed, we might want to save it
120         if (!vcs->owner()->isLyxClean() &&
121             Alert::askQuestion(_("Changes in document:"),
122                         MakeDisplayPath(vcs->owner()->fileName(), 50),
123                         _("Save document and proceed?"))) {
124                 vcs->owner()->getUser()->owner()
125                         ->getLyXFunc()->dispatch(LFUN_MENUWRITE);
126         }
127
128         // Maybe the save fails, or we answered "no". In both cases,
129         // the document will be dirty, and we abort.
130         if (!vcs->owner()->isLyxClean()) {
131                 return;
132         }
133
134         lyxerr[Debug::LYXVC] << "LyXVC: checkIn" << endl;
135         pair<bool, string> tmp = Alert::askForText(_("LyX VC: Log Message"));
136         if (tmp.first) {
137                 if (tmp.second.empty()) {
138                         tmp.second = _("(no log message)");
139                 }
140                 vcs->checkIn(tmp.second);
141         } else {
142                 lyxerr[Debug::LYXVC] << "LyXVC: user cancelled" << endl;
143         }
144 }
145
146
147 void LyXVC::checkOut()
148 {
149         lyxerr[Debug::LYXVC] << "LyXVC: checkOut" << endl;
150         if (!vcs->owner()->isLyxClean()
151             && !Alert::askQuestion(_("Changes in document:"),
152                            MakeDisplayPath(vcs->owner()->fileName(), 50),
153                            _("Ignore changes and proceed with check out?"))) {
154                 return;
155         }
156
157         vcs->checkOut();
158
159 }
160
161
162 void LyXVC::revert()
163 {
164         lyxerr[Debug::LYXVC] << "LyXVC: revert" << endl;
165         // Here we should check if the buffer is dirty. And if it is
166         // we should warn the user that reverting will discard all
167         // changes made since the last check in.
168         if (Alert::askQuestion(_("When you revert, you will loose all changes made"),
169                         _("to the document since the last check in."),
170                         _("Do you still want to do it?"))) {
171
172                 vcs->revert();
173         }
174 }
175
176
177 void LyXVC::undoLast()
178 {
179         vcs->undoLast();
180 }
181
182
183 void LyXVC::toggleReadOnly()
184 {
185         switch (vcs->status()) {
186         case VCS::UNLOCKED:
187                 lyxerr[Debug::LYXVC] << "LyXVC: toggle to locked" << endl;
188                 checkOut();
189                 break;
190         case VCS::LOCKED:
191                 lyxerr[Debug::LYXVC] << "LyXVC: toggle to unlocked" << endl;
192                 checkIn();
193                 break;
194         }
195 }
196
197
198 bool LyXVC::inUse()
199 {
200         if (vcs) return true;
201         return false;
202 }
203
204
205 //string const & LyXVC::version() const
206 //{
207 //      return vcs->version();
208 //}
209
210 string const LyXVC::versionString() const
211 {
212         return vcs->versionString();
213 }
214
215
216 string const & LyXVC::locker() const
217 {
218         return vcs->locker();
219 }
220
221
222 const string LyXVC::getLogFile() const
223 {
224         if (!vcs)
225                 return string();
226
227         string tmpf = lyx::tempName(string(), "lyxvclog");
228         lyxerr[Debug::LYXVC] << "Generating logfile " << tmpf << endl;
229         vcs->getLog(tmpf);
230         return tmpf;
231 }