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