~phablet-team/telephony-service/trunk

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
/*
 * Copyright (C) 2012-2016 Canonical, Ltd.
 *
 * Authors:
 *  Tiago Salem Herrmann <tiago.herrmann@canonical.com>
 *  Gustavo Pichorim Boiko <gustavo.boiko@canonical.com>
 *
 * This file is part of telephony-service.
 *
 * telephony-service is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 3.
 *
 * telephony-service 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef TELEPATHYHELPER_H
#define TELEPATHYHELPER_H

#include <QObject>
#include <QQmlListProperty>
#include <TelepathyQt/AccountManager>
#include <TelepathyQt/Contact>
#include <TelepathyQt/Connection>
#include <TelepathyQt/ConnectionManager>
#include <TelepathyQt/Types>
#include "channelobserver.h"
#include "accountentry.h"
#include "protocol.h"

#define CANONICAL_TELEPHONY_VOICEMAIL_IFACE "com.canonical.Telephony.Voicemail"
#define CANONICAL_TELEPHONY_AUDIOOUTPUTS_IFACE "com.canonical.Telephony.AudioOutputs"
#define CANONICAL_TELEPHONY_USSD_IFACE "com.canonical.Telephony.USSD"
#define CANONICAL_TELEPHONY_EMERGENCYMODE_IFACE "com.canonical.Telephony.EmergencyMode"

template<> bool qMapLessThanKey<QStringList>(const QStringList &key1, const QStringList &key2);

class AccountList;

class TelepathyHelper : public QObject
{
    Q_OBJECT
    Q_PROPERTY(bool ready READ ready NOTIFY setupReady)
    Q_PROPERTY(QStringList accountIds READ accountIds NOTIFY accountIdsChanged)
    Q_PROPERTY(AccountList *accounts READ qmlAccounts CONSTANT)
    Q_PROPERTY(AccountList *voiceAccounts READ qmlVoiceAccounts CONSTANT)
    Q_PROPERTY(AccountList *textAccounts READ qmlTextAccounts CONSTANT)
    Q_PROPERTY(AccountList *phoneAccounts READ qmlPhoneAccounts CONSTANT)
    Q_PROPERTY(AccountEntry *defaultMessagingAccount READ defaultMessagingAccount NOTIFY defaultMessagingAccountChanged)
    Q_PROPERTY(AccountEntry *defaultCallAccount READ defaultCallAccount NOTIFY defaultCallAccountChanged)
    Q_PROPERTY(bool flightMode READ flightMode WRITE setFlightMode NOTIFY flightModeChanged)
    Q_PROPERTY(bool mmsEnabled READ mmsEnabled WRITE setMmsEnabled NOTIFY mmsEnabledChanged)
    Q_PROPERTY(bool emergencyCallsAvailable READ emergencyCallsAvailable NOTIFY emergencyCallsAvailableChanged)
    Q_PROPERTY(QVariantMap simNames READ simNames NOTIFY simNamesChanged)
    Q_ENUMS(AccountType)
    Q_ENUMS(ChatType)
public:
    enum AccountType {
        Messaging = Protocol::TextChats,
        Voice = Protocol::VoiceCalls,
    };
    Q_DECLARE_FLAGS(AccountTypes, AccountType)

    enum ChatType {
        ChatTypeNone = Tp::HandleTypeNone,
        ChatTypeContact = Tp::HandleTypeContact,
        ChatTypeRoom = Tp::HandleTypeRoom
    };

    ~TelepathyHelper();

    static TelepathyHelper *instance();

    /********************************* Account stuff *********************************/
    QList<AccountEntry*> accounts() const;
    QList<AccountEntry*> phoneAccounts() const;
    QList<AccountEntry*> activeAccounts() const;
    AccountList *qmlAccounts() const;
    AccountList *qmlVoiceAccounts() const;
    AccountList *qmlTextAccounts() const;
    AccountList *qmlPhoneAccounts() const;
    ChannelObserver *channelObserver() const;
    QDBusInterface *handlerInterface() const;
    QDBusInterface *approverInterface() const;
    AccountEntry *defaultMessagingAccount() const;
    AccountEntry *defaultCallAccount() const;
    AccountEntry *accountForConnection(const Tp::ConnectionPtr &connection) const;
    Q_INVOKABLE AccountEntry *accountForId(const QString &accountId) const;
    Q_INVOKABLE void setDefaultAccount(AccountType type, AccountEntry* account);
    Q_INVOKABLE QList<AccountEntry*> accountsForType(int type);
    bool multiplePhoneAccounts() const;

    /** @brief Check if this account should be replaced by any overloaded protocol. */
    QList<AccountEntry*> checkAccountOverload(AccountEntry *originalAccount);

    /** @brief Check if this account has a fallback to be used when the original account is not suitable. */
    QList<AccountEntry*> checkAccountFallback(AccountEntry *originalAccount);

    /* Convenience functions to be used by qml */
    Q_INVOKABLE QList<QObject*> accountOverload(AccountEntry *originalAccount);
    Q_INVOKABLE QList<QObject*> accountFallback(AccountEntry *originalAccount);

    bool mmsEnabled();
    QVariantMap simNames() const;
    void setMmsEnabled(bool value);
    bool flightMode();
    void setFlightMode(bool value);
    bool ready() const;
    QStringList accountIds();
    bool emergencyCallsAvailable() const;
    Q_INVOKABLE void unlockSimCards() const;

    bool registerClient(Tp::AbstractClient *client, QString name);
    bool unregisterClient(Tp::AbstractClient *client);

    // pre-populated channel class specs for conferences
    static Tp::ChannelClassSpec audioConferenceSpec();

Q_SIGNALS:
    void channelObserverCreated(ChannelObserver *observer);
    void channelObserverUnregistered();
    void accountIdsChanged();
    void accountsChanged();
    void accountAdded(AccountEntry *account);
    void phoneAccountsChanged();
    void activeAccountsChanged();
    void setupReady();
    void defaultMessagingAccountChanged();
    void defaultCallAccountChanged();
    void flightModeChanged();
    void emergencyCallsAvailableChanged();
    void mmsEnabledChanged();
    void simNamesChanged();

public Q_SLOTS:
    Q_INVOKABLE void registerChannelObserver(const QString &observerName = QString::null);
    Q_INVOKABLE void unregisterChannelObserver();

protected:
    explicit TelepathyHelper(QObject *parent = 0);
    void setupAccountEntry(AccountEntry *entry);

private Q_SLOTS:
    void onAccountManagerReady(Tp::PendingOperation *op);
    void onAccountReady();
    void onNewAccount(const Tp::AccountPtr &account);
    void onAccountRemoved();
    void onPhoneSettingsChanged(const QString&);

private:
    Tp::AccountManagerPtr mAccountManager;
    Tp::Features mAccountManagerFeatures;
    Tp::Features mAccountFeatures;
    Tp::Features mContactFeatures;
    Tp::Features mConnectionFeatures;
    Tp::ClientRegistrarPtr mClientRegistrar;
    QList<AccountEntry*> mAccounts;
    int mPendingAccountReady;
    AccountList *mQmlAccounts;
    AccountList *mQmlVoiceAccounts;
    AccountList *mQmlTextAccounts;
    AccountList *mQmlPhoneAccounts;
    AccountEntry *mDefaultCallAccount;
    AccountEntry *mDefaultMessagingAccount;
    ChannelObserver *mChannelObserver;
    bool mReady;
    Tp::AbstractClientPtr mChannelObserverPtr;
    bool mMmsEnabled;
    QVariantMap mSimNames;
    mutable QDBusInterface *mHandlerInterface;
    mutable QDBusInterface *mApproverInterface;
    QDBusInterface mFlightModeInterface;
};

Q_DECLARE_OPERATORS_FOR_FLAGS(TelepathyHelper::AccountTypes)

#endif // TELEPATHYHELPER_H