Permalink
Browse files

Got basic participant refresh wokring.

  • Loading branch information...
1 parent 59273b1 commit 63bd4d0c4c4ea6a976942c3c16bae09b80486184 @klnusbaum committed Aug 28, 2012
View
@@ -139,6 +139,12 @@ DataStore::DataStore(
SLOT(refreshActivePlaylist()));
connect(
+ participantRefreshTimer,
+ SIGNAL(timeout()),
+ this,
+ SLOT(refreshParticipantList()));
+
+ connect(
serverConnection,
SIGNAL(libModError(const QString&, int, const QList<QNetworkReply::RawHeaderPair>&)),
this,
@@ -193,6 +199,13 @@ DataStore::DataStore(
this,
SLOT(onCurrentSongClearError(const QString&, int, const QList<QNetworkReply::RawHeaderPair>&)));
+
+ connect(
+ serverConnection,
+ SIGNAL(newParticipantList(const QVariantList&)),
+ this,
+ SLOT(onNewParticipantList(const QVariantList&)));
+
}
void DataStore::setupDB(){
@@ -234,10 +247,12 @@ void DataStore::setupDB(){
}
void DataStore::startPlaylistAutoRefresh(){
+ Logger::instance()->log("Starting playlist auto refresh");
activePlaylistRefreshTimer->start();
}
void DataStore::startParticipantsAutoRefresh(){
+ Logger::instance()->log("Starting particpants auto refresh");
participantRefreshTimer->start();
}
@@ -984,7 +999,7 @@ void DataStore::refreshActivePlaylist(){
}
void DataStore::refreshParticipantList(){
- //
+ serverConnection->getParticipantList();
}
@@ -1043,6 +1058,10 @@ void DataStore::onSetVolumeFailed(
}
}
+void DataStore::onNewParticipantList(const QVariantList& newParticipants){
+ emit newParticipantList(newParticipants);
+}
+
View
@@ -1119,6 +1119,11 @@ public slots:
void activePlaylistModified();
/**
+ * \brief Emitted when the participant list retrieved from server.
+ */
+ void newParticipantList(const QVariantList& newParticipants);
+
+ /**
* \brief Emitted when the current song is manually changed.
*
* @param newSong The song that should be set as the current song.
@@ -1667,6 +1672,13 @@ private slots:
int errorCode,
const QList<QNetworkReply::RawHeaderPair>& headers);
+ /**
+ * \brief Takes appropriate action when the particpants list is retrieved from the server.
+ *
+ * \param newParticipants The new list of participants.
+ */
+ void onNewParticipantList(const QVariantList& newParticipants);
+
//@}
View
@@ -167,6 +167,20 @@ QVariantMap JSONHelper::getActivePlaylistFromJSON(QNetworkReply *reply){
return activePlaylist;
}
+QVariantList JSONHelper::getParticipantListFromJSON(QNetworkReply *reply){
+ QByteArray responseData = reply->readAll();
+ QString responseString = QString::fromUtf8(responseData);
+ bool success;
+ QVariantList participantsList =
+ QtJson::Json::parse(responseString, success).toList();
+ if(!success){
+ std::cerr << "Error parsing json from a response to an get Participants List request" <<
+ std::endl <<
+ responseString.toStdString() << std::endl;
+ }
+ return participantsList;
+}
+
QByteArray JSONHelper::getJSONLibIds(const QSet<library_song_id_t>& libIds){
bool success;
QVariantList idList;
View
@@ -188,6 +188,14 @@ class JSONHelper{
static QVariantMap getActivePlaylistFromJSON(QNetworkReply *reply);
/**
+ * \brief Gets the list of participants from the JSON given in the server reply.
+ *
+ * \param reply The reply from the server.
+ * \return A QVariantMap representing the participants given in the server reply.
+ */
+ static QVariantList getParticipantListFromJSON(QNetworkReply *reply);
+
+ /**
* \brief Gets the auth data from a server authentication reply.
*
* \param reply The reply from the server.
View
@@ -17,17 +17,51 @@
* along with UDJ. If not, see <http://www.gnu.org/licenses/>.
*/
#include "ParticipantsModel.hpp"
+#include "DataStore.hpp"
+#include "Logger.hpp"
namespace UDJ{
-ParticipantsModel::ParticipantsModel(QObject *parent)
- :QStandardItemModel(parent)
+ParticipantsModel::ParticipantsModel(DataStore* dataStore, QObject *parent)
+ :QStandardItemModel(parent),
+ dataStore(dataStore)
{
+ setHeaders();
+ createConnections();
+}
+
+void ParticipantsModel::createConnections(){
+ connect(
+ dataStore,
+ SIGNAL(newParticipantList(const QVariantList&)),
+ this,
+ SLOT(onNewParticipantList(const QVariantList&)));
+}
+
+
+void ParticipantsModel::onNewParticipantList(const QVariantList& newParticipants){
+ Logger::instance()->log("Got new particpants list of length: " + QString::number(newParticipants.size()));
+ clear();
+ setHeaders();
+ QVariantMap participant;
+ for(int i=0; i<newParticipants.size(); ++i){
+ participant = newParticipants.at(i).toMap();
+ QStandardItem *newId = new QStandardItem(participant["id"].toString());
+ QStandardItem *newUsername = new QStandardItem(participant["username"].toString());
+ QStandardItem *newFirstName = new QStandardItem(participant["first_name"].toString());
+ QStandardItem *newLastName = new QStandardItem(participant["last_name"].toString());
+ QList<QStandardItem*> newRow;
+ newRow << newId << newUsername << newFirstName << newLastName;
+ appendRow(newRow);
+ }
+
+}
+
+void ParticipantsModel::setHeaders(){
QStringList headers;
headers << tr("Id") << tr("Username") << tr("First Name") << tr("Last Name");
setHorizontalHeaderLabels(headers);
}
-
} // end namespace
View
@@ -20,17 +20,23 @@
#define PARTICIPANTS_MODEL_HPP
#include <QStandardItemModel>
-class QStandardItem;
-
namespace UDJ{
+class DataStore;
class ParticipantsModel : public QStandardItemModel{
-
+Q_OBJECT
public:
- ParticipantsModel(QObject *parent=0);
+ ParticipantsModel(DataStore *dataStore, QObject *parent=0);
+
+private slots:
+ void onNewParticipantList(const QVariantList& newParticipants);
+private:
+ void createConnections();
+ void setHeaders();
+ DataStore *dataStore;
};
View
@@ -32,7 +32,7 @@ ParticipantsView::ParticipantsView(DataStore* dataStore, QWidget* parent):
{
setFocusPolicy(Qt::TabFocus);
setEditTriggers(QAbstractItemView::NoEditTriggers);
- participantsModel = new ParticipantsModel(this);
+ participantsModel = new ParticipantsModel(dataStore, this);
setModel(participantsModel);
setSelectionBehavior(QAbstractItemView::SelectRows);
setSelectionMode(QAbstractItemView::ContiguousSelection);
@@ -212,6 +212,13 @@ void UDJServerConnection::clearCurrentSong(){
/*QNetworkReply *reply =*/ netAccessManager->deleteResource(clearCurrentSongRequest);
}
+
+void UDJServerConnection::getParticipantList(){
+ QNetworkRequest getParticipantListRequest(getParticipantsUrl());
+ getParticipantListRequest.setRawHeader(getTicketHeaderName(), ticket_hash);
+ /*QNetworkReply *reply =*/ netAccessManager->get(getParticipantListRequest);
+}
+
void UDJServerConnection::recievedReply(QNetworkReply *reply){
if(reply->request().url().path() == getAuthUrl().path()){
handleAuthReply(reply);
@@ -253,6 +260,9 @@ void UDJServerConnection::recievedReply(QNetworkReply *reply){
else if(isPasswordRemoveReply(reply)){
handlePlayerPasswordRemoveReply(reply);
}
+ else if(reply->request().url().path() == getParticipantsUrl().path()){
+ handleParticipantsResponse(reply);
+ }
else{
Logger::instance()->log("Received unknown response");
Logger::instance()->log(reply->request().url().path());
@@ -298,6 +308,21 @@ void UDJServerConnection::handleRecievedClearCurrentSong(QNetworkReply *reply){
}
+void UDJServerConnection::handleParticipantsResponse(QNetworkReply *reply){
+ if(isResponseType(reply, 200)){
+ emit newParticipantList(JSONHelper::getParticipantListFromJSON(reply));
+ }
+ else{
+ QString responseData = QString(reply->readAll());
+ Logger::instance()->log("Participan get error " + responseData);
+ emit getParticipantsError(
+ responseData,
+ reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(),
+ reply->rawHeaderPairs());
+ }
+
+}
+
void UDJServerConnection::handleSetStateReply(QNetworkReply *reply){
if(isResponseType(reply, 200)){
emit playerStateSet(reply->property(getStatePropertyName()).toString());
@@ -498,6 +523,10 @@ QUrl UDJServerConnection::getPlayerPasswordUrl() const{
QString::number(playerId) + "/password");
}
+QUrl UDJServerConnection::getParticipantsUrl() const{
+ return QUrl(getServerUrlPath()+ "players/"+QString::number(playerId)+"/users");
+}
+
bool UDJServerConnection::isResponseType(QNetworkReply *reply, int code){
@@ -529,4 +558,6 @@ bool UDJServerConnection::isPasswordRemoveReply(const QNetworkReply *reply) cons
}
+
+
}//end namespace
@@ -206,6 +206,11 @@ public slots:
*/
void clearCurrentSong();
+ /**
+ * \brief Retrieves a list of active participants from the server.
+ */
+ void getParticipantList();
+
//@}
signals:
@@ -387,6 +392,8 @@ public slots:
/**
* \brief Emitted when a new version of the active playlist is retrieved from
* the server.
+ *
+ * @param newPlaylist The new playlist that was retreived from the server.
*/
void newActivePlaylist(const QVariantMap& newPlaylist);
@@ -403,6 +410,25 @@ public slots:
const QList<QNetworkReply::RawHeaderPair>& headers);
/**
+ * \brief Emitted when a new version of the participants list is retrieved from the server.
+ *
+ * @param newParticipants The new list of participants that was retrieved from the server.
+ */
+ void newParticipantList(const QVariantList& newParticipants);
+
+ /**
+ * \brief Emitted when there was an error getting the list of participants from the server.
+ *
+ * @param errMessage A message describing the error.
+ * @param errorCode The http status code that describes the error.
+ * @param headers The headers from the http response that indicated a failure.
+ */
+ void getParticipantsError(
+ const QString& errMessage,
+ int errorCode,
+ const QList<QNetworkReply::RawHeaderPair>& headers);
+
+ /**
* \brief Emitted when the current song that the player is playing is
* succesfully set on the server.
*/
@@ -586,18 +612,25 @@ private slots:
/**
* \brief Handle a response from the server regarding the setting of the player location.
*
- * @param reply Response from the server.
+ * @param reply response from the server.
*/
void handleLocationSetReply(QNetworkReply *reply);
/**
* \brief Handles a response from the server regarding the clearing of the current song.
*
- * @param reply The Reesponse from the server.
+ * @param reply The response from the server.
*/
void handleRecievedClearCurrentSong(QNetworkReply *reply);
/**
+ * \brief Handles a response from the server regarding getting a list of active participants.
+ *
+ * @param reply The response from the server.
+ */
+ void handleParticipantsResponse(QNetworkReply *reply);
+
+ /**
* \brief Prepares a network request that is going to include JSON.
*
* @param request Request to prepare.
@@ -640,6 +673,15 @@ private slots:
QUrl getCurrentSongUrl() const;
/**
+ * \brief Get the url to be used for retrieving the list of current participants.
+ *
+ * @return The url to be used for getting the current participants.
+ */
+ QUrl getParticipantsUrl() const;
+
+
+
+ /**
* \brief Gets the url that should be used for creating a player on the server.
*
* \return The url that sholud be used for creating a player on the server.

0 comments on commit 63bd4d0

Please sign in to comment.