]> git.lyx.org Git - lyx.git/blob - src/mover.C
Fix inline collapsable inset set focus crash
[lyx.git] / src / mover.C
1 /**
2  * \file mover.C
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
24 namespace lyx {
25
26 using std::ios;
27 using std::string;
28
29 Movers movers;
30 Movers system_movers;
31
32
33 bool Mover::copy(support::FileName const & from, support::FileName const & to,
34                  unsigned long int mode) const
35 {
36         return do_copy(from, to, to.absFilename(), mode);
37 }
38
39
40 bool Mover::do_copy(support::FileName const & from, support::FileName const & to,
41                     string const &, unsigned long int mode) const
42 {
43         return support::copy(from, to, mode);
44 }
45
46
47 bool Mover::rename(support::FileName const & from,
48                    support::FileName const & to) const
49 {
50         return do_rename(from, to, to.absFilename());
51 }
52
53
54 bool Mover::do_rename(support::FileName const & from, support::FileName const & to,
55                       string const &) const
56 {
57         return support::rename(from, to);
58 }
59
60
61 bool SpecialisedMover::do_copy(support::FileName const & from, support::FileName const & to,
62                                string const & latex, unsigned long int mode) const
63 {
64         if (command_.empty())
65                 return Mover::do_copy(from, to, latex, mode);
66
67         if (mode != (unsigned long int)-1) {
68                 std::ofstream ofs(to.toFilesystemEncoding().c_str(), ios::binary | ios::out | ios::trunc);
69                 if (!ofs)
70                         return false;
71                 ofs.close();
72                 if (!support::chmod(to, mode))
73                         return false;
74         }
75
76         string command = support::libScriptSearch(command_);
77         command = support::subst(command, "$$i", from.toFilesystemEncoding());
78         command = support::subst(command, "$$o", to.toFilesystemEncoding());
79         command = support::subst(command, "$$l", latex);
80
81         support::Systemcall one;
82         return one.startscript(support::Systemcall::Wait, command) == 0;
83 }
84
85
86 bool SpecialisedMover::do_rename(support::FileName const & from, support::FileName const & to,
87                                  string const & latex) const
88 {
89         if (command_.empty())
90                 return Mover::do_rename(from, to, latex);
91
92         if (!do_copy(from, to, latex, (unsigned long int)-1))
93                 return false;
94         return support::unlink(from) == 0;
95 }
96
97
98 void Movers::set(string const & fmt, string const & command)
99 {
100         specials_[fmt] = SpecialisedMover(command);
101 }
102
103
104 Mover const & Movers::operator()(string const & fmt) const
105 {
106         SpecialsMap::const_iterator const it = specials_.find(fmt);
107         return (it == specials_.end()) ? default_ : it->second;
108 }
109
110
111 string const Movers::command(string  const & fmt) const
112 {
113         SpecialsMap::const_iterator const it = specials_.find(fmt);
114         return (it == specials_.end()) ? string() : it->second.command();
115 }
116
117
118 } // namespace lyx