diff options
Diffstat (limited to 'src/maingui')
-rw-r--r-- | src/maingui/main.cpp | 25 | ||||
-rw-r--r-- | src/maingui/printergui.cpp | 243 | ||||
-rw-r--r-- | src/maingui/printergui.h | 41 | ||||
-rw-r--r-- | src/maingui/printergui.ui | 202 |
4 files changed, 511 insertions, 0 deletions
diff --git a/src/maingui/main.cpp b/src/maingui/main.cpp new file mode 100644 index 0000000..f9b8bb2 --- /dev/null +++ b/src/maingui/main.cpp @@ -0,0 +1,25 @@ +#include <QApplication> +#include <fstream> +#include "printergui.h" +#include <fcntl.h> +#include <sys/stat.h> + +int main(int argc, char *argv[]) +{ + // First check parameter count + if (argc != 3) + return 2; + + // Check if file exists + int fh = open(argv[2], O_RDONLY); + if (fh < 0) { + fprintf(stderr, "ERROR: Could not open %s for reading..\n", argv[2]); + return 2; + } + close(fh); + + QApplication a(argc, argv); + PrinterGui w(argv); + w.show(); + return a.exec(); +} diff --git a/src/maingui/printergui.cpp b/src/maingui/printergui.cpp new file mode 100644 index 0000000..3744ffc --- /dev/null +++ b/src/maingui/printergui.cpp @@ -0,0 +1,243 @@ +#include "printergui.h" +#include "ui_printergui.h" +#include <QMessageBox> +#include <unistd.h> +#include <QTimer> + +// ____________________________________________________________________________ +PrinterGui::PrinterGui(char *argv[], QWidget *parent) : + QMainWindow(parent), + ui(new Ui::PrinterGui), + bgTimeout(-1) +{ + // When called it is guaranteed that argv has (at least) 3 elements + + // Set username + char buf[200]; + if (getlogin_r(buf, 200) == 0) { + // Detect real name + this->user = strdup(buf); + } 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) + strcpy(this->file, argv[2]); + + // Initialize cups + num_dests = cupsGetDests(&dests); + + // Initialize UI + initializeUI(); + + // Timer + this->bgTimer = new QTimer(this); + this->bgTimer->setInterval(1000); + connect(bgTimer, SIGNAL(timeout()), this, SLOT(on_bgTimer_timeout())); +} + +// ____________________________________________________________________________ +PrinterGui::~PrinterGui() +{ + cupsFreeDests(num_dests, dests); + free(this->user); + delete[] this->file; + delete this->ui; +} + +// ____________________________________________________________________________ +void PrinterGui::initializeUI() +{ + ui->setupUi(this); + ui->horizontalLayoutButtons->setAlignment(Qt::AlignRight); + + /* Initialize Treeview */ + + ui->printerList->setColumnCount(3); + ui->printerList->setSizePolicy(QSizePolicy::Minimum,QSizePolicy::Minimum); + + // Rename headers + QStringList h; + h.append("Drucker"); + h.append("Information"); + h.append("Standort"); + ui->printerList->setHeaderLabels(h); + + // Fill treewidget with data from cups dests; + cups_dest_t *dest = dests; + for (int i = num_dests; i>0; ++dest, --i ) + if (dest->instance == NULL) { + QTreeWidgetItem *wi = new QTreeWidgetItem(); + wi->setText(0, QString(dest->name)); + wi->setText(1, QString(cupsGetOption("printer-info", dest->num_options, dest->options))); + wi->setText(2, QString(cupsGetOption("printer-location", dest->num_options, dest->options))); + ui->printerList->addTopLevelItem(wi); + if (dest->is_default) + ui->printerList->setCurrentItem(wi); + } + + // Resize columns to contents + for (int i = 0; i < 3; ++i) + ui->printerList->resizeColumnToContents(i); + + /* Main Window properties */ + + // Disable close button + this->setWindowFlags((this->windowFlags() & ~Qt::WindowCloseButtonHint) | Qt::WindowStaysOnTopHint); + // center dialog on screen center + QRect desktopRect = QApplication::desktop()->screenGeometry(this); + this->move( desktopRect.width()/2-this->width()/2, + desktopRect.height()/2-this->height()/2); +} + +// ____________________________________________________________________________ +void PrinterGui::on_printerList_currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous) +{ + ui->printerList->setFocus(); + + cups_dest_t *dest =cupsGetNamedDest(CUPS_HTTP_DEFAULT, current->text(0).toUtf8().constData(), NULL); + + + /* * Check printer properties (auth, color, duplex, copies) * */ + // 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); + } + + // Check copy capabilities + if (res & CUPS_PRINTER_COPIES) { + ui->lineEditCopies->setEnabled(true); + ui->labelCopies->setEnabled(true); + } else { + ui->lineEditCopies->setEnabled(false); + ui->lineEditCopies->setText("1"); + ui->labelCopies->setEnabled(false); + } + + // Check availability + if (res & CUPS_PRINTER_REJECTING) { + ui->buttonPrint->setEnabled(false); + } else { + ui->buttonPrint->setEnabled(true); + } +} + +// ____________________________________________________________________________ +void PrinterGui::on_buttonCancel_clicked() +{ + // Quit with code 1 + QCoreApplication::instance()->exit(1); +} + +// ____________________________________________________________________________ +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=psgray -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 * */ + + // Destination / Queue + cups_dest_t *dest = cupsGetDest( + ui->printerList->currentItem()->text(0).toUtf8().constData(), + NULL, + 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; + } + } + + // Kopien + if (ui->lineEditCopies->isEnabled()) { + dest->num_options = cupsAddOption ("copies", + ui->lineEditCopies->text().toUtf8().constData(), + dest->num_options, + &(dest->options)); + } + + cupsSetUser(this->user); + + // Drucken + if( 0 == cupsPrintFile(dest->name, + file, + NULL, + dest->num_options, + dest->options)) { + QMessageBox::critical(this, "CUPS Fehler", cupsLastErrorString()); + } else { + this->bgTimeout = 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()->quit(); + } +} + diff --git a/src/maingui/printergui.h b/src/maingui/printergui.h new file mode 100644 index 0000000..223d787 --- /dev/null +++ b/src/maingui/printergui.h @@ -0,0 +1,41 @@ +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include <QMainWindow> +#include <QDebug> +#include <QDesktopWidget> +#include <QTreeWidget> +#include "cups/cups.h" + +namespace Ui { +class PrinterGui; +} + +class QTimer; + +class PrinterGui : public QMainWindow +{ + Q_OBJECT + +public: + explicit PrinterGui(char *argv[], QWidget *parent = 0); + ~PrinterGui(); + +private slots: + void on_printerList_currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous); + void on_buttonCancel_clicked(); + void on_buttonPrint_clicked(); + void on_bgTimer_timeout(); + +private: + Ui::PrinterGui *ui; + void initializeUI(); + cups_dest_t *dests; + int num_dests; + char * user; + char * file; + QTimer * bgTimer; + int bgTimeout; +}; + +#endif // MAINWINDOW_H diff --git a/src/maingui/printergui.ui b/src/maingui/printergui.ui new file mode 100644 index 0000000..8fafd23 --- /dev/null +++ b/src/maingui/printergui.ui @@ -0,0 +1,202 @@ +<?xml version="1.0" encoding="UTF-8"?> +<ui version="4.0"> + <class>PrinterGui</class> + <widget class="QMainWindow" name="PrinterGui"> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>590</width> + <height>286</height> + </rect> + </property> + <property name="minimumSize"> + <size> + <width>590</width> + <height>286</height> + </size> + </property> + <property name="windowTitle"> + <string>Druckauftrag</string> + </property> + <widget class="QWidget" name="centralWidget"> + <layout class="QGridLayout" name="gridLayout"> + <item row="0" column="0"> + <layout class="QVBoxLayout" name="verticalLayout"> + <item> + <widget class="QTreeWidget" name="printerList"> + <property name="showDropIndicator" stdset="0"> + <bool>false</bool> + </property> + <property name="alternatingRowColors"> + <bool>true</bool> + </property> + <column> + <property name="text"> + <string notr="true">1</string> + </property> + </column> + </widget> + </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> + <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> + <item row="3" column="2"> + <layout class="QHBoxLayout" name="horizontalLayoutButtons"> + <item> + <widget class="QPushButton" name="buttonCancel"> + <property name="sizePolicy"> + <sizepolicy hsizetype="Fixed" vsizetype="Fixed"> + <horstretch>0</horstretch> + <verstretch>0</verstretch> + </sizepolicy> + </property> + <property name="autoFillBackground"> + <bool>false</bool> + </property> + <property name="text"> + <string>Abbrechen</string> + </property> + </widget> + </item> + <item> + <widget class="QPushButton" name="buttonPrint"> + <property name="enabled"> + <bool>false</bool> + </property> + <property name="sizePolicy"> + <sizepolicy hsizetype="Fixed" vsizetype="Fixed"> + <horstretch>0</horstretch> + <verstretch>0</verstretch> + </sizepolicy> + </property> + <property name="text"> + <string>Drucken</string> + </property> + </widget> + </item> + </layout> + </item> + <item row="2" column="2"> + <layout class="QHBoxLayout" name="horizontalLayoutCopies"> + <item> + <widget class="QLabel" name="labelCopies"> + <property name="enabled"> + <bool>false</bool> + </property> + <property name="text"> + <string>Anzahl Kopien:</string> + </property> + <property name="alignment"> + <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set> + </property> + </widget> + </item> + <item> + <widget class="QLineEdit" name="lineEditCopies"> + <property name="enabled"> + <bool>false</bool> + </property> + <property name="sizePolicy"> + <sizepolicy hsizetype="Fixed" vsizetype="Fixed"> + <horstretch>0</horstretch> + <verstretch>0</verstretch> + </sizepolicy> + </property> + <property name="maximumSize"> + <size> + <width>40</width> + <height>16777215</height> + </size> + </property> + <property name="inputMask"> + <string>00; </string> + </property> + <property name="text"> + <string>1</string> + </property> + <property name="maxLength"> + <number>2</number> + </property> + <property name="alignment"> + <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set> + </property> + </widget> + </item> + </layout> + </item> + <item row="2" column="1"> + <spacer name="horizontalSpacer"> + <property name="orientation"> + <enum>Qt::Horizontal</enum> + </property> + <property name="sizeHint" stdset="0"> + <size> + <width>40</width> + <height>20</height> + </size> + </property> + </spacer> + </item> + <item row="3" column="1"> + <spacer name="horizontalSpacer_2"> + <property name="orientation"> + <enum>Qt::Horizontal</enum> + </property> + <property name="sizeHint" stdset="0"> + <size> + <width>40</width> + <height>20</height> + </size> + </property> + </spacer> + </item> + </layout> + </item> + </layout> + </item> + </layout> + </widget> + </widget> + <layoutdefault spacing="6" margin="11"/> + <resources/> + <connections/> +</ui> |