QString AlbumCoverChoiceController::SearchForCover(Song* song) {
  // Get something sensible to stick in the search box
  QImage image = cover_searcher_->Exec(song->artist(), song->album());

  if (!image.isNull()) {
    QString cover = SaveCoverInCache(song->artist(), song->album(), image);
    SaveCover(song, cover);

    return cover;
  } else {
    return QString();
  }
}
void AlbumCoverChoiceController::AlbumCoverFetched(
    quint64 id, const QImage& image, const CoverSearchStatistics& statistics) {
  Song song;
  if (cover_fetching_tasks_.contains(id)) {
    song = cover_fetching_tasks_.take(id);
  }

  if (!image.isNull()) {
    QString cover = SaveCoverInCache(song.artist(), song.album(), image);
    SaveCover(&song, cover);
  }

  emit AutomaticCoverSearchDone();
}
QString AlbumCoverChoiceController::LoadCoverFromURL(Song* song) {
  if (!cover_from_url_dialog_) {
    cover_from_url_dialog_ = new CoverFromURLDialog(this);
  }

  QImage image = cover_from_url_dialog_->Exec();

  if (!image.isNull()) {
    QString cover = SaveCoverInCache(song->artist(), song->album(), image);
    SaveCover(song, cover);

    return cover;
  } else {
    return QString();
  }
}
QString AlbumCoverChoiceController::SearchForCover(Song* song) {
  // Get something sensible to stick in the search box.
  // We search for the 'effective' values, but we cache the covers with the
  // Song's artist() and album().
  QImage image = cover_searcher_->Exec(song->effective_albumartist(),
                                       song->effective_album());

  if (!image.isNull()) {
    QString cover = SaveCoverInCache(song->artist(), song->album(), image);
    SaveCover(song, cover);

    return cover;
  } else {
    return QString();
  }
}
QString AlbumCoverChoiceController::SaveCover(Song* song, const QDropEvent* e) {
  for (const QUrl& url : e->mimeData()->urls()) {
    const QString filename = url.toLocalFile();
    const QString suffix = QFileInfo(filename).suffix().toLower();

    if (IsKnownImageExtension(suffix)) {
      SaveCover(song, filename);
      return filename;
    }
  }

  if (e->mimeData()->hasImage()) {
    QImage image = qvariant_cast<QImage>(e->mimeData()->imageData());
    if (!image.isNull()) {
      QString cover_path =
          SaveCoverInCache(song->artist(), song->album(), image);
      SaveCover(song, cover_path);
      return cover_path;
    }
  }

  return QString();
}