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