| 1 | #include "PrinterDialog.h" |
| 2 | #include "ui_PrinterDialog.h" |
| 3 | #include "PrinterItem.h" |
| 4 | #include "PrinterController.h" |
| 5 | #include "PagePrinter.h" |
| 6 | #include "Preferences.h" |
| 7 | #include "FieldTip.h" |
| 8 | #include "Stationery.h" |
| 9 | #include "Environment.h" |
| 10 | |
| 11 | #include <QMessageBox> |
| 12 | #include <QFileDialog> |
| 13 | #include <QCloseEvent> |
| 14 | #include <QMenu> |
| 15 | #include <QMovie> |
| 16 | #include <QDir> |
| 17 | #include <QFileInfo> |
| 18 | |
| 19 | |
| 20 | PrinterDialog::PrinterDialog(QWidget *parent, const QString &item) : |
| 21 | IpValidator(parent), |
| 22 | ui(new Ui::PrinterDialog), |
| 23 | mPrinterItem(NULL), |
| 24 | mListening(false) |
| 25 | { |
| 26 | ui->setupUi(this); |
| 27 | this->setWindowFlags(Qt::Window); |
| 28 | |
| 29 | QStringList stationeries = Preferences::getInstance().stationeryList(); |
| 30 | for (int i=0; i<stationeries.size(); i++) |
| 31 | ui->stationeryComboBox->addItem(stationeries[i]); |
| 32 | |
| 33 | QStringList decolations = Preferences::getInstance().decolationList(); |
| 34 | for (int i=0; i<decolations.size() ; i++) |
| 35 | ui->decolationComboBox->addItem(decolations[i]); |
| 36 | |
| 37 | ui->printerNameComboBox->insertItems(0,Preferences::getInstance().printerList()); |
| 38 | ui->printerNameComboBox->setCurrentIndex(0); |
| 39 | |
| 40 | if (item.length() > 0) |
| 41 | { |
| 42 | populate(item); |
| 43 | } |
| 44 | else |
| 45 | { |
| 46 | QString lastPrinter = Preferences::getInstance().lastPrinterName().c_str(); |
| 47 | if (lastPrinter.length() > 0) |
| 48 | { |
| 49 | ui->printerNameComboBox->setCurrentText(lastPrinter); |
| 50 | populate(lastPrinter); |
| 51 | } |
| 52 | else |
| 53 | { |
| 54 | ui->printerNameComboBox->setCurrentIndex(0); |
| 55 | populate(ui->printerNameComboBox->currentText()); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | connect(ui->DirButton, SIGNAL(clicked()), this, SLOT(dirClicked())); |
| 60 | connect(ui->cancelButton, SIGNAL(clicked()), this, SLOT(cancelClicked())); |
| 61 | connect(ui->deleteButton, SIGNAL(clicked()), this, SLOT(deleteClicked())); |
| 62 | connect(ui->connectButton, SIGNAL(clicked()), this, SLOT(connectClicked())); |
| 63 | connect(ui->printerNameComboBox, SIGNAL(currentTextChanged(QString)), this, SLOT(adjustPrinter(QString))); |
| 64 | connect(ui->printerNameComboBox, SIGNAL(editTextChanged(QString)), this, SLOT(printerNameChanged(QString))); |
| 65 | |
| 66 | QString path = Environment::getIconsPath().c_str(); |
| 67 | QIcon trayIcon(path + "/printerw.png"); |
| 68 | mSystemTrayIcon = new QSystemTrayIcon(trayIcon, this); |
| 69 | mSystrayHintEjected = false; |
| 70 | |
| 71 | QMenu *trayIconMenu = new QMenu(this); |
| 72 | QAction *restoreAction = new QAction(tr("&Hide"), this); |
| 73 | connect(restoreAction, SIGNAL(triggered()), this, SLOT(hideRestore())); |
| 74 | trayIconMenu->addAction(restoreAction); |
| 75 | mSystemTrayIcon->setContextMenu(trayIconMenu); |
| 76 | connect(mSystemTrayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(systrayClick(QSystemTrayIcon::ActivationReason))); |
| 77 | mSystemTrayIcon->setToolTip("Printer is connected"); |
| 78 | |
| 79 | mMovie = QSharedPointer<QMovie>(new QMovie(QString(Environment::getIconsPath().c_str()) + "/connecting.gif")); |
| 80 | ui->animatedGif->setMovie(mMovie.data()); |
| 81 | ui->animatedGif->setVisible(false); |
| 82 | ui->connectedImage->setVisible(false); |
| 83 | } |
| 84 | |
| 85 | PrinterDialog::~PrinterDialog() |
| 86 | { |
| 87 | delete ui; |
| 88 | if (!mController.isNull()) |
| 89 | { |
| 90 | mController->stop(); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | void PrinterDialog::populate(QString printerName) |
| 95 | { |
| 96 | PrinterItemPtr printer(Preferences::getInstance().printer(printerName)); |
| 97 | |
| 98 | ui->IP->setText(printer->mIp); |
| 99 | ui->PortNumber->setText(QString::number(printer->mPort)); |
| 100 | ui->stationeryComboBox->setCurrentText(printer->mStationery); |
| 101 | ui->lpiSpinBox->setValue(printer->mLinesPerInch); |
| 102 | ui->fontSizeSpinBox->setValue(printer->mFontSize); |
| 103 | ui->spoolDirectory->setText(printer->mSpoolDir); |
| 104 | ui->decolationComboBox->setCurrentText(printer->mDecolationName); |
| 105 | } |
| 106 | |
| 107 | void PrinterDialog::dirClicked() |
| 108 | { |
| 109 | QString dir = ui->spoolDirectory->text(); |
| 110 | if (dir.isEmpty()) dir = QDir::homePath()+"/Desktop"; |
| 111 | dir = QFileDialog::getExistingDirectory(this, |
| 112 | tr("Spool directory"), |
| 113 | dir, |
| 114 | QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks); |
| 115 | if (dir.length() > 0) ui->spoolDirectory->setText(dir); |
| 116 | } |
| 117 | |
| 118 | bool PrinterDialog::save(bool quiet) |
| 119 | { |
| 120 | if (validateIp(ui->IP, false, false) == false) return false; |
| 121 | |
| 122 | if ((ui->PortNumber->text().toUInt() > 0xffff) || |
| 123 | (ui->PortNumber->text().toUInt() == 0)) |
| 124 | { |
| 125 | if (!quiet) |
| 126 | { |
| 127 | QString msg(ui->PortNumber->text()); |
| 128 | msg += " is an ilegal port number"; |
| 129 | QMessageBox::warning(this, "Illegal port number", msg , QMessageBox::Ok); |
| 130 | } |
| 131 | return false; |
| 132 | } |
| 133 | |
| 134 | if (ui->printerNameComboBox->currentText().length() == 0) |
| 135 | { |
| 136 | if (!quiet) |
| 137 | { |
| 138 | QMessageBox::warning(this, "printer", "Please enter printer name", |
| 139 | QMessageBox::Ok, QMessageBox::NoButton); |
| 140 | FieldTip *balloon = new FieldTip(this, true); |
| 141 | balloon->setOwner(ui->printerNameComboBox); |
| 142 | balloon->showMessage("Give a name to this printer",2000); |
| 143 | ui->printerNameComboBox->setFocus(); |
| 144 | } |
| 145 | return false; |
| 146 | } |
| 147 | if (ui->printerNameComboBox->currentText().indexOf("\"") >= 0) |
| 148 | { |
| 149 | if (!quiet) |
| 150 | { |
| 151 | QMessageBox::warning(this, "printer", "Printer name cannot contain quotes", |
| 152 | QMessageBox::Ok, QMessageBox::NoButton); |
| 153 | ui->printerNameComboBox->setFocus(); |
| 154 | } |
| 155 | return false; |
| 156 | } |
| 157 | mPrinterItem = PrinterItemPtr(new PrinterItem()); |
| 158 | mPrinterItem->mName = ui->printerNameComboBox->currentText(); |
| 159 | mPrinterItem->mIp = ui->IP->text(); |
| 160 | mPrinterItem->mPort = ui->PortNumber->text().toInt(); |
| 161 | mPrinterItem->mStationery = ui->stationeryComboBox->currentText(); |
| 162 | mPrinterItem->mLinesPerInch = ui->lpiSpinBox->value(); |
| 163 | mPrinterItem->mFontSize = ui->fontSizeSpinBox->value(); |
| 164 | mPrinterItem->mSpoolDir = ui->spoolDirectory->text(); |
| 165 | mPrinterItem->mDecolationName = ui->decolationComboBox->currentText(); |
| 166 | Preferences::getInstance().setPrinter(ui->printerNameComboBox->currentText(), *mPrinterItem); |
| 167 | return true; |
| 168 | } |
| 169 | |
| 170 | void PrinterDialog::deleteClicked() |
| 171 | { |
| 172 | Preferences::getInstance().deletePrinter(ui->printerNameComboBox->currentText()); |
| 173 | ui->printerNameComboBox->clear(); |
| 174 | ui->printerNameComboBox->insertItems(0,Preferences::getInstance().printerList()); |
| 175 | ui->printerNameComboBox->setCurrentIndex(0); |
| 176 | } |
| 177 | |
| 178 | void PrinterDialog::cancelClicked() |
| 179 | { |
| 180 | stop(); |
| 181 | if (save(true)) |
| 182 | { |
| 183 | Preferences::getInstance().setLastPrinterName(ui->printerNameComboBox->currentText().toStdString()); |
| 184 | } |
| 185 | deleteLater(); |
| 186 | } |
| 187 | |
| 188 | void PrinterDialog::stop() |
| 189 | { |
| 190 | if (mController != NULL) |
| 191 | mController->stop(); |
| 192 | |
| 193 | } |
| 194 | |
| 195 | void PrinterDialog::adjustPrinter(QString name) |
| 196 | { |
| 197 | if (Preferences::getInstance().printerList().indexOf(name) >= 0) |
| 198 | { |
| 199 | populate(name); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | void PrinterDialog::printerNameChanged(QString) |
| 204 | { |
| 205 | |
| 206 | } |
| 207 | |
| 208 | void PrinterDialog::connectClicked() |
| 209 | { |
| 210 | if (mListening) // disconnect |
| 211 | { |
| 212 | if (!mController.isNull()) |
| 213 | { |
| 214 | mController->stop(); |
| 215 | } |
| 216 | } |
| 217 | else |
| 218 | { |
| 219 | if (!testDirectory(ui->spoolDirectory->text())) return; |
| 220 | if (!save(false)) return ; |
| 221 | if (mPrinterItem == NULL) |
| 222 | mPrinterItem = PrinterItemPtr(new PrinterItem()); |
| 223 | mPrinterItem->mName = ui->printerNameComboBox->currentText(); |
| 224 | mPrinterItem->mIp = ui->IP->text(); |
| 225 | mPrinterItem->mPort = ui->PortNumber->text().toInt(); |
| 226 | mPrinterItem->mStationery = ui->stationeryComboBox->currentText(); |
| 227 | mPrinterItem->mLinesPerInch = ui->lpiSpinBox->value(); |
| 228 | mPrinterItem->mFontSize = ui->fontSizeSpinBox->value(); |
| 229 | mPrinterItem->mSpoolDir = ui->spoolDirectory->text(); |
| 230 | mPrinterItem->mDecolationName = ui->decolationComboBox->currentText(); |
| 231 | |
| 232 | StationeryPtr stationery(Preferences::getInstance().stationery(mPrinterItem->mStationery)); |
| 233 | DecolationRulesPtr decolation(Preferences::getInstance().decolation(mPrinterItem->mDecolationName)); |
| 234 | |
| 235 | mPrinter = PdfPrinterPtr(new PdfPrinter(ui->spoolDirectory->text(), stationery, decolation, mPrinterItem->mLinesPerInch, mPrinterItem->mFontSize)); |
| 236 | if (!mController.isNull()) |
| 237 | { |
| 238 | mController->stop(); |
| 239 | } |
| 240 | else |
| 241 | { |
| 242 | mController = PrinterControllerPtr(new PrinterController(mPrinterItem, mPrinter)); |
| 243 | connect(mController.data(), SIGNAL(connected()), this, SLOT(connected())); |
| 244 | connect(mController.data(), SIGNAL(waiting()), this, SLOT(waiting())); |
| 245 | connect(mController.data(), SIGNAL(stoppedWaiting()), this, SLOT(stoppedWaiting())); |
| 246 | } |
| 247 | mController->exec(); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | void PrinterDialog::connected() |
| 252 | { |
| 253 | ui->connectedLabel->setText("Connected"); |
| 254 | ui->animatedGif->setVisible(false); |
| 255 | ui->connectedImage->setVisible(true); |
| 256 | mMovie->stop(); |
| 257 | mSystemTrayIcon->setVisible(true); |
| 258 | setProtected(true); |
| 259 | } |
| 260 | |
| 261 | void PrinterDialog::waiting() |
| 262 | { |
| 263 | ui->connectButton->setText("Disconnect"); |
| 264 | ui->connectedLabel->setText("Waiting..."); |
| 265 | mListening = true; |
| 266 | ui->connectedImage->setVisible(false); |
| 267 | mMovie->start(); |
| 268 | ui->animatedGif->setVisible(true); |
| 269 | setProtected(true); |
| 270 | } |
| 271 | |
| 272 | void PrinterDialog::stoppedWaiting() |
| 273 | { |
| 274 | ui->connectButton->setText("Connect"); |
| 275 | ui->connectedLabel->setText("Disconnected"); |
| 276 | mListening = false; |
| 277 | ui->connectedImage->setVisible(false); |
| 278 | ui->animatedGif->setVisible(false); |
| 279 | mMovie->stop(); |
| 280 | setProtected(false); |
| 281 | } |
| 282 | |
| 283 | bool PrinterDialog::isConnected() |
| 284 | { |
| 285 | return (ui->connectedLabel->text().compare("Connected") == 0); |
| 286 | } |
| 287 | |
| 288 | void PrinterDialog::closeEvent(QCloseEvent *event) |
| 289 | { |
| 290 | if (isConnected()) |
| 291 | { |
| 292 | hideRestore(); |
| 293 | event->ignore(); |
| 294 | return; |
| 295 | } |
| 296 | if (!save(false)) |
| 297 | { |
| 298 | event->ignore(); |
| 299 | return; |
| 300 | } |
| 301 | deleteLater(); |
| 302 | } |
| 303 | |
| 304 | void PrinterDialog::hideRestore() |
| 305 | { |
| 306 | if (isVisible()) |
| 307 | { |
| 308 | setVisible(false); |
| 309 | systrayHint(); |
| 310 | mSystemTrayIcon->contextMenu()->actions().at(0)->setText("Restore"); |
| 311 | |
| 312 | } |
| 313 | else |
| 314 | { |
| 315 | setVisible(true); |
| 316 | mSystemTrayIcon->contextMenu()->actions().at(0)->setText("Hide"); |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | void PrinterDialog::systrayClick(QSystemTrayIcon::ActivationReason) |
| 321 | { |
| 322 | hideRestore(); |
| 323 | } |
| 324 | |
| 325 | void PrinterDialog::systrayHint() |
| 326 | { |
| 327 | if (!mSystrayHintEjected) |
| 328 | { |
| 329 | mSystemTrayIcon->showMessage("The printer is still connected and operational","Click the Printer icon to restore its dialog",QSystemTrayIcon::Information,5000); |
| 330 | mSystemTrayIcon->setToolTip("Click on this icon to restore the dialog"); |
| 331 | mSystrayHintEjected = true; |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | void PrinterDialog::setProtected(bool protect) |
| 336 | { |
| 337 | ui->printerNameComboBox->setEnabled(!protect); |
| 338 | ui->IP->setEnabled(!protect); |
| 339 | ui->PortNumber->setEnabled(!protect); |
| 340 | ui->stationeryComboBox->setEnabled(!protect); |
| 341 | ui->lpiSpinBox->setEnabled(!protect); |
| 342 | ui->fontSizeSpinBox->setEnabled(!protect); |
| 343 | ui->spoolDirectory->setEnabled(!protect); |
| 344 | ui->decolationComboBox->setEnabled(!protect); |
| 345 | ui->deleteButton->setEnabled(!protect); |
| 346 | } |
| 347 | |
| 348 | bool PrinterDialog::testDirectory(QString dirName) |
| 349 | { |
| 350 | QDir dir(dirName); |
| 351 | if (!dir.exists()) |
| 352 | { |
| 353 | QString msg("Directory \"" + dirName); |
| 354 | msg += "\" does not exist"; |
| 355 | QMessageBox::warning(this, "non-existing directory", msg , QMessageBox::Ok); |
| 356 | ui->spoolDirectory->setFocus(); |
| 357 | return false; |
| 358 | } |
| 359 | QFileInfo fileInfo(dirName); |
| 360 | if (!fileInfo.isDir()) |
| 361 | { |
| 362 | QString msg("\"" + dirName); |
| 363 | msg += "\" is not a directory"; |
| 364 | QMessageBox::warning(this, "invalid directory", msg , QMessageBox::Ok); |
| 365 | ui->spoolDirectory->setFocus(); |
| 366 | return false; |
| 367 | |
| 368 | } |
| 369 | if (!fileInfo.isWritable()) |
| 370 | { |
| 371 | QString msg("You do not have permission to write into directory \"" + dirName); |
| 372 | msg += "\""; |
| 373 | QMessageBox::warning(this, "invalid directory", msg , QMessageBox::Ok); |
| 374 | ui->spoolDirectory->setFocus(); |
| 375 | return false; |
| 376 | |
| 377 | } |
| 378 | return true; |
| 379 | } |