aboutsummaryrefslogtreecommitdiff
path: root/connectionprivate.cpp
blob: 6556a8eecd53d7b3b065c80f4fc0bb419ba77934 (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
/******************************************************************************
 * 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
 */

#include "connectionprivate.h"
#include "connection.h"
#include "state.h"
#include "room.h"
#include "user.h"
#include "jobs/passwordlogin.h"
#include "jobs/syncjob.h"
#include "jobs/joinroomjob.h"
#include "jobs/roommembersjob.h"
#include "events/event.h"
#include "events/roommessageevent.h"
#include "events/roommemberevent.h"

#include <QtCore/QDebug>
#include <QtNetwork/QDnsLookup>

using namespace QMatrixClient;

ConnectionPrivate::ConnectionPrivate(Connection* parent)
    : q(parent)
{
    isConnected = false;
    data = nullptr;
}

ConnectionPrivate::~ConnectionPrivate()
{
    delete data;
}

void ConnectionPrivate::resolveServer(QString domain)
{
    // Find the Matrix server for the given domain.
    QDnsLookup* dns = new QDnsLookup();
    dns->setType(QDnsLookup::SRV);
    dns->setName("_matrix._tcp." + domain);

    connect(dns, &QDnsLookup::finished, [this,dns]() {
        // Check the lookup succeeded.
        if (dns->error() != QDnsLookup::NoError ||
                dns->serviceRecords().isEmpty()) {
            emit q->resolveError("DNS lookup failed");
            dns->deleteLater();
            return;
        }

        // Handle the results.
        QDnsServiceRecord record = dns->serviceRecords().first();
        data->setHost(record.target());
        data->setPort(record.port());
        emit q->resolved();
        dns->deleteLater();
    });
    dns->lookup();
}

void ConnectionPrivate::processState(State* state)
{
    if( state->event()->type() == QMatrixClient::EventType::RoomMember )
    {
        QMatrixClient::RoomMemberEvent* e = static_cast<QMatrixClient::RoomMemberEvent*>(state->event());
        User* user = q->user(e->userId());
        user->processEvent(e);
    }

    if ( Room* r = provideRoom(state->event()->roomId()) )
        r->addInitialState(state);
}

Room* ConnectionPrivate::provideRoom(QString id)
{
    if (id.isEmpty())
    {
        qDebug() << "ConnectionPrivate::provideRoom() with empty id, doing nothing";
        return nullptr;
    }

    if (roomMap.contains(id))
        return roomMap.value(id);

    // Not yet in the map, create a new one.
    Room* room = q->createRoom(id);
    if (!room)
        qCritical() << "Failed to create a room!!!" << id;

    roomMap.insert( id, room );
    emit q->newRoom(room);
    return room;
}

void ConnectionPrivate::gotRoomMembers(BaseJob* job)
{
    RoomMembersJob* membersJob = static_cast<RoomMembersJob*>(job);
    if( !membersJob->error() )
    {
        for( State* state: membersJob->states() )
        {
            processState(state);
        }
        qDebug() << membersJob->states().count() << " processed...";
    }
    else
    {
        qDebug() << "MembersJob error: " <<membersJob->errorString();
        if( membersJob->error() == BaseJob::NetworkError )
            emit q->connectionError( membersJob->errorString() );
    }
}