File indexing completed on 2023-10-01 08:41:44

0001 /*
0002  * Global Presence - wraps calls to set and get presence for all accounts.
0003  *
0004  * Copyright (C) 2011 David Edmundson <kde@davidedmundson.co.uk>
0005  *
0006  * This library is free software; you can redistribute it and/or
0007  * modify it under the terms of the GNU Lesser General Public
0008  * License as published by the Free Software Foundation; either
0009  * version 2.1 of the License, or (at your option) any later version.
0010  *
0011  * This library is distributed in the hope that it will be useful,
0012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0014  * Lesser General Public License for more details.
0015  *
0016  * You should have received a copy of the GNU Lesser General Public
0017  * License along with this library; if not, write to the Free Software
0018  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
0019  */
0020 
0021 #ifndef GLOBALPRESENCE_H
0022 #define GLOBALPRESENCE_H
0023 
0024 #include <QObject>
0025 #include <QIcon>
0026 #include <QDBusInterface>
0027 
0028 #include <TelepathyQt/AccountManager>
0029 #include <TelepathyQt/AccountSet>
0030 #include <TelepathyQt/Constants>
0031 
0032 #include <KTp/ktpcommoninternals_export.h>
0033 #include <KTp/types.h>
0034 #include <KTp/presence.h>
0035 
0036 namespace KTp
0037 {
0038 
0039 /** This class handles the presence between all enabled accounts.
0040  * It shows the highest current / requested presence, indicates if any accounts
0041  * are changing state, and the highest they are changing to.
0042 */
0043 
0044 class KTPCOMMONINTERNALS_EXPORT GlobalPresence : public QObject
0045 {
0046     Q_OBJECT
0047     Q_PROPERTY(Tp::AccountManagerPtr accountManager READ accountManager WRITE addAccountManager NOTIFY accountManagerReady)
0048 
0049     Q_PROPERTY(QString presenceMessage READ currentPresenceMessage NOTIFY currentPresenceChanged)
0050     Q_PROPERTY(ConnectionPresenceType presenceType READ currentPresenceType NOTIFY currentPresenceChanged)
0051     Q_PROPERTY(QIcon currentPresenceIcon READ currentPresenceIcon NOTIFY currentPresenceChanged)
0052     Q_PROPERTY(QString currentPresenceIconName READ currentPresenceIconName NOTIFY currentPresenceChanged)
0053     Q_PROPERTY(KTp::Presence currentPresence READ currentPresence NOTIFY currentPresenceChanged)
0054     Q_PROPERTY(QString currentPresenceName READ currentPresenceName NOTIFY currentPresenceChanged);
0055     Q_PROPERTY(KTp::Presence requestedPresence READ requestedPresence NOTIFY requestedPresenceChanged WRITE setPresence)
0056     Q_PROPERTY(QString requestedPresenceName READ requestedPresenceName NOTIFY requestedPresenceChanged)
0057     Q_PROPERTY(KTp::Presence globalPresence READ globalPresence WRITE setPresence)
0058     Q_PROPERTY(ConnectionStatus connectionStatus READ connectionStatus NOTIFY connectionStatusChanged)
0059     Q_PROPERTY(bool isChangingPresence READ isChangingPresence NOTIFY changingPresence)
0060     Q_PROPERTY(bool hasConnectionError READ hasConnectionError NOTIFY connectionStatusChanged)
0061     Q_PROPERTY(bool hasEnabledAccounts READ hasEnabledAccounts NOTIFY enabledAccountsChanged)
0062     Q_PROPERTY(Tp::AccountSetPtr enabledAccounts READ enabledAccounts)
0063     Q_PROPERTY(Tp::AccountSetPtr onlineAccounts READ onlineAccounts)
0064 
0065 public:
0066     explicit GlobalPresence(QObject *parent = nullptr);
0067 
0068     enum ConnectionPresenceType
0069     {
0070         Offline = Tp::ConnectionPresenceTypeOffline,
0071         Available = Tp::ConnectionPresenceTypeAvailable,
0072         Away = Tp::ConnectionPresenceTypeAway,
0073         ExtendedAway = Tp::ConnectionPresenceTypeExtendedAway,
0074         Hidden = Tp::ConnectionPresenceTypeHidden,
0075         Busy = Tp::ConnectionPresenceTypeBusy,
0076         Unknown = Tp::ConnectionPresenceTypeUnknown,
0077         Unset = Tp::ConnectionPresenceTypeUnset,
0078         Error = Tp::ConnectionPresenceTypeError
0079     };
0080     Q_ENUM(ConnectionPresenceType)
0081 
0082     enum ConnectionStatus
0083     {
0084         Disconnected = Tp::ConnectionStatusDisconnected,
0085         Connecting = Tp::ConnectionStatusConnecting,
0086         Connected = Tp::ConnectionStatusConnected
0087     };
0088     Q_ENUM(ConnectionStatus)
0089 
0090     enum PresenceClass
0091     {
0092         Persistent,
0093         Session
0094     };
0095     Q_ENUM(PresenceClass)
0096 
0097     /**
0098      * \brief Set a ready account manager.
0099      *
0100      * \param accountManager A Tp::AccountManagerPtr.
0101      */
0102     void setAccountManager(const Tp::AccountManagerPtr &accountManager);
0103 
0104     /**
0105      * \brief Add a new (unready) account manager.
0106      *
0107      * \param accountManager A Tp::AccountManagerPtr.
0108      */
0109     void addAccountManager(const Tp::AccountManagerPtr &accountManager);
0110 
0111     /**
0112      * \brief The account manager.
0113      *
0114      * \return A Tp::AccountManagerPtr.
0115      */
0116     Tp::AccountManagerPtr accountManager() const;
0117 
0118     /**
0119      * \brief Global connection status. Returns connecting if any account is
0120      * connecting, else connected if at least one account is connected,
0121      * disconnected otherwise.
0122      *
0123      * \return A ConnectionStatus enum.
0124      */
0125     ConnectionStatus connectionStatus() const;
0126 
0127     /**
0128      * \brief The most online presence of all accounts. Returns the same presence
0129      * as the requested presence if the most online account supports the
0130      * requested presence.
0131      */
0132     KTp::Presence currentPresence() const;
0133     QString currentPresenceMessage() const;
0134     QIcon currentPresenceIcon() const;
0135     QString currentPresenceIconName() const;
0136     ConnectionPresenceType currentPresenceType() const;
0137     QString currentPresenceName() const;
0138 
0139     /**
0140      * \brief The most online requested presence for all accounts.
0141      */
0142     KTp::Presence requestedPresence() const;
0143     QString requestedPresenceName() const;
0144 
0145     /**
0146      * \brief If any account is changing presence.
0147      *
0148      * \return true if any account is changing state.
0149      */
0150     bool isChangingPresence() const;
0151 
0152     /**
0153      * \brief The KDED module requested global presence.
0154      *
0155      * \return A KTp::Presence.
0156      */
0157     KTp::Presence globalPresence() const;
0158 
0159     /**
0160      * \brief If any account has a connection error.
0161      *
0162      * \return true if any account has a connection error.
0163      */
0164     bool hasConnectionError() const;
0165 
0166     /**
0167      * \brief If the account manager has enabled accounts.
0168      *
0169      * \return true if the account manager has enabled accounts.
0170      */
0171     bool hasEnabledAccounts() const;
0172 
0173     /**
0174      * \brief The account manager enabled accounts.
0175      *
0176      * \return The account manager enabled accounts set.
0177      */
0178     Tp::AccountSetPtr enabledAccounts() const;
0179 
0180     /**
0181      * \brief The account manager online accounts.
0182      *
0183      * \return The account manager online accounts set.
0184      */
0185     Tp::AccountSetPtr onlineAccounts() const;
0186 
0187 Q_SIGNALS:
0188     void requestedPresenceChanged(const KTp::Presence &requestedPresence);
0189     void currentPresenceChanged(const KTp::Presence &currentPresence);
0190     void connectionStatusChanged(KTp::GlobalPresence::ConnectionStatus connectionStatus);
0191     void changingPresence(bool isChangingPresence);
0192     void enabledAccountsChanged(bool hasEnabledAccounts);
0193     void accountManagerReady();
0194 
0195 public Q_SLOTS:
0196     /**
0197      * \brief Set the requested presence of all enabled accounts. If setting
0198      * the global requested presence fails, will set each account to the
0199      * specified presence. A presence type of unset will unset the presence.
0200      *
0201      * \param presence The requested presence.
0202      *
0203      * \overload presenceClass Session or Persistent presence class.
0204      **/
0205     void setPresence(const KTp::Presence &presence, PresenceClass presenceClass = Persistent);
0206 
0207     /**
0208      * \brief Set the requested presence of all enabled accounts. If setting
0209      * the global requested presence fails, will set each account to the
0210      * specified presence. A presence type of unset will unset the presence.
0211      *
0212      * \param type The ConnectionPresenceType.
0213      *
0214      * \overload message A status message.
0215      * \overload presenceClass Session or Persistent presence class.
0216      **/
0217     void setPresence(ConnectionPresenceType type, QString message = QString(), PresenceClass presenceClass = Session);
0218 
0219 private Q_SLOTS:
0220     void onCurrentPresenceChanged(const Tp::Presence &currentPresence);
0221     void onRequestedPresenceChanged(const Tp::Presence &requestedPresence);
0222     void onChangingPresence(bool isChangingPresence);
0223     void onConnectionStatusChanged(Tp::ConnectionStatus connectionStatus);
0224 
0225     void onAccountEnabledChanged(const Tp::AccountPtr &account);
0226 
0227 private:
0228     QDBusInterface *m_statusHandlerInterface;
0229     Tp::AccountManagerPtr m_accountManager;
0230     Tp::AccountSetPtr m_enabledAccounts;
0231     Tp::AccountSetPtr m_onlineAccounts;
0232 
0233     KTp::Presence m_requestedPresence;
0234     KTp::Presence m_currentPresence;
0235     ConnectionStatus m_connectionStatus;
0236     bool m_changingPresence;
0237     bool m_hasConnectionError;
0238     bool m_hasEnabledAccounts;
0239 };
0240 
0241 }
0242 
0243 #endif // GLOBALPRESENCE_H