1 /*
2  * Copyright 2013 Vitaly Valtman
3  * Copyright 2014 Canonical Ltd.
4  * Authors:
5  *      Roberto Mier
6  *      Tiago Herrmann
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; version 3.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #include "abstractapi.h"
23 
AbstractApi(Session * session,Settings * settings,CryptoUtils * crypto,QObject * parent)24 AbstractApi::AbstractApi(Session *session, Settings *settings, CryptoUtils *crypto, QObject *parent) :
25     SessionManager(session, settings, crypto, parent) {
26     // connect responses and updates signals in main session
27     connectResponsesSignals(mMainSession);
28     connectUpdatesSignals(mMainSession);
29 }
30 
~AbstractApi()31 AbstractApi::~AbstractApi() {
32 }
33 
connectUpdatesSignals(Session * session)34 void AbstractApi::connectUpdatesSignals(Session *session) {
35     connect(session, SIGNAL(updatesTooLong()), this, SIGNAL(updatesTooLong()));
36     connect(session, SIGNAL(updateShortMessage(qint32,qint32,const QString&,qint32,qint32,qint32,qint32,qint32,qint32,bool,bool)), this, SIGNAL(updateShortMessage(qint32,qint32,const QString&,qint32,qint32,qint32,qint32,qint32,qint32,bool,bool)));
37     connect(session, SIGNAL(updateShortChatMessage(qint32,qint32,qint32,const QString&,qint32,qint32,qint32,qint32,qint32,qint32,bool,bool)), this, SIGNAL(updateShortChatMessage(qint32,qint32,qint32,const QString&,qint32,qint32,qint32,qint32,qint32,qint32,bool,bool)));
38     connect(session, SIGNAL(updateShort(const Update&,qint32)), this, SIGNAL(updateShort(const Update&,qint32)));
39     connect(session, SIGNAL(updatesCombined(const QList<Update>&,const QList<User>&,const QList<Chat>&,qint32,qint32,qint32)), this, SIGNAL(updatesCombined(const QList<Update>&,const QList<User>&,const QList<Chat>&,qint32,qint32,qint32)));
40     connect(session, SIGNAL(updates(const QList<Update>&,const QList<User>&,const QList<Chat>&,qint32,qint32)), this, SIGNAL(updates(const QList<Update>&,const QList<User>&,const QList<Chat>&,qint32,qint32)));
41 }
42 
connectResponsesSignals(Session * session)43 void AbstractApi::connectResponsesSignals(Session *session) {
44     connect(session, SIGNAL(resultReceived(Query*,InboundPkt&)), this, SLOT(onResultReceived(Query*,InboundPkt&)));
45     connect(session, SIGNAL(errorReceived(Query*,qint32,QString)), this, SLOT(onErrorReceived(Query*,qint32,QString)));
46 }
47 
48 /**
49  * @brief AbstractApi::onErrorReceived manages any error received from the server and links to declared
50  *  "onError" QueryMethod for current operation
51  * @param q
52  * @param errorCode
53  * @param errorText
54  */
onErrorReceived(Query * q,qint32 errorCode,QString errorText)55 void AbstractApi::onErrorReceived(Query *q, qint32 errorCode, QString errorText) {
56     if (q->methods() && q->methods()->onError) {
57         (((Api *)this)->*(q->methods()->onError))(q, errorCode, errorText);
58     } else {
59         onError(q, errorCode, errorText);
60     }
61     delete q;
62 }
63 
64 /**
65  * @brief AbstractApi::onResultReceived manages any positive response received from telegram servers
66  *  and links to declared "onAnswer" QueryMethod for current operation
67  * @param q
68  * @param inboundPkt
69  */
onResultReceived(Query * q,InboundPkt & inboundPkt)70 void AbstractApi::onResultReceived(Query *q, InboundPkt &inboundPkt) {
71     if (q->methods() && q->methods()->onAnswer) {
72         (((Api *)this)->*(q->methods()->onAnswer))(q, inboundPkt);
73         if(inboundPkt.inPtr() != inboundPkt.inEnd())
74         {
75             Q_EMIT fatalError();
76             return;
77         }
78     }
79     delete q;
80 }
81