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