matrixion/src/ChatPage.h

247 lines
8.5 KiB
C
Raw Normal View History

// SPDX-FileCopyrightText: Nheko Contributors
2021-03-05 02:35:15 +03:00
//
// SPDX-License-Identifier: GPL-3.0-or-later
2017-04-06 02:06:42 +03:00
#pragma once
2017-04-06 02:06:42 +03:00
#include <atomic>
2019-12-14 19:08:36 +03:00
#include <optional>
2019-12-05 17:31:53 +03:00
#include <mtx/common.hpp>
2020-10-27 19:45:28 +03:00
#include <mtx/events.hpp>
#include <mtx/events/encrypted.hpp>
#include <mtx/events/member.hpp>
2022-10-13 18:19:54 +03:00
#include <mtx/events/policy_rules.hpp>
2020-10-27 19:45:28 +03:00
#include <mtx/events/presence.hpp>
2020-12-18 05:04:18 +03:00
#include <mtx/secret_storage.hpp>
2023-10-26 01:52:14 +03:00
#include <QDateTime>
2017-10-28 15:46:39 +03:00
#include <QMap>
#include <QPoint>
2022-01-30 15:31:39 +03:00
#include <QSharedPointer>
2017-04-06 02:06:42 +03:00
#include <QTimer>
#include "ui/RoomSummary.h"
2017-10-28 15:46:39 +03:00
class TimelineViewManager;
class UserSettings;
class NotificationsManager;
2020-07-29 00:55:47 +03:00
class TimelineModel;
2020-10-27 19:45:28 +03:00
class CallManager;
2017-04-06 02:06:42 +03:00
2020-10-27 19:45:28 +03:00
namespace mtx::requests {
struct CreateRoom;
}
namespace mtx::responses {
struct Notifications;
struct Sync;
struct Timeline;
struct Rooms;
}
2020-12-18 05:04:18 +03:00
using SecretsToDecrypt = std::map<std::string, mtx::secret_storage::AesHmacSha2EncryptedData>;
2022-10-10 15:38:29 +03:00
class ChatPage final : public QObject
2017-04-06 02:06:42 +03:00
{
2021-09-18 01:22:33 +03:00
Q_OBJECT
2017-04-06 02:06:42 +03:00
public:
2022-01-12 21:09:46 +03:00
ChatPage(QSharedPointer<UserSettings> userSettings, QObject *parent = nullptr);
2017-04-06 02:06:42 +03:00
2021-09-18 01:22:33 +03:00
// Initialize all the components of the UI.
void bootstrap(QString userid, QString homeserver, QString token);
2018-01-03 19:05:49 +03:00
2021-09-18 01:22:33 +03:00
static ChatPage *instance() { return instance_; }
2021-09-18 01:22:33 +03:00
QSharedPointer<UserSettings> userSettings() { return userSettings_; }
CallManager *callManager() { return callManager_; }
TimelineViewManager *timelineManager() { return view_manager_; }
void deleteConfigs();
2017-04-06 02:06:42 +03:00
2021-09-18 01:22:33 +03:00
void initiateLogout();
2021-09-18 01:22:33 +03:00
QString status() const;
void setStatus(const QString &status);
2021-09-18 01:22:33 +03:00
mtx::presence::PresenceState currentPresence() const;
void startChat(QString userid, std::optional<bool> encryptionEnabled);
2021-05-29 00:25:57 +03:00
//! Check if the given room is currently open.
bool isRoomActive(const QString &room_id);
const std::unique_ptr<mtx::pushrules::PushRuleEvaluator> &pushruleEvaluator() const
{
return pushrules;
}
void removeAllNotifications();
public slots:
bool handleMatrixUri(QString uri);
2021-09-18 01:22:33 +03:00
bool handleMatrixUri(const QUrl &uri);
void startChat(QString userid) { startChat(userid, std::nullopt); }
2022-03-31 00:38:38 +03:00
void leaveRoom(const QString &room_id, const QString &reason);
2021-09-18 01:22:33 +03:00
void createRoom(const mtx::requests::CreateRoom &req);
2022-03-31 00:38:38 +03:00
void joinRoom(const QString &room, const QString &reason = "");
void knockRoom(const QString &room, QString reason = "") { knockRoom(room, {}, reason, false); }
void knockRoom(const QString &room,
const std::vector<std::string> &via,
QString reason = "",
bool failedJoin = false,
bool promptForConfirmation = true);
2021-09-18 01:22:33 +03:00
void joinRoomVia(const std::string &room_id,
const std::vector<std::string> &via,
2022-03-31 00:38:38 +03:00
bool promptForConfirmation = true,
const QString &reason = "");
2021-09-18 01:22:33 +03:00
2022-05-07 19:53:16 +03:00
void inviteUser(const QString &room, QString userid, QString reason);
void kickUser(const QString &room, QString userid, QString reason);
void banUser(const QString &room, QString userid, QString reason);
void unbanUser(const QString &room, QString userid, QString reason);
2021-09-18 01:22:33 +03:00
void receivedSessionKey(const std::string &room_id, const std::string &session_id);
void decryptDownloadedSecrets(mtx::secret_storage::AesHmacSha2KeyDescription keyDesc,
const SecretsToDecrypt &secrets);
void sendNotificationReply(const QString &roomid, const QString &eventid, const QString &body);
signals:
2021-09-18 01:22:33 +03:00
void connectionLost();
void connectionRestored();
void contentLoaded();
void closing();
void changeWindowTitle(const int);
void unreadMessages(int count);
void showNotification(const QString &msg);
void showLoginPage(const QString &msg);
void showUserSettingsPage();
void ownProfileOk();
void setUserDisplayName(const QString &name);
void setUserAvatar(const QString &avatar);
void loggedOut();
void trySyncCb();
void tryDelayedSyncCb();
void tryInitialSyncCb();
void newSyncResponse(const mtx::responses::Sync &res, const std::string &prev_batch_token);
void leftRoom(const QString &room_id);
void newRoom(const QString &room_id);
void changeToRoom(const QString &room_id);
2022-05-14 02:42:21 +03:00
void startRemoveFallbackKeyTimer();
2021-09-18 01:22:33 +03:00
void initializeViews(const mtx::responses::Sync &rooms);
2021-09-18 01:22:33 +03:00
void initializeEmptyViews();
void syncUI(const mtx::responses::Sync &sync);
2021-09-18 01:22:33 +03:00
void dropToLoginPageCb(const QString &msg);
void notifyMessage(const QString &roomid,
const QString &eventid,
const QString &roomname,
const QString &sender,
const QString &message,
const QImage &icon);
void retrievedPresence(const QString &statusMsg, mtx::presence::PresenceState state);
void themeChanged();
void decryptSidebarChanged();
//! Signals for device verificaiton
void receivedDeviceVerificationAccept(const mtx::events::msg::KeyVerificationAccept &message);
void receivedDeviceVerificationRequest(const mtx::events::msg::KeyVerificationRequest &message,
std::string sender);
void receivedRoomDeviceVerificationRequest(
const mtx::events::RoomEvent<mtx::events::msg::KeyVerificationRequest> &message,
TimelineModel *model);
void receivedDeviceVerificationCancel(const mtx::events::msg::KeyVerificationCancel &message);
void receivedDeviceVerificationKey(const mtx::events::msg::KeyVerificationKey &message);
void receivedDeviceVerificationMac(const mtx::events::msg::KeyVerificationMac &message);
void receivedDeviceVerificationStart(const mtx::events::msg::KeyVerificationStart &message,
std::string sender);
void receivedDeviceVerificationReady(const mtx::events::msg::KeyVerificationReady &message);
void receivedDeviceVerificationDone(const mtx::events::msg::KeyVerificationDone &message);
void downloadedSecrets(mtx::secret_storage::AesHmacSha2KeyDescription keyDesc,
const SecretsToDecrypt &secrets);
2020-12-18 05:04:18 +03:00
void showRoomJoinPrompt(RoomSummary *);
void internalKnock(const QString &room,
const std::vector<std::string> &via,
QString reason = "",
bool failedJoin = false,
bool promptForConfirmation = true);
void newOnlineKeyBackupAvailable();
void callFunctionOnGuiThread(std::function<void()>);
private slots:
2021-09-18 01:22:33 +03:00
void logout();
void removeRoom(const QString &room_id);
void changeRoom(const QString &room_id);
void dropToLoginPage(const QString &msg);
2021-09-18 01:22:33 +03:00
void handleSyncResponse(const mtx::responses::Sync &res, const std::string &prev_batch_token);
2017-04-06 02:06:42 +03:00
private:
2021-09-18 01:22:33 +03:00
static ChatPage *instance_;
2018-01-03 19:05:49 +03:00
2021-09-18 01:22:33 +03:00
void startInitialSync();
void tryInitialSync();
void trySync();
void verifyOneTimeKeyCountAfterStartup();
2023-10-31 18:38:15 +03:00
void ensureOneTimeKeyCount(const std::map<std::string_view, uint16_t> &counts,
2022-05-14 02:42:21 +03:00
const std::optional<std::vector<std::string>> &fallback_keys);
void removeOldFallbackKey();
2021-09-18 01:22:33 +03:00
void getProfileInfo();
void getBackupVersion();
2021-09-18 01:22:33 +03:00
using UserID = QString;
using Membership = mtx::events::StateEvent<mtx::events::state::Member>;
using Memberships = std::map<std::string, Membership>;
2021-09-18 01:22:33 +03:00
void loadStateFromCache();
void resetUI();
2021-09-18 01:22:33 +03:00
template<class Collection>
Memberships getMemberships(const std::vector<Collection> &events) const;
2021-09-18 01:22:33 +03:00
template<typename T>
void connectCallMessage();
2020-07-11 02:19:48 +03:00
2021-09-18 01:22:33 +03:00
TimelineViewManager *view_manager_;
2017-04-06 02:06:42 +03:00
2021-09-18 01:22:33 +03:00
QTimer connectivityTimer_;
std::atomic_bool isConnected_;
2017-04-06 02:06:42 +03:00
2021-09-18 01:22:33 +03:00
// Global user settings.
QSharedPointer<UserSettings> userSettings_;
2022-06-18 02:35:30 +03:00
NotificationsManager *notificationsManager;
2021-09-18 01:22:33 +03:00
CallManager *callManager_;
2022-10-13 18:19:54 +03:00
std::unique_ptr<mtx::pushrules::PushRuleEvaluator> pushrules;
QDateTime lastSpacesUpdate = QDateTime::currentDateTime();
2023-10-26 00:22:39 +03:00
// Stores when our windows lost focus. Invalid when our windows have focus.
QDateTime lastWindowActive;
2017-04-06 02:06:42 +03:00
};
template<class Collection>
std::map<std::string, mtx::events::StateEvent<mtx::events::state::Member>>
ChatPage::getMemberships(const std::vector<Collection> &collection) const
{
2021-09-18 01:22:33 +03:00
std::map<std::string, mtx::events::StateEvent<mtx::events::state::Member>> memberships;
2021-09-18 01:22:33 +03:00
using Member = mtx::events::StateEvent<mtx::events::state::Member>;
2021-09-18 01:22:33 +03:00
for (const auto &event : collection) {
if (auto member = std::get_if<Member>(event)) {
memberships.emplace(member->state_key, *member);
}
2021-09-18 01:22:33 +03:00
}
2021-09-18 01:22:33 +03:00
return memberships;
}