diff --git a/cmake/CompilerFlags.cmake b/cmake/CompilerFlags.cmake index 2aa92e5..f5860a9 100644 --- a/cmake/CompilerFlags.cmake +++ b/cmake/CompilerFlags.cmake @@ -4,6 +4,7 @@ if(CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID MATCHES "GNU") -Wextra \ -Werror \ -pipe \ + -Wno-unused-function \ -pedantic \ -Wunreachable-code") diff --git a/include/AvatarProvider.h b/include/AvatarProvider.h index fd5f15c..af13a57 100644 --- a/include/AvatarProvider.h +++ b/include/AvatarProvider.h @@ -22,7 +22,6 @@ #include #include -class MatrixClient; class TimelineItem; class Cache; @@ -31,11 +30,7 @@ class AvatarProvider : public QObject Q_OBJECT public: - static void init(QSharedPointer client, QSharedPointer cache) - { - client_ = client; - cache_ = cache; - } + static void init(QSharedPointer cache) { cache_ = cache; } //! The callback is called with the downloaded avatar for the given user //! or the avatar is downloaded first and then saved for re-use. static void resolve(const QString &room_id, @@ -44,6 +39,5 @@ public: std::function callback); private: - static QSharedPointer client_; static QSharedPointer cache_; }; diff --git a/include/ChatPage.h b/include/ChatPage.h index 6e0cf2d..057ce69 100644 --- a/include/ChatPage.h +++ b/include/ChatPage.h @@ -30,7 +30,6 @@ #include -class MatrixClient; class OverlayModal; class QuickSwitcher; class RoomList; @@ -59,9 +58,7 @@ class ChatPage : public QWidget Q_OBJECT public: - ChatPage(QSharedPointer client, - QSharedPointer userSettings, - QWidget *parent = 0); + ChatPage(QSharedPointer userSettings, QWidget *parent = 0); // Initialize all the components of the UI. void bootstrap(QString userid, QString homeserver, QString token); @@ -70,14 +67,6 @@ public: QString currentRoom() const { return current_room_; } static ChatPage *instance() { return instance_; } - void readEvent(const QString &room_id, const QString &event_id) - { - client_->readEvent(room_id, event_id); - } - void redactEvent(const QString &room_id, const QString &event_id) - { - client_->redactEvent(room_id, event_id); - } QSharedPointer userSettings() { return userSettings_; } QSharedPointer cache() { return cache_; } @@ -192,9 +181,6 @@ private: QSharedPointer receiptsDialog_; QSharedPointer receiptsModal_; - // Matrix Client API provider. - QSharedPointer client_; - // Global user settings. QSharedPointer userSettings_; diff --git a/include/CommunitiesList.h b/include/CommunitiesList.h index 81408f5..3299e7c 100644 --- a/include/CommunitiesList.h +++ b/include/CommunitiesList.h @@ -6,7 +6,6 @@ #include "CommunitiesListItem.h" #include "Community.h" -#include "MatrixClient.h" #include "ui/Theme.h" class CommunitiesList : public QWidget @@ -14,7 +13,7 @@ class CommunitiesList : public QWidget Q_OBJECT public: - CommunitiesList(QSharedPointer client, QWidget *parent = nullptr); + CommunitiesList(QWidget *parent = nullptr); void setCommunities(const std::map> &communities); void clear() { communities_.clear(); } @@ -44,6 +43,4 @@ private: QScrollArea *scrollArea_; std::map> communities_; - - QSharedPointer client_; }; diff --git a/include/LoginPage.h b/include/LoginPage.h index 3483522..34a08df 100644 --- a/include/LoginPage.h +++ b/include/LoginPage.h @@ -24,7 +24,6 @@ class FlatButton; class LoadingIndicator; -class MatrixClient; class OverlayModal; class RaisedButton; class TextField; @@ -34,7 +33,7 @@ class LoginPage : public QWidget Q_OBJECT public: - LoginPage(QSharedPointer client, QWidget *parent = 0); + LoginPage(QWidget *parent = 0); void reset(); @@ -97,7 +96,4 @@ private: TextField *matrixid_input_; TextField *password_input_; TextField *serverInput_; - - // Matrix client API provider. - QSharedPointer client_; }; diff --git a/include/MainWindow.h b/include/MainWindow.h index 1ffbe85..0fbc756 100644 --- a/include/MainWindow.h +++ b/include/MainWindow.h @@ -31,7 +31,6 @@ class ChatPage; class LoadingIndicator; -class MatrixClient; class OverlayModal; class SnackBar; class TrayIcon; @@ -125,8 +124,6 @@ private: //! Used to hide undefined states between page transitions. QSharedPointer progressModal_; QSharedPointer spinner_; - //! Matrix Client API provider. - QSharedPointer client_; //! Tray icon that shows the unread message count. TrayIcon *trayIcon_; //! Notifications display. diff --git a/include/MatrixClient.h b/include/MatrixClient.h index 35f05c3..6ed1c2c 100644 --- a/include/MatrixClient.h +++ b/include/MatrixClient.h @@ -43,7 +43,7 @@ class MatrixClient : public QNetworkAccessManager { Q_OBJECT public: - MatrixClient(QString server, QObject *parent = 0); + MatrixClient(QObject *parent = 0); // Client API. void initialSync() noexcept; @@ -211,3 +211,13 @@ private: //! Filter to be send as filter-param for (initial) /sync requests. QString filter_; }; + +namespace http { +//! Initialize the http module +void +init(QObject *parent); + +//! Retrieve the client instance. +MatrixClient * +client(); +} diff --git a/include/RegisterPage.h b/include/RegisterPage.h index 32f2fcf..f4d9781 100644 --- a/include/RegisterPage.h +++ b/include/RegisterPage.h @@ -23,7 +23,6 @@ #include class FlatButton; -class MatrixClient; class RaisedButton; class TextField; @@ -36,7 +35,7 @@ class RegisterPage : public QWidget Q_OBJECT public: - RegisterPage(QSharedPointer client, QWidget *parent = 0); + RegisterPage(QWidget *parent = 0); protected: void paintEvent(QPaintEvent *event) override; @@ -75,8 +74,6 @@ private: TextField *password_confirmation_; TextField *server_input_; - // Matrix client API provider. - QSharedPointer client_; //! ReCaptcha dialog. std::shared_ptr captchaDialog_; }; diff --git a/include/RoomList.h b/include/RoomList.h index bdfb721..202e075 100644 --- a/include/RoomList.h +++ b/include/RoomList.h @@ -26,7 +26,6 @@ #include class LeaveRoomDialog; -class MatrixClient; class OverlayModal; class RoomInfoListItem; class Sync; @@ -40,9 +39,7 @@ class RoomList : public QWidget Q_OBJECT public: - RoomList(QSharedPointer client, - QSharedPointer userSettings, - QWidget *parent = 0); + RoomList(QSharedPointer userSettings, QWidget *parent = 0); void setCache(QSharedPointer cache) { cache_ = cache; } void initialize(const QMap &info); @@ -105,7 +102,6 @@ private: //! Which rooms to include in the room list. std::vector roomFilter_; - QSharedPointer client_; QSharedPointer cache_; QSharedPointer userSettings_; diff --git a/include/timeline/TimelineItem.h b/include/timeline/TimelineItem.h index 82ff5ca..064dead 100644 --- a/include/timeline/TimelineItem.h +++ b/include/timeline/TimelineItem.h @@ -26,11 +26,11 @@ #include #include "AvatarProvider.h" -#include "ChatPage.h" #include "RoomInfoListItem.h" #include "Utils.h" #include "Cache.h" +#include "MatrixClient.h" class ImageItem; class StickerItem; @@ -121,7 +121,7 @@ public: void sendReadReceipt() const { if (!event_id_.isEmpty()) - ChatPage::instance()->readEvent(room_id_, event_id_); + http::client()->readEvent(room_id_, event_id_); } //! Add a user avatar for this event. diff --git a/include/timeline/TimelineView.h b/include/timeline/TimelineView.h index 02e2872..2ad0005 100644 --- a/include/timeline/TimelineView.h +++ b/include/timeline/TimelineView.h @@ -25,6 +25,7 @@ #include #include #include +#include #include #include @@ -117,12 +118,9 @@ class TimelineView : public QWidget public: TimelineView(const mtx::responses::Timeline &timeline, - QSharedPointer client, - const QString &room_id, - QWidget *parent = 0); - TimelineView(QSharedPointer client, const QString &room_id, QWidget *parent = 0); + TimelineView(const QString &room_id, QWidget *parent = 0); // Add new events at the end of the timeline. void addEvents(const mtx::responses::Timeline &timeline); @@ -298,7 +296,6 @@ private: QMap eventIds_; QQueue pending_msgs_; QList pending_sent_msgs_; - QSharedPointer client_; }; template @@ -311,7 +308,7 @@ TimelineView::addUserMessage(const QString &url, auto with_sender = (lastSender_ != local_user_) || isDateDifference(lastMsgTimestamp_); auto trimmed = QFileInfo{filename}.fileName(); // Trim file path. - auto widget = new Widget(client_, url, trimmed, size, this); + auto widget = new Widget(url, trimmed, size, this); TimelineItem *view_item = new TimelineItem(widget, local_user_, with_sender, room_id_, scroll_widget_); @@ -325,7 +322,7 @@ TimelineView::addUserMessage(const QString &url, // Keep track of the sender and the timestamp of the current message. saveLastMessageInfo(local_user_, QDateTime::currentDateTime()); - int txn_id = client_->incrementTransactionId(); + int txn_id = http::client()->incrementTransactionId(); PendingMessage message(MsgType, txn_id, url, trimmed, mime, size, "", view_item); handleNewUserMessage(message); @@ -343,7 +340,7 @@ template TimelineItem * TimelineView::createTimelineItem(const Event &event, bool withSender) { - auto eventWidget = new Widget(client_, event); + auto eventWidget = new Widget(event); auto item = new TimelineItem(eventWidget, event, withSender, room_id_, scroll_widget_); return item; diff --git a/include/timeline/TimelineViewManager.h b/include/timeline/TimelineViewManager.h index 4c99409..308b83a 100644 --- a/include/timeline/TimelineViewManager.h +++ b/include/timeline/TimelineViewManager.h @@ -24,7 +24,6 @@ class QFile; -class MatrixClient; class RoomInfoListItem; class TimelineView; struct DescInfo; @@ -34,7 +33,7 @@ class TimelineViewManager : public QStackedWidget Q_OBJECT public: - TimelineViewManager(QSharedPointer client, QWidget *parent); + TimelineViewManager(QWidget *parent); // Initialize with timeline events. void initialize(const mtx::responses::Rooms &rooms); @@ -91,5 +90,4 @@ private: QString active_room_; std::map> views_; - QSharedPointer client_; }; diff --git a/include/timeline/widgets/AudioItem.h b/include/timeline/widgets/AudioItem.h index b1d47dd..1020c3e 100644 --- a/include/timeline/widgets/AudioItem.h +++ b/include/timeline/widgets/AudioItem.h @@ -24,8 +24,6 @@ #include #include -#include "MatrixClient.h" - #include class AudioItem : public QWidget @@ -42,12 +40,10 @@ class AudioItem : public QWidget durationForegroundColor) public: - AudioItem(QSharedPointer client, - const mtx::events::RoomEvent &event, + AudioItem(const mtx::events::RoomEvent &event, QWidget *parent = nullptr); - AudioItem(QSharedPointer client, - const QString &url, + AudioItem(const QString &url, const QString &filename, uint64_t size, QWidget *parent = nullptr); @@ -90,7 +86,6 @@ private: QString filenameToSave_; mtx::events::RoomEvent event_; - QSharedPointer client_; QMediaPlayer *player_; diff --git a/include/timeline/widgets/FileItem.h b/include/timeline/widgets/FileItem.h index bc75913..4f590df 100644 --- a/include/timeline/widgets/FileItem.h +++ b/include/timeline/widgets/FileItem.h @@ -25,8 +25,6 @@ #include -#include "MatrixClient.h" - class FileItem : public QWidget { Q_OBJECT @@ -36,12 +34,10 @@ class FileItem : public QWidget Q_PROPERTY(QColor backgroundColor WRITE setBackgroundColor READ backgroundColor) public: - FileItem(QSharedPointer client, - const mtx::events::RoomEvent &event, + FileItem(const mtx::events::RoomEvent &event, QWidget *parent = nullptr); - FileItem(QSharedPointer client, - const QString &url, + FileItem(const QString &url, const QString &filename, uint64_t size, QWidget *parent = nullptr); @@ -71,7 +67,6 @@ private: QString filenameToSave_; mtx::events::RoomEvent event_; - QSharedPointer client_; QIcon icon_; diff --git a/include/timeline/widgets/ImageItem.h b/include/timeline/widgets/ImageItem.h index 146aa7f..b17b2d8 100644 --- a/include/timeline/widgets/ImageItem.h +++ b/include/timeline/widgets/ImageItem.h @@ -24,8 +24,6 @@ #include -#include "MatrixClient.h" - namespace dialogs { class ImageOverlay; } @@ -34,12 +32,10 @@ class ImageItem : public QWidget { Q_OBJECT public: - ImageItem(QSharedPointer client, - const mtx::events::RoomEvent &event, + ImageItem(const mtx::events::RoomEvent &event, QWidget *parent = nullptr); - ImageItem(QSharedPointer client, - const QString &url, + ImageItem(const QString &url, const QString &filename, uint64_t size, QWidget *parent = nullptr); @@ -81,8 +77,6 @@ private: QRectF imageRegion_; mtx::events::RoomEvent event_; - - QSharedPointer client_; }; class StickerItem : public ImageItem @@ -90,11 +84,8 @@ class StickerItem : public ImageItem Q_OBJECT public: - StickerItem(QSharedPointer client, - const mtx::events::Sticker &event, - QWidget *parent = nullptr) - : ImageItem{client, - QString::fromStdString(event.content.url), + StickerItem(const mtx::events::Sticker &event, QWidget *parent = nullptr) + : ImageItem{QString::fromStdString(event.content.url), QString::fromStdString(event.content.body), event.content.info.size, parent} diff --git a/include/timeline/widgets/VideoItem.h b/include/timeline/widgets/VideoItem.h index 8973297..26fa1c3 100644 --- a/include/timeline/widgets/VideoItem.h +++ b/include/timeline/widgets/VideoItem.h @@ -20,10 +20,9 @@ #include #include #include +#include #include -#include "MatrixClient.h" - #include class VideoItem : public QWidget @@ -31,12 +30,10 @@ class VideoItem : public QWidget Q_OBJECT public: - VideoItem(QSharedPointer client, - const mtx::events::RoomEvent &event, + VideoItem(const mtx::events::RoomEvent &event, QWidget *parent = nullptr); - VideoItem(QSharedPointer client, - const QString &url, + VideoItem(const QString &url, const QString &filename, uint64_t size, QWidget *parent = nullptr); @@ -51,5 +48,4 @@ private: QLabel *label_; mtx::events::RoomEvent event_; - QSharedPointer client_; }; diff --git a/src/AvatarProvider.cc b/src/AvatarProvider.cc index c774523..51ff27a 100644 --- a/src/AvatarProvider.cc +++ b/src/AvatarProvider.cc @@ -22,7 +22,6 @@ #include "Cache.h" #include "MatrixClient.h" -QSharedPointer AvatarProvider::client_; QSharedPointer AvatarProvider::cache_; void @@ -46,7 +45,7 @@ AvatarProvider::resolve(const QString &room_id, return; } - auto proxy = client_->fetchUserAvatar(avatarUrl); + auto proxy = http::client()->fetchUserAvatar(avatarUrl); if (proxy.isNull()) return; diff --git a/src/ChatPage.cc b/src/ChatPage.cc index 1f5fa99..649d783 100644 --- a/src/ChatPage.cc +++ b/src/ChatPage.cc @@ -48,11 +48,8 @@ constexpr int INITIAL_SYNC_RETRY_TIMEOUT = 240 * 1000; ChatPage *ChatPage::instance_ = nullptr; -ChatPage::ChatPage(QSharedPointer client, - QSharedPointer userSettings, - QWidget *parent) +ChatPage::ChatPage(QSharedPointer userSettings, QWidget *parent) : QWidget(parent) - , client_(client) , userSettings_{userSettings} { setObjectName("chatPage"); @@ -61,7 +58,7 @@ ChatPage::ChatPage(QSharedPointer client, topLayout_->setSpacing(0); topLayout_->setMargin(0); - communitiesList_ = new CommunitiesList(client, this); + communitiesList_ = new CommunitiesList(this); topLayout_->addWidget(communitiesList_); auto splitter = new Splitter(this); @@ -82,12 +79,12 @@ ChatPage::ChatPage(QSharedPointer client, connect( sidebarActions_, &SideBarActions::showSettings, this, &ChatPage::showUserSettingsPage); connect( - sidebarActions_, &SideBarActions::joinRoom, client_.data(), &MatrixClient::joinRoom); + sidebarActions_, &SideBarActions::joinRoom, http::client(), &MatrixClient::joinRoom); connect( - sidebarActions_, &SideBarActions::createRoom, client_.data(), &MatrixClient::createRoom); + sidebarActions_, &SideBarActions::createRoom, http::client(), &MatrixClient::createRoom); user_info_widget_ = new UserInfoWidget(sideBar_); - room_list_ = new RoomList(client, userSettings_, sideBar_); + room_list_ = new RoomList(userSettings_, sideBar_); sideBarLayout_->addWidget(user_info_widget_); sideBarLayout_->addWidget(room_list_); @@ -105,7 +102,7 @@ ChatPage::ChatPage(QSharedPointer client, contentLayout_->setMargin(0); top_bar_ = new TopRoomBar(this); - view_manager_ = new TimelineViewManager(client, this); + view_manager_ = new TimelineViewManager(this); contentLayout_->addWidget(top_bar_); contentLayout_->addWidget(view_manager_); @@ -124,15 +121,15 @@ ChatPage::ChatPage(QSharedPointer client, typingRefresher_->setInterval(TYPING_REFRESH_TIMEOUT); connect(user_info_widget_, &UserInfoWidget::logout, this, [this]() { - client_->logout(); + http::client()->logout(); emit showOverlayProgressBar(); }); - connect(client_.data(), &MatrixClient::loggedOut, this, &ChatPage::logout); + connect(http::client(), &MatrixClient::loggedOut, this, &ChatPage::logout); connect(top_bar_, &TopRoomBar::inviteUsers, this, [this](QStringList users) { for (int ii = 0; ii < users.size(); ++ii) { QTimer::singleShot(ii * 1000, this, [this, ii, users]() { - client_->inviteUser(current_room_, users.at(ii)); + http::client()->inviteUser(current_room_, users.at(ii)); }); } }); @@ -158,12 +155,12 @@ ChatPage::ChatPage(QSharedPointer client, connect(room_list_, &RoomList::acceptInvite, this, [this](const QString &room_id) { view_manager_->addRoom(room_id); - client_->joinRoom(room_id); + http::client()->joinRoom(room_id); room_list_->removeRoom(room_id, currentRoom() == room_id); }); connect(room_list_, &RoomList::declineInvite, this, [this](const QString &room_id) { - client_->leaveRoom(room_id); + http::client()->leaveRoom(room_id); room_list_->removeRoom(room_id, currentRoom() == room_id); }); @@ -172,7 +169,7 @@ ChatPage::ChatPage(QSharedPointer client, return; typingRefresher_->start(); - client_->sendTypingNotification(current_room_); + http::client()->sendTypingNotification(current_room_); }); connect(text_input_, &TextInputWidget::stoppedTyping, this, [this]() { @@ -180,14 +177,14 @@ ChatPage::ChatPage(QSharedPointer client, return; typingRefresher_->stop(); - client_->removeTypingNotification(current_room_); + http::client()->removeTypingNotification(current_room_); }); connect(typingRefresher_, &QTimer::timeout, this, [this]() { if (!userSettings_->isTypingNotificationsEnabled()) return; - client_->sendTypingNotification(current_room_); + http::client()->sendTypingNotification(current_room_); }); connect(view_manager_, @@ -212,45 +209,45 @@ ChatPage::ChatPage(QSharedPointer client, connect(text_input_, &TextInputWidget::sendJoinRoomRequest, - client_.data(), + http::client(), &MatrixClient::joinRoom); connect(text_input_, &TextInputWidget::uploadImage, this, [this](QSharedPointer data, const QString &fn) { - client_->uploadImage(current_room_, fn, data); + http::client()->uploadImage(current_room_, fn, data); }); connect(text_input_, &TextInputWidget::uploadFile, this, [this](QSharedPointer data, const QString &fn) { - client_->uploadFile(current_room_, fn, data); + http::client()->uploadFile(current_room_, fn, data); }); connect(text_input_, &TextInputWidget::uploadAudio, this, [this](QSharedPointer data, const QString &fn) { - client_->uploadAudio(current_room_, fn, data); + http::client()->uploadAudio(current_room_, fn, data); }); connect(text_input_, &TextInputWidget::uploadVideo, this, [this](QSharedPointer data, const QString &fn) { - client_->uploadVideo(current_room_, fn, data); + http::client()->uploadVideo(current_room_, fn, data); }); connect( - client_.data(), &MatrixClient::roomCreationFailed, this, &ChatPage::showNotification); - connect(client_.data(), &MatrixClient::joinFailed, this, &ChatPage::showNotification); - connect(client_.data(), &MatrixClient::uploadFailed, this, [this](int, const QString &msg) { + http::client(), &MatrixClient::roomCreationFailed, this, &ChatPage::showNotification); + connect(http::client(), &MatrixClient::joinFailed, this, &ChatPage::showNotification); + connect(http::client(), &MatrixClient::uploadFailed, this, [this](int, const QString &msg) { text_input_->hideUploadSpinner(); emit showNotification(msg); }); connect( - client_.data(), + http::client(), &MatrixClient::imageUploaded, this, [this](QString roomid, QString filename, QString url, QString mime, uint64_t dsize) { @@ -258,7 +255,7 @@ ChatPage::ChatPage(QSharedPointer client, view_manager_->queueImageMessage(roomid, filename, url, mime, dsize); }); connect( - client_.data(), + http::client(), &MatrixClient::fileUploaded, this, [this](QString roomid, QString filename, QString url, QString mime, uint64_t dsize) { @@ -266,7 +263,7 @@ ChatPage::ChatPage(QSharedPointer client, view_manager_->queueFileMessage(roomid, filename, url, mime, dsize); }); connect( - client_.data(), + http::client(), &MatrixClient::audioUploaded, this, [this](QString roomid, QString filename, QString url, QString mime, uint64_t dsize) { @@ -274,7 +271,7 @@ ChatPage::ChatPage(QSharedPointer client, view_manager_->queueAudioMessage(roomid, filename, url, mime, dsize); }); connect( - client_.data(), + http::client(), &MatrixClient::videoUploaded, this, [this](QString roomid, QString filename, QString url, QString mime, uint64_t dsize) { @@ -284,28 +281,28 @@ ChatPage::ChatPage(QSharedPointer client, connect(room_list_, &RoomList::roomAvatarChanged, this, &ChatPage::updateTopBarAvatar); - connect(client_.data(), + connect(http::client(), &MatrixClient::initialSyncCompleted, this, &ChatPage::initialSyncCompleted); connect( - client_.data(), &MatrixClient::initialSyncFailed, this, &ChatPage::retryInitialSync); - connect(client_.data(), &MatrixClient::syncCompleted, this, &ChatPage::syncCompleted); - connect(client_.data(), + http::client(), &MatrixClient::initialSyncFailed, this, &ChatPage::retryInitialSync); + connect(http::client(), &MatrixClient::syncCompleted, this, &ChatPage::syncCompleted); + connect(http::client(), &MatrixClient::getOwnProfileResponse, this, &ChatPage::updateOwnProfileInfo); - connect(client_.data(), + connect(http::client(), SIGNAL(getOwnCommunitiesResponse(QList)), this, SLOT(updateOwnCommunitiesInfo(QList))); - connect(client_.data(), + connect(http::client(), &MatrixClient::communityProfileRetrieved, this, [this](QString communityId, QJsonObject profile) { communities_[communityId]->parseProfile(profile); }); - connect(client_.data(), + connect(http::client(), &MatrixClient::communityRoomsRetrieved, this, [this](QString communityId, QJsonObject rooms) { @@ -321,7 +318,7 @@ ChatPage::ChatPage(QSharedPointer client, } }); - connect(client_.data(), &MatrixClient::joinedRoom, this, [this](const QString &room_id) { + connect(http::client(), &MatrixClient::joinedRoom, this, [this](const QString &room_id) { emit showNotification("You joined the room."); // We remove any invites with the same room_id. @@ -332,17 +329,17 @@ ChatPage::ChatPage(QSharedPointer client, .arg(QString::fromStdString(e.what()))); } }); - connect(client_.data(), &MatrixClient::leftRoom, this, &ChatPage::removeRoom); - connect(client_.data(), &MatrixClient::invitedUser, this, [this](QString, QString user) { + connect(http::client(), &MatrixClient::leftRoom, this, &ChatPage::removeRoom); + connect(http::client(), &MatrixClient::invitedUser, this, [this](QString, QString user) { emit showNotification(QString("Invited user %1").arg(user)); }); - connect(client_.data(), &MatrixClient::roomCreated, this, [this](QString room_id) { + connect(http::client(), &MatrixClient::roomCreated, this, [this](QString room_id) { emit showNotification(QString("Room %1 created").arg(room_id)); }); - connect(client_.data(), &MatrixClient::redactionFailed, this, [this](const QString &error) { + connect(http::client(), &MatrixClient::redactionFailed, this, [this](const QString &error) { emit showNotification(QString("Message redaction failed: %1").arg(error)); }); - connect(client_.data(), + connect(http::client(), &MatrixClient::notificationsRetrieved, this, &ChatPage::sendDesktopNotifications); @@ -369,13 +366,13 @@ ChatPage::ChatPage(QSharedPointer client, syncTimeoutTimer_ = new QTimer(this); connect(syncTimeoutTimer_, &QTimer::timeout, this, [this]() { - if (client_->getHomeServer().isEmpty()) { + if (http::client()->getHomeServer().isEmpty()) { syncTimeoutTimer_->stop(); return; } qDebug() << "Sync took too long. Retrying..."; - client_->sync(); + http::client()->sync(); }); connect(communitiesList_, @@ -399,8 +396,8 @@ ChatPage::ChatPage(QSharedPointer client, connect(this, &ChatPage::continueSync, this, [this](const QString &next_batch) { syncTimeoutTimer_->start(SYNC_RETRY_TIMEOUT); - client_->setNextBatchToken(next_batch); - client_->sync(); + http::client()->setNextBatchToken(next_batch); + http::client()->sync(); }); connect(this, &ChatPage::startConsesusTimer, this, [this]() { @@ -440,7 +437,7 @@ ChatPage::ChatPage(QSharedPointer client, } if (hasNotifications) - client_->getNotifications(); + http::client()->getNotifications(); }); connect(this, &ChatPage::syncRoomlist, room_list_, &RoomList::sync); @@ -489,22 +486,22 @@ ChatPage::deleteConfigs() cache_->deleteData(); - client_->reset(); + http::client()->reset(); } void ChatPage::bootstrap(QString userid, QString homeserver, QString token) { - client_->setServer(homeserver); - client_->setAccessToken(token); - client_->getOwnProfile(); - client_->getOwnCommunities(); + http::client()->setServer(homeserver); + http::client()->setAccessToken(token); + http::client()->getOwnProfile(); + http::client()->getOwnCommunities(); cache_ = QSharedPointer(new Cache(userid)); room_list_->setCache(cache_); text_input_->setCache(cache_); - AvatarProvider::init(client_, cache_); + AvatarProvider::init(cache_); try { cache_->setup(); @@ -525,7 +522,7 @@ ChatPage::bootstrap(QString userid, QString homeserver, QString token) qInfo() << "Falling back to initial sync ..."; } - client_->initialSync(); + http::client()->initialSync(); initialSyncTimer_->start(INITIAL_SYNC_RETRY_TIMEOUT); } @@ -602,7 +599,7 @@ ChatPage::updateOwnProfileInfo(const QUrl &avatar_url, const QString &display_na } } - auto proxy = client_->fetchUserAvatar(avatar_url); + auto proxy = http::client()->fetchUserAvatar(avatar_url); if (proxy.isNull()) return; @@ -825,7 +822,7 @@ ChatPage::retryInitialSync(int status_code) { initialSyncTimer_->stop(); - if (client_->getHomeServer().isEmpty()) { + if (http::client()->getHomeServer().isEmpty()) { deleteConfigs(); resetUI(); emit showLoginPage("Sync error. Please try again."); @@ -836,7 +833,7 @@ ChatPage::retryInitialSync(int status_code) if (status_code == -1 || status_code == 504 || status_code == 502 || status_code == 524) { qWarning() << "retrying initial sync"; - client_->initialSync(); + http::client()->initialSync(); initialSyncTimer_->start(INITIAL_SYNC_RETRY_TIMEOUT); } else { // Drop into the login screen. diff --git a/src/CommunitiesList.cc b/src/CommunitiesList.cc index 25021ba..0d7f5aa 100644 --- a/src/CommunitiesList.cc +++ b/src/CommunitiesList.cc @@ -1,10 +1,10 @@ #include "CommunitiesList.h" +#include "MatrixClient.h" #include -CommunitiesList::CommunitiesList(QSharedPointer client, QWidget *parent) +CommunitiesList::CommunitiesList(QWidget *parent) : QWidget(parent) - , client_(client) { QSizePolicy sizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding); sizePolicy.setHorizontalStretch(0); @@ -38,14 +38,14 @@ CommunitiesList::CommunitiesList(QSharedPointer client, QWidget *p scrollArea_->setWidget(scrollAreaContents_); topLayout_->addWidget(scrollArea_); - connect(client_.data(), + connect(http::client(), &MatrixClient::communityProfileRetrieved, this, - [this](QString communityId, QJsonObject profile) { - client_->fetchCommunityAvatar(communityId, - QUrl(profile["avatar_url"].toString())); + [](QString communityId, QJsonObject profile) { + http::client()->fetchCommunityAvatar( + communityId, QUrl(profile["avatar_url"].toString())); }); - connect(client_.data(), + connect(http::client(), SIGNAL(communityAvatarRetrieved(const QString &, const QPixmap &)), this, SLOT(updateCommunityAvatar(const QString &, const QPixmap &))); @@ -61,8 +61,8 @@ CommunitiesList::setCommunities(const std::mapfetchCommunityProfile(community.first); - client_->fetchCommunityRooms(community.first); + http::client()->fetchCommunityProfile(community.first); + http::client()->fetchCommunityRooms(community.first); } communities_["world"]->setPressedState(true); @@ -77,7 +77,7 @@ CommunitiesList::addCommunity(QSharedPointer community, const QString communities_.emplace(community_id, QSharedPointer(list_item)); - client_->fetchCommunityAvatar(community_id, community->getAvatar()); + http::client()->fetchCommunityAvatar(community_id, community->getAvatar()); contentsLayout_->insertWidget(contentsLayout_->count() - 1, list_item); diff --git a/src/LoginPage.cc b/src/LoginPage.cc index b820722..c7f9b04 100644 --- a/src/LoginPage.cc +++ b/src/LoginPage.cc @@ -30,10 +30,9 @@ using namespace mtx::identifiers; -LoginPage::LoginPage(QSharedPointer client, QWidget *parent) +LoginPage::LoginPage(QWidget *parent) : QWidget(parent) , inferredServerAddress_() - , client_{client} { top_layout_ = new QVBoxLayout(); @@ -143,11 +142,11 @@ LoginPage::LoginPage(QSharedPointer client, QWidget *parent) connect(matrixid_input_, SIGNAL(returnPressed()), login_button_, SLOT(click())); connect(password_input_, SIGNAL(returnPressed()), login_button_, SLOT(click())); connect(serverInput_, SIGNAL(returnPressed()), login_button_, SLOT(click())); - connect(client_.data(), SIGNAL(loginError(QString)), this, SLOT(loginError(QString))); - connect(client_.data(), SIGNAL(loginError(QString)), this, SIGNAL(errorOccurred())); + connect(http::client(), SIGNAL(loginError(QString)), this, SLOT(loginError(QString))); + connect(http::client(), SIGNAL(loginError(QString)), this, SIGNAL(errorOccurred())); connect(matrixid_input_, SIGNAL(editingFinished()), this, SLOT(onMatrixIdEntered())); - connect(client_.data(), SIGNAL(versionError(QString)), this, SLOT(versionError(QString))); - connect(client_.data(), SIGNAL(versionSuccess()), this, SLOT(versionSuccess())); + connect(http::client(), SIGNAL(versionError(QString)), this, SLOT(versionError(QString))); + connect(http::client(), SIGNAL(versionSuccess()), this, SLOT(versionSuccess())); connect(serverInput_, SIGNAL(editingFinished()), this, SLOT(onServerAddressEntered())); } @@ -181,8 +180,8 @@ LoginPage::onMatrixIdEntered() inferredServerAddress_ = homeServer; serverInput_->setText(homeServer); - client_->setServer(homeServer); - client_->versions(); + http::client()->setServer(homeServer); + http::client()->versions(); } } @@ -190,8 +189,8 @@ void LoginPage::onServerAddressEntered() { error_label_->setText(""); - client_->setServer(serverInput_->text()); - client_->versions(); + http::client()->setServer(serverInput_->text()); + http::client()->versions(); serverLayout_->removeWidget(errorIcon_); errorIcon_->hide(); @@ -202,7 +201,7 @@ LoginPage::onServerAddressEntered() void LoginPage::versionError(QString error) { - QUrl currentServer = client_->getHomeServer(); + QUrl currentServer = http::client()->getHomeServer(); QString mxidAddress = matrixid_input_->text().split(":").at(1); error_label_->setText(error); @@ -242,8 +241,8 @@ LoginPage::onLoginButtonClicked() if (password_input_->text().isEmpty()) return loginError(tr("Empty password")); - client_->setServer(serverInput_->text()); - client_->login(QString::fromStdString(user.localpart()), password_input_->text()); + http::client()->setServer(serverInput_->text()); + http::client()->login(QString::fromStdString(user.localpart()), password_input_->text()); emit loggingIn(); } diff --git a/src/MainWindow.cc b/src/MainWindow.cc index 404baec..b0a0af4 100644 --- a/src/MainWindow.cc +++ b/src/MainWindow.cc @@ -54,6 +54,9 @@ MainWindow::MainWindow(QWidget *parent) setWindowTitle("nheko"); setObjectName("MainWindow"); + // Initialize the http client. + http::init(this); + restoreWindowSize(); QFont font("Open Sans"); @@ -61,14 +64,13 @@ MainWindow::MainWindow(QWidget *parent) font.setStyleStrategy(QFont::PreferAntialias); setFont(font); - client_ = QSharedPointer(new MatrixClient("matrix.org")); userSettings_ = QSharedPointer(new UserSettings); trayIcon_ = new TrayIcon(":/logos/nheko-32.png", this); welcome_page_ = new WelcomePage(this); - login_page_ = new LoginPage(client_, this); - register_page_ = new RegisterPage(client_, this); - chat_page_ = new ChatPage(client_, userSettings_, this); + login_page_ = new LoginPage(this); + register_page_ = new RegisterPage(this); + chat_page_ = new ChatPage(userSettings_, this); userSettingsPage_ = new UserSettingsPage(userSettings_, this); // Initialize sliding widget manager. @@ -122,16 +124,16 @@ MainWindow::MainWindow(QWidget *parent) connect( chat_page_, &ChatPage::showUserSettingsPage, this, &MainWindow::showUserSettingsPage); - connect(client_.data(), + connect(http::client(), SIGNAL(loginSuccess(QString, QString, QString)), this, SLOT(showChatPage(QString, QString, QString))); - connect(client_.data(), + connect(http::client(), SIGNAL(registerSuccess(QString, QString, QString)), this, SLOT(showChatPage(QString, QString, QString))); - connect(client_.data(), &MatrixClient::invalidToken, this, [this]() { + connect(http::client(), &MatrixClient::invalidToken, this, [this]() { chat_page_->deleteConfigs(); showLoginPage(); login_page_->loginError("Invalid token detected. Please try to login again."); @@ -315,7 +317,7 @@ MainWindow::openLeaveRoomDialog(const QString &room_id) leaveRoomModal_->hide(); if (leaving) - client_->leaveRoom(roomToLeave); + http::client()->leaveRoom(roomToLeave); }); leaveRoomModal_ = diff --git a/src/MatrixClient.cc b/src/MatrixClient.cc index 54756c7..c6ca74a 100644 --- a/src/MatrixClient.cc +++ b/src/MatrixClient.cc @@ -33,7 +33,27 @@ #include "MatrixClient.h" -MatrixClient::MatrixClient(QString server, QObject *parent) +namespace { +MatrixClient *instance_ = nullptr; +} + +namespace http { + +void +init(QObject *parent) +{ + if (!instance_) + instance_ = new MatrixClient(parent); +} + +MatrixClient * +client() +{ + return instance_; +} +} + +MatrixClient::MatrixClient(QObject *parent) : QNetworkAccessManager(parent) , clientApiUrl_{"/_matrix/client/r0"} , mediaApiUrl_{"/_matrix/media/r0"} @@ -55,8 +75,6 @@ MatrixClient::MatrixClient(QString server, QObject *parent) [](QNetworkReply *reply, const QList &) { reply->ignoreSslErrors(); }); } - setServer(server); - QJsonObject default_filter{ { "room", diff --git a/src/RegisterPage.cc b/src/RegisterPage.cc index c0fa11a..7d80b72 100644 --- a/src/RegisterPage.cc +++ b/src/RegisterPage.cc @@ -28,9 +28,8 @@ #include "dialogs/ReCaptcha.hpp" -RegisterPage::RegisterPage(QSharedPointer client, QWidget *parent) +RegisterPage::RegisterPage(QWidget *parent) : QWidget(parent) - , client_(client) { top_layout_ = new QVBoxLayout(); @@ -126,11 +125,11 @@ RegisterPage::RegisterPage(QSharedPointer client, QWidget *parent) connect(password_input_, SIGNAL(returnPressed()), register_button_, SLOT(click())); connect(password_confirmation_, SIGNAL(returnPressed()), register_button_, SLOT(click())); connect(server_input_, SIGNAL(returnPressed()), register_button_, SLOT(click())); - connect(client_.data(), + connect(http::client(), SIGNAL(registerError(const QString &)), this, SLOT(registerError(const QString &))); - connect(client_.data(), + connect(http::client(), &MatrixClient::registrationFlow, this, [this](const QString &user, @@ -148,7 +147,8 @@ RegisterPage::RegisterPage(QSharedPointer client, QWidget *parent) [this, user, pass, server, session]() { captchaDialog_->close(); emit registering(); - client_->registerUser(user, pass, server, session); + http::client()->registerUser( + user, pass, server, session); }); } @@ -189,7 +189,7 @@ RegisterPage::onRegisterButtonClicked() QString password = password_input_->text(); QString server = server_input_->text(); - client_->registerUser(username, password, server); + http::client()->registerUser(username, password, server); emit registering(); } } diff --git a/src/RoomList.cc b/src/RoomList.cc index c0447f9..12fec31 100644 --- a/src/RoomList.cc +++ b/src/RoomList.cc @@ -29,11 +29,8 @@ #include "UserSettingsPage.h" #include "Utils.h" -RoomList::RoomList(QSharedPointer client, - QSharedPointer userSettings, - QWidget *parent) +RoomList::RoomList(QSharedPointer userSettings, QWidget *parent) : QWidget(parent) - , client_(client) , userSettings_{userSettings} { setStyleSheet("border: none;"); @@ -58,7 +55,7 @@ RoomList::RoomList(QSharedPointer client, scrollArea_->setWidget(scrollAreaContents_); topLayout_->addWidget(scrollArea_); - connect(client_.data(), + connect(http::client(), &MatrixClient::roomAvatarRetrieved, this, [this](const QString &room_id, @@ -104,7 +101,7 @@ RoomList::updateAvatar(const QString &room_id, const QString &url) savedImgData = cache_->image(url); if (savedImgData.isEmpty()) { - client_->fetchRoomAvatar(room_id, url); + http::client()->fetchRoomAvatar(room_id, url); } else { QPixmap img; img.loadFromData(savedImgData); @@ -317,7 +314,7 @@ RoomList::closeJoinRoomDialog(bool isJoining, QString roomAlias) joinRoomModal_->hide(); if (isJoining) - client_->joinRoom(roomAlias); + http::client()->joinRoom(roomAlias); } void diff --git a/src/timeline/TimelineItem.cc b/src/timeline/TimelineItem.cc index 5084b97..6f126d3 100644 --- a/src/timeline/TimelineItem.cc +++ b/src/timeline/TimelineItem.cc @@ -19,8 +19,10 @@ #include #include #include +#include #include "Avatar.h" +#include "ChatPage.h" #include "Config.h" #include "timeline/TimelineItem.h" @@ -63,7 +65,7 @@ TimelineItem::init() connect(redactMsg_, &QAction::triggered, this, [this]() { if (!event_id_.isEmpty()) - ChatPage::instance()->redactEvent(room_id_, event_id_); + http::client()->redactEvent(room_id_, event_id_); }); connect(markAsRead_, &QAction::triggered, this, [this]() { sendReadReceipt(); }); diff --git a/src/timeline/TimelineView.cc b/src/timeline/TimelineView.cc index 5b43367..94ecb30 100644 --- a/src/timeline/TimelineView.cc +++ b/src/timeline/TimelineView.cc @@ -84,26 +84,21 @@ DateSeparator::paintEvent(QPaintEvent *) } TimelineView::TimelineView(const mtx::responses::Timeline &timeline, - QSharedPointer client, const QString &room_id, QWidget *parent) : QWidget(parent) , room_id_{room_id} - , client_{client} { init(); addEvents(timeline); } -TimelineView::TimelineView(QSharedPointer client, - const QString &room_id, - QWidget *parent) +TimelineView::TimelineView(const QString &room_id, QWidget *parent) : QWidget(parent) , room_id_{room_id} - , client_{client} { init(); - client_->messages(room_id_, ""); + http::client()->messages(room_id_, ""); } void @@ -143,7 +138,7 @@ TimelineView::fetchHistory() return; isPaginationInProgress_ = true; - client_->messages(room_id_, prev_batch_token_); + http::client()->messages(room_id_, prev_batch_token_); paginationTimer_->start(5000); return; @@ -194,7 +189,7 @@ TimelineView::sliderMoved(int position) // FIXME: Maybe move this to TimelineViewManager to remove the // extra calls? - client_->messages(room_id_, prev_batch_token_); + http::client()->messages(room_id_, prev_batch_token_); } } @@ -430,7 +425,7 @@ TimelineView::init() paginationTimer_ = new QTimer(this); connect(paginationTimer_, &QTimer::timeout, this, &TimelineView::fetchHistory); - connect(client_.data(), + connect(http::client(), &MatrixClient::messagesRetrieved, this, &TimelineView::addBackwardsEvents); @@ -551,7 +546,7 @@ TimelineView::addUserMessage(mtx::events::MessageType ty, const QString &body) saveLastMessageInfo(local_user_, QDateTime::currentDateTime()); - int txn_id = client_->incrementTransactionId(); + int txn_id = http::client()->incrementTransactionId(); PendingMessage message(ty, txn_id, body, "", "", -1, "", view_item); handleNewUserMessage(message); } @@ -577,11 +572,12 @@ TimelineView::sendNextPendingMessage() case mtx::events::MessageType::Video: case mtx::events::MessageType::File: // FIXME: Improve the API - client_->sendRoomMessage( + http::client()->sendRoomMessage( m.ty, m.txn_id, room_id_, m.filename, m.mime, m.media_size, m.body); break; default: - client_->sendRoomMessage(m.ty, m.txn_id, room_id_, m.body, m.mime, m.media_size); + http::client()->sendRoomMessage( + m.ty, m.txn_id, room_id_, m.body, m.mime, m.media_size); break; } } @@ -675,7 +671,7 @@ TimelineView::readLastEvent() const const auto eventId = getLastEventId(); if (!eventId.isEmpty()) - client_->readEvent(room_id_, eventId); + http::client()->readEvent(room_id_, eventId); } QString diff --git a/src/timeline/TimelineViewManager.cc b/src/timeline/TimelineViewManager.cc index 28f4477..b7ce53a 100644 --- a/src/timeline/TimelineViewManager.cc +++ b/src/timeline/TimelineViewManager.cc @@ -31,21 +31,20 @@ #include "timeline/widgets/ImageItem.h" #include "timeline/widgets/VideoItem.h" -TimelineViewManager::TimelineViewManager(QSharedPointer client, QWidget *parent) +TimelineViewManager::TimelineViewManager(QWidget *parent) : QStackedWidget(parent) - , client_(client) { setStyleSheet("border: none;"); connect( - client_.data(), &MatrixClient::messageSent, this, &TimelineViewManager::messageSent); + http::client(), &MatrixClient::messageSent, this, &TimelineViewManager::messageSent); - connect(client_.data(), + connect(http::client(), &MatrixClient::messageSendFailed, this, &TimelineViewManager::messageSendFailed); - connect(client_.data(), + connect(http::client(), &MatrixClient::redactionCompleted, this, [this](const QString &room_id, const QString &event_id) { @@ -190,7 +189,7 @@ TimelineViewManager::addRoom(const mtx::responses::JoinedRoom &room, const QStri return; // Create a history view with the room events. - TimelineView *view = new TimelineView(room.timeline, client_, room_id); + TimelineView *view = new TimelineView(room.timeline, room_id); views_.emplace(room_id, QSharedPointer(view)); connect(view, @@ -209,7 +208,7 @@ TimelineViewManager::addRoom(const QString &room_id) return; // Create a history view without any events. - TimelineView *view = new TimelineView(client_, room_id); + TimelineView *view = new TimelineView(room_id); views_.emplace(room_id, QSharedPointer(view)); connect(view, diff --git a/src/timeline/widgets/AudioItem.cc b/src/timeline/widgets/AudioItem.cc index 79f944f..5a2bca9 100644 --- a/src/timeline/widgets/AudioItem.cc +++ b/src/timeline/widgets/AudioItem.cc @@ -23,6 +23,7 @@ #include #include +#include "MatrixClient.h" #include "Utils.h" #include "timeline/widgets/AudioItem.h" @@ -57,7 +58,7 @@ AudioItem::init() QString media_params = url_parts[1]; url_ = QString("%1/_matrix/media/r0/download/%2") - .arg(client_.data()->getHomeServer().toString(), media_params); + .arg(http::client()->getHomeServer().toString(), media_params); player_ = new QMediaPlayer; player_->setMedia(QUrl(url_)); @@ -73,29 +74,21 @@ AudioItem::init() }); } -AudioItem::AudioItem(QSharedPointer client, - const mtx::events::RoomEvent &event, - QWidget *parent) +AudioItem::AudioItem(const mtx::events::RoomEvent &event, QWidget *parent) : QWidget(parent) , url_{QUrl(QString::fromStdString(event.content.url))} , text_{QString::fromStdString(event.content.body)} , event_{event} - , client_{client} { readableFileSize_ = utils::humanReadableFileSize(event.content.info.size); init(); } -AudioItem::AudioItem(QSharedPointer client, - const QString &url, - const QString &filename, - uint64_t size, - QWidget *parent) +AudioItem::AudioItem(const QString &url, const QString &filename, uint64_t size, QWidget *parent) : QWidget(parent) , url_{url} , text_{filename} - , client_{client} { readableFileSize_ = utils::humanReadableFileSize(size); @@ -134,7 +127,7 @@ AudioItem::mousePressEvent(QMouseEvent *event) if (filenameToSave_.isEmpty()) return; - auto proxy = client_->downloadFile(url_); + auto proxy = http::client()->downloadFile(url_); connect(proxy.data(), &DownloadMediaProxy::fileDownloaded, this, diff --git a/src/timeline/widgets/FileItem.cc b/src/timeline/widgets/FileItem.cc index 7445eb0..7ff60d8 100644 --- a/src/timeline/widgets/FileItem.cc +++ b/src/timeline/widgets/FileItem.cc @@ -23,6 +23,7 @@ #include #include +#include "MatrixClient.h" #include "Utils.h" #include "timeline/widgets/FileItem.h" @@ -56,32 +57,24 @@ FileItem::init() QString media_params = url_parts[1]; url_ = QString("%1/_matrix/media/r0/download/%2") - .arg(client_.data()->getHomeServer().toString(), media_params); + .arg(http::client()->getHomeServer().toString(), media_params); } -FileItem::FileItem(QSharedPointer client, - const mtx::events::RoomEvent &event, - QWidget *parent) +FileItem::FileItem(const mtx::events::RoomEvent &event, QWidget *parent) : QWidget(parent) , url_{QString::fromStdString(event.content.url)} , text_{QString::fromStdString(event.content.body)} , event_{event} - , client_{client} { readableFileSize_ = utils::humanReadableFileSize(event.content.info.size); init(); } -FileItem::FileItem(QSharedPointer client, - const QString &url, - const QString &filename, - uint64_t size, - QWidget *parent) +FileItem::FileItem(const QString &url, const QString &filename, uint64_t size, QWidget *parent) : QWidget(parent) , url_{url} , text_{filename} - , client_{client} { readableFileSize_ = utils::humanReadableFileSize(size); @@ -120,7 +113,7 @@ FileItem::mousePressEvent(QMouseEvent *event) if (filenameToSave_.isEmpty()) return; - auto proxy = client_->downloadFile(url_); + auto proxy = http::client()->downloadFile(url_); connect(proxy.data(), &DownloadMediaProxy::fileDownloaded, this, diff --git a/src/timeline/widgets/ImageItem.cc b/src/timeline/widgets/ImageItem.cc index 03f9741..66cd31a 100644 --- a/src/timeline/widgets/ImageItem.cc +++ b/src/timeline/widgets/ImageItem.cc @@ -25,16 +25,14 @@ #include #include "Config.h" +#include "MatrixClient.h" #include "Utils.h" #include "dialogs/ImageOverlay.h" #include "timeline/widgets/ImageItem.h" -ImageItem::ImageItem(QSharedPointer client, - const mtx::events::RoomEvent &event, - QWidget *parent) +ImageItem::ImageItem(const mtx::events::RoomEvent &event, QWidget *parent) : QWidget(parent) , event_{event} - , client_{client} { setMouseTracking(true); setCursor(Qt::PointingHandCursor); @@ -52,9 +50,9 @@ ImageItem::ImageItem(QSharedPointer client, QString media_params = url_parts[1]; url_ = QString("%1/_matrix/media/r0/download/%2") - .arg(client_.data()->getHomeServer().toString(), media_params); + .arg(http::client()->getHomeServer().toString(), media_params); - auto proxy = client_.data()->downloadImage(url_); + auto proxy = http::client()->downloadImage(url_); connect(proxy.data(), &DownloadMediaProxy::imageDownloaded, @@ -65,15 +63,10 @@ ImageItem::ImageItem(QSharedPointer client, }); } -ImageItem::ImageItem(QSharedPointer client, - const QString &url, - const QString &filename, - uint64_t size, - QWidget *parent) +ImageItem::ImageItem(const QString &url, const QString &filename, uint64_t size, QWidget *parent) : QWidget(parent) , url_{url} , text_{filename} - , client_{client} { Q_UNUSED(size); @@ -90,9 +83,9 @@ ImageItem::ImageItem(QSharedPointer client, QString media_params = url_parts[1]; url_ = QString("%1/_matrix/media/r0/download/%2") - .arg(client_.data()->getHomeServer().toString(), media_params); + .arg(http::client()->getHomeServer().toString(), media_params); - auto proxy = client_.data()->downloadImage(url_); + auto proxy = http::client()->downloadImage(url_); connect(proxy.data(), &DownloadMediaProxy::imageDownloaded, @@ -238,7 +231,7 @@ ImageItem::saveAs() if (filename.isEmpty()) return; - auto proxy = client_->downloadFile(url_); + auto proxy = http::client()->downloadFile(url_); connect(proxy.data(), &DownloadMediaProxy::fileDownloaded, this, diff --git a/src/timeline/widgets/VideoItem.cc b/src/timeline/widgets/VideoItem.cc index 788db0e..f5bcfd6 100644 --- a/src/timeline/widgets/VideoItem.cc +++ b/src/timeline/widgets/VideoItem.cc @@ -20,6 +20,7 @@ #include #include "Config.h" +#include "MatrixClient.h" #include "Utils.h" #include "timeline/widgets/VideoItem.h" @@ -34,17 +35,14 @@ VideoItem::init() QString media_params = url_parts[1]; url_ = QString("%1/_matrix/media/r0/download/%2") - .arg(client_.data()->getHomeServer().toString(), media_params); + .arg(http::client()->getHomeServer().toString(), media_params); } -VideoItem::VideoItem(QSharedPointer client, - const mtx::events::RoomEvent &event, - QWidget *parent) +VideoItem::VideoItem(const mtx::events::RoomEvent &event, QWidget *parent) : QWidget(parent) , url_{QString::fromStdString(event.content.url)} , text_{QString::fromStdString(event.content.body)} , event_{event} - , client_{client} { readableFileSize_ = utils::humanReadableFileSize(event.content.info.size); @@ -65,15 +63,10 @@ VideoItem::VideoItem(QSharedPointer client, layout->addWidget(label_); } -VideoItem::VideoItem(QSharedPointer client, - const QString &url, - const QString &filename, - uint64_t size, - QWidget *parent) +VideoItem::VideoItem(const QString &url, const QString &filename, uint64_t size, QWidget *parent) : QWidget(parent) , url_{url} , text_{filename} - , client_{client} { readableFileSize_ = utils::humanReadableFileSize(size);