summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorManuel Schneider2013-10-01 16:44:47 +0200
committerManuel Schneider2013-10-01 16:44:47 +0200
commit617d6ec516fc660251e357e8cc8f70b06341a357 (patch)
treeb99c8af9a818d0a53548c28077625cd6cf6c95a9 /src
parentAdded new param 'username' (diff)
downloadprintergui-617d6ec516fc660251e357e8cc8f70b06341a357.tar.gz
printergui-617d6ec516fc660251e357e8cc8f70b06341a357.tar.xz
printergui-617d6ec516fc660251e357e8cc8f70b06341a357.zip
Changed tree stucture
Diffstat (limited to 'src')
-rw-r--r--src/PrinterGUI.pro22
-rw-r--r--src/main.cpp28
-rw-r--r--src/mainwindow.cpp277
-rw-r--r--src/mainwindow.h36
-rw-r--r--src/mainwindow.ui203
5 files changed, 566 insertions, 0 deletions
diff --git a/src/PrinterGUI.pro b/src/PrinterGUI.pro
new file mode 100644
index 0000000..e51ec31
--- /dev/null
+++ b/src/PrinterGUI.pro
@@ -0,0 +1,22 @@
+#-------------------------------------------------
+#
+# Project created by QtCreator 2013-09-26T00:51:08
+#
+#-------------------------------------------------
+
+QT += core gui
+
+greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
+
+TARGET = PrinterGUI
+TEMPLATE = app
+
+
+SOURCES += main.cpp\
+ mainwindow.cpp
+
+HEADERS += mainwindow.h
+
+FORMS += mainwindow.ui
+
+LIBS += -L/usr/lib/x86_64-linux-gnu -lcups
diff --git a/src/main.cpp b/src/main.cpp
new file mode 100644
index 0000000..2b97c95
--- /dev/null
+++ b/src/main.cpp
@@ -0,0 +1,28 @@
+#include <QApplication>
+#include <fstream>
+#include "mainwindow.h"
+
+int main(int argc, char *argv[])
+{
+ // First check parameter count
+ if (argc != 3)
+ return 2;
+
+ // Check if file exists
+ std::fstream f;
+ try
+ {
+ f.open(argv[2], std::ios::in);
+ }
+ catch (std::fstream::failure e)
+ {
+ return 3;
+ }
+ f.close();
+
+ QApplication a(argc, argv);
+ MainWindow w(argv);
+ w.show();
+
+ return a.exec();
+}
diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp
new file mode 100644
index 0000000..cc40d5e
--- /dev/null
+++ b/src/mainwindow.cpp
@@ -0,0 +1,277 @@
+#include "mainwindow.h"
+#include "ui_mainwindow.h"
+#include <pwd.h>
+
+
+
+MainWindow::MainWindow(char *argv[], QWidget *parent) :
+ QMainWindow(parent),
+ ui(new Ui::MainWindow),
+ user(argv[1]),
+ file(argv[2])
+{
+ // Initialize cups
+ num_dests = cupsGetDests(&dests);
+
+ // Initialize UI
+ initializeUI();
+}
+
+
+
+MainWindow::~MainWindow()
+{
+ cupsFreeDests(num_dests, dests);
+ delete ui;
+}
+
+
+
+void MainWindow::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(windowFlags() & ~Qt::WindowCloseButtonHint);
+ // 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 MainWindow::on_printerList_currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous)
+{
+ ui->printerList->setFocus();
+
+ cups_dest_t *dest =cupsGetNamedDest (CUPS_HTTP_DEFAULT, current->text(0).toAscii(), NULL);
+
+
+ /* * Check printer properties (auth, color, duplex, copies) * */
+ // get printer capabilities
+ int res = ::atoi(cupsGetOption("printer-type",
+ dest->num_options,
+ dest->options));
+
+ // Check authentication
+ if (res & CUPS_PRINTER_AUTHENTICATED)
+ {
+ ui->labelUser->setEnabled(true);
+ ui->labelPass->setEnabled(true);
+ ui->lineEditUser->setEnabled(true);
+ ui->lineEditPass->setEnabled(true);
+ }
+ else
+ {
+ ui->labelUser->setEnabled(false);
+ ui->labelPass->setEnabled(false);
+ ui->lineEditUser->setEnabled(false);
+ ui->lineEditPass->setEnabled(false);
+ }
+
+ // 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 MainWindow::on_buttonCancel_clicked()
+{
+ // Quit with code 1
+ QCoreApplication::instance()->exit(1);
+}
+
+
+
+void MainWindow::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("gs -sDEVICE=psgray -dNOPAUSE -dBATCH -dQUIET -dSAFER -sOutputFile=\"%22\" \"%1\"").arg(
+ file,
+ file);
+ if (system(cmd.toAscii()))
+ return; // TODO WARN ABOUT MISSED GS JOB
+
+ }
+
+
+
+
+/* * Print via sysctem command * */
+// // Username, part of whoami source
+// register uid_t uid = geteuid();
+// const char *username = getpwuid(uid)->pw_name;
+// cmd = QString("lp -U %1").arg(username);
+// // Duplex
+// if (ui->comboBoxSides->isEnabled())
+// {
+// switch (ui->comboBoxSides->currentIndex()){
+// case 0:
+// cmd.append(" -o sides=one-sided");
+// break;
+// case 1:
+// cmd.append(" -o sides=two-sided-long-edge");
+// break;
+// case 2:
+// cmd.append(" -o sides=two-sided-long-edge");
+// break;
+// }
+// }
+// // Kopien
+// if (ui->lineEditCopies->isEnabled())
+// cmd.append(QString(" -n %1").arg(ui->lineEditCopies->text()));
+// // Queue
+// cmd.append(QString(" -d %1").arg(ui->printerList->currentItem()->text(0)));
+// // File
+// cmd.append(QString(" -- \"%1\"").arg(file));
+// // Print results to stdout
+// QTextStream (stdout, QIODevice::WriteOnly) << cmd;
+// // Execute the command
+// system(cmd.toAscii());
+
+
+
+
+ /* * Print via cups lib * */
+
+ // Username
+ cupsSetUser(user);
+
+ // Destination / Queue
+ cups_dest_t *dest = cupsGetDest(
+ ui->printerList->currentItem()->text(0).toAscii(),
+ 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().toAscii(),
+ dest->num_options,
+ &(dest->options));
+ }
+
+ // Drucken
+ if( 0 == cupsPrintFile(dest->name,
+ file,
+ NULL,
+ dest->num_options,
+ dest->options))
+ return; // TODO ERROR OUTPUT
+ else // Quit with code 0
+ QCoreApplication::instance()->quit();
+
+}
+
+
diff --git a/src/mainwindow.h b/src/mainwindow.h
new file mode 100644
index 0000000..4b89262
--- /dev/null
+++ b/src/mainwindow.h
@@ -0,0 +1,36 @@
+#ifndef MAINWINDOW_H
+#define MAINWINDOW_H
+
+#include <QMainWindow>
+#include <QDebug>
+#include <QDesktopWidget>
+#include <QTreeWidget>
+#include "cups/cups.h"
+
+namespace Ui {
+class MainWindow;
+}
+
+class MainWindow : public QMainWindow
+{
+ Q_OBJECT
+
+public:
+ explicit MainWindow(char *argv[], QWidget *parent = 0);
+ ~MainWindow();
+
+private slots:
+ void on_printerList_currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous);
+ void on_buttonCancel_clicked();
+ void on_buttonPrint_clicked();
+
+private:
+ Ui::MainWindow *ui;
+ void initializeUI();
+ cups_dest_t *dests;
+ int num_dests;
+ const char * const user;
+ const char * const file;
+};
+
+#endif // MAINWINDOW_H
diff --git a/src/mainwindow.ui b/src/mainwindow.ui
new file mode 100644
index 0000000..0a56de6
--- /dev/null
+++ b/src/mainwindow.ui
@@ -0,0 +1,203 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ui version="4.0">
+ <class>MainWindow</class>
+ <widget class="QMainWindow" name="MainWindow">
+ <property name="geometry">
+ <rect>
+ <x>0</x>
+ <y>0</y>
+ <width>574</width>
+ <height>286</height>
+ </rect>
+ </property>
+ <property name="windowTitle">
+ <string>Printjob</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="1">
+ <layout class="QHBoxLayout" name="horizontalLayoutUser">
+ <item>
+ <widget class="QLabel" name="labelUser">
+ <property name="text">
+ <string>User:</string>
+ </property>
+ <property name="alignment">
+ <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
+ </property>
+ </widget>
+ </item>
+ <item>
+ <widget class="QLineEdit" name="lineEditUser">
+ <property name="sizePolicy">
+ <sizepolicy hsizetype="Fixed" vsizetype="Fixed">
+ <horstretch>0</horstretch>
+ <verstretch>0</verstretch>
+ </sizepolicy>
+ </property>
+ </widget>
+ </item>
+ </layout>
+ </item>
+ <item row="2" column="0">
+ <widget class="QComboBox" name="comboBoxColor">
+ <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">
+ <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="1">
+ <layout class="QHBoxLayout" name="horizontalLayoutPass">
+ <item>
+ <widget class="QLabel" name="labelPass">
+ <property name="text">
+ <string>Password:</string>
+ </property>
+ <property name="alignment">
+ <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
+ </property>
+ </widget>
+ </item>
+ <item>
+ <widget class="QLineEdit" name="lineEditPass">
+ <property name="sizePolicy">
+ <sizepolicy hsizetype="Fixed" vsizetype="Fixed">
+ <horstretch>0</horstretch>
+ <verstretch>0</verstretch>
+ </sizepolicy>
+ </property>
+ </widget>
+ </item>
+ </layout>
+ </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>Abrechen</string>
+ </property>
+ </widget>
+ </item>
+ <item>
+ <widget class="QPushButton" name="buttonPrint">
+ <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="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="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>
+ </layout>
+ </item>
+ </layout>
+ </item>
+ </layout>
+ </widget>
+ </widget>
+ <layoutdefault spacing="6" margin="11"/>
+ <resources/>
+ <connections/>
+</ui>