1 /**************************************************************************
5 Unix Remote Printing Operation
6 Copyright (c) Arto Hyvättinen 2010
8 This file is part of URPO.
10 URPO is free software: you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation, either version 3 of the License, or
13 (at your option) any later version.
15 URPO is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
21 **************************************************************************/
23 #include "urpomainwindow.h"
26 #include "urpoconnectionsettings.h"
27 #include "printerlistjob.h"
28 #include "debugconsole.h"
29 #include "printwidget.h"
33 #include <QMessageBox>
34 #include <QApplication>
35 #include <QTextBrowser>
42 #include "settingsdialog.h"
44 #define VERSION "0.9.2" /*! Program version */
46 UrpoMainWindow::UrpoMainWindow(QWidget *parent)
49 setWindowTitle(QString("URPO ") + VERSION );
50 setWindowIcon( QIcon(":/urpo.png"));
52 // Load connection settings
53 settings_ = new UrpoConnectionSettings("Urpo","Urpo");
56 // Connect to debug monitor
57 monitor_ = new DebugConsole();
58 settings_->setDebugMonitor(monitor_);
60 // Init central widget
61 printWidget_ = new PrintWidget;
62 setCentralWidget(printWidget_);
70 connect( printWidget_, SIGNAL(reconnect()), this, SLOT(getPrinters()));
71 connect( printWidget_, SIGNAL(print(QString,QString)), this, SLOT(print(QString,QString)));
75 // Init printers list job to get printers list
76 printerListJob_ = new PrinterListJob( settings_);
77 // Connect finished to printersReceives:
78 // - if success, enable printing
79 // - if fail, go to Settings dialog
80 connect( printerListJob_, SIGNAL(finished(bool,QString)), this, SLOT(printersReceived(bool,QString)));
83 printJob_ = new PrintJob(settings_);
84 connect( printJob_, SIGNAL(finished(bool,QString)), this, SLOT(printFinished(bool,QString)));
86 if( settings_->getHost().isEmpty())
87 // If no host settings, go first to settings
90 // Try to get printers list
96 UrpoMainWindow::~UrpoMainWindow()
101 void UrpoMainWindow::getPrinters()
104 printWidget_->setStatus(tr("Connecting..."),true);
105 connect(printWidget_, SIGNAL(cancel()), printerListJob_, SLOT(cancel()));
106 printerListJob_->start();
111 void UrpoMainWindow::initMenu()
114 QAction* settingsAction = new QAction( tr("Settings"), this);
115 connect(settingsAction, SIGNAL(triggered()), this, SLOT(settings()) );
116 menuBar()->addAction(settingsAction);
118 QAction* debugAction = new QAction( tr("Debug"), this);
119 debugAction->setStatusTip(tr("Open debug console"));
120 connect(debugAction, SIGNAL(triggered()), this, SLOT(debugWindow() ));
121 menuBar()->addAction(debugAction);
123 QAction* aboutAction = new QAction( tr("About"), this);
124 connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
125 menuBar()->addAction(aboutAction);
127 QAction* aboutQtAction = new QAction( tr("About Qt"), this );
128 connect(aboutQtAction, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
129 menuBar()->addAction(aboutQtAction);
132 QAction* helpAction = new QAction( tr("Help"), this );
133 connect( helpAction, SIGNAL(triggered()), this, SLOT(helpWindow()));
134 menuBar()->addAction(helpAction);
138 void UrpoMainWindow::initHelp()
144 helpBrowser_ = new QTextBrowser();
145 helpBrowser_->setWindowTitle(tr("Urpo Help"));
148 // Try to load locale version index_fi etc.
150 // using LANG enviroment variable instead of QLocale because of Qt on Maemo problem
152 QString language = std::getenv("LANG");
154 QString helpfilename = QString(":/help/index_") + language.left(2).toLower() + QString(".html");
156 QFile helpfile( helpfilename );
157 if( helpfile.exists() )
158 helpBrowser_->setSource(QUrl( QString("qrc") + helpfilename ));
160 // Not find, load general
161 helpBrowser_->setSource(QUrl("qrc:/help/index.html"));
163 helpBrowser_->setOpenExternalLinks(true);
167 void UrpoMainWindow::printersReceived(bool success, QString error)
169 // Disconnect cancel button out of printerListJob
170 disconnect(printWidget_, SIGNAL(cancel()), printerListJob_, SLOT(cancel()));
173 // PrinterListJob successed
174 printWidget_->setPrinters( printerListJob_->getPrinters());
175 if( printerListJob_->getPrinters().isEmpty())
177 // No printers, can't print
178 printWidget_->setStatus( QString("<font color=red>") + tr("No printers found") + QString("</font>"),false);
179 printWidget_->setReady(false);
184 printWidget_->setReady( true );
191 printWidget_->setStatus( QString("<font color=red>") + error + QString("</font>") );
192 printWidget_->setReady(false);
196 void UrpoMainWindow::about()
198 QMessageBox::about(this, tr("About Urpo"),
199 tr("<b>Unix Remote Printing Operation %1 </b>"
200 "<p>Copyright © Arto Hyvättinen 2010"
201 "<p>License: General Public License v3"
205 void UrpoMainWindow::settings()
207 printWidget_->doCancel(); // Cancel current process
209 SettingsDialog* dialog = new SettingsDialog(this);
210 dialog->setSettings(settings_);
211 dialog->setHelp(helpBrowser_);
214 // Dialog done -> get printers
215 connect( dialog, SIGNAL(accepted()), this, SLOT(getPrinters()));
219 void UrpoMainWindow::debugWindow()
223 monitor_->activateWindow();
226 void UrpoMainWindow::helpWindow()
230 helpBrowser_->home();
231 helpBrowser_->show();
232 helpBrowser_->raise();
233 helpBrowser_->activateWindow();
236 void UrpoMainWindow::print(QString file, QString options)
238 printWidget_->setStatus( tr("Printing..."), true );
239 connect( printWidget_, SIGNAL(cancel()), printJob_, SLOT(cancel()));
240 printJob_->printFile(file,options);
243 void UrpoMainWindow::printFinished(bool success, QString errorStr)
245 disconnect( printWidget_, SIGNAL(cancel()), printJob_, SLOT(cancel()));
246 printWidget_->setReady(true); // Ready to print again!
247 if( success == false ) // Error -- set error message!
249 printWidget_->setReady(true);
250 printWidget_->setStatus( QString("<font color=red>") + errorStr + QString("</font>"));