Skip to content

Latest commit

 

History

History
289 lines (226 loc) · 8.61 KB

mediatransferinterface.cpp

File metadata and controls

289 lines (226 loc) · 8.61 KB
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/****************************************************************************************
**
** Copyright (C) 2013 Jolla Ltd.
** Contact: Marko Mattila <marko.mattila@jollamobile.com>
** All rights reserved.
**
** This file is part of Nemo Transfer Engine package.
**
** You may use this file under the terms of the GNU Lesser General
** Public License version 2.1 as published by the Free Software Foundation
** and appearing in the file license.lgpl included in the packaging
** of this file.
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation
** and appearing in the file license.lgpl included in the packaging
** of this file.
**
** This library is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
** Lesser General Public License for more details.
**
****************************************************************************************/
#include "mediatransferinterface.h"
#include "mediaitem.h"
#include <QtDebug>
// Update progress (to dbus) only in every 5% progress changes
#define PROGRESS_THRESHOLD 0.05
class MediaTransferInterfacePrivate
{
public:
MediaTransferInterfacePrivate():
m_mediaItem(0),
m_status(MediaTransferInterface::NotStarted),
m_progress(0),
m_prevProgress(0)
{}
MediaItem *m_mediaItem;
MediaTransferInterface::TransferStatus m_status;
qreal m_progress;
qreal m_prevProgress;
};
/*!
\class MediaTransferInterface
\brief The MediaTransferInterface class is responsible of implementing
\ingroup transfer-engine-lib
MediaTransferInterface is an abstract class which must be implemented by
each share plugin. The subclass of this class is resposible of providing:
\list
Jan 17, 2018
Jan 17, 2018
63
64
65
66
\li Status information of the sharing
\li Progress of the sharing
\li Information, if cancel and restart actions are supported
\li Starting and/or canceling the sharing
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
\endlist
The flow:
The instance of this class is created when SailfishShare::start() method is
called.
*/
/*!
\enum MediaTransferInterface::TransferStatus
Transfers status
\value NotStarted Transfer not started
\value TransferStarted Transfer is ongoing
\value TransferCanceled Transfer is canceled usually via user actions
\value TransferFinished Transfer is finished successfully
\value TransferInterrupted Transfer failed
*/
/*!
\fn virtual QString MediaTransferInterface::displayName() const = 0
Subclass must implement this method. Return a human readable display
name for the service e.g. "Facebook".
*/
/*!
\fn virtual QUrl MediaTransferInterface::serviceIcon() const = 0
Subclass must implement this method. Return the URL to the service
icon. Service icon can be e.g. Facebook or BT icon.
*/
/*!
\fn virtual bool MediaTransferInterface::cancelEnabled() const = 0
Subclass must implement this method. Return true if cancel is supported
or false otherwise.
*/
/*!
\fn virtual bool MediaTransferInterface::restartEnabled() const = 0
Subclass must implement this method. Return true if restart is supported
or false otherwise.
*/
/*!
\fn virtual void MediaTransferInterface::start() = 0
Subclass must implement this to start the actual sharing operation. This
same method is called when sharing is restarted.
*/
/*!
\fn virtual void MediaTransferInterface::cancel() = 0
Subclass must implement this to cancel the ongoing share operation.
*/
/*!
\fn void MediaTransferInterface::statusChanged(MediaTransferInterface::TransferStatus status)
This signal with changed \a status is emitted when setStatus() is called.
Note that this signal is NOT meant to be emitted by the subclass. Instead of
emitting it manually the subclass should use setStatus() method, which causes
this signal to be emitted automatically.
*/
/*!
\fn void MediaTransferInterface::progressUpdated(qreal progress)
This signal is emitted when the \a progress changes. Progress is changed when the
subclass calls setProgress() method.
Note that, like the statusChanged() signal, also this signal is not to meant to be
emitted by the subclass. It's emitted automatically when subclass calls setProgress().
*/
/*!
Construct MediaTransferInterface object with optional \a parent object.
*/
MediaTransferInterface::MediaTransferInterface(QObject *parent):
QObject(parent),
d_ptr(new MediaTransferInterfacePrivate)
Nov 29, 2018
Nov 29, 2018
159
160
{
}
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
MediaTransferInterface::~MediaTransferInterface()
{
delete d_ptr;
d_ptr = 0;
}
/*!
Set MediaItem for this object. MediaItem is a container for all the information related
to the \a mediaItem (image, video, vcard,..) which is going to be shared. This information
must contain e.g. url to the media item. For more details all the available values,
see MediaItem::ValueKey.
NOTE: That the values set to this media item depends on the related Share UI. For example
BT share UI won't provide information related to the accounts because "accountId" or "description"
are not required for the Bluetooh. But then again Facebook share UI provides this information.
This means that because plugin provides the Share UI, it knows which fields are set for the
MediaItem instance.
MediaItem is passed to this item by TransferEngine.
*/
void MediaTransferInterface::setMediaItem(MediaItem *mediaItem)
{
Q_D(MediaTransferInterface);
if (d->m_mediaItem) {
qWarning() << "MediaTransferInterface::setMediaItem: MediaItem is not null. Old media item will be deleted!";
delete d->m_mediaItem;
}
d->m_mediaItem = mediaItem;
d->m_mediaItem->setParent(this);
}
/*!
Return the MediaItem instance or 0, if it hasn't been set.
*/
MediaItem *MediaTransferInterface::mediaItem()
{
Q_D(MediaTransferInterface);
return d->m_mediaItem;
}
/*!
Returns the status of the media transfer.
*/
MediaTransferInterface::TransferStatus MediaTransferInterface::status() const
{
Q_D(const MediaTransferInterface);
return d->m_status;
}
/*!
Returns the current progress of the ongoing media transfer.
*/
qreal MediaTransferInterface::progress() const
{
Q_D(const MediaTransferInterface);
return d->m_progress;
}
/*!
Sets the \a status for the media transfer. Note that this method also
sets the progress if status changes to MediaTransferInterface::TransferFinished.
If status changes to MediaTransferInterface::TransferCanceled or
MediaTransferInterface::TransferInterrupted, the progress is set to 0.
This method emits statusChanged() and progressUpdated() signals automatically based
on status changes and if this method marks progress to 1 or to 0.
*/
void MediaTransferInterface::setStatus(TransferStatus status)
{
Q_D(MediaTransferInterface);
// Make sure that progress is 1 if we are really finished
if (status == MediaTransferInterface::TransferFinished &&
d->m_prevProgress < 1 ) {
d->m_progress = 1;
d->m_prevProgress = 1;
emit progressUpdated(1);
}
// Make sure that progress is set to 0 if transfer is canceled or
// interrupted
if (status == MediaTransferInterface::TransferCanceled ||
status == MediaTransferInterface::TransferInterrupted) {
d->m_progress = 0;
d->m_prevProgress = 0;
emit progressUpdated(0);
}
// And update the status
if (d->m_status != status) {
d->m_status = status;
emit statusChanged(d->m_status);
}
}
/*!
Set the \a progress for the on going media transfer.
Note that progressUpdated() signal might not be emitted in every call of this method
because it might pollute dbus if progress changes too many times.
*/
void MediaTransferInterface::setProgress(qreal progress)
{
Q_D(MediaTransferInterface);
if (progress < 0 || 1 < progress) {
qWarning() << "MediaTransferInterface::setProgress: progress must be in between 0 and 1";
// Just show the warning and let the progress update. Sometimes there might be some
// decimals which may not match exactly with 1, like 1.0001
}
if (d->m_progress == progress) {
return;
}
d->m_progress = progress;
// To avoid dbus overload let's not emit this signal in every progress change
if (qAbs(d->m_progress - d->m_prevProgress) >= PROGRESS_THRESHOLD) {
emit progressUpdated(progress);
d->m_prevProgress = progress;
}
}