31 #include "gui/formupdate.h"
33 #include "definitions/definitions.h"
34 #include "miscellaneous/systemfactory.h"
35 #include "miscellaneous/iconfactory.h"
36 #include "network-web/webfactory.h"
37 #include "network-web/downloader.h"
38 #include "network-web/networkfactory.h"
39 #include "gui/custommessagebox.h"
40 #include "gui/systemtrayicon.h"
42 #include <QNetworkReply>
43 #include <QDesktopServices>
47 FormUpdate::FormUpdate(QWidget *parent)
48 : QDialog(parent), m_downloader(NULL), m_readyToInstall(false), m_ui(new Ui::
FormUpdate) {
52 setWindowFlags(Qt::MSWindowsFixedSizeDialogHint | Qt::Dialog | Qt::WindowSystemMenuHint | Qt::WindowTitleHint);
55 m_btnUpdate = m_ui->m_buttonBox->addButton(tr(
"Update"), QDialogButtonBox::ActionRole);
56 m_btnUpdate->setToolTip(tr(
"Download new installation files."));
58 connect(m_btnUpdate, SIGNAL(clicked()),
this, SLOT(startUpdate()));
61 m_ui->m_lblCurrentRelease->setText(APP_VERSION);
65 FormUpdate::~FormUpdate() {
70 return m_updateInfo.m_urls.keys().contains(OS_ID);
75 #if defined(Q_OS_WIN) || defined(Q_OS_OS2)
83 UpdateCheck update = qApp->checkForUpdates();
85 m_updateInfo = update.first;
87 if (update.second != QNetworkReply::NoError) {
89 m_ui->m_lblAvailableRelease->setText(tr(
"unknown"));
90 m_ui->m_txtChanges->clear();
91 m_ui->m_lblStatus->setStatus(WidgetWithStatus::Error,
93 tr(
"List with updates was "
94 "not\ndownloaded successfully."));
95 m_btnUpdate->setEnabled(
false);
96 m_btnUpdate->setToolTip(tr(
"Checking for updates failed."));
99 m_ui->m_lblAvailableRelease->setText(update.first.m_availableVersion);
100 m_ui->m_txtChanges->setText(update.first.m_changes);
102 if (update.first.m_availableVersion > APP_VERSION) {
103 m_ui->m_lblStatus->setStatus(WidgetWithStatus::Ok,
104 tr(
"New release available."),
105 tr(
"This is new version which can be\ndownloaded and installed."));
106 m_btnUpdate->setEnabled(
true);
108 tr(
"Download installation file for your OS.") :
109 tr(
"Installation file is not available directly.\n"
110 "Go to application website to obtain it manually."));
113 m_btnUpdate->setText(tr(
"Download update"));
117 m_ui->m_lblStatus->setStatus(WidgetWithStatus::Warning,
118 tr(
"No new release available."),
119 tr(
"This release is not newer than\ncurrently installed one."));
120 m_btnUpdate->setEnabled(
false);
121 m_btnUpdate->setToolTip(tr(
"No new update available."));
126 void FormUpdate::updateProgress(qint64 bytes_received, qint64 bytes_total) {
127 qApp->processEvents();
128 m_ui->m_lblStatus->setStatus(WidgetWithStatus::Information,
129 tr(
"Downloaded %1% (update size is %2 kB).").arg(QString::number((bytes_received * 100.0) / bytes_total,
132 QString::number(bytes_total / 1000,
135 tr(
"Downloading update..."));
138 void FormUpdate::saveUpdateFile(
const QByteArray &file_contents) {
139 QString url_file = m_updateInfo.m_urls.value(OS_ID).m_fileUrl;;
141 #if QT_VERSION >= 0x050000
142 QString temp_directory = QStandardPaths::writableLocation(QStandardPaths::TempLocation);
144 QString temp_directory = QDesktopServices::storageLocation(QDesktopServices::TempLocation);
147 if (!temp_directory.isEmpty()) {
148 QString output_file_name = url_file.mid(url_file.lastIndexOf(
'/') + 1);
149 QFile output_file(temp_directory + QDir::separator() + output_file_name);
151 if (output_file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
152 qDebug(
"Storing update file to temporary location '%s'.",
153 qPrintable(output_file.fileName()));
155 output_file.write(file_contents);
159 qDebug(
"Update file contents was successfuly saved.");
161 m_updateFilePath = output_file.fileName();
162 m_readyToInstall =
true;
165 qDebug(
"Cannot save downloaded update file because target temporary file '%s' cannot be "
166 "opened for writing.", qPrintable(output_file_name));
170 qDebug(
"Cannot save downloaded update file because no TEMP folder is available.");
174 void FormUpdate::updateCompleted(QNetworkReply::NetworkError status, QByteArray contents) {
175 qDebug(
"Download of application update file was completed with code '%d'.",
179 case QNetworkReply::NoError:
180 saveUpdateFile(contents);
182 m_ui->m_lblStatus->setStatus(WidgetWithStatus::Ok,
183 tr(
"Downloaded successfully, stored in file '%1'.").arg(m_updateFilePath),
184 tr(
"Package was downloaded successfully."));
185 m_btnUpdate->setText(tr(
"Open update file"));
186 m_btnUpdate->setEnabled(
true);
190 m_ui->m_lblStatus->setStatus(WidgetWithStatus::Error,
191 tr(
"Error occured."),
192 tr(
"Error occured during downloading of the package."));
193 m_btnUpdate->setText(tr(
"Error occured"));
198 void FormUpdate::startUpdate() {
202 if (update_for_this_system) {
203 url_file = m_updateInfo.m_urls.value(OS_ID).m_fileUrl;
206 url_file = APP_URL_DOWNLOADS;
209 if (!WebFactory::instance()->openUrlInExternalBrowser(url_file)) {
211 qApp->trayIcon()->showMessage(tr(
"Cannot update application"),
212 tr(
"Cannot navigate to installation file. Check new installation downloads "
213 "manually on project website."),
214 QSystemTrayIcon::Warning);
218 QMessageBox::Warning,
219 tr(
"Cannot update application"),
220 tr(
"Cannot navigate to installation file. Check new installation downloads "
221 "manually on project website."));
static QString networkErrorText(QNetworkReply::NetworkError error_code)
Gets human readable text for given network error.
static bool isSystemTrayAvailable()
Indicates whether tray icon is supported.
static QMessageBox::StandardButton show(QWidget *parent, QMessageBox::Icon icon, const QString &title, const QString &text, const QString &informative_text=QString(), const QString &detailed_text=QString(), QMessageBox::StandardButtons buttons=QMessageBox::Ok, QMessageBox::StandardButton default_button=QMessageBox::Ok)
Displays custom message box.
static IconFactory * instance()
Singleton getter.