Ejemplo n.º 1
0
void MainWindow::loadBrowserData() {
  QUrl url = API::sharedAPI()->sharedAniListAPI()->API_BROWSE;

  QString season = ui->comboSeason->currentText();
  QString year   = ui->comboYear->currentText();
  QString type   = ui->comboType->currentText();
  QString status = ui->comboStatus->currentText();

  // Clear the browser view
  QLayoutItem *item;

  while ((item = layout2->takeAt(0))) {
    delete item->widget();
    delete item;
  }

  if (!season.isEmpty()) {
    url = addQuery(url, "season", season);
  }

  if (!year.isEmpty()) {
    url = addQuery(url, "year", year);
  }

  if (!type.isEmpty()) {
    url = addQuery(url, "type", type);
  }

  if (!status.isEmpty()) {
    url = addQuery(url, "status", status);
  }

  QStringList genres;
  QStringList exclude;

  for (int i = 0; i < ui->genreList->count(); i++) {
    QCheckBox *w = static_cast<QCheckBox *>
        (dynamic_cast<QWidgetItem *>(ui->genreList->itemAt(i))->widget());

    if (w->checkState() == Qt::PartiallyChecked) {
      exclude.append(w->text());
    } else if (w->checkState() == Qt::Checked) {
      genres.append(w->text());
    }
  }

  if (!genres.isEmpty()) {
    url = addQuery(url, "genres", genres.join(","));
  }

  if (!exclude.isEmpty()) {
    url = addQuery(url, "genres_exclude", exclude.join(","));
  }

  // Load the results for the requested type
  QJsonArray browse_results =
      API::sharedAPI()->sharedAniListAPI()->get(url).array();

  for (int i = 0; i <= browse_results.size(); i++) {
    QJsonObject anime = browse_results.at(i).toObject();

    Anime *a = User::sharedUser()->getAnimeByTitle(
          anime.value("title_romaji").toString());

    if (a == 0) {
      a = new Anime();
      a->setID(QString::number(anime.value("id").toInt()));
      a->setMyProgress(0);
      a->setMyNotes("");
      a->setMyRewatch(0);
      a->setMyStatus("");

      if (a->getID() == "0") {
        delete a;
        continue;
      }
    }

    BrowseAnime *s = new BrowseAnime(this, User::sharedUser()->scoreType());

    if (a->needsLoad() || a->needsCover()) {
      User::sharedUser()->loadAnimeData(a, true);

      QEventLoop evt;
      connect(a, SIGNAL(finishedReloading()), &evt, SLOT(quit()));
      evt.exec();
    }

    s->setAnime(a);

    layout2->addWidget(s);

    // Do we need to keep loading?
    if (season != ui->comboSeason->currentText() ||
       year != ui->comboYear->currentText() ||
       type != ui->comboType->currentText() ||
       status != ui->comboStatus->currentText()) {
      return;
    }

    int width = layout2->geometry().width();
    int cwidth = layout2->contentsWidth();

    if (cwidth < 0) {
      width = this->width() - 2;
      cwidth = this->width() - (this->width() % 200);
    }

    layout2->setContentsMargins((width-cwidth)/2, 0, 0, 0);
  }
}
Ejemplo n.º 2
0
int User::loadNext() {
  if (queue.empty()) return 1;

  if (this->cancel) return 1;

  QMap<Anime *, bool> data = queue.front();
  queue.pop();

  Anime *anime = data.keys().first();
  bool download_cover = data.values().first();

  QString ID = anime->getID();
  QUrl ID_URL = API::sharedAPI()->sharedAniListAPI()->API_ANIME(ID);

  QJsonObject result =
      API::sharedAPI()->sharedAniListAPI()->get(ID_URL).object();

  anime->setCoverURL(QUrl(result.value("image_url_lge").toString()));

  if (download_cover) {
    QEventLoop evt;
    connect(anime, SIGNAL(new_image()), &evt, SLOT(quit()));
    anime->downloadCover();
    evt.exec();
  }

  QString description = result.value("description").toString();

  anime->setDuration(result.value("duration").toInt());
  anime->setSynopsis(description);
  anime->setRomajiTitle(result.value("title_romaji").toString());
  anime->setJapaneseTitle(result.value("title_japanese").toString());
  anime->setEnglishTitle(result.value("title_english").toString());
  anime->setType(result.value("type").toString());
  anime->setAiringStatus(result.value("airing_status").toString());
  anime->setEpisodeCount(result.value("total_episodes").toInt());
  anime->setAverageScore(result.value("average_score").toString());
  anime->setTitle(result.value(title_language).toString());

  if (anime->getAiringStatus() == "currently airing") {
    QJsonObject airing = result.value("airing").toObject();

    anime->setNextEpisode(airing.value("next_episode").toInt());
    anime->setCountdown(airing.value("countdown").toInt());

    if (anime->getCountdown() > 0) {
      anime->setAiringSchedule(true);
    } else {
      anime->setAiringSchedule(false);
    }
  }

  QJsonArray synonyms = result.value("synonyms").toArray();

  for (int j = 0; j < synonyms.count(); j++) {
    anime->addSynonym(synonyms.at(j).toString());
  }

  anime->finishReload();

  qDebug() << "Loaded extra data for anime" << anime->getTitle();
  db->saveAnime(anime);

  if (!queue.empty()) {
    async_registry.append(QtConcurrent::run([&, this]() {  // NOLINT
      loadNext();
      return 1;
    }));
  }

  return 1;
}