1
/* This file is part of Clementine.
2
Copyright 2010, David Sansome <me@davidsansome.com>
4
Clementine is free software: you can redistribute it and/or modify
5
it under the terms of the GNU General Public License as published by
6
the Free Software Foundation, either version 3 of the License, or
7
(at your option) any later version.
9
Clementine is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
GNU General Public License for more details.
14
You should have received a copy of the GNU General Public License
15
along with Clementine. If not, see <http://www.gnu.org/licenses/>.
18
#include "icecastbackend.h"
19
#include "core/database.h"
20
#include "core/scopedtransaction.h"
25
const char* IcecastBackend::kTableName = "icecast_stations";
27
IcecastBackend::IcecastBackend(QObject* parent)
32
void IcecastBackend::Init(boost::shared_ptr<Database> db) {
36
QStringList IcecastBackend::GetGenresAlphabetical(const QString& filter) {
38
QMutexLocker l(db_->Mutex());
39
QSqlDatabase db = db_->Connect();
41
QString where = filter.isEmpty() ? "" : "WHERE name LIKE :filter";
43
QString sql = QString("SELECT DISTINCT genre FROM %1 %2 ORDER BY genre")
44
.arg(kTableName, where);
47
if (!filter.isEmpty()) {
48
q.bindValue(":filter", "%" + filter + "%");
52
if (db_->CheckErrors(q)) return ret;
55
ret << q.value(0).toString();
60
QStringList IcecastBackend::GetGenresByPopularity(const QString& filter) {
62
QMutexLocker l(db_->Mutex());
63
QSqlDatabase db = db_->Connect();
65
QString where = filter.isEmpty() ? "" : "WHERE name LIKE :filter";
67
QString sql = QString("SELECT genre, COUNT(*) AS count FROM %1 "
70
" ORDER BY count DESC").arg(kTableName, where);
72
if (!filter.isEmpty()) {
73
q.bindValue(":filter", "%" + filter + "%");
77
if (db_->CheckErrors(q)) return ret;
80
ret << q.value(0).toString();
85
IcecastBackend::StationList IcecastBackend::GetStations(const QString& filter,
86
const QString& genre) {
88
QMutexLocker l(db_->Mutex());
89
QSqlDatabase db = db_->Connect();
91
QStringList where_clauses;
92
QStringList bound_items;
94
if (!genre.isEmpty()) {
95
where_clauses << "genre = :genre";
98
if (!filter.isEmpty()) {
99
where_clauses << "name LIKE :filter";
100
bound_items << "%" + filter + "%";
103
QString sql = QString("SELECT name, url, mime_type, bitrate, channels,"
105
" FROM %1").arg(kTableName);
107
if (!where_clauses.isEmpty()) {
108
sql += " WHERE " + where_clauses.join(" AND ");
110
QSqlQuery q(sql, db);
111
foreach (const QString& value, bound_items) {
112
q.addBindValue(value);
116
if (db_->CheckErrors(q)) return ret;
120
station.name = q.value(0).toString();
121
station.url = q.value(1).toString();
122
station.mime_type = q.value(2).toString();
123
station.bitrate = q.value(3).toInt();
124
station.channels = q.value(4).toInt();
125
station.samplerate = q.value(5).toInt();
126
station.genre = q.value(6).toString();
132
bool IcecastBackend::IsEmpty() {
133
QMutexLocker l(db_->Mutex());
134
QSqlDatabase db = db_->Connect();
135
QSqlQuery q(QString("SELECT ROWID FROM %1 LIMIT 1").arg(kTableName), db);
140
void IcecastBackend::ClearAndAddStations(const StationList& stations) {
142
QMutexLocker l(db_->Mutex());
143
QSqlDatabase db = db_->Connect();
144
ScopedTransaction t(&db);
146
// Remove all existing items
147
QSqlQuery q(QString("DELETE FROM %1").arg(kTableName), db);
149
if (db_->CheckErrors(q)) return;
151
q = QSqlQuery(QString("INSERT INTO %1 (name, url, mime_type, bitrate,"
152
" channels, samplerate, genre)"
153
" VALUES (:name, :url, :mime_type, :bitrate,"
154
" :channels, :samplerate, :genre)")
155
.arg(kTableName), db);
158
foreach (const Station& station, stations) {
159
q.bindValue(":name", station.name);
160
q.bindValue(":url", station.url);
161
q.bindValue(":mime_type", station.mime_type);
162
q.bindValue(":bitrate", station.bitrate);
163
q.bindValue(":channels", station.channels);
164
q.bindValue(":samplerate", station.samplerate);
165
q.bindValue(":genre", station.genre);
167
if (db_->CheckErrors(q)) return;
173
emit DatabaseReset();
176
Song IcecastBackend::Station::ToSong() const {
181
ret.set_bitrate(bitrate);
182
ret.set_samplerate(samplerate);
183
ret.set_genre(genre);
184
ret.set_filetype(Song::Type_Stream);