Everything working fine now. Tested with InterCall conferencing
[confmgr] / confmanager.cpp
1 #include "confmanager.h"
2 #include <QDebug>
3 #include <QTimer>
4
5 confManager::confManager(QObject *parent) :
6     QObject(parent)
7 {
8     mProfileSet = false;
9     mInStep = 0;
10 }
11
12 void confManager::setProfile(Profile &p)
13 {
14     mInStep = 0;
15     mProfileInUse = p;
16     mProfileSet = true;
17 }
18
19 void confManager::continueSendDTMF()
20 {
21     qDebug() << "In ContinueSendDTF for mInStep " << mInStep;
22     // We have now waited for the required period of seconds
23     // Lets send the DTMF now
24     Steps step = mProfileInUse.mSteps.at(mInStep);
25
26     // Increment the steps as we want to point to the next one
27     mInStep++;
28
29     QList<QVariant> argsToSend;
30     qDebug() << "Sending DTMF = " << step.value();
31     argsToSend.append(step.value());
32     argsToSend.append(0);
33
34     bool status = mDBusUtility.sendMethodCall(CSD_SERVICE, CSD_CALL_PATH, CSD_CALL_INTERFACE,
35                                          QString("SendDTMF"),argsToSend);
36
37     if(!status)
38     {
39         qDebug() << "Unable to send DTMF code.";
40         QString error = "DBus Error (continueSendDTMF): " + mDBusUtility.getErrorMessage();
41         mDBusUtility.displayNotification(error);
42     }
43
44     // Check if we are over with the sequence or we need to continue
45     if(mInStep >= mProfileInUse.mNoOfSteps)
46     {
47         StopCallMonitors();
48         return;
49     }
50
51     step = mProfileInUse.mSteps.at(mInStep);
52     QTimer *timer = new QTimer(this);
53     timer->setSingleShot(true);
54     connect(timer, SIGNAL(timeout()), this, SLOT(continueSendDTMF()));
55     qDebug() << "Setting delay for " << step.delay() << "seconds...";
56     timer->start(step.delay()*1000);
57 }
58
59 void confManager::sendDTMF(const QDBusMessage &dBusMessage)
60 {
61     QList<QVariant> listArguments = dBusMessage.arguments();
62     bool audioConnect =  listArguments.first().toBool();
63
64     if(mInStep >= mProfileInUse.mNoOfSteps)
65     {
66         StopCallMonitors();
67         return;
68     }
69
70     Steps step = mProfileInUse.mSteps.at(mInStep);
71
72     if (audioConnect)
73     {
74         qDebug() << "Call Active...";
75         //Wait for specified delay in the step
76         QTimer *timer = new QTimer(this);
77         timer->setSingleShot(true);
78         connect(timer, SIGNAL(timeout()), this, SLOT(continueSendDTMF()));
79         qDebug() << "Setting delay for " << step.delay() << "seconds...";
80         timer->start(step.delay()*1000);
81     }
82 }
83
84 void confManager::StartCallMonitors()
85 {
86     QDBusConnection connection = mDBusUtility.getConnection();
87     /* Declare the slot to be executed when a call is picked up by other party (Audio connection established).
88        We need this to confirm whether a call went though successfully.
89     */
90     bool status = connection.connect(CSD_CALL_SERVICE, CSD_CALL_INSTANCE_PATH, CSD_CALL_INSTANCE_INTERFACE,
91                            QString("AudioConnect"),this, SLOT(sendDTMF(const QDBusMessage&)));
92
93     if(!status)
94     {
95         qDebug() << "Failed to connect to Dbus signal AudioConnect in interface "<< CSD_CALL_INSTANCE_INTERFACE;
96         QString error = "DBus Error (StartCallMonitors): " + mDBusUtility.getErrorMessage();
97         mDBusUtility.displayNotification(error);
98         qDebug() << "Error is: " << error;
99     }
100
101     qDebug() << "Successfully connected to Dbus signal AudioConnect in interface "<< CSD_CALL_INSTANCE_INTERFACE;
102
103     /* Declare the slot to be executed when the call is terminated (due to connection errors etc).
104        We need this to avoid sending DTMF code on wrong calls.
105     */
106     status = connection.connect(CSD_CALL_SERVICE, CSD_CALL_INSTANCE_PATH, CSD_CALL_INSTANCE_INTERFACE,
107                                QString("Terminated"),this, SLOT(StopCallMonitors()));
108
109     if(!status)
110     {
111         qDebug() << "Failed to connect to Dbus signal Terminated in interface "<< CSD_CALL_INSTANCE_INTERFACE;
112         QString error = "DBus Error (StartCallMonitors): " + mDBusUtility.getErrorMessage();
113         mDBusUtility.displayNotification(error);
114         qDebug() << "Error is: " << error;
115     }
116
117     qDebug() << "Successfully connected to Dbus signal Terminated in interface "<< CSD_CALL_INSTANCE_INTERFACE;
118
119     /* Declare the slot to be executed when a call is received
120       (before we can place the call to calling card number).
121        It is extremely rare that somebody should get a call within these few seconds.
122        In any case, we need this to avoid sending DTMF code on the received call.
123
124        Btw - I don't care for the incoming number here. If anyone is calling the user before we can send DTMF code,
125        then we stop sending the DTMF code even if user does not respond to the call.
126     */
127
128     status = connection.connect(CSD_CALL_SERVICE, CSD_CALL_PATH, CSD_CALL_INTERFACE,
129                                QString("Coming"),this, SLOT(StopCallMonitors()));
130
131     if(!status)
132     {
133         qDebug() << "Failed to connect to Dbus signal Coming in interface" << CSD_CALL_INTERFACE;
134         QString error = "DBus Error (StartCallMonitors): " + mDBusUtility.getErrorMessage();
135         mDBusUtility.displayNotification(error);
136         qDebug() << "Error is: " << error;
137     }
138     qDebug() << "Successfully connected to Dbus signal Coming in interface" << CSD_CALL_INTERFACE;
139 }
140
141 void confManager::StopCallMonitors()
142 {
143     mInStep = 0;
144     mProfileSet = false;
145
146     QDBusConnection connection = mDBusUtility.getConnection();
147
148     // Disconnect the slot for audio connection status
149     bool status = connection.disconnect(CSD_CALL_BUS_NAME, CSD_CALL_INSTANCE_PATH, CSD_CALL_INSTANCE_INTERFACE,
150                                    QString("AudioConnect"),this, SLOT(sendDTMF(const QDBusMessage&)));
151
152     if(!status)
153     {
154         qDebug() << "Failed to disconnect from Dbus signal AudioConnect in interface" << CSD_CALL_INSTANCE_INTERFACE;
155         QString error = "DBus Error (StopCallMonitors): " + mDBusUtility.getErrorMessage();
156         mDBusUtility.displayNotification(error);
157         qDebug() << "Error is: " << error;
158     }
159     qDebug() << "Successfully disconnected from Dbus signal AudioConnect in interface "<< CSD_CALL_INSTANCE_INTERFACE;
160
161     // Disconnect the slot for monitoring terminated calls
162     status = connection.disconnect(CSD_CALL_BUS_NAME, CSD_CALL_INSTANCE_PATH, CSD_CALL_INSTANCE_INTERFACE,
163                                    QString("Terminated"),this, SLOT(StopCallMonitors()));
164
165     if(!status)
166     {
167         qDebug() << "Failed to disconnect from Dbus signal Terminated in interface" << CSD_CALL_INSTANCE_INTERFACE;
168         QString error = "DBus Error (StopCallMonitors): " + mDBusUtility.getErrorMessage();
169         mDBusUtility.displayNotification(error);
170         qDebug() << "Error is: " << error;
171     }
172     qDebug() << "Successfully disconnected from Dbus signal Terminated in interface "<< CSD_CALL_INSTANCE_INTERFACE;
173
174     // Disconnect the slot for monitoring incoming calls
175     status = connection.disconnect(CSD_CALL_BUS_NAME, CSD_CALL_PATH, CSD_CALL_INTERFACE,
176                                    QString("Coming"),this, SLOT(StopCallMonitors()));
177
178     if(!status)
179     {
180         qDebug() << "Failed to disconnect from Dbus signal Coming in interface" << CSD_CALL_INTERFACE;
181         QString error = "DBus Error (StopCallMonitors): " + mDBusUtility.getErrorMessage();
182         mDBusUtility.displayNotification(error);
183         qDebug() << "Error is: " << error;
184     }
185     qDebug() << "Successfully disconnected from Dbus signal Coming in interface" << CSD_CALL_INTERFACE;
186 }
187
188 void confManager::startConference()
189 {
190     mInStep = 0;
191     if(!mProfileSet)
192     {
193         qDebug() << "Please set the profile to use with Conference Manager first!";
194         return;
195     }
196     //Assume that the first number is always a phone number...
197     Steps step = mProfileInUse.mSteps.at(mInStep);
198     mInStep++;
199     QList<QVariant> sendArgs;
200     sendArgs.append(step.value());
201     sendArgs.append(0);
202     bool status = mDBusUtility.sendMethodCall(CSD_SERVICE, CSD_CALL_PATH, CSD_CALL_INTERFACE,
203                                               QString("CreateWith"), sendArgs);
204     if(!status)
205     {
206         QString error = "Error while dialing: " + mDBusUtility.getErrorMessage();
207         qDebug() << error;
208         mDBusUtility.displayNotification(error);
209         return;
210     }
211     StartCallMonitors();
212 }