-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
140 lines (124 loc) · 4.32 KB
/
main.cpp
File metadata and controls
140 lines (124 loc) · 4.32 KB
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
#include "loginwindow.h"
#include "logwindow.h"
#include "profileapiclient.h"
#include "usersession.h"
#include "widget.h"
#include <QApplication>
#include <QDateTime>
#include <QMetaObject>
#include <QRegularExpression>
#include <QtGlobal>
#include <cstdlib>
#include <cstdio>
namespace {
LogWindow *g_logWindow = nullptr;
QtMessageHandler g_previousHandler = nullptr;
QString messageTypeName(QtMsgType type) {
switch (type) {
case QtDebugMsg:
return "DEBUG";
case QtInfoMsg:
return "INFO";
case QtWarningMsg:
return "WARN";
case QtCriticalMsg:
return "ERROR";
case QtFatalMsg:
return "FATAL";
}
return "UNKNOWN";
}
void forwardLogToWindow(const QString &line) {
if (!g_logWindow)
return;
QMetaObject::invokeMethod(
g_logWindow,
[line]() {
if (g_logWindow) {
g_logWindow->appendLog(line);
}
},
Qt::QueuedConnection);
}
void appMessageHandler(QtMsgType type, const QMessageLogContext &context,
const QString &message) {
const QString timestamp = QDateTime::currentDateTime().toString("HH:mm:ss.zzz");
const QString category =
(context.category && context.category[0] != '\0') ? context.category : "app";
const QString line =
QString("%1 [%2] [%3] %4")
.arg(timestamp, messageTypeName(type), category, message);
std::fprintf(stderr, "%s\n", line.toLocal8Bit().constData());
forwardLogToWindow(line);
if (g_previousHandler) {
g_previousHandler(type, context, message);
}
if (type == QtFatalMsg) {
std::abort();
}
}
} // namespace
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
UserSession::instance().clear();
LogWindow logWindow;
g_logWindow = &logWindow;
g_previousHandler = qInstallMessageHandler(appMessageHandler);
logWindow.show();
// 创建登录窗口
LoginWindow loginWindow;
Widget mainWidget;
ProfileApiClient profileApiClient;
QString currentUserId;
mainWidget.setProfileApiClient(&profileApiClient);
const auto applyProfileToMainWidget =
[&](const ProfileInfo &info) {
const QString displayName = info.nickname.trimmed().isEmpty()
? currentUserId
: info.nickname.trimmed();
mainWidget.setUserInfo(displayName, info.avatarUrl);
};
QObject::connect(&profileApiClient, &ProfileApiClient::profileInfoReceived,
[&](const QString &requestId, const ProfileInfo &info) {
Q_UNUSED(requestId);
applyProfileToMainWidget(info);
qInfo() << "Profile GET_INFO success for user:" << currentUserId;
});
QObject::connect(&profileApiClient, &ProfileApiClient::profileInfoSetSuccess,
[&](const QString &requestId, const ProfileInfo &info) {
applyProfileToMainWidget(info);
qInfo() << "Profile SET_INFO success request_id:" << requestId;
});
QObject::connect(&profileApiClient, &ProfileApiClient::requestFailed,
[&](const QString &requestId, const QString &action,
const QString &error) {
qWarning() << "Profile request failed, action:" << action
<< "request_id:" << requestId << "error:" << error;
});
// 登录成功后显示主窗口
QObject::connect(&loginWindow, &LoginWindow::loginSuccess,
[&](const QString &username, const QString &userId) {
static const QRegularExpression kUnsignedIntRe(QStringLiteral("^\\d+$"));
currentUserId.clear();
loginWindow.close();
mainWidget.setUserInfo(username); // 设置用户信息
mainWidget.setWindowTitle("IM聊天 - " + username);
mainWidget.show();
const QString normalizedUserId = userId.trimmed();
if (kUnsignedIntRe.match(normalizedUserId).hasMatch()) {
currentUserId = normalizedUserId;
}
mainWidget.setCurrentUserId(currentUserId);
if (!currentUserId.isEmpty()) {
profileApiClient.requestProfileInfo(currentUserId);
} else {
qWarning() << "Skip PROFILE GET_INFO: missing numeric user_id from login response";
}
});
loginWindow.show();
const int exitCode = a.exec();
qInstallMessageHandler(g_previousHandler);
g_logWindow = nullptr;
return exitCode;
}