-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.cpp
128 lines (107 loc) · 3.58 KB
/
app.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include "app.h"
#include "feed.h"
#include "feedfetch.h"
#include "feedfolder.h"
#include "javascriptplugins.h"
#include <KSharedConfig>
#include <KWindowConfig>
#include <QDebug>
#include <QDomDocument>
#include <QFile>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QQuickWindow>
#include <QStandardPaths>
void importOpmlDocumentIn(FeedFolder *folder, QDomDocument &document);
void exportAsOpmlDocument(const FeedFolder *folder, QDomDocument &document);
App::App(QObject *parent)
: QObject(parent)
{
m_javaScriptPlugins = new JavaScriptPlugins();
m_sharingService = new SharingService();
m_rootFolder = new FeedFolder();
m_autosaveTimer.setInterval(1000 * 60 * 90); // 1.5h but I couldnt get chrono literals to work
m_autosaveTimer.start();
connect(&m_autosaveTimer, &QTimer::timeout, this, &App::save);
}
App::~App()
{
save();
m_javaScriptPlugins->deleteLater();
m_sharingService->deleteLater();
m_rootFolder->deleteLater();
}
void App::restoreWindowGeometry(QQuickWindow *window, const QString &group) const
{
KConfig dataResource(QStringLiteral("data"), KConfig::SimpleConfig, QStandardPaths::AppDataLocation);
KConfigGroup windowGroup(&dataResource, QStringLiteral("Window-") + group);
KWindowConfig::restoreWindowSize(window, windowGroup);
KWindowConfig::restoreWindowPosition(window, windowGroup);
}
void App::saveWindowGeometry(QQuickWindow *window, const QString &group) const
{
KConfig dataResource(QStringLiteral("data"), KConfig::SimpleConfig, QStandardPaths::AppDataLocation);
KConfigGroup windowGroup(&dataResource, QStringLiteral("Window-") + group);
KWindowConfig::saveWindowPosition(window, windowGroup);
KWindowConfig::saveWindowSize(window, windowGroup);
dataResource.sync();
}
QString App::storagePath()
{
return QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + QStringLiteral("/feeds.json");
}
QString App::backupStoragePath()
{
return storagePath() + QStringLiteral(".backup");
}
void App::load()
{
QFile file(storagePath());
if (file.open(QIODevice::ReadOnly)) {
QJsonDocument document = QJsonDocument::fromJson(file.readAll());
QJsonObject root = document.object();
m_rootFolder->loadFromJson(root);
QFile::copy(storagePath(), backupStoragePath());
} else
qDebug() << "App::load: failed to open" << storagePath();
}
void App::save()
{
QFile file(storagePath());
if (file.open(QIODevice::WriteOnly)) {
QJsonObject root;
m_rootFolder->triggerBeforeSave();
m_rootFolder->saveToJson(root);
if (root[QStringLiteral("items")].toArray().size() > 0)
file.write(QJsonDocument(root).toJson());
else
qDebug() << "App::save: not writing to disk, items array is empty";
} else
qDebug() << "App::save: failed to open" << storagePath();
}
void App::importOpml(const QUrl &filepath)
{
QFile file(filepath.path());
if (file.open(QIODevice::ReadOnly)) {
QDomDocument document;
QDomElement base;
document.setContent(file.readAll());
importOpmlDocumentIn(m_rootFolder, document);
}
}
void App::exportOpml(const QUrl &filepath)
{
QFile file(filepath.path());
if (file.open(QIODevice::WriteOnly)) {
QDomDocument document;
exportAsOpmlDocument(m_rootFolder, document);
file.write(QStringLiteral("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n").toUtf8());
file.write(document.toString().toUtf8());
}
}
void App::stopFetching()
{
FeedFetcher::interrupt();
}
#include "moc_app.cpp"