Don't use shared pointers for cache

This commit is contained in:
Konstantinos Sideris 2018-05-08 20:30:09 +03:00
parent 4c4ea557b3
commit ebed87ea57
17 changed files with 86 additions and 96 deletions

View File

@ -17,27 +17,18 @@
#pragma once
#include <QHash>
#include <QImage>
#include <QSharedPointer>
#include <functional>
class TimelineItem;
class Cache;
class AvatarProvider : public QObject
{
Q_OBJECT
public:
static void init(QSharedPointer<Cache> 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,
const QString &userId,
QObject *receiver,
std::function<void(QImage)> callback);
private:
static QSharedPointer<Cache> cache_;
};

View File

@ -435,3 +435,11 @@ private:
QString localUserId_;
QString cacheDirectory_;
};
namespace cache {
void
init(const QString &userId, QObject *parent);
Cache *
client();
}

View File

@ -69,7 +69,6 @@ public:
static ChatPage *instance() { return instance_; }
QSharedPointer<UserSettings> userSettings() { return userSettings_; }
QSharedPointer<Cache> cache() { return cache_; }
void deleteConfigs();
signals:
@ -183,9 +182,6 @@ private:
// Global user settings.
QSharedPointer<UserSettings> userSettings_;
// LMDB wrapper.
QSharedPointer<Cache> cache_;
};
template<class Collection>

View File

@ -22,7 +22,6 @@
#include <QVBoxLayout>
#include <QWidget>
#include "Cache.h"
#include "SuggestionsPopup.hpp"
#include "TextField.h"
@ -50,7 +49,7 @@ class QuickSwitcher : public QWidget
Q_OBJECT
public:
QuickSwitcher(QSharedPointer<Cache> cache, QWidget *parent = nullptr);
QuickSwitcher(QWidget *parent = nullptr);
signals:
void closing();
@ -77,6 +76,4 @@ private:
//! Autocomplete popup box with the room suggestions.
SuggestionsPopup popup_;
//! Cache client for room quering.
QSharedPointer<Cache> cache_;
};

View File

@ -30,7 +30,6 @@ class OverlayModal;
class RoomInfoListItem;
class Sync;
class UserSettings;
class Cache;
struct DescInfo;
struct RoomInfo;
@ -41,7 +40,6 @@ class RoomList : public QWidget
public:
RoomList(QSharedPointer<UserSettings> userSettings, QWidget *parent = 0);
void setCache(QSharedPointer<Cache> cache) { cache_ = cache; }
void initialize(const QMap<QString, RoomInfo> &info);
void sync(const std::map<QString, RoomInfo> &info);
@ -102,7 +100,6 @@ private:
//! Which rooms to include in the room list.
std::vector<QString> roomFilter_;
QSharedPointer<Cache> cache_;
QSharedPointer<UserSettings> userSettings_;
bool isSortPending_ = false;

View File

@ -36,8 +36,6 @@
#include "emoji/PickButton.h"
class Cache;
namespace dialogs {
class PreviewUploadOverlay;
}
@ -131,7 +129,6 @@ public:
QColor borderColor() const { return borderColor_; }
void setBorderColor(QColor &color) { borderColor_ = color; }
void setCache(QSharedPointer<Cache> cache) { cache_ = cache; }
public slots:
void openFileSelection();
@ -172,7 +169,5 @@ private:
FlatButton *sendMessageBtn_;
emoji::PickButton *emojiBtn_;
QSharedPointer<Cache> cache_;
QColor borderColor_;
};

View File

@ -4,7 +4,6 @@
#include <QListWidget>
class Avatar;
class Cache;
class FlatButton;
class QHBoxLayout;
class QLabel;
@ -38,7 +37,7 @@ class MemberList : public QFrame
{
Q_OBJECT
public:
MemberList(const QString &room_id, QSharedPointer<Cache> cache, QWidget *parent = nullptr);
MemberList(const QString &room_id, QWidget *parent = nullptr);
public slots:
void addUsers(const std::vector<RoomMember> &users);
@ -57,7 +56,6 @@ private:
QString room_id_;
QLabel *topLabel_;
QListWidget *list_;
QSharedPointer<Cache> cache_;
FlatButton *moreBtn_;
};
} // dialogs

View File

@ -59,9 +59,7 @@ class RoomSettings : public QFrame
{
Q_OBJECT
public:
RoomSettings(const QString &room_id,
QSharedPointer<Cache> cache,
QWidget *parent = nullptr);
RoomSettings(const QString &room_id, QWidget *parent = nullptr);
signals:
void closing();
@ -74,8 +72,6 @@ private:
void setAvatar(const QImage &img) { avatarImg_ = img; }
QSharedPointer<Cache> cache_;
// Button section
FlatButton *saveBtn_;
FlatButton *cancelBtn_;

View File

@ -22,8 +22,6 @@
#include "Cache.h"
#include "MatrixClient.h"
QSharedPointer<Cache> AvatarProvider::cache_;
void
AvatarProvider::resolve(const QString &room_id,
const QString &user_id,
@ -33,13 +31,13 @@ AvatarProvider::resolve(const QString &room_id,
const auto key = QString("%1 %2").arg(room_id).arg(user_id);
const auto avatarUrl = Cache::avatarUrl(room_id, user_id);
if (!Cache::AvatarUrls.contains(key) || cache_.isNull())
if (!Cache::AvatarUrls.contains(key) || !cache::client())
return;
if (avatarUrl.isEmpty())
return;
auto data = cache_->image(avatarUrl);
auto data = cache::client()->image(avatarUrl);
if (!data.isNull()) {
callback(QImage::fromData(data));
return;
@ -61,7 +59,7 @@ AvatarProvider::resolve(const QString &room_id,
buffer.open(QIODevice::WriteOnly);
img.save(&buffer, "PNG");
cache_->saveImage(avatarUrl, data);
cache::client()->saveImage(avatarUrl, data);
});
callback(img);
});

View File

@ -53,6 +53,25 @@ static constexpr const char *NOTIFICATIONS_DB = "sent_notifications";
using CachedReceipts = std::multimap<uint64_t, std::string, std::greater<uint64_t>>;
using Receipts = std::map<std::string, std::map<std::string, uint64_t>>;
namespace {
Cache *instance_ = nullptr;
}
namespace cache {
void
init(const QString &user_id, QObject *parent)
{
if (!instance_)
instance_ = new Cache(user_id, parent);
}
Cache *
client()
{
return instance_;
}
}
Cache::Cache(const QString &userId, QObject *parent)
: QObject{parent}
, env_{nullptr}

View File

@ -323,7 +323,7 @@ ChatPage::ChatPage(QSharedPointer<UserSettings> userSettings, QWidget *parent)
// We remove any invites with the same room_id.
try {
cache_->removeInvite(room_id.toStdString());
cache::client()->removeInvite(room_id.toStdString());
} catch (const lmdb::error &e) {
emit showNotification(QString("Failed to remove invite: %1")
.arg(QString::fromStdString(e.what())));
@ -416,7 +416,7 @@ ChatPage::ChatPage(QSharedPointer<UserSettings> userSettings, QWidget *parent)
[this](const std::vector<std::string> &rooms) { view_manager_->initialize(rooms); });
connect(this, &ChatPage::syncUI, this, [this](const mtx::responses::Rooms &rooms) {
try {
room_list_->cleanupInvites(cache_->invites());
room_list_->cleanupInvites(cache::client()->invites());
} catch (const lmdb::error &e) {
qWarning() << "failed to retrieve invites" << e.what();
}
@ -484,7 +484,7 @@ ChatPage::deleteConfigs()
settings.remove("");
settings.endGroup();
cache_->deleteData();
cache::client()->deleteData();
http::client()->reset();
}
@ -497,28 +497,24 @@ ChatPage::bootstrap(QString userid, QString homeserver, QString token)
http::client()->getOwnProfile();
http::client()->getOwnCommunities();
cache_ = QSharedPointer<Cache>(new Cache(userid));
room_list_->setCache(cache_);
text_input_->setCache(cache_);
AvatarProvider::init(cache_);
cache::init(userid, this);
try {
cache_->setup();
cache::client()->setup();
if (!cache_->isFormatValid()) {
cache_->deleteData();
cache_->setup();
cache_->setCurrentFormat();
if (!cache::client()->isFormatValid()) {
cache::client()->deleteData();
cache::client()->setup();
cache::client()->setCurrentFormat();
}
if (cache_->isInitialized()) {
if (cache::client()->isInitialized()) {
loadStateFromCache();
return;
}
} catch (const lmdb::error &e) {
qCritical() << "Cache failure" << e.what();
cache_->deleteData();
cache::client()->deleteData();
qInfo() << "Falling back to initial sync ...";
}
@ -534,16 +530,16 @@ ChatPage::syncCompleted(const mtx::responses::Sync &response)
QtConcurrent::run([this, res = std::move(response)]() {
try {
cache_->saveState(res);
cache::client()->saveState(res);
emit syncUI(res.rooms);
emit syncRoomlist(cache_->roomUpdates(res));
emit syncRoomlist(cache::client()->roomUpdates(res));
} catch (const lmdb::error &e) {
std::cout << "save cache error:" << e.what() << '\n';
// TODO: retry sync.
return;
}
emit continueSync(cache_->nextBatchToken());
emit continueSync(cache::client()->nextBatchToken());
});
}
@ -556,16 +552,16 @@ ChatPage::initialSyncCompleted(const mtx::responses::Sync &response)
QtConcurrent::run([this, res = std::move(response)]() {
try {
cache_->saveState(res);
cache::client()->saveState(res);
emit initializeViews(std::move(res.rooms));
emit initializeRoomList(cache_->roomInfo());
emit initializeRoomList(cache::client()->roomInfo());
} catch (const lmdb::error &e) {
qWarning() << "cache error:" << QString::fromStdString(e.what());
emit retryInitialSync();
return;
}
emit continueSync(cache_->nextBatchToken());
emit continueSync(cache::client()->nextBatchToken());
emit contentLoaded();
});
}
@ -591,8 +587,8 @@ ChatPage::updateOwnProfileInfo(const QUrl &avatar_url, const QString &display_na
if (!avatar_url.isValid())
return;
if (!cache_.isNull()) {
auto data = cache_->image(avatar_url.toString());
if (cache::client()) {
auto data = cache::client()->image(avatar_url.toString());
if (!data.isNull()) {
user_info_widget_->setAvatar(QImage::fromData(data));
return;
@ -635,7 +631,7 @@ ChatPage::changeTopRoomInfo(const QString &room_id)
}
try {
auto room_info = cache_->getRoomInfo({room_id.toStdString()});
auto room_info = cache::client()->getRoomInfo({room_id.toStdString()});
if (room_info.find(room_id) == room_info.end())
return;
@ -646,7 +642,7 @@ ChatPage::changeTopRoomInfo(const QString &room_id)
top_bar_->updateRoomName(name);
top_bar_->updateRoomTopic(QString::fromStdString(room_info[room_id].topic));
auto img = cache_->getRoomAvatar(room_id);
auto img = cache::client()->getRoomAvatar(room_id);
if (img.isNull())
top_bar_->updateRoomAvatarFromName(name);
@ -679,10 +675,10 @@ ChatPage::loadStateFromCache()
QtConcurrent::run([this]() {
try {
cache_->populateMembers();
cache::client()->populateMembers();
emit initializeEmptyViews(cache_->joinedRooms());
emit initializeRoomList(cache_->roomInfo());
emit initializeEmptyViews(cache::client()->joinedRooms());
emit initializeRoomList(cache::client()->roomInfo());
} catch (const lmdb::error &e) {
std::cout << "load cache error:" << e.what() << '\n';
// TODO Clear cache and restart.
@ -690,7 +686,7 @@ ChatPage::loadStateFromCache()
}
// Start receiving events.
emit continueSync(cache_->nextBatchToken());
emit continueSync(cache::client()->nextBatchToken());
// Check periodically if the timelines have been loaded.
emit startConsesusTimer();
@ -702,7 +698,7 @@ ChatPage::showQuickSwitcher()
{
if (quickSwitcher_.isNull()) {
quickSwitcher_ = QSharedPointer<QuickSwitcher>(
new QuickSwitcher(cache_, this),
new QuickSwitcher(this),
[](QuickSwitcher *switcher) { switcher->deleteLater(); });
connect(quickSwitcher_.data(),
@ -731,8 +727,8 @@ void
ChatPage::removeRoom(const QString &room_id)
{
try {
cache_->removeRoom(room_id);
cache_->removeInvite(room_id.toStdString());
cache::client()->removeRoom(room_id);
cache::client()->removeInvite(room_id.toStdString());
} catch (const lmdb::error &e) {
qCritical() << "The cache couldn't be updated: " << e.what();
// TODO: Notify the user.
@ -800,7 +796,7 @@ ChatPage::showReadReceipts(const QString &event_id)
receiptsModal_->setColor(QColor(30, 30, 30, 170));
}
receiptsDialog_->addUsers(cache_->readReceipts(event_id, current_room_));
receiptsDialog_->addUsers(cache::client()->readReceipts(event_id, current_room_));
receiptsModal_->show();
}
@ -858,23 +854,24 @@ ChatPage::sendDesktopNotifications(const mtx::responses::Notifications &res)
try {
if (item.read) {
cache_->removeReadNotification(event_id);
cache::client()->removeReadNotification(event_id);
continue;
}
if (!cache_->isNotificationSent(event_id)) {
if (!cache::client()->isNotificationSent(event_id)) {
const auto room_id = QString::fromStdString(item.room_id);
const auto user_id = utils::event_sender(item.event);
// We should only sent one notification per event.
cache_->markSentNotification(event_id);
cache::client()->markSentNotification(event_id);
// Don't send a notification when the current room is opened.
if (isRoomActive(room_id))
continue;
NotificationsManager::postNotification(
QString::fromStdString(cache_->singleRoomInfo(item.room_id).name),
QString::fromStdString(
cache::client()->singleRoomInfo(item.room_id).name),
Cache::displayName(room_id, user_id),
utils::event_body(item.event));
}

View File

@ -276,8 +276,8 @@ MainWindow::openRoomSettings(const QString &room_id)
{
const auto roomToSearch = room_id.isEmpty() ? chat_page_->currentRoom() : "";
roomSettingsDialog_ = QSharedPointer<dialogs::RoomSettings>(
new dialogs::RoomSettings(roomToSearch, chat_page_->cache(), this));
roomSettingsDialog_ =
QSharedPointer<dialogs::RoomSettings>(new dialogs::RoomSettings(roomToSearch, this));
connect(roomSettingsDialog_.data(), &dialogs::RoomSettings::closing, this, [this]() {
roomSettingsModal_->hide();
@ -294,8 +294,8 @@ MainWindow::openMemberListDialog(const QString &room_id)
{
const auto roomToSearch = room_id.isEmpty() ? chat_page_->currentRoom() : "";
memberListDialog_ = QSharedPointer<dialogs::MemberList>(
new dialogs::MemberList(roomToSearch, chat_page_->cache(), this));
memberListDialog_ =
QSharedPointer<dialogs::MemberList>(new dialogs::MemberList(roomToSearch, this));
memberListModal_ =
QSharedPointer<OverlayModal>(new OverlayModal(this, memberListDialog_.data()));

View File

@ -56,9 +56,8 @@ RoomSearchInput::hideEvent(QHideEvent *event)
TextField::hideEvent(event);
}
QuickSwitcher::QuickSwitcher(QSharedPointer<Cache> cache, QWidget *parent)
QuickSwitcher::QuickSwitcher(QWidget *parent)
: QWidget(parent)
, cache_{cache}
{
qRegisterMetaType<std::vector<RoomSearchResult>>();
setMaximumWidth(450);
@ -93,7 +92,8 @@ QuickSwitcher::QuickSwitcher(QSharedPointer<Cache> cache, QWidget *parent)
QtConcurrent::run([this, query = query.toLower()]() {
try {
emit queryResults(cache_->searchRooms(query.toStdString()));
emit queryResults(
cache::client()->searchRooms(query.toStdString()));
} catch (const lmdb::error &e) {
qWarning() << "room search failed:" << e.what();
}

View File

@ -62,8 +62,8 @@ RoomList::RoomList(QSharedPointer<UserSettings> userSettings, QWidget *parent)
const QPixmap &img,
const QString &url,
const QByteArray &data) {
if (!cache_.isNull())
cache_->saveImage(url, data);
if (cache::client())
cache::client()->saveImage(url, data);
updateRoomAvatar(room_id, img);
});
@ -97,8 +97,8 @@ RoomList::updateAvatar(const QString &room_id, const QString &url)
QByteArray savedImgData;
if (!cache_.isNull())
savedImgData = cache_->image(url);
if (cache::client())
savedImgData = cache::client()->image(url);
if (savedImgData.isEmpty()) {
http::client()->fetchRoomAvatar(room_id, url);

View File

@ -453,12 +453,12 @@ TextInputWidget::TextInputWidget(QWidget *parent)
input_->setFixedHeight(textInputHeight);
});
connect(input_, &FilteredTextEdit::showSuggestions, this, [this](const QString &q) {
if (q.isEmpty() || cache_.isNull())
if (q.isEmpty() || !cache::client())
return;
QtConcurrent::run([this, q = q.toLower().toStdString()]() {
try {
emit input_->resultsRetrieved(cache_->searchUsers(
emit input_->resultsRetrieved(cache::client()->searchUsers(
ChatPage::instance()->currentRoom().toStdString(), q));
} catch (const lmdb::error &e) {
std::cout << e.what() << '\n';

View File

@ -56,10 +56,9 @@ MemberItem::MemberItem(const RoomMember &member, QWidget *parent)
topLayout_->addLayout(textLayout_, 1);
}
MemberList::MemberList(const QString &room_id, QSharedPointer<Cache> cache, QWidget *parent)
MemberList::MemberList(const QString &room_id, QWidget *parent)
: QFrame(parent)
, room_id_{room_id}
, cache_{cache}
{
setMaximumSize(420, 380);
setAttribute(Qt::WA_DeleteOnClose, true);
@ -99,11 +98,11 @@ MemberList::MemberList(const QString &room_id, QSharedPointer<Cache> cache, QWid
const size_t numMembers = list_->count() - 1;
if (numMembers > 0)
addUsers(cache_->getMembers(room_id_.toStdString(), numMembers));
addUsers(cache::client()->getMembers(room_id_.toStdString(), numMembers));
});
try {
addUsers(cache_->getMembers(room_id_.toStdString()));
addUsers(cache::client()->getMembers(room_id_.toStdString()));
} catch (const lmdb::error &e) {
qCritical() << e.what();
}

View File

@ -15,18 +15,17 @@
using namespace dialogs;
RoomSettings::RoomSettings(const QString &room_id, QSharedPointer<Cache> cache, QWidget *parent)
RoomSettings::RoomSettings(const QString &room_id, QWidget *parent)
: QFrame(parent)
, cache_{cache}
, room_id_{std::move(room_id)}
{
setMaximumWidth(385);
try {
auto res = cache_->getRoomInfo({room_id_.toStdString()});
auto res = cache::client()->getRoomInfo({room_id_.toStdString()});
info_ = res[room_id_];
setAvatar(QImage::fromData(cache_->image(info_.avatar_url)));
setAvatar(QImage::fromData(cache::client()->image(info_.avatar_url)));
} catch (const lmdb::error &e) {
qWarning() << "failed to retrieve room info from cache" << room_id;
}