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