summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorManuel Schneider2014-12-03 17:44:04 +0100
committerManuel Schneider2014-12-03 17:44:04 +0100
commit676989e51a78ba08ed068e3184cfe1bd796ac4c1 (patch)
treea3254529271efe231ec4ccdc89f50788ee559a77
parentFix intention (diff)
downloadprintergui-676989e51a78ba08ed068e3184cfe1bd796ac4c1.tar.gz
printergui-676989e51a78ba08ed068e3184cfe1bd796ac4c1.tar.xz
printergui-676989e51a78ba08ed068e3184cfe1bd796ac4c1.zip
Make color and duplex options whitelisted. [NEEDS TESTING]
-rw-r--r--src/maingui/printergui.cpp251
-rw-r--r--src/maingui/printergui.h10
-rw-r--r--src/maingui/printergui.ui77
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 <unistd.h>
#include <QTimer>
#include <pwd.h>
+#include <cups/ppd.h>
// ____________________________________________________________________________
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 @@
</item>
<item>
<layout class="QGridLayout" name="gridLayout_2">
- <item row="2" column="0">
- <widget class="QComboBox" name="comboBoxColor">
- <property name="enabled">
- <bool>false</bool>
- </property>
- <item>
- <property name="text">
- <string>Graustufen</string>
- </property>
- </item>
- <item>
- <property name="text">
- <string>Farbe</string>
- </property>
- </item>
- </widget>
+ <item row="2" column="1">
+ <widget class="QComboBox" name="comboBoxColor"/>
</item>
- <item row="3" column="0">
- <widget class="QComboBox" name="comboBoxSides">
- <property name="enabled">
- <bool>false</bool>
- </property>
- <item>
- <property name="text">
- <string>Simplex</string>
- </property>
- </item>
- <item>
- <property name="text">
- <string>Duplex, Long Edge</string>
- </property>
- </item>
- <item>
- <property name="text">
- <string>Duplex, Short Edge</string>
- </property>
- </item>
- </widget>
+ <item row="3" column="1">
+ <widget class="QComboBox" name="comboBoxSides"/>
</item>
- <item row="3" column="2">
+ <item row="3" column="3">
<layout class="QHBoxLayout" name="horizontalLayoutButtons">
<item>
<widget class="QPushButton" name="buttonCancel">
@@ -115,7 +82,7 @@
</item>
</layout>
</item>
- <item row="2" column="2">
+ <item row="2" column="3">
<layout class="QHBoxLayout" name="horizontalLayoutCopies">
<item>
<widget class="QLabel" name="labelCopies">
@@ -141,6 +108,12 @@
<verstretch>0</verstretch>
</sizepolicy>
</property>
+ <property name="minimumSize">
+ <size>
+ <width>0</width>
+ <height>26</height>
+ </size>
+ </property>
<property name="maximumSize">
<size>
<width>40</width>
@@ -148,7 +121,7 @@
</size>
</property>
<property name="inputMask">
- <string>00; </string>
+ <string>00</string>
</property>
<property name="text">
<string>1</string>
@@ -163,7 +136,7 @@
</item>
</layout>
</item>
- <item row="2" column="1">
+ <item row="2" column="2">
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
@@ -176,7 +149,7 @@
</property>
</spacer>
</item>
- <item row="3" column="1">
+ <item row="3" column="2">
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
@@ -189,6 +162,26 @@
</property>
</spacer>
</item>
+ <item row="2" column="0">
+ <widget class="QLabel" name="label_color">
+ <property name="text">
+ <string>Farbe:</string>
+ </property>
+ <property name="alignment">
+ <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
+ </property>
+ </widget>
+ </item>
+ <item row="3" column="0">
+ <widget class="QLabel" name="label_duplex">
+ <property name="text">
+ <string>Duplex:</string>
+ </property>
+ <property name="alignment">
+ <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
+ </property>
+ </widget>
+ </item>
</layout>
</item>
</layout>