forked from isaevi/PodcastLoader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeedManager.cpp
188 lines (164 loc) · 5.08 KB
/
feedManager.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include <QDomDocument>
#include <QFile>
#include <QDebug>
#include "feedmanager.h"
#include "feeddata.h"
const QString dirAttr = "dir";
const QString urlAttr = "url";
const QString prefixAttr = "prefix";
const QString titleAttr = "title";
const QString feedNode = "Feed";
const QString guidNode = "Guid";
const QString feedsConfiguration = "Configuration";
const QString configName = "config.xml";
const QString tempoparyConfigName = "config_new.xml";
FeedData* FeedManager::feedAt(const int index)
{
Q_ASSERT(index >=0 && index < _feeds.size());
return _feeds[index];
}
void FeedManager::load()
{
QFile file(configName);
if(!file.open(QFile::ReadOnly | QFile::Text) || !file.exists())
{
qDebug() << "Can't open configuration.";
return;
}
QDomDocument document;
if (!document.setContent(file.readAll()))
{
qDebug() << "Can't read configuration for loading.";
return ;
}
QDomElement element = document.documentElement().firstChildElement(feedNode);
QString value;
while ( !element.isNull() )
{
// do all you need to do with <name> element
QString title = element.attribute(titleAttr);
QString dir = element.attribute(dirAttr);
QString url = element.attribute(urlAttr);
QString prefix = element.attribute(prefixAttr);
auto feed = addFeed(title, url, dir, prefix);
QDomNode node = element.firstChild();
while(!node.isNull())
{
if(node.hasChildNodes())
{
value = node.firstChild().nodeValue();
if(!value.isEmpty())
feed->addProcessedGuid(value);
}
node = node.nextSibling();
}
element = element.nextSiblingElement(feedNode);
}
file.close();
}
void FeedManager::save()
{
QFile file(tempoparyConfigName);
if(!file.open(QIODevice::WriteOnly))
{
qDebug() << "Couldn't open configuration for writing.";
return;
}
const int Indent = 4;
QDomDocument doc;
QDomElement root = doc.createElement(feedsConfiguration);
doc.appendChild(root);
for(auto it = _feeds.begin(); it != _feeds.end(); ++it )
{
if((*it)->isAggregateStub())
continue;
QDomElement node = doc.createElement(feedNode);
node.setAttribute(titleAttr, (*it)->title());
node.setAttribute(urlAttr, (*it)->url().toString());
node.setAttribute(dirAttr, (*it)->dir());
node.setAttribute(prefixAttr, (*it)->prefix());
const QSet<QString> guids = (*it)->getProcessedGuids();
for(auto guid : guids)
{
QDomElement list = doc.createElement(guidNode);
QDomText text = doc.createTextNode(guid);
list.appendChild(text);
node.appendChild(list);
}
root.appendChild(node);
}
QDomProcessingInstruction pi = doc.createProcessingInstruction("xml", "version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\" ");
doc.insertBefore(pi, QDomNode());
QTextStream out(&file);
doc.save(out, Indent);
QFile oldConfig(configName);
if(!oldConfig.remove())
{
qDebug() << "Couldn't remove old configuration.";
return;
}
if(!file.rename(configName))
qDebug() << "Couldn't rename configuration.";
}
FeedManager::FeedManager(QObject *parent) : QObject(parent)
{
_allFeedsStub = new FeedDataAggregate(this);
_feeds.append(_allFeedsStub);
}
FeedData* FeedManager::addFeed(QString feedTitle, QString feedUrl, QString feedDir, QString feedPrefix)
{
FeedData* feed = new FeedData(feedTitle, feedUrl, feedDir, feedPrefix, this);
_feeds.append(feed);
_allFeedsStub->addFeed(feed);
emit feedsChanged();
return feed;
}
void FeedManager::addStubFeed()
{
FeedData* feed = new FeedData();
_feeds.append(feed);
emit feedsChanged();
}
void FeedManager::resetFeedAt(const int index)
{
FeedData* feed = feedAt(index);
if(feed->empty())
{
_feeds.removeAt(index);
//delete feed;
feed->deleteLater();
}
emit feedsChanged();
}
void FeedManager::removeAt(const int index)
{
FeedData* feed = feedAt(index);
_allFeedsStub->removeFeed(feed);
_feeds.removeAt(index);
feed->deleteLater();
emit feedsChanged();
}
QQmlListProperty<FeedData> FeedManager::feeds()
{
return QQmlListProperty<FeedData>(this, 0, FeedManager::add, FeedManager::count, FeedManager::at, nullptr);
}
void FeedManager::add(QQmlListProperty<FeedData> *list, FeedData *feed)
{
FeedManager *config = qobject_cast<FeedManager *>(list->object);
if (config)
config->addFeed("", feed->url().toString(), feed->dir(), feed->prefix());
}
int FeedManager::count(QQmlListProperty<FeedData> *list)
{
FeedManager *config = qobject_cast<FeedManager *>(list->object);
if (config)
return config->feedCount();
return 0;
}
FeedData* FeedManager::at(QQmlListProperty<FeedData> *list, int index)
{
FeedManager *config = qobject_cast<FeedManager *>(list->object);
if (config)
return config->feedAt(index);
return nullptr;
}