]> git.lyx.org Git - lyx.git/blob - src/VCBackend.cpp
f54e54e96e19ff4840649dcf71356e18f64ef01e
[lyx.git] / src / VCBackend.cpp
1 /**
2  * \file VCBackend.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  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "VCBackend.h"
14 #include "Buffer.h"
15 #include "LyX.h"
16 #include "FuncRequest.h"
17
18 #include "frontends/alert.h"
19 #include "frontends/Application.h"
20
21 #include "support/debug.h"
22 #include "support/filetools.h"
23 #include "support/gettext.h"
24 #include "support/lstrings.h"
25 #include "support/Path.h"
26 #include "support/Systemcall.h"
27
28 #include <boost/regex.hpp>
29
30 #include <fstream>
31
32 using namespace std;
33 using namespace lyx::support;
34
35 using boost::regex;
36 using boost::regex_match;
37 using boost::smatch;
38
39 namespace lyx {
40
41
42 int VCS::doVCCommandCall(string const & cmd, FileName const & path)
43 {
44         LYXERR(Debug::LYXVC, "doVCCommandCall: " << cmd);
45         Systemcall one;
46         support::PathChanger p(path);
47         return one.startscript(Systemcall::Wait, cmd, false);
48 }
49
50
51 int VCS::doVCCommand(string const & cmd, FileName const & path)
52 {
53         if (owner_)
54                 owner_->setBusy(true);
55
56         int const ret = doVCCommandCall(cmd, path);
57
58         if (owner_)
59                 owner_->setBusy(false);
60         if (ret)
61                 frontend::Alert::error(_("Revision control error."),
62                         bformat(_("Some problem occured while running the command:\n"
63                                   "'%1$s'."),
64                         from_utf8(cmd)));
65         return ret;
66 }
67
68
69 /////////////////////////////////////////////////////////////////////
70 //
71 // RCS
72 //
73 /////////////////////////////////////////////////////////////////////
74
75 RCS::RCS(FileName const & m)
76 {
77         master_ = m;
78         scanMaster();
79 }
80
81
82 FileName const RCS::findFile(FileName const & file)
83 {
84         // Check if *,v exists.
85         FileName tmp(file.absFilename() + ",v");
86         LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under rcs: " << tmp);
87         if (tmp.isReadableFile()) {
88                 LYXERR(Debug::LYXVC, "Yes, " << file << " is under rcs.");
89                 return tmp;
90         }
91
92         // Check if RCS/*,v exists.
93         tmp = FileName(addName(addPath(onlyPath(file.absFilename()), "RCS"), file.absFilename()) + ",v");
94         LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under rcs: " << tmp);
95         if (tmp.isReadableFile()) {
96                 LYXERR(Debug::LYXVC, "Yes, " << file << " is under rcs.");
97                 return tmp;
98         }
99
100         return FileName();
101 }
102
103
104 void RCS::retrieve(FileName const & file)
105 {
106         LYXERR(Debug::LYXVC, "LyXVC::RCS: retrieve.\n\t" << file);
107         doVCCommandCall("co -q -r " + quoteName(file.toFilesystemEncoding()),
108                          FileName());
109 }
110
111
112 void RCS::scanMaster()
113 {
114         if (master_.empty())
115                 return;
116
117         LYXERR(Debug::LYXVC, "LyXVC::RCS: scanMaster: " << master_);
118
119         ifstream ifs(master_.toFilesystemEncoding().c_str());
120
121         string token;
122         bool read_enough = false;
123
124         while (!read_enough && ifs >> token) {
125                 LYXERR(Debug::LYXVC, "LyXVC::scanMaster: current lex text: `"
126                         << token << '\'');
127
128                 if (token.empty())
129                         continue;
130                 else if (token == "head") {
131                         // get version here
132                         string tmv;
133                         ifs >> tmv;
134                         tmv = rtrim(tmv, ";");
135                         version_ = tmv;
136                         LYXERR(Debug::LYXVC, "LyXVC: version found to be " << tmv);
137                 } else if (contains(token, "access")
138                            || contains(token, "symbols")
139                            || contains(token, "strict")) {
140                         // nothing
141                 } else if (contains(token, "locks")) {
142                         // get locker here
143                         if (contains(token, ';')) {
144                                 locker_ = "Unlocked";
145                                 vcstatus = UNLOCKED;
146                                 continue;
147                         }
148                         string tmpt;
149                         string s1;
150                         string s2;
151                         do {
152                                 ifs >> tmpt;
153                                 s1 = rtrim(tmpt, ";");
154                                 // tmp is now in the format <user>:<version>
155                                 s1 = split(s1, s2, ':');
156                                 // s2 is user, and s1 is version
157                                 if (s1 == version_) {
158                                         locker_ = s2;
159                                         vcstatus = LOCKED;
160                                         break;
161                                 }
162                         } while (!contains(tmpt, ';'));
163
164                 } else if (token == "comment") {
165                         // we don't need to read any further than this.
166                         read_enough = true;
167                 } else {
168                         // unexpected
169                         LYXERR(Debug::LYXVC, "LyXVC::scanMaster(): unexpected token");
170                 }
171         }
172 }
173
174
175 void RCS::registrer(string const & msg)
176 {
177         string cmd = "ci -q -u -i -t-\"";
178         cmd += msg;
179         cmd += "\" ";
180         cmd += quoteName(onlyFilename(owner_->absFileName()));
181         doVCCommand(cmd, FileName(owner_->filePath()));
182 }
183
184
185 string RCS::checkIn(string const & msg)
186 {
187         int ret = doVCCommand("ci -q -u -m\"" + msg + "\" "
188                     + quoteName(onlyFilename(owner_->absFileName())),
189                     FileName(owner_->filePath()));
190         return ret ? string() : "RCS: Proceeded";
191 }
192
193
194 bool RCS::checkInEnabled()
195 {
196         return owner_ && !owner_->isReadonly();
197 }
198
199
200 string RCS::checkOut()
201 {
202         owner_->markClean();
203         int ret = doVCCommand("co -q -l " + quoteName(onlyFilename(owner_->absFileName())),
204                     FileName(owner_->filePath()));
205         return ret ? string() : "RCS: Proceeded";
206 }
207
208
209 bool RCS::checkOutEnabled()
210 {
211         return owner_ && owner_->isReadonly();
212 }
213
214
215 string RCS::repoUpdate()
216 {
217         lyxerr << "Sorry, not implemented." << endl;
218         return string();
219 }
220
221
222 bool RCS::repoUpdateEnabled()
223 {
224         return false;
225 }
226
227
228 string RCS::lockingToggle()
229 {
230         lyxerr << "Sorry, not implemented." << endl;
231         return string();
232 }
233
234
235 bool RCS::lockingToggleEnabled()
236 {
237         return false;
238 }
239
240
241 void RCS::revert()
242 {
243         doVCCommand("co -f -u" + version() + " "
244                     + quoteName(onlyFilename(owner_->absFileName())),
245                     FileName(owner_->filePath()));
246         // We ignore changes and just reload!
247         owner_->markClean();
248 }
249
250
251 void RCS::undoLast()
252 {
253         LYXERR(Debug::LYXVC, "LyXVC: undoLast");
254         doVCCommand("rcs -o" + version() + " "
255                     + quoteName(onlyFilename(owner_->absFileName())),
256                     FileName(owner_->filePath()));
257 }
258
259
260 bool RCS::undoLastEnabled()
261 {
262         return true;
263 }
264
265
266 void RCS::getLog(FileName const & tmpf)
267 {
268         doVCCommand("rlog " + quoteName(onlyFilename(owner_->absFileName()))
269                     + " > " + quoteName(tmpf.toFilesystemEncoding()),
270                     FileName(owner_->filePath()));
271 }
272
273
274 bool RCS::toggleReadOnlyEnabled()
275 {
276         // This got broken somewhere along lfuns dispatch reorganization.
277         // reloadBuffer would be needed after this, but thats problematic
278         // since we are inside Buffer::dispatch.
279         // return true;
280         return false;
281 }
282
283 // FIXME This could be implemented with cache from scanMaster
284 string RCS::revisionInfo(LyXVC::RevisionInfo const)
285 {
286         return string();
287 }
288
289
290 /////////////////////////////////////////////////////////////////////
291 //
292 // CVS
293 //
294 /////////////////////////////////////////////////////////////////////
295
296 CVS::CVS(FileName const & m, FileName const & f)
297 {
298         master_ = m;
299         file_ = f;
300         scanMaster();
301 }
302
303
304 FileName const CVS::findFile(FileName const & file)
305 {
306         // First we look for the CVS/Entries in the same dir
307         // where we have file.
308         FileName const entries(onlyPath(file.absFilename()) + "/CVS/Entries");
309         string const tmpf = '/' + onlyFilename(file.absFilename()) + '/';
310         LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under cvs in `" << entries
311                              << "' for `" << tmpf << '\'');
312         if (entries.isReadableFile()) {
313                 // Ok we are at least in a CVS dir. Parse the CVS/Entries
314                 // and see if we can find this file. We do a fast and
315                 // dirty parse here.
316                 ifstream ifs(entries.toFilesystemEncoding().c_str());
317                 string line;
318                 while (getline(ifs, line)) {
319                         LYXERR(Debug::LYXVC, "\tEntries: " << line);
320                         if (contains(line, tmpf))
321                                 return entries;
322                 }
323         }
324         return FileName();
325 }
326
327
328 void CVS::scanMaster()
329 {
330         LYXERR(Debug::LYXVC, "LyXVC::CVS: scanMaster. \n     Checking: " << master_);
331         // Ok now we do the real scan...
332         ifstream ifs(master_.toFilesystemEncoding().c_str());
333         string tmpf = '/' + onlyFilename(file_.absFilename()) + '/';
334         LYXERR(Debug::LYXVC, "\tlooking for `" << tmpf << '\'');
335         string line;
336         static regex const reg("/(.*)/(.*)/(.*)/(.*)/(.*)");
337         while (getline(ifs, line)) {
338                 LYXERR(Debug::LYXVC, "\t  line: " << line);
339                 if (contains(line, tmpf)) {
340                         // Ok extract the fields.
341                         smatch sm;
342
343                         regex_match(line, sm, reg);
344
345                         //sm[0]; // whole matched string
346                         //sm[1]; // filename
347                         version_ = sm.str(2);
348                         string const file_date = sm.str(3);
349
350                         //sm[4]; // options
351                         //sm[5]; // tag or tagdate
352                         // FIXME: must double check file is stattable/existing
353                         time_t mod = file_.lastModified();
354                         string mod_date = rtrim(asctime(gmtime(&mod)), "\n");
355                         LYXERR(Debug::LYXVC, "Date in Entries: `" << file_date
356                                 << "'\nModification date of file: `" << mod_date << '\'');
357                         //FIXME this whole locking bussiness is not working under cvs and the machinery
358                         // conforms to the ci usage, not cvs.
359                         if (file_date == mod_date) {
360                                 locker_ = "Unlocked";
361                                 vcstatus = UNLOCKED;
362                         } else {
363                                 // Here we should also to some more checking
364                                 // to see if there are conflicts or not.
365                                 locker_ = "Locked";
366                                 vcstatus = LOCKED;
367                         }
368                         break;
369                 }
370         }
371 }
372
373
374 void CVS::registrer(string const & msg)
375 {
376         doVCCommand("cvs -q add -m \"" + msg + "\" "
377                     + quoteName(onlyFilename(owner_->absFileName())),
378                     FileName(owner_->filePath()));
379 }
380
381
382 string CVS::checkIn(string const & msg)
383 {
384         int ret = doVCCommand("cvs -q commit -m \"" + msg + "\" "
385                     + quoteName(onlyFilename(owner_->absFileName())),
386                     FileName(owner_->filePath()));
387         return ret ? string() : "CVS: Proceeded";
388 }
389
390
391 bool CVS::checkInEnabled()
392 {
393         return true;
394 }
395
396
397 string CVS::checkOut()
398 {
399         // cvs update or perhaps for cvs this should be a noop
400         // we need to detect conflict (eg "C" in output)
401         // before we can do this.
402         lyxerr << "Sorry, not implemented." << endl;
403         return string();
404 }
405
406
407 bool CVS::checkOutEnabled()
408 {
409         return false;
410 }
411
412
413 string CVS::repoUpdate()
414 {
415         lyxerr << "Sorry, not implemented." << endl;
416         return string();
417 }
418
419
420 bool CVS::repoUpdateEnabled()
421 {
422         return false;
423 }
424
425
426 string CVS::lockingToggle()
427 {
428         lyxerr << "Sorry, not implemented." << endl;
429         return string();
430 }
431
432
433 bool CVS::lockingToggleEnabled()
434 {
435         return false;
436 }
437
438
439 void CVS::revert()
440 {
441         // Reverts to the version in CVS repository and
442         // gets the updated version from the repository.
443         string const fil = quoteName(onlyFilename(owner_->absFileName()));
444         // This is sensitive operation, so at lest some check about
445         // existence of cvs program and its file
446         if (doVCCommand("cvs log "+ fil, FileName(owner_->filePath())))
447                 return;
448         FileName f(owner_->absFileName());
449         f.removeFile();
450         doVCCommand("cvs update " + fil,
451                     FileName(owner_->filePath()));
452         owner_->markClean();
453 }
454
455
456 void CVS::undoLast()
457 {
458         // merge the current with the previous version
459         // in a reverse patch kind of way, so that the
460         // result is to revert the last changes.
461         lyxerr << "Sorry, not implemented." << endl;
462 }
463
464
465 bool CVS::undoLastEnabled()
466 {
467         return false;
468 }
469
470
471 void CVS::getLog(FileName const & tmpf)
472 {
473         doVCCommand("cvs log " + quoteName(onlyFilename(owner_->absFileName()))
474                     + " > " + quoteName(tmpf.toFilesystemEncoding()),
475                     FileName(owner_->filePath()));
476 }
477
478
479 bool CVS::toggleReadOnlyEnabled()
480 {
481         return false;
482 }
483
484
485 string CVS::revisionInfo(LyXVC::RevisionInfo const)
486 {
487         return string();
488 }
489
490
491 /////////////////////////////////////////////////////////////////////
492 //
493 // SVN
494 //
495 /////////////////////////////////////////////////////////////////////
496
497 SVN::SVN(FileName const & m, FileName const & f)
498 {
499         owner_ = 0;
500         master_ = m;
501         file_ = f;
502         locked_mode_ = 0;
503         scanMaster();
504 }
505
506
507 FileName const SVN::findFile(FileName const & file)
508 {
509         // First we look for the .svn/entries in the same dir
510         // where we have file.
511         FileName const entries(onlyPath(file.absFilename()) + "/.svn/entries");
512         string const tmpf = onlyFilename(file.absFilename());
513         LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under svn in `" << entries
514                              << "' for `" << tmpf << '\'');
515         if (entries.isReadableFile()) {
516                 // Ok we are at least in a SVN dir. Parse the .svn/entries
517                 // and see if we can find this file. We do a fast and
518                 // dirty parse here.
519                 ifstream ifs(entries.toFilesystemEncoding().c_str());
520                 string line, oldline;
521                 while (getline(ifs, line)) {
522                         if (line == "dir" || line == "file")
523                                 LYXERR(Debug::LYXVC, "\tEntries: " << oldline);
524                         if (oldline == tmpf && line == "file")
525                                 return entries;
526                         oldline = line;
527                 }
528         }
529         return FileName();
530 }
531
532
533 void SVN::scanMaster()
534 {
535         locker_.clear();
536         // vcstatus code is somewhat superflous, until we want
537         // to implement read-only toggle for svn.
538         vcstatus = NOLOCKING;
539         if (checkLockMode()) {
540                 if (isLocked()) {
541                         locker_ = "Locked";
542                         vcstatus = LOCKED;
543                 } else {
544                         locker_ = "Unlocked";
545                         vcstatus = UNLOCKED;
546                 }
547         }
548 }
549
550
551 bool SVN::checkLockMode()
552 {
553         FileName tmpf = FileName::tempName("lyxvcout");
554         if (tmpf.empty()){
555                 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
556                 return N_("Error: Could not generate logfile.");
557         }
558
559         LYXERR(Debug::LYXVC, "Detecting locking mode...");
560         if (doVCCommandCall("svn proplist " + quoteName(file_.onlyFileName())
561                     + " > " + quoteName(tmpf.toFilesystemEncoding()),
562                     file_.onlyPath()))
563                 return false;
564
565         ifstream ifs(tmpf.toFilesystemEncoding().c_str());
566         string line;
567         bool ret = false;
568
569         while (ifs) {
570                 getline(ifs, line);
571                 LYXERR(Debug::LYXVC, line);
572                 if (contains(line, "svn:needs-lock"))
573                         ret = true;
574         }
575         LYXERR(Debug::LYXVC, "Locking enabled: " << ret);
576         ifs.close();
577         locked_mode_ = ret;
578         return ret;
579
580 }
581
582
583 bool SVN::isLocked() const
584 {
585         file_.refresh();
586         return !file_.isReadOnly();
587 }
588
589
590 void SVN::registrer(string const & /*msg*/)
591 {
592         doVCCommand("svn add -q " + quoteName(onlyFilename(owner_->absFileName())),
593                     FileName(owner_->filePath()));
594 }
595
596
597 string SVN::checkIn(string const & msg)
598 {
599         FileName tmpf = FileName::tempName("lyxvcout");
600         if (tmpf.empty()){
601                 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
602                 return N_("Error: Could not generate logfile.");
603         }
604
605         doVCCommand("svn commit -m \"" + msg + "\" "
606                     + quoteName(onlyFilename(owner_->absFileName()))
607                     + " > " + quoteName(tmpf.toFilesystemEncoding()),
608                     FileName(owner_->filePath()));
609
610         string log;
611         string res = scanLogFile(tmpf, log);
612         if (!res.empty())
613                 frontend::Alert::error(_("Revision control error."),
614                                 _("Error when committing to repository.\n"
615                                 "You have to manually resolve the problem.\n"
616                                 "LyX will reopen the document after you press OK."));
617         else
618                 fileLock(false, tmpf, log);
619
620         tmpf.erase();
621         return log.empty() ? string() : "SVN: " + log;
622 }
623
624
625 bool SVN::checkInEnabled()
626 {
627         if (locked_mode_)
628                 return isLocked();
629         else
630                 return true;
631 }
632
633
634 // FIXME Correctly return code should be checked instead of this.
635 // This would need another solution than just plain startscript.
636 // Hint from Andre': QProcess::readAllStandardError()...
637 string SVN::scanLogFile(FileName const & f, string & status)
638 {
639         ifstream ifs(f.toFilesystemEncoding().c_str());
640         string line;
641
642         while (ifs) {
643                 getline(ifs, line);
644                 LYXERR(Debug::LYXVC, line << "\n");
645                 if (!line.empty()) 
646                         status += line + "; ";
647                 if (prefixIs(line, "C ") || prefixIs(line, "CU ")
648                                          || contains(line, "Commit failed")) {
649                         ifs.close();
650                         return line;
651                 }
652                 if (contains(line, "svn:needs-lock")) {
653                         ifs.close();
654                         return line;
655                 }
656         }
657         ifs.close();
658         return string();
659 }
660
661
662 void SVN::fileLock(bool lock, FileName const & tmpf, string &status)
663 {
664         if (!locked_mode_ || (isLocked() == lock))
665                 return;
666
667         string const arg = lock ? "lock " : "unlock ";
668         doVCCommand("svn "+ arg + quoteName(onlyFilename(owner_->absFileName()))
669                     + " > " + quoteName(tmpf.toFilesystemEncoding()),
670                     FileName(owner_->filePath()));
671
672         // Lock error messages go unfortunately on stderr and are unreachible this way.
673         ifstream ifs(tmpf.toFilesystemEncoding().c_str());
674         string line;
675         while (ifs) {
676                 getline(ifs, line);
677                 if (!line.empty()) status += line + "; ";
678         }
679         ifs.close();
680
681         if (!isLocked() && lock)
682                 frontend::Alert::error(_("Revision control error."),
683                         _("Error when acquiring write lock.\n"
684                         "Most probably another user is editing\n"
685                         "the current document now!\n"
686                         "Also check the access to the repository."));
687         if (isLocked() && !lock)
688                 frontend::Alert::error(_("Revision control error."),
689                         _("Error when releasing write lock.\n"
690                         "Check the access to the repository."));
691 }
692
693
694 string SVN::checkOut()
695 {
696         FileName tmpf = FileName::tempName("lyxvcout");
697         if (tmpf.empty()) {
698                 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
699                 return N_("Error: Could not generate logfile.");
700         }
701
702         doVCCommand("svn update --non-interactive " + quoteName(onlyFilename(owner_->absFileName()))
703                     + " > " + quoteName(tmpf.toFilesystemEncoding()),
704                     FileName(owner_->filePath()));
705
706         string log;
707         string res = scanLogFile(tmpf, log);
708         if (!res.empty())
709                 frontend::Alert::error(_("Revision control error."),
710                         bformat(_("Error when updating from repository.\n"
711                                 "You have to manually resolve the conflicts NOW!\n'%1$s'.\n\n"
712                                 "After pressing OK, LyX will try to reopen the resolved document."),
713                         from_local8bit(res)));
714
715         fileLock(true, tmpf, log);
716
717         tmpf.erase();
718         return log.empty() ? string() : "SVN: " + log;
719 }
720
721
722 bool SVN::checkOutEnabled()
723 {
724         if (locked_mode_)
725                 return !isLocked();
726         else
727                 return true;
728 }
729
730
731 string SVN::repoUpdate()
732 {
733         FileName tmpf = FileName::tempName("lyxvcout");
734         if (tmpf.empty()) {
735                 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
736                 return N_("Error: Could not generate logfile.");
737         }
738
739         doVCCommand("svn diff " + quoteName(owner_->filePath())
740         + " > " + quoteName(tmpf.toFilesystemEncoding()),
741         FileName(owner_->filePath()));
742         docstring res = tmpf.fileContents("UTF-8");
743         if (!res.empty()) {
744                 LYXERR(Debug::LYXVC, "Diff detected:\n" << res);
745                 docstring const file = from_utf8(owner_->filePath());
746                 docstring text = bformat(_("There were detected changes "
747                                 "in the working directory:\n%1$s\n\n"
748                                 "In case of file conflict version of the local directory files "
749                                 "will be preferred."
750                                 "\n\nContinue?"), file);
751                 int ret = frontend::Alert::prompt(_("Changes detected"),
752                                 text, 0, 1, _("&Yes"), _("&No"), _("View &Log ..."));
753                 if (ret == 2 ) {
754                         dispatch(FuncRequest(LFUN_DIALOG_SHOW, "file " + tmpf.absFilename()));
755                         ret = frontend::Alert::prompt(_("Changes detected"),
756                                 text, 0, 1, _("&Yes"), _("&No"));
757                         hideDialogs("file", 0);
758                 }
759                 if (ret == 1 ) {
760                         tmpf.erase();
761                         return string();
762                 }
763         }
764
765         // Reverting looks too harsh, see bug #6255.
766         // doVCCommand("svn revert -R " + quoteName(owner_->filePath())
767         // + " > " + quoteName(tmpf.toFilesystemEncoding()),
768         // FileName(owner_->filePath()));
769         // res = "Revert log:\n" + tmpf.fileContents("UTF-8");
770         doVCCommand("svn update --accept mine-full " + quoteName(owner_->filePath())
771                 + " > " + quoteName(tmpf.toFilesystemEncoding()),
772         FileName(owner_->filePath()));
773         res += "Update log:\n" + tmpf.fileContents("UTF-8");
774
775         LYXERR(Debug::LYXVC, res);
776         tmpf.erase();
777         return to_utf8(res);
778 }
779
780
781 bool SVN::repoUpdateEnabled()
782 {
783         return true;
784 }
785
786
787 string SVN::lockingToggle()
788 {
789         FileName tmpf = FileName::tempName("lyxvcout");
790         if (tmpf.empty()) {
791                 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
792                 return N_("Error: Could not generate logfile.");
793         }
794
795         int ret = doVCCommand("svn proplist " + quoteName(onlyFilename(owner_->absFileName()))
796                     + " > " + quoteName(tmpf.toFilesystemEncoding()),
797                     FileName(owner_->filePath()));
798         if (ret)
799                 return string();
800
801         string log;
802         string res = scanLogFile(tmpf, log);
803         bool locking = contains(res, "svn:needs-lock");
804         if (!locking)
805                 ret = doVCCommand("svn propset svn:needs-lock ON "
806                     + quoteName(onlyFilename(owner_->absFileName()))
807                     + " > " + quoteName(tmpf.toFilesystemEncoding()),
808                     FileName(owner_->filePath()));
809         else
810                 ret = doVCCommand("svn propdel svn:needs-lock "
811                     + quoteName(onlyFilename(owner_->absFileName()))
812                     + " > " + quoteName(tmpf.toFilesystemEncoding()),
813                     FileName(owner_->filePath()));
814         if (ret)
815                 return string();
816
817         tmpf.erase();
818         frontend::Alert::warning(_("VCN File Locking"),
819                 (locking ? _("Locking property unset.") : _("Locking property set.")) + "\n"
820                 + _("Do not forget to commit the locking property into the repository."),
821                 true);
822
823         return string("SVN: ") +  N_("Locking property set.");
824 }
825
826
827 bool SVN::lockingToggleEnabled()
828 {
829         return true;
830 }
831
832
833 void SVN::revert()
834 {
835         // Reverts to the version in CVS repository and
836         // gets the updated version from the repository.
837         string const fil = quoteName(onlyFilename(owner_->absFileName()));
838
839         doVCCommand("svn revert -q " + fil,
840                     FileName(owner_->filePath()));
841         owner_->markClean();
842 }
843
844
845 void SVN::undoLast()
846 {
847         // merge the current with the previous version
848         // in a reverse patch kind of way, so that the
849         // result is to revert the last changes.
850         lyxerr << "Sorry, not implemented." << endl;
851 }
852
853
854 bool SVN::undoLastEnabled()
855 {
856         return false;
857 }
858
859
860 string SVN::revisionInfo(LyXVC::RevisionInfo const info)
861 {
862         switch (info) {
863                 case LyXVC::File:
864                         if (rev_file_cache_.empty())
865                                 rev_file_cache_ = getFileRevisionInfo();
866                         if (rev_file_cache_.empty())
867                                 rev_file_cache_ = "?";
868                         if (rev_file_cache_ == "?")
869                                 return string();
870
871                         return rev_file_cache_;
872
873                 case LyXVC::Tree:
874                         if (rev_tree_cache_.empty())
875                                 rev_tree_cache_ = getTreeRevisionInfo();
876                         if (rev_tree_cache_.empty())
877                                 rev_tree_cache_ = "?";
878                         if (rev_tree_cache_ == "?")
879                                 return string();
880
881                         return rev_tree_cache_;
882         }
883         return string();
884 }
885
886
887 std::string SVN::getFileRevisionInfo()
888 {
889         FileName tmpf = FileName::tempName("lyxvcout");
890
891         doVCCommand("svn info --xml " + quoteName(onlyFilename(owner_->absFileName()))
892                     + " > " + quoteName(tmpf.toFilesystemEncoding()),
893                     FileName(owner_->filePath()));
894
895         if (tmpf.empty())
896                 return string();
897
898         ifstream ifs(tmpf.toFilesystemEncoding().c_str());
899         string line;
900         // commit log part
901         bool c = false;
902         string rev;
903
904         while (ifs) {
905                 getline(ifs, line);
906                 LYXERR(Debug::LYXVC, line);
907                 if (prefixIs(line, "<commit"))
908                         c = true;
909                 if (c && prefixIs(line, "   revision=\"") && suffixIs(line, "\">")) {
910                         string l1 = subst(line, "revision=\"", "");
911                         string l2 = trim(subst(l1, "\">", ""));
912                         if (isStrInt(l2))
913                                 rev = l2;
914                 }
915         }
916         ifs.close();
917         tmpf.erase();
918         return rev;
919 }
920
921
922 std::string SVN::getTreeRevisionInfo()
923 {
924         FileName tmpf = FileName::tempName("lyxvcout");
925
926         doVCCommand("svnversion -n . > " + quoteName(tmpf.toFilesystemEncoding()),
927                     FileName(owner_->filePath()));
928
929         if (tmpf.empty())
930                 return string();
931
932         // only first line in case something bad happens.
933         ifstream ifs(tmpf.toFilesystemEncoding().c_str());
934         string line;
935         getline(ifs, line);
936         ifs.close();
937         tmpf.erase();
938         return line;
939 }
940
941
942 void SVN::getLog(FileName const & tmpf)
943 {
944         doVCCommand("svn log " + quoteName(onlyFilename(owner_->absFileName()))
945                     + " > " + quoteName(tmpf.toFilesystemEncoding()),
946                     FileName(owner_->filePath()));
947 }
948
949
950 bool SVN::toggleReadOnlyEnabled()
951 {
952         return false;
953 }
954
955
956 } // namespace lyx