]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ControlSendto.C
Angus Custom Export patch and removal of unused files from repository.
[lyx.git] / src / frontends / controllers / ControlSendto.C
1 /**
2  * \file ControlSendto.C
3  * Copyright 2002 The LyX Team.
4  * See the file COPYING.
5  *
6  * \author Angus Leeming, a.leeming@.ac.uk
7  */
8
9 #include <config.h>
10 #include <fstream>
11
12 #ifdef __GNUG__
13 #pragma implementation
14 #endif
15
16 #include "ViewBase.h"
17 #include "ButtonControllerBase.h"
18 #include "ControlSendto.h"
19 #include "Dialogs.h"
20 #include "LyXView.h"
21 #include "BufferView.h"
22 #include "buffer.h"
23 #include "converter.h"
24 #include "exporter.h"
25 #include "gettext.h"
26 #include "lyxrc.h"
27 #include "support/filetools.h"
28 #include "support/lstrings.h"
29 #include "support/systemcall.h"
30
31 using std::vector;
32
33
34 ControlSendto::ControlSendto(LyXView & lv, Dialogs & d)
35         : ControlDialogBD(lv, d),
36           format_(0),
37           command_(lyxrc.custom_export_command)
38 {
39         d_.showSendto.connect(SigC::slot(this, &ControlSendto::show));
40 }
41
42
43 vector<Format const *> const ControlSendto::allFormats() const
44 {
45         // Well, we can start with "lyx"
46         vector<Format const *> lyx_formats;
47         lyx_formats.push_back(formats.getFormat("lyx"));
48
49         // lyx can export to latex, what formats can be reached from latex?
50         Formats::const_iterator it  = formats.begin();
51         Formats::const_iterator end = formats.end();
52         vector<Format const *>::const_iterator lbegin = lyx_formats.begin();
53         for (; it != end; ++it) {
54                 if (converters.isReachable("latex", it->name())) {
55                         vector<Format const *>::const_iterator lend =
56                                 lyx_formats.end();
57                         
58                         if (std::find(lbegin, lend, it) == lend)
59                                 lyx_formats.push_back(it);
60                 }
61         }
62
63         return lyx_formats;
64 }
65
66
67 void ControlSendto::setFormat(Format const * fmt)
68 {
69         format_ = fmt;
70 }
71
72
73 void ControlSendto::setCommand(string const & cmd)
74 {
75         command_ = strip(frontStrip(cmd));
76 }
77
78
79 void ControlSendto::apply()
80 {
81         if (!lv_.view()->available())
82                 return;
83    
84         view().apply();
85
86         if (command_.empty() || !format_)
87                 return;
88
89         // The name of the file created by the conversion process
90         string filename = lv_.buffer()->getLatexName(false);
91         if (!lv_.buffer()->tmppath.empty())
92                 filename = AddName(lv_.buffer()->tmppath, filename);
93         filename = ChangeExtension(filename, format_->extension());
94
95         // Output to filename
96         if (format_->name() == "lyx") {
97                 lv_.buffer()->writeFile(filename, true);
98
99         } else {
100                 Exporter::Export(lv_.buffer(), format_->name(), true,
101                                  filename);
102         }
103
104         // Substitute $$FName for filename
105         string command = command_;
106         if (!contains(command, "$$FName"))
107                 command = "( " + command + " ) < $$FName";
108         command = subst(command, "$$FName", filename);
109
110         // Execute the command in the background
111         Systemcall call;
112         call.startscript(Systemcall::DontWait, command);
113 }
114