File indexing completed on 2024-06-02 05:00:31
0001 // SPDX-FileCopyrightText: 2021 Linus Jahn <lnj@kaidan.im> 0002 // SPDX-License-Identifier: GPL-3.0-or-later 0003 0004 #include "subscriptioncontroller.h" 0005 0006 #include "plasmatube.h" 0007 0008 #include <QFutureWatcher> 0009 0010 SubscriptionController::SubscriptionController(QObject *parent) 0011 : SubscriptionWatcher(parent) 0012 , m_watcher(nullptr) 0013 { 0014 } 0015 0016 bool SubscriptionController::isLoading() const 0017 { 0018 return m_watcher != nullptr; 0019 } 0020 0021 bool SubscriptionController::canToggleSubscription() const 0022 { 0023 // subscriptions need to be loaded and no other task needs to run 0024 return PlasmaTube::instance().selectedSource()->loggedIn() && PlasmaTube::instance().selectedSource()->subscriptions().has_value() && !m_watcher; 0025 return false; 0026 } 0027 0028 void SubscriptionController::toggleSubscription() 0029 { 0030 if (m_watcher) { 0031 m_watcher->cancel(); 0032 m_watcher->deleteLater(); 0033 m_watcher = nullptr; 0034 } 0035 0036 auto future = isSubscribed() ? PlasmaTube::instance().sourceManager()->selectedSource()->api()->unsubscribeFromChannel(channelId()) 0037 : PlasmaTube::instance().sourceManager()->selectedSource()->api()->subscribeToChannel(channelId()); 0038 0039 m_watcher = new QFutureWatcher<QInvidious::Result>(this); 0040 connect(m_watcher, &QFutureWatcherBase::finished, this, [=] { 0041 auto result = m_watcher->result(); 0042 if (std::holds_alternative<QInvidious::Success>(result)) { 0043 auto &subs = PlasmaTube::instance().selectedSource()->subscriptions(); 0044 if (subs.has_value()) { 0045 if (isSubscribed()) { 0046 // was subscribed -> now unsubscribed 0047 subs->removeAll(channelId()); 0048 } else { 0049 // was not subscribed -> now subscribed 0050 subs->append(channelId()); 0051 } 0052 Q_EMIT PlasmaTube::instance().selectedSource()->subscriptionsChanged(); 0053 } else { 0054 qDebug() << "SubscriptionController::toggleSubscription():" 0055 << "(Un)subscription successful, but subscriptions are not loaded."; 0056 setIsSubscribed(!isSubscribed()); 0057 } 0058 } else if (const auto error = std::get_if<QInvidious::Error>(&result)) { 0059 Q_EMIT errorOccurred(error->second); 0060 } 0061 0062 m_watcher->deleteLater(); 0063 m_watcher = nullptr; 0064 Q_EMIT isLoadingChanged(); 0065 }); 0066 Q_EMIT isLoadingChanged(); 0067 m_watcher->setFuture(future); 0068 } 0069 0070 #include "moc_subscriptioncontroller.cpp"