Files
cantata/dynamic/dynamic.h
craig.p.drummond 531b479d88 Improvements to HTTP mode
- Show spinner when loading list
- Only mark entry as active if it is the current entry, and HTTP state is not IDLE
- Clear currentJob when response received
2012-09-09 10:12:01 +00:00

146 lines
4.2 KiB
C++

/*
* Cantata
*
* Copyright (c) 2011-2012 Craig Drummond <craig.p.drummond@gmail.com>
*
* ----
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef DYNAMIC_H
#define DYNAMIC_H
#include <QtCore/QAbstractItemModel>
#include <QtCore/QList>
#include <QtCore/QMap>
#include <QtCore/QString>
#include <QtCore/QStringList>
class QTimer;
class NetworkAccessManager;
class QNetworkReply;
class Dynamic : public QAbstractItemModel
{
Q_OBJECT
public:
typedef QMap<QString, QString> Rule;
struct Entry {
Entry(const QString &n=QString()) : name(n) { }
bool operator==(const Entry &o) const { return name==o.name; }
QString name;
QList<Rule> rules;
};
static Dynamic * self();
static const QString constRuleKey;
static const QString constArtistKey;
static const QString constAlbumArtistKey;
static const QString constAlbumKey;
static const QString constTitleKey;
static const QString constGenreKey;
static const QString constDateKey;
static const QString constExactKey;
static const QString constExcludeKey;
Dynamic();
bool isRemote() const { return !dynamicUrl.isEmpty(); }
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
int rowCount(const QModelIndex &parent = QModelIndex()) const;
int columnCount(const QModelIndex&) const { return 1; }
bool hasChildren(const QModelIndex &parent) const;
QModelIndex parent(const QModelIndex &index) const;
QModelIndex index(int row, int column, const QModelIndex &parent) const;
QVariant data(const QModelIndex &, int) const;
Qt::ItemFlags flags(const QModelIndex &index) const;
Entry entry(const QString &e);
bool exists(const QString &e) {
return entryList.end()!=find(e);
}
bool save(const Entry &e);
void del(const QString &name);
void start(const QString &name);
void stop();
void toggle(const QString &name);
bool isRunning();
QString current() const {
return currentEntry;
}
const QList<Entry> & entries() const {
return entryList;
}
void helperMessage(const QString &message) {
Q_UNUSED(message)
checkHelper();
}
Q_SIGNALS:
void running(bool status);
void error(const QString &str);
// These are for communicating with MPD object (which is in its own thread, so need to talk via signal/slots)
void clear();
// These are as the result of asynchronous HTTP calls
void saved(bool s);
void loadingList();
void loadedList();
public Q_SLOTS:
void refreshList();
private Q_SLOTS:
void checkHelper();
void checkRemoteHelper();
void jobFinished();
void dynamicUrlChanged(const QString &url);
private:
int getPid() const;
bool controlApp(bool isStart);
QList<Entry>::Iterator find(const QString &e);
NetworkAccessManager * network();
void sendCommand(const QString &cmd, const QStringList &args=QStringList());
void loadLocal();
void loadRemote();
void parseRemote(const QString &response);
void parseStatus(const QString &response);
void checkResponse(const QString &response);
void updateEntry(const Entry &e);
private:
QTimer *timer;
QList<Entry> entryList;
QString currentEntry;
// For remote dynamic servers...
int statusTime;
QString lastState;
QString dynamicUrl;
QNetworkReply *currentJob;
NetworkAccessManager *manager;
QString currentCommand;
QStringList currentArgs;
Entry currentSave;
};
#endif