summaryrefslogtreecommitdiffstats
path: root/src/ifmedia/qifmediaindexercontrol.cpp
blob: 9eeb86b4d604e084cf51a94d64e70cc67c0d02f0 (plain)
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
63
64
65
66
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
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
// Copyright (C) 2021 The Qt Company Ltd.
// Copyright (C) 2019 Luxoft Sweden AB
// Copyright (C) 2018 Pelagicore AG
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "qifmediaindexercontrol.h"
#include "qifmediaindexercontrol_p.h"

#include <QIfServiceObject>
#include <QtDebug>
#include <QDataStream>
#include <QMetaEnum>

using namespace Qt::StringLiterals;

QT_BEGIN_NAMESPACE

QIfMediaIndexerControlPrivate::QIfMediaIndexerControlPrivate(const QString &interface, QIfMediaIndexerControl *parent)
    : QIfAbstractFeaturePrivate(interface, parent)
    , q_ptr(parent)
    , m_progress(0)
    , m_state(QIfMediaIndexerControl::Idle)
{
}

void QIfMediaIndexerControlPrivate::clearToDefaults()
{
    onProgressChanged(0);
    onStateChanged(QIfMediaIndexerControl::Idle);
}

void QIfMediaIndexerControlPrivate::onProgressChanged(qreal progress)
{
    if (qFuzzyCompare(m_progress, progress))
        return;

    Q_Q(QIfMediaIndexerControl);
    m_progress = progress;
    emit q->progressChanged(progress);
}

void QIfMediaIndexerControlPrivate::onStateChanged(QIfMediaIndexerControl::State state)
{
    if (m_state == state)
        return;

    Q_Q(QIfMediaIndexerControl);
    m_state = state;
    emit q->stateChanged(state);
}

QIfMediaIndexerControlBackendInterface *QIfMediaIndexerControlPrivate::indexerBackend() const
{
    return backend<QIfMediaIndexerControlBackendInterface*>();
}

/*!
    \class QIfMediaIndexerControl
    \inmodule QtIfMedia
    \brief Provides an interface to control the media indexer.

    The QIfMediaIndexerControl controls the media indexer. It provides a way to temporarily pause the indexing
    and resume it, as well as to inquire about the current state and progress of the indexing operation.

    The QIfMediaIndexerControl expects a single backend to be available. It is recommended to use it
    with \l {QIfAbstractFeature::}{discoveryMode} set to \l QIfAbstractFeature::AutoDiscovery.

    Uses \c qtifmedia as configurationId for \l QIfConfiguration based settings.
*/

/*!
    \qmltype MediaIndexerControl
    \instantiates QIfMediaIndexerControl
    \inqmlmodule QtInterfaceFramework.Media
    \inherits AbstractFeature
    \brief Provides an interface to control the media indexer.

    The MediaIndexerControl controls the media indexer. It provides a way to temporarily pause the indexing
    and resume it, as well as to inquire about the current state and progress of the indexing operation.

    The MediaIndexerControl expects a single backend to be available. It is recommended to use it
    with \l {AbstractFeature::}{discoveryMode} set to AbstractFeature.AutoDiscovery.

    Uses \c qtifmedia as configurationId for \l InterfaceFrameworkConfiguration based settings.
*/

/*!
    \enum QIfMediaIndexerControl::State
    \value Idle
           The indexer is currently idle and is waiting for new files to be indexed.
    \value Active
           The indexer is currently busy with indexing.
    \value Paused
           The indexer is paused, due to a call to pause().
    \value Error
           An error has occurred during the indexing operation.
*/

/*!
    Constructs a QIfMediaIndexerControl.

    The \a parent argument is passed on to the \l QIfAbstractFeature base class.
*/
QIfMediaIndexerControl::QIfMediaIndexerControl(QObject *parent)
    : QIfAbstractFeature(*new QIfMediaIndexerControlPrivate(QLatin1String(QIfMediaIndexer_iid), this), parent)
{
    qRegisterMetaType<QIfMediaIndexerControl::State>();
    setConfigurationId(u"qtifmedia"_s);
}

/*!
    \qmlproperty real MediaIndexerControl::progress
    \brief Holds the progress of the indexing operation.

    The value is between \e 0 and \e 1.
*/
/*!
    \property QIfMediaIndexerControl::progress
    \brief Holds the progress of the indexing operation.

    The value is between \e 0 and \e 1.
*/
qreal QIfMediaIndexerControl::progress() const
{
    Q_D(const QIfMediaIndexerControl);
    return d->m_progress;
}

/*!
    \qmlproperty enumeration MediaIndexerControl::state
    \brief Holds the current state of the indexer.

    It can be one of the following values:
    \value Idle
           The indexer is currently idle and is waiting for new files to be indexed.
    \value Active
           The indexer is currently busy with indexing.
    \value Paused
           The indexer is paused, due to a call to pause().
    \value Error
           An error has occurred during the indexing operation.
*/
/*!
    \property QIfMediaIndexerControl::state
    \brief Holds the current state of the indexer.
*/
QIfMediaIndexerControl::State QIfMediaIndexerControl::state() const
{
    Q_D(const QIfMediaIndexerControl);
    return d->m_state;
}

/*!
    \qmlmethod MediaIndexerControl::pause()

    Pauses the currently ongoing indexing operation.

    \sa resume() state
*/

/*!
    \fn void QIfMediaIndexerControl::pause()

    Pauses the currently ongoing indexing operation.

    \sa resume() state
*/
void QIfMediaIndexerControl::pause()
{
    Q_IF_BACKEND(QIfMediaIndexerControl, d->indexerBackend(), "Can't pause without a connected backend");

    backend->pause();
}

/*!
    \qmlmethod MediaIndexerControl::resume()

    Resumes from the Paused state and resumes the indexing operation.

    \sa pause() state
*/

/*!
    \fn void QIfMediaIndexerControl::resume()

    Resumes from the Paused state and resumes the indexing operation.

    \sa pause() state
*/
void QIfMediaIndexerControl::resume()
{
    Q_IF_BACKEND(QIfMediaIndexerControl, d->indexerBackend(), "Can't resume without a connected backend");

    backend->resume();
}

/*!
    \internal
*/
QIfMediaIndexerControl::QIfMediaIndexerControl(QIfMediaIndexerControlPrivate &dd, QObject *parent)
    : QIfAbstractFeature(dd, parent)
{
}

/*!
    \reimp
*/
void QIfMediaIndexerControl::connectToServiceObject(QIfServiceObject *serviceObject)
{
    Q_UNUSED(serviceObject);

    Q_D(QIfMediaIndexerControl);

    QIfMediaIndexerControlBackendInterface *backend = d->indexerBackend();
    if (!backend)
        return;

    QObjectPrivate::connect(backend, &QIfMediaIndexerControlBackendInterface::progressChanged,
                            d, &QIfMediaIndexerControlPrivate::onProgressChanged);
    QObjectPrivate::connect(backend, &QIfMediaIndexerControlBackendInterface::stateChanged,
                            d, &QIfMediaIndexerControlPrivate::onStateChanged);

    QIfAbstractFeature::connectToServiceObject(serviceObject);

    backend->initialize();
}

/*!
    \reimp
*/
void QIfMediaIndexerControl::clearServiceObject()
{
    Q_D(QIfMediaIndexerControl);
    d->clearToDefaults();
}

QDataStream &operator <<(QDataStream &out, QIfMediaIndexerControl::State var)
{
    out << int(var);
    return out;
}

QDataStream &operator>>(QDataStream &in, QIfMediaIndexerControl::State &var)
{
    int val;
    in >> val;
    QMetaEnum metaEnum = QMetaEnum::fromType<QIfMediaIndexerControl::State>();
    if (metaEnum.valueToKey(val) == nullptr)
        qWarning() << "Received an invalid enum value for type QIfMediaIndexerControl::State, value =" << val;
    var = QIfMediaIndexerControl::State(val);
    return in;
}

QT_END_NAMESPACE

#include "moc_qifmediaindexercontrol.cpp"