-
Notifications
You must be signed in to change notification settings - Fork 10
/
datastorage.cpp
80 lines (61 loc) · 2.24 KB
/
datastorage.cpp
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include "datastorage.hpp"
#include "info.hpp"
#include <TelegramQt/TelegramNamespace>
#include <QDir>
#include <QFile>
#include <QLoggingCategory>
#include <QTimer>
static const QString c_telegramStateFile = QLatin1String("telegram-state.bin");
MorseDataStorage::MorseDataStorage(QObject *parent) :
Telegram::Client::InMemoryDataStorage(parent)
{
}
void MorseDataStorage::setInfo(MorseInfo *info)
{
m_info = info;
}
void MorseDataStorage::scheduleSave()
{
if (!m_delayedSaveTimer) {
m_delayedSaveTimer = new QTimer(this);
m_delayedSaveTimer->setSingleShot(true);
m_delayedSaveTimer->setInterval(500);
connect(m_delayedSaveTimer, &QTimer::timeout, this, &MorseDataStorage::saveData);
}
// Do not restart the timer if it is already active to make sure that
// two or three messages per second do not completely prevent the client from the save.
if (!m_delayedSaveTimer->isActive()) {
m_delayedSaveTimer->start();
}
}
bool MorseDataStorage::saveData() const
{
const QByteArray data = saveState();
QDir dir;
dir.mkpath(m_info->accountDataDirectory());
QFile stateFile(m_info->accountDataDirectory() + QLatin1Char('/') + c_telegramStateFile);
// TODO: Save sent message ids map
if (!stateFile.open(QIODevice::WriteOnly)) {
qWarning() << Q_FUNC_INFO << "Unable to open state file" << stateFile.fileName();
}
if (stateFile.write(data) != data.size()) {
qWarning() << Q_FUNC_INFO << "Unable to save the session data to file"
<< "for account"
<< Telegram::Utils::maskPhoneNumber(m_info->accountIdentifier());
}
qDebug() << Q_FUNC_INFO << "State saved to file" << stateFile.fileName();
return true;
}
bool MorseDataStorage::loadData()
{
// TODO: Load sent message ids map
QFile stateFile(m_info->accountDataDirectory() + QLatin1Char('/') + c_telegramStateFile);
if (!stateFile.open(QIODevice::ReadOnly)) {
qDebug() << Q_FUNC_INFO << "Unable to open state file" << stateFile.fileName();
return false;
}
const QByteArray data = stateFile.readAll();
qDebug() << Q_FUNC_INFO << m_info->accountIdentifier() << "(" << data.size() << "bytes)";
loadState(data);
return true;
}