1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
|
//
// C++ Implementation: cupsprint
//
// Description:
//
//
// Author: Oleksandr Shneyder <oleksandr.shneyder@obviously-nice.de>, (C) 2009
//
// Copyright: See COPYING file that comes with this distribution
//
//
#include "cupsprint.h"
#ifndef Q_OS_WIN
#include "x2gologdebug.h"
#include "x2gosettings.h"
#include <QDir>
CUPSPrint::CUPSPrint()
{
ppd=0l;
num_dests= cupsGetDests ( &dests );
}
CUPSPrint::~CUPSPrint()
{
cupsFreeDests ( num_dests, dests );
if ( ppd )
ppdClose ( ppd );
ppd=0l;
}
QStringList CUPSPrint::getPrinters()
{
QStringList printers;
for ( int i=0;i<num_dests;++i )
printers<<dests[i].name;
return printers;
}
QString CUPSPrint::getDefaultUserPrinter()
{
X2goSettings st ( "printing" );
QString defPrint=st.setting()->value (
"CUPS/defaultprinter","" ). toString();
if ( defPrint.length() >0 )
{
cups_dest_t *dest = cupsGetDest ( defPrint.toAscii(),
0l, num_dests, dests );
if ( dest )
return defPrint;
}
cups_dest_t *dest = cupsGetDest ( 0l, 0l, num_dests, dests );
if ( dest )
defPrint=dest->name;
return defPrint;
}
void CUPSPrint::setDefaultUserPrinter ( QString printer )
{
X2goSettings st ( "printing" );
st.setting()->setValue ( "CUPS/defaultprinter", QVariant ( printer ) );
}
bool CUPSPrint::getPrinterInfo ( const QString& printerName, QString& info,
bool& acceptJobs, QString& location,
QString& model, printState& state,
QString& stateReason )
{
cups_dest_t *dest = cupsGetDest ( printerName.toAscii(), 0l,
num_dests,
dests );
if ( !dest )
return false;
acceptJobs=qstrcmp ( cupsGetOption ( "printer-is-accepting-jobs",
dest->num_options,
dest->options ),"0" );
info=QString::fromLocal8Bit (
cupsGetOption ( "printer-info",
dest->num_options, dest->options ) );
location=QString::fromLocal8Bit (
cupsGetOption ( "printer-location",
dest->num_options, dest->options ) );
model=QString::fromLocal8Bit (
cupsGetOption ( "printer-make-and-model",
dest->num_options, dest->options ) );
QString st=cupsGetOption ( "printer-state",
dest->num_options, dest->options );
state=NDEF;
if ( st=="3" )
state=IDLE;
if ( st=="4" )
state=PRINTING;
if ( st=="5" )
state=STOPPED;
stateReason=QString::fromLocal8Bit (
cupsGetOption ( "printer-state-reasons",
dest->num_options, dest->options ) );
return true;
}
bool CUPSPrint::setCurrentPrinter ( QString prn )
{
currentPrinter=prn;
QString fl=cupsGetPPD ( prn.toAscii() );
if ( fl.length() <=0 )
return false;
if ( ppd )
ppdClose ( ppd );
ppd=0l;
ppd=ppdOpenFile ( fl.toAscii() );
unlink ( fl.toAscii() );
if ( ppd==0l )
return false;
ppdMarkDefaults ( ppd );
loadUserOptions();
if ( ppdConflicts ( ppd ) !=0 )
{
x2goDebug<<"There are conflicting options in user settings,\n"
"loading defaults"<<endl;
setDefaults();
}
return true;
}
bool CUPSPrint::getOptionValue ( const QString& option,
QString& value, QString& valueText )
{
if ( !ppd )
return false;
ppd_choice_t* choice=ppdFindMarkedChoice ( ppd,option.toAscii() );
if ( !choice )
{
ppd_option_t* opt=ppdFindOption ( ppd,option.toAscii() );
if ( !opt )
return false;
choice=ppdFindChoice ( opt,opt->defchoice );
if ( !choice )
return false;
}
value=QString::fromLocal8Bit ( choice->choice );
valueText=QString::fromLocal8Bit ( choice->text );
// x2goDebug<<"getValue:"<<value<<endl;
return true;
}
int CUPSPrint::getOptionValues ( const QString& option,
QStringList& values,
QStringList& descriptions )
{
values.clear();
descriptions.clear();
if ( !ppd )
return -1;
int cur_val=-1;
values.clear();
descriptions.clear();
ppd_option_t* opt=ppdFindOption ( ppd,option.toAscii() );
if ( !opt )
return -1;
for ( int k=0;k<opt->num_choices;++k )
{
ppd_choice_t* choice=& ( opt->choices[k] );
if ( choice->marked )
{
cur_val=values.size();
}
//if no choice is marked, return default
if ( !qstrcmp ( choice->choice,opt->defchoice ) && cur_val==-1 )
{
cur_val=values.size();
}
values<<QString::fromLocal8Bit ( choice->choice );
descriptions<<QString::fromLocal8Bit ( choice->text );
}
return cur_val;
}
int CUPSPrint::getOptionGroups ( QStringList& names, QStringList& texts )
{
names.clear();
texts.clear();
if ( !ppd )
return -1;
for ( int i=0;i<ppd->num_groups;++i )
{
ppd_group_t* group=& ( ppd->groups[i] );
names<<QString::fromLocal8Bit ( group->name );
texts<<QString::fromLocal8Bit ( group->text );
}
return names.size();
}
int CUPSPrint::getOptionsList ( const QString& groupName, QStringList& names,
QStringList& texts )
{
names.clear();
texts.clear();
if ( !ppd )
return -1;
for ( int i=0;i<ppd->num_groups;++i )
{
ppd_group_t* group=& ( ppd->groups[i] );
if ( groupName.length() >0 && groupName !=
QString::fromLocal8Bit ( group->name ) )
continue;
for ( int j=0;j<group->num_options;++j )
{
ppd_option_t* option=& ( group->options[j] );
names<<QString::fromLocal8Bit ( option->keyword );
texts<<QString::fromLocal8Bit ( option->text );
}
}
return names.size();
}
bool CUPSPrint::setValue ( const QString& option, const QString& value,
QString& conflict_opt, QString& conflict_val )
{
if ( !ppd )
return false;
int conflictsBefore= ppdConflicts ( ppd );
QString valueBefore, textBefore;
if ( !getOptionValue ( option,valueBefore,textBefore ) )
return false;
ppdMarkOption ( ppd,option.toAscii(),value.toAscii() );
if ( conflictsBefore==ppdConflicts ( ppd ) )
{
return true;
}
//find conflicting option
for ( int i=0;i<ppd->num_consts;++i )
{
QString confOpt,confVal;
if ( option==ppd->consts[i].option1 &&
value==ppd->consts[i].choice1 )
{
confOpt=ppd->consts[i].option2;
confVal=ppd->consts[i].choice2;
}
else if ( option==ppd->consts[i].option2 &&
value==ppd->consts[i].choice2 )
{
confOpt=ppd->consts[i].option1;
confVal=ppd->consts[i].choice1;
}
else
continue;
QString selectedValue, selectedText;
if ( getOptionValue ( confOpt,selectedValue,selectedText ) )
{
if ( selectedValue==confVal )
{
//conflicting option/choice found
conflict_val=confVal;
conflict_opt=confOpt;
break;
}
}
}
//set previous value
ppdMarkOption ( ppd,option.toAscii(),valueBefore.toAscii() );
return false;
}
bool CUPSPrint::getOptionText ( const QString& option, QString& text )
{
if ( !ppd )
return false;
ppd_option_t* opt=ppdFindOption ( ppd,option .toAscii() );
if ( !opt )
return false;
text=QString::fromLocal8Bit ( opt->text );
return true;
}
void CUPSPrint::setDefaults()
{
//don't use ppdMarkDefaults here
//ppdMarkDefaults do not unmark
//already marked choices
if ( !ppd )
return;
for ( int i=0;i<ppd->num_groups;++i )
{
ppd_group_t* group=& ( ppd->groups[i] );
for ( int j=0;j<group->num_options;++j )
{
ppd_option_t* option=& ( group->options[j] );
ppdMarkOption ( ppd,option->keyword,option->defchoice );
}
}
}
void CUPSPrint::saveOptions()
{
if ( !ppd )
return;
X2goSettings st( "printing" );
QStringList options;
for ( int i=0;i<ppd->num_groups;++i )
{
ppd_group_t* group=& ( ppd->groups[i] );
for ( int j=0;j<group->num_options;++j )
{
ppd_option_t* option=& ( group->options[j] );
QString val,valtext;
if ( !getOptionValue ( option->keyword,val,valtext ) )
continue; //something is wrong here
if ( val!=option->defchoice )
{
QString opt=option->keyword;
opt+="="+val;
options<<opt;
}
}
}
st.setting()->setValue ( "CUPS/options/"+currentPrinter,
QVariant ( options ) );
}
void CUPSPrint::loadUserOptions()
{
X2goSettings st ( "printing" );
QStringList options=st.setting()->value (
"CUPS/options/"+currentPrinter ).toStringList();
for ( int i=0;i<options.size();++i )
{
QStringList opt=options[i].split ( "=" );
ppdMarkOption ( ppd,opt[0].toAscii(),opt[1].toAscii() );
}
}
void CUPSPrint::print ( const QString& file, QString title )
{
if ( !ppd )
return;
int num_options = 0;
cups_option_t *options = NULL;
for ( int i=0;i<ppd->num_groups;++i )
{
ppd_group_t* group=& ( ppd->groups[i] );
for ( int j=0;j<group->num_options;++j )
{
ppd_option_t* option=& ( group->options[j] );
QString val,valtext;
if ( !getOptionValue ( option->keyword,val,valtext ) )
continue; //something is wrong here
if ( val!=option->defchoice )
{
num_options = cupsAddOption ( option->keyword,
val.toAscii(),
num_options,
&options );
}
}
}
cupsPrintFile ( currentPrinter.toAscii(),file.toAscii(),
title.toAscii(), num_options,options );
cupsFreeOptions ( num_options, options );
}
#endif
|