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 | |
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::(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(sender: Instance::obexManager(), signal: &ObexManager::sessionRemoved, context: this, slot: &ObexTransferPrivate::sessionRemoved); |
48 | } |
49 | |
50 | init(properties); |
51 | } |
52 | |
53 | void ObexTransferPrivate::(const QVariantMap &properties) |
54 | { |
55 | m_dbusProperties = new DBusProperties(Strings::orgBluezObex(), m_bluezTransfer->path(), DBusConnection::orgBluezObex(), this); |
56 | |
57 | connect(sender: m_dbusProperties, signal: &DBusProperties::PropertiesChanged, context: this, slot: &ObexTransferPrivate::propertiesChanged, type: Qt::QueuedConnection); |
58 | |
59 | // Init properties |
60 | m_status = stringToStatus(status: 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::(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::(const ObexSessionPtr &session) |
93 | { |
94 | if (!m_bluezTransfer->path().startsWith(s: 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(status: m_status); |
102 | } |
103 | } |
104 | |
105 | ObexTransfer::(const QString &path, const QVariantMap &properties) |
106 | : QObject() |
107 | , d(new ObexTransferPrivate(path, properties)) |
108 | { |
109 | } |
110 | |
111 | ObexTransfer::() = default; |
112 | |
113 | ObexTransferPtr ObexTransfer::() const |
114 | { |
115 | return d->q.toStrongRef(); |
116 | } |
117 | |
118 | QDBusObjectPath ObexTransfer::() const |
119 | { |
120 | return QDBusObjectPath(d->m_bluezTransfer->path()); |
121 | } |
122 | |
123 | ObexTransfer::Status ObexTransfer::() const |
124 | { |
125 | return d->m_status; |
126 | } |
127 | |
128 | QString ObexTransfer::() const |
129 | { |
130 | return d->m_name; |
131 | } |
132 | |
133 | QString ObexTransfer::() const |
134 | { |
135 | return d->m_type; |
136 | } |
137 | |
138 | quint64 ObexTransfer::() const |
139 | { |
140 | return d->m_time; |
141 | } |
142 | |
143 | quint64 ObexTransfer::() const |
144 | { |
145 | return d->m_size; |
146 | } |
147 | |
148 | quint64 ObexTransfer::() const |
149 | { |
150 | return d->m_transferred; |
151 | } |
152 | |
153 | QString ObexTransfer::() const |
154 | { |
155 | return d->m_fileName; |
156 | } |
157 | |
158 | bool ObexTransfer::() const |
159 | { |
160 | return d->m_suspendable; |
161 | } |
162 | |
163 | PendingCall *ObexTransfer::() |
164 | { |
165 | return new PendingCall(d->m_bluezTransfer->Cancel(), PendingCall::ReturnVoid, this); |
166 | } |
167 | |
168 | PendingCall *ObexTransfer::() |
169 | { |
170 | return new PendingCall(d->m_bluezTransfer->Suspend(), PendingCall::ReturnVoid, this); |
171 | } |
172 | |
173 | PendingCall *ObexTransfer::() |
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" |
182 | |