]> git.lyx.org Git - lyx.git/blob - src/Mover.h
Avoid full metrics computation with Update:FitCursor
[lyx.git] / src / Mover.h
1 // -*- C++ -*-
2 /**
3  * \file Mover.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Angus Leeming
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #ifndef MOVER_H
13 #define MOVER_H
14
15 #include <map>
16 #include <string>
17
18 #include "support/mute_warning.h"
19
20 namespace lyx {
21
22 namespace support { class FileName; }
23
24 /**
25  *  Utility to copy a file of a specified format from one place to another.
26  *  This base class simply invokes the command support::copy().
27  */
28 class Mover
29 {
30 public:
31         virtual ~Mover() {}
32         Mover(Mover &&) = default;
33         Mover & operator=(Mover &&) = default;
34         Mover(Mover const &) = default;
35         Mover & operator=(Mover const &) = default;
36
37         Mover() = default;
38
39         /** Copy file @c from to @c to.
40          *  This version should be used to copy files from the original
41          *  location to the temporary directory.
42          *  \returns true if successful.
43          */
44         bool
45         copy(support::FileName const & from, support::FileName const & to) const;
46
47         /** Copy file @c from to @c to.
48          *  \see SpecialisedMover::SpecialisedMover() for an explanation of
49          *  @c latex.
50          *  This version should be used to copy files from the temporary
51          *  directory to the export location, since @c to and @c latex may
52          *  not be equal in this case.
53          *  \returns true if successful.
54          *  NOTE: Although this routine simply calls do_copy() and
55          *  Mover::do_copy() does not itself make any use of the @c latex argument,
56          *  SpecialisedMover overrides do_copy(), so SpecialisedMover::copy(), which
57          *  is just Mover::copy(), calls SpecialisedMover::do_copy(), and the @c latex
58          *  argument IS in that case used.
59          */
60         bool
61         copy(support::FileName const & from, support::FileName const & to,
62              std::string const & latex) const
63         {
64                 return do_copy(from, to, latex);
65         }
66
67         /** Rename file @c from as @c to.
68          *  This version should be used to move files from the original
69          *  location to the temporary directory.
70          *  \returns true if successful.
71          */
72         bool
73         rename(support::FileName const & from, support::FileName const & to) const;
74
75         /** Rename file @c from as @c to.
76          *  \see SpecialisedMover::SpecialisedMover() for an explanation of
77          *  @c latex.
78          *  This version should be used to move files from the temporary
79          *  directory to the export location, since @c to and @c latex may
80          *  not be equal in this case.
81          *  \returns true if successful.
82          */
83         bool
84         rename(support::FileName const & from, support::FileName const & to,
85                std::string const & latex) const
86         {
87                 return do_rename(from, to, latex);
88         }
89
90 protected:
91         virtual bool
92         do_copy(support::FileName const & from, support::FileName const & to,
93                 std::string const &) const;
94
95         virtual bool
96         do_rename(support::FileName const & from, support::FileName const & to,
97                   std::string const &) const;
98 };
99
100
101 /**
102  *  Specialisation of the Mover concept that uses an external command
103  *  to copy a file.
104  *
105  *  For example, an Xfig .fig file can contain references to external
106  *  picture files. If such a reference has a relative path, then the
107  *  copied .fig file will require a transformation of the picture file
108  *  reference if it is to be found by Xfig.
109  *
110  *  So, in this case, we need three arguments:
111  *  (i)   @c from  the location of the file to be moved
112  *  (ii)  @c to    the location to which it should be moved
113  *  (iii) @c latex the identifier that should be used in the sort of
114  *                 transformation just mentioned.
115  */
116 class SpecialisedMover : public Mover
117 {
118 public:
119         SpecialisedMover() = default;
120
121         /** @c command should be of the form
122          *  <code>
123          *      python $$s/scripts/fig_copy.py $$i $$o $$l
124          *  </code>
125          *  where $$s is a placeholder for the lyx support directory,
126          *        $$i is a placeholder for the name of the file to be moved,
127          *        $$o is a placeholder for the name of the file after moving,
128          *        $$l is a placeholder for the latex argument, as explained above.
129          *  $$o and $$l can only differ if the file is copied from the temporary
130          *  directory to the export location. If it is copied from the original
131          *  location to the temporary directory, they are the same, so $$l may be
132          *  ignored in this case, as it is in the Mover baseclass.
133          */
134         SpecialisedMover(std::string const & command)
135                 : command_(command) {}
136
137         /// The template used to launch the external command.
138         std::string const & command() const { return command_; }
139
140 private:
141         bool do_copy(support::FileName const & from, support::FileName const & to,
142                 std::string const & latex) const override;
143
144         bool do_rename(support::FileName const & from, support::FileName const & to,
145                   std::string const & latex) const override;
146
147         std::string command_;
148 };
149
150
151 /**
152  *  Manage the store of (Mover)s.
153  */
154 class Movers
155 {
156 public:
157         /** Register a specialised @c command to be used to copy a file
158          *  of format @c fmt.
159          */
160         void set(std::string const & fmt, std::string const & command);
161
162         /// @c returns the Mover registered for format @c fmt.
163         Mover const & operator()(std::string  const & fmt) const;
164
165         /** @returns the command template if @c fmt 'finds' a
166          *  SpecialisedMover. Otherwise, returns an empty string.
167          */
168         std::string const command(std::string  const & fmt) const;
169
170 private:
171         typedef std::map<std::string, SpecialisedMover> SpecialsMap;
172
173 public:
174         typedef SpecialsMap::const_iterator const_iterator;
175         const_iterator begin() const { return specials_.begin(); }
176         const_iterator end() const { return specials_.end(); }
177
178 private:
179         Mover default_;
180         SpecialsMap specials_;
181 };
182
183
184 extern Movers & theMovers();
185 LYX_BEGIN_MUTE_GCC_WARNING(dangling-reference)
186 /// @c returns the Mover registered for format @c fmt.
187 extern Mover const & getMover(std::string  const & fmt);
188 LYX_END_MUTE_GCC_WARNING
189 /** Register a specialised @c command to be used to copy a file
190  *  of format @c fmt.
191  */
192 extern void setMover(std::string const & fmt, std::string const & command);
193 extern Movers & theSystemMovers();
194
195
196 } // namespace lyx
197
198 #endif // MOVER_H