]> git.lyx.org Git - lyx.git/blob - src/Mover.cpp
b989cf118e147265dac58311bd7e5df608608eea
[lyx.git] / src / Mover.cpp
1 /**
2  * \file Mover.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Angus Leeming
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "Mover.h"
14
15 #include "support/filetools.h"
16 #include "support/lstrings.h"
17 #include "support/lyxlib.h"
18 #include "support/Systemcall.h"
19
20 #include <fstream>
21 #include <sstream>
22
23 using namespace std;
24
25 namespace lyx {
26
27 using support::quoteName;
28
29 bool Mover::copy(support::FileName const & from, support::FileName const & to,
30                  unsigned long int mode) const
31 {
32         return do_copy(from, to, to.absFilename(), mode);
33 }
34
35
36 bool Mover::do_copy(support::FileName const & from, support::FileName const & to,
37                     string const &, unsigned long int mode) const
38 {
39         return support::copy(from, to, mode);
40 }
41
42
43 bool Mover::rename(support::FileName const & from,
44                    support::FileName const & to) const
45 {
46         return do_rename(from, to, to.absFilename());
47 }
48
49
50 bool Mover::do_rename(support::FileName const & from, support::FileName const & to,
51                       string const &) const
52 {
53         return support::rename(from, to);
54 }
55
56
57 bool SpecialisedMover::do_copy(support::FileName const & from, support::FileName const & to,
58                                string const & latex, unsigned long int mode) const
59 {
60         if (command_.empty())
61                 return Mover::do_copy(from, to, latex, mode);
62
63         if (mode != (unsigned long int)-1) {
64                 std::ofstream ofs(to.toFilesystemEncoding().c_str(), ios::binary | ios::out | ios::trunc);
65                 if (!ofs)
66                         return false;
67                 ofs.close();
68                 if (!support::chmod(to, mode))
69                         return false;
70         }
71
72         string command = support::libScriptSearch(command_);
73         command = support::subst(command, "$$i", quoteName(from.toFilesystemEncoding()));
74         command = support::subst(command, "$$o", quoteName(to.toFilesystemEncoding()));
75         command = support::subst(command, "$$l", quoteName(latex));
76
77         support::Systemcall one;
78         return one.startscript(support::Systemcall::Wait, command) == 0;
79 }
80
81
82 bool SpecialisedMover::do_rename(support::FileName const & from, support::FileName const & to,
83                                  string const & latex) const
84 {
85         if (command_.empty())
86                 return Mover::do_rename(from, to, latex);
87
88         if (!do_copy(from, to, latex, (unsigned long int)-1))
89                 return false;
90         return from.removeFile();
91 }
92
93
94 void Movers::set(string const & fmt, string const & command)
95 {
96         specials_[fmt] = SpecialisedMover(command);
97 }
98
99
100 Mover const & Movers::operator()(string const & fmt) const
101 {
102         SpecialsMap::const_iterator const it = specials_.find(fmt);
103         if (it == specials_.end())
104                 return default_;
105         return  it->second;
106 }
107
108
109 string const Movers::command(string  const & fmt) const
110 {
111         SpecialsMap::const_iterator const it = specials_.find(fmt);
112         return (it == specials_.end()) ? string() : it->second.command();
113 }
114
115
116 } // namespace lyx