From d9694ef28a55ddb5c901f6e2c34397af6d3b38ba Mon Sep 17 00:00:00 2001 From: Manuel Schneider Date: Fri, 27 Sep 2013 15:45:53 +0200 Subject: Initial --- PrinterGUI/PrinterGUI_SRC/PrinterGUI.pro | 22 ++++ PrinterGUI/PrinterGUI_SRC/main.cpp | 28 ++++ PrinterGUI/PrinterGUI_SRC/mainwindow.cpp | 215 +++++++++++++++++++++++++++++++ PrinterGUI/PrinterGUI_SRC/mainwindow.h | 35 +++++ PrinterGUI/PrinterGUI_SRC/mainwindow.ui | 197 ++++++++++++++++++++++++++++ TODO | 4 + printerd | 26 ++++ 7 files changed, 527 insertions(+) create mode 100644 PrinterGUI/PrinterGUI_SRC/PrinterGUI.pro create mode 100644 PrinterGUI/PrinterGUI_SRC/main.cpp create mode 100644 PrinterGUI/PrinterGUI_SRC/mainwindow.cpp create mode 100644 PrinterGUI/PrinterGUI_SRC/mainwindow.h create mode 100644 PrinterGUI/PrinterGUI_SRC/mainwindow.ui create mode 100644 TODO create mode 100755 printerd diff --git a/PrinterGUI/PrinterGUI_SRC/PrinterGUI.pro b/PrinterGUI/PrinterGUI_SRC/PrinterGUI.pro new file mode 100644 index 0000000..e51ec31 --- /dev/null +++ b/PrinterGUI/PrinterGUI_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/PrinterGUI/PrinterGUI_SRC/main.cpp b/PrinterGUI/PrinterGUI_SRC/main.cpp new file mode 100644 index 0000000..784c88a --- /dev/null +++ b/PrinterGUI/PrinterGUI_SRC/main.cpp @@ -0,0 +1,28 @@ +#include +#include +#include "mainwindow.h" + +int main(int argc, char *argv[]) +{ + // First check parameter count + if (argc != 2) + return 2; + + // Check if file exists + std::fstream f; + try + { + f.open(argv[1], std::ios::in); + } + catch (std::fstream::failure e) + { + return 3; + } + f.close(); + + QApplication a(argc, argv); + MainWindow w(argv[1]); + w.show(); + + return a.exec(); +} diff --git a/PrinterGUI/PrinterGUI_SRC/mainwindow.cpp b/PrinterGUI/PrinterGUI_SRC/mainwindow.cpp new file mode 100644 index 0000000..03e74e9 --- /dev/null +++ b/PrinterGUI/PrinterGUI_SRC/mainwindow.cpp @@ -0,0 +1,215 @@ +#include "mainwindow.h" +#include "ui_mainwindow.h" +#include + + + +MainWindow::MainWindow(char *argv, QWidget *parent) : + QMainWindow(parent), + ui(new Ui::MainWindow), + file(argv) +{ + // 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); + } + + // 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); + system(cmd.toAscii()); + } + + // 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()); + + // Quit with code 0 + QCoreApplication::instance()->quit(); +} diff --git a/PrinterGUI/PrinterGUI_SRC/mainwindow.h b/PrinterGUI/PrinterGUI_SRC/mainwindow.h new file mode 100644 index 0000000..0078cde --- /dev/null +++ b/PrinterGUI/PrinterGUI_SRC/mainwindow.h @@ -0,0 +1,35 @@ +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include +#include +#include +#include +#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 file; +}; + +#endif // MAINWINDOW_H diff --git a/PrinterGUI/PrinterGUI_SRC/mainwindow.ui b/PrinterGUI/PrinterGUI_SRC/mainwindow.ui new file mode 100644 index 0000000..1be9776 --- /dev/null +++ b/PrinterGUI/PrinterGUI_SRC/mainwindow.ui @@ -0,0 +1,197 @@ + + + MainWindow + + + + 0 + 0 + 559 + 286 + + + + Printjob + + + + + + + + + + 1 + + + + + + + + + + + + User: + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + + 0 + 0 + + + + + + + + + + + Graustufen + + + + + Farbe + + + + + + + + + Simplex + + + + + Duplex, Long Edge + + + + + Duplex, Short Edge + + + + + + + + + + Password: + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + + 0 + 0 + + + + + + + + + + + + + 0 + 0 + + + + false + + + Abrechen + + + + + + + + 0 + 0 + + + + Drucken + + + + + + + + + + + Anzahl Kopien: + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + + 0 + 0 + + + + + 40 + 16777215 + + + + 00; + + + 1 + + + 2 + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + + + + + + + + + + diff --git a/TODO b/TODO new file mode 100644 index 0000000..1ca3440 --- /dev/null +++ b/TODO @@ -0,0 +1,4 @@ +- Do the authentication stuff +- Execute the printcommand directly from C/binary +- Show a options command which offers all options and their choices + diff --git a/printerd b/printerd new file mode 100755 index 0000000..ef93722 --- /dev/null +++ b/printerd @@ -0,0 +1,26 @@ +#! /bin/bash + +# This script runs a daemon that listens on a port for printjobs and pipes +# incoming data into UNIX lpd, which starts a helper program when a print- +# job is incoming. + +USER="$1" +BUSYBOX="busybox" +IP="0.0.0.0" +PORT="515" +SPOOLDIR="/var/spool" +SPOOLQUEUE="STANDARD" +GUIBINARY="printerGUI" + +# Create the directory for the queue +mkdir -p "$SPOOLDIR/$SPOOLQUEUE" + +# Change the owner of the directory such that lpd +# is able to write to it +chown $USER:$USER "$SPOOLDIR/$SPOOLQUEUE" + +# Start the lpdaemon listening on the given port +exec "$BUSYBOX" tcpsvd -Eu "$USER:$USER" "$IP" "$PORT" \ + "$BUSYBOX" lpd "$SPOOLDIR" \ + "$GUIBINARY" + -- cgit v1.2.3-55-g7522