| 1 |
/* |
| 2 |
* BluezQt - Asynchronous Bluez wrapper library |
| 3 |
* |
| 4 |
* SPDX-FileCopyrightText: 2014 David Rosca <[email protected]> |
| 5 |
* |
| 6 |
* SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "obextransfer.h" |
| 10 |
#include "macros.h" |
| 11 |
#include "obexmanager.h" |
| 12 |
#include "obexsession.h" |
| 13 |
#include "obextransfer_p.h" |
| 14 |
#include "pendingcall.h" |
| 15 |
#include "utils.h" |
| 16 |
|
| 17 |
namespace BluezQt |
| 18 |
{ |
| 19 |
static ObexTransfer::Status stringToStatus(const QString &status) |
| 20 |
{ |
| 21 |
if (status == QLatin1String("queued")) { |
| 22 |
return ObexTransfer::Queued; |
| 23 |
} else if (status == QLatin1String("active")) { |
| 24 |
return ObexTransfer::Active; |
| 25 |
} else if (status == QLatin1String("suspended")) { |
| 26 |
return ObexTransfer::Suspended; |
| 27 |
} else if (status == QLatin1String("complete")) { |
| 28 |
return ObexTransfer::Complete; |
| 29 |
} else if (status == QLatin1String("error")) { |
| 30 |
return ObexTransfer::Error; |
| 31 |
} |
| 32 |
return ObexTransfer::Unknown; |
| 33 |
} |
| 34 |
|
| 35 |
ObexTransferPrivate::ObexTransferPrivate(const QString &path, const QVariantMap &properties) |
| 36 |
: QObject() |
| 37 |
, m_dbusProperties(nullptr) |
| 38 |
, m_status(ObexTransfer::Error) |
| 39 |
, m_time(0) |
| 40 |
, m_size(0) |
| 41 |
, m_transferred(0) |
| 42 |
, m_suspendable(false) |
| 43 |
{ |
| 44 |
m_bluezTransfer = new BluezTransfer(Strings::orgBluezObex(), path, DBusConnection::orgBluezObex(), this); |
| 45 |
|
| 46 |
if (Instance::obexManager()) { |
| 47 |
connect(Instance::obexManager(), &ObexManager::sessionRemoved, this, &ObexTransferPrivate::sessionRemoved); |
| 48 |
} |
| 49 |
|
| 50 |
init(properties); |
| 51 |
} |
| 52 |
|
| 53 |
void ObexTransferPrivate::init(const QVariantMap &properties) |
| 54 |
{ |
| 55 |
m_dbusProperties = new DBusProperties(Strings::orgBluezObex(), m_bluezTransfer->path(), DBusConnection::orgBluezObex(), this); |
| 56 |
|
| 57 |
connect(m_dbusProperties, &DBusProperties::PropertiesChanged, this, &ObexTransferPrivate::propertiesChanged, Qt::QueuedConnection); |
| 58 |
|
| 59 |
// Init properties |
| 60 |
m_status = stringToStatus(properties.value(QStringLiteral("Status")).toString()); |
| 61 |
m_name = properties.value(QStringLiteral("Name")).toString(); |
| 62 |
m_type = properties.value(QStringLiteral("Type")).toString(); |
| 63 |
m_time = properties.value(QStringLiteral("Time")).toUInt(); |
| 64 |
m_size = properties.value(QStringLiteral("Size")).toUInt(); |
| 65 |
m_transferred = properties.value(QStringLiteral("Transferred")).toUInt(); |
| 66 |
m_fileName = properties.value(QStringLiteral("Filename")).toString(); |
| 67 |
} |
| 68 |
|
| 69 |
void ObexTransferPrivate::propertiesChanged(const QString &interface, const QVariantMap &changed, const QStringList &invalidated) |
| 70 |
{ |
| 71 |
Q_UNUSED(invalidated) |
| 72 |
|
| 73 |
if (interface != Strings::orgBluezObexTransfer1()) { |
| 74 |
return; |
| 75 |
} |
| 76 |
|
| 77 |
QVariantMap::const_iterator i; |
| 78 |
for (i = changed.constBegin(); i != changed.constEnd(); ++i) { |
| 79 |
const QVariant &value = i.value(); |
| 80 |
const QString &property = i.key(); |
| 81 |
|
| 82 |
if (property == QLatin1String("Status")) { |
| 83 |
PROPERTY_CHANGED2(m_status, stringToStatus(value.toString()), statusChanged); |
| 84 |
} else if (property == QLatin1String("Transferred")) { |
| 85 |
PROPERTY_CHANGED(m_transferred, toUInt, transferredChanged); |
| 86 |
} else if (property == QLatin1String("Filename")) { |
| 87 |
PROPERTY_CHANGED(m_fileName, toString, fileNameChanged); |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
void ObexTransferPrivate::sessionRemoved(const ObexSessionPtr &session) |
| 93 |
{ |
| 94 |
if (!m_bluezTransfer->path().startsWith(session->objectPath().path())) { |
| 95 |
return; |
| 96 |
} |
| 97 |
|
| 98 |
// Change status to Error if org.bluez.obex crashes |
| 99 |
if (m_status != ObexTransfer::Complete && m_status != ObexTransfer::Error) { |
| 100 |
m_status = ObexTransfer::Error; |
| 101 |
Q_EMIT q.lock()->statusChanged(m_status); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
ObexTransfer::ObexTransfer(const QString &path, const QVariantMap &properties) |
| 106 |
: QObject() |
| 107 |
, d(new ObexTransferPrivate(path, properties)) |
| 108 |
{ |
| 109 |
} |
| 110 |
|
| 111 |
ObexTransfer::~ObexTransfer() = default; |
| 112 |
|
| 113 |
ObexTransferPtr ObexTransfer::toSharedPtr() const |
| 114 |
{ |
| 115 |
return d->q.toStrongRef(); |
| 116 |
} |
| 117 |
|
| 118 |
QDBusObjectPath ObexTransfer::objectPath() const |
| 119 |
{ |
| 120 |
return QDBusObjectPath(d->m_bluezTransfer->path()); |
| 121 |
} |
| 122 |
|
| 123 |
ObexTransfer::Status ObexTransfer::status() const |
| 124 |
{ |
| 125 |
return d->m_status; |
| 126 |
} |
| 127 |
|
| 128 |
QString ObexTransfer::name() const |
| 129 |
{ |
| 130 |
return d->m_name; |
| 131 |
} |
| 132 |
|
| 133 |
QString ObexTransfer::type() const |
| 134 |
{ |
| 135 |
return d->m_type; |
| 136 |
} |
| 137 |
|
| 138 |
quint64 ObexTransfer::time() const |
| 139 |
{ |
| 140 |
return d->m_time; |
| 141 |
} |
| 142 |
|
| 143 |
quint64 ObexTransfer::size() const |
| 144 |
{ |
| 145 |
return d->m_size; |
| 146 |
} |
| 147 |
|
| 148 |
quint64 ObexTransfer::transferred() const |
| 149 |
{ |
| 150 |
return d->m_transferred; |
| 151 |
} |
| 152 |
|
| 153 |
QString ObexTransfer::fileName() const |
| 154 |
{ |
| 155 |
return d->m_fileName; |
| 156 |
} |
| 157 |
|
| 158 |
bool ObexTransfer::isSuspendable() const |
| 159 |
{ |
| 160 |
return d->m_suspendable; |
| 161 |
} |
| 162 |
|
| 163 |
PendingCall *ObexTransfer::cancel() |
| 164 |
{ |
| 165 |
return new PendingCall(d->m_bluezTransfer->Cancel(), PendingCall::ReturnVoid, this); |
| 166 |
} |
| 167 |
|
| 168 |
PendingCall *ObexTransfer::suspend() |
| 169 |
{ |
| 170 |
return new PendingCall(d->m_bluezTransfer->Suspend(), PendingCall::ReturnVoid, this); |
| 171 |
} |
| 172 |
|
| 173 |
PendingCall *ObexTransfer::resume() |
| 174 |
{ |
| 175 |
return new PendingCall(d->m_bluezTransfer->Resume(), PendingCall::ReturnVoid, this); |
| 176 |
} |
| 177 |
|
| 178 |
} // namespace BluezQt |
| 179 |
|
| 180 |
#include "moc_obextransfer.cpp" |
| 181 |
#include "moc_obextransfer_p.cpp" |