aboutsummaryrefslogtreecommitdiff
path: root/connection.h
blob: 849106a122fe61511cc7876437e8ac246d509594 (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
/******************************************************************************
 * Copyright (C) 2015 Felix Rohrbach <kde@fxrh.de>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#pragma once

#include <QtCore/QObject>
#include <QtCore/QUrl>
#include <QtCore/QSize>

namespace QMatrixClient
{
    class Room;
    class User;
    class Event;
    class ConnectionPrivate;
    class ConnectionData;

    class SyncJob;
    class RoomMessagesJob;
    class PostReceiptJob;
    class MediaThumbnailJob;
    class JoinRoomJob;

    class Connection: public QObject {
            Q_OBJECT
        public:
            Connection(QUrl server, QObject* parent = nullptr);
            Connection();
            virtual ~Connection();

            QHash<QString, Room*> roomMap() const;
            Q_INVOKABLE virtual bool isConnected();

            Q_INVOKABLE virtual void resolveServer( QString domain );
            Q_INVOKABLE virtual void connectToServer( QString user, QString password );
            Q_INVOKABLE virtual void connectWithToken( QString userId, QString token );
            Q_INVOKABLE virtual void reconnect();
            Q_INVOKABLE virtual void disconnectFromServer();
            Q_INVOKABLE virtual void logout();

            Q_INVOKABLE virtual void sync(int timeout=-1);
            Q_INVOKABLE virtual void stopSync();
            /** @deprecated Use callApi<PostMessageJob>() or Room::postMessage() instead */
            Q_INVOKABLE virtual void postMessage( Room* room, QString type, QString message );
            /** @deprecated Use callApi<PostReceiptJob>() or Room::postReceipt() instead */
            Q_INVOKABLE virtual PostReceiptJob* postReceipt( Room* room, Event* event );
            Q_INVOKABLE virtual JoinRoomJob* joinRoom( QString roomAlias );
            Q_INVOKABLE virtual void leaveRoom( Room* room );
            Q_INVOKABLE virtual RoomMessagesJob* getMessages( Room* room, QString from );
            virtual MediaThumbnailJob* getThumbnail( QUrl url, QSize requestedSize );
            MediaThumbnailJob* getThumbnail( QUrl url, int requestedWidth, int requestedHeight );

            Q_INVOKABLE QUrl homeserver() const;
            Q_INVOKABLE User* user(QString userId);
            Q_INVOKABLE User* user();
            Q_INVOKABLE QString userId() const;
            /** @deprecated Use accessToken() instead. */
            Q_INVOKABLE QString token() const;
            Q_INVOKABLE QString accessToken() const;
            Q_INVOKABLE SyncJob* syncJob() const;
            Q_INVOKABLE int millisToReconnect() const;

            template <typename JobT, typename... JobArgTs>
            JobT* callApi(JobArgTs... jobArgs)
            {
                auto job = new JobT(connectionData(), jobArgs...);
                job->start();
                return job;
            }

        signals:
            void resolved();
            void connected();
            void reconnected();
            void loggedOut();

            void syncDone();
            void newRoom(Room* room);
            void joinedRoom(Room* room);

            void loginError(QString error);
            void networkError(size_t nextAttempt, int inMilliseconds);
            void resolveError(QString error);
            void syncError(QString error);
            //void jobError(BaseJob* job);

        protected:
            /**
             * @brief Access the underlying ConnectionData class
             */
            ConnectionData* connectionData();

            /**
             * @brief Find a (possibly new) Room object for the specified id
             * Use this method whenever you need to find a Room object in
             * the local list of rooms. Note that this does not interact with
             * the server; in particular, does not automatically create rooms
             * on the server.
             * @return a pointer to a Room object with the specified id; nullptr
             * if roomId is empty if createRoom() failed to create a Room object.
             */
            Room* provideRoom(QString roomId);

            /**
             * makes it possible for derived classes to have its own User class
             */
            virtual User* createUser(QString userId);

            /**
             * makes it possible for derived classes to have its own Room class
             */
            virtual Room* createRoom(QString roomId);

        private:
            class Private;
            Private* d;
    };
}