From 529a3b36e5e851673304300f57f89db96128744f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20=C3=81ngel=20San=20Mart=C3=ADn?= Date: Mon, 6 Oct 2014 17:38:11 +0200 Subject: [PATCH] Added comic vine API key input dialog, YACReader's api key will not longer be used. The user will be responsible for getting his/her own API key. --- .../comic_vine/api_key_dialog.cpp | 67 +++++++++++++++++++ YACReaderLibrary/comic_vine/api_key_dialog.h | 31 +++++++++ YACReaderLibrary/comic_vine/comic_vine.pri | 6 +- .../comic_vine/comic_vine_client.cpp | 19 ++++-- .../comic_vine/comic_vine_client.h | 5 ++ .../comic_vine/comic_vine_dialog.cpp | 2 +- .../comic_vine/model/response_parser.cpp | 32 +++++++-- .../comic_vine/model/response_parser.h | 3 + YACReaderLibrary/library_window.cpp | 30 ++++++--- YACReaderLibrary/options_dialog.cpp | 17 +++++ YACReaderLibrary/options_dialog.h | 3 + common/yacreader_global.h | 2 + 12 files changed, 195 insertions(+), 22 deletions(-) create mode 100644 YACReaderLibrary/comic_vine/api_key_dialog.cpp create mode 100644 YACReaderLibrary/comic_vine/api_key_dialog.h diff --git a/YACReaderLibrary/comic_vine/api_key_dialog.cpp b/YACReaderLibrary/comic_vine/api_key_dialog.cpp new file mode 100644 index 00000000..5ab0c354 --- /dev/null +++ b/YACReaderLibrary/comic_vine/api_key_dialog.cpp @@ -0,0 +1,67 @@ +#include "api_key_dialog.h" + +#include +#include +#include +#include +#include +#include + +#include "yacreader_global.h" + +ApiKeyDialog::ApiKeyDialog(QWidget *parent) : + QDialog(parent) +{ + QVBoxLayout * layout = new QVBoxLayout; + QHBoxLayout * buttonsLayout = new QHBoxLayout; + + settings = new QSettings(YACReader::getSettingsPath()+"/YACReaderLibrary.ini",QSettings::IniFormat); //TODO unificar la creación del fichero de config con el servidor + settings->beginGroup("ComicVine"); + + QLabel * info = new QLabel(tr("Before you can connect to Comic Vine, you need your own API key. Please, get one free here")); + info->setWordWrap(true); + edit = new QLineEdit(); + edit->setPlaceholderText(tr("Paste here your Comic Vine API key")); + connect(edit,SIGNAL(textChanged(QString)),this,SLOT(enableAccept(QString))); + + acceptButton = new QPushButton(tr("Accept")); + acceptButton->setDisabled(true); + connect(acceptButton,SIGNAL(clicked()),this,SLOT(saveApiKey())); + + cancelButton = new QPushButton(tr("Cancel")); + connect(cancelButton,SIGNAL(clicked()),this,SLOT(reject())); + + layout->addWidget(info); + layout->addWidget(edit); + layout->addStretch(); + + buttonsLayout->addStretch(); + buttonsLayout->addWidget(acceptButton); + buttonsLayout->addWidget(cancelButton); + + layout->addLayout(buttonsLayout); + + setLayout(layout); + + resize(400,150); + + if(settings->contains(COMIC_VINE_API_KEY)) + edit->setText(settings->value(COMIC_VINE_API_KEY).toString()); +} + +ApiKeyDialog::~ApiKeyDialog() +{ + delete settings; +} + +void ApiKeyDialog::enableAccept(const QString &text) +{ + //TODO key validation + acceptButton->setEnabled(!text.isEmpty()); +} + +void ApiKeyDialog::saveApiKey() +{ + settings->setValue(COMIC_VINE_API_KEY,edit->text()); + accept(); +} diff --git a/YACReaderLibrary/comic_vine/api_key_dialog.h b/YACReaderLibrary/comic_vine/api_key_dialog.h new file mode 100644 index 00000000..13ff5750 --- /dev/null +++ b/YACReaderLibrary/comic_vine/api_key_dialog.h @@ -0,0 +1,31 @@ +#ifndef API_KEY_DIALOG_H +#define API_KEY_DIALOG_H + +#include + +class QPushButton; +class QLineEdit; +class QSettings; + +class ApiKeyDialog : public QDialog +{ + Q_OBJECT +public: + explicit ApiKeyDialog(QWidget *parent = 0); + ~ApiKeyDialog(); +signals: + +public slots: + +protected slots: + void enableAccept(const QString & text); + void saveApiKey(); + +protected: + QPushButton * acceptButton; + QPushButton * cancelButton; + QLineEdit * edit; + QSettings * settings; +}; + +#endif // API_KEY_DIALOG_H diff --git a/YACReaderLibrary/comic_vine/comic_vine.pri b/YACReaderLibrary/comic_vine/comic_vine.pri index 823e536a..c76c0acc 100644 --- a/YACReaderLibrary/comic_vine/comic_vine.pri +++ b/YACReaderLibrary/comic_vine/comic_vine.pri @@ -19,7 +19,8 @@ HEADERS += \ comic_vine/model/volume_comics_model.h \ comic_vine/scraper_scroll_label.h \ comic_vine/scraper_results_paginator.h \ - comic_vine/scraper_selector.h + comic_vine/scraper_selector.h \ + comic_vine/api_key_dialog.h SOURCES += \ comic_vine/comic_vine_dialog.cpp \ @@ -41,4 +42,5 @@ SOURCES += \ comic_vine/model/volume_comics_model.cpp \ comic_vine/scraper_scroll_label.cpp \ comic_vine/scraper_results_paginator.cpp \ - comic_vine/scraper_selector.cpp + comic_vine/scraper_selector.cpp \ + comic_vine/api_key_dialog.cpp diff --git a/YACReaderLibrary/comic_vine/comic_vine_client.cpp b/YACReaderLibrary/comic_vine/comic_vine_client.cpp index de515f3a..cb36d3ca 100644 --- a/YACReaderLibrary/comic_vine/comic_vine_client.cpp +++ b/YACReaderLibrary/comic_vine/comic_vine_client.cpp @@ -2,7 +2,8 @@ //this is the API key used by YACReader to access Comic Vine //please, do not use it in your own software, get one for free at Comic Vine -static const QString CV_API_KEY = "46680bebb358f1de690a5a365e15d325f9649f91"; +static const QString CV_API_KEY = "%CV_API_KEY%"; //get from settings +static const QString CV_API_KEY_DEFAULT = "46680bebb358f1de690a5a365e15d325f9649f91"; static const QString CV_WEB_ADDRESS = "http://www.comicvine.com/api"; @@ -43,13 +44,19 @@ static const QString CV_COVER_URL = CV_WEB_ADDRESS + "/issue/4000-%1/?api_key=" ComicVineClient::ComicVineClient(QObject *parent) : QObject(parent) { + settings = new QSettings(YACReader::getSettingsPath()+"/YACReaderLibrary.ini",QSettings::IniFormat); //TODO unificar la creación del fichero de config con el servidor + settings->beginGroup("ComicVine"); +} +ComicVineClient::~ComicVineClient() +{ + delete settings; } //CV_SEARCH void ComicVineClient::search(const QString & query, int page) { - HttpWorker * search = new HttpWorker(CV_SEARCH.arg(query).arg(page)); + HttpWorker * search = new HttpWorker(QString(CV_SEARCH).replace(CV_API_KEY,settings->value(COMIC_VINE_API_KEY,CV_API_KEY_DEFAULT).toString()).arg(query).arg(page)); connect(search,SIGNAL(dataReady(const QByteArray &)),this,SLOT(proccessVolumesSearchData(const QByteArray &))); connect(search,SIGNAL(timeout()),this,SIGNAL(timeOut())); connect(search,SIGNAL(finished()),search,SLOT(deleteLater())); @@ -87,7 +94,7 @@ void ComicVineClient::proccessComicDetailData(const QByteArray &data) //CV_SERIES_DETAIL void ComicVineClient::getSeriesDetail(const QString & id) { - HttpWorker * search = new HttpWorker(CV_SERIES_DETAIL.arg(id)); + HttpWorker * search = new HttpWorker(QString(CV_SERIES_DETAIL).replace(CV_API_KEY,settings->value(COMIC_VINE_API_KEY,CV_API_KEY_DEFAULT).toString()).arg(id)); connect(search,SIGNAL(dataReady(const QByteArray &)),this,SLOT(proccessSeriesDetailData(const QByteArray &))); connect(search,SIGNAL(timeout()),this,SIGNAL(timeOut())); connect(search,SIGNAL(finished()),search,SLOT(deleteLater())); @@ -106,7 +113,7 @@ void ComicVineClient::getSeriesCover(const QString & url) //CV_COMIC_IDS void ComicVineClient::getVolumeComicsInfo(const QString & idVolume, int page) { - HttpWorker * search = new HttpWorker(CV_COMICS_INFO.arg(idVolume).arg((page-1)*100)); //page on works for search, using offset instead + HttpWorker * search = new HttpWorker(QString(CV_COMICS_INFO).replace(CV_API_KEY,settings->value(COMIC_VINE_API_KEY,CV_API_KEY_DEFAULT).toString()).arg(idVolume).arg((page-1)*100)); //page on works for search, using offset instead connect(search,SIGNAL(dataReady(const QByteArray &)),this,SLOT(processVolumeComicsInfo(const QByteArray &))); connect(search,SIGNAL(timeout()),this,SIGNAL(timeOut())); //TODO connect(search,SIGNAL(finished()),search,SLOT(deleteLater())); @@ -122,7 +129,7 @@ void ComicVineClient::getComicId(const QString & id, int comicNumber) //CV_COMIC_DETAIL QByteArray ComicVineClient::getComicDetail(const QString & id, bool & outError, bool & outTimeout) { - HttpWorker * search = new HttpWorker(CV_COMIC_DETAIL.arg(id)); + HttpWorker * search = new HttpWorker(QString(CV_COMIC_DETAIL).replace(CV_API_KEY,settings->value(COMIC_VINE_API_KEY,CV_API_KEY_DEFAULT).toString()).arg(id)); //connect(search,SIGNAL(dataReady(const QByteArray &)),this,SLOT(proccessComicDetailData(const QByteArray &))); //connect(search,SIGNAL(timeout()),this,SIGNAL(timeOut())); @@ -140,7 +147,7 @@ QByteArray ComicVineClient::getComicDetail(const QString & id, bool & outError, //CV_COMIC_DETAIL void ComicVineClient::getComicDetailAsync(const QString & id) { - HttpWorker * search = new HttpWorker(CV_COMIC_DETAIL.arg(id)); + HttpWorker * search = new HttpWorker(QString(CV_COMIC_DETAIL).replace(CV_API_KEY,settings->value(COMIC_VINE_API_KEY,CV_API_KEY_DEFAULT).toString()).arg(id)); connect(search,SIGNAL(dataReady(const QByteArray &)),this,SLOT(proccessComicDetailData(const QByteArray &))); connect(search,SIGNAL(timeout()),this,SIGNAL(timeOut())); diff --git a/YACReaderLibrary/comic_vine/comic_vine_client.h b/YACReaderLibrary/comic_vine/comic_vine_client.h index 6e1bc579..f5fbc44c 100644 --- a/YACReaderLibrary/comic_vine/comic_vine_client.h +++ b/YACReaderLibrary/comic_vine/comic_vine_client.h @@ -4,12 +4,14 @@ #include "http_worker.h" #include +#include class ComicVineClient : public QObject { Q_OBJECT public: explicit ComicVineClient(QObject *parent = 0); + ~ComicVineClient(); signals: void searchResult(QString); @@ -37,5 +39,8 @@ protected slots: void processVolumeComicsInfo(const QByteArray & data); void proccessComicDetailData(const QByteArray & data); +protected: + QSettings * settings; + }; #endif // COMIC_VINE_CLIENT_H diff --git a/YACReaderLibrary/comic_vine/comic_vine_dialog.cpp b/YACReaderLibrary/comic_vine/comic_vine_dialog.cpp index 77e8311e..fcfea60b 100644 --- a/YACReaderLibrary/comic_vine/comic_vine_dialog.cpp +++ b/YACReaderLibrary/comic_vine/comic_vine_dialog.cpp @@ -259,7 +259,7 @@ void ComicVineDialog::debugClientResults(const QString & string) //QMessageBox::information(0,"Result", QString("Number of results : %1").arg(p.getNumResults())); if(p.responseError()) { - QMessageBox::critical(0,tr("Error connecting to ComicVine"), tr("unknown error")); + QMessageBox::critical(0,tr("Error connecting to ComicVine"), p.errorDescription()); goBack(); } else diff --git a/YACReaderLibrary/comic_vine/model/response_parser.cpp b/YACReaderLibrary/comic_vine/model/response_parser.cpp index 1901def6..eb212107 100644 --- a/YACReaderLibrary/comic_vine/model/response_parser.cpp +++ b/YACReaderLibrary/comic_vine/model/response_parser.cpp @@ -4,13 +4,18 @@ #include ResponseParser::ResponseParser(QObject *parent) : - QObject(parent),error(false),numResults(-1),currentPage(-1),totalPages(-1) + QObject(parent),error(false),numResults(-1),currentPage(-1),totalPages(-1),errorTxt("None") { } bool ResponseParser::responseError() { - return error; + return error; +} + +QString ResponseParser::errorDescription() +{ + return errorTxt; } qint32 ResponseParser::getNumResults() @@ -25,7 +30,19 @@ qint32 ResponseParser::getCurrentPage() qint32 ResponseParser::getTotalPages() { - return totalPages; + return totalPages; +} + +bool ResponseParser::isError(qint32 error) +{ + switch(error) + { + case 100: + return true; + + default: + return false; + } } void ResponseParser::loadJSONResponse(const QString &response) @@ -34,10 +51,15 @@ void ResponseParser::loadJSONResponse(const QString &response) QScriptValue sc; sc = engine.evaluate("(" + response + ")"); - if (!sc.property("error").isValid() && sc.property("error").toString() != "OK") + errorTxt = "None"; + + if (!sc.property("status_code").isValid() || isError(sc.property("status_code").toInt32())) { error = true; - qDebug("Error detected"); + if(sc.property("error").isValid()) + errorTxt = sc.property("error").toString(); + else + errorTxt = "Unknown error"; } else { diff --git a/YACReaderLibrary/comic_vine/model/response_parser.h b/YACReaderLibrary/comic_vine/model/response_parser.h index 9d325edf..10014ecb 100644 --- a/YACReaderLibrary/comic_vine/model/response_parser.h +++ b/YACReaderLibrary/comic_vine/model/response_parser.h @@ -9,9 +9,11 @@ class ResponseParser : public QObject public: explicit ResponseParser(QObject *parent = 0); bool responseError(); + QString errorDescription(); qint32 getNumResults(); qint32 getCurrentPage(); qint32 getTotalPages(); + bool isError(qint32 error); signals: public slots: @@ -19,6 +21,7 @@ public slots: protected: bool error; + QString errorTxt; qint32 numResults; qint32 currentPage; qint32 totalPages; diff --git a/YACReaderLibrary/library_window.cpp b/YACReaderLibrary/library_window.cpp index 62a828fd..e8e6b702 100644 --- a/YACReaderLibrary/library_window.cpp +++ b/YACReaderLibrary/library_window.cpp @@ -58,6 +58,7 @@ #include "yacreader_treeview.h" #include "comic_vine_dialog.h" +#include "api_key_dialog.h" //#include "yacreader_social_dialog.h" #include "classic_comics_view.h" @@ -1650,17 +1651,30 @@ void LibraryWindow::showProperties() void LibraryWindow::showComicVineScraper() { - QModelIndexList indexList = getSelectedComics(); + QSettings s(YACReader::getSettingsPath()+"/YACReaderLibrary.ini",QSettings::IniFormat); //TODO unificar la creación del fichero de config con el servidor + s.beginGroup("ComicVine"); - QList comics = comicsModel->getComics(indexList); - ComicDB c = comics[0]; - _comicIdEdited = c.id;//static_cast(indexList[0].internalPointer())->data(4).toULongLong(); + if(!s.contains(COMIC_VINE_API_KEY)) + { + ApiKeyDialog d; + d.exec(); + } - comicVineDialog->databasePath = foldersModel->getDatabase(); - comicVineDialog->basePath = currentPath(); - comicVineDialog->setComics(comics); + //check if the api key was inserted + if(s.contains(COMIC_VINE_API_KEY)) + { + QModelIndexList indexList = getSelectedComics(); - comicVineDialog->show(); + QList comics = comicsModel->getComics(indexList); + ComicDB c = comics[0]; + _comicIdEdited = c.id;//static_cast(indexList[0].internalPointer())->data(4).toULongLong(); + + comicVineDialog->databasePath = foldersModel->getDatabase(); + comicVineDialog->basePath = currentPath(); + comicVineDialog->setComics(comics); + + comicVineDialog->show(); + } } void LibraryWindow::setRemoveError() diff --git a/YACReaderLibrary/options_dialog.cpp b/YACReaderLibrary/options_dialog.cpp index 98794977..f0165e31 100644 --- a/YACReaderLibrary/options_dialog.cpp +++ b/YACReaderLibrary/options_dialog.cpp @@ -3,6 +3,7 @@ #include "yacreader_flow_gl.h" #include "yacreader_flow_config_widget.h" #include "yacreader_gl_flow_config_widget.h" +#include "api_key_dialog.h" #include #include @@ -42,12 +43,22 @@ OptionsDialog::OptionsDialog(QWidget * parent) sw->hide(); + QVBoxLayout * apiKeyLayout = new QVBoxLayout(); + QPushButton * apiKeyButton = new QPushButton(tr("Edit Comic Vine API key")); + apiKeyLayout->addWidget(apiKeyButton); + + QGroupBox * apiKeyBox = new QGroupBox(tr("Comic Vine API key")); + apiKeyBox->setLayout(apiKeyLayout); + + connect(apiKeyButton,SIGNAL(clicked()),this,SLOT(editApiKey())); + QWidget * comicFlowW = new QWidget; comicFlowW->setLayout(flowLayout); QWidget * generalW = new QWidget; generalW->setLayout(generalLayout); generalLayout->addWidget(shortcutsBox); + generalLayout->addWidget(apiKeyBox); generalLayout->addStretch(); tabWidget->addTab(comicFlowW,tr("Comic Flow")); @@ -65,5 +76,11 @@ OptionsDialog::OptionsDialog(QWidget * parent) } +void OptionsDialog::editApiKey() +{ + ApiKeyDialog d; + d.exec(); +} + diff --git a/YACReaderLibrary/options_dialog.h b/YACReaderLibrary/options_dialog.h index d42a9ead..9a2ca1bc 100644 --- a/YACReaderLibrary/options_dialog.h +++ b/YACReaderLibrary/options_dialog.h @@ -12,6 +12,9 @@ class OptionsDialog : public YACReaderOptionsDialog Q_OBJECT public: OptionsDialog(QWidget * parent = 0); + + public slots: + void editApiKey(); }; diff --git a/common/yacreader_global.h b/common/yacreader_global.h index 651ff54a..593799bd 100644 --- a/common/yacreader_global.h +++ b/common/yacreader_global.h @@ -65,6 +65,8 @@ #define LIBRARIES "LIBRARIES" +#define COMIC_VINE_API_KEY "COMIC_VINE_API_KEY" + namespace YACReader {