]> git.lyx.org Git - lyx.git/blob - src/LyXVC.cpp
8b9d7c608ea382b9969025b75b5bdbeb0cc6b9a9
[lyx.git] / src / LyXVC.cpp
1 /**
2  * \file LyXVC.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author Jean-Marc Lasgouttes
8  * \author Angus Leeming
9  * \author John Levon
10  * \author André Pönitz
11  * \author Allan Rae
12  *
13  * Full author contact details are available in file CREDITS.
14  */
15
16 #include <config.h>
17
18 #include "LyXVC.h"
19 #include "VCBackend.h"
20 #include "debug.h"
21 #include "Buffer.h"
22 #include "gettext.h"
23
24 #include "frontends/alert.h"
25
26 #include "support/filetools.h"
27 #include "support/lstrings.h"
28 #include "support/lyxlib.h"
29
30
31 namespace lyx {
32
33 using support::bformat;
34 using support::FileName;
35 using support::makeAbsPath;
36 using support::tempName;
37
38 using std::endl;
39 using std::string;
40 using std::pair;
41
42 namespace Alert = frontend::Alert;
43
44
45 LyXVC::LyXVC()
46 {
47         owner_ = 0;
48 }
49
50
51 // for the sake of boost::scoped_ptr
52 LyXVC::~LyXVC()
53 {}
54
55
56 bool LyXVC::file_found_hook(FileName const & fn)
57 {
58         FileName found_file;
59         // Check if file is under RCS
60         if (!(found_file = RCS::find_file(fn)).empty()) {
61                 vcs.reset(new RCS(found_file));
62                 vcs->owner(owner_);
63                 return true;
64         }
65         // Check if file is under CVS
66         if (!(found_file = CVS::find_file(fn)).empty()) {
67                 vcs.reset(new CVS(found_file, fn));
68                 vcs->owner(owner_);
69                 return true;
70         }
71         // file is not under any VCS.
72         return false;
73 }
74
75
76 bool LyXVC::file_not_found_hook(FileName const & fn)
77 {
78         // Check if file is under RCS
79         if (!RCS::find_file(fn).empty())
80                 return true;
81         if (!CVS::find_file(fn).empty())
82                 return true;
83         return false;
84 }
85
86
87 void LyXVC::setBuffer(Buffer * buf)
88 {
89         owner_ = buf;
90 }
91
92
93 void LyXVC::registrer()
94 {
95         FileName const filename = owner_->fileName();
96
97         // there must be a file to save
98         if (!filename.isFileReadable()) {
99                 Alert::error(_("Document not saved"),
100                              _("You must save the document "
101                                             "before it can be registered."));
102                 return;
103         }
104
105         // it is very likely here that the vcs is not created yet...
106         if (!vcs) {
107                 FileName const cvs_entries(makeAbsPath("CVS/Entries"));
108
109                 if (cvs_entries.isFileReadable()) {
110                         LYXERR(Debug::LYXVC, "LyXVC: registering "
111                                 << to_utf8(filename.displayName()) << " with CVS");
112                         vcs.reset(new CVS(cvs_entries, filename));
113
114                 } else {
115                         LYXERR(Debug::LYXVC, "LyXVC: registering "
116                                 << to_utf8(filename.displayName()) << " with RCS");
117                         vcs.reset(new RCS(filename));
118                 }
119
120                 vcs->owner(owner_);
121         }
122
123         LYXERR(Debug::LYXVC, "LyXVC: registrer");
124         docstring response;
125         bool ok = Alert::askForText(response, _("LyX VC: Initial description"),
126                         _("(no initial description)"));
127         if (!ok || response.empty()) {
128                 // should we insist on checking response.empty()?
129                 LYXERR(Debug::LYXVC, "LyXVC: user cancelled");
130                 return;
131         }
132
133         vcs->registrer(to_utf8(response));
134 }
135
136
137 void LyXVC::checkIn()
138 {
139         LYXERR(Debug::LYXVC, "LyXVC: checkIn");
140         docstring response;
141         bool ok = Alert::askForText(response, _("LyX VC: Log Message"));
142         if (ok) {
143                 if (response.empty())
144                         response = _("(no log message)");
145                 vcs->checkIn(to_utf8(response));
146         } else {
147                 LYXERR(Debug::LYXVC, "LyXVC: user cancelled");
148         }
149 }
150
151
152 void LyXVC::checkOut()
153 {
154         LYXERR(Debug::LYXVC, "LyXVC: checkOut");
155         vcs->checkOut();
156 }
157
158
159 void LyXVC::revert()
160 {
161         LYXERR(Debug::LYXVC, "LyXVC: revert");
162
163         docstring const file = owner_->fileName().displayName(20);
164         docstring text = bformat(_("Reverting to the stored version of the "
165                 "document %1$s will lose all current changes.\n\n"
166                                              "Do you want to revert to the saved version?"), file);
167         int const ret = Alert::prompt(_("Revert to stored version of document?"),
168                 text, 0, 1, _("&Revert"), _("&Cancel"));
169
170         if (ret == 0)
171                 vcs->revert();
172 }
173
174
175 void LyXVC::undoLast()
176 {
177         vcs->undoLast();
178 }
179
180
181 void LyXVC::toggleReadOnly()
182 {
183         switch (vcs->status()) {
184         case VCS::UNLOCKED:
185                 LYXERR(Debug::LYXVC, "LyXVC: toggle to locked");
186                 checkOut();
187                 break;
188         case VCS::LOCKED:
189                 LYXERR(Debug::LYXVC, "LyXVC: toggle to unlocked");
190                 checkIn();
191                 break;
192         }
193 }
194
195
196 bool LyXVC::inUse()
197 {
198         if (vcs)
199                 return true;
200         return false;
201 }
202
203
204 //string const & LyXVC::version() const
205 //{
206 //      return vcs->version();
207 //}
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 string const LyXVC::getLogFile() const
223 {
224         if (!vcs)
225                 return string();
226
227         FileName const tmpf(tempName(FileName(), "lyxvclog"));
228         if (tmpf.empty()) {
229                 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
230                 return string();
231         }
232         LYXERR(Debug::LYXVC, "Generating logfile " << tmpf);
233         vcs->getLog(tmpf);
234         return tmpf.absFilename();
235 }
236
237
238 } // namespace lyx