File indexing completed on 2025-02-16 04:23:12

0001 /*
0002     SPDX-FileCopyrightText: 2022 Volker Krause <vkrause@kde.org>
0003     SPDX-License-Identifier: LGPL-2.0-or-later
0004 */
0005 
0006 #ifndef KUNIFIEDPUSH_ABSTRACTPUSHPROVIDER_H
0007 #define KUNIFIEDPUSH_ABSTRACTPUSHPROVIDER_H
0008 
0009 #include <QObject>
0010 
0011 class QNetworkAccessManager;
0012 class QSettings;
0013 
0014 namespace KUnifiedPush {
0015 
0016 class Client;
0017 class Message;
0018 
0019 /** Base class for push provider protocol implementations.
0020  *  Needed to support different push providers as that part of the
0021  *  protocol is not part of the UnifiedPush spec.
0022  */
0023 class AbstractPushProvider : public QObject
0024 {
0025     Q_OBJECT
0026 public:
0027     ~AbstractPushProvider();
0028 
0029     enum Error {
0030         NoError, ///< operation succeeded
0031         ProviderRejected, ///< communication worked, but the provider refused to complete the operation
0032         TransientNetworkError, ///< temporary network error, try again
0033     };
0034     Q_ENUM(Error)
0035 
0036     /** Load connection settings.
0037      *  @param settings can be read on the top level, the correct group is already selected.
0038      *  @returns @c true if the settings are valid, @c false otherwise.
0039      */
0040     virtual bool loadSettings(const QSettings &settings) = 0;
0041 
0042     /** Attempt to establish a connection to the push provider. */
0043     virtual void connectToProvider() = 0;
0044 
0045     /** Disconnect and existing connection to the push provider. */
0046     virtual void disconnectFromProvider() = 0;
0047 
0048     /** Register a new client with the provider. */
0049     virtual void registerClient(const Client &client) = 0;
0050 
0051     /** Unregister a client from the provider. */
0052     virtual void unregisterClient(const Client &client) = 0;
0053 
0054     /** Provider id used e.g. in settings. */
0055     const char* providerId() const;
0056 
0057 Q_SIGNALS:
0058     /** Inform about a received push notification. */
0059     void messageReceived(const KUnifiedPush::Message &msg);
0060 
0061     /** Emitted after successful client registration. */
0062     void clientRegistered(const KUnifiedPush::Client &client, KUnifiedPush::AbstractPushProvider::Error error = NoError, const QString &errorMsg = {});
0063 
0064     /** Emitted after successful client unregistration. */
0065     void clientUnregistered(const KUnifiedPush::Client &client, KUnifiedPush::AbstractPushProvider::Error error = NoError);
0066 
0067     /** Emitted after the connection to the push provider has been established successfully. */
0068     void connected();
0069 
0070     /** Emitted after the connection to the push provider disconnected or failed to be established. */
0071     void disconnected(KUnifiedPush::AbstractPushProvider::Error error, const QString &errorMsg = {});
0072 
0073 protected:
0074     explicit AbstractPushProvider(const char *providerId, QObject *parent);
0075     QNetworkAccessManager *nam();
0076 
0077 private:
0078     const char *m_providerId = nullptr;
0079     QNetworkAccessManager *m_nam = nullptr;
0080 };
0081 
0082 }
0083 
0084 #endif // KUNIFIEDPUSH_ABSTRACTPUSHPROVIDER_H