summaryrefslogtreecommitdiffstats
path: root/src/mainwindow.cpp
blob: e8111850efdef21e881cc3f392340bdc75f86812 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#include "mainwindow.h"
#include "ui_mainwindow.h"
#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]),
    authRequired(false) {
  // 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);

  // 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((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 MainWindow::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);
  }

  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) {
    ui->labelUser->setEnabled(true);
    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
  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.toUtf8().constData()))
      return; // TODO WARN ABOUT MISSED GS JOB

  }

  /* * 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));
  }

  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,
                         NULL,
                         dest->num_options,
                         dest->options)) {
    QMessageBox msgBox;
    msgBox.setText(cupsLastErrorString());
    msgBox.exec();
  } else { // Quit with code 0
    QCoreApplication::instance()->quit();
  }

}


///////////////////////////////  CODE PAPIERKORB  /////////////////////////////

/* * 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.toUtf8().constData());