Entropy Piano Tuner  1.1.3 (documentation not yet complete)
An open-source experimental software for piano tuning by entropy minimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
versioncheck.cpp
Go to the documentation of this file.
1 /*****************************************************************************
2  * Copyright 2015 Haye Hinrichsen, Christoph Wick
3  *
4  * This file is part of Entropy Piano Tuner.
5  *
6  * Entropy Piano Tuner is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by the
8  * Free Software Foundation, either version 3 of the License, or (at your
9  * option) any later version.
10  *
11  * Entropy Piano Tuner is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14  * more details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * Entropy Piano Tuner. If not, see http://www.gnu.org/licenses/.
18  *****************************************************************************/
19 
20 #include "versioncheck.h"
21 
22 #include "thirdparty/tinyxml2/tinyxml2.h"
23 
25 #include "core/system/version.h"
26 #include "core/system/log.h"
27 #include "core/system/serverinfo.h"
28 
29 using tinyxml2::XMLElement;
30 using tinyxml2::XMLDocument;
31 
32 VersionCheck::VersionCheck(QObject *parent) : QNetworkAccessManager(parent)
33 {
34 #if CONFIG_ENABLE_UPDATE_TOOL
35  QUrl versionFileUrl(serverinfo::getVersionFileAddress().c_str());
36 
37  QObject::connect(this, SIGNAL(finished(QNetworkReply*)), this, SLOT(onNetworkReply(QNetworkReply*)));
38  this->get(QNetworkRequest(versionFileUrl));
39 #else
40  deleteLater();
41 #endif
42 }
43 
44 
45 void VersionCheck::onNetworkReply(QNetworkReply *reply) {
46 #if CONFIG_ENABLE_UPDATE_TOOL
47  if (reply->error() == QNetworkReply::NoError) {
48  int httpstatuscode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toUInt();
49  switch(httpstatuscode) {
50  case 301: { // redirecting
51  QUrl redirect = reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl().toString();
52  this->get(QNetworkRequest(redirect));
53  break;
54  }
55  case 200: // status code ok
56  if (reply->isReadable()) {
57  QString replyString = QString::fromUtf8(reply->readAll().data());
58  XMLDocument doc;
59  doc.Parse(replyString.toStdString().c_str());
60  if (doc.Error()) {
62  }
63 
64  XMLElement *root = doc.FirstChildElement();
65  EptAssert(root, "Root must exist.");
66 
67  XMLElement *appsElem = root->FirstChildElement("app");
68  XMLElement *depsElem = root->FirstChildElement("dependencies");
69 
70  if (!appsElem || !depsElem) {
71  LogW("Required nodes not found. Requesting update.");
72  emit updateAvailable({"Unknown", "Unknown"});
73  } else {
74  int appRolling = -1, depsRolling = -1;
75  appsElem->QueryIntAttribute("rolling", &appRolling);
76  depsElem->QueryIntAttribute("rolling", &depsRolling);
77 
78  QString appVersion("Unkown"), depsVersion("Unknown");
79  if (appsElem->Attribute("string")) {appVersion = appsElem->Attribute("string");}
80  if (depsElem->Attribute("string")) {depsVersion = depsElem->Attribute("string");}
81 
82  VersionInformation info = {appVersion, depsVersion};
83 
84  if (appRolling < 0 || depsRolling < 0) {
85  LogW("Required attributes not found. Requesting update.");
86  emit updateAvailable(info);
87  } else if (appRolling > EPT_VERSION_ROLLING || depsRolling > EPT_DEPS_VERSION_ROLLING) {
88  LogD("Update available");
89  emit updateAvailable(info);
90  } else {
91  LogV("No update available.");
92  }
93  }
94  } else {
95  LogE("File not readable");
96  }
97  // we did our job, delete us
98  this->deleteLater();
99  break;
100  default:
101  LogD("Invalid network status. Code (%i)", httpstatuscode);
102  break;
103  }
104  } else {
105  LogD("No network reply: %s.", reply->errorString().toStdString().c_str());
106  }
107 
108  reply->deleteLater();
109 #else
110  Q_UNUSED(reply);
111 #endif // CONFIG_ENABLE_UPDATE_TOOL
112 
113 }
114 
std::string getVersionFileAddress()
Definition: serverinfo.cpp:28
#define LogV(...)
Definition: log.h:38
#define LogW(...)
Definition: log.h:56
#define LogD(...)
Definition: log.h:44
#define EPT_EXCEPT(num, desc)
Definition: eptexception.h:119
VersionCheck(QObject *parent=0)
#define LogE(...)
Definition: log.h:62
#define EptAssert(a, b)
Definition: eptexception.h:47
void onNetworkReply(QNetworkReply *reply)
#define EPT_VERSION_ROLLING
Definition: version.h:31
#define EPT_DEPS_VERSION_ROLLING
Definition: version.h:41
static const std::string VERSION_FILENAME
Definition: serverinfo.h:35
void updateAvailable(VersionInformation info)