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