]> git.lyx.org Git - lyx.git/blob - src/frontends/Liason.C
Export patch from Dekel
[lyx.git] / src / frontends / Liason.C
1 /* This file is part of
2  * ====================================================== 
3  * 
4  *           LyX, The Document Processor
5  *        
6  *           Copyright 1995 Matthias Ettrich
7  *           Copyright 1995-2000 The LyX Team.
8  *
9  * ====================================================== */
10
11 #include <config.h>
12
13 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16
17 #include "Liason.h"
18 #include "LyXView.h"
19 #include "BufferView.h"
20 #include "buffer.h"
21 #include "lyxrc.h"
22 #include "PrinterParams.h"
23 #include "lyx_gui_misc.h"
24 #include "support/lstrings.h"
25 #include "support/filetools.h"
26 #include "support/path.h"
27 #include "exporter.h"
28 #include "converter.h"
29 #include "minibuffer.h"
30 #include "support/syscall.h"
31
32 using std::endl;
33
34 extern LyXRC lyxrc;
35 #ifndef NEW_EXPORT
36 extern bool RunScript(Buffer * buffer, bool wait, string const & command,
37                       string const & orgname = string(), bool need_shell=true);
38 #endif
39
40 #ifdef CXX_WORKING_NAMESPACES
41 namespace Liason 
42 {
43 #endif
44
45 PrinterParams getPrinterParams(Buffer * buffer)
46 {
47         return PrinterParams(PrinterParams::PRINTER,
48                              lyxrc.printer,
49                              ChangeExtension(buffer->fileName(),
50                                              lyxrc.print_file_extension));
51 }
52
53
54 bool printBuffer(Buffer * buffer, PrinterParams const & pp) 
55 {
56 #ifndef NEW_EXPORT
57         bool result(false);
58 #endif
59         string command(lyxrc.print_command + ' ');
60         
61         if (pp.target == PrinterParams::PRINTER
62             && lyxrc.print_adapt_output  // dvips wants a printer name
63             && !pp.printer_name.empty()) {// printer name given
64                 command += lyxrc.print_to_printer
65                         + pp.printer_name
66                         + ' ';
67         }
68
69         switch (pp.which_pages) {
70         case PrinterParams::EVEN:
71                 command += lyxrc.print_evenpage_flag + ' ';
72                 break;
73
74         case PrinterParams::ODD:
75                 command += lyxrc.print_oddpage_flag + ' ';
76                 break;
77     
78         default:
79                 // only option left is print all of them
80                 break;
81         }
82
83         if (!pp.from_page.empty()) {
84                 command += lyxrc.print_pagerange_flag + ' ';
85                 command += pp.from_page;
86                 if (pp.to_page) {
87                                 // we have a range "from-to"
88                         command += '-';
89                         command += tostr(pp.to_page);
90                 }
91                 command += ' ';
92         }
93
94         if (pp.reverse_order) {
95                 command += lyxrc.print_reverse_flag + ' ';
96         }
97
98         if (1 < pp.count_copies) {
99                 if (pp.unsorted_copies) {
100                         command += lyxrc.print_copies_flag;
101                 } else {
102                         command += lyxrc.print_collcopies_flag;
103                 }
104                 command += ' ';
105                 command += tostr(pp.count_copies);
106                 command += ' ';
107         }
108
109         if (!lyxrc.print_extra_options.empty()) {
110                 command += lyxrc.print_extra_options + ' ';
111         }
112
113         command += Converter::dvips_options(buffer) + ' ';
114
115 #ifdef NEW_EXPORT
116         if (!Exporter::Export(buffer, "dvi", true))
117                 return false;
118
119         // Push directory path.
120         string path = OnlyPath(buffer->fileName());
121         if (lyxrc.use_tempdir || (IsDirWriteable(path) < 1)) {
122                 path = buffer->tmppath;
123         }
124         Path p(path);
125
126         // there are three cases here:
127         // 1. we print to a file
128         // 2. we print direct to a printer
129         // 3. we print using a spool command (print to file first)
130         Systemcalls one;
131         int res = 0;
132         string dviname = ChangeExtension(buffer->getLatexName(true), "dvi");
133         switch (pp.target) {
134         case PrinterParams::PRINTER:
135                 if (!lyxrc.print_spool_command.empty()) {
136                         // case 3
137                         string psname = ChangeExtension(dviname, ".ps");
138                         command += lyxrc.print_to_file
139                                 + QuoteName(psname) + ' ';
140                         command += QuoteName(dviname);
141                         string command2 = lyxrc.print_spool_command + ' ';
142                         if (!pp.printer_name.empty())
143                                 command2 += lyxrc.print_spool_printerprefix
144                                         + pp.printer_name + ' ';
145                         command2 += QuoteName(psname);
146                         // First run dvips.
147                         // If successful, then spool command
148                         res = one.startscript(Systemcalls::System, command);
149                         if (res == 0)
150                                 res = one.startscript(Systemcalls::SystemDontWait,
151                                                       command2);
152                 } else
153                         // case 2
154                         res = one.startscript(Systemcalls::SystemDontWait, command);
155                 break;
156
157         case PrinterParams::FILE:
158                 // case 1
159                 command += lyxrc.print_to_file
160                         + QuoteName(MakeAbsPath(pp.file_name, path));
161                 command += ' ' + QuoteName(dviname);
162                 res = one.startscript(Systemcalls::SystemDontWait, command);
163                 break;
164         }
165         return res == 0;
166 #else
167         // Push directory path if necessary.
168         // PS file should go where the source file is unless it's a
169         // read-only directory in which case we write it to tmpdir.
170         // All temporary files go in tmpdir (like spool files).
171         string path = OnlyPath(buffer->fileName());
172         if ((pp.target != PrinterParams::FILE
173              && lyxrc.use_tempdir)
174             || (IsDirWriteable(path) < 1)) {
175                 path = buffer->tmppath;
176         }
177         Path p(path);
178
179         // there are three cases here:
180         // 1. we print to a file
181         // 2. we print direct to a printer
182         // 3. we print using a spool command (print to file first)
183         switch (pp.target) {
184         case PrinterParams::PRINTER:
185                 if (!lyxrc.print_spool_command.empty()) {
186                                 // case 3
187                         command += lyxrc.print_to_file
188                                 + QuoteName(pp.file_name);
189                         string command2 = lyxrc.print_spool_command
190                                 + ' ';
191                         if (!pp.printer_name.empty()) {
192                                 command2 += lyxrc.print_spool_printerprefix
193                                         + pp.printer_name;
194                         }
195                                 // First run dvips.
196                                 // If successful, then spool command
197                         if (result = RunScript(buffer, true, command)) {
198                                 result = RunScript(buffer, false,
199                                                    command2,
200                                                    QuoteName(pp.file_name));
201                         }
202                 } else {
203                                 // case 2
204                         result = RunScript(buffer, false, command);
205                 }
206                 break;
207
208         case PrinterParams::FILE:
209                 // case 1
210                 command += lyxrc.print_to_file
211                         + QuoteName(MakeAbsPath(pp.file_name, path));
212                 result = RunScript(buffer, false, command);
213                 break;
214         }
215         return result;
216 #endif
217 }
218
219 void setMinibuffer(LyXView * lv, char const * msg)
220 {
221         lv->getMiniBuffer()->Set(msg);
222 }
223
224 #ifdef CXX_WORKING_NAMESPACES
225 }
226 #endif
227