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>
39 #include "settingsdialog.h"
41 #define VERSION "0.9" /*! Program version */
43 UrpoMainWindow::UrpoMainWindow(QWidget *parent)
46 setWindowTitle(QString("URPO ") + VERSION );
48 // Load connection settings
49 settings_ = new UrpoConnectionSettings("Urpo","Urpo");
52 // Connect to debug monitor
53 monitor_ = new DebugConsole();
54 settings_->setDebugMonitor(monitor_);
56 // Init central widget
57 printWidget_ = new PrintWidget;
58 setCentralWidget(printWidget_);
66 connect( printWidget_, SIGNAL(reconnect()), this, SLOT(getPrinters()));
67 connect( printWidget_, SIGNAL(print(QString,QString)), this, SLOT(print(QString,QString)));
71 // Init printers list job to get printers list
72 printerListJob_ = new PrinterListJob( settings_);
73 // Connect finished to printersReceives:
74 // - if success, enable printing
75 // - if fail, go to Settings dialog
76 connect( printerListJob_, SIGNAL(finished(bool,QString)), this, SLOT(printersReceived(bool,QString)));
79 printJob_ = new PrintJob(settings_);
80 connect( printJob_, SIGNAL(finished(bool,QString)), this, SLOT(printFinished(bool,QString)));
82 if( settings_->getHost().isEmpty())
83 // If no host settings, go first to settings
86 // Try to get printers list
92 UrpoMainWindow::~UrpoMainWindow()
97 void UrpoMainWindow::getPrinters()
100 printWidget_->setStatus(tr("Connecting..."),true);
101 connect(printWidget_, SIGNAL(cancel()), printerListJob_, SLOT(cancel()));
102 printerListJob_->start();
107 void UrpoMainWindow::initMenu()
110 QAction* settingsAction = new QAction( tr("Settings"), this);
111 connect(settingsAction, SIGNAL(triggered()), this, SLOT(settings()) );
112 menuBar()->addAction(settingsAction);
114 QAction* debugAction = new QAction( tr("Debug"), this);
115 debugAction->setStatusTip(tr("Open debug console"));
116 connect(debugAction, SIGNAL(triggered()), this, SLOT(debugWindow() ));
117 menuBar()->addAction(debugAction);
119 QAction* aboutAction = new QAction( tr("About"), this);
120 connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
121 menuBar()->addAction(aboutAction);
123 QAction* aboutQtAction = new QAction( tr("About Qt"), this );
124 connect(aboutQtAction, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
125 menuBar()->addAction(aboutQtAction);
128 QAction* helpAction = new QAction( tr("Help"), this );
129 connect( helpAction, SIGNAL(triggered()), this, SLOT(helpWindow()));
130 menuBar()->addAction(helpAction);
134 void UrpoMainWindow::initHelp()
138 helpBrowser_ = new QTextBrowser();
139 helpBrowser_->setWindowTitle(tr("Urpo Help"));
142 // Try to load locale version index_fi etc.
143 QString language=QLocale::system().name().left(2);
144 QString helpfilename = QString(":/help/index_") + language + QString(".html");
146 QFile helpfile( helpfilename );
147 if( helpfile.exists() )
148 helpBrowser_->setSource(QUrl( QString("qrc") + helpfilename ));
150 // Not find, load general
151 helpBrowser_->setSource(QUrl("qrc:/help/index.html"));
153 helpBrowser_->setOpenExternalLinks(true);
157 void UrpoMainWindow::printersReceived(bool success, QString error)
159 // Disconnect cancel button out of printerListJob
160 disconnect(printWidget_, SIGNAL(cancel()), printerListJob_, SLOT(cancel()));
163 // PrinterListJob successed
164 printWidget_->setPrinters( printerListJob_->getPrinters());
165 if( printerListJob_->getPrinters().isEmpty())
167 // No printers, can't print
168 printWidget_->setStatus( QString("<font color=red>") + tr("No printers found") + QString("</font>"),false);
169 printWidget_->setReady(false);
174 printWidget_->setReady( true );
181 printWidget_->setStatus( QString("<font color=red>") + error + QString("</font>") );
182 printWidget_->setReady(false);
186 void UrpoMainWindow::about()
188 QMessageBox::about(this, tr("About Urpo"),
189 tr("<b>Unix Remote Printing Operation %1 </b>"
190 "<p>Copyright © Arto Hyvättinen 2010"
191 "<p>License: General Public License v3"
195 void UrpoMainWindow::settings()
197 printWidget_->doCancel(); // Cancel current process
199 SettingsDialog* dialog = new SettingsDialog(this);
200 dialog->setSettings(settings_);
201 dialog->setHelp(helpBrowser_);
204 // Dialog done -> get printers
205 connect( dialog, SIGNAL(accepted()), this, SLOT(getPrinters()));
209 void UrpoMainWindow::debugWindow()
213 monitor_->activateWindow();
216 void UrpoMainWindow::helpWindow()
220 helpBrowser_->home();
221 helpBrowser_->show();
222 helpBrowser_->raise();
223 helpBrowser_->activateWindow();
226 void UrpoMainWindow::print(QString file, QString options)
228 printWidget_->setStatus( tr("Printing..."), true );
229 connect( printWidget_, SIGNAL(cancel()), printJob_, SLOT(cancel()));
230 printJob_->printFile(file,options);
233 void UrpoMainWindow::printFinished(bool success, QString errorStr)
235 disconnect( printWidget_, SIGNAL(cancel()), printJob_, SLOT(cancel()));
236 printWidget_->setReady(true); // Ready to print again!
237 if( success == false ) // Error -- set error message!
239 printWidget_->setReady(true);
240 printWidget_->setStatus( QString("<font color=red>") + errorStr + QString("</font>"));