$END_LICENSE */
#include "ytchannel.h"
+#include "database.h"
#include "http.h"
#include "httputils.h"
-#include "database.h"
#include <QtSql>
#include "yt3.h"
#include "iconutils.h"
-YTChannel::YTChannel(const QString &channelId, QObject *parent) : QObject(parent),
- id(0),
- channelId(channelId),
- loadingThumbnail(false),
- notifyCount(0),
- checked(0),
- watched(0),
- loaded(0),
- loading(false) { }
+#include "ivchannel.h"
+#include "videoapi.h"
+#include "ytjschannel.h"
+
+YTChannel::YTChannel(const QString &channelId, QObject *parent)
+ : QObject(parent), id(0), channelId(channelId), loadingThumbnail(false), notifyCount(0),
+ checked(0), watched(0), loaded(0), loading(false) {}
-QHash<QString, YTChannel*> YTChannel::cache;
+QHash<QString, YTChannel *> YTChannel::cache;
-YTChannel* YTChannel::forId(const QString &channelId) {
+YTChannel *YTChannel::forId(const QString &channelId) {
if (channelId.isEmpty()) return 0;
auto i = cache.constFind(channelId);
bool success = query.exec();
if (!success) qWarning() << query.lastQuery() << query.lastError().text();
- YTChannel* channel = 0;
+ YTChannel *channel = 0;
if (query.next()) {
// Change userId to ChannelId
channel->checked = query.value(6).toUInt();
channel->loaded = query.value(7).toUInt();
channel->thumbnail = QPixmap(channel->getThumbnailLocation());
- channel->thumbnail.setDevicePixelRatio(IconUtils::maxSupportedPixelRatio());
+ channel->thumbnail.setDevicePixelRatio(2.0);
channel->maybeLoadfromAPI();
cache.insert(channelId, channel);
}
loading = true;
- QUrl url = YT3::instance().method("channels");
- QUrlQuery q(url);
- q.addQueryItem("id", channelId);
- q.addQueryItem("part", "snippet");
- url.setQuery(q);
-
- QObject *reply = HttpUtils::yt().get(url);
- connect(reply, SIGNAL(data(QByteArray)), SLOT(parseResponse(QByteArray)));
- connect(reply, SIGNAL(error(QString)), SLOT(requestError(QString)));
+ if (VideoAPI::impl() == VideoAPI::YT3) {
+ QUrl url = YT3::instance().method("channels");
+ QUrlQuery q(url);
+ q.addQueryItem("id", channelId);
+ q.addQueryItem("part", "snippet");
+ url.setQuery(q);
+
+ QObject *reply = HttpUtils::yt().get(url);
+ connect(reply, SIGNAL(data(QByteArray)), SLOT(parseResponse(QByteArray)));
+ connect(reply, SIGNAL(error(QString)), SLOT(requestError(QString)));
+ } else if (VideoAPI::impl() == VideoAPI::IV) {
+ auto ivChannel = new IVChannel(channelId);
+ connect(ivChannel, &IVChannel::error, this, &YTChannel::requestError);
+ connect(ivChannel, &IVChannel::loaded, this, [this, ivChannel] {
+ displayName = ivChannel->getDisplayName();
+ description = ivChannel->getDescription();
+ thumbnailUrl = ivChannel->getThumbnailUrl();
+ ivChannel->deleteLater();
+ emit infoLoaded();
+ storeInfo();
+ loading = false;
+ });
+ } else if (VideoAPI::impl() == VideoAPI::JS) {
+ auto ivChannel = new YTJSChannel(channelId);
+ connect(ivChannel, &YTJSChannel::error, this, &YTChannel::requestError);
+ connect(ivChannel, &YTJSChannel::loaded, this, [this, ivChannel] {
+ displayName = ivChannel->getDisplayName();
+ description = ivChannel->getDescription();
+ thumbnailUrl = ivChannel->getThumbnailUrl();
+ ivChannel->deleteLater();
+ emit infoLoaded();
+ storeInfo();
+ loading = false;
+ });
+ }
}
void YTChannel::parseResponse(const QByteArray &bytes) {
connect(reply, SIGNAL(error(QString)), SLOT(requestError(QString)));
}
-const QString & YTChannel::getThumbnailDir() {
- static const QString thumbDir = QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + "/channels/";
+const QString &YTChannel::getThumbnailDir() {
+ static const QString thumbDir =
+ QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + "/channels/";
return thumbDir;
}
QString YTChannel::latestVideoId() {
QSqlDatabase db = Database::instance().getConnection();
QSqlQuery query(db);
- query.prepare("select video_id from subscriptions_videos where user_id=? order by published desc limit 1");
+ query.prepare("select video_id from subscriptions_videos where user_id=? order by published "
+ "desc limit 1");
query.bindValue(0, channelId);
bool success = query.exec();
if (!success) qWarning() << query.lastQuery() << query.lastError().text();
void YTChannel::storeThumbnail(const QByteArray &bytes) {
thumbnail.loadFromData(bytes);
- qreal maxRatio = IconUtils::maxSupportedPixelRatio();
+ qreal maxRatio = 2.0;
thumbnail.setDevicePixelRatio(maxRatio);
const int maxWidth = 88 * maxRatio;
loadThumbnail();
}
-void YTChannel::subscribe(const QString &channelId) {
- if (channelId.isEmpty()) return;
+bool YTChannel::subscribe(const QString &channelId) {
+ if (channelId.isEmpty()) return false;
uint now = QDateTime::currentDateTime().toTime_t();
query.bindValue(2, now);
bool success = query.exec();
if (!success) qWarning() << query.lastQuery() << query.lastError().text();
+ return success;
// This will call maybeLoadFromApi
YTChannel::forId(channelId);
query.bindValue(0, channelId);
bool success = query.exec();
if (!success) qWarning() << query.lastQuery() << query.lastError().text();
- if (query.next())
- return query.value(0).toInt() > 0;
+ if (query.next()) return query.value(0).toInt() > 0;
return false;
}
QSqlDatabase db = Database::instance().getConnection();
QSqlQuery query(db);
- query.prepare("update subscriptions set watched=?, notify_count=0, views=views+1 where user_id=?");
+ query.prepare(
+ "update subscriptions set watched=?, notify_count=0, views=views+1 where user_id=?");
query.bindValue(0, now);
query.bindValue(1, channelId);
bool success = query.exec();
}
void YTChannel::storeNotifyCount(int count) {
- if (notifyCount != count)
- emit notifyCountChanged();
+ if (notifyCount != count) emit notifyCountChanged();
notifyCount = count;
QSqlDatabase db = Database::instance().getConnection();