]> git.lyx.org Git - lyx.git/blob - src/lyxvc.C
Alfredo's second patch
[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 "support/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, 1, _("&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::error(_("Document not saved"),
109                         _("You must save the document "
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                 return;
149         }
150
151         vcs->registrer(tmp.second);
152 }
153
154
155 void LyXVC::checkIn()
156 {
157         // If the document is changed, we might want to save it
158         if (!vcs->owner()->isClean()) {
159                 vcs->owner()->getUser()->owner()
160                         ->dispatch(FuncRequest(LFUN_MENUWRITE));
161         }
162
163         // Maybe the save fails, or we answered "no". In both cases,
164         // the document will be dirty, and we abort.
165         if (!vcs->owner()->isClean())
166                 return;
167
168         lyxerr[Debug::LYXVC] << "LyXVC: checkIn" << endl;
169         pair<bool, string> tmp = Alert::askForText(_("LyX VC: Log Message"));
170         if (tmp.first) {
171                 if (tmp.second.empty()) {
172                         tmp.second = _("(no log message)");
173                 }
174                 vcs->checkIn(tmp.second);
175         } else {
176                 lyxerr[Debug::LYXVC] << "LyXVC: user cancelled" << endl;
177         }
178 }
179
180
181 void LyXVC::checkOut()
182 {
183         lyxerr[Debug::LYXVC] << "LyXVC: checkOut" << endl;
184         if (!ensureClean())
185                 return;
186
187         vcs->checkOut();
188 }
189
190
191 void LyXVC::revert()
192 {
193         lyxerr[Debug::LYXVC] << "LyXVC: revert" << endl;
194
195         string const file = MakeDisplayPath(owner_->fileName(), 20);
196 #if USE_BOOST_FORMAT
197         boost::format fmt(_("Reverting to the stored version of the document %1$s will "
198                         "lose all current changes.\n\nDo you want to revert to the saved version?"));
199         fmt % file;
200         string text = fmt.str();
201 #else
202         string text = _("Reverting to the stored version of the document ");
203         text += file + _(" will lose all current changes.\n\nDo you want to revert to the saved version?");
204 #endif
205         int const ret = Alert::prompt(_("Revert to stored version of document?"),
206                 text, 0, 1, _("&Revert"), _("&Cancel"));
207
208         if (ret == 0)
209                 vcs->revert();
210 }
211
212
213 void LyXVC::undoLast()
214 {
215         vcs->undoLast();
216 }
217
218
219 void LyXVC::toggleReadOnly()
220 {
221         switch (vcs->status()) {
222         case VCS::UNLOCKED:
223                 lyxerr[Debug::LYXVC] << "LyXVC: toggle to locked" << endl;
224                 checkOut();
225                 break;
226         case VCS::LOCKED:
227                 lyxerr[Debug::LYXVC] << "LyXVC: toggle to unlocked" << endl;
228                 checkIn();
229                 break;
230         }
231 }
232
233
234 bool LyXVC::inUse()
235 {
236         if (vcs) return true;
237         return false;
238 }
239
240
241 //string const & LyXVC::version() const
242 //{
243 //      return vcs->version();
244 //}
245
246 string const LyXVC::versionString() const
247 {
248         return vcs->versionString();
249 }
250
251
252 string const & LyXVC::locker() const
253 {
254         return vcs->locker();
255 }
256
257
258 const string LyXVC::getLogFile() const
259 {
260         if (!vcs)
261                 return string();
262
263         string tmpf = lyx::tempName(string(), "lyxvclog");
264         lyxerr[Debug::LYXVC] << "Generating logfile " << tmpf << endl;
265         vcs->getLog(tmpf);
266         return tmpf;
267 }