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