No password authentication - it does not work!
[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.92"   /*! 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     QFile helpfile( helpfilename );
157     if( helpfile.exists() )
158         helpBrowser_->setSource(QUrl( QString("qrc") + helpfilename  ));
159     else
160         // Not find, load general
161         helpBrowser_->setSource(QUrl("qrc:/help/index.html"));
162
163     helpBrowser_->setOpenExternalLinks(true);
164
165 }
166
167 void UrpoMainWindow::printersReceived(bool success, QString error)
168 {
169     // Disconnect cancel button out of printerListJob
170     disconnect(printWidget_, SIGNAL(cancel()), printerListJob_, SLOT(cancel()));
171     if(success)
172     {
173         // PrinterListJob successed
174         printWidget_->setPrinters( printerListJob_->getPrinters());
175         if( printerListJob_->getPrinters().isEmpty())
176         {
177             // No printers, can't print
178             printWidget_->setStatus( QString("<font color=red>") + tr("No printers found") + QString("</font>"),false);
179             printWidget_->setReady(false);
180         }
181         else
182         {
183             // Ready to print
184             printWidget_->setReady( true );
185         }
186
187     }
188     else
189     {
190         // Unsuccess!
191         printWidget_->setStatus(  QString("<font color=red>") + error + QString("</font>") );
192         printWidget_->setReady(false);
193     }
194 }
195
196 void UrpoMainWindow::about()
197 {
198     QMessageBox::about(this, tr("About Urpo"),
199                        tr("<b>Unix Remote Printing Operation %1 </b>"
200                           "<p>Copyright &copy; Arto Hyv&auml;ttinen 2010"
201                           "<p>License: General Public License v3"
202                           ).arg(VERSION));
203 }
204
205 void UrpoMainWindow::settings()
206 {
207     printWidget_->doCancel(); // Cancel current process
208
209     SettingsDialog* dialog = new SettingsDialog(this);
210     dialog->setSettings(settings_);
211     dialog->setHelp(helpBrowser_);
212     dialog->show();
213
214     // Dialog done -> get printers
215     connect( dialog, SIGNAL(accepted()), this, SLOT(getPrinters()));
216
217 }
218
219 void UrpoMainWindow::debugWindow()
220 {
221     monitor_->show();
222     monitor_->raise();
223     monitor_->activateWindow();
224 }
225
226 void UrpoMainWindow::helpWindow()
227 {
228
229     // Show help
230     helpBrowser_->home();
231     helpBrowser_->show();
232     helpBrowser_->raise();
233     helpBrowser_->activateWindow();
234 }
235
236 void UrpoMainWindow::print(QString file, QString options)
237 {
238     printWidget_->setStatus( tr("Printing..."), true );
239     connect( printWidget_, SIGNAL(cancel()), printJob_, SLOT(cancel()));
240     printJob_->printFile(file,options);
241 }
242
243 void UrpoMainWindow::printFinished(bool success, QString errorStr)
244 {
245     disconnect( printWidget_, SIGNAL(cancel()), printJob_, SLOT(cancel()));
246     printWidget_->setReady(true); // Ready to print again!
247     if( success == false )  // Error -- set error message!
248     {
249         printWidget_->setReady(true);
250         printWidget_->setStatus( QString("<font color=red>") + errorStr + QString("</font>"));
251     }
252 }