]> git.lyx.org Git - lyx.git/blob - src/mover.h
The Movers patch.
[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          *  \returns true if successful.
29          */
30         bool
31         copy(std::string const & from, std::string const & to) const
32         {
33                 return do_copy(from, to);
34         }
35
36         /** Rename file @c from as @c to.
37          *  \returns true if successful.
38          */
39         bool
40         rename(std::string const & from, std::string const & to) const
41         {
42                 return do_rename(from, to);
43         }
44
45 protected:
46         virtual bool
47         do_copy(std::string const & from, std::string const & to) const;
48
49         virtual bool
50         do_rename(std::string const & from, std::string const & to) const;
51 };
52
53
54 /**
55  *  Specialisation of the Mover concept that uses an external command
56  *  to copy a file.
57  *
58  *  For example, an XFig .fig file can contain references to external
59  *  picture files. If such a reference has a relative path, then the
60  *  copied .fig file will require a transformation of the picture file
61  *  reference if it is to be found by XFig.
62  */
63 struct SpecialisedMover : public Mover
64 {
65         SpecialisedMover() {}
66
67         /** @c command should be of the form
68          *  <code>
69          *      sh $$s/copy_fig.sh $$i $$o
70          *  </code>
71          *  where $$s is a placeholder for the lyx script directory,
72          *        $$i is a placeholder for the name of the file to be moved,
73          *        $$o is a placeholder for the name of the file after moving.
74          */
75         SpecialisedMover(std::string const & command)
76                 : command_(command) {}
77
78         /// The template used to launch the external command.
79         std::string const & command() const { return command_; }
80
81 private:
82         virtual bool
83         do_copy(std::string const & from, std::string const & to) const;
84
85         virtual bool
86         do_rename(std::string const & from, std::string const & to) const;
87
88         std::string command_;
89 };
90
91
92 /**
93  *  Manage the store of (Mover)s.
94  */
95 class Movers
96 {
97 public:
98         /** Register a specialised @c command to be used to copy a file
99          *  of format @c fmt.
100          */
101         void set(std::string const & fmt, std::string const & command);
102
103         /// @c returns the Mover registered for format @c fmt.
104         Mover const & operator()(std::string  const & fmt) const;
105
106         /** @returns the command template if @c fmt 'finds' a
107          *  SpecialisedMover. Otherwise, returns an empty string.
108          */
109         std::string const command(std::string  const & fmt) const;
110
111 private:
112         typedef std::map<std::string, SpecialisedMover> SpecialsMap;
113
114 public:
115         typedef SpecialsMap::const_iterator iterator;
116         iterator begin() const { return specials_.begin(); }
117         iterator end() const { return specials_.end(); }
118
119 private:
120         Mover default_;
121         SpecialsMap specials_;
122 };
123
124
125 extern Movers movers;
126 extern Movers system_movers;
127
128 #endif // MOVER_H