]> git.lyx.org Git - features.git/blob - src/LyXVC.cpp
Simplify FileName::tempName().
[features.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 "support/debug.h"
21 #include "Buffer.h"
22 #include "support/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 using namespace std;
31 using namespace lyx::support;
32
33 namespace lyx {
34
35 namespace Alert = frontend::Alert;
36
37
38 LyXVC::LyXVC()
39 {
40         owner_ = 0;
41 }
42
43
44 // for the sake of boost::scoped_ptr
45 LyXVC::~LyXVC()
46 {}
47
48
49 bool LyXVC::file_found_hook(FileName const & fn)
50 {
51         FileName found_file;
52         // Check if file is under RCS
53         if (!(found_file = RCS::findFile(fn)).empty()) {
54                 vcs.reset(new RCS(found_file));
55                 vcs->owner(owner_);
56                 return true;
57         }
58         // Check if file is under CVS
59         if (!(found_file = CVS::findFile(fn)).empty()) {
60                 vcs.reset(new CVS(found_file, fn));
61                 vcs->owner(owner_);
62                 return true;
63         }
64         // file is not under any VCS.
65         return false;
66 }
67
68
69 bool LyXVC::file_not_found_hook(FileName const & fn)
70 {
71         // Check if file is under RCS
72         if (!RCS::findFile(fn).empty())
73                 return true;
74         if (!CVS::findFile(fn).empty())
75                 return true;
76         return false;
77 }
78
79
80 void LyXVC::setBuffer(Buffer * buf)
81 {
82         owner_ = buf;
83 }
84
85
86 void LyXVC::registrer()
87 {
88         FileName const filename = owner_->fileName();
89
90         // there must be a file to save
91         if (!filename.isReadableFile()) {
92                 Alert::error(_("Document not saved"),
93                              _("You must save the document "
94                                             "before it can be registered."));
95                 return;
96         }
97
98         // it is very likely here that the vcs is not created yet...
99         if (!vcs) {
100                 FileName const cvs_entries(makeAbsPath("CVS/Entries"));
101
102                 if (cvs_entries.isReadableFile()) {
103                         LYXERR(Debug::LYXVC, "LyXVC: registering "
104                                 << to_utf8(filename.displayName()) << " with CVS");
105                         vcs.reset(new CVS(cvs_entries, filename));
106
107                 } else {
108                         LYXERR(Debug::LYXVC, "LyXVC: registering "
109                                 << to_utf8(filename.displayName()) << " with RCS");
110                         vcs.reset(new RCS(filename));
111                 }
112
113                 vcs->owner(owner_);
114         }
115
116         LYXERR(Debug::LYXVC, "LyXVC: registrer");
117         docstring response;
118         bool ok = Alert::askForText(response, _("LyX VC: Initial description"),
119                         _("(no initial description)"));
120         if (!ok || response.empty()) {
121                 // should we insist on checking response.empty()?
122                 LYXERR(Debug::LYXVC, "LyXVC: user cancelled");
123                 return;
124         }
125
126         vcs->registrer(to_utf8(response));
127 }
128
129
130 void LyXVC::checkIn()
131 {
132         LYXERR(Debug::LYXVC, "LyXVC: checkIn");
133         docstring response;
134         bool ok = Alert::askForText(response, _("LyX VC: Log Message"));
135         if (ok) {
136                 if (response.empty())
137                         response = _("(no log message)");
138                 vcs->checkIn(to_utf8(response));
139         } else {
140                 LYXERR(Debug::LYXVC, "LyXVC: user cancelled");
141         }
142 }
143
144
145 void LyXVC::checkOut()
146 {
147         LYXERR(Debug::LYXVC, "LyXVC: checkOut");
148         vcs->checkOut();
149 }
150
151
152 void LyXVC::revert()
153 {
154         LYXERR(Debug::LYXVC, "LyXVC: revert");
155
156         docstring const file = owner_->fileName().displayName(20);
157         docstring text = bformat(_("Reverting to the stored version of the "
158                 "document %1$s will lose all current changes.\n\n"
159                                              "Do you want to revert to the saved version?"), file);
160         int const ret = Alert::prompt(_("Revert to stored version of document?"),
161                 text, 0, 1, _("&Revert"), _("&Cancel"));
162
163         if (ret == 0)
164                 vcs->revert();
165 }
166
167
168 void LyXVC::undoLast()
169 {
170         vcs->undoLast();
171 }
172
173
174 void LyXVC::toggleReadOnly()
175 {
176         switch (vcs->status()) {
177         case VCS::UNLOCKED:
178                 LYXERR(Debug::LYXVC, "LyXVC: toggle to locked");
179                 checkOut();
180                 break;
181         case VCS::LOCKED:
182                 LYXERR(Debug::LYXVC, "LyXVC: toggle to unlocked");
183                 checkIn();
184                 break;
185         }
186 }
187
188
189 bool LyXVC::inUse()
190 {
191         if (vcs)
192                 return true;
193         return false;
194 }
195
196
197 //string const & LyXVC::version() const
198 //{
199 //      return vcs->version();
200 //}
201
202
203 string const LyXVC::versionString() const
204 {
205         return vcs->versionString();
206 }
207
208
209 string const & LyXVC::locker() const
210 {
211         return vcs->locker();
212 }
213
214
215 string const LyXVC::getLogFile() const
216 {
217         if (!vcs)
218                 return string();
219
220         FileName const tmpf = FileName::tempName("lyxvclog");
221         if (tmpf.empty()) {
222                 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
223                 return string();
224         }
225         LYXERR(Debug::LYXVC, "Generating logfile " << tmpf);
226         vcs->getLog(tmpf);
227         return tmpf.absFilename();
228 }
229
230
231 } // namespace lyx