Skip to content

Commit

Permalink
Make main url configurable over config.ini
Browse files Browse the repository at this point in the history
Signed-off-by: Yunusemre Şentürk <[email protected]>
  • Loading branch information
Yunusemre Şentürk committed Jan 17, 2019
1 parent aaf481c commit 007d6ac
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 19 deletions.
36 changes: 35 additions & 1 deletion src/helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Helper::Helper(QObject *parent) : QObject(parent),
s = new QSettings(CONFIG_PATH, QSettings::IniFormat);
readSettings();

nh = new NetworkHandler(10000,this);
nh = new NetworkHandler(url(),port(),10000,this);
fh = new FileHandler(this);
ph = new PackageHandler(this);

Expand All @@ -38,6 +38,12 @@ Helper::Helper(QObject *parent) : QObject(parent),
connect(fh,SIGNAL(correctingSourcesFinishedWithError(QString)),this,SIGNAL(correctingFinishedWithError(QString)));
}

Helper::~Helper()
{
writeSettings("url",m_url);
writeSettings("port",m_port);
}

bool Helper::animate() const
{
return m_animate;
Expand Down Expand Up @@ -100,12 +106,40 @@ void Helper::setUsedark(bool d)
}
}

QString Helper::url() const
{
return m_url;
}

QString Helper::port() const
{
return m_port;
}

void Helper::setUrl(const QString &u)
{
if(m_url != u) {
m_url = u;
writeSettings("url",m_url);
}
}

void Helper::setPort(const QString &p)
{
if(m_port != p) {
m_port = p;
writeSettings("port",m_port);
}
}

void Helper::readSettings()
{
setAnimate(s->value("animate", true).toBool());
setUpdate(s->value("update", true).toBool());
setRatio(s->value("ratio", 3).toUInt());
setUsedark(s->value("dark-theme", true).toBool());
setUrl(s->value("url","http://store.pardus.org.tr").toString());
setPort(s->value("port","5000").toString());
}

void Helper::writeSettings(const QString &key, const QVariant &value)
Expand Down
7 changes: 7 additions & 0 deletions src/helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class Helper : public QObject

public:
explicit Helper(QObject *parent = 0);
~Helper();
bool processing() const;
bool animate() const;
void setAnimate(bool a);
Expand All @@ -79,6 +80,10 @@ class Helper : public QObject
void setRatio(const unsigned int &r);
bool usedark() const;
void setUsedark(bool d);
QString url() const;
QString port() const;
void setUrl(const QString &u);
void setPort(const QString &p);
QString choice() const;
QString version() const;
bool corrected() const;
Expand Down Expand Up @@ -129,6 +134,8 @@ class Helper : public QObject
bool m_update;
unsigned int m_ratio;
bool m_usedark;
QString m_url;
QString m_port;
void readSettings();
QStringList m_categories;
QHash<QString,QString> m_categorieswithlocal;
Expand Down
32 changes: 16 additions & 16 deletions src/networkhandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
#include <QNetworkRequest>
#include <QUrl>
#include <QNetworkInterface>
#include <QNetworkProxyFactory>
#include <QNetworkProxy>
#include <QNetworkProxyFactory>
#include <QNetworkProxyQuery>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
Expand All @@ -15,8 +16,6 @@
#include <QTimer>
#include <QDebug>

#define MAIN_URL "http://store.pardus.org.tr:5000"

namespace {

QTimer *timer_get(std::map<QNetworkReply *, QTimer *> *m, QNetworkReply *reply)
Expand Down Expand Up @@ -54,11 +53,12 @@ QNetworkReply *reply_get(std::map<QNetworkReply *, QTimer *> *m, QTimer *t)
}
}

NetworkHandler::NetworkHandler(int msec, QObject *parent) : QObject(parent),
m_timeoutDuration(msec)
NetworkHandler::NetworkHandler(const QString &url, const QString &port, int msec,
QObject *parent) : QObject(parent), m_timeoutDuration(msec)
{
QNetworkProxyQuery npq(QUrl(MAIN_URL));
QList<QNetworkProxy> listOfProxies = QNetworkProxyFactory::systemProxyForQuery(npq);
m_mainUrl = url.trimmed() + ":" + port.trimmed();
QNetworkProxyQuery *npq = new QNetworkProxyQuery(QUrl(m_mainUrl));
QList<QNetworkProxy> listOfProxies = QNetworkProxyFactory::systemProxyForQuery(*npq);
foreach ( QNetworkProxy p, listOfProxies ) {
if(p.hostName() != "") {
m_nam.setProxy(p);
Expand All @@ -83,15 +83,15 @@ void NetworkHandler::getApplicationList()
QTimer *timer = new QTimer();
connect(timer, SIGNAL(timeout()), this, SLOT(onTimeout()));
timer->setSingleShot(true);
reply = m_nam.get(QNetworkRequest(QUrl(QString(MAIN_URL).append("/api/v2/apps/"))));
reply = m_nam.get(QNetworkRequest(QUrl(m_mainUrl + QString("/api/v2/apps/"))));
timer_put(&m_timerMap, reply, timer);
timer->start(m_timeoutDuration);
}

void NetworkHandler::getApplicationDetails(const QString &packageName)
{
QNetworkReply *reply;
QString url = QString(MAIN_URL).append("/api/v2/apps/"+ packageName);
QString url = m_mainUrl + QString("/api/v2/apps/"+ packageName);
QTimer *timer = new QTimer();
connect(timer, SIGNAL(timeout()), this, SLOT(onTimeout()));
timer->setSingleShot(true);
Expand All @@ -102,7 +102,7 @@ void NetworkHandler::getApplicationDetails(const QString &packageName)

void NetworkHandler::ratingControl(const QString &name, const unsigned int &rating)
{
QString url(QString(MAIN_URL).append("/api/v2/rating"));
QString url(m_mainUrl + QString("/api/v2/rating"));
QNetworkRequest request(url);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");

Expand All @@ -123,7 +123,7 @@ void NetworkHandler::ratingControl(const QString &name, const unsigned int &rati
void NetworkHandler::getHomeDetails()
{
QNetworkReply *reply;
QString url = QString(MAIN_URL).append("/api/v2/home");
QString url = m_mainUrl + QString("/api/v2/home");
QTimer *timer = new QTimer();
connect(timer, SIGNAL(timeout()), this, SLOT(onTimeout()));
timer->setSingleShot(true);
Expand All @@ -134,7 +134,7 @@ void NetworkHandler::getHomeDetails()

void NetworkHandler::sendApplicationInstalled(const QString &name)
{
QString url(QString(MAIN_URL).append("/api/v2/statistics"));
QString url(m_mainUrl + QString("/api/v2/statistics"));
QNetworkRequest request(url);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");

Expand All @@ -153,7 +153,7 @@ void NetworkHandler::sendApplicationInstalled(const QString &name)

void NetworkHandler::surveyCheck()
{
QUrl url(QString(MAIN_URL).append("/api/v1/survey/list"));
QUrl url(m_mainUrl + QString("/api/v1/survey/list"));
QNetworkRequest request(url);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");

Expand All @@ -171,7 +171,7 @@ void NetworkHandler::surveyCheck()

void NetworkHandler::surveyJoin(const QString &appName, const QString &duty)
{
QUrl url(QString(MAIN_URL).append("/api/v1/survey/join"));
QUrl url(m_mainUrl + QString("/api/v1/survey/join"));
QNetworkRequest request(url);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");

Expand All @@ -191,7 +191,7 @@ void NetworkHandler::surveyJoin(const QString &appName, const QString &duty)

QString NetworkHandler::getMainUrl() const
{
return QString(MAIN_URL);
return m_mainUrl;
}

void NetworkHandler::replyFinished(QNetworkReply *reply)
Expand Down Expand Up @@ -311,7 +311,7 @@ void NetworkHandler::parseDetailsResponse(const QJsonObject &obj)
content.value("maintainer").toObject().value("name").toString());
ad.setName(content.value("name").toString());
foreach (const QVariant var, content.value("screenshots").toArray().toVariantList()) {
sl.append(QString(MAIN_URL).append(var.toString()));
sl.append(QString(m_mainUrl).append(var.toString()));
}
ad.setScreenshots(sl);
jo = content.value("section").toObject();
Expand Down
4 changes: 2 additions & 2 deletions src/networkhandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ class NetworkHandler : public QObject
{
Q_OBJECT
public:
explicit NetworkHandler(int msec = 10000, QObject *parent = 0);

explicit NetworkHandler(const QString &url, const QString &port,int msec = 10000, QObject *parent = 0);
void getApplicationList();
void getApplicationDetails(const QString &packageName);
void ratingControl(const QString &name, const unsigned int &rating);
Expand Down Expand Up @@ -51,6 +50,7 @@ private slots:
std::map<QNetworkReply *, QTimer *> m_timerMap;
int m_timeoutDuration;
QString m_macId;
QString m_mainUrl;
void parseAppsResponse(const QJsonObject &obj);
void parseDetailsResponse(const QJsonObject &obj);
void parseSurveyResponse(const QJsonObject &obj);
Expand Down

0 comments on commit 007d6ac

Please sign in to comment.