File indexing completed on 2024-04-14 05:19:45

0001 /*
0002  *  SPDX-FileCopyrightText: 2013 Alejandro Fiestas Fiestas <afiestas@kde.org>
0003  *  SPDX-FileCopyrightText: 2014-2015 David Rosca <nowrep@gmail.com>
0004  *
0005  *  SPDX-License-Identifier: GPL-2.0-or-later
0006  */
0007 
0008 #include "obexagent.h"
0009 #include "bluedevil_kded.h"
0010 #include "bluedevildaemon.h"
0011 #include "filereceiversettings.h"
0012 #include "receivefilejob.h"
0013 
0014 #include <QDBusObjectPath>
0015 
0016 ObexAgent::ObexAgent(BlueDevilDaemon *daemon)
0017     : BluezQt::ObexAgent(daemon)
0018     , m_manager(daemon->manager())
0019 {
0020 }
0021 
0022 BluezQt::Manager *ObexAgent::manager() const
0023 {
0024     return m_manager;
0025 }
0026 
0027 bool ObexAgent::shouldAutoAcceptTransfer(const QString &address) const
0028 {
0029     if (!m_transferTimes.contains(address)) {
0030         return false;
0031     }
0032 
0033     // Auto-accept transfers from the same device within 2 seconds from last finished transfer
0034     const int timeout = 2;
0035     return m_transferTimes.value(address).secsTo(QDateTime::currentDateTime()) < timeout;
0036 }
0037 
0038 QDBusObjectPath ObexAgent::objectPath() const
0039 {
0040     return QDBusObjectPath(QStringLiteral("/modules/bluedevil/ObexAgent"));
0041 }
0042 
0043 void ObexAgent::authorizePush(BluezQt::ObexTransferPtr transfer, BluezQt::ObexSessionPtr session, const BluezQt::Request<QString> &request)
0044 {
0045     qCDebug(BLUEDEVIL_KDED_LOG) << "ObexAgent-AuthorizePush";
0046 
0047     FileReceiverSettings::self()->load();
0048 
0049     ReceiveFileJob *job = new ReceiveFileJob(request, transfer, session, this);
0050     connect(job, &ReceiveFileJob::finished, this, &ObexAgent::receiveFileJobFinished);
0051     job->start();
0052 }
0053 
0054 void ObexAgent::receiveFileJobFinished(KJob *job)
0055 {
0056     Q_ASSERT(qobject_cast<ReceiveFileJob *>(job));
0057     ReceiveFileJob *j = static_cast<ReceiveFileJob *>(job);
0058 
0059     if (j->error()) {
0060         m_transferTimes.remove(j->deviceAddress());
0061         return;
0062     }
0063 
0064     m_transferTimes[j->deviceAddress()] = QDateTime::currentDateTime();
0065 }
0066 
0067 #include "moc_obexagent.cpp"