From 50866075b592a436d37a997346ab282c53e126f7 Mon Sep 17 00:00:00 2001 From: Rafael Date: Mon, 9 May 2016 10:24:34 +0200 Subject: [PATCH] showing name of crew and created date of picture --- AppEnv.cpp | 5 +++ AppEnv.h | 1 + CrewDatabase.cpp | 29 +++++++++++++++-- CrewDatabase.h | 2 ++ DatabaseThread.cpp | 77 ++++++++++++++++++++++++++++++++++++++++++++ DatabaseThread.h | 1 + PictureDialog.cpp | 14 ++++---- PictureDialog.h | 5 ++- PictureDialog.ui | 4 +-- ProfileInterface.cpp | 2 +- SnapmaticWidget.cpp | 6 ++-- SnapmaticWidget.h | 4 ++- UserInterface.cpp | 2 +- main.cpp | 9 +++--- 14 files changed, 140 insertions(+), 21 deletions(-) diff --git a/AppEnv.cpp b/AppEnv.cpp index 6dbe091..4303fe9 100755 --- a/AppEnv.cpp +++ b/AppEnv.cpp @@ -110,6 +110,11 @@ QByteArray AppEnv::getUserAgent() return QString("Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0 gta5sync/1.0").toUtf8(); } +QUrl AppEnv::getCrewFetchingUrl(QString crewID) +{ + return QUrl(QString("https://socialclub.rockstargames.com/reference/crewfeed/%1").arg(crewID)); +} + QUrl AppEnv::getPlayerFetchingUrl(QString crewID, QString pageNumber) { return QUrl(QString("https://socialclub.rockstargames.com/crewsapi/GetMembersList?crewId=%1&pageNumber=%2").arg(crewID, pageNumber)); diff --git a/AppEnv.h b/AppEnv.h index cc04915..ed24295 100755 --- a/AppEnv.h +++ b/AppEnv.h @@ -35,6 +35,7 @@ public: // Web Stuff static QByteArray getUserAgent(); + static QUrl getCrewFetchingUrl(QString crewID); static QUrl getPlayerFetchingUrl(QString crewID, QString pageNumber); }; diff --git a/CrewDatabase.cpp b/CrewDatabase.cpp index aae5683..9a34a50 100755 --- a/CrewDatabase.cpp +++ b/CrewDatabase.cpp @@ -47,10 +47,35 @@ CrewDatabase::~CrewDatabase() QStringList CrewDatabase::getCrews() { - return crewDB->childKeys(); + QStringList compatibleCrewList = crewDB->childKeys(); + crewDB->endGroup(); + crewDB->beginGroup("CrewList"); + QStringList crewIDs = crewDB->value("IDs", QStringList()).toStringList(); + crewIDs.append(compatibleCrewList); + crewIDs.removeDuplicates(); + crewDB->endGroup(); + crewDB->beginGroup("Crews"); + return crewIDs; +} + +QString CrewDatabase::getCrewName(int crewID) +{ + return crewDB->value(QString::number(crewID), crewID).toString(); +} + +void CrewDatabase::setCrewName(int crewID, QString crewName) +{ + crewDB->setValue(QString::number(crewID), crewName); } void CrewDatabase::addCrew(int crewID) { - crewDB->setValue(QString::number(crewID), crewID); + QStringList crews = getCrews(); + crews.append(QString::number(crewID)); + crews.removeDuplicates(); + crewDB->endGroup(); + crewDB->beginGroup("CrewList"); + crewDB->setValue("IDs", crews); + crewDB->endGroup(); + crewDB->beginGroup("Crews"); } diff --git a/CrewDatabase.h b/CrewDatabase.h index a480548..6204fe9 100755 --- a/CrewDatabase.h +++ b/CrewDatabase.h @@ -28,6 +28,8 @@ class CrewDatabase : public QObject Q_OBJECT public: explicit CrewDatabase(QObject *parent = 0); + void setCrewName(int crewID, QString crewName); + QString getCrewName(int crewID); QStringList getCrews(); ~CrewDatabase(); diff --git a/DatabaseThread.cpp b/DatabaseThread.cpp index 02eec2e..8ca7ac0 100755 --- a/DatabaseThread.cpp +++ b/DatabaseThread.cpp @@ -45,11 +45,13 @@ void DatabaseThread::run() // Quick time scan if (crewList.length() <= 3) { + scanCrewReference(crewList, 2500); scanCrewMembersList(crewList, 3, 2500); emit playerNameUpdated(); } else if (crewList.length() <= 5) { + scanCrewReference(crewList, 2500); scanCrewMembersList(crewList, 2, 2500); emit playerNameUpdated(); } @@ -64,6 +66,7 @@ void DatabaseThread::run() crewList = crewDB->getCrews(); // Long time scan + scanCrewReference(crewList, 10000); scanCrewMembersList(crewList, crewMaxPages, 10000); emit playerNameUpdated(); @@ -72,6 +75,80 @@ void DatabaseThread::run() } } +void DatabaseThread::scanCrewReference(QStringList crewList, int requestDelay) +{ + foreach (const QString &crewID, crewList) + { + if (crewID != "0") + { + QNetworkAccessManager *netManager = new QNetworkAccessManager(); + + QNetworkRequest netRequest(AppEnv::getCrewFetchingUrl(crewID)); + netRequest.setRawHeader("User-Agent", AppEnv::getUserAgent()); + netRequest.setRawHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); + netRequest.setRawHeader("Accept-Language", "en-US;q=0.5,en;q=0.3"); + netRequest.setRawHeader("Connection", "keep-alive"); + + QNetworkReply *netReply = netManager->get(netRequest); + + QEventLoop *downloadLoop = new QEventLoop(); + QObject::connect(netReply, SIGNAL(finished()), downloadLoop, SLOT(quit())); + QTimer::singleShot(30000, downloadLoop, SLOT(quit())); + downloadLoop->exec(); + delete downloadLoop; + + if (netReply->isFinished()) + { + QByteArray crewJson = netReply->readAll(); + QJsonDocument crewDocument = QJsonDocument::fromJson(crewJson); + QJsonObject crewObject = crewDocument.object(); + QVariantMap crewMap = crewObject.toVariantMap(); + QString crewName; + bool isFound = false; + + if (crewMap.contains("activities")) + { + QList activitiesList = crewMap["activities"].toList(); + foreach (const QVariant &activitiesVariant, activitiesList) + { + QMap activityRootMap = activitiesVariant.toMap(); + foreach(const QVariant &activityRootVariant, activityRootMap) + { + QMap activityMap = activityRootVariant.toMap(); + foreach(const QVariant &activityVariant, activityMap) + { + QMap activityFinalMap = activityVariant.toMap(); + if (activityFinalMap.contains("id") && activityFinalMap["id"] == crewID) + { + if (activityFinalMap.contains("name") && isFound == false) + { + isFound = true; + crewName = activityFinalMap["name"].toString(); + } + } + } + } + } + } + if (!crewName.isNull()) + { + crewDB->setCrewName(crewID.toInt(), crewName); + } + } + + QEventLoop *waitingLoop = new QEventLoop(); + QTimer::singleShot(requestDelay, waitingLoop, SLOT(quit())); + waitingLoop->exec(); + delete waitingLoop; + + netReply->deleteLater(); + delete netReply; + netManager->deleteLater(); + delete netManager; + } + } +} + void DatabaseThread::scanCrewMembersList(QStringList crewList, int maxPages, int requestDelay) { foreach (const QString &crewID, crewList) diff --git a/DatabaseThread.h b/DatabaseThread.h index a272bf1..068b55c 100755 --- a/DatabaseThread.h +++ b/DatabaseThread.h @@ -32,6 +32,7 @@ public: private: CrewDatabase *crewDB; void scanCrewMembersList(QStringList crewList, int maxPages, int requestDelay); + void scanCrewReference(QStringList crewList, int requestDelay); bool threadRunning; int crewMaxPages; int plyrPerReq; diff --git a/PictureDialog.cpp b/PictureDialog.cpp index 32d1337..718d0f3 100755 --- a/PictureDialog.cpp +++ b/PictureDialog.cpp @@ -44,8 +44,8 @@ #include #include -PictureDialog::PictureDialog(ProfileDatabase *profileDB, QWidget *parent) : - QDialog(parent), profileDB(profileDB), +PictureDialog::PictureDialog(ProfileDatabase *profileDB, CrewDatabase *crewDB, QWidget *parent) : + QDialog(parent), profileDB(profileDB), crewDB(crewDB), ui(new Ui::PictureDialog) { ui->setupUi(this); @@ -59,6 +59,7 @@ PictureDialog::PictureDialog(ProfileDatabase *profileDB, QWidget *parent) : picArea = ""; picTitl = ""; picPath = ""; + created = ""; crewID = ""; locX = ""; locY = ""; @@ -178,7 +179,8 @@ void PictureDialog::setSnapmaticPicture(SnapmaticPicture *picture, QString pictu locX = QString::number(picture->getLocationX()); locY = QString::number(picture->getLocationY()); locZ = QString::number(picture->getLocationZ()); - crewID = QString::number(picture->getCrewNumber()); + crewID = crewDB->getCrewName(picture->getCrewNumber()); + created = picture->getCreatedDateTime().toString(Qt::DefaultLocaleShortDate); plyrsList = picture->getPlayers(); picTitl = picture->getPictureTitl(); picArea = picture->getArea(); @@ -214,8 +216,8 @@ void PictureDialog::setSnapmaticPicture(SnapmaticPicture *picture, QString pictu if (crewID == "") { crewID = tr("No crew"); } - this->setWindowTitle(windowTitleStr.arg(picture->getCreatedDateTime().toString(Qt::DefaultLocaleLongDate))); - ui->labJSON->setText(jsonDrawString.arg(locX, locY, locZ, plyrsStr, crewID, picTitl, picAreaStr)); + this->setWindowTitle(windowTitleStr.arg(picture->getPictureStr())); + ui->labJSON->setText(jsonDrawString.arg(locX, locY, locZ, plyrsStr, crewID, picTitl, picAreaStr, created)); } else { @@ -279,7 +281,7 @@ void PictureDialog::playerNameUpdated() plyrsStr.append(""); } plyrsStr.remove(0,2); - ui->labJSON->setText(jsonDrawString.arg(locX, locY, locZ, plyrsStr, crewID, picTitl, picAreaStr)); + ui->labJSON->setText(jsonDrawString.arg(locX, locY, locZ, plyrsStr, crewID, picTitl, picAreaStr, created)); } } diff --git a/PictureDialog.h b/PictureDialog.h index e3cbf7a..8ae39e9 100755 --- a/PictureDialog.h +++ b/PictureDialog.h @@ -21,6 +21,7 @@ #include "SnapmaticPicture.h" #include "ProfileDatabase.h" +#include "CrewDatabase.h" #include #include #include @@ -34,7 +35,7 @@ class PictureDialog : public QDialog { Q_OBJECT public: - explicit PictureDialog(ProfileDatabase *profileDB, QWidget *parent = 0); + explicit PictureDialog(ProfileDatabase *profileDB, CrewDatabase *crewDB, QWidget *parent = 0); void setSnapmaticPicture(SnapmaticPicture *picture, QString picPath, bool readOk, bool indexed, int index); void setSnapmaticPicture(SnapmaticPicture *picture, QString picPath, bool readOk); void setSnapmaticPicture(SnapmaticPicture *picture, QString picPath); @@ -71,6 +72,7 @@ protected: private: ProfileDatabase *profileDB; + CrewDatabase *crewDB; Ui::PictureDialog *ui; QMap globalMap; SnapmaticPicture *smpic; @@ -85,6 +87,7 @@ private: QString picArea; QString picTitl; QString picPath; + QString created; QString crewID; QString locX; QString locY; diff --git a/PictureDialog.ui b/PictureDialog.ui index cfe5345..7985e14 100755 --- a/PictureDialog.ui +++ b/PictureDialog.ui @@ -80,8 +80,8 @@ <span style=" font-weight:600;">Title: </span>%6<br/> <span style=" font-weight:600;">Location: </span>%7 (%1, %2, %3)<br/> -<span style=" font-weight:600;">Players: </span>%4<br/> -<span style=" font-weight:600;">Crew ID: </span>%5 +<span style=" font-weight:600;">Players: </span>%4 (Crew %5)<br/> +<span style=" font-weight:600;">Created: </span>%8<br/> true diff --git a/ProfileInterface.cpp b/ProfileInterface.cpp index d4feac0..ddc5e55 100755 --- a/ProfileInterface.cpp +++ b/ProfileInterface.cpp @@ -136,7 +136,7 @@ void ProfileInterface::pictureLoaded(SnapmaticPicture *picture, QString pictureP void ProfileInterface::pictureLoaded_f(SnapmaticPicture *picture, QString picturePath, bool inserted) { - SnapmaticWidget *picWidget = new SnapmaticWidget(profileDB, threadDB); + SnapmaticWidget *picWidget = new SnapmaticWidget(profileDB, crewDB, threadDB); picWidget->setSnapmaticPicture(picture, picturePath); picWidget->setContentMode(contentMode); widgets[picWidget] = "PIC" + picture->getPictureSortStr(); diff --git a/SnapmaticWidget.cpp b/SnapmaticWidget.cpp index c099403..bf01ed8 100755 --- a/SnapmaticWidget.cpp +++ b/SnapmaticWidget.cpp @@ -30,8 +30,8 @@ #include #include -SnapmaticWidget::SnapmaticWidget(ProfileDatabase *profileDB, DatabaseThread *threadDB, QWidget *parent) : - ProfileWidget(parent), profileDB(profileDB), threadDB(threadDB), +SnapmaticWidget::SnapmaticWidget(ProfileDatabase *profileDB, CrewDatabase *crewDB, DatabaseThread *threadDB, QWidget *parent) : + ProfileWidget(parent), profileDB(profileDB), crewDB(crewDB), threadDB(threadDB), ui(new Ui::SnapmaticWidget) { ui->setupUi(this); @@ -95,7 +95,7 @@ void SnapmaticWidget::setSnapmaticPicture(SnapmaticPicture *picture) void SnapmaticWidget::on_cmdView_clicked() { - PictureDialog *picDialog = new PictureDialog(profileDB, this); + PictureDialog *picDialog = new PictureDialog(profileDB, crewDB, this); picDialog->setWindowFlags(picDialog->windowFlags()^Qt::WindowContextHelpButtonHint); picDialog->setSnapmaticPicture(smpic, picPath, true); picDialog->setModal(true); diff --git a/SnapmaticWidget.h b/SnapmaticWidget.h index 0ee9fc4..1c85fff 100755 --- a/SnapmaticWidget.h +++ b/SnapmaticWidget.h @@ -24,6 +24,7 @@ #include "ProfileDatabase.h" #include "DatabaseThread.h" #include "ProfileWidget.h" +#include "CrewDatabase.h" #include #include #include @@ -38,7 +39,7 @@ class SnapmaticWidget : public ProfileWidget Q_OBJECT public: - SnapmaticWidget(ProfileDatabase *profileDB, DatabaseThread *threadDB, QWidget *parent = 0); + SnapmaticWidget(ProfileDatabase *profileDB, CrewDatabase *crewDB, DatabaseThread *threadDB, QWidget *parent = 0); void setSnapmaticPicture(SnapmaticPicture *picture, QString picturePath); void setSnapmaticPicture(SnapmaticPicture *picture); void setSelectionMode(bool selectionMode); @@ -69,6 +70,7 @@ protected: private: ProfileDatabase *profileDB; + CrewDatabase *crewDB; DatabaseThread *threadDB; Ui::SnapmaticWidget *ui; SnapmaticPicture *smpic; diff --git a/UserInterface.cpp b/UserInterface.cpp index 7ceb684..3ce3f28 100755 --- a/UserInterface.cpp +++ b/UserInterface.cpp @@ -406,7 +406,7 @@ bool UserInterface::openFile(QString selectedFile, bool warn) void UserInterface::openSnapmaticFile(SnapmaticPicture *picture) { - PictureDialog *picDialog = new PictureDialog(profileDB, this); + PictureDialog *picDialog = new PictureDialog(profileDB, crewDB, this); picDialog->setWindowFlags(picDialog->windowFlags()^Qt::WindowContextHelpButtonHint); picDialog->setSnapmaticPicture(picture, true); picDialog->setModal(true); diff --git a/main.cpp b/main.cpp index 35adda2..263b91e 100755 --- a/main.cpp +++ b/main.cpp @@ -252,6 +252,10 @@ int main(int argc, char *argv[]) } } } + else if (language == "en" || language == "English") + { + QLocale::setDefault(QLocale(QLocale::English, QLocale::AnyCountry)); + } else { QString languageName = language; @@ -360,9 +364,6 @@ int main(int argc, char *argv[]) #endif // End internal translate loading - QLocale locale; - qDebug() << locale.nativeLanguageName(); - QStringList applicationArgs = a.arguments(); QString selectedAction; QString arg1; @@ -416,7 +417,7 @@ int main(int argc, char *argv[]) CrewDatabase *crewDB = new CrewDatabase(); ProfileDatabase *profileDB = new ProfileDatabase(); DatabaseThread *threadDB = new DatabaseThread(crewDB); - PictureDialog *picDialog = new PictureDialog(profileDB); + PictureDialog *picDialog = new PictureDialog(profileDB, crewDB); SnapmaticPicture picture; bool readOk = picture.readingPictureFromFile(arg1);