diff --git a/qt/update_dialog.cpp b/qt/update_dialog.cpp index c90ae0ce39..1c67ec3527 100644 --- a/qt/update_dialog.cpp +++ b/qt/update_dialog.cpp @@ -2,15 +2,21 @@ #include "../base/assert.hpp" +#include "../version/version.hpp" + #include #include +#include #include +#include #include #include #include #include +#define CHECK_FOR_UPDATE "Check for update" + using namespace storage; enum @@ -47,24 +53,33 @@ namespace qt UpdateDialog::UpdateDialog(QWidget * parent, Storage & storage) : QDialog(parent), m_storage(storage) { + m_label = new QLabel(QObject::tr("Version: ") + VERSION_STRING, this); + + m_button = new QPushButton(QObject::tr(CHECK_FOR_UPDATE), this); + connect(m_button, SIGNAL(clicked(bool)), this, SLOT(OnButtonClick(bool))); + m_tree = new QTreeWidget(this); m_tree->setColumnCount(KNumberOfColumns); QStringList columnLabels; columnLabels << tr("Country") << tr("Status") << tr("Size"); m_tree->setHeaderLabels(columnLabels); - connect(m_tree, SIGNAL(itemClicked(QTreeWidgetItem *, int)), this, SLOT(OnItemClick(QTreeWidgetItem *, int))); - QVBoxLayout * layout = new QVBoxLayout(); - layout->addWidget(m_tree); - setLayout(layout); + QHBoxLayout * horizontalLayout = new QHBoxLayout(); + horizontalLayout->addWidget(m_label); + horizontalLayout->addWidget(m_button); + QVBoxLayout * verticalLayout = new QVBoxLayout(); + verticalLayout->addLayout(horizontalLayout); + verticalLayout->addWidget(m_tree); + setLayout(verticalLayout); setWindowTitle(tr("Geographical Regions")); resize(600, 500); // we want to receive all download progress and result events m_storage.Subscribe(boost::bind(&UpdateDialog::OnCountryChanged, this, _1), - boost::bind(&UpdateDialog::OnCountryDownloadProgress, this, _1, _2)); + boost::bind(&UpdateDialog::OnCountryDownloadProgress, this, _1, _2), + boost::bind(&UpdateDialog::OnUpdateCheck, this, _1, _2)); FillTree(); } @@ -141,6 +156,13 @@ namespace qt return item; } + void UpdateDialog::OnButtonClick(bool) + { + m_button->setText(QObject::tr("Checking for update...")); + m_button->setDisabled(true); + m_storage.CheckForUpdate(); + } + /// Changes row's text color void SetRowColor(QTreeWidgetItem & item, QColor const & color) { @@ -148,6 +170,30 @@ namespace qt item.setTextColor(column, color); } + void UpdateDialog::OnUpdateCheck(int64_t updateSize, char const * readme) + { + if (updateSize < 0) + m_label->setText(QObject::tr("No update is available")); + else + { + QString title(QObject::tr("Update is available")); + QString text(readme ? readme : ""); + if (updateSize / (1000 * 1000 * 1000) > 0) + text.append(QObject::tr("\n\nDo you want to perform update and download %1 GB?").arg( + uint(updateSize / (1000 * 1000 * 1000)))); + else if (updateSize / (1000 * 1000) > 0) + text.append(QObject::tr("\n\nDo you want to perform update and download %1 MB?").arg( + uint(updateSize / (1000 * 1000)))); + else + text.append(QObject::tr("\n\nDo you want to perform update and download %1 kB?").arg( + uint((updateSize + 999) / 1000))); + if (QMessageBox::Yes == QMessageBox::question(this, title, text, QMessageBox::Yes, QMessageBox::No)) + m_storage.PerformUpdate(); + } + m_button->setText(CHECK_FOR_UPDATE); + m_button->setDisabled(false); + } + void UpdateDialog::UpdateRowWithCountryInfo(TIndex const & index) { QTreeWidgetItem * item = GetTreeItemByIndex(*m_tree, index); diff --git a/qt/update_dialog.hpp b/qt/update_dialog.hpp index 7dcf2bff85..851a59bfce 100644 --- a/qt/update_dialog.hpp +++ b/qt/update_dialog.hpp @@ -7,6 +7,7 @@ class QTreeWidget; class QTreeWidgetItem; class QLabel; +class QPushButton; namespace qt { @@ -23,10 +24,14 @@ namespace qt void OnCountryChanged(storage::TIndex const & index); void OnCountryDownloadProgress(storage::TIndex const & index, TDownloadProgress const & progress); + /// @param updateSize if -1 then no update is available + /// @param readme optional, can be NULL + void OnUpdateCheck(int64_t updateSize, char const * readme); //@} private slots: void OnItemClick(QTreeWidgetItem * item, int column); + void OnButtonClick(bool checked = false); private: void FillTree(); @@ -34,6 +39,8 @@ namespace qt private: QTreeWidget * m_tree; + QLabel * m_label; + QPushButton * m_button; storage::Storage & m_storage; }; } // namespace qt diff --git a/storage/storage.cpp b/storage/storage.cpp index d25faaa8c4..ebbe3850ce 100644 --- a/storage/storage.cpp +++ b/storage/storage.cpp @@ -38,16 +38,6 @@ namespace storage return UPDATE_BASE_URL + utils::to_string(m_currentVersion) + "/"; } - bool Storage::UpdateCheck() - { - GetDownloadManager().DownloadFile( - (UpdateBaseUrl() + UPDATE_CHECK_FILE).c_str(), - (GetPlatform().WritablePathForFile(UPDATE_CHECK_FILE)).c_str(), - boost::bind(&Storage::OnUpdateDownloadFinished, this, _1, _2), - TDownloadProgressFunction(), false); - return true; - } - TCountriesContainer const & NodeFromIndex(TCountriesContainer const & root, TIndex const & index) { // complex logic to avoid [] out_of_bounds exceptions @@ -161,7 +151,7 @@ namespace storage if (!IsTileDownloaded(*it)) { GetDownloadManager().DownloadFile( - (UPDATE_BASE_URL + it->first).c_str(), + (UpdateBaseUrl() + it->first).c_str(), (GetPlatform().WritablePathForFile(it->first).c_str()), boost::bind(&Storage::OnMapDownloadFinished, this, _1, _2), boost::bind(&Storage::OnMapDownloadProgress, this, _1, _2), @@ -245,10 +235,12 @@ namespace storage m_observerChange(index); } - void Storage::Subscribe(TObserverChangeCountryFunction change, TObserverProgressFunction progress) + void Storage::Subscribe(TObserverChangeCountryFunction change, TObserverProgressFunction progress, + TUpdateCheckFunction check) { m_observerChange = change; m_observerProgress = progress; + m_observerUpdateCheck = check; TTilesContainer tiles; if (LoadTiles(tiles, GetPlatform().WritablePathForFile(UPDATE_CHECK_FILE), m_currentVersion)) @@ -266,6 +258,7 @@ namespace storage { m_observerChange.clear(); m_observerProgress.clear(); + m_observerUpdateCheck.clear(); m_countries.Clear(); } @@ -316,12 +309,29 @@ namespace storage TDownloadProgress(m_countryProgress.first + progress.first, m_countryProgress.second)); } + void Storage::CheckForUpdate() + { + string const update = UpdateBaseUrl() + UPDATE_CHECK_FILE; + GetDownloadManager().CancelDownload(update.c_str()); + GetDownloadManager().DownloadFile( + update.c_str(), + (GetPlatform().WritablePathForFile(UPDATE_CHECK_FILE)).c_str(), + boost::bind(&Storage::OnUpdateDownloadFinished, this, _1, _2), + TDownloadProgressFunction(), false); + } + void Storage::OnUpdateDownloadFinished(char const * url, bool successfully) { if (!successfully) { LOG(LWARNING, ("Update check failed for url:", url)); - return; + if (m_observerUpdateCheck) + m_observerUpdateCheck(-1, NULL); + } + else + { // @TODO parse update file and notify GUI + if (m_observerUpdateCheck) + m_observerUpdateCheck(666000, "Arbaiten, Zmagary! Tasks for release:\n\n0. Fast YG\n1. Simplificator tuning\n2. Updater\n3. World map"); } // parse update file @@ -343,4 +353,9 @@ namespace storage // // @TODO report to GUI about reloading all countries // LOG(LINFO, ("Update check complete")); } + + void Storage::PerformUpdate() + { + // @TODO: + } } diff --git a/storage/storage.hpp b/storage/storage.hpp index dca6bf38e0..59a85d3571 100644 --- a/storage/storage.hpp +++ b/storage/storage.hpp @@ -56,8 +56,10 @@ namespace storage //@{ typedef boost::function TObserverChangeCountryFunction; typedef boost::function TObserverProgressFunction; + typedef boost::function TUpdateCheckFunction; TObserverChangeCountryFunction m_observerChange; TObserverProgressFunction m_observerProgress; + TUpdateCheckFunction m_observerUpdateCheck; //@} /// @name Communicate with Framework @@ -70,7 +72,6 @@ namespace storage void DownloadNextCountryFromQueue(); Country const & CountryByIndex(TIndex const & index) const; - bool UpdateCheck(); string UpdateBaseUrl() const; public: @@ -88,7 +89,9 @@ namespace storage /// @name Current impl supports only one observer //@{ - void Subscribe(TObserverChangeCountryFunction change, TObserverProgressFunction progress); + void Subscribe(TObserverChangeCountryFunction change, + TObserverProgressFunction progress, + TUpdateCheckFunction check); void Unsubscribe(); //@} @@ -99,5 +102,8 @@ namespace storage void DownloadCountry(TIndex const & index); void DeleteCountry(TIndex const & index); + + void CheckForUpdate(); + void PerformUpdate(); }; }