BluezQt

obextransfer.cpp
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")) {
27  } else if (status == QLatin1String("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 
112 {
113  delete d;
114 }
115 
117 {
118  return d->q.toStrongRef();
119 }
120 
122 {
123  return QDBusObjectPath(d->m_bluezTransfer->path());
124 }
125 
126 ObexTransfer::Status ObexTransfer::status() const
127 {
128  return d->m_status;
129 }
130 
131 QString ObexTransfer::name() const
132 {
133  return d->m_name;
134 }
135 
136 QString ObexTransfer::type() const
137 {
138  return d->m_type;
139 }
140 
141 quint64 ObexTransfer::time() const
142 {
143  return d->m_time;
144 }
145 
146 quint64 ObexTransfer::size() const
147 {
148  return d->m_size;
149 }
150 
151 quint64 ObexTransfer::transferred() const
152 {
153  return d->m_transferred;
154 }
155 
156 QString ObexTransfer::fileName() const
157 {
158  return d->m_fileName;
159 }
160 
162 {
163  return d->m_suspendable;
164 }
165 
167 {
168  return new PendingCall(d->m_bluezTransfer->Cancel(), PendingCall::ReturnVoid, this);
169 }
170 
172 {
173  return new PendingCall(d->m_bluezTransfer->Suspend(), PendingCall::ReturnVoid, this);
174 }
175 
177 {
178  return new PendingCall(d->m_bluezTransfer->Resume(), PendingCall::ReturnVoid, this);
179 }
180 
181 } // namespace BluezQt
@ Active
Indicates that the transfer is active.
Definition: obextransfer.h:51
QCA_EXPORT void init()
T value() const const
@ Suspended
Indicates that the transfer is suspended.
Definition: obextransfer.h:53
QDBusObjectPath objectPath() const
D-Bus object path of the transfer.
KGuiItem properties()
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
ObexTransferPtr toSharedPtr() const
Returns a shared pointer from this.
PendingCall * cancel()
Stops the current transfer.
@ Complete
Indicates that the transfer have completed successfully.
Definition: obextransfer.h:55
void sessionRemoved(ObexSessionPtr session)
Indicates that the session was removed.
char * toString(const T &value)
@ Error
Indicates that the transfer have failed with error.
Definition: obextransfer.h:57
QueuedConnection
bool isSuspendable() const
Returns whether the transfer is suspendable.
Status
Status of transfer.
Definition: obextransfer.h:47
@ Unknown
Indicates that the transfer status is unknown.
Definition: obextransfer.h:59
@ Queued
Indicates that the transfer is queued.
Definition: obextransfer.h:49
PendingCall * resume()
Resumes the current transfer.
PendingCall * suspend()
Suspends the current transfer.
~ObexTransfer() override
Destroys an ObexTransfer object.
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sun Sep 25 2022 04:19:10 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.