From 676989e51a78ba08ed068e3184cfe1bd796ac4c1 Mon Sep 17 00:00:00 2001 From: Manuel Schneider Date: Wed, 3 Dec 2014 17:44:04 +0100 Subject: Make color and duplex options whitelisted. [NEEDS TESTING] --- src/maingui/printergui.cpp | 251 ++++++++++++++++++++++++++++++--------------- src/maingui/printergui.h | 10 +- src/maingui/printergui.ui | 77 +++++++------- 3 files changed, 212 insertions(+), 126 deletions(-) diff --git a/src/maingui/printergui.cpp b/src/maingui/printergui.cpp index 6f72b15..595bdee 100644 --- a/src/maingui/printergui.cpp +++ b/src/maingui/printergui.cpp @@ -4,25 +4,26 @@ #include #include #include +#include // ____________________________________________________________________________ PrinterGui::PrinterGui(char *argv[], QWidget *parent) : QMainWindow(parent), - ui(new Ui::PrinterGui), - bgTimeout(-1) + ui(new Ui::PrinterGui) + //bgTimeout(-1) { // When called it is guaranteed that argv has (at least) 3 elements // Set username // Do not use getlogin[_r] as it doesn't fucking work! - struct passwd *pw = getpwuid(getuid()); - if (pw != NULL && pw->pw_name != NULL) { - // Detect real name - this->user = strdup(pw->pw_name); - } else { - // Fallback to what command line says - this->user = strdup(argv[1]); - } +// struct passwd *pw = getpwuid(getuid()); +// if (pw != NULL && pw->pw_name != NULL) { +// // Detect real name +// this->user = strdup(pw->pw_name); +// } else { +// // Fallback to what command line says + this->user = strdup(argv[1]); +// } // Filename this->file = new char[strlen(argv[2]) + 10]; // + 10 in case we rename (ghostscript) @@ -34,10 +35,11 @@ PrinterGui::PrinterGui(char *argv[], QWidget *parent) : // Initialize UI initializeUI(); - // Timer - this->bgTimer = new QTimer(this); - this->bgTimer->setInterval(1000); - connect(bgTimer, SIGNAL(timeout()), this, SLOT(on_bgTimer_timeout())); +// // Timer +// this->bgTimer = new QTimer(this); +// this->bgTimer->setInterval(1000); +// this->bgTimer->start(1000); +// connect(bgTimer, SIGNAL(timeout()), this, SLOT(on_bgTimer_timeout())); } // ____________________________________________________________________________ @@ -93,8 +95,15 @@ void PrinterGui::initializeUI() this->move( desktopRect.width()/2-this->width()/2, desktopRect.height()/2-this->height()/2); this->setWindowTitle(QString::fromUtf8("Drucken - %1").arg(this->user)); + + ui->comboBoxColor->hide(); + ui->comboBoxSides->hide(); + ui->label_color->hide(); + ui->label_duplex->hide(); } + + // ____________________________________________________________________________ void PrinterGui::on_printerList_currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous) { @@ -104,31 +113,89 @@ void PrinterGui::on_printerList_currentItemChanged(QTreeWidgetItem *current, QTr /* * Check printer properties (auth, color, duplex, copies) * */ - // get printer capabilities - + // get printer capabilities const char *type = cupsGetOption("printer-type", dest->num_options, dest->options); int res = 0; if (type != NULL) { res = ::atoi(type); } - // Check color capabilities - if (res & CUPS_PRINTER_COLOR) { - ui->comboBoxColor->setEnabled(true); - ui->comboBoxColor->setCurrentIndex(1); - } else { - ui->comboBoxColor->setEnabled(false); - ui->comboBoxColor->setCurrentIndex(0); - } - - // Check duplex capabilities - if (res & CUPS_PRINTER_DUPLEX) { - ui->comboBoxSides->setEnabled(true); - } else { - ui->comboBoxSides->setEnabled(false); - ui->comboBoxSides->setCurrentIndex(0); - } + ui->comboBoxSides->hide(); + ui->comboBoxColor->hide(); + ui->label_color->hide(); + ui->label_duplex->hide(); + + ppd_file_t *ppd = ppdOpenFile(cupsGetPPD2(CUPS_HTTP_DEFAULT, dest->name)); + if (ppd != NULL) + { + // Check color capabilities + if (res & CUPS_PRINTER_COLOR) + { + // Check for each option if it matches an + _colorOptionName.clear(); + QStringList knownColorOptions; + knownColorOptions << "XRXColor"; // TODO read from a file + + for (ppd_option_t *o = ppdFirstOption(ppd); o != NULL; o = ppdNextOption(ppd)) + { + QString option(o->keyword); + for (QStringList::iterator it = knownColorOptions.begin(); + it != knownColorOptions.end(); ++it) + { + if(option.contains(*it)) + { + _colorOptionName = option; + // Add the choices to the combobox + ui->comboBoxColor->clear(); + for (int i = 0; i < o->num_choices; ++i) { + ui->comboBoxColor->addItem(o->choices[i].text, o->choices[i].choice); + if (strcmp(o->choices[i].choice, o->defchoice) == 0) + ui->comboBoxColor->setCurrentIndex(i); + } + ui->comboBoxColor->show(); + ui->label_color->show(); + ui->comboBoxColor->adjustSize(); + break; + } + } + } + } + + // Check duplex capabilities + if (res & CUPS_PRINTER_DUPLEX) { + // Check for each option if it matches an + _duplexOptionName.clear(); + QStringList knownDuplexOptions; + knownDuplexOptions << "Duplex"; // TODO read from a file + + for (ppd_option_t *o = ppdFirstOption(ppd); o != NULL; o = ppdNextOption(ppd)) + { + QString option(o->keyword); + for (QStringList::iterator it = knownDuplexOptions.begin(); + it != knownDuplexOptions.end(); ++it) + { + if (option == *it) + { + _duplexOptionName = option; + // Add the choices to the combobox + ui->comboBoxSides->clear(); + for (int i = 0; i < o->num_choices; ++i) { + ui->comboBoxSides->addItem(o->choices[i].text, o->choices[i].choice); + if (strcmp(o->choices[i].choice, o->defchoice) == 0) + ui->comboBoxSides->setCurrentIndex(i); + } + ui->comboBoxSides->show(); + ui->label_duplex->show(); + ui->comboBoxSides->adjustSize(); + break; + } + } + } + } + } + else + qDebug() << "ppd is null"<< dest->name << cupsLastErrorString(); // Check copy capabilities if (res & CUPS_PRINTER_COPIES) { @@ -160,20 +227,20 @@ void PrinterGui::on_buttonPrint_clicked() { QString cmd; - // Wenn Farbe möglich ist. Aber trotzdem Graustufen gewählt - // Schieb file durch ghostscript - if (ui->comboBoxColor->isEnabled() && ui->comboBoxColor->currentIndex() == 0) { - // Run ghostscript to make file grayscale - cmd = QString::fromUtf8("gs -sDEVICE=ps2write -sProcessColorModel=DeviceGray -sColorConversionStrategy=/Gray -dNOPAUSE -dBATCH -dQUIET -dSAFER -sOutputFile=\"%1.conv\" \"%1\"") - .arg(QString::fromUtf8(this->file)); - if (system(cmd.toUtf8().constData())) { - QMessageBox::critical(this, "PrinterGUI", "Kann Druckauftrag nicht in Graustufen konvertieren."); - return; - } else { - strcat(this->file, ".conv"); - } - - } +// // Wenn Farbe möglich ist. Aber trotzdem Graustufen gewählt +// // Schieb file durch ghostscript +// if (ui->comboBoxColor->isEnabled() && ui->comboBoxColor->currentIndex() == 0) { +// // Run ghostscript to make file grayscale +// cmd = QString::fromUtf8("gs -sDEVICE=ps2write -sProcessColorModel=DeviceGray -sColorConversionStrategy=/Gray -dNOPAUSE -dBATCH -dQUIET -dSAFER -sOutputFile=\"%1.conv\" \"%1\"") +// .arg(QString::fromUtf8(this->file)); +// if (system(cmd.toUtf8().constData())) { +// QMessageBox::critical(this, "PrinterGUI", "Kann Druckauftrag nicht in Graustufen konvertieren."); +// return; +// } else { +// strcat(this->file, ".conv"); +// } + +// } /* * Print via cups lib * */ @@ -184,29 +251,50 @@ void PrinterGui::on_buttonPrint_clicked() num_dests, dests); - // Duplex - if (ui->comboBoxSides->isEnabled()) { - switch (ui->comboBoxSides->currentIndex()) { - case 0: - dest->num_options = cupsAddOption ("Duplex", - "None", - dest->num_options, - &(dest->options)); - break; - case 1: - dest->num_options = cupsAddOption ("Duplex", - "DuplexNoTumble", - dest->num_options, - &(dest->options)); - break; - case 2: - dest->num_options = cupsAddOption ("Duplex", - "DuplexTumble", - dest->num_options, - &(dest->options)); - break; - } - } + // Color + if (!_colorOptionName.isEmpty()) + dest->num_options = cupsAddOption (_colorOptionName.toStdString().c_str(), + ui->comboBoxColor->itemData(ui->comboBoxColor->currentIndex()).toString().toStdString().c_str(), + dest->num_options, + &(dest->options)); + // Duplex + if (!_duplexOptionName.isEmpty()) + dest->num_options = cupsAddOption (_duplexOptionName.toStdString().c_str(), + ui->comboBoxSides->itemData(ui->comboBoxSides->currentIndex()).toString().toStdString().c_str(), + dest->num_options, + &(dest->options)); + + qDebug() << ui->comboBoxColor->currentIndex(); + qDebug() << ui->comboBoxSides->currentIndex(); + qDebug() << ui->comboBoxColor->itemData(ui->comboBoxColor->currentIndex()).toString().toStdString().c_str(); + qDebug() << ui->comboBoxSides->itemData(ui->comboBoxSides->currentIndex()).toString().toStdString().c_str(); + + + + +// // Duplex +// if (ui->comboBoxSides->isEnabled()) { +// switch (ui->comboBoxSides->currentIndex()) { +// case 0: +// dest->num_options = cupsAddOption ("Duplex", +// "None", +// dest->num_options, +// &(dest->options)); +// break; +// case 1: +// dest->num_options = cupsAddOption ("Duplex", +// "DuplexNoTumble", +// dest->num_options, +// &(dest->options)); +// break; +// case 2: +// dest->num_options = cupsAddOption ("Duplex", +// "DuplexTumble", +// dest->num_options, +// &(dest->options)); +// break; +// } +// } // Kopien if (ui->lineEditCopies->isEnabled()) { @@ -228,21 +316,22 @@ void PrinterGui::on_buttonPrint_clicked() dest->options)) { QMessageBox::critical(this, "CUPS Fehler", cupsLastErrorString()); } else { - this->bgTimeout = 0; + //this->bgTimeout = 0; + QCoreApplication::instance()->exit(0); this->hide(); } } -void PrinterGui::on_bgTimer_timeout() -{ - if (this->bgTimeout == -1) { - // Could do something here every second.... - return; - } - if (++this->bgTimeout > 4) { - // Job was sent, GUI is invisible, quit after a few seconds - QCoreApplication::instance()->exit(0); - } -} +//void PrinterGui::on_bgTimer_timeout() +//{ +// if (this->bgTimeout == -1) { +// // Could do something here every second.... +// return; +// } +// if (++this->bgTimeout > 4) { +// // Job was sent, GUI is invisible, quit after a few seconds +// QCoreApplication::instance()->exit(0); +// } +//} diff --git a/src/maingui/printergui.h b/src/maingui/printergui.h index 223d787..7dcea7c 100644 --- a/src/maingui/printergui.h +++ b/src/maingui/printergui.h @@ -25,7 +25,7 @@ private slots: void on_printerList_currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous); void on_buttonCancel_clicked(); void on_buttonPrint_clicked(); - void on_bgTimer_timeout(); +// void on_bgTimer_timeout(); private: Ui::PrinterGui *ui; @@ -34,8 +34,12 @@ private: int num_dests; char * user; char * file; - QTimer * bgTimer; - int bgTimeout; +// QTimer * bgTimer; +// int bgTimeout; + + QString _colorOptionName; + QString _duplexOptionName; + }; #endif // MAINWINDOW_H diff --git a/src/maingui/printergui.ui b/src/maingui/printergui.ui index 8fafd23..dd13d0a 100644 --- a/src/maingui/printergui.ui +++ b/src/maingui/printergui.ui @@ -40,46 +40,13 @@ - - - - false - - - - Graustufen - - - - - Farbe - - - + + - - - - false - - - - Simplex - - - - - Duplex, Long Edge - - - - - Duplex, Short Edge - - - + + - + @@ -115,7 +82,7 @@ - + @@ -141,6 +108,12 @@ 0 + + + 0 + 26 + + 40 @@ -148,7 +121,7 @@ - 00; + 00 1 @@ -163,7 +136,7 @@ - + Qt::Horizontal @@ -176,7 +149,7 @@ - + Qt::Horizontal @@ -189,6 +162,26 @@ + + + + Farbe: + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + Duplex: + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + -- cgit v1.2.3-55-g7522