~ubuntu-branches/ubuntu/saucy/clementine/saucy

« back to all changes in this revision

Viewing changes to src/internet/icecastbackend.cpp

  • Committer: Package Import Robot
  • Author(s): Thomas PIERSON
  • Date: 2012-01-01 20:43:39 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20120101204339-lsb6nndwhfy05sde
Tags: 1.0.1+dfsg-1
New upstream release. (Closes: #653926, #651611, #657391)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This file is part of Clementine.
 
2
   Copyright 2010, David Sansome <me@davidsansome.com>
 
3
 
 
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.
 
8
 
 
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.
 
13
 
 
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/>.
 
16
*/
 
17
 
 
18
#include "icecastbackend.h"
 
19
#include "core/database.h"
 
20
#include "core/scopedtransaction.h"
 
21
 
 
22
#include <QSqlQuery>
 
23
#include <QVariant>
 
24
 
 
25
const char* IcecastBackend::kTableName = "icecast_stations";
 
26
 
 
27
IcecastBackend::IcecastBackend(QObject* parent)
 
28
  : QObject(parent)
 
29
{
 
30
}
 
31
 
 
32
void IcecastBackend::Init(boost::shared_ptr<Database> db) {
 
33
  db_ = db;
 
34
}
 
35
 
 
36
QStringList IcecastBackend::GetGenresAlphabetical(const QString& filter) {
 
37
  QStringList ret;
 
38
  QMutexLocker l(db_->Mutex());
 
39
  QSqlDatabase db = db_->Connect();
 
40
 
 
41
  QString where = filter.isEmpty() ? "" : "WHERE name LIKE :filter";
 
42
 
 
43
  QString sql = QString("SELECT DISTINCT genre FROM %1 %2 ORDER BY genre")
 
44
      .arg(kTableName, where);
 
45
 
 
46
  QSqlQuery q(sql, db);
 
47
  if (!filter.isEmpty()) {
 
48
    q.bindValue(":filter", "%" + filter + "%");
 
49
  }
 
50
 
 
51
  q.exec();
 
52
  if (db_->CheckErrors(q)) return ret;
 
53
 
 
54
  while (q.next()) {
 
55
    ret << q.value(0).toString();
 
56
  }
 
57
  return ret;
 
58
}
 
59
 
 
60
QStringList IcecastBackend::GetGenresByPopularity(const QString& filter) {
 
61
  QStringList ret;
 
62
  QMutexLocker l(db_->Mutex());
 
63
  QSqlDatabase db = db_->Connect();
 
64
 
 
65
  QString where = filter.isEmpty() ? "" : "WHERE name LIKE :filter";
 
66
 
 
67
  QString sql = QString("SELECT genre, COUNT(*) AS count FROM %1 "
 
68
                        " %2"
 
69
                        " GROUP BY genre"
 
70
                        " ORDER BY count DESC").arg(kTableName, where);
 
71
  QSqlQuery q(sql, db);
 
72
  if (!filter.isEmpty()) {
 
73
    q.bindValue(":filter", "%" + filter + "%");
 
74
  }
 
75
 
 
76
  q.exec();
 
77
  if (db_->CheckErrors(q)) return ret;
 
78
 
 
79
  while (q.next()) {
 
80
    ret << q.value(0).toString();
 
81
  }
 
82
  return ret;
 
83
}
 
84
 
 
85
IcecastBackend::StationList IcecastBackend::GetStations(const QString& filter,
 
86
                                                        const QString& genre) {
 
87
  StationList ret;
 
88
  QMutexLocker l(db_->Mutex());
 
89
  QSqlDatabase db = db_->Connect();
 
90
 
 
91
  QStringList where_clauses;
 
92
  QStringList bound_items;
 
93
 
 
94
  if (!genre.isEmpty()) {
 
95
    where_clauses << "genre = :genre";
 
96
    bound_items << genre;
 
97
  }
 
98
  if (!filter.isEmpty()) {
 
99
    where_clauses << "name LIKE :filter";
 
100
    bound_items << "%" + filter + "%";
 
101
  }
 
102
 
 
103
  QString sql = QString("SELECT name, url, mime_type, bitrate, channels,"
 
104
                        "       samplerate, genre"
 
105
                        " FROM %1").arg(kTableName);
 
106
 
 
107
  if (!where_clauses.isEmpty()) {
 
108
    sql += " WHERE " + where_clauses.join(" AND ");
 
109
  }
 
110
  QSqlQuery q(sql, db);
 
111
  foreach (const QString& value, bound_items) {
 
112
    q.addBindValue(value);
 
113
  }
 
114
 
 
115
  q.exec();
 
116
  if (db_->CheckErrors(q)) return ret;
 
117
 
 
118
  while (q.next()) {
 
119
    Station station;
 
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();
 
127
    ret << station;
 
128
  }
 
129
  return ret;
 
130
}
 
131
 
 
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);
 
136
  q.exec();
 
137
  return !q.next();
 
138
}
 
139
 
 
140
void IcecastBackend::ClearAndAddStations(const StationList& stations) {
 
141
  {
 
142
    QMutexLocker l(db_->Mutex());
 
143
    QSqlDatabase db = db_->Connect();
 
144
    ScopedTransaction t(&db);
 
145
 
 
146
    // Remove all existing items
 
147
    QSqlQuery q(QString("DELETE FROM %1").arg(kTableName), db);
 
148
    q.exec();
 
149
    if (db_->CheckErrors(q)) return;
 
150
 
 
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);
 
156
 
 
157
    // Add these ones
 
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);
 
166
      q.exec();
 
167
      if (db_->CheckErrors(q)) return;
 
168
    }
 
169
 
 
170
    t.Commit();
 
171
  }
 
172
 
 
173
  emit DatabaseReset();
 
174
}
 
175
 
 
176
Song IcecastBackend::Station::ToSong() const {
 
177
  Song ret;
 
178
  ret.set_valid(true);
 
179
  ret.set_title(name);
 
180
  ret.set_url(url);
 
181
  ret.set_bitrate(bitrate);
 
182
  ret.set_samplerate(samplerate);
 
183
  ret.set_genre(genre);
 
184
  ret.set_filetype(Song::Type_Stream);
 
185
  return ret;
 
186
}