/
podcaststore.cpp
104 lines (82 loc) · 2.83 KB
/
podcaststore.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
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>
#include <QTextStream>
#include "podcaststore.h"
#include "episode.h"
bool PodcastStore::instanceFlag = false;
PodcastStore* PodcastStore::instance = NULL;
QSqlDatabase PodcastStore::db;
PodcastStore::PodcastStore()
{
bool ret;
// Find SQLite driver
db = QSqlDatabase::addDatabase("QSQLITE");
#ifdef Q_OS_LINUX
// NOTE: We have to store database file into user home folder in Linux
QString path(QDir::home().path());
path.append(QDir::separator()).append("hicast.db.sqlite");
path = QDir::toNativeSeparators(path);
db.setDatabaseName(path);
#else
// NOTE: File exists in the application private folder, in Symbian Qt implementation
db.setDatabaseName("hicast.db.sqlite");
#endif
ret = db.open();
qWarning("Database open returns: %d", ret);
if (!ret) {
QMessageBox::critical(0, "Cannot open database",
"Unable to establish a database connection.\n"
"This example needs SQLite support. Please read "
"the Qt SQL driver documentation for information how "
"to build it.\n\n"
"Click Cancel to exit.", QMessageBox::Cancel);
}
QStringList dbTables = db.tables();
if (dbTables.isEmpty())
{
if (db.isOpen())
{
QSqlQuery query;
ret = query.exec("create table podcasts ("
"podcastname varchar(255), "
"episodename varchar(255), "
"status integer, "
"size integer, "
"duration integer, "
"playposition integer)"
);
qWarning("Database table create returns: %d", ret);
}
}
}
bool PodcastStore::syncEpisodeWithDB(Episode* e)
{
QSqlQuery query;
QString cmd;
bool ret;
QTextStream(&cmd) << "SELECT * FROM podcasts WHERE podcastname='" << e->podcastName() << "' AND " <<
" episodename='" << e->name() << "'";
ret = query.exec(cmd);
if(!ret) return false;
if (query.next())
{
// this episode alrady exists in the database
return true;
} else
{
// need to create new record for the episode
query.clear();
e->setStatus(Episode::ENotDownloaded);
cmd.clear();
QTextStream(&cmd) << "INSERT INTO podcasts VALUES ('" << e->podcastName() << "', '" <<
e->name() << "', " << e->status() << ", " << e->size() << ", " <<
e->duration() << ", " << e->playPosition() << ")";
if(!query.exec(cmd)) return false;
}
return true;
}
void PodcastStore::close()
{
db.close();
}