-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
149 lines (122 loc) · 5.36 KB
/
main.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
#include "login.h"
#include "mainwindow.h"
#include <QApplication>
#include <QDebug>
#include <QEventLoop>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QLocale>
#include <QMessageBox>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QObject>
#include <QTranslator>
QString gDomain, gUser, gPassword;
QMap<QString, QMap<QString, QJsonObject>> gCommentsByPost;
QString gPostTitle, gCommentTitle;
QJsonArray getComments(QString domain, QString user, QString password, MainWindow* w);
QMap<QString, QMap<QString, QJsonObject>> getCommentsByPost(QJsonArray comments, MainWindow* w);
int main(int argc, char* argv[])
{
QApplication a(argc, argv);
QTranslator translator;
QLocale locale;
qApp->installTranslator(&translator);
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
if (locale.language() == QLocale::Chinese && (locale.territory() == QLocale::China || locale.territory() == QLocale::Singapore)) {
#else
if (locale.language() == QLocale::Chinese && (locale.country() == QLocale::China || locale.country() == QLocale::Singapore)) {
#endif
if (!translator.load(":/i18n/zh_CN.qm")) {
qApp->removeTranslator(&translator);
}
} else if (locale.language() == QLocale::Chinese) {
if (!translator.load(":/i18n/zh_TW.qm")) {
qApp->removeTranslator(&translator);
}
} else if (locale.language() == QLocale::Thai) {
if (!translator.load(":/i18n/th_TH.qm")) {
qApp->removeTranslator(&translator);
}
} else {
qApp->removeTranslator(&translator);
}
MainWindow w;
w.show();
Login login(&w);
if (login.exec() == QDialog::Rejected) {
return 0;
}
extern QString gDomain, gUser, gPassword;
QJsonArray comments = getComments(gDomain, gUser, gPassword, &w);
if (comments.isEmpty()) {
QMessageBox::critical(nullptr, QObject::tr("Error"), QObject::tr("No unapproved comments"));
return 0;
}
extern QMap<QString, QMap<QString, QJsonObject>> gCommentsByPost;
gCommentsByPost = getCommentsByPost(comments, &w);
w.set_comboBox_1(gCommentsByPost);
return a.exec();
}
QJsonArray getComments(QString domain, QString user, QString password, MainWindow* w)
{
w->set_placeholder(QString(QObject::tr("[ Reading comments... ]")));
QNetworkAccessManager manager;
QNetworkRequest request;
QNetworkReply* reply;
request.setUrl(QUrl("https://" + domain + "/wp-json/wp/v2/comments?status=hold&per_page=100"));
request.setRawHeader("Authorization", "Basic " + QByteArray(QString("%1:%2").arg(user).arg(password).toLatin1()).toBase64());
reply = manager.get(request);
QEventLoop loop;
QObject::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
loop.exec();
if (reply->error() != QNetworkReply::NoError) {
QMessageBox::critical(nullptr, QObject::tr("Error"), QObject::tr("Failed to read comments"));
qDebug() << reply->errorString();
return QJsonArray();
}
QByteArray data = reply->readAll();
QJsonDocument doc = QJsonDocument::fromJson(data);
return doc.array();
}
QMap<QString, QMap<QString, QJsonObject>> getCommentsByPost(QJsonArray comments, MainWindow* w)
{
w->set_placeholder(QString(QObject::tr("[ Reading post title... ]")));
QSet<int> postIds;
for (int i = 0; i < comments.size(); i++) {
postIds.insert(comments.at(i).toObject().value("post").toInt());
}
QMap<int, QString> postName;
for (int postId : postIds) {
QNetworkAccessManager manager;
QNetworkRequest request;
QNetworkReply* reply;
extern QString gDomain, gUser, gPassword;
request.setUrl(QUrl("https://" + gDomain + "/wp-json/wp/v2/posts/" + QString::number(postId)));
request.setRawHeader("Authorization", "Basic " + QByteArray(QString("%1:%2").arg(gUser).arg(gPassword).toLatin1()).toBase64());
reply = manager.get(request);
QEventLoop loop;
QObject::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
loop.exec();
if (reply->error() != QNetworkReply::NoError) {
QMessageBox::critical(nullptr, QObject::tr("Error"), QObject::tr("Failed to read post title"));
qDebug() << reply->errorString();
return QMap<QString, QMap<QString, QJsonObject>>();
}
QByteArray data = reply->readAll();
QJsonDocument doc = QJsonDocument::fromJson(data);
QString title = doc.object().value("title").toObject().value("rendered").toString();
postName.insert(postId, QString::number(postId) + " - " + title);
w->set_placeholder(QString(QObject::tr("[ Identified post ")) + title + QString(" ]"));
}
QMap<QString, QMap<QString, QJsonObject>> commentsByPost;
for (int i = 0; i < comments.size(); i++) {
QJsonObject comment = comments.at(i).toObject();
QString postTitle = postName.value(comment.value("post").toInt());
QString commentTitle = QString::number(comment.value("id").toInt()) + " - " + comment.value("author_name").toString() + " - " + comment.value("content").toObject().value("rendered").toString().simplified();
commentsByPost[postTitle][commentTitle.length() > 50 ? commentTitle.left(50) : commentTitle] = comment;
}
w->set_placeholder(QString(QObject::tr("[ Loading complete ]")));
return commentsByPost;
}