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