]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ControlPrint.C
Georg Baum\'s no-tempdir patch
[lyx.git] / src / frontends / controllers / ControlPrint.C
1 /**
2  * \file ControlPrint.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Angus Leeming
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "ControlPrint.h"
14
15 #include "ViewBase.h"
16 #include "ButtonController.h"
17
18 #include "buffer.h"
19 #include "bufferparams.h"
20 #include "gettext.h"
21 #include "helper_funcs.h"
22 #include "PrinterParams.h"
23 #include "exporter.h"
24
25 #include "frontends/Alert.h"
26
27 #include "support/tostr.h"
28 #include "support/filetools.h"
29 #include "support/globbing.h"
30 #include "support/path.h"
31 #include "support/systemcall.h"
32
33 #include "debug.h"
34
35 using lyx::support::bformat;
36 using lyx::support::ChangeExtension;
37 using lyx::support::FileFilterList;
38 using lyx::support::IsDirWriteable;
39 using lyx::support::MakeAbsPath;
40 using lyx::support::MakeDisplayPath;
41 using lyx::support::Path;
42 using lyx::support::QuoteName;
43 using lyx::support::Systemcall;
44
45 using std::endl;
46 using std::string;
47
48
49 ControlPrint::ControlPrint(LyXView & lv, Dialogs & d)
50         : ControlDialogBD(lv, d),
51           params_(0)
52 {}
53
54
55 PrinterParams & ControlPrint::params() const
56 {
57         BOOST_ASSERT(params_);
58         return *params_;
59 }
60
61
62 void ControlPrint::setParams()
63 {
64         if (params_) delete params_;
65
66         /// get global printer parameters
67         string const name =  ChangeExtension(buffer()->fileName(),
68                                         lyxrc.print_file_extension);
69         params_ = new PrinterParams (PrinterParams::PRINTER,
70                                         lyxrc.printer, name);
71
72         bc().valid(); // so that the user can press Ok
73 }
74
75
76 void ControlPrint::clearParams()
77 {
78         if (params_) {
79                 delete params_;
80                 params_ = 0;
81         }
82 }
83
84
85 string const ControlPrint::browse(string const & in_name) const
86 {
87         return browseRelFile(in_name, buffer().filePath(),
88                              _("Print to file"),
89                              FileFilterList("PostScript files (*.ps)"),
90                              true);
91 }
92
93
94 namespace {
95
96 void showPrintError(string const & name)
97 {
98                 string str = bformat(_("Could not print the document %1$s.\n"
99                         "Check that your printer is set up correctly."),
100                         MakeDisplayPath(name, 50));
101                 Alert::error(_("Print document failed"), str);
102 }
103
104 }
105
106
107 /// print the current buffer
108 void ControlPrint::apply()
109 {
110         if (!bufferIsAvailable())
111                 return;
112
113         view().apply();
114
115         PrinterParams const pp = params();
116         string command(lyxrc.print_command + ' ');
117
118         if (pp.target == PrinterParams::PRINTER
119             && lyxrc.print_adapt_output  // dvips wants a printer name
120             && !pp.printer_name.empty()) {// printer name given
121                 command += lyxrc.print_to_printer
122                         + pp.printer_name
123                         + ' ';
124         }
125
126         if (!pp.all_pages && pp.from_page) {
127                 command += lyxrc.print_pagerange_flag + ' ';
128                 command += tostr(pp.from_page);
129                 if (pp.to_page) {
130                         // we have a range "from-to"
131                         command += '-'
132                                 + tostr(pp.to_page);
133                 }
134                 command += ' ';
135         }
136
137         // If both are, or both are not selected, then skip the odd/even printing
138         if (pp.odd_pages != pp.even_pages) {
139                 if (pp.odd_pages) {
140                         command += lyxrc.print_oddpage_flag + ' ';
141                 } else if (pp.even_pages) {
142                         command += lyxrc.print_evenpage_flag + ' ';
143                 }
144         }
145
146         if (pp.count_copies > 1) {
147                 if (pp.sorted_copies) {
148                         command += lyxrc.print_collcopies_flag;
149                 } else {
150                         command += lyxrc.print_copies_flag;
151                 }
152                 command += ' '
153                         + tostr(pp.count_copies)
154                         + ' ';
155         }
156
157         if (pp.reverse_order) {
158                 command += lyxrc.print_reverse_flag + ' ';
159         }
160
161         if (!lyxrc.print_extra_options.empty()) {
162                 command += lyxrc.print_extra_options + ' ';
163         }
164
165         command += buffer()->params().dvips_options() + ' ';
166
167         if (!Exporter::Export(buffer(), "dvi", true)) {
168                 showPrintError(buffer()->fileName());
169                 return;
170         }
171
172         // Push directory path.
173         string const path = buffer()->temppath();
174         Path p(path);
175
176         // there are three cases here:
177         // 1. we print to a file
178         // 2. we print directly to a printer
179         // 3. we print using a spool command (print to file first)
180         Systemcall one;
181         int res = 0;
182         string const dviname = ChangeExtension(buffer()->getLatexName(true), "dvi");
183         switch (pp.target) {
184         case PrinterParams::PRINTER:
185                 if (!lyxrc.print_spool_command.empty()) {
186                         // case 3: print using a spool
187                         string const psname = ChangeExtension(dviname, ".ps");
188                         command += lyxrc.print_to_file
189                                 + QuoteName(psname)
190                                 + ' '
191                                 + QuoteName(dviname);
192
193                         string command2 = lyxrc.print_spool_command + ' ';
194                         if (!pp.printer_name.empty()) {
195                                 command2 += lyxrc.print_spool_printerprefix
196                                         + pp.printer_name
197                                         + ' ';
198                         }
199                         command2 += QuoteName(psname);
200                         // First run dvips.
201                         // If successful, then spool command
202                         res = one.startscript(Systemcall::Wait, command);
203                         if (res == 0)
204                                 res = one.startscript(Systemcall::DontWait,
205                                                       command2);
206                 } else {
207                         // case 2: print directly to a printer
208                         res = one.startscript(Systemcall::DontWait,
209                                               command + QuoteName(dviname));
210                 }
211                 break;
212
213         case PrinterParams::FILE:
214                 // case 1: print to a file
215                 command += lyxrc.print_to_file
216                         + QuoteName(MakeAbsPath(pp.file_name, path))
217                         + ' '
218                         + QuoteName(dviname);
219                 res = one.startscript(Systemcall::DontWait, command);
220                 break;
221         }
222
223         lyxerr[Debug::LATEX] << "ControlPrint::apply(): print command = \""
224                              << command << '"' << endl;
225
226         if (res != 0)
227                 showPrintError(buffer()->fileName());
228 }