File indexing completed on 2024-09-15 06:28:16
0001 /* 0002 This file is part of KDE. 0003 0004 SPDX-FileCopyrightText: 2008 Cornelius Schumacher <schumacher@kde.org> 0005 0006 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL 0007 */ 0008 0009 #include "postjob.h" 0010 0011 #include <QDebug> 0012 #include <QXmlStreamReader> 0013 0014 #include <QNetworkAccessManager> 0015 0016 #include "platformdependent.h" 0017 0018 using namespace Attica; 0019 0020 PostJob::PostJob(PlatformDependent *internals, const QNetworkRequest &request, QIODevice *iodevice) 0021 : BaseJob(internals) 0022 , m_ioDevice(iodevice) 0023 , m_request(request) 0024 { 0025 } 0026 0027 Attica::PostJob::PostJob(PlatformDependent *internals, const QNetworkRequest &request, const QByteArray &byteArray) 0028 : BaseJob(internals) 0029 , m_ioDevice(nullptr) 0030 , m_byteArray(byteArray) 0031 , m_request(request) 0032 { 0033 } 0034 0035 PostJob::PostJob(PlatformDependent *internals, const QNetworkRequest &request, const StringMap ¶meters) 0036 : BaseJob(internals) 0037 , m_ioDevice(nullptr) 0038 , m_request(request) 0039 { 0040 // Create post data 0041 int j = 0; 0042 for (StringMap::const_iterator i = parameters.begin(); i != parameters.end(); ++i) { 0043 if (j++ > 0) { 0044 m_byteArray.append('&'); 0045 } 0046 m_byteArray.append(QUrl::toPercentEncoding(i.key())); 0047 m_byteArray.append('='); 0048 m_byteArray.append(QUrl::toPercentEncoding(i.value())); 0049 } 0050 } 0051 0052 QNetworkReply *PostJob::executeRequest() 0053 { 0054 if (m_ioDevice) { 0055 return internals()->post(m_request, m_ioDevice); 0056 } else { 0057 return internals()->post(m_request, m_byteArray); 0058 } 0059 } 0060 0061 void PostJob::parse(const QString &xmlString) 0062 { 0063 // qCDebug(ATTICA) << "PostJob::parse" << xmlString; 0064 QXmlStreamReader xml(xmlString); 0065 Metadata data; 0066 while (!xml.atEnd()) { 0067 xml.readNext(); 0068 0069 if (xml.isStartElement()) { 0070 if (xml.name() == QLatin1String("meta")) { 0071 while (!xml.atEnd()) { 0072 xml.readNext(); 0073 if (xml.isEndElement() && xml.name() == QLatin1String("meta")) { 0074 break; 0075 } else if (xml.isStartElement()) { 0076 if (xml.name() == QLatin1String("status")) { 0077 data.setStatusString(xml.readElementText()); 0078 } else if (xml.name() == QLatin1String("statuscode")) { 0079 data.setStatusCode(xml.readElementText().toInt()); 0080 } else if (xml.name() == QLatin1String("message")) { 0081 data.setMessage(xml.readElementText()); 0082 } else if (xml.name() == QLatin1String("totalitems")) { 0083 data.setTotalItems(xml.readElementText().toInt()); 0084 } else if (xml.name() == QLatin1String("itemsperpage")) { 0085 data.setItemsPerPage(xml.readElementText().toInt()); 0086 } 0087 } 0088 } 0089 } else if (xml.name() == QLatin1String("data")) { 0090 while (!xml.atEnd()) { 0091 xml.readNext(); 0092 if (xml.isEndElement() && xml.name() == QLatin1String("data")) { 0093 break; 0094 } else if (xml.isStartElement()) { 0095 if (xml.name() == QLatin1String("projectid")) { 0096 data.setResultingId(xml.readElementText()); 0097 } 0098 if (xml.name() == QLatin1String("buildjobid")) { 0099 data.setResultingId(xml.readElementText()); 0100 } 0101 } 0102 } 0103 } 0104 } 0105 } 0106 setMetadata(data); 0107 } 0108 0109 #include "moc_postjob.cpp"