From dd007877a994aa11b2320d69823998a377b9d91e Mon Sep 17 00:00:00 2001 From: Anthony Bilinski Date: Mon, 22 Apr 2019 00:54:15 -0700 Subject: [PATCH] refactor(status): move Status out of Widget --- CMakeLists.txt | 2 + src/core/core.cpp | 25 ++--- src/core/core.h | 21 ++-- src/core/corefile.cpp | 5 +- src/core/corefile.h | 10 +- src/model/friend.cpp | 9 +- src/model/friend.h | 9 +- src/model/status.cpp | 95 ++++++++++++++++++ src/model/status.h | 44 +++++++++ src/net/avatarbroadcaster.cpp | 3 +- src/nexus.cpp | 3 +- src/widget/categorywidget.cpp | 5 +- src/widget/categorywidget.h | 5 +- src/widget/contentdialog.cpp | 15 +-- src/widget/contentdialog.h | 8 +- src/widget/form/chatform.cpp | 8 +- src/widget/form/chatform.h | 3 +- src/widget/form/settings/advancedform.cpp | 3 +- src/widget/friendlistlayout.cpp | 13 +-- src/widget/friendlistlayout.h | 7 +- src/widget/friendlistwidget.cpp | 5 +- src/widget/friendlistwidget.h | 5 +- src/widget/friendwidget.cpp | 5 +- src/widget/groupwidget.cpp | 5 +- src/widget/widget.cpp | 113 +++++----------------- src/widget/widget.h | 12 +-- 26 files changed, 262 insertions(+), 176 deletions(-) create mode 100644 src/model/status.cpp create mode 100644 src/model/status.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 3d3b27487..3c972eb45 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -323,6 +323,8 @@ set(${PROJECT_NAME}_SOURCES src/model/groupinvite.h src/model/group.cpp src/model/group.h + src/model/status.cpp + src/model/status.h src/model/interface.h src/model/profile/iprofileinfo.h src/model/profile/profileinfo.cpp diff --git a/src/core/core.cpp b/src/core/core.cpp index 6826fb7e0..05dec71ad 100644 --- a/src/core/core.cpp +++ b/src/core/core.cpp @@ -27,6 +27,7 @@ #include "src/core/toxoptions.h" #include "src/core/toxstring.h" #include "src/model/groupinvite.h" +#include "src/model/status.h" #include "src/nexus.h" #include "src/net/bootstrapnodeupdater.h" #include "src/persistence/profile.h" @@ -506,18 +507,18 @@ void Core::onStatusMessageChanged(Tox*, uint32_t friendId, const uint8_t* cMessa void Core::onUserStatusChanged(Tox*, uint32_t friendId, Tox_User_Status userstatus, void* core) { - Status status; + Status::Status status; switch (userstatus) { case TOX_USER_STATUS_AWAY: - status = Status::Away; + status = Status::Status::Away; break; case TOX_USER_STATUS_BUSY: - status = Status::Busy; + status = Status::Status::Busy; break; default: - status = Status::Online; + status = Status::Status::Online; break; } @@ -527,9 +528,9 @@ void Core::onUserStatusChanged(Tox*, uint32_t friendId, Tox_User_Status userstat void Core::onConnectionStatusChanged(Tox*, uint32_t friendId, Tox_Connection status, void* vCore) { Core* core = static_cast(vCore); - Status friendStatus = status != TOX_CONNECTION_NONE ? Status::Online : Status::Offline; + Status::Status friendStatus = status != TOX_CONNECTION_NONE ? Status::Status::Online : Status::Status::Offline; // Ignore Online because it will be emited from onUserStatusChanged - bool isOffline = friendStatus == Status::Offline; + bool isOffline = friendStatus == Status::Status::Offline; if (isOffline) { emit core->friendStatusChanged(friendId, friendStatus); core->checkLastOnline(friendId); @@ -908,11 +909,11 @@ QString Core::getStatusMessage() const /** * @brief Returns our user status */ -Status Core::getStatus() const +Status::Status Core::getStatus() const { QMutexLocker ml{&coreLoopLock}; - return static_cast(tox_self_get_status(tox.get())); + return static_cast(tox_self_get_status(tox.get())); } void Core::setStatusMessage(const QString& message) @@ -933,21 +934,21 @@ void Core::setStatusMessage(const QString& message) emit statusMessageSet(message); } -void Core::setStatus(Status status) +void Core::setStatus(Status::Status status) { QMutexLocker ml{&coreLoopLock}; Tox_User_Status userstatus; switch (status) { - case Status::Online: + case Status::Status::Online: userstatus = TOX_USER_STATUS_NONE; break; - case Status::Away: + case Status::Status::Away: userstatus = TOX_USER_STATUS_AWAY; break; - case Status::Busy: + case Status::Status::Busy: userstatus = TOX_USER_STATUS_BUSY; break; diff --git a/src/core/core.h b/src/core/core.h index b4a2aaf36..d16e1c581 100644 --- a/src/core/core.h +++ b/src/core/core.h @@ -27,6 +27,7 @@ #include "groupid.h" #include "src/util/strongtype.h" +#include "src/model/status.h" #include #include @@ -42,16 +43,6 @@ class CoreFile; class ICoreSettings; class GroupInvite; class Profile; - -enum class Status -{ - Online = 0, - Away, - Busy, - Offline, - Blocked -}; - class Core; using ToxCorePtr = std::unique_ptr; @@ -97,7 +88,7 @@ public: void quitGroupChat(int groupId) const; QString getUsername() const; - Status getStatus() const; + Status::Status getStatus() const; QString getStatusMessage() const; ToxId getSelfId() const; ToxPk getSelfPublicKey() const; @@ -118,7 +109,7 @@ public slots: void removeFriend(uint32_t friendId); void removeGroup(int groupId); - void setStatus(Status status); + void setStatus(Status::Status status); void setUsername(const QString& username); void setStatusMessage(const QString& message); @@ -144,12 +135,12 @@ signals: void usernameSet(const QString& username); void statusMessageSet(const QString& message); - void statusSet(Status status); + void statusSet(Status::Status status); void idSet(const ToxId& id); void failedToSetUsername(const QString& username); void failedToSetStatusMessage(const QString& message); - void failedToSetStatus(Status status); + void failedToSetStatus(Status::Status status); void failedToSetTyping(bool typing); void avReady(); @@ -165,7 +156,7 @@ signals: void friendMessageReceived(uint32_t friendId, const QString& message, bool isAction); void friendAdded(uint32_t friendId, const ToxPk& friendPk); - void friendStatusChanged(uint32_t friendId, Status status); + void friendStatusChanged(uint32_t friendId, Status::Status status); void friendStatusMessageChanged(uint32_t friendId, const QString& message); void friendUsernameChanged(uint32_t friendId, const QString& username); void friendTypingChanged(uint32_t friendId, bool isTyping); diff --git a/src/core/corefile.cpp b/src/core/corefile.cpp index 0f116cde4..537f9b93d 100644 --- a/src/core/corefile.cpp +++ b/src/core/corefile.cpp @@ -23,6 +23,7 @@ #include "toxfile.h" #include "toxstring.h" #include "src/persistence/settings.h" +#include "src/model/status.h" #include #include #include @@ -528,9 +529,9 @@ void CoreFile::onFileRecvChunkCallback(Tox* tox, uint32_t friendId, uint32_t fil } } -void CoreFile::onConnectionStatusChanged(uint32_t friendId, Status state) +void CoreFile::onConnectionStatusChanged(uint32_t friendId, Status::Status state) { - bool isOffline = state == Status::Offline; + bool isOffline = state == Status::Status::Offline; // TODO: Actually resume broken file transfers // We need to: // - Start a new file transfer with the same 32byte file ID with toxcore diff --git a/src/core/corefile.h b/src/core/corefile.h index e38459d1e..6d9161074 100644 --- a/src/core/corefile.h +++ b/src/core/corefile.h @@ -21,20 +21,22 @@ #ifndef COREFILE_H #define COREFILE_H -#include -#include -#include #include #include "toxfile.h" #include "src/core/core.h" #include "src/core/toxpk.h" +#include "src/model/status.h" #include #include #include #include +#include +#include +#include + struct Tox; class CoreFile; @@ -100,7 +102,7 @@ private: static QString getCleanFileName(QString filename); private slots: - void onConnectionStatusChanged(uint32_t friendId, Status state); + void onConnectionStatusChanged(uint32_t friendId, Status::Status state); private: QHash fileMap; diff --git a/src/model/friend.cpp b/src/model/friend.cpp index 29e4f7066..433076148 100644 --- a/src/model/friend.cpp +++ b/src/model/friend.cpp @@ -20,6 +20,7 @@ #include "friend.h" #include "src/model/group.h" +#include "src/model/status.h" #include "src/grouplist.h" #include "src/persistence/profile.h" #include "src/widget/form/chatform.h" @@ -30,7 +31,7 @@ Friend::Friend(uint32_t friendId, const ToxPk& friendPk, const QString& userAlia , friendPk{friendPk} , friendId{friendId} , hasNewEvents{false} - , friendStatus{Status::Offline} + , friendStatus{Status::Status::Offline} { if (userName.isEmpty()) { this->userName = friendPk.toString(); @@ -152,7 +153,7 @@ bool Friend::getEventFlag() const return hasNewEvents; } -void Friend::setStatus(Status s) +void Friend::setStatus(Status::Status s) { if (friendStatus != s) { friendStatus = s; @@ -160,12 +161,12 @@ void Friend::setStatus(Status s) } } -Status Friend::getStatus() const +Status::Status Friend::getStatus() const { return friendStatus; } bool Friend::isOnline() const { - return friendStatus != Status::Offline && friendStatus != Status::Blocked; + return friendStatus != Status::Status::Offline && friendStatus != Status::Status::Blocked; } diff --git a/src/model/friend.h b/src/model/friend.h index f7dc3b1cd..efb6f4223 100644 --- a/src/model/friend.h +++ b/src/model/friend.h @@ -24,6 +24,7 @@ #include "src/core/core.h" #include "src/core/toxid.h" #include "src/core/contactid.h" +#include "src/model/status.h" #include #include @@ -50,14 +51,14 @@ public: uint32_t getId() const override; const ContactId& getPersistentId() const override; - void setStatus(Status s); - Status getStatus() const; + void setStatus(Status::Status s); + Status::Status getStatus() const; bool isOnline() const; signals: void nameChanged(const ToxPk& friendId, const QString& name); void aliasChanged(const ToxPk& friendId, QString alias); - void statusChanged(const ToxPk& friendId, Status status); + void statusChanged(const ToxPk& friendId, Status::Status status); void statusMessageChanged(const ToxPk& friendId, const QString& message); void loadChatHistory(); @@ -70,7 +71,7 @@ private: ToxPk friendPk; uint32_t friendId; bool hasNewEvents; - Status friendStatus; + Status::Status friendStatus; }; #endif // FRIEND_H diff --git a/src/model/status.cpp b/src/model/status.cpp new file mode 100644 index 000000000..0ed5b991a --- /dev/null +++ b/src/model/status.cpp @@ -0,0 +1,95 @@ +/* + Copyright © 2019 by The qTox Project Contributors + + This file is part of qTox, a Qt-based graphical interface for Tox. + + qTox is libre software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + qTox is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with qTox. If not, see . +*/ + +#include + +#include +#include +#include +#include + +#include + +namespace Status +{ + QPixmap getIconPixmap(QString path, uint32_t w, uint32_t h) + { + QPixmap pix(w, h); + pix.load(path); + return pix; + } + + QString getTitle(Status status) + { + switch (status) { + case Status::Online: + return QObject::tr("online", "contact status"); + case Status::Away: + return QObject::tr("away", "contact status"); + case Status::Busy: + return QObject::tr("busy", "contact status"); + case Status::Offline: + return QObject::tr("offline", "contact status"); + case Status::Blocked: + return QObject::tr("blocked", "contact status"); + } + + assert(false); + return QStringLiteral(""); + } + + Status getFromString(QString status) + { + if (status == QStringLiteral("online")) + return Status::Online; + else if (status == QStringLiteral("away")) + return Status::Away; + else if (status == QStringLiteral("busy")) + return Status::Busy; + else if (status == QStringLiteral("offline")) + return Status::Offline; + else if (status == QStringLiteral("blocked")) + return Status::Blocked; + else { + assert(false); + return Status::Offline; + } + } + + QString getIconPath(Status status, bool event) + { + const QString eventSuffix = event ? QStringLiteral("_notification") : QString(); + + switch (status) { + case Status::Online: + return ":/img/status/online" + eventSuffix + ".svg"; + case Status::Away: + return ":/img/status/away" + eventSuffix + ".svg"; + case Status::Busy: + return ":/img/status/busy" + eventSuffix + ".svg"; + case Status::Offline: + return ":/img/status/offline" + eventSuffix + ".svg"; + case Status::Blocked: + return ":/img/status/blocked.svg"; + } + qWarning() << "Status unknown"; + assert(false); + return QString{}; + } +} // namespace Status diff --git a/src/model/status.h b/src/model/status.h new file mode 100644 index 000000000..feffca9fe --- /dev/null +++ b/src/model/status.h @@ -0,0 +1,44 @@ +/* + Copyright © 2019 by The qTox Project Contributors + + This file is part of qTox, a Qt-based graphical interface for Tox. + + qTox is libre software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + qTox is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with qTox. If not, see . +*/ + +#include +#include + +#ifndef STATUS_H +#define STATUS_H + +namespace Status +{ + // Status::Status is weird, but Status is a fitting name for both the namespace and enum class.. + enum class Status + { + Online = 0, + Away, + Busy, + Offline, + Blocked + }; + + QString getIconPath(Status status, bool event = false); + QPixmap getIconPixmap(QString path, uint32_t w, uint32_t h); + QString getTitle(Status status); + Status getFromString(QString status); +} + +#endif // STATUS_H diff --git a/src/net/avatarbroadcaster.cpp b/src/net/avatarbroadcaster.cpp index 39eaaab4d..fd87687de 100644 --- a/src/net/avatarbroadcaster.cpp +++ b/src/net/avatarbroadcaster.cpp @@ -21,6 +21,7 @@ #include "avatarbroadcaster.h" #include "src/core/core.h" #include "src/core/corefile.h" +#include "src/model/status.h" #include #include @@ -36,7 +37,7 @@ QByteArray AvatarBroadcaster::avatarData; QMap AvatarBroadcaster::friendsSentTo; static QMetaObject::Connection autoBroadcastConn; -static auto autoBroadcast = [](uint32_t friendId, Status) { +static auto autoBroadcast = [](uint32_t friendId, Status::Status) { AvatarBroadcaster::sendAvatarTo(friendId); }; diff --git a/src/nexus.cpp b/src/nexus.cpp index e773fda04..269ad7767 100644 --- a/src/nexus.cpp +++ b/src/nexus.cpp @@ -23,6 +23,7 @@ #include "src/core/core.h" #include "src/core/coreav.h" #include "src/model/groupinvite.h" +#include "src/model/status.h" #include "src/persistence/profile.h" #include "src/widget/widget.h" #include "video/camerasource.h" @@ -83,7 +84,7 @@ void Nexus::start() qDebug() << "Starting up"; // Setup the environment - qRegisterMetaType("Status"); + qRegisterMetaType("Status::Status"); qRegisterMetaType("vpx_image"); qRegisterMetaType("uint8_t"); qRegisterMetaType("uint16_t"); diff --git a/src/widget/categorywidget.cpp b/src/widget/categorywidget.cpp index aab87b22c..8e55f8ba5 100644 --- a/src/widget/categorywidget.cpp +++ b/src/widget/categorywidget.cpp @@ -21,6 +21,7 @@ #include "friendlistlayout.h" #include "friendlistwidget.h" #include "friendwidget.h" +#include "src/model/status.h" #include "src/widget/style.h" #include "tool/croppinglabel.h" #include @@ -126,7 +127,7 @@ void CategoryWidget::editName() nameLabel->setMaximumWidth(QWIDGETSIZE_MAX); } -void CategoryWidget::addFriendWidget(FriendWidget* w, Status s) +void CategoryWidget::addFriendWidget(FriendWidget* w, Status::Status s) { listLayout->addFriendWidget(w, s); updateStatus(); @@ -134,7 +135,7 @@ void CategoryWidget::addFriendWidget(FriendWidget* w, Status s) w->reloadTheme(); // Otherwise theme will change when moving to another circle. } -void CategoryWidget::removeFriendWidget(FriendWidget* w, Status s) +void CategoryWidget::removeFriendWidget(FriendWidget* w, Status::Status s) { listLayout->removeFriendWidget(w, s); updateStatus(); diff --git a/src/widget/categorywidget.h b/src/widget/categorywidget.h index e005bd515..fe5a23205 100644 --- a/src/widget/categorywidget.h +++ b/src/widget/categorywidget.h @@ -22,6 +22,7 @@ #include "genericchatitemwidget.h" #include "src/core/core.h" +#include "src/model/status.h" class FriendListLayout; class FriendListWidget; @@ -39,8 +40,8 @@ public: void setExpanded(bool isExpanded, bool save = true); void setName(const QString& name, bool save = true); - void addFriendWidget(FriendWidget* w, Status s); - void removeFriendWidget(FriendWidget* w, Status s); + void addFriendWidget(FriendWidget* w, Status::Status s); + void removeFriendWidget(FriendWidget* w, Status::Status s); void updateStatus(); bool hasChatrooms() const; diff --git a/src/widget/contentdialog.cpp b/src/widget/contentdialog.cpp index e7baa5b13..71e92d110 100644 --- a/src/widget/contentdialog.cpp +++ b/src/widget/contentdialog.cpp @@ -34,6 +34,7 @@ #include "src/model/chatroom/friendchatroom.h" #include "src/model/friend.h" #include "src/model/group.h" +#include "src/model/status.h" #include "src/persistence/settings.h" #include "src/widget/contentlayout.h" #include "src/widget/friendwidget.h" @@ -196,8 +197,8 @@ void ContentDialog::removeFriend(const ToxPk& friendPk) cycleContacts(/* forward = */ true, /* inverse = */ false); } - friendLayout->removeFriendWidget(chatroomWidget, Status::Offline); - friendLayout->removeFriendWidget(chatroomWidget, Status::Online); + friendLayout->removeFriendWidget(chatroomWidget, Status::Status::Offline); + friendLayout->removeFriendWidget(chatroomWidget, Status::Status::Online); chatroomWidget->deleteLater(); @@ -385,8 +386,8 @@ void ContentDialog::updateTitleAndStatusIcon() return; } - Status currentStatus = activeChatroomWidget->getFriend()->getStatus(); - setWindowIcon(QIcon{Widget::getStatusIconPath(currentStatus)}); + Status::Status currentStatus = activeChatroomWidget->getFriend()->getStatus(); + setWindowIcon(QIcon{Status::getIconPath(currentStatus)}); } /** @@ -590,7 +591,7 @@ bool ContentDialog::containsContact(const ContactId& contactId) const return contactWidgets.contains(contactId); } -void ContentDialog::updateFriendStatus(const ToxPk& friendPk, Status status) +void ContentDialog::updateFriendStatus(const ToxPk& friendPk, Status::Status status) { auto widget = qobject_cast(contactWidgets.value(friendPk)); addFriendWidget(widget, status); @@ -633,7 +634,7 @@ void ContentDialog::updateFriendWidget(const ToxPk& friendPk, QString alias) Friend* f = FriendList::findFriend(friendPk); FriendWidget* friendWidget = qobject_cast(contactWidgets[friendPk]); - Status status = f->getStatus(); + Status::Status status = f->getStatus(); friendLayout->addFriendWidget(friendWidget, status); } @@ -698,7 +699,7 @@ QLayout* ContentDialog::nextLayout(QLayout* layout, bool forward) const return layouts[next]; } -void ContentDialog::addFriendWidget(FriendWidget* widget, Status status) +void ContentDialog::addFriendWidget(FriendWidget* widget, Status::Status status) { friendLayout->addFriendWidget(widget, status); } diff --git a/src/widget/contentdialog.h b/src/widget/contentdialog.h index b8c5ade7d..97772a179 100644 --- a/src/widget/contentdialog.h +++ b/src/widget/contentdialog.h @@ -22,7 +22,9 @@ #include "src/widget/genericchatitemlayout.h" #include "src/widget/tool/activatedialog.h" -#include "src/core/core.h" // Status +#include "src/model/status.h" +#include "src/core/groupid.h" +#include "src/core/toxpk.h" #include #include @@ -68,13 +70,13 @@ public: void onVideoShow(QSize size); void onVideoHide(); - void addFriendWidget(FriendWidget* widget, Status status); + void addFriendWidget(FriendWidget* widget, Status::Status status); bool isActiveWidget(GenericChatroomWidget* widget); bool hasContactWidget(const ContactId& contactId) const; void focusContact(const ContactId& friendPk); bool containsContact(const ContactId& friendPk) const; - void updateFriendStatus(const ToxPk& friendPk, Status status); + void updateFriendStatus(const ToxPk& friendPk, Status::Status status); void updateContactStatusLight(const ContactId& contactId); bool isContactWidgetActive(const ContactId& contactId); diff --git a/src/widget/form/chatform.cpp b/src/widget/form/chatform.cpp index 498944f31..9e7317f80 100644 --- a/src/widget/form/chatform.cpp +++ b/src/widget/form/chatform.cpp @@ -27,6 +27,7 @@ #include "src/core/coreav.h" #include "src/core/corefile.h" #include "src/model/friend.h" +#include "src/model/status.h" #include "src/nexus.h" #include "src/persistence/history.h" #include "src/persistence/offlinemsgengine.h" @@ -40,10 +41,12 @@ #include "src/widget/style.h" #include "src/widget/tool/callconfirmwidget.h" #include "src/widget/tool/chattextedit.h" +#include "src/widget/tool/croppinglabel.h" #include "src/widget/tool/screenshotgrabber.h" #include "src/widget/translator.h" #include "src/widget/widget.h" +#include #include #include #include @@ -51,6 +54,7 @@ #include #include #include +#include #include #include @@ -612,7 +616,7 @@ void ChatForm::onFileSendFailed(uint32_t friendId, const QString& fname) QDateTime::currentDateTime()); } -void ChatForm::onFriendStatusChanged(uint32_t friendId, Status status) +void ChatForm::onFriendStatusChanged(uint32_t friendId, Status::Status status) { // Disable call buttons if friend is offline if (friendId != f->getId()) { @@ -629,7 +633,7 @@ void ChatForm::onFriendStatusChanged(uint32_t friendId, Status status) updateCallButtons(); if (Settings::getInstance().getStatusChangeNotificationEnabled()) { - QString fStatus = Widget::getStatusTitle(status); + QString fStatus = Status::getTitle(status); addSystemInfoMessage(tr("%1 is now %2", "e.g. \"Dubslow is now online\"") .arg(f->getDisplayedName()) .arg(fStatus), diff --git a/src/widget/form/chatform.h b/src/widget/form/chatform.h index ba1ac0871..3a34676b7 100644 --- a/src/widget/form/chatform.h +++ b/src/widget/form/chatform.h @@ -28,6 +28,7 @@ #include "genericchatform.h" #include "src/core/core.h" #include "src/persistence/history.h" +#include "src/model/status.h" #include "src/widget/tool/screenshotgrabber.h" class CallConfirmWidget; @@ -99,7 +100,7 @@ private slots: void onVolMuteToggle(); void onFileSendFailed(uint32_t friendId, const QString& fname); - void onFriendStatusChanged(quint32 friendId, Status status); + void onFriendStatusChanged(quint32 friendId, Status::Status status); void onFriendTypingChanged(quint32 friendId, bool isTyping); void onFriendNameChanged(const QString& name); void onFriendMessageReceived(quint32 friendId, const QString& message, bool isAction); diff --git a/src/widget/form/settings/advancedform.cpp b/src/widget/form/settings/advancedform.cpp index 61d284d08..c9be6593e 100644 --- a/src/widget/form/settings/advancedform.cpp +++ b/src/widget/form/settings/advancedform.cpp @@ -30,6 +30,7 @@ #include "src/core/core.h" #include "src/core/coreav.h" #include "src/nexus.h" +#include "src/model/status.h" #include "src/persistence/profile.h" #include "src/persistence/settings.h" #include "src/widget/gui.h" @@ -222,7 +223,7 @@ void AdvancedForm::on_reconnectButton_clicked() return; } - emit Core::getInstance()->statusSet(Status::Offline); + emit Core::getInstance()->statusSet(Status::Status::Offline); Nexus::getProfile()->restartCore(); } diff --git a/src/widget/friendlistlayout.cpp b/src/widget/friendlistlayout.cpp index 5a4647db1..f2dc38ae6 100644 --- a/src/widget/friendlistlayout.cpp +++ b/src/widget/friendlistlayout.cpp @@ -16,6 +16,7 @@ #include "friendlistwidget.h" #include "friendwidget.h" #include "src/model/friend.h" +#include "src/model/status.h" #include "src/friendlist.h" #include @@ -46,12 +47,12 @@ void FriendListLayout::init() addLayout(friendOfflineLayout.getLayout()); } -void FriendListLayout::addFriendWidget(FriendWidget* w, Status s) +void FriendListLayout::addFriendWidget(FriendWidget* w, Status::Status s) { friendOfflineLayout.removeSortedWidget(w); friendOnlineLayout.removeSortedWidget(w); - if (s == Status::Offline) { + if (s == Status::Status::Offline) { friendOfflineLayout.addSortedWidget(w); return; } @@ -59,9 +60,9 @@ void FriendListLayout::addFriendWidget(FriendWidget* w, Status s) friendOnlineLayout.addSortedWidget(w); } -void FriendListLayout::removeFriendWidget(FriendWidget* widget, Status s) +void FriendListLayout::removeFriendWidget(FriendWidget* widget, Status::Status s) { - if (s == Status::Offline) + if (s == Status::Status::Offline) friendOfflineLayout.removeSortedWidget(widget); else friendOnlineLayout.removeSortedWidget(widget); @@ -123,7 +124,7 @@ QLayout* FriendListLayout::getLayoutOffline() const return friendOfflineLayout.getLayout(); } -QLayout* FriendListLayout::getFriendLayout(Status s) const +QLayout* FriendListLayout::getFriendLayout(Status::Status s) const { - return s == Status::Offline ? friendOfflineLayout.getLayout() : friendOnlineLayout.getLayout(); + return s == Status::Status::Offline ? friendOfflineLayout.getLayout() : friendOnlineLayout.getLayout(); } diff --git a/src/widget/friendlistlayout.h b/src/widget/friendlistlayout.h index 28b12c04b..7ee546caa 100644 --- a/src/widget/friendlistlayout.h +++ b/src/widget/friendlistlayout.h @@ -16,6 +16,7 @@ #define FRIENDLISTLAYOUT_H #include "genericchatitemlayout.h" +#include "src/model/status.h" #include "src/core/core.h" #include @@ -29,8 +30,8 @@ public: explicit FriendListLayout(); explicit FriendListLayout(QWidget* parent); - void addFriendWidget(FriendWidget* widget, Status s); - void removeFriendWidget(FriendWidget* widget, Status s); + void addFriendWidget(FriendWidget* widget, Status::Status s); + void removeFriendWidget(FriendWidget* widget, Status::Status s); int indexOfFriendWidget(GenericChatItemWidget* widget, bool online) const; void moveFriendWidgets(FriendListWidget* listWidget); int friendOnlineCount() const; @@ -45,7 +46,7 @@ public: private: void init(); - QLayout* getFriendLayout(Status s) const; + QLayout* getFriendLayout(Status::Status s) const; GenericChatItemLayout friendOnlineLayout; GenericChatItemLayout friendOfflineLayout; diff --git a/src/widget/friendlistwidget.cpp b/src/widget/friendlistwidget.cpp index 50cb0021e..fc93e8965 100644 --- a/src/widget/friendlistwidget.cpp +++ b/src/widget/friendlistwidget.cpp @@ -26,6 +26,7 @@ #include "src/friendlist.h" #include "src/model/friend.h" #include "src/model/group.h" +#include "src/model/status.h" #include "src/persistence/settings.h" #include #include @@ -289,7 +290,7 @@ void FriendListWidget::addGroupWidget(GroupWidget* widget) }); } -void FriendListWidget::addFriendWidget(FriendWidget* w, Status s, int circleIndex) +void FriendListWidget::addFriendWidget(FriendWidget* w, Status::Status s, int circleIndex) { CircleWidget* circleWidget = CircleWidget::getFromID(circleIndex); if (circleWidget == nullptr) @@ -609,7 +610,7 @@ void FriendListWidget::dayTimeout() dayTimer->start(timeUntilTomorrow()); } -void FriendListWidget::moveWidget(FriendWidget* widget, Status s, bool add) +void FriendListWidget::moveWidget(FriendWidget* widget, Status::Status s, bool add) { if (mode == Name) { const Friend* f = widget->getFriend(); diff --git a/src/widget/friendlistwidget.h b/src/widget/friendlistwidget.h index 773042847..09279d60d 100644 --- a/src/widget/friendlistwidget.h +++ b/src/widget/friendlistwidget.h @@ -22,6 +22,7 @@ #include "genericchatitemlayout.h" #include "src/core/core.h" +#include "src/model/status.h" #include class QVBoxLayout; @@ -50,7 +51,7 @@ public: Mode getMode() const; void addGroupWidget(GroupWidget* widget); - void addFriendWidget(FriendWidget* w, Status s, int circleIndex); + void addFriendWidget(FriendWidget* w, Status::Status s, int circleIndex); void removeGroupWidget(GroupWidget* w); void removeFriendWidget(FriendWidget* w); void addCircleWidget(int id); @@ -72,7 +73,7 @@ public slots: void renameCircleWidget(CircleWidget* circleWidget, const QString& newName); void onFriendWidgetRenamed(FriendWidget* friendWidget); void onGroupchatPositionChanged(bool top); - void moveWidget(FriendWidget* w, Status s, bool add = false); + void moveWidget(FriendWidget* w, Status::Status s, bool add = false); protected: void dragEnterEvent(QDragEnterEvent* event) override; diff --git a/src/widget/friendwidget.cpp b/src/widget/friendwidget.cpp index 674e5ffe8..9a4a84fc8 100644 --- a/src/widget/friendwidget.cpp +++ b/src/widget/friendwidget.cpp @@ -29,6 +29,7 @@ #include "src/model/chatroom/friendchatroom.h" #include "src/model/friend.h" #include "src/model/group.h" +#include "src/model/status.h" #include "src/persistence/settings.h" #include "src/widget/about/aboutfriendform.h" #include "src/widget/contentdialogmanager.h" @@ -62,7 +63,7 @@ FriendWidget::FriendWidget(std::shared_ptr chatroom, bool compac , isDefaultAvatar{true} { avatar->setPixmap(QPixmap(":/img/contact.svg")); - statusPic.setPixmap(QPixmap(Widget::getStatusIconPath(Status::Offline))); + statusPic.setPixmap(QPixmap(Status::getIconPath(Status::Status::Offline))); statusPic.setMargin(3); auto frnd = chatroom->getFriend(); @@ -324,7 +325,7 @@ void FriendWidget::updateStatusLight() { const auto frnd = chatroom->getFriend(); const bool event = frnd->getEventFlag(); - statusPic.setPixmap(QPixmap(Widget::getStatusIconPath(frnd->getStatus(), event))); + statusPic.setPixmap(QPixmap(Status::getIconPath(frnd->getStatus(), event))); if (event) { const Settings& s = Settings::getInstance(); diff --git a/src/widget/groupwidget.cpp b/src/widget/groupwidget.cpp index f9de822a6..b663d90ff 100644 --- a/src/widget/groupwidget.cpp +++ b/src/widget/groupwidget.cpp @@ -34,6 +34,7 @@ #include "src/model/friend.h" #include "src/friendlist.h" #include "src/model/group.h" +#include "src/model/status.h" #include "src/grouplist.h" #include "src/widget/contentdialogmanager.h" #include "src/widget/friendwidget.h" @@ -48,7 +49,7 @@ GroupWidget::GroupWidget(std::shared_ptr chatroom, bool compact) , chatroom{chatroom} { avatar->setPixmap(Style::scaleSvgImage(":img/group.svg", avatar->width(), avatar->height())); - statusPic.setPixmap(QPixmap(Widget::getStatusIconPath(Status::Online))); + statusPic.setPixmap(QPixmap(Status::getIconPath(Status::Status::Online))); statusPic.setMargin(3); Group* g = chatroom->getGroup(); @@ -181,7 +182,7 @@ void GroupWidget::updateStatusLight() Group* g = chatroom->getGroup(); const bool event = g->getEventFlag(); - statusPic.setPixmap(QPixmap(Widget::getStatusIconPath(Status::Online, event))); + statusPic.setPixmap(QPixmap(Status::getIconPath(Status::Status::Online, event))); statusPic.setMargin(event ? 1 : 3); } diff --git a/src/widget/widget.cpp b/src/widget/widget.cpp index cf94725ed..1571e9373 100644 --- a/src/widget/widget.cpp +++ b/src/widget/widget.cpp @@ -60,6 +60,7 @@ #include "src/model/friend.h" #include "src/model/group.h" #include "src/model/groupinvite.h" +#include "src/model/status.h" #include "src/model/profile/profileinfo.h" #include "src/net/updatecheck.h" #include "src/nexus.h" @@ -128,15 +129,15 @@ void Widget::init() // Preparing icons and set their size statusOnline = new QAction(this); - statusOnline->setIcon(prepareIcon(getStatusIconPath(Status::Online), icon_size, icon_size)); + statusOnline->setIcon(prepareIcon(Status::getIconPath(Status::Status::Online), icon_size, icon_size)); connect(statusOnline, &QAction::triggered, this, &Widget::setStatusOnline); statusAway = new QAction(this); - statusAway->setIcon(prepareIcon(getStatusIconPath(Status::Away), icon_size, icon_size)); + statusAway->setIcon(prepareIcon(Status::getIconPath(Status::Status::Away), icon_size, icon_size)); connect(statusAway, &QAction::triggered, this, &Widget::setStatusAway); statusBusy = new QAction(this); - statusBusy->setIcon(prepareIcon(getStatusIconPath(Status::Busy), icon_size, icon_size)); + statusBusy->setIcon(prepareIcon(Status::getIconPath(Status::Status::Busy), icon_size, icon_size)); connect(statusBusy, &QAction::triggered, this, &Widget::setStatusBusy); actionLogout = new QAction(this); @@ -215,7 +216,7 @@ void Widget::init() ui->mainSplitter->setStretchFactor(0, 0); ui->mainSplitter->setStretchFactor(1, 1); - onStatusSet(Status::Offline); + onStatusSet(Status::Status::Offline); // Disable some widgets until we're connected to the DHT ui->statusButton->setEnabled(false); @@ -575,7 +576,7 @@ void Widget::closeEvent(QCloseEvent* event) QWidget::closeEvent(event); } else { if (autoAwayActive) { - emit statusSet(Status::Online); + emit statusSet(Status::Status::Online); autoAwayActive = false; } saveWindowGeometry(); @@ -620,7 +621,7 @@ void Widget::onConnected() void Widget::onDisconnected() { ui->statusButton->setEnabled(false); - emit core->statusSet(Status::Offline); + emit core->statusSet(Status::Status::Offline); } void Widget::onFailedToStartCore() @@ -646,10 +647,10 @@ void Widget::onBadProxyCore() onShowSettings(); } -void Widget::onStatusSet(Status status) +void Widget::onStatusSet(Status::Status status) { - ui->statusButton->setProperty("status", getStatusTitle(status)); - ui->statusButton->setIcon(prepareIcon(getStatusIconPath(status), icon_size, icon_size)); + ui->statusButton->setProperty("status", getTitle(status)); + ui->statusButton->setIcon(prepareIcon(getIconPath(status), icon_size, icon_size)); updateIcons(); } @@ -991,7 +992,7 @@ void Widget::addFriend(uint32_t friendId, const ToxPk& friendPk) settings.setFriendActivity(friendPk, chatDate); } - contactListWidget->addFriendWidget(widget, Status::Offline, settings.getFriendCircleID(friendPk)); + contactListWidget->addFriendWidget(widget, Status::Status::Offline, settings.getFriendCircleID(friendPk)); connect(newfriend, &Friend::aliasChanged, this, &Widget::onFriendAliasChanged); connect(newfriend, &Friend::displayedNameChanged, this, &Widget::onFriendDisplayedNameChanged); @@ -1037,7 +1038,7 @@ void Widget::addFriendFailed(const ToxPk&, const QString& errorInfo) QMessageBox::critical(nullptr, "Error", info); } -void Widget::onFriendStatusChanged(int friendId, Status status) +void Widget::onFriendStatusChanged(int friendId, Status::Status status) { const auto& friendPk = FriendList::id2Key(friendId); Friend* f = FriendList::findFriend(friendPk); @@ -1050,9 +1051,9 @@ void Widget::onFriendStatusChanged(int friendId, Status status) FriendWidget* widget = friendWidgets[f->getPublicKey()]; if (isActualChange) { if (!f->isOnline()) { - contactListWidget->moveWidget(widget, Status::Online); - } else if (status == Status::Offline) { - contactListWidget->moveWidget(widget, Status::Offline); + contactListWidget->moveWidget(widget, Status::Status::Online); + } else if (status == Status::Status::Offline) { + contactListWidget->moveWidget(widget, Status::Status::Offline); } } @@ -1110,10 +1111,10 @@ void Widget::onFriendAliasChanged(const ToxPk& friendId, const QString& alias) // TODO(sudden6): don't update the contact list here, make it update itself FriendWidget* friendWidget = friendWidgets[friendId]; - Status status = f->getStatus(); + Status::Status status = f->getStatus(); contactListWidget->moveWidget(friendWidget, status); FilterCriteria criteria = getFilterCriteria(); - bool filter = status == Status::Offline ? filterOffline(criteria) : filterOnline(criteria); + bool filter = status == Status::Status::Offline ? filterOffline(criteria) : filterOnline(criteria); friendWidget->searchName(ui->searchContactText->text(), filter); settings.setFriendAlias(friendId, alias); @@ -1446,7 +1447,7 @@ bool Widget::newMessageAlert(QWidget* currentWindow, bool isActive, bool sound, #endif eventFlag = true; } - bool isBusy = core->getStatus() == Status::Busy; + bool isBusy = core->getStatus() == Status::Status::Busy; bool busySound = settings.getBusySound(); bool notifySound = settings.getNotifySound(); @@ -1972,11 +1973,11 @@ void Widget::onUserAwayCheck() if (online && away) { qDebug() << "auto away activated at" << QTime::currentTime().toString(); - emit statusSet(Status::Away); + emit statusSet(Status::Status::Away); autoAwayActive = true; } else if (autoAwayActive && !away) { qDebug() << "auto away deactivated at" << QTime::currentTime().toString(); - emit statusSet(Status::Online); + emit statusSet(Status::Status::Online); autoAwayActive = false; } #endif @@ -2042,7 +2043,7 @@ void Widget::setStatusOnline() return; } - core->setStatus(Status::Online); + core->setStatus(Status::Status::Online); } void Widget::setStatusAway() @@ -2051,7 +2052,7 @@ void Widget::setStatusAway() return; } - core->setStatus(Status::Away); + core->setStatus(Status::Status::Away); } void Widget::setStatusBusy() @@ -2060,7 +2061,7 @@ void Widget::setStatusBusy() return; } - core->setStatus(Status::Busy); + core->setStatus(Status::Status::Busy); } void Widget::onGroupSendFailed(uint32_t groupnumber) @@ -2204,30 +2205,6 @@ void Widget::previousContact() cycleContacts(false); } -QString Widget::getStatusIconPath(Status status, bool event) -{ - QString eventSuffix{""}; - if (event) { - eventSuffix = "_notification"; - } - - switch (status) { - case Status::Online: - return ":/img/status/online" + eventSuffix + ".svg"; - case Status::Away: - return ":/img/status/away" + eventSuffix + ".svg"; - case Status::Busy: - return ":/img/status/busy" + eventSuffix + ".svg"; - case Status::Offline: - return ":/img/status/offline" + eventSuffix + ".svg"; - case Status::Blocked: - return ":/img/status/blocked.svg"; - } - qWarning() << "Status unknown"; - assert(false); - return QString{}; -} - // Preparing needed to set correct size of icons for GTK tray backend inline QIcon Widget::prepareIcon(QString path, int w, int h) { @@ -2255,50 +2232,6 @@ inline QIcon Widget::prepareIcon(QString path, int w, int h) return QIcon(path); } -QPixmap Widget::getStatusIconPixmap(QString path, uint32_t w, uint32_t h) -{ - QPixmap pix(w, h); - pix.load(path); - return pix; -} - -QString Widget::getStatusTitle(Status status) -{ - switch (status) { - case Status::Online: - return tr("online", "contact status"); - case Status::Away: - return tr("away", "contact status"); - case Status::Busy: - return tr("busy", "contact status"); - case Status::Offline: - return tr("offline", "contact status"); - case Status::Blocked: - return tr("blocked", "contact status"); - } - - assert(false); - return QStringLiteral(""); -} - -Status Widget::getStatusFromString(QString status) -{ - if (status == QStringLiteral("online")) - return Status::Online; - else if (status == QStringLiteral("away")) - return Status::Away; - else if (status == QStringLiteral("busy")) - return Status::Busy; - else if (status == QStringLiteral("offline")) - return Status::Offline; - else if (status == QStringLiteral("blocked")) - return Status::Blocked; - else { - assert(false); - return Status::Offline; - } -} - void Widget::searchContacts() { QString searchString = ui->searchContactText->text(); diff --git a/src/widget/widget.h b/src/widget/widget.h index 527eda9f0..e2e5a1db1 100644 --- a/src/widget/widget.h +++ b/src/widget/widget.h @@ -135,11 +135,7 @@ public: void reloadHistory(); void reloadTheme(); - static QString getStatusIconPath(Status status, bool event = false); static inline QIcon prepareIcon(QString path, int w = 0, int h = 0); - static QPixmap getStatusIconPixmap(QString path, uint32_t w, uint32_t h); - static QString getStatusTitle(Status status); - static Status getStatusFromString(QString status); void searchCircle(CircleWidget* circleWidget); bool groupsVisible() const; @@ -154,7 +150,7 @@ public slots: void forceShow(); void onConnected(); void onDisconnected(); - void onStatusSet(Status status); + void onStatusSet(Status::Status status); void onFailedToStartCore(); void onBadProxyCore(); void onSelfAvatarLoaded(const QPixmap& pic); @@ -162,7 +158,7 @@ public slots: void setStatusMessage(const QString& statusMessage); void addFriend(uint32_t friendId, const ToxPk& friendPk); void addFriendFailed(const ToxPk& userId, const QString& errorInfo = QString()); - void onFriendStatusChanged(int friendId, Status status); + void onFriendStatusChanged(int friendId, Status::Status status); void onFriendStatusMessageChanged(int friendId, const QString& message); void onFriendDisplayedNameChanged(const QString& displayed); void onFriendUsernameChanged(int friendId, const QString& username); @@ -193,8 +189,8 @@ public slots: signals: void friendRequestAccepted(const ToxPk& friendPk); void friendRequested(const ToxId& friendAddress, const QString& message); - void statusSet(Status status); - void statusSelected(Status status); + void statusSet(Status::Status status); + void statusSelected(Status::Status status); void usernameChanged(const QString& username); void changeGroupTitle(uint32_t groupnumber, const QString& title); void statusMessageChanged(const QString& statusMessage);