Attica

postjob.cpp
1 /*
2  This file is part of KDE.
3 
4  SPDX-FileCopyrightText: 2008 Cornelius Schumacher <[email protected]>
5 
6  SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
7 */
8 
9 #include "postjob.h"
10 
11 #include <QDebug>
12 #include <QXmlStreamReader>
13 
14 #include <QNetworkAccessManager>
15 
16 #include "platformdependent.h"
17 
18 using namespace Attica;
19 
20 PostJob::PostJob(PlatformDependent *internals, const QNetworkRequest &request, QIODevice *iodevice)
21  : BaseJob(internals)
22  , m_ioDevice(iodevice)
23  , m_request(request)
24 {
25 }
26 
27 Attica::PostJob::PostJob(PlatformDependent *internals, const QNetworkRequest &request, const QByteArray &byteArray)
28  : BaseJob(internals)
29  , m_ioDevice(nullptr)
30  , m_byteArray(byteArray)
31  , m_request(request)
32 {
33 }
34 
35 PostJob::PostJob(PlatformDependent *internals, const QNetworkRequest &request, const StringMap &parameters)
36  : BaseJob(internals)
37  , m_ioDevice(nullptr)
38  , m_request(request)
39 {
40  // Create post data
41  int j = 0;
42  for (StringMap::const_iterator i = parameters.begin(); i != parameters.end(); ++i) {
43  if (j++ > 0) {
44  m_byteArray.append('&');
45  }
46  m_byteArray.append(QUrl::toPercentEncoding(i.key()));
47  m_byteArray.append('=');
48  m_byteArray.append(QUrl::toPercentEncoding(i.value()));
49  }
50 }
51 
52 QNetworkReply *PostJob::executeRequest()
53 {
54  if (m_ioDevice) {
55  return internals()->post(m_request, m_ioDevice);
56  } else {
57  return internals()->post(m_request, m_byteArray);
58  }
59 }
60 
61 void PostJob::parse(const QString &xmlString)
62 {
63  // qCDebug(ATTICA) << "PostJob::parse" << xmlString;
64  QXmlStreamReader xml(xmlString);
65  Metadata data;
66  while (!xml.atEnd()) {
67  xml.readNext();
68 
69  if (xml.isStartElement()) {
70  if (xml.name() == QLatin1String("meta")) {
71  while (!xml.atEnd()) {
72  xml.readNext();
73  if (xml.isEndElement() && xml.name() == QLatin1String("meta")) {
74  break;
75  } else if (xml.isStartElement()) {
76  if (xml.name() == QLatin1String("status")) {
77  data.setStatusString(xml.readElementText());
78  } else if (xml.name() == QLatin1String("statuscode")) {
79  data.setStatusCode(xml.readElementText().toInt());
80  } else if (xml.name() == QLatin1String("message")) {
81  data.setMessage(xml.readElementText());
82  } else if (xml.name() == QLatin1String("totalitems")) {
83  data.setTotalItems(xml.readElementText().toInt());
84  } else if (xml.name() == QLatin1String("itemsperpage")) {
85  data.setItemsPerPage(xml.readElementText().toInt());
86  }
87  }
88  }
89  } else if (xml.name() == QLatin1String("data")) {
90  while (!xml.atEnd()) {
91  xml.readNext();
92  if (xml.isEndElement() && xml.name() == QLatin1String("data")) {
93  break;
94  } else if (xml.isStartElement()) {
95  if (xml.name() == QLatin1String("projectid")) {
96  data.setResultingId(xml.readElementText());
97  }
98  if (xml.name() == QLatin1String("buildjobid")) {
99  data.setResultingId(xml.readElementText());
100  }
101  }
102  }
103  }
104  }
105  }
106  setMetadata(data);
107 }
108 
109 #include "moc_postjob.cpp"
QMap::iterator begin()
QMap::iterator end()
QByteArray toPercentEncoding(const QString &input, const QByteArray &exclude, const QByteArray &include)
The Attica namespace,.
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Dec 11 2023 04:05:13 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.