aboutsummaryrefslogtreecommitdiff
path: root/src/client/QXmppUserTuneManager.cpp
blob: 6cde4e90ce80ad41d40da2eaf99f51b6669dcf4e (plain) (blame)
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
// SPDX-FileCopyrightText: 2021 Linus Jahn <lnj@kaidan.im>
//
// SPDX-License-Identifier: LGPL-2.1-or-later

#include "QXmppUserTuneManager.h"

#include "QXmppConstants_p.h"
#include "QXmppFutureUtils_p.h"
#include "QXmppPubSubEvent.h"
#include "QXmppPubSubManager.h"
#include "QXmppTuneItem.h"

using namespace QXmpp::Private;

///
/// \class QXmppUserTuneManager
///
/// The QXmppUserTuneManager implements \xep{0118, User Tune}. You'll receive
/// tune updates from all presence subscriptions. You can publish tune
/// information on the user's account (publish()) and request tune information
/// from specific accounts (request()).
///
/// The manager needs to be added to the client first and also requires the
/// QXmppPubSubManager.
/// \code
/// QXmppClient client;
/// auto *pubSubManager = client.addNewExtension<QXmppPubSubManager>();
/// auto *tuneManager = client.addNewExtension<QXmppUserTuneManager>();
/// \endcode
///
/// \since QXmpp 1.5
///

///
/// \typedef QXmppUserTuneManager::TuneResult
///
/// Contains the User Tune information or an error.
///

///
/// \typedef QXmppUserTuneManager::PublishResult
///
/// Contains the ID of the published item on success or a stanza error.
///

///
/// \fn QXmppUserTuneManager::userTuneChanged()
///
/// Emitted whenever an \xep{0118, User Tune} items event arrives.
///

QXmppUserTuneManager::QXmppUserTuneManager()
{
}

QStringList QXmppUserTuneManager::discoveryFeatures() const
{
    return {
        ns_tune,
        ns_tune_notify,
    };
}

///
/// Request User Tune information from an account.
///
/// \param jid The account JID to request.
///
QFuture<QXmppUserTuneManager::TuneResult> QXmppUserTuneManager::request(const QString &jid)
{
    using PubSub = QXmppPubSubManager;
    using Error = QXmppStanza::Error;

    return chain<TuneResult>(pubSub()->requestItems<QXmppTuneItem>(jid, ns_tune), this,
                             [](PubSub::ItemsResult<QXmppTuneItem> &&result) -> TuneResult {
                                 if (const auto items = std::get_if<PubSub::Items<QXmppTuneItem>>(&result)) {
                                     if (!items->items.isEmpty()) {
                                         return items->items.constFirst();
                                     }
                                     return Error(Error::Cancel, Error::ItemNotFound, QStringLiteral("No tune available."));
                                 } else {
                                     return std::get<QXmppStanza::Error>(result);
                                 }
                             });
}

///
/// Publishes User Tune information on the user's account.
///
/// \param item The User Tune item to be published.
///
QFuture<QXmppUserTuneManager::PublishResult> QXmppUserTuneManager::publish(const QXmppTuneItem &item)
{
    return pubSub()->publishPepItem(ns_tune, item);
}

/// \cond
bool QXmppUserTuneManager::handlePubSubEvent(const QDomElement &element, const QString &pubSubService, const QString &nodeName)
{
    if (nodeName == ns_tune && QXmppPubSubEvent<QXmppTuneItem>::isPubSubEvent(element)) {
        QXmppPubSubEvent<QXmppTuneItem> event;
        event.parse(element);

        if (event.eventType() == QXmppPubSubEventBase::Items) {
            if (!event.items().isEmpty()) {
                emit userTuneChanged(pubSubService, event.items().constFirst());
            } else {
                emit userTuneChanged(pubSubService, {});
            }
            return true;
        }
    }
    return false;
}
/// \endcond