diff --git a/include/Cache.h b/include/Cache.h
new file mode 100644
index 00000000..dc9583ac
--- /dev/null
+++ b/include/Cache.h
@@ -0,0 +1,57 @@
+/*
+ * nheko Copyright (C) 2017 Konstantinos Sideris <siderisk@auth.gr>
+ *
+ * This program is free 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.
+ *
+ * This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#pragma once
+
+#include <lmdb++.h>
+
+#include "RoomState.h"
+
+class Cache
+{
+public:
+ Cache(const QString &userId);
+
+ void insertRoomState(const QString &roomid, const RoomState &state);
+ void setNextBatchToken(const QString &token);
+ bool isInitialized();
+
+ QString nextBatchToken();
+ QMap<QString, RoomState> states();
+
+ inline void unmount();
+ inline QString memberDbName(const QString &roomid);
+
+private:
+ lmdb::env env_;
+ lmdb::dbi stateDb_;
+ lmdb::dbi roomDb_;
+
+ bool isMounted_;
+
+ QString userId_;
+};
+
+inline void Cache::unmount()
+{
+ isMounted_ = false;
+}
+
+inline QString Cache::memberDbName(const QString &roomid)
+{
+ return QString("m.%1").arg(roomid);
+}
diff --git a/include/ChatPage.h b/include/ChatPage.h
index 74db6b15..88d4435e 100644
--- a/include/ChatPage.h
+++ b/include/ChatPage.h
@@ -21,6 +21,7 @@
#include <QTimer>
#include <QWidget>
+#include "Cache.h"
#include "MatrixClient.h"
#include "RoomList.h"
#include "RoomSettings.h"
@@ -43,6 +44,7 @@ public:
void bootstrap(QString userid, QString homeserver, QString token);
signals:
+ void contentLoaded();
void close();
void changeWindowTitle(const QString &msg);
void unreadMessages(int count);
@@ -62,6 +64,7 @@ private slots:
private:
void updateDisplayNames(const RoomState &state);
void updateRoomState(RoomState &room_state, const QJsonArray &events);
+ void loadStateFromCache();
QHBoxLayout *topLayout_;
Splitter *splitter;
@@ -97,4 +100,7 @@ private:
// Matrix Client API provider.
QSharedPointer<MatrixClient> client_;
+
+ // LMDB wrapper.
+ QSharedPointer<Cache> cache_;
};
diff --git a/include/RoomState.h b/include/RoomState.h
index 0389a6df..1d8ae429 100644
--- a/include/RoomState.h
+++ b/include/RoomState.h
@@ -17,6 +17,7 @@
#pragma once
+#include <QJsonDocument>
#include <QPixmap>
#include <QUrl>
@@ -45,6 +46,7 @@ public:
// e.g If the room is 1-on-1 name and avatar should be extracted from a user.
void resolveName();
void resolveAvatar();
+ void parse(const QJsonObject &object);
inline QUrl getAvatar() const;
inline QString getName() const;
@@ -53,6 +55,8 @@ public:
void removeLeaveMemberships();
void update(const RoomState &state);
+ QJsonObject serialize() const;
+
// The latest state events.
events::StateEvent<events::AliasesEventContent> aliases;
events::StateEvent<events::AvatarEventContent> avatar;
diff --git a/include/TimelineView.h b/include/TimelineView.h
index b50970b9..ea3e5fb3 100644
--- a/include/TimelineView.h
+++ b/include/TimelineView.h
@@ -63,6 +63,7 @@ class TimelineView : public QWidget
public:
TimelineView(const Timeline &timeline, QSharedPointer<MatrixClient> client, const QString &room_id, QWidget *parent = 0);
+ TimelineView(QSharedPointer<MatrixClient> client, const QString &room_id, QWidget *parent = 0);
TimelineItem *createTimelineItem(const events::MessageEvent<msgs::Image> &e, const QString &color, bool with_sender);
TimelineItem *createTimelineItem(const events::MessageEvent<msgs::Notice> &e, const QString &color, bool with_sender);
@@ -72,6 +73,7 @@ public:
int addEvents(const Timeline &timeline);
void addUserTextMessage(const QString &msg, int txn_id);
void updatePendingMessage(int txn_id, QString event_id);
+ void fetchHistory();
void scrollDown();
public slots:
@@ -90,6 +92,7 @@ private:
// Used to determine whether or not we should prefix a message with the sender's name.
bool isSenderRendered(const QString &user_id, TimelineDirection direction);
bool isPendingMessage(const events::MessageEvent<msgs::Text> &e, const QString &userid);
+ inline bool isDuplicate(const QString &event_id);
// Return nullptr if the event couldn't be parsed.
TimelineItem *parseMessageEvent(const QJsonObject &event, TimelineDirection direction);
@@ -121,6 +124,13 @@ private:
int oldPosition_;
int oldHeight_;
+ // The events currently rendered. Used for duplicate detection.
+ QMap<QString, bool> eventIds_;
QList<PendingMessage> pending_msgs_;
QSharedPointer<MatrixClient> client_;
};
+
+inline bool TimelineView::isDuplicate(const QString &event_id)
+{
+ return eventIds_.contains(event_id);
+}
diff --git a/include/TimelineViewManager.h b/include/TimelineViewManager.h
index dc2445e2..c5b37542 100644
--- a/include/TimelineViewManager.h
+++ b/include/TimelineViewManager.h
@@ -34,7 +34,10 @@ public:
TimelineViewManager(QSharedPointer<MatrixClient> client, QWidget *parent);
~TimelineViewManager();
+ // Initialize with timeline events.
void initialize(const Rooms &rooms);
+ // Empty initialization.
+ void initialize(const QList<QString> &rooms);
void sync(const Rooms &rooms);
void clearAll();
|