Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: some minor optimizations #75

Merged
merged 1 commit into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

- [x] 快速(单点)登录社区论坛

- [ ] 提交问题反馈和需求
- [x] 提交问题反馈和需求

- [ ] 社区论坛消息提醒

Expand Down
1 change: 0 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
set(LIBRARY_OUTPUT_PATH "${CMAKE_CURRENT_SOURCE_DIR}/lib")
add_definitions(-DLOCALLIBPATH="${LIBRARY_OUTPUT_PATH}")
add_definitions(-DDEBUG)
endif()

add_subdirectory(main)
Expand Down
8 changes: 4 additions & 4 deletions src/main/account.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Account::Account(QObject *parent, API *api, QString server)
});
// 启动后检查deepinid是否登陆
QTimer::singleShot(1000, [this]() {
qDebug() << m_deepinidDaemon->isLogin();
qCDebug(logger) << "deepinid is login" << m_deepinidDaemon->isLogin();
if(m_deepinidDaemon->isLogin()) {
this->clientLogin();
}
Expand All @@ -53,7 +53,7 @@ Account::~Account() {}
// state 用于区分登录用途
void Account::authorized(QString code, QString state)
{
qDebug() << "account authorized";
qCDebug(logger) << "account authorized";
// 论坛登陆
if (state == "bbs") {
auto url = m_api->getForumURL(m_server, code);
Expand Down Expand Up @@ -106,7 +106,7 @@ bool Account::isLogin()
// 客户端自己的登陆
void Account::clientLogin()
{
qDebug() << "client login";
qCDebug(logger) << "client login";
// 已登录,则先获取code,再用code获取自动登录的论坛地址
auto opt = m_api->getLoginOption(m_server);
// 注册登录回调接口
Expand All @@ -120,7 +120,7 @@ void Account::clientLogin()
UserInfo Account::getUserInfo()
{
auto logininfo = m_api->getLoginInfo(m_server, m_token);
qDebug() << logininfo.user_id;
qCDebug(logger) << logininfo.user_id;
UserInfo info;
info.uid = logininfo.user_id;
info.nickname = logininfo.nickname;
Expand Down
2 changes: 1 addition & 1 deletion src/main/account.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Account : public QObject
DeepinidClientProxy *m_deepinidClient;
DeepinidDaemonProxy *m_deepinidDaemon;
PropertiesChangedProxy *m_deepinidDaemonPropertiesChanged;

QLoggingCategory logger = QLoggingCategory("account");
public:
explicit Account(QObject *parent, API *api, QString node);
~Account();
Expand Down
37 changes: 19 additions & 18 deletions src/main/homeDaemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,13 @@ void HomeDaemon::initSysTrayIcon()
QString HomeDaemon::getMachineID()
{
auto machineID = m_settings.value("machineID").toString();
qDebug() << "try to get machine id from setting";
qCDebug(logger) << "try to get machine id from setting";
if (!machineID.isEmpty()) {
return machineID;
}
machineID = newUUID();
m_settings.setValue("machineID", machineID);
qDebug() << "new machine id saved to setting";
qCDebug(logger) << "new machine id saved to setting";
return machineID;
}

Expand Down Expand Up @@ -138,7 +138,7 @@ QStringList HomeDaemon::getChannels()
void HomeDaemon::markRead(QString channel, QString topic, QString uuid)
{
auto settingKey = messageSettingKey(channel, topic, uuid);
qDebug() << "mark read" << settingKey;
qCDebug(logger) << "mark read" << settingKey;
m_settings.beginGroup("messages");
m_settings.setValue(settingKey, "read");
m_settings.endGroup();
Expand All @@ -150,7 +150,7 @@ bool HomeDaemon::isRead(QString channel, QString topic, QString uuid)
m_settings.beginGroup("messages");
auto value = m_settings.value(settingKey).toString();
m_settings.endGroup();
qDebug() << "get message status" << settingKey << value;
qCDebug(logger) << "get message status" << settingKey << value;
return value == "read";
}

Expand All @@ -176,7 +176,7 @@ void HomeDaemon::start()
// 主流程,更新node信息,并启动定时器刷新渠道消息
void HomeDaemon::run()
{
qDebug() << "Refresh Node";
qCDebug(logger) << "Refresh Node";
// 用于停止旧的定时器
auto cronID = newUUID();
m_refreshChannelCronID = cronID;
Expand All @@ -195,7 +195,7 @@ void HomeDaemon::run()
} catch (...) {
qWarning() << "Refresh Node Error";
}
qDebug() << "node next refresh will be in the" << nextRefreshTime << "seconds";
qCDebug(logger) << "node next refresh will be in the" << nextRefreshTime << "seconds";
QTimer::singleShot(nextRefreshTime * 1000, this, &HomeDaemon::run);
}
void HomeDaemon::refreshNode()
Expand All @@ -209,7 +209,7 @@ void HomeDaemon::refreshNode()
// 定时刷新单个渠道
void HomeDaemon::refreshChannel(QString cronID, QString channel)
{
qDebug() << "Refresh Channel" << channel;
qCDebug(logger) << "Refresh Channel" << channel;

// 如果执行发生异常,在十分钟后重试
auto nextRefreshTime = 60 * 10;
Expand All @@ -231,7 +231,7 @@ void HomeDaemon::refreshChannel(QString cronID, QString channel)
} catch (...) {
qWarning() << "Refresh Channel Error";
}
qDebug() << "topic next refresh will be in the" << nextRefreshTime << "seconds";
qCDebug(logger) << "topic next refresh will be in the" << nextRefreshTime << "seconds";
// 延迟再次运行
QTimer::singleShot(nextRefreshTime * 1000, this, [this, cronID, channel] {
// 如果cron id刷新,则任务中断
Expand All @@ -244,7 +244,7 @@ void HomeDaemon::refreshChannel(QString cronID, QString channel)
// 在固定时机提醒填写调查问卷
void HomeDaemon::execFirstNotify()
{
qDebug() << "execFirstNotify";
qCDebug(logger) << "execFirstNotify";
try {
auto messages = m_api->getMessages(getNode(),
DEEPIN_HOME_CHANNEL_PUBLIC,
Expand All @@ -259,7 +259,7 @@ void HomeDaemon::execFirstNotify()
// 发送消息通知
notify(message.title, message.summary, message.url);
m_settings.setValue("firstNotify", true);
qDebug() << "send first notify" << message.title << message.summary;
qCDebug(logger) << "send first notify" << message.title << message.summary;
}
} catch (...) {
qWarning() << "Network Error";
Expand All @@ -268,7 +268,7 @@ void HomeDaemon::execFirstNotify()
// 处理消息
void HomeDaemon::refreshMessage(QString channel, QString topic, QString changeID)
{
qDebug() << "refresh message" << channel << topic;
qCDebug(logger) << "refresh message" << channel << topic;
auto messages = m_api->getMessages(getNode(), channel, topic, getLanguage(), changeID);
QStringList ids;
m_settings.beginGroup("messages");
Expand All @@ -287,13 +287,13 @@ void HomeDaemon::refreshMessage(QString channel, QString topic, QString changeID
m_settings.setValue(settingKey, "notify");
// 发送消息通知
notify(message.title, message.summary, message.url);
qDebug() << "send notify" << settingKey << message.title << message.summary;
qCDebug(logger) << "send notify" << settingKey << message.title << message.summary;
}
// 清理过期的消息
auto prefix = QString("%1_%2").arg(channel).arg(topic);
for (auto key : m_settings.childKeys()) {
if (key.startsWith(prefix) && !ids.contains(key)) {
qDebug() << "remove" << key;
qCDebug(logger) << "remove" << key;
m_settings.remove(key);
}
}
Expand Down Expand Up @@ -376,19 +376,19 @@ QMap<QString, QVariant> HomeDaemon::getUserInfo()
// 获取用户token
QStringList HomeDaemon::getToken(QString publicKey)
{
qDebug() << "get token";
#ifndef DEBUG
qCDebug(logger) << "get token";
#ifndef QT_DEBUG
auto pid = connection().interface()->servicePid(message().service());
qDebug() << "sender pid" << pid;
qCDebug(logger) << "sender pid" << pid;
auto sender = QFile::symLinkTarget(QString("/proc/%1/exe").arg(pid));
if (sender != QString("%1%2").arg(APP_BIN_INSTALL_DIR).arg(APP_NAME)) {
qDebug() << "Not allow";
qCDebug(logger) << "Not allow";
return {};
};
#endif
auto token = m_account->getToken();
if (token.isEmpty()) {
qDebug() << "No token";
qCDebug(logger) << "No token";
return {};
}
// 解析公钥
Expand Down Expand Up @@ -439,6 +439,7 @@ void HomeDaemon::openForum()
// 退出daemon
void HomeDaemon::quit()
{
qCInfo(logger) << "quic by dbus call";
QCoreApplication::quit();
}
// 获取开机自启配置
Expand Down
1 change: 1 addition & 0 deletions src/main/homeDaemon.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class HomeDaemon : public QObject, protected QDBusContext
QMenu *m_menu = nullptr;
API *m_api = nullptr;
Account *m_account = nullptr;
QLoggingCategory logger = QLoggingCategory("daemon");

public:
explicit HomeDaemon(QObject *parent = nullptr);
Expand Down
76 changes: 33 additions & 43 deletions src/maincomponentplugin/api/API.qml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ pragma Singleton
import QtQuick 2.0

Item {
// 服务器节点
property string node: ""
// 当前是否登陆
property bool isLogin: false
// 当前登陆的用户token
Expand All @@ -28,7 +30,7 @@ Item {
signal signalFeedbackListUpdate(var feedbacks)
// 发送http请求
function request(method, rawUrl, body, callback) {
const url = worker.getNode() + rawUrl
const url = node + rawUrl
console.log("send %1 request".arg(method), url)
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = () => {
Expand Down Expand Up @@ -80,10 +82,6 @@ Item {
function delete_(url, callback) {
return request("DELETE", url, null, callback)
}
// 获取服务器判定语言(归类)
function getLanguage(callback) {
callback(worker.getLanguage())
}
// 获取通知列表
function getNotify(callback) {
// p: public channel
Expand All @@ -98,22 +96,16 @@ Item {
}
// 获取内测渠道内容
function getInternalTest(callback){
getLanguage(lang=>{
get("/api/v1/public/setting/internal-test_"+lang, callback)
})
get("/api/v1/public/setting/internal-test_"+language, callback)
}
// 获取社区动态内容
function getAboutUs(callback){
getLanguage(lang=>{
get("/api/v1/public/setting/aboutus_"+lang, callback)
})
get("/api/v1/public/setting/aboutus_"+language, callback)
}
// 获取首页配置
function getClientHome(callback) {
getLanguage(lang=>{
get("/api/v1/public/setting/client-home_"+lang, (resp)=>{
callback(JSON.parse(resp.value))
})
get("/api/v1/public/setting/client-home_"+language, (resp)=>{
callback(JSON.parse(resp.value))
})
}
// 给用户反馈列表填充和用户关联关系,用于显示是否已点赞,已收藏
Expand Down Expand Up @@ -165,14 +157,11 @@ Item {
ids+="&public_id=" + id
}
}
const lang = worker.awaitPromise(promise=>{
getLanguage(promise.resolve)
})
const url = "/api/v1/public/feedback?offset=%1&limit=%2&&type=%4&language=%5%6".
arg(opt.offset).
arg(opt.limit).
arg(opt.type).
arg(lang).
arg(language).
arg(ids)
const resp = worker.awaitPromise(promise=>{
get(url, promise.resolve)
Expand All @@ -187,7 +176,7 @@ Item {
feedback.collect_count = stat.collect_count
if(feedback.screenshots) {
feedback.screenshots = feedback.screenshots.map((id)=> {
return worker.getNode() + "/api/v1/public/upload/" + id
return node + "/api/v1/public/upload/" + id
})
}
}
Expand All @@ -199,27 +188,25 @@ Item {
}
// 获取我的反馈
function getMyFeedback(opt) {
getLanguage(lang=>{
const url = "/api/v1/user/feedback?offset=%1&limit=%2&type=%4".arg(opt.offset).arg(opt.limit).arg(opt.type)
get(url, (resp)=>{
for(let feedback of resp) {
const id = feedback.public_id
const stat = worker.awaitPromise(promise=>{
feedbackStat(id, promise.resolve)
const url = "/api/v1/user/feedback?offset=%1&limit=%2&type=%4".arg(opt.offset).arg(opt.limit).arg(opt.type)
get(url, (resp)=>{
for(let feedback of resp) {
const id = feedback.public_id
const stat = worker.awaitPromise(promise=>{
feedbackStat(id, promise.resolve)
})
feedback.view_count = stat.view_count
feedback.like_count = stat.like_count
feedback.collect_count = stat.collect_count
feedback.avatar = avatar
feedback.nickname = nickname
if(feedback.screenshots) {
feedback.screenshots = feedback.screenshots.map((id)=> {
return node + "/api/v1/public/upload/" + id
})
feedback.view_count = stat.view_count
feedback.like_count = stat.like_count
feedback.collect_count = stat.collect_count
feedback.avatar = avatar
feedback.nickname = nickname
if(feedback.screenshots) {
feedback.screenshots = feedback.screenshots.map((id)=> {
return worker.getNode() + "/api/v1/public/upload/" + id
})
}
}
return fill_feedback(resp)
})
}
return fill_feedback(resp)
})
}
// 点赞一个反馈
Expand Down Expand Up @@ -300,8 +287,10 @@ Item {
worker.notify(title, message)
}
// 登陆
function login() {
API.notify(qsTr("Please log in with your Deepin ID."), qsTr("You need to log in to proceed with the subsequent operations."))
function login(notify = true) {
if(notify) {
API.notify(qsTr("Please log in with your Deepin ID."), qsTr("You need to log in to proceed with the subsequent operations."))
}
worker.login()
}
// 登出
Expand Down Expand Up @@ -350,10 +339,11 @@ Item {
return language.startsWith("zh")
}
Component.onCompleted: {
refreshAccount()
messageCount()
node = worker.getNode()
autostart = getAutoStart()
language = worker.getLanguage()
refreshAccount()
messageCount()
}
Connections {
target: worker
Expand Down
1 change: 1 addition & 0 deletions src/maincomponentplugin/list/List.qml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ Control {
source: ["Notify.qml", "Questionnaire.qml"][root.index]
Connections {
target: list_loader.item
ignoreUnknownSignals: true
function onSelected(notify) {
root.selectedNotify = notify
}
Expand Down
5 changes: 4 additions & 1 deletion src/maincomponentplugin/main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,13 @@ AppLoader {
window.showNormal();
if (window.active) {
// 如果窗口已激活,点击托盘将关闭窗口
if (isIconClick)
if (isIconClick) {
console.log("close window")
window.close();
}

} else {
console.log("active window")
window.requestActivate();
}
}
Expand Down
Loading