]> git.lyx.org Git - lyx.git/blob - src/lyxvc.C
5dff933476ccba05a8e623ef84f80be54f5ea890
[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 #include "BoostFormat.h"
17
18 #include <unistd.h>
19
20 using std::endl;
21 using std::pair;
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 #if USE_BOOST_FORMAT
84         boost::format fmt(_("The document %1$s has unsaved changes.\n\nDo you want to save the document?"));
85         fmt % file;
86         string text = fmt.str();
87 #else
88         string text = _("The document ");
89         text += file + _(" has unsaved changes.\n\nDo you want to save the document?");
90 #endif
91         int const ret = Alert::prompt(_("Save changed document?"),
92                 text, 0, _("&Save"), _("&Cancel"));
93
94         if (ret == 0) {
95                 vcs->owner()->getUser()->owner()->dispatch(FuncRequest(LFUN_MENUWRITE));
96         }
97
98         return owner_->isClean();
99 }
100
101
102 void LyXVC::registrer()
103 {
104         string const filename = owner_->fileName();
105
106         // there must be a file to save
107         if (!IsFileReadable(filename)) {
108                 Alert::alert(_("File not saved"),
109                         _("You must save the file"),
110                         _("before it can be registered."));
111                 return;
112         }
113
114         // it is very likely here that the vcs is not created yet...
115         if (!vcs) {
116                 string const cvs_entries = "CVS/Entries";
117
118                 if (IsFileReadable(cvs_entries)) {
119                         lyxerr[Debug::LYXVC]
120                                 << "LyXVC: registering "
121                                 << MakeDisplayPath(filename)
122                                 << " with CVS" << endl;
123                         vcs = new CVS(cvs_entries, filename);
124
125                 } else {
126                         lyxerr[Debug::LYXVC]
127                                 << "LyXVC: registering "
128                                 << MakeDisplayPath(filename)
129                                 << " with RCS" << endl;
130                         vcs = new RCS(filename);
131                 }
132
133                 vcs->owner(owner_);
134         }
135
136         // Maybe the save fails, or we answered "no". In both cases,
137         // the document will be dirty, and we abort.
138         if (!ensureClean())
139                 return;
140
141         lyxerr[Debug::LYXVC] << "LyXVC: registrer" << endl;
142         pair<bool, string> tmp =
143                 Alert::askForText(_("LyX VC: Initial description"),
144                            _("(no initial description)"));
145         if (!tmp.first || tmp.second.empty()) {
146                 // should we insist on checking tmp.second.empty()?
147                 lyxerr[Debug::LYXVC] << "LyXVC: user cancelled" << endl;
148                 Alert::alert(_("Info"),
149                            _("This document has NOT been registered."));
150                 return;
151         }
152
153         vcs->registrer(tmp.second);
154 }
155
156
157 void LyXVC::checkIn()
158 {
159         // If the document is changed, we might want to save it
160         if (!vcs->owner()->isClean()) {
161                 vcs->owner()->getUser()->owner()
162                         ->dispatch(FuncRequest(LFUN_MENUWRITE));
163         }
164
165         // Maybe the save fails, or we answered "no". In both cases,
166         // the document will be dirty, and we abort.
167         if (!vcs->owner()->isClean())
168                 return;
169
170         lyxerr[Debug::LYXVC] << "LyXVC: checkIn" << endl;
171         pair<bool, string> tmp = Alert::askForText(_("LyX VC: Log Message"));
172         if (tmp.first) {
173                 if (tmp.second.empty()) {
174                         tmp.second = _("(no log message)");
175                 }
176                 vcs->checkIn(tmp.second);
177         } else {
178                 lyxerr[Debug::LYXVC] << "LyXVC: user cancelled" << endl;
179         }
180 }
181
182
183 void LyXVC::checkOut()
184 {
185         lyxerr[Debug::LYXVC] << "LyXVC: checkOut" << endl;
186         if (!ensureClean())
187                 return;
188
189         vcs->checkOut();
190 }
191
192
193 void LyXVC::revert()
194 {
195         lyxerr[Debug::LYXVC] << "LyXVC: revert" << endl;
196
197         string const file = MakeDisplayPath(owner_->fileName(), 20);
198 #if USE_BOOST_FORMAT
199         boost::format fmt(_("Reverting to the stored version of the document %1$s will "
200                         "lose all current changes.\n\nDo you want to revert to the saved version?"));
201         fmt % file;
202         string text = fmt.str();
203 #else
204         string text = _("Reverting to the stored version of the document ");
205         text += file + _(" will lose all current changes.\n\nDo you want to revert to the saved version?");
206 #endif
207         int const ret = Alert::prompt(_("Revert to stored version of document?"),
208                 text, 1, _("&Revert"), _("&Cancel"));
209
210         if (ret == 0)
211                 vcs->revert();
212 }
213
214
215 void LyXVC::undoLast()
216 {
217         vcs->undoLast();
218 }
219
220
221 void LyXVC::toggleReadOnly()
222 {
223         switch (vcs->status()) {
224         case VCS::UNLOCKED:
225                 lyxerr[Debug::LYXVC] << "LyXVC: toggle to locked" << endl;
226                 checkOut();
227                 break;
228         case VCS::LOCKED:
229                 lyxerr[Debug::LYXVC] << "LyXVC: toggle to unlocked" << endl;
230                 checkIn();
231                 break;
232         }
233 }
234
235
236 bool LyXVC::inUse()
237 {
238         if (vcs) return true;
239         return false;
240 }
241
242
243 //string const & LyXVC::version() const
244 //{
245 //      return vcs->version();
246 //}
247
248 string const LyXVC::versionString() const
249 {
250         return vcs->versionString();
251 }
252
253
254 string const & LyXVC::locker() const
255 {
256         return vcs->locker();
257 }
258
259
260 const string LyXVC::getLogFile() const
261 {
262         if (!vcs)
263                 return string();
264
265         string tmpf = lyx::tempName(string(), "lyxvclog");
266         lyxerr[Debug::LYXVC] << "Generating logfile " << tmpf << endl;
267         vcs->getLog(tmpf);
268         return tmpf;
269 }