]> git.lyx.org Git - lyx.git/blob - src/VCBackend.cpp
Add \makeat switches to babel settings if necessary.
[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  * \author Pavel Sanda
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "VCBackend.h"
15 #include "Buffer.h"
16 #include "LyX.h"
17 #include "FuncRequest.h"
18
19 #include "frontends/alert.h"
20 #include "frontends/Application.h"
21
22 #include "support/convert.h"
23 #include "support/debug.h"
24 #include "support/filetools.h"
25 #include "support/gettext.h"
26 #include "support/lstrings.h"
27 #include "support/Path.h"
28 #include "support/Systemcall.h"
29 #include "support/regex.h"
30
31 #include <fstream>
32
33 using namespace std;
34 using namespace lyx::support;
35
36
37
38 namespace lyx {
39
40
41 int VCS::doVCCommandCall(string const & cmd, FileName const & path)
42 {
43         LYXERR(Debug::LYXVC, "doVCCommandCall: " << cmd);
44         Systemcall one;
45         support::PathChanger p(path);
46         return one.startscript(Systemcall::Wait, cmd, string(), false);
47 }
48
49
50 int VCS::doVCCommand(string const & cmd, FileName const & path, bool reportError)
51 {
52         if (owner_)
53                 owner_->setBusy(true);
54
55         int const ret = doVCCommandCall(cmd, path);
56
57         if (owner_)
58                 owner_->setBusy(false);
59         if (ret && reportError)
60                 frontend::Alert::error(_("Revision control error."),
61                         bformat(_("Some problem occured while running the command:\n"
62                                   "'%1$s'."),
63                         from_utf8(cmd)));
64         return ret;
65 }
66
67
68 bool VCS::makeRCSRevision(string const &version, string &revis) const
69 {
70         string rev = revis;
71         
72         if (isStrInt(rev)) {
73                 int back = convert<int>(rev);
74                 // if positive use as the last number in the whole revision string
75                 if (back > 0) {
76                         string base;
77                         rsplit(version, base , '.' );
78                         rev = base + "." + rev;
79                 }
80                 if (back == 0)
81                         rev = version;
82                 // we care about the last number from revision string
83                 // in case of backward indexing
84                 if (back < 0) {
85                         string cur, base;
86                         cur = rsplit(version, base , '.' );
87                         if (!isStrInt(cur))
88                                 return false;
89                         int want = convert<int>(cur) + back;
90                         if (want <= 0)
91                                 return false;
92                         
93                         rev = base + "." + convert<string>(want);
94                 }
95         }
96
97         revis = rev;
98         return true;
99 }
100
101 bool VCS::checkparentdirs(FileName const & file, std::string const & pathname)
102 {
103         FileName dirname = file.onlyPath();
104         FileName tocheck = FileName(addName(dirname.absFileName(),pathname));
105         bool result = tocheck.exists();
106         while ( !result && !dirname.empty() ) {
107                 dirname = dirname.parentPath();
108                 tocheck = FileName(addName(dirname.absFileName(),pathname));
109                 result = tocheck.exists();
110         }
111         return result;
112 }
113
114         
115 /////////////////////////////////////////////////////////////////////
116 //
117 // RCS
118 //
119 /////////////////////////////////////////////////////////////////////
120
121 RCS::RCS(FileName const & m)
122 {
123         master_ = m;
124         scanMaster();
125 }
126
127
128 FileName const RCS::findFile(FileName const & file)
129 {
130         // Check if *,v exists.
131         FileName tmp(file.absFileName() + ",v");
132         LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under rcs: " << tmp);
133         if (tmp.isReadableFile()) {
134                 LYXERR(Debug::LYXVC, "Yes, " << file << " is under rcs.");
135                 return tmp;
136         }
137
138         // Check if RCS/*,v exists.
139         tmp = FileName(addName(addPath(onlyPath(file.absFileName()), "RCS"), file.absFileName()) + ",v");
140         LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under rcs: " << tmp);
141         if (tmp.isReadableFile()) {
142                 LYXERR(Debug::LYXVC, "Yes, " << file << " is under rcs.");
143                 return tmp;
144         }
145
146         return FileName();
147 }
148
149
150 void RCS::retrieve(FileName const & file)
151 {
152         LYXERR(Debug::LYXVC, "LyXVC::RCS: retrieve.\n\t" << file);
153         doVCCommandCall("co -q -r " + quoteName(file.toFilesystemEncoding()),
154                          FileName());
155 }
156
157
158 void RCS::scanMaster()
159 {
160         if (master_.empty())
161                 return;
162
163         LYXERR(Debug::LYXVC, "LyXVC::RCS: scanMaster: " << master_);
164
165         ifstream ifs(master_.toFilesystemEncoding().c_str());
166
167         string token;
168         bool read_enough = false;
169
170         while (!read_enough && ifs >> token) {
171                 LYXERR(Debug::LYXVC, "LyXVC::scanMaster: current lex text: `"
172                         << token << '\'');
173
174                 if (token.empty())
175                         continue;
176                 else if (token == "head") {
177                         // get version here
178                         string tmv;
179                         ifs >> tmv;
180                         tmv = rtrim(tmv, ";");
181                         version_ = tmv;
182                         LYXERR(Debug::LYXVC, "LyXVC: version found to be " << tmv);
183                 } else if (contains(token, "access")
184                            || contains(token, "symbols")
185                            || contains(token, "strict")) {
186                         // nothing
187                 } else if (contains(token, "locks")) {
188                         // get locker here
189                         if (contains(token, ';')) {
190                                 locker_ = "Unlocked";
191                                 vcstatus = UNLOCKED;
192                                 continue;
193                         }
194                         string tmpt;
195                         string s1;
196                         string s2;
197                         do {
198                                 ifs >> tmpt;
199                                 s1 = rtrim(tmpt, ";");
200                                 // tmp is now in the format <user>:<version>
201                                 s1 = split(s1, s2, ':');
202                                 // s2 is user, and s1 is version
203                                 if (s1 == version_) {
204                                         locker_ = s2;
205                                         vcstatus = LOCKED;
206                                         break;
207                                 }
208                         } while (!contains(tmpt, ';'));
209
210                 } else if (token == "comment") {
211                         // we don't need to read any further than this.
212                         read_enough = true;
213                 } else {
214                         // unexpected
215                         LYXERR(Debug::LYXVC, "LyXVC::scanMaster(): unexpected token");
216                 }
217         }
218 }
219
220
221 void RCS::registrer(string const & msg)
222 {
223         string cmd = "ci -q -u -i -t-\"";
224         cmd += msg;
225         cmd += "\" ";
226         cmd += quoteName(onlyFileName(owner_->absFileName()));
227         doVCCommand(cmd, FileName(owner_->filePath()));
228 }
229
230
231 string RCS::checkIn(string const & msg)
232 {
233         int ret = doVCCommand("ci -q -u -m\"" + msg + "\" "
234                     + quoteName(onlyFileName(owner_->absFileName())),
235                     FileName(owner_->filePath()));
236         return ret ? string() : "RCS: Proceeded";
237 }
238
239
240 bool RCS::checkInEnabled()
241 {
242         return owner_ && !owner_->isReadonly();
243 }
244
245
246 bool RCS::isCheckInWithConfirmation()
247 {
248         // FIXME one day common getDiff for all backends
249         // docstring diff;
250         // if (getDiff(file, diff) && diff.empty())
251         //      return false;
252
253         FileName tmpf = FileName::tempName("lyxvcout");
254         if (tmpf.empty()) {
255                 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
256                 return true;
257         }
258
259         doVCCommandCall("rcsdiff " + quoteName(owner_->absFileName())
260                     + " > " + quoteName(tmpf.toFilesystemEncoding()),
261                 FileName(owner_->filePath()));
262
263         docstring diff = tmpf.fileContents("UTF-8");
264         tmpf.erase();
265
266         if (diff.empty())
267                 return false;
268
269         return true;
270 }
271
272
273 string RCS::checkOut()
274 {
275         owner_->markClean();
276         int ret = doVCCommand("co -q -l " + quoteName(onlyFileName(owner_->absFileName())),
277                     FileName(owner_->filePath()));
278         return ret ? string() : "RCS: Proceeded";
279 }
280
281
282 bool RCS::checkOutEnabled()
283 {
284         return owner_ && owner_->isReadonly();
285 }
286
287
288 string RCS::repoUpdate()
289 {
290         lyxerr << "Sorry, not implemented." << endl;
291         return string();
292 }
293
294
295 bool RCS::repoUpdateEnabled()
296 {
297         return false;
298 }
299
300
301 string RCS::lockingToggle()
302 {
303         //FIXME this might be actually possible, study rcs -U, rcs -L.
304         //State should be easy to get inside scanMaster.
305         //It would fix #4370 and make rcs/svn usage even more closer.
306         lyxerr << "Sorry, not implemented." << endl;
307         return string();
308 }
309
310
311 bool RCS::lockingToggleEnabled()
312 {
313         return false;
314 }
315
316
317 bool RCS::revert()
318 {
319         if (doVCCommand("co -f -u" + version_ + " "
320                     + quoteName(onlyFileName(owner_->absFileName())),
321                     FileName(owner_->filePath())))
322                 return false;
323         // We ignore changes and just reload!
324         owner_->markClean();
325         return true;
326 }
327
328
329 bool RCS::isRevertWithConfirmation()
330 {
331         //FIXME owner && diff ?
332         return true;
333 }
334
335
336 void RCS::undoLast()
337 {
338         LYXERR(Debug::LYXVC, "LyXVC: undoLast");
339         doVCCommand("rcs -o" + version_ + " "
340                     + quoteName(onlyFileName(owner_->absFileName())),
341                     FileName(owner_->filePath()));
342 }
343
344
345 bool RCS::undoLastEnabled()
346 {
347         return true;
348 }
349
350
351 void RCS::getLog(FileName const & tmpf)
352 {
353         doVCCommand("rlog " + quoteName(onlyFileName(owner_->absFileName()))
354                     + " > " + quoteName(tmpf.toFilesystemEncoding()),
355                     FileName(owner_->filePath()));
356 }
357
358
359 bool RCS::toggleReadOnlyEnabled()
360 {
361         // This got broken somewhere along lfuns dispatch reorganization.
362         // reloadBuffer would be needed after this, but thats problematic
363         // since we are inside Buffer::dispatch.
364         // return true;
365         return false;
366 }
367
368
369 string RCS::revisionInfo(LyXVC::RevisionInfo const info)
370 {
371         if (info == LyXVC::File)
372                 return version_;
373         // fill the rest of the attributes for a single file
374         if (rev_date_cache_.empty())
375                 if (!getRevisionInfo())
376                         return string();
377
378         switch (info) {
379                 case LyXVC::Author:
380                         return rev_author_cache_;
381                 case LyXVC::Date:
382                         return rev_date_cache_;
383                 case LyXVC::Time:
384                         return rev_time_cache_;
385                 default: ;
386         }
387
388         return string();
389 }
390
391
392 bool RCS::getRevisionInfo()
393 {
394         FileName tmpf = FileName::tempName("lyxvcout");
395         if (tmpf.empty()) {
396                 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
397                 return false;
398         }
399         doVCCommand("rlog -r " + quoteName(onlyFileName(owner_->absFileName()))
400                 + " > " + quoteName(tmpf.toFilesystemEncoding()),
401                 FileName(owner_->filePath()));
402
403         if (tmpf.empty())
404                 return false;
405
406         ifstream ifs(tmpf.toFilesystemEncoding().c_str());
407         string line;
408
409         // we reached to the entry, i.e. after initial log message
410         bool entry=false;
411         // line with critical info, e.g:
412         //"date: 2011/07/02 11:02:54;  author: sanda;  state: Exp;  lines: +17 -2"
413         string result;
414
415         while (ifs) {
416                 getline(ifs, line);
417                 LYXERR(Debug::LYXVC, line);
418                 if (entry && prefixIs(line, "date:")) {
419                         result = line;
420                         break;
421                 }
422                 if (prefixIs(line, "revision"))
423                         entry = true;
424         }
425         if (result.empty())
426                 return false;
427
428         rev_date_cache_ = token(result, ' ', 1);
429         rev_time_cache_ = rtrim(token(result, ' ', 2), ";");
430         rev_author_cache_ = trim(token(token(result, ';', 1), ':', 1));
431
432         return !rev_author_cache_.empty();
433 }
434
435 bool RCS::prepareFileRevision(string const &revis, string & f)
436 {
437         string rev = revis;
438         if (!VCS::makeRCSRevision(version_, rev))
439                 return false;
440
441         FileName tmpf = FileName::tempName("lyxvcrev_" + rev + "_");
442         if (tmpf.empty()) {
443                 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
444                 return false;
445         }
446
447         doVCCommand("co -p" + rev + " "
448                       + quoteName(onlyFileName(owner_->absFileName()))
449                       + " > " + quoteName(tmpf.toFilesystemEncoding()),
450                 FileName(owner_->filePath()));
451         if (tmpf.isFileEmpty())
452                 return false;
453
454         f = tmpf.absFileName();
455         return true;
456 }
457
458
459 bool RCS::prepareFileRevisionEnabled()
460 {
461         return true;
462 }
463
464
465 /////////////////////////////////////////////////////////////////////
466 //
467 // CVS
468 //
469 /////////////////////////////////////////////////////////////////////
470
471 CVS::CVS(FileName const & m, FileName const & f)
472 {
473         master_ = m;
474         file_ = f;
475         have_rev_info_ = false;
476         scanMaster();
477 }
478
479
480 FileName const CVS::findFile(FileName const & file)
481 {
482         // First we look for the CVS/Entries in the same dir
483         // where we have file.
484         FileName const entries(onlyPath(file.absFileName()) + "/CVS/Entries");
485         string const tmpf = '/' + onlyFileName(file.absFileName()) + '/';
486         LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under cvs in `" << entries
487                              << "' for `" << tmpf << '\'');
488         if (entries.isReadableFile()) {
489                 // Ok we are at least in a CVS dir. Parse the CVS/Entries
490                 // and see if we can find this file. We do a fast and
491                 // dirty parse here.
492                 ifstream ifs(entries.toFilesystemEncoding().c_str());
493                 string line;
494                 while (getline(ifs, line)) {
495                         LYXERR(Debug::LYXVC, "\tEntries: " << line);
496                         if (contains(line, tmpf))
497                                 return entries;
498                 }
499         }
500         return FileName();
501 }
502
503
504 void CVS::scanMaster()
505 {
506         LYXERR(Debug::LYXVC, "LyXVC::CVS: scanMaster. \n     Checking: " << master_);
507         // Ok now we do the real scan...
508         ifstream ifs(master_.toFilesystemEncoding().c_str());
509         string name = onlyFileName(file_.absFileName());
510         string tmpf = '/' + name + '/';
511         LYXERR(Debug::LYXVC, "\tlooking for `" << tmpf << '\'');
512         string line;
513         static regex const reg("/(.*)/(.*)/(.*)/(.*)/(.*)");
514         while (getline(ifs, line)) {
515                 LYXERR(Debug::LYXVC, "\t  line: " << line);
516                 if (contains(line, tmpf)) {
517                         // Ok extract the fields.
518                         smatch sm;
519
520                         regex_match(line, sm, reg);
521
522                         //sm[0]; // whole matched string
523                         //sm[1]; // filename
524                         version_ = sm.str(2);
525                         string const file_date = sm.str(3);
526
527                         //sm[4]; // options
528                         //sm[5]; // tag or tagdate
529                         if (file_.isReadableFile()) {
530                                 time_t mod = file_.lastModified();
531                                 string mod_date = rtrim(asctime(gmtime(&mod)), "\n");
532                                 LYXERR(Debug::LYXVC, "Date in Entries: `" << file_date
533                                         << "'\nModification date of file: `" << mod_date << '\'');
534                                 if (file_.isReadOnly()) {
535                                         // readonly checkout is unlocked
536                                         vcstatus = UNLOCKED;
537                                 } else {
538                                         FileName bdir(addPath(master_.onlyPath().absFileName(),"Base"));
539                                         FileName base(addName(bdir.absFileName(),name));
540                                         // if base version is existent "cvs edit" was used to lock
541                                         vcstatus = base.isReadableFile() ? LOCKED : NOLOCKING;
542                                 }
543                         } else {
544                                 vcstatus = NOLOCKING;
545                         }
546                         break;
547                 }
548         }
549 }
550
551
552 string const CVS::getTarget(OperationMode opmode) const
553 {
554         switch(opmode) {
555         case Directory:
556                 // in client server mode CVS does not like full path operand for directory operation
557                 // since LyX switches to the repo dir "." is good enough as target
558                 return ".";
559         case File:
560                 return quoteName(onlyFileName(owner_->absFileName()));
561         }
562         return string();
563 }
564
565
566 docstring CVS::toString(CvsStatus status) const
567 {
568         switch (status) {
569         case UpToDate:
570                 return _("Up-to-date");
571         case LocallyModified:
572                 return _("Locally Modified");
573         case LocallyAdded:
574                 return _("Locally Added");
575         case NeedsMerge:
576                 return _("Needs Merge");
577         case NeedsCheckout:
578                 return _("Needs Checkout");
579         case NoCvsFile:
580                 return _("No CVS file");
581         case StatusError:
582                 return _("Cannot retrieve CVS status");
583         }
584         return 0;
585 }
586
587
588 int CVS::doVCCommandWithOutput(string const & cmd, FileName const & path,
589         FileName const & output, bool reportError)
590 {
591         string redirection = output.empty() ? "" : " > " + quoteName(output.toFilesystemEncoding());
592         return doVCCommand(cmd + redirection, path, reportError);
593 }
594
595
596 int CVS::doVCCommandCallWithOutput(std::string const & cmd,
597         support::FileName const & path,
598         support::FileName const & output)
599 {
600         string redirection = output.empty() ? "" : " > " + quoteName(output.toFilesystemEncoding());
601         return doVCCommandCall(cmd + redirection, path);
602 }
603
604
605 CVS::CvsStatus CVS::getStatus()
606 {
607         FileName tmpf = FileName::tempName("lyxvcout");
608         if (tmpf.empty()) {
609                 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
610                 return StatusError;
611         }
612
613         if (doVCCommandCallWithOutput("cvs status " + getTarget(File),
614                 FileName(owner_->filePath()), tmpf)) {
615                 tmpf.removeFile();
616                 return StatusError;
617         }
618
619         ifstream ifs(tmpf.toFilesystemEncoding().c_str());
620         CvsStatus status = NoCvsFile;
621
622         while (ifs) {
623                 string line;
624                 getline(ifs, line);
625                 LYXERR(Debug::LYXVC, line << "\n");
626                 if (prefixIs(line, "File:")) {
627                         if (contains(line, "Up-to-date"))
628                                 status = UpToDate;
629                         else if (contains(line, "Locally Modified"))
630                                 status = LocallyModified;
631                         else if (contains(line, "Locally Added"))
632                                 status = LocallyAdded;
633                         else if (contains(line, "Needs Merge"))
634                                 status = NeedsMerge;
635                         else if (contains(line, "Needs Checkout"))
636                                 status = NeedsCheckout;
637                 }
638         }
639         tmpf.removeFile();
640         return status;
641 }
642
643 void CVS::getRevisionInfo()
644 {
645         if (have_rev_info_)
646                 return;
647         have_rev_info_ = true;
648         FileName tmpf = FileName::tempName("lyxvcout");
649         if (tmpf.empty()) {
650                 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
651                 return;
652         }
653         
654         int rc = doVCCommandCallWithOutput("cvs log -r" + version_ 
655                 + " " + getTarget(File),
656                 FileName(owner_->filePath()), tmpf);
657         if (rc) {
658                 tmpf.removeFile();
659                 LYXERR(Debug::LYXVC, "cvs log failed with exit code " << rc);
660                 return;
661         }
662         
663         ifstream ifs(tmpf.toFilesystemEncoding().c_str());
664         static regex const reg("date: (.*) (.*) (.*);  author: (.*);  state: (.*);(.*)");
665
666         while (ifs) {
667                 string line;
668                 getline(ifs, line);
669                 LYXERR(Debug::LYXVC, line << "\n");
670                 if (prefixIs(line, "date:")) {
671                         smatch sm;
672                         regex_match(line, sm, reg);
673                         //sm[0]; // whole matched string
674                         rev_date_cache_ = sm[1];
675                         rev_time_cache_ = sm[2];
676                         //sm[3]; // GMT offset
677                         rev_author_cache_ = sm[4];
678                         break;
679                 }
680         }
681         tmpf.removeFile();
682         if (rev_author_cache_.empty())
683                 LYXERR(Debug::LYXVC,
684                    "Could not retrieve revision info for " << version_ <<
685                    " of " << getTarget(File));
686 }
687
688
689 void CVS::registrer(string const & msg)
690 {
691         doVCCommand("cvs -q add -m \"" + msg + "\" "
692                 + getTarget(File),
693                 FileName(owner_->filePath()));
694 }
695
696
697 void CVS::getDiff(OperationMode opmode, FileName const & tmpf)
698 {
699         doVCCommandWithOutput("cvs diff " + getTarget(opmode),
700                 FileName(owner_->filePath()), tmpf, false);
701 }
702
703
704 int CVS::edit()
705 {
706         vcstatus = LOCKED;
707         return doVCCommand("cvs -q edit " + getTarget(File),
708                 FileName(owner_->filePath()));
709 }
710
711
712 int CVS::unedit()
713 {
714         vcstatus = UNLOCKED;
715         return doVCCommand("cvs -q unedit " + getTarget(File),
716                 FileName(owner_->filePath()));
717 }
718
719
720 int CVS::update(OperationMode opmode, FileName const & tmpf)
721 {
722         return doVCCommandWithOutput("cvs -q update "
723                 + getTarget(opmode),
724                 FileName(owner_->filePath()), tmpf, false);
725 }
726
727
728 string CVS::scanLogFile(FileName const & f, string & status)
729 {
730         ifstream ifs(f.toFilesystemEncoding().c_str());
731
732         while (ifs) {
733                 string line;
734                 getline(ifs, line);
735                 LYXERR(Debug::LYXVC, line << "\n");
736                 if (!line.empty())
737                         status += line + "; ";
738                 if (prefixIs(line, "C ")) {
739                         ifs.close();
740                         return line;
741                 }
742         }
743         ifs.close();
744         return string();
745 }
746         
747         
748 string CVS::checkIn(string const & msg)
749 {
750         CvsStatus status = getStatus();
751         switch (status) {
752         case UpToDate:
753                 if (vcstatus != NOLOCKING)
754                         unedit();
755                 return "CVS: Proceeded";
756         case LocallyModified:
757         case LocallyAdded: {
758                 int rc = doVCCommand("cvs -q commit -m \"" + msg + "\" "
759                         + getTarget(File),
760                     FileName(owner_->filePath()));
761                 return rc ? string() : "CVS: Proceeded";
762         }
763         case NeedsMerge:
764         case NeedsCheckout:
765                 frontend::Alert::error(_("Revision control error."),
766                         _("The repository version is newer then the current check out.\n"
767                           "You have to update from repository first or revert your changes.")) ;
768                 break;
769         default:
770                 frontend::Alert::error(_("Revision control error."),
771                         bformat(_("Bad status when checking in changes.\n"
772                                           "\n'%1$s'\n\n"),
773                                 toString(status)));
774                 break;
775         }
776         return string();
777 }
778
779
780 bool CVS::isLocked() const
781 {
782         FileName fn(owner_->absFileName());
783         fn.refresh();
784         return !fn.isReadOnly();
785 }
786
787
788 bool CVS::checkInEnabled()
789 {
790         if (vcstatus != NOLOCKING)
791                 return isLocked();
792         else
793                 return true;
794 }
795
796
797 bool CVS::isCheckInWithConfirmation()
798 {
799         CvsStatus status = getStatus();
800         return status == LocallyModified || status == LocallyAdded;
801 }
802
803
804 string CVS::checkOut()
805 {
806         if (vcstatus != NOLOCKING && edit())
807                 return string();
808         FileName tmpf = FileName::tempName("lyxvcout");
809         if (tmpf.empty()) {
810                 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
811                 return string();
812         }
813         
814         int rc = update(File, tmpf);
815         string log;
816         string const res = scanLogFile(tmpf, log);
817         if (!res.empty()) {
818                 frontend::Alert::error(_("Revision control error."),
819                         bformat(_("Error when updating from repository.\n"
820                                 "You have to manually resolve the conflicts NOW!\n'%1$s'.\n\n"
821                                 "After pressing OK, LyX will try to reopen the resolved document."),
822                                 from_local8bit(res)));
823                 rc = 0;
824         }
825         
826         tmpf.erase();
827         return rc ? string() : log.empty() ? "CVS: Proceeded" : "CVS: " + log;
828 }
829
830
831 bool CVS::checkOutEnabled()
832 {
833         if (vcstatus != NOLOCKING)
834                 return !isLocked();
835         else
836                 return true;
837 }
838
839
840 string CVS::repoUpdate()
841 {
842         FileName tmpf = FileName::tempName("lyxvcout");
843         if (tmpf.empty()) {
844                 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
845                 return string();
846         }
847         
848         getDiff(Directory, tmpf);
849         docstring res = tmpf.fileContents("UTF-8");
850         if (!res.empty()) {
851                 LYXERR(Debug::LYXVC, "Diff detected:\n" << res);
852                 docstring const file = from_utf8(owner_->filePath());
853                 docstring text = bformat(_("There were detected changes "
854                                 "in the working directory:\n%1$s\n\n"
855                                 "Possible file conflicts must be then resolved manually "
856                                 "or you will need to revert back to the repository version."), file);
857                 int ret = frontend::Alert::prompt(_("Changes detected"),
858                                 text, 0, 1, _("&Continue"), _("&Abort"), _("View &Log ..."));
859                 if (ret == 2 ) {
860                         dispatch(FuncRequest(LFUN_DIALOG_SHOW, "file " + tmpf.absFileName()));
861                         ret = frontend::Alert::prompt(_("Changes detected"),
862                                 text, 0, 1, _("&Continue"), _("&Abort"));
863                         hideDialogs("file", 0);
864                 }
865                 if (ret == 1 ) {
866                         tmpf.removeFile();
867                         return string();
868                 }
869         }
870
871         int rc = update(Directory, tmpf);
872         res += "Update log:\n" + tmpf.fileContents("UTF-8");
873         LYXERR(Debug::LYXVC, res);
874
875         string log;
876         string sres = scanLogFile(tmpf, log);
877         if (!sres.empty()) {
878                 docstring const file = owner_->fileName().displayName(20);
879                 frontend::Alert::error(_("Revision control error."),
880                         bformat(_("Error when updating document %1$s from repository.\n"
881                                           "You have to manually resolve the conflicts NOW!\n'%2$s'.\n\n"
882                                           "After pressing OK, LyX will try to reopen the resolved document."),
883                                 file, from_local8bit(sres)));
884                 rc = 0;
885         }
886         
887         tmpf.removeFile();
888
889         return rc ? string() : log.empty() ? "CVS: Proceeded" : "CVS: " + log;
890 }
891
892
893 bool CVS::repoUpdateEnabled()
894 {
895         return true;
896 }
897
898
899 string CVS::lockingToggle()
900 {
901         lyxerr << "Sorry, not implemented." << endl;
902         return string();
903 }
904
905
906 bool CVS::lockingToggleEnabled()
907 {
908         return false;
909 }
910
911
912 bool CVS::isRevertWithConfirmation()
913 {
914         CvsStatus status = getStatus();
915         return !owner_->isClean() || status == LocallyModified || status == NeedsMerge;
916 }
917
918
919 bool CVS::revert()
920 {
921         // Reverts to the version in CVS repository and
922         // gets the updated version from the repository.
923         CvsStatus status = getStatus();
924         switch (status) {
925         case UpToDate:
926                 if (vcstatus != NOLOCKING)
927                         return 0 == unedit();
928                 break;
929         case NeedsMerge:
930         case NeedsCheckout:
931         case LocallyModified: {
932                 FileName f(owner_->absFileName());
933                 f.removeFile();
934                 update(File, FileName());
935                 owner_->markClean();
936                 break;
937         }
938         case LocallyAdded: {
939                 docstring const file = owner_->fileName().displayName(20);
940                 frontend::Alert::error(_("Revision control error."),
941                         bformat(_("The document %1$s is not in repository.\n"
942                                   "You have to check in the first revision before you can revert."),
943                                 file)) ;
944                 return false;
945         }
946         default: {
947                 docstring const file = owner_->fileName().displayName(20);
948                 frontend::Alert::error(_("Revision control error."),
949                         bformat(_("Cannot revert document %1$s to repository version.\n"
950                                   "The status '%2$s' is unexpected."),
951                                 file, toString(status)));
952                 return false;
953                 }
954         }
955         return true;
956 }
957
958
959 void CVS::undoLast()
960 {
961         // merge the current with the previous version
962         // in a reverse patch kind of way, so that the
963         // result is to revert the last changes.
964         lyxerr << "Sorry, not implemented." << endl;
965 }
966
967
968 bool CVS::undoLastEnabled()
969 {
970         return false;
971 }
972
973
974 void CVS::getLog(FileName const & tmpf)
975 {
976         doVCCommandWithOutput("cvs log " + getTarget(File),
977                 FileName(owner_->filePath()),
978                 tmpf);
979 }
980
981
982 bool CVS::toggleReadOnlyEnabled()
983 {
984         return false;
985 }
986
987
988 string CVS::revisionInfo(LyXVC::RevisionInfo const info)
989 {
990         if (!version_.empty()) {
991                 getRevisionInfo();
992                 switch (info) {
993                 case LyXVC::File:
994                         return version_;
995                 case LyXVC::Author:
996                         return rev_author_cache_;
997                 case LyXVC::Date:
998                         return rev_date_cache_;
999                 case LyXVC::Time:
1000                         return rev_time_cache_;
1001                 default: ;
1002                 }
1003         }
1004         return string();
1005 }
1006
1007
1008 bool CVS::prepareFileRevision(string const & revis, string & f)
1009 {
1010         string rev = revis;
1011         if (!VCS::makeRCSRevision(version_, rev))
1012                 return false;
1013
1014         FileName tmpf = FileName::tempName("lyxvcrev_" + rev + "_");
1015         if (tmpf.empty()) {
1016                 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1017                 return false;
1018         }
1019
1020         doVCCommandWithOutput("cvs update -p -r" + rev + " "
1021                 + getTarget(File),
1022                 FileName(owner_->filePath()), tmpf);
1023         if (tmpf.isFileEmpty())
1024                 return false;
1025
1026         f = tmpf.absFileName();
1027         return true;
1028 }
1029
1030
1031 bool CVS::prepareFileRevisionEnabled()
1032 {
1033         return true;
1034 }
1035
1036
1037 /////////////////////////////////////////////////////////////////////
1038 //
1039 // SVN
1040 //
1041 /////////////////////////////////////////////////////////////////////
1042
1043 SVN::SVN(FileName const & m, FileName const & f)
1044 {
1045         owner_ = 0;
1046         master_ = m;
1047         file_ = f;
1048         locked_mode_ = 0;
1049         scanMaster();
1050 }
1051
1052
1053 FileName const SVN::findFile(FileName const & file)
1054 {
1055         // First we check the existence of repository meta data.
1056         if (!VCS::checkparentdirs(file, ".svn")) {
1057                 LYXERR(Debug::LYXVC, "Cannot find SVN meta data for " << file);
1058                 return FileName();
1059         }
1060
1061         // Now we check the status of the file.
1062         FileName tmpf = FileName::tempName("lyxvcout");
1063         if (tmpf.empty()) {
1064                 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1065                 return FileName();
1066         }
1067
1068         string const fname = onlyFileName(file.absFileName());
1069         LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under svn control for `" << fname << '\'');
1070         bool found = 0 == doVCCommandCall("svn info " + quoteName(fname)
1071                                                 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1072                                                 file.onlyPath());
1073         LYXERR(Debug::LYXVC, "SVN control: " << (found ? "enabled" : "disabled"));
1074         return found ? file : FileName();
1075 }
1076
1077
1078 void SVN::scanMaster()
1079 {
1080         // vcstatus code is somewhat superflous, until we want
1081         // to implement read-only toggle for svn.
1082         vcstatus = NOLOCKING;
1083         if (checkLockMode()) {
1084                 if (isLocked()) {
1085                         vcstatus = LOCKED;
1086                 } else {
1087                         vcstatus = UNLOCKED;
1088                 }
1089         }
1090 }
1091
1092
1093 bool SVN::checkLockMode()
1094 {
1095         FileName tmpf = FileName::tempName("lyxvcout");
1096         if (tmpf.empty()){
1097                 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1098                 return false;
1099         }
1100
1101         LYXERR(Debug::LYXVC, "Detecting locking mode...");
1102         if (doVCCommandCall("svn proplist " + quoteName(file_.onlyFileName())
1103                     + " > " + quoteName(tmpf.toFilesystemEncoding()),
1104                     file_.onlyPath()))
1105                 return false;
1106
1107         ifstream ifs(tmpf.toFilesystemEncoding().c_str());
1108         string line;
1109         bool ret = false;
1110
1111         while (ifs && !ret) {
1112                 getline(ifs, line);
1113                 LYXERR(Debug::LYXVC, line);
1114                 if (contains(line, "svn:needs-lock"))
1115                         ret = true;
1116         }
1117         LYXERR(Debug::LYXVC, "Locking enabled: " << ret);
1118         ifs.close();
1119         locked_mode_ = ret;
1120         return ret;
1121
1122 }
1123
1124
1125 bool SVN::isLocked() const
1126 {
1127         file_.refresh();
1128         return !file_.isReadOnly();
1129 }
1130
1131
1132 void SVN::registrer(string const & /*msg*/)
1133 {
1134         doVCCommand("svn add -q " + quoteName(onlyFileName(owner_->absFileName())),
1135                     FileName(owner_->filePath()));
1136 }
1137
1138
1139 string SVN::checkIn(string const & msg)
1140 {
1141         FileName tmpf = FileName::tempName("lyxvcout");
1142         if (tmpf.empty()){
1143                 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1144                 return N_("Error: Could not generate logfile.");
1145         }
1146
1147         doVCCommand("svn commit -m \"" + msg + "\" "
1148                     + quoteName(onlyFileName(owner_->absFileName()))
1149                     + " > " + quoteName(tmpf.toFilesystemEncoding()),
1150                     FileName(owner_->filePath()));
1151
1152         string log;
1153         string res = scanLogFile(tmpf, log);
1154         if (!res.empty())
1155                 frontend::Alert::error(_("Revision control error."),
1156                                 _("Error when committing to repository.\n"
1157                                 "You have to manually resolve the problem.\n"
1158                                 "LyX will reopen the document after you press OK."));
1159         else
1160                 fileLock(false, tmpf, log);
1161
1162         tmpf.erase();
1163         return log.empty() ? string() : "SVN: " + log;
1164 }
1165
1166
1167 bool SVN::checkInEnabled()
1168 {
1169         if (locked_mode_)
1170                 return isLocked();
1171         else
1172                 return true;
1173 }
1174
1175
1176 bool SVN::isCheckInWithConfirmation()
1177 {
1178         // FIXME one day common getDiff and perhaps OpMode for all backends
1179
1180         FileName tmpf = FileName::tempName("lyxvcout");
1181         if (tmpf.empty()) {
1182                 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1183                 return true;
1184         }
1185
1186         doVCCommandCall("svn diff " + quoteName(owner_->absFileName())
1187                     + " > " + quoteName(tmpf.toFilesystemEncoding()),
1188                 FileName(owner_->filePath()));
1189
1190         docstring diff = tmpf.fileContents("UTF-8");
1191         tmpf.erase();
1192
1193         if (diff.empty())
1194                 return false;
1195
1196         return true;
1197 }
1198
1199
1200 // FIXME Correctly return code should be checked instead of this.
1201 // This would need another solution than just plain startscript.
1202 // Hint from Andre': QProcess::readAllStandardError()...
1203 string SVN::scanLogFile(FileName const & f, string & status)
1204 {
1205         ifstream ifs(f.toFilesystemEncoding().c_str());
1206         string line;
1207
1208         while (ifs) {
1209                 getline(ifs, line);
1210                 LYXERR(Debug::LYXVC, line << "\n");
1211                 if (!line.empty()) 
1212                         status += line + "; ";
1213                 if (prefixIs(line, "C ") || prefixIs(line, "CU ")
1214                                          || contains(line, "Commit failed")) {
1215                         ifs.close();
1216                         return line;
1217                 }
1218                 if (contains(line, "svn:needs-lock")) {
1219                         ifs.close();
1220                         return line;
1221                 }
1222         }
1223         ifs.close();
1224         return string();
1225 }
1226
1227
1228 void SVN::fileLock(bool lock, FileName const & tmpf, string &status)
1229 {
1230         if (!locked_mode_ || (isLocked() == lock))
1231                 return;
1232
1233         string const arg = lock ? "lock " : "unlock ";
1234         doVCCommand("svn "+ arg + quoteName(onlyFileName(owner_->absFileName()))
1235                     + " > " + quoteName(tmpf.toFilesystemEncoding()),
1236                     FileName(owner_->filePath()));
1237
1238         // Lock error messages go unfortunately on stderr and are unreachible this way.
1239         ifstream ifs(tmpf.toFilesystemEncoding().c_str());
1240         string line;
1241         while (ifs) {
1242                 getline(ifs, line);
1243                 if (!line.empty()) status += line + "; ";
1244         }
1245         ifs.close();
1246
1247         if (!isLocked() && lock)
1248                 frontend::Alert::error(_("Revision control error."),
1249                         _("Error while acquiring write lock.\n"
1250                         "Another user is most probably editing\n"
1251                         "the current document now!\n"
1252                         "Also check the access to the repository."));
1253         if (isLocked() && !lock)
1254                 frontend::Alert::error(_("Revision control error."),
1255                         _("Error while releasing write lock.\n"
1256                         "Check the access to the repository."));
1257 }
1258
1259
1260 string SVN::checkOut()
1261 {
1262         FileName tmpf = FileName::tempName("lyxvcout");
1263         if (tmpf.empty()) {
1264                 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1265                 return N_("Error: Could not generate logfile.");
1266         }
1267
1268         doVCCommand("svn update --non-interactive " + quoteName(onlyFileName(owner_->absFileName()))
1269                     + " > " + quoteName(tmpf.toFilesystemEncoding()),
1270                     FileName(owner_->filePath()));
1271
1272         string log;
1273         string const res = scanLogFile(tmpf, log);
1274         if (!res.empty())
1275                 frontend::Alert::error(_("Revision control error."),
1276                         bformat(_("Error when updating from repository.\n"
1277                                 "You have to manually resolve the conflicts NOW!\n'%1$s'.\n\n"
1278                                 "After pressing OK, LyX will try to reopen the resolved document."),
1279                         from_local8bit(res)));
1280
1281         fileLock(true, tmpf, log);
1282
1283         tmpf.erase();
1284         return log.empty() ? string() : "SVN: " + log;
1285 }
1286
1287
1288 bool SVN::checkOutEnabled()
1289 {
1290         if (locked_mode_)
1291                 return !isLocked();
1292         else
1293                 return true;
1294 }
1295
1296
1297 string SVN::repoUpdate()
1298 {
1299         FileName tmpf = FileName::tempName("lyxvcout");
1300         if (tmpf.empty()) {
1301                 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1302                 return N_("Error: Could not generate logfile.");
1303         }
1304
1305         doVCCommand("svn diff " + quoteName(owner_->filePath())
1306                     + " > " + quoteName(tmpf.toFilesystemEncoding()),
1307                 FileName(owner_->filePath()));
1308         docstring res = tmpf.fileContents("UTF-8");
1309         if (!res.empty()) {
1310                 LYXERR(Debug::LYXVC, "Diff detected:\n" << res);
1311                 docstring const file = from_utf8(owner_->filePath());
1312                 docstring text = bformat(_("There were detected changes "
1313                                 "in the working directory:\n%1$s\n\n"
1314                                 "In case of file conflict version of the local directory files "
1315                                 "will be preferred."
1316                                 "\n\nContinue?"), file);
1317                 int ret = frontend::Alert::prompt(_("Changes detected"),
1318                                 text, 0, 1, _("&Yes"), _("&No"), _("View &Log ..."));
1319                 if (ret == 2 ) {
1320                         dispatch(FuncRequest(LFUN_DIALOG_SHOW, "file " + tmpf.absFileName()));
1321                         ret = frontend::Alert::prompt(_("Changes detected"),
1322                                 text, 0, 1, _("&Yes"), _("&No"));
1323                         hideDialogs("file", 0);
1324                 }
1325                 if (ret == 1 ) {
1326                         tmpf.erase();
1327                         return string();
1328                 }
1329         }
1330
1331         // Reverting looks too harsh, see bug #6255.
1332         // doVCCommand("svn revert -R " + quoteName(owner_->filePath())
1333         // + " > " + quoteName(tmpf.toFilesystemEncoding()),
1334         // FileName(owner_->filePath()));
1335         // res = "Revert log:\n" + tmpf.fileContents("UTF-8");
1336         doVCCommand("svn update --accept mine-full " + quoteName(owner_->filePath())
1337                 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1338                 FileName(owner_->filePath()));
1339         res += "Update log:\n" + tmpf.fileContents("UTF-8");
1340
1341         LYXERR(Debug::LYXVC, res);
1342         tmpf.erase();
1343         return to_utf8(res);
1344 }
1345
1346
1347 bool SVN::repoUpdateEnabled()
1348 {
1349         return true;
1350 }
1351
1352
1353 string SVN::lockingToggle()
1354 {
1355         FileName tmpf = FileName::tempName("lyxvcout");
1356         if (tmpf.empty()) {
1357                 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1358                 return N_("Error: Could not generate logfile.");
1359         }
1360
1361         int ret = doVCCommand("svn proplist " + quoteName(onlyFileName(owner_->absFileName()))
1362                     + " > " + quoteName(tmpf.toFilesystemEncoding()),
1363                     FileName(owner_->filePath()));
1364         if (ret)
1365                 return string();
1366
1367         string log;
1368         string res = scanLogFile(tmpf, log);
1369         bool locking = contains(res, "svn:needs-lock");
1370         if (!locking)
1371                 ret = doVCCommand("svn propset svn:needs-lock ON "
1372                     + quoteName(onlyFileName(owner_->absFileName()))
1373                     + " > " + quoteName(tmpf.toFilesystemEncoding()),
1374                     FileName(owner_->filePath()));
1375         else
1376                 ret = doVCCommand("svn propdel svn:needs-lock "
1377                     + quoteName(onlyFileName(owner_->absFileName()))
1378                     + " > " + quoteName(tmpf.toFilesystemEncoding()),
1379                     FileName(owner_->filePath()));
1380         if (ret)
1381                 return string();
1382
1383         tmpf.erase();
1384         frontend::Alert::warning(_("VCN File Locking"),
1385                 (locking ? _("Locking property unset.") : _("Locking property set.")) + "\n"
1386                 + _("Do not forget to commit the locking property into the repository."),
1387                 true);
1388
1389         return string("SVN: ") +  N_("Locking property set.");
1390 }
1391
1392
1393 bool SVN::lockingToggleEnabled()
1394 {
1395         return true;
1396 }
1397
1398
1399 bool SVN::revert()
1400 {
1401         // Reverts to the version in SVN repository and
1402         // gets the updated version from the repository.
1403         string const fil = quoteName(onlyFileName(owner_->absFileName()));
1404
1405         if (doVCCommand("svn revert -q " + fil,
1406                     FileName(owner_->filePath())))
1407                 return false;
1408         owner_->markClean();
1409         return true;
1410 }
1411
1412
1413 bool SVN::isRevertWithConfirmation()
1414 {
1415         //FIXME owner && diff
1416         return true;
1417 }
1418
1419
1420 void SVN::undoLast()
1421 {
1422         // merge the current with the previous version
1423         // in a reverse patch kind of way, so that the
1424         // result is to revert the last changes.
1425         lyxerr << "Sorry, not implemented." << endl;
1426 }
1427
1428
1429 bool SVN::undoLastEnabled()
1430 {
1431         return false;
1432 }
1433
1434
1435 string SVN::revisionInfo(LyXVC::RevisionInfo const info)
1436 {
1437         if (info == LyXVC::Tree) {
1438                         if (rev_tree_cache_.empty())
1439                                 if (!getTreeRevisionInfo())
1440                                         rev_tree_cache_ = "?";
1441                         if (rev_tree_cache_ == "?")
1442                                 return string();
1443
1444                         return rev_tree_cache_;
1445         }
1446
1447         // fill the rest of the attributes for a single file
1448         if (rev_file_cache_.empty())
1449                 if (!getFileRevisionInfo())
1450                         rev_file_cache_ = "?";
1451
1452         switch (info) {
1453                 case LyXVC::File:
1454                         if (rev_file_cache_ == "?")
1455                                 return string();
1456                         return rev_file_cache_;
1457                 case LyXVC::Author:
1458                         return rev_author_cache_;
1459                 case LyXVC::Date:
1460                         return rev_date_cache_;
1461                 case LyXVC::Time:
1462                         return rev_time_cache_;
1463                 default: ;
1464
1465         }
1466
1467         return string();
1468 }
1469
1470
1471 bool SVN::getFileRevisionInfo()
1472 {
1473         FileName tmpf = FileName::tempName("lyxvcout");
1474         if (tmpf.empty()) {
1475                 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1476                 return false;
1477         }
1478
1479         doVCCommand("svn info --xml " + quoteName(onlyFileName(owner_->absFileName()))
1480                     + " > " + quoteName(tmpf.toFilesystemEncoding()),
1481                     FileName(owner_->filePath()));
1482
1483         if (tmpf.empty())
1484                 return false;
1485
1486         ifstream ifs(tmpf.toFilesystemEncoding().c_str());
1487         string line;
1488         // commit log part
1489         bool c = false;
1490         string rev;
1491
1492         while (ifs) {
1493                 getline(ifs, line);
1494                 LYXERR(Debug::LYXVC, line);
1495                 if (prefixIs(line, "<commit"))
1496                         c = true;
1497                 if (c && prefixIs(line, "   revision=\"") && suffixIs(line, "\">")) {
1498                         string l1 = subst(line, "revision=\"", "");
1499                         string l2 = trim(subst(l1, "\">", ""));
1500                         if (isStrInt(l2))
1501                                 rev_file_cache_ = rev = l2;
1502                 }
1503                 if (c && prefixIs(line, "<author>") && suffixIs(line, "</author>")) {
1504                         string l1 = subst(line, "<author>", "");
1505                         string l2 = subst(l1, "</author>", "");
1506                         rev_author_cache_ = l2;
1507                 }
1508                 if (c && prefixIs(line, "<date>") && suffixIs(line, "</date>")) {
1509                         string l1 = subst(line, "<date>", "");
1510                         string l2 = subst(l1, "</date>", "");
1511                         l2 = split(l2, l1, 'T');
1512                         rev_date_cache_ = l1;
1513                         l2 = split(l2, l1, '.');
1514                         rev_time_cache_ = l1;
1515                 }
1516         }
1517
1518         ifs.close();
1519         tmpf.erase();
1520         return !rev.empty();
1521 }
1522
1523
1524 bool SVN::getTreeRevisionInfo()
1525 {
1526         FileName tmpf = FileName::tempName("lyxvcout");
1527         if (tmpf.empty()) {
1528                 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1529                 return false;
1530         }
1531
1532         doVCCommand("svnversion -n . > " + quoteName(tmpf.toFilesystemEncoding()),
1533                     FileName(owner_->filePath()));
1534
1535         if (tmpf.empty())
1536                 return false;
1537
1538         // only first line in case something bad happens.
1539         ifstream ifs(tmpf.toFilesystemEncoding().c_str());
1540         string line;
1541         getline(ifs, line);
1542         ifs.close();
1543         tmpf.erase();
1544
1545         rev_tree_cache_ = line;
1546         return !line.empty();
1547 }
1548
1549
1550 void SVN::getLog(FileName const & tmpf)
1551 {
1552         doVCCommand("svn log " + quoteName(onlyFileName(owner_->absFileName()))
1553                     + " > " + quoteName(tmpf.toFilesystemEncoding()),
1554                     FileName(owner_->filePath()));
1555 }
1556
1557
1558 bool SVN::prepareFileRevision(string const & revis, string & f)
1559 {
1560         if (!isStrInt(revis))
1561                 return false;
1562
1563         int rev = convert<int>(revis);
1564         if (rev <= 0)
1565                 if (!getFileRevisionInfo())
1566                         return false;
1567         if (rev == 0)
1568                 rev = convert<int>(rev_file_cache_);
1569         // go back for minus rev
1570         else if (rev < 0) {
1571                 rev = rev + convert<int>(rev_file_cache_);
1572                 if (rev < 1)
1573                         return false;
1574         }
1575
1576         string revname = convert<string>(rev);
1577         FileName tmpf = FileName::tempName("lyxvcrev_" + revname + "_");
1578         if (tmpf.empty()) {
1579                 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1580                 return false;
1581         }
1582
1583         doVCCommand("svn cat -r " + revname + " "
1584                       + quoteName(onlyFileName(owner_->absFileName()))
1585                       + " > " + quoteName(tmpf.toFilesystemEncoding()),
1586                 FileName(owner_->filePath()));
1587         if (tmpf.isFileEmpty())
1588                 return false;
1589
1590         f = tmpf.absFileName();
1591         return true;
1592 }
1593
1594
1595 bool SVN::prepareFileRevisionEnabled()
1596 {
1597         return true;
1598 }
1599
1600
1601
1602 bool SVN::toggleReadOnlyEnabled()
1603 {
1604         return false;
1605 }
1606
1607
1608 } // namespace lyx