]> git.lyx.org Git - lyx.git/blob - src/Mover.h
Fixed some lines that were too long. It compiled afterwards.
[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, since @c to and @c latex
35          *  would be equal in this case.
36          *  \returns true if successful.
37          */
38         bool
39         copy(support::FileName const & from, support::FileName const & to,
40              unsigned long int mode = (unsigned long int)-1) const;
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(support::FileName const & from, support::FileName const & to,
52              std::string const & latex,
53              unsigned long int mode = (unsigned long int)-1) const
54         {
55                 return do_copy(from, to, latex, mode);
56         }
57
58         /** Rename file @c from as @c to.
59          *  This version should be used to move files from the original
60          *  location to the temporary directory, since @c to and @c latex
61          *  would be equal in this case.
62          *  \returns true if successful.
63          */
64         bool
65         rename(support::FileName const & from, support::FileName const & to) const;
66
67         /** Rename file @c from as @c to.
68          *  \see SpecialisedMover::SpecialisedMover() for an explanation of
69          *  @c latex.
70          *  This version should be used to move files from the temporary
71          *  directory to the export location, since @c to and @c latex may
72          *  not be equal in this case.
73          *  \returns true if successful.
74          */
75         bool
76         rename(support::FileName const & from, support::FileName const & to,
77                std::string const & latex) const
78         {
79                 return do_rename(from, to, latex);
80         }
81
82 protected:
83         virtual bool
84         do_copy(support::FileName const & from, support::FileName const & to,
85                 std::string const &, unsigned long int mode) const;
86
87         virtual bool
88         do_rename(support::FileName const & from, support::FileName const & to,
89                   std::string const &) const;
90 };
91
92
93 /**
94  *  Specialisation of the Mover concept that uses an external command
95  *  to copy a file.
96  *
97  *  For example, an Xfig .fig file can contain references to external
98  *  picture files. If such a reference has a relative path, then the
99  *  copied .fig file will require a transformation of the picture file
100  *  reference if it is to be found by Xfig.
101  */
102 class SpecialisedMover : public Mover
103 {
104 public:
105         SpecialisedMover() {}
106
107         virtual ~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(support::FileName const & from, support::FileName const & to,
134                 std::string const & latex, unsigned long int mode) const;
135
136         virtual bool
137         do_rename(support::FileName const & from, support::FileName 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 const_iterator;
168         const_iterator begin() const { return specials_.begin(); }
169         const_iterator end() const { return specials_.end(); }
170
171 private:
172         Mover default_;
173         SpecialsMap specials_;
174 };
175
176
177 extern Movers & theMovers();
178 /// @c returns the Mover registered for format @c fmt.
179 extern Mover const & getMover(std::string  const & fmt);
180 /** Register a specialised @c command to be used to copy a file
181  *  of format @c fmt.
182  */
183 extern void setMover(std::string const & fmt, std::string const & command);
184 extern Movers & theSystemMovers();
185
186
187 } // namespace lyx
188
189 #endif // MOVER_H