]> git.lyx.org Git - lyx.git/blob - src/frontends/Liason.C
This file is part of LyX, the document processor.
[lyx.git] / src / frontends / Liason.C
1 /**
2  * \file Liason.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Allan Rae
7  *
8  * Full author contact details are available in file CREDITS
9  */
10
11 #include <config.h>
12
13 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16
17 #include "Liason.h"
18 #include "lyxrc.h"
19 #include "PrinterParams.h"
20
21 #include "frontends/LyXView.h"
22 #include "BufferView.h"
23 #include "buffer.h"
24 #include "exporter.h"
25 #include "converter.h"
26
27 #include "support/LAssert.h"
28 #include "support/lstrings.h"
29 #include "support/filetools.h"
30 #include "support/path.h"
31 #include "support/systemcall.h"
32
33 using std::endl;
34
35 namespace Liason {
36
37 PrinterParams getPrinterParams(Buffer * buffer)
38 {
39         return PrinterParams(PrinterParams::PRINTER,
40                              lyxrc.printer,
41                              ChangeExtension(buffer->fileName(),
42                                              lyxrc.print_file_extension));
43 }
44
45
46 bool printBuffer(Buffer * buffer, PrinterParams const & pp)
47 {
48         string command(lyxrc.print_command + ' ');
49
50         if (pp.target == PrinterParams::PRINTER
51             && lyxrc.print_adapt_output  // dvips wants a printer name
52             && !pp.printer_name.empty()) {// printer name given
53                 command += lyxrc.print_to_printer
54                         + pp.printer_name
55                         + ' ';
56         }
57
58         switch (pp.which_pages) {
59         case PrinterParams::EVEN:
60                 command += lyxrc.print_evenpage_flag + ' ';
61                 break;
62
63         case PrinterParams::ODD:
64                 command += lyxrc.print_oddpage_flag + ' ';
65                 break;
66
67         default:
68                 // only option left is print all of them
69                 break;
70         }
71
72         if (!pp.from_page.empty()) {
73                 command += lyxrc.print_pagerange_flag + ' ';
74                 command += pp.from_page;
75                 if (pp.to_page) {
76                                 // we have a range "from-to"
77                         command += '-';
78                         command += tostr(pp.to_page);
79                 }
80                 command += ' ';
81         }
82
83         if (pp.reverse_order) {
84                 command += lyxrc.print_reverse_flag + ' ';
85         }
86
87         if (1 < pp.count_copies) {
88                 if (pp.unsorted_copies) {
89                         command += lyxrc.print_copies_flag;
90                 } else {
91                         command += lyxrc.print_collcopies_flag;
92                 }
93                 command += ' ';
94                 command += tostr(pp.count_copies);
95                 command += ' ';
96         }
97
98         if (!lyxrc.print_extra_options.empty()) {
99                 command += lyxrc.print_extra_options + ' ';
100         }
101
102         command += converters.dvips_options(buffer) + ' ';
103
104         if (!Exporter::Export(buffer, "dvi", true))
105                 return false;
106
107         // Push directory path.
108         string path = buffer->filePath();
109         if (lyxrc.use_tempdir || !IsDirWriteable(path)) {
110                 path = buffer->tmppath;
111         }
112         Path p(path);
113
114         // there are three cases here:
115         // 1. we print to a file
116         // 2. we print direct to a printer
117         // 3. we print using a spool command (print to file first)
118         Systemcall one;
119         int res = 0;
120         string dviname = ChangeExtension(buffer->getLatexName(true), "dvi");
121         switch (pp.target) {
122         case PrinterParams::PRINTER:
123                 if (!lyxrc.print_spool_command.empty()) {
124                         // case 3
125                         string psname = ChangeExtension(dviname, ".ps");
126                         command += lyxrc.print_to_file
127                                 + QuoteName(psname) + ' ';
128                         command += QuoteName(dviname);
129                         string command2 = lyxrc.print_spool_command + ' ';
130                         if (!pp.printer_name.empty())
131                                 command2 += lyxrc.print_spool_printerprefix
132                                         + pp.printer_name + ' ';
133                         command2 += QuoteName(psname);
134                         // First run dvips.
135                         // If successful, then spool command
136                         res = one.startscript(Systemcall::Wait, command);
137                         if (res == 0)
138                                 res = one.startscript(Systemcall::DontWait,
139                                                       command2);
140                 } else
141                         // case 2
142                         res = one.startscript(Systemcall::DontWait,
143                                               command + QuoteName(dviname));
144                 break;
145
146         case PrinterParams::FILE:
147                 // case 1
148                 command += lyxrc.print_to_file
149                         + QuoteName(MakeAbsPath(pp.file_name, path));
150                 command += ' ' + QuoteName(dviname);
151                 res = one.startscript(Systemcall::DontWait, command);
152                 break;
153         }
154         return res == 0;
155 }
156
157
158 void setMinibuffer(LyXView * lv, string const & msg)
159 {
160         lyx::Assert(lv);
161         lv->message(msg);
162 }
163
164 } // namespace Liason