00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
#include "kcupsprinterimpl.h"
00021
#include "kprinter.h"
00022
#include "driver.h"
00023
#include "kmfactory.h"
00024
#include "kmmanager.h"
00025
#include "cupsinfos.h"
00026
00027
#include <qfile.h>
00028
#include <cups/cups.h>
00029
#include <stdlib.h>
00030
#include <kprocess.h>
00031
00032
static void mapToCupsOptions(
const QMap<QString,QString>& opts,
QString& cmd);
00033
00034
QSize rangeToSize(
const QString& s)
00035 {
00036
QString range = s;
00037
int p(-1);
00038
int from, to;
00039
00040
if ((p=range.find(
',')) != -1)
00041 range.truncate(p);
00042
if ((p=range.find(
'-')) != -1)
00043 {
00044 from = range.left(p).toInt();
00045 to = range.right(range.length()-p-1).toInt();
00046 }
00047
else if (!range.isEmpty())
00048 from = to = range.toInt();
00049
else
00050 from = to = 0;
00051
00052
return QSize(from,to);
00053 }
00054
00055
00056 KCupsPrinterImpl::KCupsPrinterImpl(
QObject *parent,
const char *name,
const QStringList & )
00057 : KPrinterImpl(parent,name)
00058 {
00059 }
00060
00061 KCupsPrinterImpl::~KCupsPrinterImpl()
00062 {
00063 }
00064
00065
bool KCupsPrinterImpl::setupCommand(
QString& cmd,
KPrinter *printer)
00066 {
00067
00068
if (!printer)
return false;
00069
00070
QString hoststr = QString::fromLatin1(
"%1:%2").arg(CupsInfos::self()->host()).arg(CupsInfos::self()->port());
00071 cmd = QString::fromLatin1(
"cupsdoprint -P %1 -J %3 -H %2").arg(quote(printer->
printerName())).arg(quote(hoststr)).arg(quote(printer->
docName()));
00072
if (!CupsInfos::self()->login().isEmpty())
00073 {
00074
QString userstr(CupsInfos::self()->login());
00075
00076
00077 cmd.append(
" -U ").append(quote(userstr));
00078 }
00079 mapToCupsOptions(printer->
options(),cmd);
00080
return true;
00081 }
00082
00083
void KCupsPrinterImpl::preparePrinting(
KPrinter *printer)
00084 {
00085
00086
QString o = printer->
option(
"orientation-requested");
00087 printer->
setOption(
"kde-orientation",(o ==
"4" || o ==
"5" ?
"Landscape" :
"Portrait"));
00088
00089
if (printer->
applicationType() == KPrinter::Dialog)
00090 printer->
setOption(
"orientation-requested",(o ==
"5" || o ==
"6" ?
"6" :
"3"));
00091
00092
00093
if (!printer->
option(
"kde-copies").isEmpty()) printer->
setOption(
"copies",printer->
option(
"kde-copies"));
00094
00095
00096
if (printer->
pageSelection() == KPrinter::SystemSide)
00097 {
00098
00099
if (!printer->
option(
"kde-range").isEmpty())
00100 printer->
setOption(
"page-ranges",printer->
option(
"kde-range"));
00101
if (printer->
option(
"kde-pageorder") ==
"Reverse")
00102 printer->
setOption(
"OutputOrder",printer->
option(
"kde-pageorder"));
00103 o = printer->
option(
"kde-pageset");
00104
if (!o.isEmpty() && o !=
"0")
00105 printer->
setOption(
"page-set",(o ==
"1" ?
"odd" :
"even"));
00106 printer->
setOption(
"multiple-document-handling",(printer->
option(
"kde-collate") ==
"Collate" ?
"separate-documents-collated-copies" :
"separate-documents-uncollated-copies"));
00107 }
00108
else
00109 {
00110
QString range = printer->
option(
"kde-range");
00111
if (!range.isEmpty())
00112 {
00113
QSize s = rangeToSize(range);
00114 printer->
setOption(
"kde-from",QString::number(s.width()));
00115 printer->
setOption(
"kde-to",QString::number(s.height()));
00116 }
00117 }
00118
00119
00120 KPrinterImpl::preparePrinting(printer);
00121 }
00122
00123
void KCupsPrinterImpl::broadcastOption(
const QString& key,
const QString& value)
00124 {
00125 KPrinterImpl::broadcastOption(key,value);
00126
if (key ==
"kde-orientation")
00127 KPrinterImpl::broadcastOption(
"orientation-requested",(value ==
"Landscape" ?
"4" :
"3"));
00128
else if (key ==
"kde-pagesize")
00129 {
00130
QString pagename = QString::fromLatin1(pageSizeToPageName((KPrinter::PageSize)value.toInt()));
00131 KPrinterImpl::broadcastOption(
"PageSize",pagename);
00132
00133 KPrinterImpl::broadcastOption(
"media",pagename);
00134 }
00135 }
00136
00137
00138
00139
static void mapToCupsOptions(
const QMap<QString,QString>& opts,
QString& cmd)
00140 {
00141
QString optstr;
00142
for (
QMap<QString,QString>::ConstIterator it=opts.begin(); it!=opts.end(); ++it)
00143 {
00144
00145
if (!it.key().startsWith(
"kde-") && !it.key().startsWith(
"app-") && !it.key().startsWith(
"_kde"))
00146 {
00147
QString key = it.key();
00148
if (key.startsWith(
"KDEPrint-"))
00149
00150
00151 key = key.mid(9);
00152 optstr.append(
" ").append(key);
00153
if (!it.data().isEmpty())
00154 optstr.append(
"=").append(it.data());
00155 }
00156 }
00157
if (!optstr.isEmpty())
00158 cmd.append(
" -o ").append( KProcess::quote( optstr ) );
00159 }