first release
[groupsms] / sms / sendsmssession.cpp
1 #include <QtDebug>
2 #include "sendsmssession.h"
3
4 SendSMSSession::SendSMSSession( bool sync, QObject *parent ) :
5     QObject(parent)
6 {
7     syncSend = sync;
8     isReady = false;
9     tps = NULL;
10 }
11
12 void SendSMSSession::initTpSession()
13 {
14     qDebug() << __PRETTY_FUNCTION__ ;
15
16     if( tps == NULL )
17     {
18         tps = new TpSession( "ring", syncSend );
19
20         if( !isReady && !syncSend )
21         {
22             connect(tps,SIGNAL(accountReady(TpSessionAccount *)),SLOT(onAccountReady(TpSessionAccount *)));
23         }else
24         {
25             SendSMS();
26         }
27
28         connect(tps,SIGNAL(messageSent(const Tp::Message &,TpSessionAccount *)),
29                 SLOT(onSMSSent(const Tp::Message &,TpSessionAccount *)));
30
31         connect(tps,SIGNAL(messageReceived(const Tp::ReceivedMessage &,TpSessionAccount *)),
32                 SLOT(onMessageReceived(const Tp::ReceivedMessage &,TpSessionAccount *)));
33     }else
34     {
35         if( !isReady && !syncSend )
36         {
37             connect(tps,SIGNAL(accountReady(TpSessionAccount *)),SLOT(onAccountReady(TpSessionAccount *)));
38         }else
39         {
40             SendSMS();
41         }
42     }
43 }
44
45 void SendSMSSession::setSMSToSend(QString addr, QString msg)
46 {
47     qDebug() << __PRETTY_FUNCTION__ ;
48     addresses.append( addr );
49     messages.append( msg );
50
51     initTpSession();
52 }
53
54 void SendSMSSession::setSMSToSend( QStringList addrs, QStringList msgs )
55 {
56     qDebug() << __PRETTY_FUNCTION__ ;
57     addresses = addrs ;
58     messages = msgs ;
59
60     initTpSession();
61 }
62
63 void SendSMSSession::SendSMS()
64 {
65     qDebug() << __PRETTY_FUNCTION__ ;
66     for( int i = 0; i < addresses.size(); i++ )
67     {
68         tps->sendMessageToAddress( "ring", addresses.at(i), messages.at(i) );
69     }
70     addresses.clear();
71     messages.clear();
72 }
73
74 void SendSMSSession::onAccountReady(TpSessionAccount *tpsa)
75 {
76     qDebug() << __PRETTY_FUNCTION__ ;
77
78     isReady = true;
79     for( int i = 0; i < addresses.size(); i++ )
80     {
81         tpsa->sendMessageToAddress( addresses.at(i), messages.at(i) );
82     }
83     addresses.clear();
84     messages.clear();
85 }
86
87 void SendSMSSession::onSMSSent( const Tp::Message &msg, TpSessionAccount *acc )
88 {
89     qDebug() << "SendSMSSession::onSMSSent :" << msg.text();
90     Q_EMIT smsSent( msg.text() );
91 }
92
93 void SendSMSSession::onMessageReceived(const Tp::ReceivedMessage &msg,TpSessionAccount *acc)
94 {
95     qDebug() << "SendSMSSession::onMessageReceived " << msg.text() << "from " << msg.sender()->id();
96 }
97
98