Added program's help files to www pages and a ling to the english help file to the...
[urpo] / src / urpomainwindow.cpp
1 /**************************************************************************
2
3     URPO
4
5     Unix Remote Printing Operation
6     Copyright (c) Arto Hyvättinen 2010
7
8     This file is part of URPO.
9
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.
14
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.
19
20
21 **************************************************************************/
22
23 #include "urpomainwindow.h"
24
25
26 #include "urpoconnectionsettings.h"
27 #include "printerlistjob.h"
28 #include "debugconsole.h"
29 #include "printwidget.h"
30 #include "printjob.h"
31
32 #include <QMenuBar>
33 #include <QMessageBox>
34 #include <QApplication>
35 #include <QTextBrowser>
36 #include <QUrl>
37 #include <QLocale>
38 #include <QFile>
39 #include "settingsdialog.h"
40
41 #define VERSION "0.9"   /*! Program version */
42
43 UrpoMainWindow::UrpoMainWindow(QWidget *parent)
44     : QMainWindow(parent)
45 {
46     setWindowTitle(QString("URPO ") + VERSION );
47
48     // Load connection settings
49     settings_ = new UrpoConnectionSettings("Urpo","Urpo");
50     settings_->load();
51
52     // Connect to debug monitor
53     monitor_ = new DebugConsole();
54     settings_->setDebugMonitor(monitor_);
55
56     // Init central widget
57     printWidget_ = new PrintWidget;
58     setCentralWidget(printWidget_);
59
60     // Init Help
61     initHelp();
62     // Init menu
63     initMenu();
64
65     // Connect buttons
66     connect( printWidget_, SIGNAL(reconnect()), this, SLOT(getPrinters()));
67     connect( printWidget_, SIGNAL(print(QString,QString)), this, SLOT(print(QString,QString)));
68
69     // Init jobs
70
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)));
77
78     // Init print job
79     printJob_ = new PrintJob(settings_);
80     connect( printJob_, SIGNAL(finished(bool,QString)), this, SLOT(printFinished(bool,QString)));
81
82     if( settings_->getHost().isEmpty())
83         // If no host settings, go first to settings
84         settings();
85     else
86         // Try to get printers list
87         getPrinters();
88
89
90 }
91
92 UrpoMainWindow::~UrpoMainWindow()
93 {
94
95 }
96
97 void UrpoMainWindow::getPrinters()
98 {
99
100     printWidget_->setStatus(tr("Connecting..."),true);
101     connect(printWidget_, SIGNAL(cancel()), printerListJob_, SLOT(cancel()));
102     printerListJob_->start();
103
104 }
105
106
107 void UrpoMainWindow::initMenu()
108 {
109
110     QAction* settingsAction = new QAction( tr("Settings"), this);
111     connect(settingsAction, SIGNAL(triggered()), this, SLOT(settings()) );
112     menuBar()->addAction(settingsAction);
113
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);
118
119     QAction* aboutAction = new QAction( tr("About"), this);
120     connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
121     menuBar()->addAction(aboutAction);
122
123     QAction* aboutQtAction = new QAction( tr("About Qt"), this );
124     connect(aboutQtAction, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
125     menuBar()->addAction(aboutQtAction);
126
127
128     QAction* helpAction = new QAction( tr("Help"), this );
129     connect( helpAction, SIGNAL(triggered()), this, SLOT(helpWindow()));
130     menuBar()->addAction(helpAction);
131
132 }
133
134 void UrpoMainWindow::initHelp()
135 {
136
137     // Init help
138     helpBrowser_ = new QTextBrowser();
139     helpBrowser_->setWindowTitle(tr("Urpo Help"));
140
141     // Load help file
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");
145
146     QFile helpfile( helpfilename );
147     if( helpfile.exists() )
148         helpBrowser_->setSource(QUrl( QString("qrc") + helpfilename  ));
149     else
150         // Not find, load general
151         helpBrowser_->setSource(QUrl("qrc:/help/index.html"));
152
153     helpBrowser_->setOpenExternalLinks(true);
154
155 }
156
157 void UrpoMainWindow::printersReceived(bool success, QString error)
158 {
159     // Disconnect cancel button out of printerListJob
160     disconnect(printWidget_, SIGNAL(cancel()), printerListJob_, SLOT(cancel()));
161     if(success)
162     {
163         // PrinterListJob successed
164         printWidget_->setPrinters( printerListJob_->getPrinters());
165         if( printerListJob_->getPrinters().isEmpty())
166         {
167             // No printers, can't print
168             printWidget_->setStatus( QString("<font color=red>") + tr("No printers found") + QString("</font>"),false);
169             printWidget_->setReady(false);
170         }
171         else
172         {
173             // Ready to print
174             printWidget_->setReady( true );
175         }
176
177     }
178     else
179     {
180         // Unsuccess!
181         printWidget_->setStatus(  QString("<font color=red>") + error + QString("</font>") );
182         printWidget_->setReady(false);
183     }
184 }
185
186 void UrpoMainWindow::about()
187 {
188     QMessageBox::about(this, tr("About Urpo"),
189                        tr("<b>Unix Remote Printing Operation %1 </b>"
190                           "<p>Copyright &copy; Arto Hyv&auml;ttinen 2010"
191                           "<p>License: General Public License v3"
192                           ).arg(VERSION));
193 }
194
195 void UrpoMainWindow::settings()
196 {
197     printWidget_->doCancel(); // Cancel current process
198
199     SettingsDialog* dialog = new SettingsDialog(this);
200     dialog->setSettings(settings_);
201     dialog->setHelp(helpBrowser_);
202     dialog->show();
203
204     // Dialog done -> get printers
205     connect( dialog, SIGNAL(accepted()), this, SLOT(getPrinters()));
206
207 }
208
209 void UrpoMainWindow::debugWindow()
210 {
211     monitor_->show();
212     monitor_->raise();
213     monitor_->activateWindow();
214 }
215
216 void UrpoMainWindow::helpWindow()
217 {
218
219     // Show help
220     helpBrowser_->home();
221     helpBrowser_->show();
222     helpBrowser_->raise();
223     helpBrowser_->activateWindow();
224 }
225
226 void UrpoMainWindow::print(QString file, QString options)
227 {
228     printWidget_->setStatus( tr("Printing..."), true );
229     connect( printWidget_, SIGNAL(cancel()), printJob_, SLOT(cancel()));
230     printJob_->printFile(file,options);
231 }
232
233 void UrpoMainWindow::printFinished(bool success, QString errorStr)
234 {
235     disconnect( printWidget_, SIGNAL(cancel()), printJob_, SLOT(cancel()));
236     printWidget_->setReady(true); // Ready to print again!
237     if( success == false )  // Error -- set error message!
238     {
239         printWidget_->setReady(true);
240         printWidget_->setStatus( QString("<font color=red>") + errorStr + QString("</font>"));
241     }
242 }