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