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