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