root/src/tvshowpage.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include "tvshowpage.h"
#include "ui_tvshowpage.h"
#include "server.h"

TvShowPage::TvShowPage(Library& library, QWidget *parent) :
    Page(parent),
    ui(new Ui::TvShowPage),
    library(library),
    tvShow(NULL)
{
    ui->setupUi(this);

    ui->synopsis->hide();
    ui->label_synopsis->hide();
}

TvShowPage::~TvShowPage()
{
    delete ui;
}

void TvShowPage::initFromQuery(const QString &initString) {
    TvShow* show = library.existingTvShow(initString);
    this->setTvShow(show);
}

void TvShowPage::setTvShow(TvShow* show) {
    if (this->tvShow) {
        disconnect(this->tvShow, SIGNAL(watchCountChanged(int,int)), this, SLOT(updateWatched(int,int)));
    }
    this->tvShow = show;

    if (!show) {
        return;
    }

    connect(&tvShow->episodeList(), SIGNAL(watchCountChanged(int,int)), this, SLOT(updateWatched(int,int)));

    ui->title->setText(show->name());
    if (show->coverPath(library.getDirectory()).length() > 0) {
        QPixmap pix(show->coverPath(library.getDirectory()));
        ui->cover->setPixmap(pix);
    }
    ui->status->setText(show->getAiringStatus());
    ui->startDate->setText(show->getStartDate().toString("yyyy-MM-dd"));
    ui->endDate->setText(show->getEndDate().toString("yyyy-MM-dd"));
    ui->synopsis->setText(show->getSynopsis());

    this->updateWatched(0,0);

    QString backgroundWallpaper = show->randomWallpaper(library.getDirectory());
    if (!backgroundWallpaper.isNull()) {
        dynamic_cast<MainBackgroundWidget*>(this->parentWidget())->setBackground(backgroundWallpaper);
    } else {
        dynamic_cast<MainBackgroundWidget*>(this->parentWidget())->setBackground(QString());
    }
}

void TvShowPage::updateWatched(int,  int) {
    // allow direct calls, but cancel delayed calls when show-selection changed
    if (sender() != NULL && (TvShow*)sender() != this->tvShow) {
        return;
    }
    ui->episodes->setText(QString("%1/%2/%3").arg(
        QString::number(tvShow->episodeList().numberOfWatchedEpisodes()),
        QString::number(tvShow->episodeList().numberOfEpisodes()),
        QString::number(tvShow->getTotalEpisodes())
    ));
}

bool TvShowPage::handleApiRequest(QHttpRequest *req, QHttpResponse *resp)
{
    if (!tvShow) {
        return false;
    }

    if (req->path() == "/api/page/details") {
        tvShow->handleApiRequest("/details", req, resp);
        return true;
    }
    return false;
}

bool TvShowPage::conformsTo(QString query) const {
    if (NULL == this->tvShow) {
        return false;
    }
    return this->tvShow->matchesNameOrSynonym(query);
}