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 #include "gui/commongui.h"
28 
29 #include "ip2c/ip2cupdater.h"
30 #include <QDateTime>
31 #include <QFileInfo>
32 
33 DClass<IP2CUpdateBox> : public Ui::IP2CUpdateBox
34 {
35 public:
36  IP2CUpdater *ip2cUpdater;
37 };
38 
39 DPointered(IP2CUpdateBox)
40 
41 IP2CUpdateBox::IP2CUpdateBox(QWidget *parent)
42  : QDialog(parent)
43 {
44  d->setupUi(this);
46 
47  connect(d->btnUpdate, SIGNAL(clicked()), this, SLOT(accept()));
48  connect(d->btnCancel, SIGNAL(clicked()), this, SLOT(reject()));
49  d->progressBar->hide();
50 
51  d->ip2cUpdater = new IP2CUpdater(this);
52  this->connect(d->ip2cUpdater, SIGNAL(updateNeeded(int)), SLOT(updateInfo(int)));
53 
54  start();
55 }
56 
57 IP2CUpdateBox::~IP2CUpdateBox()
58 {
59 }
60 
61 void IP2CUpdateBox::start()
62 {
63  QString filePath = DoomseekerFilePaths::ip2cDatabaseAny();
64 
65  d->lblIP2CFileLocation->setText(!filePath.isEmpty() ? filePath : tr("N/A"));
66 
67  QString downloadPath = DoomseekerFilePaths::ip2cDatabase();
68  d->lblIP2CDownloadLocation->setText(downloadPath);
69  QFileInfo downloadedFileInfo(downloadPath);
70  d->lblDownloadIcon->setPixmap(downloadedFileInfo.isFile() ?
71  QPixmap(":/icons/edit-redo.png") :
72  QPixmap(":/icons/edit-redo-red.png"));
73  d->lblDownloadIcon->setToolTip(downloadedFileInfo.isFile() ?
74  tr("File is already downloaded.") :
75  tr("File doesn't exist yet or location doesn't point to a file."));
76 
77  QFileInfo fileInfo(filePath);
78  if (!filePath.isEmpty() && fileInfo.exists())
79  {
80  d->lblFileIcon->setPixmap(QPixmap(":/icons/edit-redo.png"));
81  d->lblDatabaseStatus->setText(tr("Verifying checksum ..."));
82  d->progressBar->show();
83  d->ip2cUpdater->needsUpdate(filePath);
84  }
85  else
86  {
87  d->lblFileIcon->setPixmap(QPixmap(":/icons/edit-redo-red.png"));
88  d->lblDatabaseStatus->setText(tr("IP2C database file was not found. "
89  "Use the update button if you want to download the newest database."));
90  }
91 }
92 
93 void IP2CUpdateBox::updateInfo(int status)
94 {
95  d->progressBar->hide();
96  switch (status)
97  {
98  case IP2CUpdater::UpdateNeeded:
99  d->lblStatusIcon->setPixmap(QPixmap(":/icons/edit-redo-red.png"));
100  d->lblDatabaseStatus->setText(tr("Update required."));
101  break;
102  case IP2CUpdater::UpToDate:
103  d->lblStatusIcon->setPixmap(QPixmap(":/icons/edit-redo.png"));
104  d->lblDatabaseStatus->setText(tr("Database is up-to-date."));
105  break;
106  case IP2CUpdater::UpdateCheckError:
107  d->lblStatusIcon->setPixmap(QPixmap(":/icons/x.png"));
108  d->lblDatabaseStatus->setText(tr("Database status check failed. See log for details."));
109  break;
110  default:
111  d->lblStatusIcon->setPixmap(QPixmap(":/icons/x.png"));
112  d->lblDatabaseStatus->setText(tr("Unhandled update check status."));
113  break;
114  }
115 }