forked from saa7go/qDebDownloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aptweb.cpp
157 lines (142 loc) · 4.72 KB
/
aptweb.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/* This file is part of qDebDownloader.
*
* Copyright (c) 2011 - Christian Kurniawan Ginting S. <[email protected]>
*
* qDebDownloader is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* qDebDownloader is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with qDebDownloader. If not, see <http://www.gnu.org/licenses/>. */
#include "aptweb.h"
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QDebug>
#include <QApplication>
#include <QFile>
#include <QStringList>
AptWeb::AptWeb(QNetworkAccessManager *manager, QObject *parent) :
QObject(parent), m_manager(manager), m_repoId(0)
{
qDebug() << this << "Created!";
}
AptWeb::~AptWeb()
{
qDebug() << this << "Destroyed";
}
void AptWeb::findPackage(const QStringList &packagesName, Distribution dist)
{
findPackage(packagesName.join(" "), dist);
}
void AptWeb::findPackage(const QString &packagesName, Distribution dist)
{
QNetworkRequest request;
int dist_num = 0;
switch(dist)
{
case Ubuntu_MaverickMeerkat_i386:
dist_num = 0; break;
case Ubuntu_MaverickMeerkat_amd64:
dist_num = 1; break;
case Ubuntu_LucidLynx_i386:
dist_num = 2; break;
case Ubuntu_LucidLynx_amd64:
dist_num = 3; break;
case Ubuntu_KarmicKoala_i386:
dist_num = 4; break;
case Ubuntu_JauntyJackalope_i386:
dist_num = 5; break;
case Ubuntu_IntrepidIbex_i386:
dist_num = 6; break;
case Ubuntu_HardyHeron_i386:
dist_num = 7; break;
case Ubuntu_GutsyGibbon_i386:
dist_num = 8; break;
case XUbuntu_GutsyGibbon_i386:
dist_num = 9; break;
case Ubuntu_FeistyFawn_i386:
dist_num = 10; break;
case Ubuntu_EdgyEft_Server_i386:
dist_num = 11; break;
default: // Ubuntu_DapperDrake_i386
dist_num = 12; break;
}
QUrl data;
data.addQueryItem("dist", QString::number(dist_num));
data.addQueryItem("mirror", QString::number(m_repoId));
data.addQueryItem("packages", packagesName);
data.addQueryItem("submit", "submit");
const QByteArray data_encoded = data.toString().remove(0, 1).toAscii();
qDebug() << data_encoded;
request.setUrl(QUrl("http://apt-web.dahsy.at"));
request.setRawHeader("User-Agent", "AptWeb-Qt 0.1");
request.setRawHeader("Referer", "http://apt-web.dahsy.at/");
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
request.setHeader(QNetworkRequest::ContentLengthHeader, data_encoded.size());
QNetworkReply *reply = m_manager->post(request, data_encoded);
connect(reply, SIGNAL(finished()), this, SLOT(replyFinished()));
}
void AptWeb::replyFinished()
{
QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender());
reply->deleteLater();
if(reply->error() != QNetworkReply::NoError)
{
QString _error;
if(reply->error() == QNetworkReply::HostNotFoundError)
_error = "Host tidak ditemukan. Periksa koneksi internet anda.";
else if(reply->error() == QNetworkReply::TimeoutError)
_error = "Koneksi timeout.";
else if(reply->error() == QNetworkReply::ProxyNotFoundError)
_error = "Proxy gagal ditemukan. Periksa konfigurasi internet anda";
else
_error = reply->errorString();
emit error(_error);
return;
}
parser(QString(reply->readAll()));
}
void AptWeb::parser(const QString &html)
{
QString tmp = html;
QTextStream stream(&tmp);
QStringList urlList;
bool found = false;
while(!found && !stream.atEnd())
{
if(stream.readLine() == "<h2>URLs</h2>")
found = true;
}
if(found)
{
bool finished = false;
while(!finished)
{
QString text = stream.readLine();
if(text == "</ul>")
finished = true;
else
{
QString pattern = "<li><a href=\"";
if(text.length() < pattern.length())
continue;
int end_pos = text.indexOf('\"', pattern.length());
urlList.append(text.mid(pattern.length(), end_pos-pattern.length()));
}
}
}
emit downloadList(urlList);
}
void AptWeb::setRepoId(int repoId)
{
if(repoId == m_repoId)
return;
m_repoId = repoId;
}