updaterinfoparser.cpp
1 //------------------------------------------------------------------------------
2 // updaterinfoparser.cpp
3 //------------------------------------------------------------------------------
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
9 //
10 // This program 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
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; 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) 2012 "Zalewa" <zalewapl@gmail.com>
22 //------------------------------------------------------------------------------
23 #include "updaterinfoparser.h"
24 
25 #include "updater/autoupdater.h"
26 #include "updater/updatepackage.h"
27 #include "json.h"
28 #include "log.h"
29 
30 DClass<UpdaterInfoParser>
31 {
32  public:
33  QList<UpdatePackage> packages;
34 };
35 
36 DPointered(UpdaterInfoParser)
39 {
40 }
41 
42 UpdaterInfoParser::~UpdaterInfoParser()
43 {
44 }
45 
46 const QList<UpdatePackage>& UpdaterInfoParser::packages() const
47 {
48  return d->packages;
49 }
50 
51 int UpdaterInfoParser::parse(const QByteArray& json)
52 {
53  d->packages.clear();
54  QVariant var = QtJson::Json::parse(json);
55  if (var.isValid())
56  {
57  QVariantMap metaData = var.toMap();
58  if (metaData.contains(AutoUpdater::MAIN_PROGRAM_PACKAGE_NAME))
59  {
60  foreach (const QString& package, metaData.keys())
61  {
62  int result = parsePackageNode(package, metaData[package].toMap());
63  if (result != AutoUpdater::EC_Ok)
64  {
65  return result;
66  }
67  }
68  }
69  else
70  {
72  }
73  }
74  else
75  {
77  }
78  return AutoUpdater::EC_Ok;
79 }
80 
81 int UpdaterInfoParser::parsePackageNode(const QString& packageName, const QVariantMap& map)
82 {
83  UpdatePackage package;
84  package.name = packageName;
85 
86  if (map.contains("revision"))
87  {
88  package.revision = map["revision"].toLongLong();
89  }
90  else
91  {
92  gLog << tr("Missing update revision info for package %1.").arg(packageName);
94  }
95 
96  if (map.contains("display-version"))
97  {
98  package.displayVersion = map["display-version"].toString();
99  }
100  else
101  {
102  package.displayVersion = QString::number(package.revision);
103  }
104 
105  if (map.contains("display-name"))
106  {
107  package.displayName = map["display-name"].toString();
108  }
109  else
110  {
111  package.displayName = packageName;
112  }
113 
114  if (map.contains("URL"))
115  {
116  QString strUrl = map["URL"].toString();
117  package.downloadUrl = strUrl;
118  if (!package.downloadUrl.isValid() || package.downloadUrl.isRelative())
119  {
120  gLog << tr("Invalid update download URL for package %1: %2")
121  .arg(packageName, strUrl);
123  }
124  }
125  else
126  {
127  gLog << tr("Missing update download URL for package %1.").arg(packageName);
129  }
130 
131  if (map.contains("URL-script"))
132  {
133  QString strUrl = map["URL-script"].toString();
134  package.downloadScriptUrl = strUrl;
135  if (!package.downloadScriptUrl.isValid() || package.downloadScriptUrl.isRelative())
136  {
137  gLog << tr("Invalid update script download URL for package %1, %2")
138  .arg(packageName, strUrl);
140  }
141  }
142  else
143  {
144  package.downloadScriptUrl = package.downloadUrl.toString() + ".xml";
145  }
146 
147  d->packages << package;
148  return AutoUpdater::EC_Ok;
149 }
QUrl downloadScriptUrl
Updater script download URL.
Definition: updatepackage.h:59
static QVariant parse(const QString &json)
Definition: json.cpp:69
One of packages has no revision info.
Definition: autoupdater.h:93
int parse(const QByteArray &json)
Parses updater info JSON and sets certain internal properties which can then be accessed through gett...
QUrl downloadUrl
Package download URL.
Definition: updatepackage.h:51
QUrl.isValid() for package download URL returned false or QUrl.isRelative() returned true...
Definition: autoupdater.h:102
QString displayName
Name displayed to the user.
Definition: updatepackage.h:45
One of packages has no download URL.
Definition: autoupdater.h:97
File was parseable but there was no main program information inside.
Definition: autoupdater.h:89
Updater info file can't be parsed.
Definition: autoupdater.h:84
QString displayVersion
Version displayed to the user.
Definition: updatepackage.h:41
static const QString MAIN_PROGRAM_PACKAGE_NAME
Package name for the main program.
Definition: autoupdater.h:131
unsigned long long revision
Revision number used for version comparison.
Definition: updatepackage.h:71
QString name
Name of the package (program name or plugin name).
Definition: updatepackage.h:63