BluezQt

obextransfer.cpp
1/*
2 * BluezQt - Asynchronous Bluez wrapper library
3 *
4 * SPDX-FileCopyrightText: 2014 David Rosca <nowrep@gmail.com>
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
17namespace BluezQt
18{
19static ObexTransfer::Status stringToStatus(const QString &status)
20{
21 if (status == QLatin1String("queued")) {
23 } else if (status == QLatin1String("active")) {
25 } else if (status == QLatin1String("suspended")) {
27 } else if (status == QLatin1String("complete")) {
29 } else if (status == QLatin1String("error")) {
31 }
33}
34
35ObexTransferPrivate::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
53void 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
69void 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
92void 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
105ObexTransfer::ObexTransfer(const QString &path, const QVariantMap &properties)
106 : QObject()
107 , d(new ObexTransferPrivate(path, properties))
108{
109}
110
111ObexTransfer::~ObexTransfer() = default;
112
113ObexTransferPtr ObexTransfer::toSharedPtr() const
114{
115 return d->q.toStrongRef();
116}
117
118QDBusObjectPath ObexTransfer::objectPath() const
119{
120 return QDBusObjectPath(d->m_bluezTransfer->path());
121}
122
123ObexTransfer::Status ObexTransfer::status() const
124{
125 return d->m_status;
126}
127
128QString ObexTransfer::name() const
129{
130 return d->m_name;
131}
132
133QString ObexTransfer::type() const
134{
135 return d->m_type;
136}
137
138quint64 ObexTransfer::time() const
139{
140 return d->m_time;
141}
142
143quint64 ObexTransfer::size() const
144{
145 return d->m_size;
146}
147
148quint64 ObexTransfer::transferred() const
149{
150 return d->m_transferred;
151}
152
153QString ObexTransfer::fileName() const
154{
155 return d->m_fileName;
156}
157
158bool ObexTransfer::isSuspendable() const
159{
160 return d->m_suspendable;
161}
162
163PendingCall *ObexTransfer::cancel()
164{
165 return new PendingCall(d->m_bluezTransfer->Cancel(), PendingCall::ReturnVoid, this);
166}
167
168PendingCall *ObexTransfer::suspend()
169{
170 return new PendingCall(d->m_bluezTransfer->Suspend(), PendingCall::ReturnVoid, this);
171}
172
173PendingCall *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"
Status
Status of transfer.
@ Unknown
Indicates that the transfer status is unknown.
@ Error
Indicates that the transfer have failed with error.
@ Active
Indicates that the transfer is active.
@ Suspended
Indicates that the transfer is suspended.
@ Queued
Indicates that the transfer is queued.
@ Complete
Indicates that the transfer have completed successfully.
Pending method call.
Q_SCRIPTABLE CaptureState status()
char * toString(const EngineQuery &query)
QString path(const QString &relativePath)
KGuiItem properties()
QCA_EXPORT void init()
QueuedConnection
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
QString toString() const const
T value() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:08:46 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.