File indexing completed on 2024-10-06 07:34:03

0001 /* This file is part of the KDE project
0002 
0003    Copyright (C) 2007 by Javier Goday <jgoday@gmail.com>
0004    Copyright (C) 2009 by Dario Massarin <nekkar@libero.it>
0005    Copyright (C) 2010 by Matthias Fuchs <mat69@gmx.net>
0006 
0007    This program is free software; you can redistribute it and/or
0008    modify it under the terms of the GNU General Public
0009    License as published by the Free Software Foundation; either
0010    version 2 of the License, or (at your option) any later version.
0011 */
0012 
0013 #include "kuiserverjobs.h"
0014 
0015 #include "kget.h"
0016 #include "kgetkjobadapter.h"
0017 #include "settings.h"
0018 #include "transferhandler.h"
0019 
0020 #include "kget_debug.h"
0021 
0022 #include <KIO/JobTracker>
0023 #include <KUiServerJobTracker>
0024 
0025 #include <QDebug>
0026 
0027 KUiServerJobs::KUiServerJobs(QObject *parent)
0028     : QObject(parent)
0029     , m_globalJob(nullptr)
0030 {
0031 }
0032 
0033 KUiServerJobs::~KUiServerJobs()
0034 {
0035     while (m_registeredJobs.size()) {
0036         unregisterJob(m_registeredJobs.begin().value(), m_registeredJobs.begin().key());
0037     }
0038 
0039     delete m_globalJob;
0040 }
0041 
0042 void KUiServerJobs::settingsChanged()
0043 {
0044     QList<TransferHandler *> transfers = KGet::allTransfers();
0045 
0046     foreach (TransferHandler *transfer, transfers) {
0047         if (shouldBeShown(transfer))
0048             registerJob(transfer->kJobAdapter(), transfer);
0049         else
0050             unregisterJob(transfer->kJobAdapter(), transfer);
0051     }
0052 
0053     // GlobalJob is associated to a virtual transfer pointer of value == nullptr
0054     if (shouldBeShown(nullptr))
0055         registerJob(globalJob(), nullptr);
0056     else
0057         unregisterJob(globalJob(), nullptr);
0058 }
0059 
0060 void KUiServerJobs::slotTransfersAdded(QList<TransferHandler *> transfers)
0061 {
0062     qCDebug(KGET_DEBUG);
0063 
0064     foreach (TransferHandler *transfer, transfers) {
0065         if (shouldBeShown(transfer))
0066             registerJob(transfer->kJobAdapter(), transfer);
0067 
0068         if (shouldBeShown(nullptr)) {
0069             globalJob()->update();
0070             registerJob(globalJob(), nullptr);
0071         } else
0072             unregisterJob(globalJob(), nullptr);
0073     }
0074 }
0075 
0076 void KUiServerJobs::slotTransfersAboutToBeRemoved(const QList<TransferHandler *> &transfers)
0077 {
0078     qCDebug(KGET_DEBUG);
0079 
0080     m_invalidTransfers << transfers;
0081     foreach (TransferHandler *transfer, transfers) {
0082         unregisterJob(transfer->kJobAdapter(), transfer);
0083 
0084         if (shouldBeShown(nullptr)) {
0085             globalJob()->update();
0086             registerJob(globalJob(), nullptr);
0087         } else {
0088             unregisterJob(globalJob(), nullptr);
0089         }
0090     }
0091 }
0092 
0093 void KUiServerJobs::slotTransfersChanged(QMap<TransferHandler *, Transfer::ChangesFlags> transfers)
0094 {
0095     qCDebug(KGET_DEBUG);
0096 
0097     if (!Settings::enableKUIServerIntegration())
0098         return;
0099 
0100     QMapIterator<TransferHandler *, Transfer::ChangesFlags> i(transfers);
0101     while (i.hasNext()) {
0102         i.next();
0103         //         if(!m_invalidTransfers.contains(i.key()))
0104         {
0105             TransferHandler *transfer = i.key();
0106             if (shouldBeShown(transfer)) {
0107                 registerJob(transfer->kJobAdapter(), transfer);
0108             } else {
0109                 unregisterJob(transfer->kJobAdapter(), transfer);
0110             }
0111         }
0112     }
0113 
0114     if (shouldBeShown(nullptr)) {
0115         globalJob()->update();
0116         registerJob(globalJob(), nullptr);
0117     } else
0118         unregisterJob(globalJob(), nullptr);
0119 }
0120 
0121 void KUiServerJobs::registerJob(KGetKJobAdapter *job, TransferHandler *transfer)
0122 {
0123     if (m_registeredJobs.contains(transfer) || !job) {
0124         return;
0125     }
0126     connect(job, &KGetKJobAdapter::requestStop, this, &KUiServerJobs::slotRequestStop);
0127     connect(job, &KGetKJobAdapter::requestSuspend, this, &KUiServerJobs::slotRequestSuspend);
0128     connect(job, &KGetKJobAdapter::requestResume, this, &KUiServerJobs::slotRequestResume);
0129 
0130     KJob *j = job;
0131     registerJob(j, transfer);
0132 }
0133 
0134 void KUiServerJobs::registerJob(KJob *job, TransferHandler *transfer)
0135 {
0136     if (m_registeredJobs.contains(transfer) || !job)
0137         return;
0138 
0139     KIO::getJobTracker()->registerJob(job);
0140     m_registeredJobs[transfer] = job;
0141 }
0142 
0143 bool KUiServerJobs::unregisterJob(KJob *job, TransferHandler *transfer)
0144 {
0145     if (!m_registeredJobs.contains(transfer) || !job)
0146         return false;
0147 
0148     // Transfer should only be suspended, thus still show the job tracker
0149     if (m_suspendRequested.contains(transfer)) {
0150         m_suspendRequested.removeAll(transfer);
0151         return false;
0152     }
0153 
0154     // unregister the job if it was a single adaptor
0155     if (job != m_globalJob) {
0156         disconnect(job);
0157     }
0158     KIO::getJobTracker()->unregisterJob(m_registeredJobs[transfer]);
0159     m_registeredJobs.remove(transfer);
0160 
0161     return true;
0162 }
0163 
0164 void KUiServerJobs::slotRequestStop(KJob *job, TransferHandler *transfer)
0165 {
0166     if (unregisterJob(job, transfer)) {
0167         if (transfer) {
0168             transfer->stop();
0169         } else {
0170             foreach (TransferHandler *t, KGet::allTransfers()) {
0171                 t->stop();
0172             }
0173         }
0174     }
0175 }
0176 
0177 bool KUiServerJobs::shouldBeShown(TransferHandler *transfer)
0178 {
0179     if (!Settings::enableKUIServerIntegration())
0180         return false;
0181 
0182     if (Settings::exportGlobalJob() && (transfer == nullptr) && existRunningTransfers())
0183         return true;
0184 
0185     if (!Settings::exportGlobalJob() && (transfer) && (transfer->status() == Job::Running))
0186         return true;
0187 
0188     return false;
0189 }
0190 
0191 bool KUiServerJobs::existRunningTransfers()
0192 {
0193     foreach (TransferHandler *transfer, KGet::allTransfers()) {
0194         // if added to m_invalidTransfers it means that the job is about to be removed
0195         if ((transfer->status() == Job::Running) && !m_invalidTransfers.contains(transfer)) {
0196             return true;
0197         }
0198     }
0199 
0200     return false;
0201 }
0202 
0203 KGetGlobalJob *KUiServerJobs::globalJob()
0204 {
0205     if (!m_globalJob) {
0206         m_globalJob = new KGetGlobalJob();
0207         connect(m_globalJob, &KGetGlobalJob::requestStop, this, &KUiServerJobs::slotRequestStop);
0208     }
0209     return m_globalJob;
0210 }
0211 
0212 void KUiServerJobs::slotRequestSuspend(KJob *job, TransferHandler *transfer)
0213 {
0214     Q_UNUSED(job)
0215     if (transfer) {
0216         m_suspendRequested << transfer;
0217         transfer->stop();
0218     }
0219 }
0220 
0221 void KUiServerJobs::slotRequestResume(KJob *job, TransferHandler *transfer)
0222 {
0223     Q_UNUSED(job)
0224     if (transfer) {
0225         transfer->start();
0226     }
0227 }
0228 
0229 #include "moc_kuiserverjobs.cpp"