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