Password authentication (stage 1 - without xosso-terminal)
[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
40 #include <cstdlib>
41
42 #include "settingsdialog.h"
43
44 #define VERSION "0.9"   /*! Program version */
45
46 UrpoMainWindow::UrpoMainWindow(QWidget *parent)
47     : QMainWindow(parent)
48 {
49     setWindowTitle(QString("URPO ") + VERSION );
50     setWindowIcon( QIcon(":/urpo.png"));
51
52     // Load connection settings
53     settings_ = new UrpoConnectionSettings("Urpo","Urpo");
54     settings_->load();
55
56     // Connect to debug monitor
57     monitor_ = new DebugConsole();
58     settings_->setDebugMonitor(monitor_);
59
60     // Init central widget
61     printWidget_ = new PrintWidget;
62     setCentralWidget(printWidget_);
63
64     // Init Help
65     initHelp();
66     // Init menu
67     initMenu();
68
69     // Connect buttons
70     connect( printWidget_, SIGNAL(reconnect()), this, SLOT(getPrinters()));
71     connect( printWidget_, SIGNAL(print(QString,QString)), this, SLOT(print(QString,QString)));
72
73     // Init jobs
74
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)));
81
82     // Init print job
83     printJob_ = new PrintJob(settings_);
84     connect( printJob_, SIGNAL(finished(bool,QString)), this, SLOT(printFinished(bool,QString)));
85
86     if( settings_->getHost().isEmpty())
87         // If no host settings, go first to settings
88         settings();
89     else
90         // Try to get printers list
91         getPrinters();
92
93
94 }
95
96 UrpoMainWindow::~UrpoMainWindow()
97 {
98
99 }
100
101 void UrpoMainWindow::getPrinters()
102 {
103
104     printWidget_->setStatus(tr("Connecting..."),true);
105     connect(printWidget_, SIGNAL(cancel()), printerListJob_, SLOT(cancel()));
106     printerListJob_->start();
107
108 }
109
110
111 void UrpoMainWindow::initMenu()
112 {
113
114     QAction* settingsAction = new QAction( tr("Settings"), this);
115     connect(settingsAction, SIGNAL(triggered()), this, SLOT(settings()) );
116     menuBar()->addAction(settingsAction);
117
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);
122
123     QAction* aboutAction = new QAction( tr("About"), this);
124     connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
125     menuBar()->addAction(aboutAction);
126
127     QAction* aboutQtAction = new QAction( tr("About Qt"), this );
128     connect(aboutQtAction, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
129     menuBar()->addAction(aboutQtAction);
130
131
132     QAction* helpAction = new QAction( tr("Help"), this );
133     connect( helpAction, SIGNAL(triggered()), this, SLOT(helpWindow()));
134     menuBar()->addAction(helpAction);
135
136 }
137
138 void UrpoMainWindow::initHelp()
139 {
140
141
142
143     // Init help
144     helpBrowser_ = new QTextBrowser();
145     helpBrowser_->setWindowTitle(tr("Urpo Help"));
146
147     // Load help file
148     // Try to load locale version index_fi etc.
149
150     // using LANG enviroment variable instead of QLocale because of Qt on Maemo problem
151     // bug #6136
152     QString language = std::getenv("LANG");
153
154     QString helpfilename = QString(":/help/index_") + language.left(2).toLower() + QString(".html");
155
156     monitor_->debugMessage(helpfilename);
157
158     QFile helpfile( helpfilename );
159     if( helpfile.exists() )
160         helpBrowser_->setSource(QUrl( QString("qrc") + helpfilename  ));
161     else
162         // Not find, load general
163         helpBrowser_->setSource(QUrl("qrc:/help/index.html"));
164
165     helpBrowser_->setOpenExternalLinks(true);
166
167 }
168
169 void UrpoMainWindow::printersReceived(bool success, QString error)
170 {
171     // Disconnect cancel button out of printerListJob
172     disconnect(printWidget_, SIGNAL(cancel()), printerListJob_, SLOT(cancel()));
173     if(success)
174     {
175         // PrinterListJob successed
176         printWidget_->setPrinters( printerListJob_->getPrinters());
177         if( printerListJob_->getPrinters().isEmpty())
178         {
179             // No printers, can't print
180             printWidget_->setStatus( QString("<font color=red>") + tr("No printers found") + QString("</font>"),false);
181             printWidget_->setReady(false);
182         }
183         else
184         {
185             // Ready to print
186             printWidget_->setReady( true );
187         }
188
189     }
190     else
191     {
192         // Unsuccess!
193         printWidget_->setStatus(  QString("<font color=red>") + error + QString("</font>") );
194         printWidget_->setReady(false);
195     }
196 }
197
198 void UrpoMainWindow::about()
199 {
200     QMessageBox::about(this, tr("About Urpo"),
201                        tr("<b>Unix Remote Printing Operation %1 </b>"
202                           "<p>Copyright &copy; Arto Hyv&auml;ttinen 2010"
203                           "<p>License: General Public License v3"
204                           ).arg(VERSION));
205 }
206
207 void UrpoMainWindow::settings()
208 {
209     printWidget_->doCancel(); // Cancel current process
210
211     SettingsDialog* dialog = new SettingsDialog(this);
212     dialog->setSettings(settings_);
213     dialog->setHelp(helpBrowser_);
214     dialog->show();
215
216     // Dialog done -> get printers
217     connect( dialog, SIGNAL(accepted()), this, SLOT(getPrinters()));
218
219 }
220
221 void UrpoMainWindow::debugWindow()
222 {
223     monitor_->show();
224     monitor_->raise();
225     monitor_->activateWindow();
226 }
227
228 void UrpoMainWindow::helpWindow()
229 {
230
231     // Show help
232     helpBrowser_->home();
233     helpBrowser_->show();
234     helpBrowser_->raise();
235     helpBrowser_->activateWindow();
236 }
237
238 void UrpoMainWindow::print(QString file, QString options)
239 {
240     printWidget_->setStatus( tr("Printing..."), true );
241     connect( printWidget_, SIGNAL(cancel()), printJob_, SLOT(cancel()));
242     printJob_->printFile(file,options);
243 }
244
245 void UrpoMainWindow::printFinished(bool success, QString errorStr)
246 {
247     disconnect( printWidget_, SIGNAL(cancel()), printJob_, SLOT(cancel()));
248     printWidget_->setReady(true); // Ready to print again!
249     if( success == false )  // Error -- set error message!
250     {
251         printWidget_->setReady(true);
252         printWidget_->setStatus( QString("<font color=red>") + errorStr + QString("</font>"));
253     }
254 }