1 #include "comic_vine_dialog.h"
2 #include <QHBoxLayout>
3 #include <QLabel>
4 #include <QMessageBox>
5 #include <QPushButton>
6 #include <QRadioButton>
7 #include <QStackedWidget>
8 #include <QTableView>
9 #include <QVBoxLayout>
10 #include <QtWidgets>
11 #if QT_VERSION >= 0x050000
12 #include <QtConcurrent/QtConcurrentRun>
13 #else
14 #include <QtConcurrentRun>
15 #endif
16 #include "data_base_management.h"
17 #include <QJsonDocument>
18 #include <QJsonParseError>
19 #include <QSqlDatabase>
20 
21 #include "yacreader_busy_widget.h"
22 #include "comic_vine_client.h"
23 #include "scraper_lineedit.h"
24 #include "title_header.h"
25 #include "series_question.h"
26 #include "search_single_comic.h"
27 #include "search_volume.h"
28 #include "select_comic.h"
29 #include "select_volume.h"
30 #include "sort_volume_comics.h"
31 #include "db_helper.h"
32 #include "response_parser.h"
33 
34 #include "QsLog.h"
35 
ComicVineDialog(QWidget * parent)36 ComicVineDialog::ComicVineDialog(QWidget *parent)
37     : QDialog(parent)
38 {
39     setWindowFlags(Qt::Window);
40 
41     doLayout();
42     doStackedWidgets();
43     doConnections();
44 }
45 
doLayout()46 void ComicVineDialog::doLayout()
47 {
48     setStyleSheet(""
49                   "QDialog {background-color: #404040; }"
50                   "");
51 
52     QString dialogButtonsStyleSheet = "QPushButton {border: 1px solid #242424; background: #2e2e2e; color:white; padding: 5px 26px 5px 26px; font-size:12px;font-family:Arial; font-weight:bold;}";
53 
54     skipButton = new QPushButton(tr("skip"));
55     backButton = new QPushButton(tr("back"));
56     nextButton = new QPushButton(tr("next"));
57     searchButton = new QPushButton(tr("search"));
58     closeButton = new QPushButton(tr("close"));
59 
60     backButton->setShortcut(QKeySequence(Qt::Key_Backspace));
61 
62     closeButton->setDefault(false);
63     closeButton->setAutoDefault(false);
64 
65     skipButton->setStyleSheet(dialogButtonsStyleSheet);
66     backButton->setStyleSheet(dialogButtonsStyleSheet);
67     nextButton->setStyleSheet(dialogButtonsStyleSheet);
68     searchButton->setStyleSheet(dialogButtonsStyleSheet);
69     closeButton->setStyleSheet(dialogButtonsStyleSheet);
70 
71     content = new QStackedWidget(this);
72 
73     auto mainLayout = new QVBoxLayout;
74 
75     auto buttonLayout = new QHBoxLayout;
76 
77     buttonLayout->addStretch();
78     buttonLayout->addWidget(skipButton);
79     buttonLayout->addWidget(backButton);
80     buttonLayout->addWidget(nextButton);
81     buttonLayout->addWidget(searchButton);
82     buttonLayout->addWidget(closeButton);
83     buttonLayout->setContentsMargins(0, 0, 0, 0);
84 
85     mainLayout->addWidget(titleHeader = new TitleHeader, 0);
86     mainLayout->addWidget(content, 1);
87     mainLayout->addLayout(buttonLayout, 0);
88 
89     mainLayout->setContentsMargins(26, 16, 26, 11);
90 
91     setLayout(mainLayout);
92 
93     setWindowTitle("Comic Vine Scraper (beta)");
94 }
95 
doStackedWidgets()96 void ComicVineDialog::doStackedWidgets()
97 {
98     doLoading();
99     content->addWidget(seriesQuestionWidget = new SeriesQuestion);
100     content->addWidget(searchSingleComicWidget = new SearchSingleComic);
101     content->addWidget(searchVolumeWidget = new SearchVolume);
102     content->addWidget(selectVolumeWidget = new SelectVolume);
103     content->addWidget(selectComicWidget = new SelectComic);
104     content->addWidget(sortVolumeComicsWidget = new SortVolumeComics);
105 }
106 
doConnections()107 void ComicVineDialog::doConnections()
108 {
109     connect(closeButton, SIGNAL(clicked()), this, SLOT(close()));
110     connect(nextButton, SIGNAL(clicked()), this, SLOT(goNext()));
111     connect(backButton, SIGNAL(clicked()), this, SLOT(goBack()));
112     connect(searchButton, SIGNAL(clicked()), this, SLOT(search()));
113     connect(skipButton, SIGNAL(clicked()), this, SLOT(goToNextComic()));
114 
115     connect(selectVolumeWidget, SIGNAL(loadPage(QString, int)), this, SLOT(searchVolume(QString, int)));
116     connect(selectComicWidget, SIGNAL(loadPage(QString, int)), this, SLOT(getVolumeComicsInfo(QString, int)));
117     connect(sortVolumeComicsWidget, SIGNAL(loadPage(QString, int)), this, SLOT(getVolumeComicsInfo(QString, int)));
118 
119     connect(this, SIGNAL(accepted()), this, SLOT(close()), Qt::QueuedConnection);
120 }
121 
goNext()122 void ComicVineDialog::goNext()
123 {
124     //
125     if (content->currentWidget() == seriesQuestionWidget) {
126         if (seriesQuestionWidget->getYes()) {
127             QString volumeSearchString = comics[0].getParentFolderName();
128             mode = Volume;
129 
130             if (volumeSearchString.isEmpty())
131                 showSearchVolume();
132             else {
133                 status = AutoSearching;
134                 showLoading(tr("Looking for volume..."));
135                 searchVolume(volumeSearchString);
136             }
137         } else {
138             status = AutoSearching;
139             mode = SingleComicInList;
140             ComicDB comic = comics[currentIndex];
141             QString title = comic.getTitleOrFileName();
142             titleHeader->setSubTitle(tr("comic %1 of %2 - %3").arg(currentIndex + 1).arg(comics.length()).arg(title));
143 
144             showLoading(tr("Looking for volume..."));
145             searchVolume(title);
146         }
147     } else if (content->currentWidget() == selectVolumeWidget) {
148         currentVolumeId = selectVolumeWidget->getSelectedVolumeId();
149         getVolumeComicsInfo(currentVolumeId);
150 
151     } else if (content->currentWidget() == sortVolumeComicsWidget) {
152         showLoading();
153 
154         //ComicDB-ComicVineID
155         QList<QPair<ComicDB, QString>> matchingInfo = sortVolumeComicsWidget->getMatchingInfo();
156         int count = selectVolumeWidget->getSelectedVolumeNumIssues();
157         QString publisher = selectVolumeWidget->getSelectedVolumePublisher();
158         QtConcurrent::run(this, &ComicVineDialog::getComicsInfo, matchingInfo, count, publisher);
159     } else if (content->currentWidget() == selectComicWidget) {
160         showLoading();
161         QString comicId = selectComicWidget->getSelectedComicId();
162         int count = selectVolumeWidget->getSelectedVolumeNumIssues();
163         QString publisher = selectVolumeWidget->getSelectedVolumePublisher();
164         QtConcurrent::run(this, &ComicVineDialog::getComicInfo, comicId, count, publisher);
165     }
166 }
167 
goBack()168 void ComicVineDialog::goBack()
169 {
170     switch (status) {
171     case SelectingSeries:
172         if (mode == Volume)
173             showSearchVolume();
174         else
175             showSearchSingleComic();
176         break;
177     case SortingComics:
178         showSelectVolume();
179         break;
180     case SelectingComic:
181         if (mode == SingleComic)
182             showSelectVolume();
183         break;
184     case AutoSearching:
185         if (mode == Volume)
186             showSearchVolume();
187         else
188             showSearchSingleComic();
189         break;
190     default:
191         if (mode == Volume)
192             showSearchVolume();
193         else
194             showSearchSingleComic();
195         break;
196     }
197 }
198 
setComics(const QList<ComicDB> & comics)199 void ComicVineDialog::setComics(const QList<ComicDB> &comics)
200 {
201     this->comics = comics;
202 }
203 
sizeHint() const204 QSize ComicVineDialog::sizeHint() const
205 {
206     int heightDesktopResolution = QApplication::desktop()->screenGeometry().height();
207     int widthDesktopResolution = QApplication::desktop()->screenGeometry().width();
208     int height, width;
209     height = qMax(529, static_cast<int>(heightDesktopResolution * 0.5));
210     width = height * 1.65;
211 
212     if (width > widthDesktopResolution)
213         return minimumSizeHint();
214 
215     return QSize(width, height);
216 }
217 
minimumSizeHint() const218 QSize ComicVineDialog::minimumSizeHint() const
219 {
220     return QSize(872, 529);
221 }
222 
show()223 void ComicVineDialog::show()
224 {
225     QDialog::show();
226 
227     currentIndex = 0;
228 
229     seriesQuestionWidget->setYes(true);
230     searchSingleComicWidget->clean();
231     searchVolumeWidget->clean();
232 
233     if (comics.length() == 1) {
234         status = AutoSearching;
235         mode = SingleComic;
236 
237         ComicDB singleComic = comics[0];
238         QString title = singleComic.getTitleOrFileName();
239         titleHeader->setSubTitle(title);
240         showLoading(tr("Looking for volume..."));
241 
242         searchVolume(singleComic.getParentFolderName());
243         QLOG_TRACE() << singleComic.getParentFolderName();
244     } else if (comics.length() > 1) {
245         titleHeader->setSubTitle(tr("%1 comics selected").arg(comics.length()));
246         showSeriesQuestion();
247     }
248 }
249 
doLoading()250 void ComicVineDialog::doLoading()
251 {
252     QWidget *w = new QWidget;
253     auto l = new QVBoxLayout;
254 
255     auto bw = new YACReaderBusyWidget;
256     loadingMessage = new QLabel;
257 
258     loadingMessage->setStyleSheet("QLabel {color:white; font-size:12px;font-family:Arial;}");
259 
260     l->addStretch();
261     l->addWidget(bw, 0, Qt::AlignHCenter);
262     l->addStretch();
263     l->addWidget(loadingMessage);
264 
265     l->setContentsMargins(0, 0, 0, 0);
266     w->setLayout(l);
267     w->setContentsMargins(0, 0, 0, 0);
268 
269     content->addWidget(w);
270 }
271 
debugClientResults(const QString & string)272 void ComicVineDialog::debugClientResults(const QString &string)
273 {
274     ResponseParser p;
275     p.loadJSONResponse(string);
276     //QMessageBox::information(0,"Result", QString("Number of results : %1").arg(p.getNumResults()));
277     if (p.responseError()) {
278         QMessageBox::critical(0, tr("Error connecting to ComicVine"), p.errorDescription());
279         goBack();
280     } else {
281         switch (mode) {
282         case SingleComic:
283         case SingleComicInList:
284             if (p.getNumResults() == 0)
285                 showSearchSingleComic();
286             else if (status == SearchingVolume)
287                 showSelectVolume(string);
288             else
289                 showSelectComic(string);
290             break;
291         case Volume:
292             if (p.getNumResults() == 0)
293                 showSearchVolume();
294             else
295                 showSelectVolume(string);
296             break;
297         }
298     }
299 }
300 
showSeriesQuestion()301 void ComicVineDialog::showSeriesQuestion()
302 {
303     status = AskingForInfo;
304     content->setCurrentWidget(seriesQuestionWidget);
305     backButton->setHidden(true);
306     skipButton->setHidden(true);
307     nextButton->setVisible(true);
308     searchButton->setHidden(true);
309     closeButton->setVisible(true);
310 
311     nextButton->setDefault(true);
312 
313     toggleSkipButton();
314 }
315 
showSearchSingleComic()316 void ComicVineDialog::showSearchSingleComic()
317 {
318     status = AskingForInfo;
319     content->setCurrentWidget(searchSingleComicWidget);
320     backButton->setHidden(true);
321     skipButton->setHidden(true);
322     nextButton->setHidden(true);
323     searchButton->setVisible(true);
324     closeButton->setVisible(true);
325 
326     searchButton->setDefault(true);
327 
328     toggleSkipButton();
329 }
330 
showSearchVolume()331 void ComicVineDialog::showSearchVolume()
332 {
333     status = AskingForInfo;
334     content->setCurrentWidget(searchVolumeWidget);
335     backButton->setHidden(true);
336     nextButton->setHidden(true);
337     searchButton->setVisible(true);
338     closeButton->setVisible(true);
339 
340     searchButton->setDefault(true);
341 
342     toggleSkipButton();
343 }
344 
showSelectVolume(const QString & json)345 void ComicVineDialog::showSelectVolume(const QString &json)
346 {
347     showSelectVolume();
348     selectVolumeWidget->load(json, currentVolumeSearchString);
349 }
350 
showSelectVolume()351 void ComicVineDialog::showSelectVolume()
352 {
353     status = SelectingSeries;
354 
355     content->setCurrentWidget(selectVolumeWidget);
356 
357     backButton->setVisible(true);
358     nextButton->setVisible(true);
359     searchButton->setHidden(true);
360     closeButton->setVisible(true);
361 
362     nextButton->setDefault(true);
363 
364     toggleSkipButton();
365 }
366 
showSelectComic(const QString & json)367 void ComicVineDialog::showSelectComic(const QString &json)
368 {
369     status = SelectingComic;
370 
371     content->setCurrentWidget(selectComicWidget);
372     selectComicWidget->load(json, currentVolumeId);
373 
374     backButton->setVisible(true);
375     nextButton->setVisible(true);
376     searchButton->setHidden(true);
377     closeButton->setVisible(true);
378 
379     nextButton->setDefault(true);
380 
381     toggleSkipButton();
382 }
383 
showSortVolumeComics(const QString & json)384 void ComicVineDialog::showSortVolumeComics(const QString &json)
385 {
386     status = SortingComics;
387 
388     content->setCurrentWidget(sortVolumeComicsWidget);
389 
390     sortVolumeComicsWidget->setData(comics, json, currentVolumeId);
391 
392     backButton->setVisible(true);
393     nextButton->setVisible(true);
394     searchButton->setHidden(true);
395     closeButton->setVisible(true);
396 
397     nextButton->setDefault(true);
398 
399     toggleSkipButton();
400 }
401 
queryTimeOut()402 void ComicVineDialog::queryTimeOut()
403 {
404     QMessageBox::warning(this, "Comic Vine error", "Time out connecting to Comic Vine");
405 
406     switch (status) {
407     case AutoSearching:
408         if (mode == Volume)
409             showSearchVolume();
410         else
411             showSearchSingleComic();
412         break;
413     case SearchingVolume:
414         if (mode == Volume)
415             showSearchVolume();
416         else
417             showSearchSingleComic();
418         break;
419     case SearchingSingleComic:
420         showSearchSingleComic();
421         break;
422     case GettingVolumeComics:
423         showSelectVolume();
424         break;
425     default:
426         break;
427     }
428 }
429 
getComicsInfo(QList<QPair<ComicDB,QString>> & matchingInfo,int count,const QString & publisher)430 void ComicVineDialog::getComicsInfo(QList<QPair<ComicDB, QString>> &matchingInfo, int count, const QString &publisher)
431 {
432     QPair<ComicDB, QString> p;
433     QList<ComicDB> comics;
434     foreach (p, matchingInfo) {
435         auto comicVineClient = new ComicVineClient;
436         //connect(comicVineClient,SIGNAL(searchResult(QString)),this,SLOT(debugClientResults(QString)));
437         //connect(comicVineClient,SIGNAL(timeOut()),this,SLOT(queryTimeOut()));
438         //connect(comicVineClient,SIGNAL(finished()),comicVineClient,SLOT(deleteLater()));
439         bool error;
440         bool timeout;
441         QByteArray result = comicVineClient->getComicDetail(p.second, error, timeout); //TODO check timeOut or Connection error
442         if (error || timeout)
443             continue; //TODO
444         ComicDB comic = parseComicInfo(p.first, result, count, publisher); //TODO check result error
445         comic.info.comicVineID = p.second;
446         comics.push_back(comic);
447 
448         setLoadingMessage(tr("Retrieving tags for : %1").arg(p.first.getFileName()));
449     }
450     QString connectionName = "";
451     {
452         QSqlDatabase db = DataBaseManagement::loadDatabase(databasePath);
453         db.open();
454         db.transaction();
455         foreach (ComicDB comic, comics) {
456             DBHelper::update(&(comic.info), db);
457         }
458         db.commit();
459         connectionName = db.connectionName();
460     }
461     QSqlDatabase::removeDatabase(connectionName);
462 
463     emit accepted();
464 }
465 
getComicInfo(const QString & comicId,int count,const QString & publisher)466 void ComicVineDialog::getComicInfo(const QString &comicId, int count, const QString &publisher)
467 {
468 
469     auto comicVineClient = new ComicVineClient;
470     bool error;
471     bool timeout;
472     QByteArray result = comicVineClient->getComicDetail(comicId, error, timeout); //TODO check timeOut or Connection error
473     if (error || timeout) {
474         //TODO
475         if (mode == SingleComic || currentIndex == (comics.count() - 1)) {
476             emit accepted();
477         } else {
478             goToNextComic();
479         }
480     }
481 
482     ComicDB comic = parseComicInfo(comics[currentIndex], result, count, publisher); //TODO check result error
483     comic.info.comicVineID = comicId;
484     setLoadingMessage(tr("Retrieving tags for : %1").arg(comics[currentIndex].getFileName()));
485     QString connectionName = "";
486     {
487         QSqlDatabase db = DataBaseManagement::loadDatabase(databasePath);
488         db.open();
489         db.transaction();
490 
491         DBHelper::update(&(comic.info), db);
492 
493         db.commit();
494         connectionName = db.connectionName();
495     }
496     QSqlDatabase::removeDatabase(connectionName);
497 
498     if (mode == SingleComic || currentIndex == (comics.count() - 1)) {
499         emit accepted();
500     } else {
501         goToNextComic();
502     }
503 }
504 
parseComicInfo(ComicDB & comic,const QString & json,int count,const QString & publisher)505 ComicDB ComicVineDialog::parseComicInfo(ComicDB &comic, const QString &json, int count, const QString &publisher)
506 {
507     QJsonParseError Err;
508 
509     QVariantMap sc = QJsonDocument::fromJson(json.toUtf8(), &Err).toVariant().toMap();
510     if (Err.error != QJsonParseError::NoError) {
511         qDebug("Error detected");
512         return comic;
513     }
514 
515     int numResults = sc.value("number_of_total_results").toInt(); //fix to weird behaviour using hasNext
516 
517     if (numResults > 0) {
518         QVariantMap result = sc.value("results").toMap();
519         comic.info.title = result.value("name");
520         comic.info.number = result.value("issue_number");
521         comic.info.volume = result.value("volume").toMap().value("name");
522 
523         if (result.contains("person_credits") && !result.value("person_credits").isNull()) {
524             QMap<QString, QString> authors = getAuthors(result.value("person_credits"));
525 
526             QString writer = QStringList(authors.values("writer")).join("\n");
527             QString penciller = QStringList(authors.values("penciller")).join("\n");
528             QString inker = QStringList(authors.values("inker")).join("\n");
529             QString colorist = QStringList(authors.values("colorist")).join("\n");
530             QString letterer = QStringList(authors.values("letterer")).join("\n");
531             QString coverArtist = QStringList(authors.values("cover")).join("\n");
532 
533             comic.info.writer = writer;
534             comic.info.penciller = penciller;
535             comic.info.inker = inker;
536             comic.info.colorist = colorist;
537             comic.info.letterer = letterer;
538             comic.info.coverArtist = coverArtist;
539         }
540 
541         if (result.contains("cover_date") && !result.value("conver_date").isNull()) {
542             QString date = result.value("cover_date").toString();
543 
544             QStringList tempList = date.split("-");
545 
546             if (tempList.length() == 3) {
547                 std::reverse(tempList.begin(), tempList.end());
548                 comic.info.date = tempList.join("/");
549             }
550         }
551 
552         if (result.contains("description") && !result.value("description").isNull()) {
553             comic.info.synopsis = result.value("description");
554         }
555 
556         if (result.contains("character_credits") && !result.value("character_credits").isNull()) {
557             comic.info.characters = getCharacters(result.value("character_credits"));
558         }
559 
560         if (result.contains("story_arc_credits") && !result.value("story_arc_credits").isNull()) {
561             QPair<QString, QString> storyArcIdAndName = getFirstStoryArcIdAndName(result.value("story_arc_credits"));
562             QString storyArcId = storyArcIdAndName.first;
563             QString storyArcName = storyArcIdAndName.second;
564             if (!storyArcId.isNull()) {
565 
566                 QString comicId = result.value("id").toString();
567 
568                 QPair<QString, QString> arcNumberAndArcCount = getArcNumberAndArcCount(storyArcId, comicId);
569                 if (!arcNumberAndArcCount.first.isNull()) {
570                     QString arcNumber = arcNumberAndArcCount.first;
571                     QString arcCount = arcNumberAndArcCount.second;
572 
573                     comic.info.storyArc = storyArcName;
574                     comic.info.arcNumber = arcNumber;
575                     comic.info.arcCount = arcCount;
576                 }
577             }
578         }
579 
580         comic.info.count = count;
581 
582         comic.info.publisher = publisher;
583     }
584 
585     return comic;
586 }
587 
getCharacters(const QVariant & json_characters)588 QString ComicVineDialog::getCharacters(const QVariant &json_characters)
589 {
590     QStringList characters;
591 
592     QListIterator<QVariant> it(json_characters.toList());
593     QVariantMap resultsValue;
594     while (it.hasNext()) {
595         resultsValue = it.next().toMap();
596 
597         characters << resultsValue.value("name").toString();
598     }
599 
600     return (characters.isEmpty()) ? "" : (characters.join("\n") + "\n");
601 }
602 
getAuthors(const QVariant & json_authors)603 QMap<QString, QString> ComicVineDialog::getAuthors(const QVariant &json_authors)
604 {
605     QMap<QString, QString> authors;
606 
607     QListIterator<QVariant> it(json_authors.toList());
608     QVariantMap resultsValue;
609     while (it.hasNext()) {
610         resultsValue = it.next().toMap();
611 
612         QString authorName = resultsValue.value("name").toString();
613 
614         QStringList roles = resultsValue.value("role").toString().split(",");
615         foreach (QString role, roles) {
616             if (role.trimmed() == "writer")
617                 authors.insertMulti("writer", authorName);
618             else if (role.trimmed() == "inker")
619                 authors.insertMulti("inker", authorName);
620             else if (role.trimmed() == "penciler" || role.trimmed() == "penciller")
621                 authors.insertMulti("penciller", authorName);
622             else if (role.trimmed() == "colorist")
623                 authors.insertMulti("colorist", authorName);
624             else if (role.trimmed() == "letterer")
625                 authors.insertMulti("letterer", authorName);
626             else if (role.trimmed() == "cover")
627                 authors.insertMulti("cover", authorName);
628         }
629     }
630 
631     return authors;
632 }
633 
getFirstStoryArcIdAndName(const QVariant & json_story_arcs)634 QPair<QString, QString> ComicVineDialog::getFirstStoryArcIdAndName(const QVariant &json_story_arcs)
635 {
636     QString story_arc_id = QString();
637     QString story_arc_name = QString();
638 
639     QListIterator<QVariant> it(json_story_arcs.toList());
640     QVariantMap resultsValue;
641     while (it.hasNext()) {
642         resultsValue = it.next().toMap();
643         story_arc_id = resultsValue.value("id").toString();
644         story_arc_name = resultsValue.value("name").toString();
645         break;
646     }
647     return qMakePair(story_arc_id, story_arc_name);
648 }
649 
getArcNumberAndArcCount(const QString & storyArcId,const QString & comicId)650 QPair<QString, QString> ComicVineDialog::getArcNumberAndArcCount(const QString &storyArcId, const QString &comicId)
651 {
652     auto comicVineClient = new ComicVineClient;
653     bool error;
654     bool timeout;
655     QByteArray result = comicVineClient->getStoryArcDetail(storyArcId, error, timeout);
656     if (error || timeout)
657         return qMakePair(QString(), QString());
658     QString json = result;
659 
660     QJsonParseError Err;
661     QVariantMap sc = QJsonDocument::fromJson(json.toUtf8(), &Err).toVariant().toMap();
662 
663     if (Err.error != QJsonParseError::NoError) {
664         qDebug("Error detected");
665         return qMakePair(QString(), QString());
666     }
667 
668     int numResults = sc.value("number_of_total_results").toInt(); //fix to weird behaviour using hasNext
669 
670     if (numResults > 0) {
671         QVariantMap result = sc.value("results").toMap();
672 
673         if (result.contains("issues")) {
674             QListIterator<QVariant> it(result.value("issues").toList());
675             int arcNumber = 0;
676             int arcCount = 0;
677 
678             QVariantMap resultsValue;
679             while (it.hasNext()) {
680                 resultsValue = it.next().toMap();
681                 if (comicId == resultsValue.value("id").toString()) {
682                     arcNumber = arcCount + 1;
683                 }
684                 arcCount++;
685             }
686             return qMakePair(QString::number(arcNumber), QString::number(arcCount));
687         }
688         return qMakePair(QString(), QString());
689     }
690     return qMakePair(QString(), QString());
691 }
692 
toggleSkipButton()693 void ComicVineDialog::toggleSkipButton()
694 {
695     if (mode == SingleComicInList)
696         skipButton->setVisible(true);
697     else
698         skipButton->setHidden(true);
699 }
700 
goToNextComic()701 void ComicVineDialog::goToNextComic()
702 {
703     if (mode == SingleComic || currentIndex == (comics.count() - 1)) {
704         emit accepted();
705         return;
706     }
707 
708     currentIndex++;
709 
710     showSearchSingleComic();
711 
712     ComicDB comic = comics[currentIndex];
713     QString title = comic.getTitleOrFileName();
714     titleHeader->setSubTitle(tr("comic %1 of %2 - %3").arg(currentIndex + 1).arg(comics.length()).arg(title));
715 }
716 
showLoading(const QString & message)717 void ComicVineDialog::showLoading(const QString &message)
718 {
719     content->setCurrentIndex(0);
720     loadingMessage->setText(message);
721     backButton->setHidden(true);
722     skipButton->setHidden(true);
723     nextButton->setHidden(true);
724     searchButton->setHidden(true);
725     closeButton->setVisible(true);
726 }
727 
setLoadingMessage(const QString & message)728 void ComicVineDialog::setLoadingMessage(const QString &message)
729 {
730     loadingMessage->setText(message);
731 }
732 
search()733 void ComicVineDialog::search()
734 {
735     switch (mode) {
736     case Volume:
737         launchSearchVolume();
738         break;
739     default:
740         launchSearchComic();
741         break;
742     }
743 }
744 
searchVolume(const QString & v,int page)745 void ComicVineDialog::searchVolume(const QString &v, int page)
746 {
747     showLoading(tr("Looking for volume..."));
748 
749     currentVolumeSearchString = v;
750 
751     auto comicVineClient = new ComicVineClient;
752     connect(comicVineClient, SIGNAL(searchResult(QString)), this, SLOT(debugClientResults(QString)));
753     connect(comicVineClient, SIGNAL(timeOut()), this, SLOT(queryTimeOut()));
754     connect(comicVineClient, SIGNAL(finished()), comicVineClient, SLOT(deleteLater()));
755     comicVineClient->search(v, page);
756 
757     status = SearchingVolume;
758 }
759 
getVolumeComicsInfo(const QString & vID,int)760 void ComicVineDialog::getVolumeComicsInfo(const QString &vID, int /* page */)
761 {
762     showLoading(tr("Retrieving volume info..."));
763 
764     status = GettingVolumeComics;
765 
766     auto comicVineClient = new ComicVineClient;
767     if (mode == Volume)
768         connect(comicVineClient, SIGNAL(volumeComicsInfo(QString)), this, SLOT(showSortVolumeComics(QString)));
769     else
770         connect(comicVineClient, SIGNAL(volumeComicsInfo(QString)), this, SLOT(showSelectComic(QString)));
771     connect(comicVineClient, SIGNAL(timeOut()), this, SLOT(queryTimeOut()));
772     connect(comicVineClient, SIGNAL(finished()), comicVineClient, SLOT(deleteLater()));
773 
774     QLOG_TRACE() << vID;
775 
776     comicVineClient->getAllVolumeComicsInfo(vID);
777 }
778 
launchSearchVolume()779 void ComicVineDialog::launchSearchVolume()
780 {
781     showLoading(tr("Looking for volume..."));
782     //TODO: check if volume info is empty.
783     searchVolume(searchVolumeWidget->getVolumeInfo());
784 }
785 
launchSearchComic()786 void ComicVineDialog::launchSearchComic()
787 {
788     showLoading(tr("Looking for comic..."));
789 
790     QString volumeInfo = searchSingleComicWidget->getVolumeInfo();
791     //QString comicInfo = searchSingleComicWidget->getComicInfo();
792     //int comicNumber = searchSingleComicWidget->getComicNumber();
793 
794     //if(comicInfo.isEmpty() && comicNumber == -1)
795     searchVolume(volumeInfo);
796 }
797