From 3b24a2e128f2938d6d827fdbc696865b9bb0db23 Mon Sep 17 00:00:00 2001 From: Manuel Schneider Date: Mon, 30 Sep 2013 17:00:07 +0200 Subject: Added new param 'username' --- PrinterGUI/PrinterGUI_SRC/main.cpp | 6 +- PrinterGUI/PrinterGUI_SRC/mainwindow.cpp | 108 ++++++++++++++++++++++++------- PrinterGUI/PrinterGUI_SRC/mainwindow.h | 3 +- PrinterGUI/PrinterGUI_SRC/mainwindow.ui | 8 ++- printerd | 24 +++++-- 5 files changed, 116 insertions(+), 33 deletions(-) diff --git a/PrinterGUI/PrinterGUI_SRC/main.cpp b/PrinterGUI/PrinterGUI_SRC/main.cpp index 784c88a..2b97c95 100644 --- a/PrinterGUI/PrinterGUI_SRC/main.cpp +++ b/PrinterGUI/PrinterGUI_SRC/main.cpp @@ -5,14 +5,14 @@ int main(int argc, char *argv[]) { // First check parameter count - if (argc != 2) + if (argc != 3) return 2; // Check if file exists std::fstream f; try { - f.open(argv[1], std::ios::in); + f.open(argv[2], std::ios::in); } catch (std::fstream::failure e) { @@ -21,7 +21,7 @@ int main(int argc, char *argv[]) f.close(); QApplication a(argc, argv); - MainWindow w(argv[1]); + MainWindow w(argv); w.show(); return a.exec(); diff --git a/PrinterGUI/PrinterGUI_SRC/mainwindow.cpp b/PrinterGUI/PrinterGUI_SRC/mainwindow.cpp index 03e74e9..cc40d5e 100644 --- a/PrinterGUI/PrinterGUI_SRC/mainwindow.cpp +++ b/PrinterGUI/PrinterGUI_SRC/mainwindow.cpp @@ -4,10 +4,11 @@ -MainWindow::MainWindow(char *argv, QWidget *parent) : +MainWindow::MainWindow(char *argv[], QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow), - file(argv) + user(argv[1]), + file(argv[2]) { // Initialize cups num_dests = cupsGetDests(&dests); @@ -53,6 +54,8 @@ void MainWindow::initializeUI() 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 @@ -170,46 +173,105 @@ void MainWindow::on_buttonPrint_clicked() cmd = QString("gs -sDEVICE=psgray -dNOPAUSE -dBATCH -dQUIET -dSAFER -sOutputFile=\"%22\" \"%1\"").arg( file, file); - system(cmd.toAscii()); + if (system(cmd.toAscii())) + return; // TODO WARN ABOUT MISSED GS JOB + } - // Username, part of whoami source - register uid_t uid = geteuid(); - const char *username = getpwuid(uid)->pw_name; - cmd = QString("lp -U %1").arg(username); + + + +/* * 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: - cmd.append(" -o sides=one-sided"); + dest->num_options = cupsAddOption ("Duplex", + "None", + dest->num_options, + &(dest->options)); break; case 1: - cmd.append(" -o sides=two-sided-long-edge"); + dest->num_options = cupsAddOption ("Duplex", + "DuplexNoTumble", + dest->num_options, + &(dest->options)); break; case 2: - cmd.append(" -o sides=two-sided-long-edge"); + dest->num_options = cupsAddOption ("Duplex", + "DuplexTumble", + dest->num_options, + &(dest->options)); 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))); + { + dest->num_options = cupsAddOption ("copies", + ui->lineEditCopies->text().toAscii(), + dest->num_options, + &(dest->options)); + } - // File - cmd.append(QString(" -- \"%1\"").arg(file)); + // 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(); - // 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 index 0078cde..4b89262 100644 --- a/PrinterGUI/PrinterGUI_SRC/mainwindow.h +++ b/PrinterGUI/PrinterGUI_SRC/mainwindow.h @@ -16,7 +16,7 @@ class MainWindow : public QMainWindow Q_OBJECT public: - explicit MainWindow(char *argv, QWidget *parent = 0); + explicit MainWindow(char *argv[], QWidget *parent = 0); ~MainWindow(); private slots: @@ -29,6 +29,7 @@ private: void initializeUI(); cups_dest_t *dests; int num_dests; + const char * const user; const char * const file; }; diff --git a/PrinterGUI/PrinterGUI_SRC/mainwindow.ui b/PrinterGUI/PrinterGUI_SRC/mainwindow.ui index 1be9776..0a56de6 100644 --- a/PrinterGUI/PrinterGUI_SRC/mainwindow.ui +++ b/PrinterGUI/PrinterGUI_SRC/mainwindow.ui @@ -6,7 +6,7 @@ 0 0 - 559 + 574 286 @@ -19,6 +19,12 @@ + + false + + + true + 1 diff --git a/printerd b/printerd index ef93722..05a1052 100755 --- a/printerd +++ b/printerd @@ -4,13 +4,28 @@ # incoming data into UNIX lpd, which starts a helper program when a print- # job is incoming. -USER="$1" +USER="manuel" BUSYBOX="busybox" IP="0.0.0.0" PORT="515" SPOOLDIR="/var/spool" SPOOLQUEUE="STANDARD" -GUIBINARY="printerGUI" + + +# SHALL BE UNNECESSARY HENCE printerd RUNS IN USERCONTEXT +## If there are more than 1 seats open, quit +#if [ $(loginctl list-seats) != "seat0" ]; then +# exit +#fi +# +## Get the name(s) of the users physically using the machine +#NAME=$( loginctl | awk '$3 !~ /root/, $4 ~ /seat0/ { print $3 }' | sort -u ) +# +## Print error in case of multiple users +#if (( $(echo $NAME | wc -w ) > 1 )) ; then +# echo "[$0] To many users on this seat. Usernames: $NAME" > /tmp/debug-report/printer +# exit +#fi # Create the directory for the queue mkdir -p "$SPOOLDIR/$SPOOLQUEUE" @@ -20,7 +35,6 @@ mkdir -p "$SPOOLDIR/$SPOOLQUEUE" 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" +"$BUSYBOX" tcpsvd -Eu "$USER:$USER" "$IP" "$PORT" \ + "$BUSYBOX" lpd "$SPOOLDIR" sh -c 'printerGUI ms1144 $DATAFILE' & -- cgit v1.2.3-55-g7522