changed pointers to values

This commit is contained in:
fourtf 2017-01-11 19:33:46 +01:00
parent 854566b57f
commit 0fbc40a52a
2 changed files with 37 additions and 38 deletions

View file

@ -12,14 +12,13 @@
Account *IrcManager::account = const_cast<Account *>(Account::anon()); Account *IrcManager::account = const_cast<Account *>(Account::anon());
IrcConnection *IrcManager::connection = NULL; IrcConnection *IrcManager::connection = NULL;
QMutex *IrcManager::connectionMutex = new QMutex(); QMutex IrcManager::connectionMutex;
long IrcManager::connectionIteration = 0; long IrcManager::connectionIteration = 0;
const QString IrcManager::defaultClientId = "7ue61iz46fz11y3cugd0l3tawb4taal"; const QString IrcManager::defaultClientId = "7ue61iz46fz11y3cugd0l3tawb4taal";
QNetworkAccessManager *IrcManager::m_accessManager = QNetworkAccessManager IrcManager::m_accessManager;
new QNetworkAccessManager();
QMap<QString, bool> *IrcManager::twitchBlockedUsers = new QMap<QString, bool>; QMap<QString, bool> IrcManager::twitchBlockedUsers;
QMutex *IrcManager::twitchBlockedUsersMutex = new QMutex(); QMutex IrcManager::twitchBlockedUsersMutex;
IrcManager::IrcManager() IrcManager::IrcManager()
{ {
@ -56,12 +55,12 @@ IrcManager::beginConnecting()
"&client_id=" + oauthClient; "&client_id=" + oauthClient;
QNetworkRequest req(QUrl(nextLink + "&oauth_token=" + oauthToken)); QNetworkRequest req(QUrl(nextLink + "&oauth_token=" + oauthToken));
QNetworkReply *reply = m_accessManager->get(req); QNetworkReply *reply = m_accessManager.get(req);
QObject::connect(reply, &QNetworkReply::finished, [=] { QObject::connect(reply, &QNetworkReply::finished, [=] {
twitchBlockedUsersMutex->lock(); twitchBlockedUsersMutex.lock();
twitchBlockedUsers->clear(); twitchBlockedUsers.clear();
twitchBlockedUsersMutex->unlock(); twitchBlockedUsersMutex.unlock();
QByteArray data = reply->readAll(); QByteArray data = reply->readAll();
QJsonDocument jsonDoc(QJsonDocument::fromJson(data)); QJsonDocument jsonDoc(QJsonDocument::fromJson(data));
@ -72,15 +71,15 @@ IrcManager::beginConnecting()
auto blocks = root.value("blocks").toArray(); auto blocks = root.value("blocks").toArray();
twitchBlockedUsersMutex->lock(); twitchBlockedUsersMutex.lock();
for (QJsonValue block : blocks) { for (QJsonValue block : blocks) {
QJsonObject user = QJsonObject user =
block.toObject().value("user").toObject(); block.toObject().value("user").toObject();
// display_name // display_name
twitchBlockedUsers->insert( twitchBlockedUsers.insert(
user.value("name").toString().toLower(), true); user.value("name").toString().toLower(), true);
} }
twitchBlockedUsersMutex->unlock(); twitchBlockedUsersMutex.unlock();
}); });
} }
@ -89,7 +88,7 @@ IrcManager::beginConnecting()
QNetworkRequest req(QUrl("https://api.twitch.tv/kraken/users/" + QNetworkRequest req(QUrl("https://api.twitch.tv/kraken/users/" +
username + "/emotes?oauth_token=" + username + "/emotes?oauth_token=" +
oauthToken + "&client_id=" + oauthClient)); oauthToken + "&client_id=" + oauthClient));
QNetworkReply *reply = m_accessManager->get(req); QNetworkReply *reply = m_accessManager.get(req);
QObject::connect(reply, &QNetworkReply::finished, [=] { QObject::connect(reply, &QNetworkReply::finished, [=] {
QByteArray data = reply->readAll(); QByteArray data = reply->readAll();
@ -101,15 +100,15 @@ IrcManager::beginConnecting()
auto blocks = root.value("blocks").toArray(); auto blocks = root.value("blocks").toArray();
twitchBlockedUsersMutex->lock(); twitchBlockedUsersMutex.lock();
for (QJsonValue block : blocks) { for (QJsonValue block : blocks) {
QJsonObject user = QJsonObject user =
block.toObject().value("user").toObject(); block.toObject().value("user").toObject();
// display_name // display_name
twitchBlockedUsers->insert( twitchBlockedUsers.insert(
user.value("name").toString().toLower(), true); user.value("name").toString().toLower(), true);
} }
twitchBlockedUsersMutex->unlock(); twitchBlockedUsersMutex.unlock();
}); });
} }
} }
@ -128,7 +127,7 @@ IrcManager::beginConnecting()
c->open(); c->open();
connectionMutex->lock(); connectionMutex.lock();
if (iteration == connectionIteration) { if (iteration == connectionIteration) {
delete connection; delete connection;
c->moveToThread(QCoreApplication::instance()->thread()); c->moveToThread(QCoreApplication::instance()->thread());
@ -136,20 +135,20 @@ IrcManager::beginConnecting()
} else { } else {
delete c; delete c;
} }
connectionMutex->unlock(); connectionMutex.unlock();
} }
void void
IrcManager::disconnect() IrcManager::disconnect()
{ {
connectionMutex->lock(); connectionMutex.lock();
if (connection != NULL) { if (connection != NULL) {
delete connection; delete connection;
connection = NULL; connection = NULL;
} }
connectionMutex->unlock(); connectionMutex.unlock();
} }
void void
@ -176,16 +175,16 @@ IrcManager::privateMessageReceived(IrcPrivateMessage *message)
bool bool
IrcManager::isTwitchBlockedUser(QString const &username) IrcManager::isTwitchBlockedUser(QString const &username)
{ {
twitchBlockedUsersMutex->lock(); twitchBlockedUsersMutex.lock();
auto iterator = twitchBlockedUsers->find(username); auto iterator = twitchBlockedUsers.find(username);
if (iterator == twitchBlockedUsers->end()) { if (iterator == twitchBlockedUsers.end()) {
twitchBlockedUsersMutex->unlock(); twitchBlockedUsersMutex.unlock();
return false; return false;
} }
twitchBlockedUsersMutex->unlock(); twitchBlockedUsersMutex.unlock();
return true; return true;
} }
@ -197,13 +196,13 @@ IrcManager::tryAddIgnoredUser(QString const &username, QString &errorMessage)
"&client_id=" + account->oauthClient()); "&client_id=" + account->oauthClient());
QNetworkRequest request(url); QNetworkRequest request(url);
auto reply = m_accessManager->put(request, QByteArray()); auto reply = m_accessManager.put(request, QByteArray());
reply->waitForReadyRead(10000); reply->waitForReadyRead(10000);
if (reply->error() == QNetworkReply::NoError) { if (reply->error() == QNetworkReply::NoError) {
twitchBlockedUsersMutex->lock(); twitchBlockedUsersMutex.lock();
twitchBlockedUsers->insert(username, true); twitchBlockedUsers.insert(username, true);
twitchBlockedUsersMutex->unlock(); twitchBlockedUsersMutex.unlock();
delete reply; delete reply;
return true; return true;
@ -231,13 +230,13 @@ IrcManager::tryRemoveIgnoredUser(QString const &username, QString &errorMessage)
"&client_id=" + account->oauthClient()); "&client_id=" + account->oauthClient());
QNetworkRequest request(url); QNetworkRequest request(url);
auto reply = m_accessManager->deleteResource(request); auto reply = m_accessManager.deleteResource(request);
reply->waitForReadyRead(10000); reply->waitForReadyRead(10000);
if (reply->error() == QNetworkReply::NoError) { if (reply->error() == QNetworkReply::NoError) {
twitchBlockedUsersMutex->lock(); twitchBlockedUsersMutex.lock();
twitchBlockedUsers->remove(username); twitchBlockedUsers.remove(username);
twitchBlockedUsersMutex->unlock(); twitchBlockedUsersMutex.unlock();
delete reply; delete reply;
return true; return true;

View file

@ -30,21 +30,21 @@ public:
static QNetworkAccessManager & static QNetworkAccessManager &
accessManager() accessManager()
{ {
return *m_accessManager; return m_accessManager;
} }
private: private:
IrcManager(); IrcManager();
static QMap<QString, bool> *twitchBlockedUsers; static QMap<QString, bool> twitchBlockedUsers;
static QMutex *twitchBlockedUsersMutex; static QMutex twitchBlockedUsersMutex;
static QNetworkAccessManager *m_accessManager; static QNetworkAccessManager m_accessManager;
static void beginConnecting(); static void beginConnecting();
static IrcConnection *connection; static IrcConnection *connection;
static QMutex *connectionMutex; static QMutex connectionMutex;
static long connectionIteration; static long connectionIteration;
static void messageReceived(IrcMessage *message); static void messageReceived(IrcMessage *message);