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