-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmainwindow.cpp
672 lines (585 loc) · 25.1 KB
/
mainwindow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
/*
Copyright (c) 2011 Carlos López Sánchez <musikolo{AT}hotmail[DOT]com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QtGui/QLabel>
#include <QtGui/QMenu>
#include <QtGui/QMenuBar>
#include <QtGui/QAction>
#include <KFileDialog>
#include <KAction>
#include <KToolBar>
#include <KMenuBar>
#include <KStatusBar>
#include <KInputDialog>
#include <QRegExp>
#include <QRegExpValidator>
#include <KDebug>
#include <KMessageBox>
#include <QDesktopServices>
#include <KHelpMenu>
#include <KCmdLineArgs>
#include <KConfigDialog>
#include <QSplitter>
#include <KStandardAction>
#include <KNotification>
#include <QTimer>
#include <QProgressBar>
#include "constants.h"
#include "taskviewhandler.h"
#include "mainwindow.h"
#include "welcomewizard.h"
#include "settings.h"
#include "settingsdialog.h"
#include <httpconnectorfactory.h>
static const KUrl START_DIR_URL( "kfiledialog:///kvirustotal" );
MainWindow::MainWindow() {
kDebug() << QString( "Starting up %1..." ).arg( General::APP_UI_NAME );
networkManager = NULL;
workloadConnector = NULL;
downloader = NULL;
wizard = NULL;
manualVersionCheckRequested = false;
setupUi( this );
// Setup the toolbar
KToolBar* toolbar = this->toolBar();
KAction* fileAction = new KAction( KIcon( "document-open" ), i18n( "File..." ), this );
fileAction->setHelpText( i18n( "Add a file" ) );
fileAction->setShortcut( Qt::CTRL + Qt::Key_F );
toolbar->addAction( fileAction );
KAction* remoteFileAction = new KAction( KIcon( "download" ), i18n( "Remote file..." ), this );
remoteFileAction->setHelpText( i18n( "Add a remote file" ) );
remoteFileAction->setShortcut( Qt::CTRL + Qt::Key_R );
toolbar->addAction( remoteFileAction );
KAction* urlAction = new KAction( KIcon( "document-open-remote" ), i18n( "URL..." ), this );
urlAction->setHelpText( i18n( "Add a URL" ) );
urlAction->setShortcut( Qt::CTRL + Qt::Key_U );
toolbar->addAction( urlAction );
KAction* rescanAction = new KAction( KIcon( "view-refresh" ), i18n( "Re-scan" ), this );
rescanAction->setHelpText( i18n( "Re-scan the selected finished task(s)" ) );
rescanAction->setShortcut( Qt::CTRL + Qt::Key_S );
toolbar->addSeparator();
toolbar->addAction( rescanAction );
// Add "Clean finished tasks" action
KAction* cleanFinishedAction = new KAction( KIcon( "run-build-clean" ), i18n( "Clean" ), this );
cleanFinishedAction->setHelpText( i18n( "Clean all finished tasks" ) );
cleanFinishedAction->setShortcut( Qt::CTRL + Qt::Key_D );
toolbar->addAction( cleanFinishedAction );
KAction* abortAction = new KAction( KIcon( "dialog-cancel" ), i18n( "Abort..." ), this );
abortAction->setHelpText( i18n( "Abort the selected tasks" ) );
abortAction->setShortcut( Qt::CTRL + Qt::Key_B );
toolbar->addAction( abortAction );
KAction* quitAction = KStandardAction::quit( this, SLOT( closeRequested() ), this );
quitAction->setHelpText( i18n( "Quit %1", General::APP_UI_NAME ) );
toolbar->addSeparator();
toolbar->addAction( quitAction );
KMenuBar* menubar = menuBar();
QMenu* fileMenu = menubar->addMenu( i18nc( "Menu name", "File" ) );
fileMenu->addAction( fileAction );
fileMenu->addAction( remoteFileAction );
fileMenu->addAction( urlAction );
fileMenu->addSeparator();
fileMenu->addAction( quitAction );
// Edit menu
QMenu* editMenu = menubar->addMenu( i18n( "Edit" ) );
KAction* selectAllAction = new KAction( i18n( "Select All" ), this );
selectAllAction->setHelpText( i18n( "Select all tasks") );
selectAllAction->setShortcut( Qt::CTRL + Qt::Key_A );
editMenu->addAction( selectAllAction );
KAction* invertSelectionAction = new KAction( i18n( "Invert Selection" ), this );
invertSelectionAction->setHelpText( i18n( "Inverts the selection of tasks") );
invertSelectionAction->setShortcut( Qt::CTRL + Qt::Key_I );
editMenu->addAction( invertSelectionAction );
KAction* settingsAction = new KAction( KIcon( "configure" ), i18n( "Settings..." ), this );
settingsAction->setHelpText( i18n( "Show the settings dialog" ) );
settingsAction->setShortcut( Qt::CTRL + Qt::Key_T );
editMenu->addSeparator();
editMenu->addAction( settingsAction );
// Action menu
QMenu* actionMenu = menubar->addMenu( i18n( "Action" ) );
actionMenu->addAction( rescanAction );
actionMenu->addAction( cleanFinishedAction );
actionMenu->addAction( abortAction );
// Help menu
QMenu* helpMenu = ( new KHelpMenu( this, KCmdLineArgs::aboutData(), false ) )->menu();
helpMenu->addSeparator();
helpMenu->addAction( KIcon( "rating" ), i18n( "Check for updates..." ), this, SLOT( checkForUpdates() ) );
helpMenu->addAction( KIcon( "tools-wizard" ), i18n( "Show welcome wizard..." ), this, SLOT( showWelcomeWizard() ) );
menubar->addMenu( helpMenu );
// Set the frame literals
scanAnalysisDateLabel->setText( i18n( "Scan analysis date:" ) );
permanentLinkLabel->setText( i18n( "Permanent link:" ) );
// Frame issues - set the result icon label
this->resultIconLabel = new QLabel( frame );
const QSize iconSize(48, 48);
resultIconLabel->setMaximumSize( iconSize );
resultIconLabel->resize( iconSize );
resultIconLabel->setEnabled( false );
KIcon icon( "security-high" ); // See setResultIcon() method
resultIconLabel->setPixmap( icon.pixmap( iconSize ) );
frameMainLayout->addWidget( resultIconLabel, Qt::AlignRight );
frame->setLayout( frameMainLayout ); // Needed to get the layout resizes as the frame does
permanentLinkValue->setToolTip( i18n( "Click to view detailed report" ) );
// Needed to get the QSplitter to expand
QVBoxLayout* layout = new QVBoxLayout( centralwidget );
layout->addWidget( splitter );
statusBar()->showMessage( i18n( "Ready..." ) );
setWindowTitle( i18n( "Online antivirus and anti-phishing tool - %1", General::APP_UI_NAME ) );
// Restore the main window state, if available
const Settings* settings = Settings::self();
const QSize size = settings->mainWindowSize();
if( size.isValid() && size.width() > 100 && size.height() > 100 ) {
move( settings->mainWindowPos() );
resize( size );
}
else {
showMaximized();
}
// Restore its state too, if available
QList< int > splitterState = settings->splitterState();
if( !splitterState.isEmpty() ) {
splitter->setSizes( splitterState );
}
// Create a network access manager object
networkManager = new QNetworkAccessManager( this );
workloadConnector = HttpConnectorFactory::getHttpConnector( networkManager );
// Setup the task table. If the serviceKey is empty, it will be updated later
HttpConnectorFactory::loadHttpConnectorSettings();
taskViewHandler = new TaskViewHandler( this, taskTableWidget, reportTableWidget );
// Set up the workload progress bars
setupWorkloadProgressBars();
// Set up connections
connect( fileAction, SIGNAL( triggered( bool ) ), this, SLOT( openFile() ) );
connect( remoteFileAction, SIGNAL( triggered( bool ) ), this, SLOT( openRemoteFile() ) );
connect( urlAction, SIGNAL( triggered( bool ) ), this, SLOT( openUrl() ) );
connect( abortAction, SIGNAL( triggered( bool ) ), taskViewHandler, SLOT( abortSelectedTask() ) );
connect( permanentLinkValue, SIGNAL( leftClickedUrl() ), this, SLOT( openPermanentLink() ) );
connect( selectAllAction, SIGNAL( triggered( Qt::MouseButtons, Qt::KeyboardModifiers ) ),
taskViewHandler, SLOT( selectAll() ) );
connect( invertSelectionAction, SIGNAL( triggered( Qt::MouseButtons, Qt::KeyboardModifiers ) ),
taskViewHandler, SLOT( invertSelection() ) );
connect( settingsAction, SIGNAL( triggered(Qt::MouseButtons,Qt::KeyboardModifiers ) ),
this, SLOT( showSettingsDialog() ) );
connect( rescanAction, SIGNAL( triggered( bool ) ), taskViewHandler, SLOT( rescanTasks() ) );
connect( cleanFinishedAction, SIGNAL( triggered( bool ) ), taskViewHandler, SLOT( clearFinishedRows() ) );
connect( workloadConnector, SIGNAL( serviceWorkloadReady( ServiceWorkload ) ),
this, SLOT( onWorkloadReady( ServiceWorkload ) ) );
// Show the minimum level and ask for the service workload
ServiceWorkload workload = { ServiceWorkload::MIN_VALUE ,ServiceWorkload::MIN_VALUE };
updateWorkloadProgressBars( workload );
//FIXME:workloadConnector->onRetrieveServiceWorkload();
// If the protocol is not supported by the current HttpProtocol, show the settings dialog
if( validateHttpConnectorProtocol() ) {
showSettingsDialog();
}
// Validate version
validateCurrentVersion();
// Call the delayed connections and accept drops
QTimer::singleShot( 1000, this, SLOT( delayedConnections() ) );
setAcceptDrops( true );
// Check for new versions
if( Settings::self()->checkVersionAtStartup() ) {
downloadVersionFile();
}
}
MainWindow::~MainWindow() {
if( wizard != NULL ) {
delete wizard;
}
if( workloadConnector != NULL ) {
workloadConnector->abort(); // Needed to prevent the app from crashing if started up and closed too fast.
workloadConnector->deleteLater();
}
if( networkManager != NULL ) {
networkManager->deleteLater();;
}
}
void MainWindow::delayedConnections() {
// Needed to be delayed, since MainWindow will throw the resizeWidth signal at construction time
connect( this, SIGNAL( resizeWidth( int ) ), taskViewHandler, SLOT( tableWidthChanged( int ) ) );
}
bool MainWindow::closeRequested() {
if( taskViewHandler->isUnfinishedTasks() &&
KMessageBox::warningContinueCancel( centralWidget(),
i18n( "There is one or more tasks in course and will be aborted. Do you really want to quit?" ),
i18n( "Tasks in course" ),
KStandardGuiItem::cont(),
KStandardGuiItem::cancel(),
QString::null,
KMessageBox::Dangerous ) != KMessageBox::Continue ) {
return false;
}
// Save current config
Settings* settings = Settings::self();
settings->setCurrentVersion( General::APP_VERSION );
settings->setMainWindowPos( pos() );
settings->setMainWindowSize( size() );
QList< int > taskTableCols;
for( int i = 0; i < taskTableWidget->columnCount(); i++ ) {
taskTableCols.append( taskTableWidget->columnWidth( i ) );
}
settings->setTaskTableCols( taskTableCols );
QList< int > reportTableCols;
for( int i = 0; i < reportTableWidget->columnCount(); i++ ) {
reportTableCols.append( reportTableWidget->columnWidth( i ) );
}
settings->setReportTableCols( reportTableCols );
settings->setSplitterState( splitter->sizes() );
kDebug() << "Storing user config...";
settings->writeConfig();
return close();
}
void MainWindow::openFile() {
// Get the file name and submit it
QString fileName = KFileDialog::getOpenFileName( START_DIR_URL );
submitFile( fileName );
}
void MainWindow::submitFile( const QString& filename ) {
if( !filename.isEmpty() ) {
taskViewHandler->submitFile( QFile( filename ) );
}
else {
kError() << "The given filename is empty!";
}
}
void MainWindow::openRemoteFile() {
// Get the remote file's URL and submit it
const QUrl url = promptUrl( i18n( "Remote file inputbox" ), i18n( "Please, enter a remote file's URL:" ) );
submitRemoteFile( url );
}
void MainWindow::submitRemoteFile( const QUrl& url ) {
if( !url.isEmpty() ) {
taskViewHandler->submitRemoteFile( networkManager, url );
}
else {
kError() << "The given URL is empty!";
}
}
void MainWindow::openUrl() {
// Get the URL and submit it
const QUrl url = promptUrl( i18n( "URL inputbox" ), i18n( "Please, enter a URL:" ) );
submitUrl( url );
}
void MainWindow::submitUrl( const QUrl& url ) {
if( !url.isEmpty() ) {
taskViewHandler->submitUrl( url );
}
else {
kError() << "The given URL is empty!";
}
}
QUrl MainWindow::promptUrl( const QString& title, const QString& message ) {
bool ok;
const QRegExp regex( "^((http)|(ftp))[s]?://[^..]+(\\.[^..]+)+$", Qt::CaseInsensitive );
QRegExpValidator validator( regex, this );
QString url = KInputDialog::getText( title, message, "http://", &ok, this, &validator );
if( ok && !url.isEmpty() ) {
kDebug() << "Entered URL is" << url;
return QUrl( url );
}
return QUrl();
}
void MainWindow::setResultIcon( const QString& iconName, bool enabled ) {
KIcon icon( iconName ); // Expected values are: security-high, security-medium, security-low
resultIconLabel->setEnabled( enabled );
resultIconLabel->setPixmap( icon.pixmap( resultIconLabel->size() ) );
}
void MainWindow::openPermanentLink() {
QDesktopServices::openUrl( permanentLinkValue->text() );
}
void MainWindow::resizeEvent ( QResizeEvent* event ) {
int width = event->size().width();
int oldWidth = event->oldSize().width();
if( width != oldWidth ) {
kDebug() << QString("resizeEvent() - newWitdh:%1 oldWidth:%2").arg( width ).arg( oldWidth );
emit( resizeWidth( width ) );
}
}
void MainWindow::showWelcomeWizard() {
if( wizard != NULL ) {
wizard->deleteLater();
wizard = NULL;
}
wizard = new WelcomeWizard( networkManager, centralwidget );
wizard->show();
connect( wizard, SIGNAL( finished( int ) ), this, SLOT( wizardFinished( int ) ) );
}
bool MainWindow::validateHttpConnectorProtocol() {
if( Settings::self()->httpConnectorType() == HttpConnectorType::WEB_HTTPCONNECTOR &&
!Settings::self()->secureProtocol() ) {
KMessageBox::sorry( this, i18n( "The current connector (WebHttpConnector) does not support the HTTP protocol. Please, choose the secure HTTPS protocol instead." ) );
return true;
}
return false;
}
void MainWindow::wizardFinished( int result ) {
kDebug() << "result=" << result;
if( HttpConnectorFactory::getFileHttpConnectorCfg().serviceKeyRequired ||
HttpConnectorFactory::getUrlHttpConnectorCfg().serviceKeyRequired ) {
const QString serviceKey = Settings::self()->serviceKey();
if( serviceKey.isEmpty() ) {
KMessageBox::sorry( this, i18n( "No service key found! Thus, the application must be closed." ) );
close();
}
}
// If the protocol is not supported by the current HttpProtocol, show the settings dialog
if( validateHttpConnectorProtocol() ) {
showSettingsDialog();
}
}
void MainWindow::showSettingsDialog() {
// If we have an instance created, do nothing
if ( KConfigDialog::showDialog( "settings" ) ) {
return;
}
// KConfigDialog didn't find an instance of this dialog, so lets create it :
KConfigDialog* dialog = new KConfigDialog( this, "settings", Settings::self() );
SettingsDialog* settingsDialog = new SettingsDialog( this );
dialog->addPage( settingsDialog, i18n( "General" ), "preferences-other" );
dialog->show();
// If a change takes place, reload the affected settings
connect( dialog, SIGNAL( settingsChanged( const QString& ) ), this, SLOT( settingsChanged() ) );
}
void MainWindow::settingsChanged() {
validateHttpConnectorProtocol();
HttpConnectorFactory::loadHttpConnectorSettings();// Re-establish the protocol (HTTPS or HTTP)
setProgressBarSecurityIcon();
}
void MainWindow::dragEnterEvent( QDragEnterEvent* event ) {
kDebug() << "Receiving dragged object with mimeData" << event->mimeData()->text();
QUrl url( event->mimeData()->text() );
if( url.isValid() ) {
const QString scheme = url.scheme();
if( !scheme.isEmpty() && ( scheme == "file" || scheme.startsWith( "http" ) || scheme.startsWith( "ftp" ) ) ) {
event->acceptProposedAction();
}
}
}
void MainWindow::dropEvent( QDropEvent* event ) {
kDebug() << "Receiving dropped object with mimeData" << event->mimeData()->text();
QUrl url( event->mimeData()->text() );
const QString scheme = url.scheme();
if( scheme == "file" ) {
QFile file( url.path() );
if( file.exists() ) {
taskViewHandler->submitFile( file );
}
}
else {
static const QChar dot( '.' );
if( url.path().contains( dot ) ) {
taskViewHandler->submitRemoteFile( networkManager, url );
}
else {
taskViewHandler->submitUrl( url );
}
}
}
void MainWindow::setupWorkloadProgressBars() {
kDebug() << "Setting up workload progress bars...";
// Antivirus progress bar
QProgressBar* progressBar = new QProgressBar( this );
progressBar->setObjectName( "antivirusProgressBar" );
progressBar->setMinimum( ServiceWorkload::MIN_VALUE - 1 ); // Set one below the min not to show zero
progressBar->setMaximum( ServiceWorkload::MAX_VALUE );
progressBar->setToolTip( i18n( "Antivirus service workload" ) );
progressBar->setFormat( i18n( "Antivirus %1", QString( "%p%" ) ) );
progressBar->setMaximumSize( 120, 20 );
statusBar()->setContentsMargins( 6, -2, 6, 0 );
statusBar()->addPermanentWidget( progressBar );
// Anti-phising progress bar
progressBar = new QProgressBar( this );
progressBar->setObjectName( "antiphisingProgressBar" );
progressBar->setMinimum( ServiceWorkload::MIN_VALUE - 1 ); // Set one below the min not to show zero
progressBar->setMaximum( ServiceWorkload::MAX_VALUE );
progressBar->setToolTip( i18n( "Anti-phising service workload" ) );
progressBar->setFormat( i18n( "Anti-phising %1", QString( "%p%" ) ) );
progressBar->setMaximumSize( 120, 20 );
statusBar()->addPermanentWidget( progressBar );
kDebug() << "Workload progress bars ready!";
// Set the security icon
setProgressBarSecurityIcon();
}
void MainWindow::setProgressBarSecurityIcon() {
// Find the icon container or create a new one, if none exists
QLabel* iconContainer = statusBar()->findChild< QLabel* >( "padlock" );
if( iconContainer == NULL ) {
iconContainer = new QLabel();
iconContainer->setObjectName( "padlock" );
statusBar()->addPermanentWidget( iconContainer );
}
// Update the icon accordingly
KIcon padlock;
if( Settings::self()->secureProtocol() ) {
padlock = KIcon( "padlock-green" );
iconContainer->setToolTip( i18n( "HTTPs is enabled, so data is being transmitted securely!" ) );
}
else {
padlock = KIcon( "padlock-red" );
iconContainer->setToolTip( i18n( "HTTPs is disabled, so data is being transmitted insecurely. Thus, it is highly recommended to enable it!" ) );
}
iconContainer->setPixmap( padlock.pixmap( 20, 20 ) );
}
void MainWindow::onWorkloadReady( ServiceWorkload workload ) {
// Update the progress bars with the given argument
updateWorkloadProgressBars( workload );
// Prepare the next connection to refresh the workload
const int nextShot = 300;
kDebug() << "Preparing" << nextShot << "seconds singleShot to refresh the workload bars...";
QTimer::singleShot( nextShot * 1000, workloadConnector, SLOT( onRetrieveServiceWorkload() ) );
}
void MainWindow::updateWorkloadProgressBars( ServiceWorkload workload ) {
kDebug() << "Updating workload progress bars...";
static QProgressBar* const antivirusPb = statusBar()->findChild< QProgressBar* >( "antivirusProgressBar" );
static QProgressBar* const antiphisingPb = statusBar()->findChild< QProgressBar* >( "antiphisingProgressBar" );
static QColor colors[] = { QColor( 72, 182, 0 ), QColor( 72, 182, 0 ), QColor( 255, 140, 0 ), QColor( 255, 140, 0 )
, QColor( 255, 69, 0 ), QColor( 255, 69, 0 ), QColor( 255, 0, 0 ) };
// Update the antivirus progress bar
int value = workload.file;
antivirusPb->setValue( value );
QPalette palette = antiphisingPb->palette();
palette.setColor( QPalette::Highlight, colors[ value - 1 ] );
antivirusPb->setPalette( palette );
// Update the anti-phising progress bar
value = workload.url;
antiphisingPb->setValue( value );
palette = antiphisingPb->palette();
palette.setColor( QPalette::Highlight, colors[ value - 1 ] );
antiphisingPb->setPalette( palette );
}
void MainWindow::showInfoNotificaton( const QString& msg ) {
KNotification::event( KNotification::Notification,
i18nc( "Application name", "%1 information", General::APP_UI_NAME ),
msg, KIcon( General::APP_NAME ).pixmap( 48, 48 ) );
}
void MainWindow::showCompleteTaskNotificaton( const QString& msg, const QString& iconName ) {
KNotification::event( KNotification::Notification,
i18nc( "Application name", "%1 - Task completed", General::APP_UI_NAME ),
msg, KIcon( iconName ).pixmap( 48, 48 ) );
}
void MainWindow::showWarningNotificaton( const QString& msg ) {
KNotification::event( KNotification::Warning,
i18nc( "Application name", "%1 warning", General::APP_UI_NAME ),
msg, KIcon( "task-attention" ).pixmap( 48, 48 ), NULL, KNotification::CloseWhenWidgetActivated );
}
void MainWindow::showErrorNotificaton( const QString& msg ) {
KNotification::event( KNotification::Warning,
i18nc( "Application name", "%1 error", General::APP_UI_NAME ),
msg, KIcon( "task-reject" ).pixmap( 48, 48 ), NULL, KNotification::Persistent );
}
void MainWindow::validateCurrentVersion() {
// If no currentVersion, then it's the first run. Thus, show the welcome wizard
if( Settings::self()->currentVersion().isEmpty() ) {
// This connection will guarantee that the task table's columns adapt the size of the window
connect( this, SIGNAL( resizeWidth( int ) ), taskViewHandler, SLOT( tableWidthChanged( int ) ) );
showWelcomeWizard();
}
// If the serviceKey is required and it's empty, show the welcome wizard
else if( ( HttpConnectorFactory::getFileHttpConnectorCfg().serviceKeyRequired ||
HttpConnectorFactory::getUrlHttpConnectorCfg().serviceKeyRequired ) &&
Settings::self()->serviceKey().isEmpty() ) {
KMessageBox::information( this,
i18n( "The current connector requires a service key, but none has been found. Thus, the welcome wizard will be shown so you can now either set it up or change the connector to be used."),
i18n( "Service key required" ) );
// This connection will guarantee that the task table's columns adapt the size of the window
connect( this, SIGNAL( resizeWidth( int ) ), taskViewHandler, SLOT( tableWidthChanged( int ) ) );
showWelcomeWizard();
}
// Show the a message to warn the user that a new connector has been implemented
else if ( isPreviousVersionTo( (uchar)0, (uchar)20, (uchar)0 ) ) {
if( KMessageBox::questionYesNo( this,
i18nc( "Application name",
"The new version of %1 has a new connector. Do you want the welcome wizard to be shown so you can change it?",
General::APP_UI_NAME ),
i18n( "Show welcome wizard" ) ) == KMessageBox::Yes ) {
// This connection will guarantee that the task table's columns adapt the size of the window
connect( this, SIGNAL( resizeWidth( int ) ), taskViewHandler, SLOT( tableWidthChanged( int ) ) );
showWelcomeWizard();
}
else {
// Leave the API connector
Settings::self()->setHttpConnectorType( HttpConnectorType::API_HTTPCONNECTOR );
Settings::self()->writeConfig();
}
}
}
bool MainWindow::isPreviousVersion() {
return isPreviousVersionTo( General::APP_VERSION_MAJOR, General::APP_VERSION_MINOR, General::APP_VERSION_BUGFIX );
}
bool MainWindow::isPreviousVersionTo( uchar major, uchar minor, uchar bugfix ) {
const QString version = Settings::self()->currentVersion();
if( !version.isEmpty() ) {
QStringList versions = version.split( ".", QString::SkipEmptyParts );
// Major
if( versions.size() > 0 ) {
bool ok = false;
int aux = versions[ 0 ].toInt( &ok );
if( ok && aux >= major ) {
// Minor
if( versions.size() > 1 ) {
aux = versions[ 1 ].toInt( &ok );
if( ok && aux >= minor ) {
if( versions.size() > 2 ) {
aux = versions[ 2 ].toInt( &ok );
if( ok && aux >= bugfix ) {
return false;
}
}
}
}
}
}
}
kDebug() << "Previous version detected!";
return true;
}
void MainWindow::checkForUpdates() {
manualVersionCheckRequested = true;
downloadVersionFile();
}
void MainWindow::downloadVersionFile() {
// Create a new dowloander and connect it, if not already done
if( downloader == NULL ) {
downloader = new FileDownloader( networkManager );
connect( downloader, SIGNAL( downloadReady( QFile* ) ), this, SLOT( onVersionFileReady(QFile*) ) );
}
// Download the latest VERSION file
const QUrl url( "http://gitorious.org/kvirustotal/kvirustotal/blobs/raw/master/VERSION" );
kDebug() << QString( "Checking for new version at %1..." ).arg( url.toString() );
downloader->download( url );
}
void MainWindow::onVersionFileReady( QFile* file ) {
// Open the file having the latest version in read-only mode
if( !file->open( QIODevice::ReadOnly ) ) {
kError() << "ERROR: Could not open the file" << file->fileName() << "in read-only mode!. Thus, new versions cannot be checked out!";
}
// Read all data and compare with the current version
const QString version( file->readAll() );
kDebug() << "Latest version is" << version;
if( !version.isEmpty() && version != General::APP_VERSION ) {
if( KMessageBox::questionYesNo( centralwidget,
i18nc( "Application name", "The new version %1 has been released. Do you want to open %2 homepage to find out what's new?",
version, General::APP_UI_NAME ),
i18n( "Vesion check" ) ) == KMessageBox::Yes ) {
QDesktopServices::openUrl( QUrl( General::APP_HOMEPAGE ) );
}
}
else if( manualVersionCheckRequested ) {
KMessageBox::information( centralwidget, i18nc( "Application name", "You are currently using the latest version of %1", General::APP_UI_NAME ),
i18n( "Vesion check" ) );
manualVersionCheckRequested = false; // Reset the flag
}
// Close and free the file
file->close();
file->deleteLater();
}
#include "mainwindow.moc"