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