updaterinfoparser.cpp
1 //------------------------------------------------------------------------------
2 // updaterinfoparser.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) 2012 "Zalewa" <zalewapl@gmail.com>
22 //------------------------------------------------------------------------------
23 #include "updaterinfoparser.h"
24 
25 #include "log.h"
26 #include "updater/autoupdater.h"
27 #include "updater/updatepackage.h"
28 
29 #include <QJsonDocument>
30 
31 DClass<UpdaterInfoParser>
32 {
33 public:
34  QList<UpdatePackage> packages;
35 
36  bool hasMainProgramName(const QVariantMap &metaData) const
37  {
38  return metaData.contains(AutoUpdater::MAIN_PROGRAM_PACKAGE_NAME);
39  }
40 };
41 
42 DPointered(UpdaterInfoParser)
45 {
46 }
47 
48 UpdaterInfoParser::~UpdaterInfoParser()
49 {
50 }
51 
52 const QList<UpdatePackage> &UpdaterInfoParser::packages() const
53 {
54  return d->packages;
55 }
56 
57 int UpdaterInfoParser::parse(const QByteArray &json)
58 {
59  d->packages.clear();
60  auto var = QJsonDocument::fromJson(json).toVariant();
61  if (var.isValid())
62  {
63  QVariantMap metaData = var.toMap();
64  if (d->hasMainProgramName(metaData))
65  {
66  for (const QString &package : metaData.keys())
67  {
68  int result = parsePackageNode(package, metaData[package].toMap());
69  if (result != AutoUpdater::EC_Ok)
70  {
71  return result;
72  }
73  }
74  }
75  else
76  {
78  }
79  }
80  else
81  {
83  }
84  return AutoUpdater::EC_Ok;
85 }
86 
87 int UpdaterInfoParser::parsePackageNode(const QString &packageName, const QVariantMap &map)
88 {
89  UpdatePackage package;
90  package.name = packageName;
91 
92  if (map.contains("revision"))
93  {
94  package.revision = map["revision"].toString();
95  }
96  else
97  {
98  gLog << tr("Missing update revision info for package %1.").arg(packageName);
100  }
101 
102  if (map.contains("display-version"))
103  {
104  package.displayVersion = map["display-version"].toString();
105  }
106  else
107  {
108  package.displayVersion = package.revision;
109  }
110 
111  if (map.contains("display-name"))
112  {
113  package.displayName = map["display-name"].toString();
114  }
115  else
116  {
117  package.displayName = packageName;
118  }
119 
120  if (map.contains("URL"))
121  {
122  QString strUrl = map["URL"].toString();
123  package.downloadUrl = strUrl;
124  if (!package.downloadUrl.isValid() || package.downloadUrl.isRelative())
125  {
126  gLog << tr("Invalid update download URL for package %1: %2")
127  .arg(packageName, strUrl);
129  }
130  }
131  else
132  {
133  gLog << tr("Missing update download URL for package %1.").arg(packageName);
135  }
136 
137  if (map.contains("URL-script"))
138  {
139  QString strUrl = map["URL-script"].toString();
140  package.downloadScriptUrl = strUrl;
141  if (!package.downloadScriptUrl.isValid() || package.downloadScriptUrl.isRelative())
142  {
143  gLog << tr("Invalid update script download URL for package %1, %2")
144  .arg(packageName, strUrl);
146  }
147  }
148  else
149  {
150  package.downloadScriptUrl = package.downloadUrl.toString() + ".xml";
151  }
152 
153  d->packages << package;
154  return AutoUpdater::EC_Ok;
155 }