/* * Copyright 2008, 2011 Jason A. Donenfeld */ #include "Credentials.h" #include "Song.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include Credentials::Credentials(QString host, QString listingPath, QString songPath, QWidget *parent) : QDialog(parent), m_host(host), m_port(80), m_listingPath(listingPath), m_songPath(songPath), m_downloadProgressBar(0), m_statusBar(0), m_networkAccess(0), m_reply(0) { QFormLayout *form = new QFormLayout; m_errorLabel = new QLabel; m_errorLabel->hide(); form->addRow(m_errorLabel); m_usernameEdit = new QLineEdit; form->addRow(tr("Username: "), m_usernameEdit); m_passwordEdit = new QLineEdit; m_passwordEdit->setEchoMode(QLineEdit::Password); form->addRow(tr("Password: "), m_passwordEdit); QPushButton *accept = new QPushButton(tr("Login")); accept->setDefault(true); connect(accept, SIGNAL(clicked()), this, SLOT(login())); form->addRow(accept); QGroupBox *frame = new QGroupBox("Enter Credentials"); frame->setLayout(form); QHBoxLayout *hbox = new QHBoxLayout; hbox->addStretch(1); hbox->addWidget(frame); hbox->addStretch(1); QVBoxLayout *vbox = new QVBoxLayout; vbox->addStretch(1); vbox->addLayout(hbox); vbox->addStretch(1); setLayout(vbox); setMinimumSize(sizeHint()); } QUrl Credentials::songUrl(QString hash) const { return QUrl( QString("http://%3:%4/%5&username=%1&password=%2") .arg(m_usernameEdit->text()) .arg(m_passwordEdit->text()) .arg(m_host) .arg(QString::number(m_port)) .arg(m_songPath.arg(hash)) ); } void Credentials::login() { m_errorLabel->hide(); setDisabled(true); if(m_downloadProgressBar) m_downloadProgressBar->show(); if(m_statusBar) m_statusBar->show(); if(!m_networkAccess) { m_networkAccess = new QNetworkAccessManager(this); } m_reply = m_networkAccess->get(QNetworkRequest(QUrl( QString("http://%3:%4/%5&username=%1&password=%2") .arg(m_usernameEdit->text()) .arg(m_passwordEdit->text()) .arg(m_host) .arg(QString::number(m_port)) .arg(m_listingPath) ))); connect(m_reply, SIGNAL(finished()), this, SLOT(downloadFinished())); connect(m_reply, SIGNAL(downloadProgress(qint64, qint64)), this, SLOT(downloadProgress(qint64, qint64))); } void Credentials::downloadProgress(qint64 done, qint64 total) { if(m_downloadProgressBar) { if(total == -1) { m_downloadProgressBar->setMaximum(m_reply->rawHeader("Uncompressed-Length").toInt()); } else { m_downloadProgressBar->setMaximum(total); } m_downloadProgressBar->setValue(done); } } void Credentials::downloadFinished() { if(m_downloadProgressBar) { m_downloadProgressBar->hide(); m_downloadProgressBar->setValue(0); } if(m_reply->error()) { databaseDownloadFailed(); return; } QDomDocument doc; if(!doc.setContent(m_reply)) { databaseDownloadFailed(); return; } QDomElement songs = doc.documentElement(); QList songList; QDomNode songNode = songs.firstChild(); while(!songNode.isNull()) { QDomElement songElement = songNode.toElement(); if(!songElement.isNull() && songElement.tagName() == "song") { Song *song = new Song; QDomNode tagNode = songElement.firstChild(); while(!tagNode.isNull()) { QDomElement tagElement = tagNode.toElement(); if(!tagElement.isNull()) { if(tagElement.tagName() == "sha1") { song->setSha1(tagElement.text()); } else if(tagElement.tagName() == "artist") { song->setArtist(tagElement.text()); } else if(tagElement.tagName() == "album") { song->setAlbum(tagElement.text()); } else if(tagElement.tagName() == "title") { song->setTitle(tagElement.text()); } else if(tagElement.tagName() == "track") { song->setTrack(tagElement.text().toInt()); } else if(tagElement.tagName() == "format") { song->setFormat(tagElement.text()); } } tagNode = tagNode.nextSibling(); } songList.append(song); } songNode = songNode.nextSibling(); } if(!songList.isEmpty()) { hide(); emit receivedList(songList); } else databaseDownloadFailed(); m_reply->deleteLater(); } void Credentials::databaseDownloadFailed() { m_reply->deleteLater(); QString errorMessage = m_reply->errorString(); int index = errorMessage.indexOf("server replied: "); if(index != -1) errorMessage = errorMessage.mid(index + 16); m_errorLabel->setText(QString("
%1
").arg(errorMessage)); m_errorLabel->show(); setDisabled(false); if(m_statusBar) m_statusBar->hide(); }