-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathLocalStorage_Service.cpp
More file actions
65 lines (53 loc) · 1.98 KB
/
LocalStorage_Service.cpp
File metadata and controls
65 lines (53 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include "LocalStorage_Service.h"
#include <qthread.h>
LocalStorage_Service* LocalStorage_Service::instance = nullptr;
QMutex LocalStorage_Service::mutex;
LocalStorage_Service* LocalStorage_Service::getInstance() {
if (!instance)
{
QMutexLocker locker(&mutex);
if (!instance)
{
instance = new LocalStorage_Service();
connect(instance, &close, instance, &safeExit);
}
}
return instance;
}
LocalStorage_Service::LocalStorage_Service(QObject* object_) : QObject(object_) {}
void LocalStorage_Service::saveAllMessages() {
if (!instance->message_storage.empty())
{
QMutexLocker locker(&mutex);
auto keys = instance->message_storage.keys();
for (const auto& key : keys)
{
QString current_time = QDateTime::currentDateTimeUtc().toString("yyyyMMdd_hhmm");
QString file_name = "rooms/" + key.toString() + "/" + current_time + ".json";
QDir().mkpath("rooms/" + key.toString());
QJsonArray array;
for (const auto& message : message_storage.value(key)) {
array.append(message->toJson());
}
if (!FileRepository::writeJsonArr(file_name, array)) {
PLOGE << "Error writing to file";
}
PLOGI << "Writing messages successfully";
instance->message_storage.remove(key);
}
}
else PLOGI << "message_storage is empty.";
}
void LocalStorage_Service::safeExit()
{
saveAllMessages();
instance->deleteLater();
PLOGI << "Local storage service safely closed";
}
void LocalStorage_Service::addMessages(DBEntity::DBMessage* message_, QUuid room_id_) {
if (!message_storage.contains(room_id_)) {
QList<QSharedPointer<DBEntity::DBMessage>> new_room_history;
message_storage.insert(room_id_, new_room_history);
}
message_storage.value(room_id_).append(QSharedPointer<DBEntity::DBMessage>(message_, &QObject::deleteLater));
}