]> git.lyx.org Git - lyx.git/blob - src/lyxvc.C
6d038b91892272d47b9f7c0abe1634f91163c2ef
[lyx.git] / src / lyxvc.C
1 /**
2  * \file lyxvc.C
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 "vc-backend.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/lyxlib.h"
28
29 using lyx::support::bformat;
30 using lyx::support::isFileReadable;
31 using lyx::support::MakeDisplayPath;
32 using lyx::support::tempName;
33
34 using std::endl;
35 using std::string;
36 using std::pair;
37
38
39 LyXVC::LyXVC()
40 {
41         owner_ = 0;
42 }
43
44
45 // for the sake of boost::scoped_ptr
46 LyXVC::~LyXVC()
47 {}
48
49
50 bool LyXVC::file_found_hook(string const & fn)
51 {
52         string found_file;
53         // Check if file is under RCS
54         if (!(found_file = RCS::find_file(fn)).empty()) {
55                 vcs.reset(new RCS(found_file));
56                 vcs->owner(owner_);
57                 return true;
58         }
59         // Check if file is under CVS
60         if (!(found_file = CVS::find_file(fn)).empty()) {
61                 vcs.reset(new CVS(found_file, fn));
62                 vcs->owner(owner_);
63                 return true;
64         }
65         // file is not under any VCS.
66         return false;
67 }
68
69
70 bool LyXVC::file_not_found_hook(string const & fn)
71 {
72         // Check if file is under RCS
73         if (!RCS::find_file(fn).empty())
74                 return true;
75         if (!CVS::find_file(fn).empty())
76                 return true;
77         return false;
78 }
79
80
81 void LyXVC::buffer(Buffer * buf)
82 {
83         owner_ = buf;
84 }
85
86
87 void LyXVC::registrer()
88 {
89         string const filename = owner_->fileName();
90
91         // there must be a file to save
92         if (!isFileReadable(filename)) {
93                 Alert::error(_("Document not saved"),
94                              _("You must save the document "
95                                "before it can be registered."));
96                 return;
97         }
98
99         // it is very likely here that the vcs is not created yet...
100         if (!vcs) {
101                 string const cvs_entries = "CVS/Entries";
102
103                 if (isFileReadable(cvs_entries)) {
104                         lyxerr[Debug::LYXVC]
105                                 << "LyXVC: registering "
106                                 << MakeDisplayPath(filename)
107                                 << " with CVS" << endl;
108                         vcs.reset(new CVS(cvs_entries, filename));
109
110                 } else {
111                         lyxerr[Debug::LYXVC]
112                                 << "LyXVC: registering "
113                                 << MakeDisplayPath(filename)
114                                 << " with RCS" << endl;
115                         vcs.reset(new RCS(filename));
116                 }
117
118                 vcs->owner(owner_);
119         }
120
121         lyxerr[Debug::LYXVC] << "LyXVC: registrer" << endl;
122         pair<bool, string> tmp =
123                 Alert::askForText(_("LyX VC: Initial description"),
124                            _("(no initial description)"));
125         if (!tmp.first || tmp.second.empty()) {
126                 // should we insist on checking tmp.second.empty()?
127                 lyxerr[Debug::LYXVC] << "LyXVC: user cancelled" << endl;
128                 return;
129         }
130
131         vcs->registrer(tmp.second);
132 }
133
134
135 void LyXVC::checkIn()
136 {
137
138         lyxerr[Debug::LYXVC] << "LyXVC: checkIn" << endl;
139         pair<bool, string> tmp = Alert::askForText(_("LyX VC: Log Message"));
140         if (tmp.first) {
141                 if (tmp.second.empty()) {
142                         tmp.second = _("(no log message)");
143                 }
144                 vcs->checkIn(tmp.second);
145         } else {
146                 lyxerr[Debug::LYXVC] << "LyXVC: user cancelled" << endl;
147         }
148 }
149
150
151 void LyXVC::checkOut()
152 {
153         lyxerr[Debug::LYXVC] << "LyXVC: checkOut" << endl;
154
155         vcs->checkOut();
156 }
157
158
159 void LyXVC::revert()
160 {
161         lyxerr[Debug::LYXVC] << "LyXVC: revert" << endl;
162
163         string const file = MakeDisplayPath(owner_->fileName(), 20);
164         string 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" << endl;
186                 checkOut();
187                 break;
188         case VCS::LOCKED:
189                 lyxerr[Debug::LYXVC] << "LyXVC: toggle to unlocked" << endl;
190                 checkIn();
191                 break;
192         }
193 }
194
195
196 bool LyXVC::inUse()
197 {
198         if (vcs) return true;
199         return false;
200 }
201
202
203 //string const & LyXVC::version() const
204 //{
205 //      return vcs->version();
206 //}
207
208 string const LyXVC::versionString() const
209 {
210         return vcs->versionString();
211 }
212
213
214 string const & LyXVC::locker() const
215 {
216         return vcs->locker();
217 }
218
219
220 string const LyXVC::getLogFile() const
221 {
222         if (!vcs)
223                 return string();
224
225         string tmpf = tempName(string(), "lyxvclog");
226         if (tmpf.empty()) {
227                 lyxerr[Debug::LYXVC] << "Could not generate logfile "
228                                      << tmpf << endl;
229                 return string();
230         }
231         lyxerr[Debug::LYXVC] << "Generating logfile " << tmpf << endl;
232         vcs->getLog(tmpf);
233         return tmpf;
234 }