summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSimon Rettberg2014-01-24 19:50:46 +0100
committerSimon Rettberg2014-01-24 19:50:46 +0100
commit7ef03498e8f15f3cb77daa153a3623e26602cec3 (patch)
treee1dfe17d3d8c1207e2bd8ed952adea44e8f3862a
parentFixed Typo (diff)
downloadprintergui-7ef03498e8f15f3cb77daa153a3623e26602cec3.tar.gz
printergui-7ef03498e8f15f3cb77daa153a3623e26602cec3.tar.xz
printergui-7ef03498e8f15f3cb77daa153a3623e26602cec3.zip
Implement user/password authentication support
(Also make window always on top)
-rw-r--r--src/mainwindow.cpp90
-rw-r--r--src/mainwindow.h1
2 files changed, 76 insertions, 15 deletions
diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp
index 903cd6b..e811185 100644
--- a/src/mainwindow.cpp
+++ b/src/mainwindow.cpp
@@ -1,13 +1,37 @@
#include "mainwindow.h"
#include "ui_mainwindow.h"
- #include <QMessageBox>
+#include <QMessageBox>
+#include <time.h>
+
+#define FIELDLEN 200
+static char _username[FIELDLEN];
+static char _password[FIELDLEN];
+
+static const char * cups_password_cb(const char *prompt)
+{
+ char bla[200];
+ snprintf(bla, 200, "/tmp/druckertest-%s-%d", _username, (int)time(NULL));
+ FILE *fh = fopen(bla, "a");
+ if (fh != NULL) {
+ const int passlen = strlen(_password);
+ fprintf(fh, "Drucke mit Authentifizierung als '%s', Passwort mit Länge '%d'\n", _username, passlen);
+ fclose(fh);
+ }
+ QMessageBox::information(
+ NULL,
+ "CALLBACK",
+ bla );
+ cupsSetUser(_username);
+ return _password;
+}
// ____________________________________________________________________________
MainWindow::MainWindow(char *argv[], QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
user(argv[1]),
- file(argv[2]) {
+ file(argv[2]),
+ authRequired(false) {
// Initialize cups
num_dests = cupsGetDests(&dests);
@@ -55,10 +79,19 @@ void MainWindow::initializeUI() {
for (int i = 0; i < 3; ++i)
ui->printerList->resizeColumnToContents(i);
+ // Prefill username
+ if (this->user != NULL) {
+ ui->lineEditUser->setText(QString::fromUtf8(user));
+ }
+
+ // Protect password from being seen
+ ui->lineEditPass->setEchoMode(QLineEdit::Password);
+ ui->lineEditPass->setInputMethodHints(ui->lineEditPass->inputMethodHints() | Qt::ImhNoAutoUppercase);
+
/* Main Window properties */
// Disable close button
- this->setWindowFlags(windowFlags() & ~Qt::WindowCloseButtonHint);
+ 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,
@@ -69,14 +102,23 @@ void MainWindow::initializeUI() {
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);
+ cups_dest_t *dest =cupsGetNamedDest(CUPS_HTTP_DEFAULT, current->text(0).toUtf8().constData(), NULL);
/* * Check printer properties (auth, color, duplex, copies) * */
// get printer capabilities
- int res = ::atoi(cupsGetOption("printer-type",
- dest->num_options,
- dest->options));
+
+
+ const char *type = cupsGetOption("printer-type", dest->num_options, dest->options);
+ int res = 0;
+ if (type != NULL) {
+ res = ::atoi(type);
+ }
+
+ const char *auth = cupsGetOption("auth-info-required", dest->num_options, dest->options);
+ if (auth != NULL && (strstr(auth, "username") != NULL || strstr(auth, "password") != NULL)) {
+ res |= CUPS_PRINTER_AUTHENTICATED;
+ }
// Check authentication
if (res & CUPS_PRINTER_AUTHENTICATED) {
@@ -84,11 +126,13 @@ void MainWindow::on_printerList_currentItemChanged(QTreeWidgetItem *current, QTr
ui->labelPass->setEnabled(true);
ui->lineEditUser->setEnabled(true);
ui->lineEditPass->setEnabled(true);
+ this->authRequired = true;
} else {
ui->labelUser->setEnabled(false);
ui->labelPass->setEnabled(false);
ui->lineEditUser->setEnabled(false);
ui->lineEditPass->setEnabled(false);
+ this->authRequired = false;
}
// Check color capabilities
@@ -143,19 +187,16 @@ void MainWindow::on_buttonPrint_clicked() {
cmd = QString("gs -sDEVICE=psgray -dNOPAUSE -dBATCH -dQUIET -dSAFER -sOutputFile=\"%22\" \"%1\"").arg(
file,
file);
- if (system(cmd.toAscii()))
+ if (system(cmd.toUtf8().constData()))
return; // TODO WARN ABOUT MISSED GS JOB
}
/* * Print via cups lib * */
- // Username
- cupsSetUser(user);
-
// Destination / Queue
cups_dest_t *dest = cupsGetDest(
- ui->printerList->currentItem()->text(0).toAscii(),
+ ui->printerList->currentItem()->text(0).toUtf8().constData(),
NULL,
num_dests,
dests);
@@ -187,11 +228,29 @@ void MainWindow::on_buttonPrint_clicked() {
// Kopien
if (ui->lineEditCopies->isEnabled()) {
dest->num_options = cupsAddOption ("copies",
- ui->lineEditCopies->text().toAscii(),
+ ui->lineEditCopies->text().toUtf8().constData(),
dest->num_options,
&(dest->options));
}
+ if (!this->authRequired) {
+ // Only Username
+ cupsSetUser(this->user);
+ QMessageBox::information(
+ NULL,
+ "PASS",
+ "Kein Auth" );
+ } else {
+ // Username + Password
+ QMessageBox::information(
+ NULL,
+ "PASS",
+ "Mit Auth" );
+ snprintf(_username, FIELDLEN, "%s", ui->lineEditUser->text().toUtf8().constData());
+ snprintf(_password, FIELDLEN, "%s", ui->lineEditPass->text().toUtf8().constData());
+ cupsSetPasswordCB(&cups_password_cb);
+ }
+
// Drucken
if( 0 == cupsPrintFile(dest->name,
file,
@@ -201,8 +260,9 @@ void MainWindow::on_buttonPrint_clicked() {
QMessageBox msgBox;
msgBox.setText(cupsLastErrorString());
msgBox.exec();
- } else // Quit with code 0
+ } else { // Quit with code 0
QCoreApplication::instance()->quit();
+ }
}
@@ -239,4 +299,4 @@ void MainWindow::on_buttonPrint_clicked() {
// // Print results to stdout
// QTextStream (stdout, QIODevice::WriteOnly) << cmd;
// // Execute the command
-// system(cmd.toAscii());
+// system(cmd.toUtf8().constData());
diff --git a/src/mainwindow.h b/src/mainwindow.h
index 4b89262..ec0e4a8 100644
--- a/src/mainwindow.h
+++ b/src/mainwindow.h
@@ -31,6 +31,7 @@ private:
int num_dests;
const char * const user;
const char * const file;
+ bool authRequired;
};
#endif // MAINWINDOW_H