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" /*! 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 monitor_->debugMessage(helpfilename);
158 QFile helpfile( helpfilename );
159 if( helpfile.exists() )
160 helpBrowser_->setSource(QUrl( QString("qrc") + helpfilename ));
162 // Not find, load general
163 helpBrowser_->setSource(QUrl("qrc:/help/index.html"));
165 helpBrowser_->setOpenExternalLinks(true);
169 void UrpoMainWindow::printersReceived(bool success, QString error)
171 // Disconnect cancel button out of printerListJob
172 disconnect(printWidget_, SIGNAL(cancel()), printerListJob_, SLOT(cancel()));
175 // PrinterListJob successed
176 printWidget_->setPrinters( printerListJob_->getPrinters());
177 if( printerListJob_->getPrinters().isEmpty())
179 // No printers, can't print
180 printWidget_->setStatus( QString("<font color=red>") + tr("No printers found") + QString("</font>"),false);
181 printWidget_->setReady(false);
186 printWidget_->setReady( true );
193 printWidget_->setStatus( QString("<font color=red>") + error + QString("</font>") );
194 printWidget_->setReady(false);
198 void UrpoMainWindow::about()
200 QMessageBox::about(this, tr("About Urpo"),
201 tr("<b>Unix Remote Printing Operation %1 </b>"
202 "<p>Copyright © Arto Hyvättinen 2010"
203 "<p>License: General Public License v3"
207 void UrpoMainWindow::settings()
209 printWidget_->doCancel(); // Cancel current process
211 SettingsDialog* dialog = new SettingsDialog(this);
212 dialog->setSettings(settings_);
213 dialog->setHelp(helpBrowser_);
216 // Dialog done -> get printers
217 connect( dialog, SIGNAL(accepted()), this, SLOT(getPrinters()));
221 void UrpoMainWindow::debugWindow()
225 monitor_->activateWindow();
228 void UrpoMainWindow::helpWindow()
232 helpBrowser_->home();
233 helpBrowser_->show();
234 helpBrowser_->raise();
235 helpBrowser_->activateWindow();
238 void UrpoMainWindow::print(QString file, QString options)
240 printWidget_->setStatus( tr("Printing..."), true );
241 connect( printWidget_, SIGNAL(cancel()), printJob_, SLOT(cancel()));
242 printJob_->printFile(file,options);
245 void UrpoMainWindow::printFinished(bool success, QString errorStr)
247 disconnect( printWidget_, SIGNAL(cancel()), printJob_, SLOT(cancel()));
248 printWidget_->setReady(true); // Ready to print again!
249 if( success == false ) // Error -- set error message!
251 printWidget_->setReady(true);
252 printWidget_->setStatus( QString("<font color=red>") + errorStr + QString("</font>"));