ip2cupdatebox.cpp
1 //------------------------------------------------------------------------------
2 // ip2cupdatebox.cpp
3 //------------------------------------------------------------------------------
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 // 02110-1301 USA
19 //
20 //------------------------------------------------------------------------------
21 // Copyright (C) 2010 "Zalewa" <zalewapl@gmail.com>
22 //------------------------------------------------------------------------------
23 #include "ip2cupdatebox.h"
24 #include "ui_ip2cupdatebox.h"
25 
26 #include "doomseekerfilepaths.h"
27 
28 #include "ip2c/ip2cupdater.h"
29 #include <QDateTime>
30 #include <QFileInfo>
31 
32 DClass<IP2CUpdateBox> : public Ui::IP2CUpdateBox
33 {
34 public:
35  IP2CUpdater *ip2cUpdater;
36 };
37 
38 DPointered(IP2CUpdateBox)
39 
40 IP2CUpdateBox::IP2CUpdateBox(QWidget *parent)
41  : QDialog(parent)
42 {
43  d->setupUi(this);
44 
45  connect(d->btnUpdate, SIGNAL(clicked()), this, SLOT(accept()));
46  connect(d->btnCancel, SIGNAL(clicked()), this, SLOT(reject()));
47  d->progressBar->hide();
48 
49  d->ip2cUpdater = new IP2CUpdater(this);
50  this->connect(d->ip2cUpdater, SIGNAL(updateNeeded(int)), SLOT(updateInfo(int)));
51 
52  start();
53 }
54 
55 IP2CUpdateBox::~IP2CUpdateBox()
56 {
57 }
58 
59 void IP2CUpdateBox::start()
60 {
61  QString filePath = DoomseekerFilePaths::ip2cDatabaseAny();
62 
63  d->lblIP2CFileLocation->setText(!filePath.isEmpty() ? filePath : tr("N/A"));
64 
65  QString downloadPath = DoomseekerFilePaths::ip2cDatabase();
66  d->lblIP2CDownloadLocation->setText(downloadPath);
67  QFileInfo downloadedFileInfo(downloadPath);
68  d->lblDownloadIcon->setPixmap(downloadedFileInfo.isFile() ?
69  QPixmap(":/icons/edit-redo.png") :
70  QPixmap(":/icons/edit-redo-red.png"));
71  d->lblDownloadIcon->setToolTip(downloadedFileInfo.isFile() ?
72  tr("File is already downloaded.") :
73  tr("File doesn't exist yet or location doesn't point to a file."));
74 
75  QFileInfo fileInfo(filePath);
76  if (!filePath.isEmpty() && fileInfo.exists())
77  {
78  d->lblFileIcon->setPixmap(QPixmap(":/icons/edit-redo.png"));
79  d->lblDatabaseStatus->setText(tr("Verifying checksum ..."));
80  d->progressBar->show();
81  d->ip2cUpdater->needsUpdate(filePath);
82  }
83  else
84  {
85  d->lblFileIcon->setPixmap(QPixmap(":/icons/edit-redo-red.png"));
86  d->lblDatabaseStatus->setText(tr("IP2C database file was not found. "
87  "Use the update button if you want to download the newest database."));
88  }
89 }
90 
91 void IP2CUpdateBox::updateInfo(int status)
92 {
93  d->progressBar->hide();
94  switch (status)
95  {
96  case IP2CUpdater::UpdateNeeded:
97  d->lblStatusIcon->setPixmap(QPixmap(":/icons/edit-redo-red.png"));
98  d->lblDatabaseStatus->setText(tr("Update required."));
99  break;
100  case IP2CUpdater::UpToDate:
101  d->lblStatusIcon->setPixmap(QPixmap(":/icons/edit-redo.png"));
102  d->lblDatabaseStatus->setText(tr("Database is up-to-date."));
103  break;
104  case IP2CUpdater::UpdateCheckError:
105  d->lblStatusIcon->setPixmap(QPixmap(":/icons/x.png"));
106  d->lblDatabaseStatus->setText(tr("Database status check failed. See log for details."));
107  break;
108  default:
109  d->lblStatusIcon->setPixmap(QPixmap(":/icons/x.png"));
110  d->lblDatabaseStatus->setText(tr("Unhandled update check status."));
111  break;
112  }
113 }