SFLPhoneApp.cpp 11.3 KB
Newer Older
jpbl's avatar
jpbl committed
1
/*
yanmorin's avatar
   
yanmorin committed
2
 *  Copyright (C) 2004-2006 Savoir-Faire Linux inc.
jpbl's avatar
jpbl committed
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
 *  Author: Yan Morin <yan.morin@savoirfairelinux.com>
 *  Author: Jean-Philippe Barrette-LaPierre
 *             <jean-philippe.barrette-lapierre@savoirfairelinux.com>
 *                                                                              
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *                                                                              
 *  This program 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 General Public License for more details.
 *                                                                              
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include "globals.h"

#include "taxidermy/Hunter.hpp"

#include "ConfigurationManager.hpp"
#include "Launcher.hpp"
#include "NumericKeypad.hpp"
#include "PhoneLine.hpp"
#include "PhoneLineButton.hpp"
#include "Requester.hpp"
#include "Session.hpp"
#include "SessionIOFactory.hpp"
#include "SFLLcd.hpp"
#include "SFLPhoneApp.hpp"
#include "SFLPhoneWindow.hpp"
#include "SFLRequest.hpp"
#include "SkinManager.hpp"
#include "TCPSessionIOCreator.hpp"
#include "VolumeControl.hpp"
#include <qclipboard.h>  // paste

SFLPhoneApp::SFLPhoneApp(int argc, char **argv)
  : QApplication(argc, argv)
  , mLauncher(new Launcher())
{
  SessionIOFactory::instance().setCreator(new TCPSessionIOCreator(QString("localhost"), 3999));

  Session session;

  ConfigurationManager::instance().setSession(session);

  PhoneLineManager::instance().initialize(session);
  PhoneLineManager::instance().setNbLines(NB_PHONELINES);
  Requester::instance().registerDefaultObject< Request >();
  Requester::instance().registerObject< Request >(QString("playtone"));
  Requester::instance().registerObject< Request >(QString("stoptone"));
  Requester::instance().registerObject< Request >(QString("playdtmf"));

yanmorin's avatar
   
yanmorin committed
60
  Requester::instance().registerObject< Request >(QString("register"));
yanmorin's avatar
   
yanmorin committed
61
  Requester::instance().registerObject< Request >(QString("switch"));
jpbl's avatar
jpbl committed
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
  Requester::instance().registerObject< ConfigGetAllRequest >(QString("configgetall"));
  Requester::instance().registerObject< ConfigSaveRequest >(QString("configsave"));
  Requester::instance().registerObject< StopRequest >(QString("stop"));


  Requester::instance().registerObject< EventRequest >(QString("getevents"));
  Requester::instance().registerObject< CallStatusRequest >(QString("getcallstatus"));
  Requester::instance().registerObject< PermanentRequest >(QString("answer"));
  Requester::instance().registerObject< PermanentRequest >(QString("notavailable"));
  Requester::instance().registerObject< PermanentRequest >(QString("refuse"));
  Requester::instance().registerObject< PermanentRequest >(QString("hangup"));
  Requester::instance().registerObject< TemporaryRequest >(QString("unmute"));
  Requester::instance().registerObject< TemporaryRequest >(QString("hold"));
  Requester::instance().registerObject< TemporaryRequest >(QString("unhold"));
  Requester::instance().registerObject< TemporaryRequest >(QString("senddtmf"));
  Requester::instance().registerObject< Request >(QString("setspkrvolume"));
  Requester::instance().registerObject< Request >(QString("setmicvolume"));
  Requester::instance().registerObject< Request >(QString("mute"));

  mKeypad = new NumericKeypad();
}

SFLPhoneApp::~SFLPhoneApp()
{}

void 
SFLPhoneApp::handleArg()
{
  if (argc() > 1) {
    PhoneLineManager::instance().makeNewCall(QString(argv()[1]));
  }
}

void
SFLPhoneApp::launch()
{
jpbl's avatar
jpbl committed
98
99
100
   if(mLauncher) {
     mLauncher->start();
   }
jpbl's avatar
jpbl committed
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
}

void
SFLPhoneApp::initConnections(SFLPhoneWindow *w)
{
  // We connect the phone line buttons to the PhoneLineManager
  unsigned int i = 0;
  for(std::list< PhoneLineButton * >::iterator pos = w->mPhoneLineButtons.begin();
      pos != w->mPhoneLineButtons.end();
      pos++) {
    PhoneLine *line = PhoneLineManager::instance().getPhoneLine(i);
    QObject::connect(*pos, SIGNAL(selected(unsigned int)),
		     &PhoneLineManager::instance(), SLOT(selectLine(unsigned int)));
    QObject::connect(*pos, SIGNAL(unselected(unsigned int)),
		     &PhoneLineManager::instance(), SLOT(unselectLine(unsigned int)));
    QObject::connect(line, SIGNAL(selected(bool)),
		     *pos, SLOT(setOn(bool)));
    QObject::connect(line, SIGNAL(backgrounded()),
		     *pos, SLOT(suspend()));
120
121
    QObject::connect(line, SIGNAL(hanguped()),
		     *pos, SLOT(stopFlashing()));
jpbl's avatar
jpbl committed
122
123
124
125
126
127
128
129
130
    QObject::connect(line, SIGNAL(peerUpdated(QString)),
		     *pos, SLOT(setToolTip(QString)));
    QObject::connect(line, SIGNAL(peerCleared()),
		     *pos, SLOT(clearToolTip()));

    i++;
  }


131
132
  QObject::connect(w, SIGNAL(needRegister(const QString&)),
                   &PhoneLineManager::instance(), SLOT(slotRegisterToServer(const QString&)));
yanmorin's avatar
   
yanmorin committed
133
134
135
136
137
138
139
140
  QObject::connect(&PhoneLineManager::instance(), SIGNAL(registerReturn(bool, QString)), 
                   w, SIGNAL(registerReturn(bool, QString)));

  QObject::connect(w, SIGNAL(soundDriverChanged()),
                   &PhoneLineManager::instance(), SLOT(slotReloadSoundDriver()));
  QObject::connect(&PhoneLineManager::instance(), SIGNAL(testSoundDriverReturn(bool, QString)), 
                   w, SIGNAL(testSoundDriverReturn(bool, QString)));

yanmorin's avatar
   
yanmorin committed
141

jpbl's avatar
jpbl committed
142
143
144
145
146
147
148
149
  //QObject::connect(&PhoneLineManager::instance(), SIGNAL(registered()),
  //		   w, SIGNAL(registered()));
  QObject::connect(w->mOk, SIGNAL(clicked()),
		   &PhoneLineManager::instance(), SLOT(call()));
  QObject::connect(w->mMute, SIGNAL(toggled(bool)),
		   &PhoneLineManager::instance(), SLOT(mute(bool)));
  QObject::connect(w->mDtmf, SIGNAL(toggled(bool)),
		   mKeypad, SLOT(setShown(bool)));
yanmorin's avatar
   
yanmorin committed
150
151
152
153
  //QObject::connect(mKeypad, SIGNAL(hidden()),
  //		   w->mDtmf, SLOT(release()));
  QObject::connect(mKeypad, SIGNAL(isShown(bool)),
		   w->mDtmf, SLOT(setOn(bool)));
jpbl's avatar
jpbl committed
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
  QObject::connect(w->mSetup, SIGNAL(clicked()),
		   &PhoneLineManager::instance(), SLOT(setup()));
  QObject::connect(w->mHangup, SIGNAL(clicked()),
		   &PhoneLineManager::instance(), SLOT(hangup()));
  QObject::connect(w->mHold, SIGNAL(clicked()),
		   &PhoneLineManager::instance(), SLOT(hold()));
  QObject::connect(w->mClear, SIGNAL(clicked()),
		   &PhoneLineManager::instance(), SLOT(clear()));
  QObject::connect(w->mTransfer, SIGNAL(clicked()),
		   &PhoneLineManager::instance(), SLOT(transfer()));
  QObject::connect(w->mRedial, SIGNAL(clicked()),
                   &PhoneLineManager::instance(), SLOT(redial()));

  QObject::connect(w, SIGNAL(keyPressed(Qt::Key)),
		   &PhoneLineManager::instance(), SLOT(sendKey(Qt::Key)));

  QObject::connect(w, SIGNAL(shortcutPressed(QKeyEvent*)),
		   this, SLOT(shortcutPressed(QKeyEvent*)));


  // Keypad connections
  QObject::connect(mKeypad, SIGNAL(keyPressed(Qt::Key)),
		   &PhoneLineManager::instance(), SLOT(sendKey(Qt::Key)));
  
  // LCD Connections.
  QObject::connect(&PhoneLineManager::instance(), SIGNAL(lineStatusSet(QString)),
		   w->mLcd, SLOT(setLineStatus(QString)));
  QObject::connect(&PhoneLineManager::instance(), SIGNAL(unselectedLineStatusSet(QString)),
		   w->mLcd, SLOT(setUnselectedLineStatus(QString)));
  QObject::connect(&PhoneLineManager::instance(), SIGNAL(actionSet(QString)),
		   w->mLcd, SLOT(setAction(QString)));
  QObject::connect(&PhoneLineManager::instance(), SIGNAL(globalStatusSet(QString)),
		   w->mLcd, SLOT(setGlobalStatus(QString)));
  QObject::connect(&PhoneLineManager::instance(), SIGNAL(bufferStatusSet(QString)),
		   w->mLcd, SLOT(setBufferStatus(QString)));
  QObject::connect(&PhoneLineManager::instance(), SIGNAL(talkingStarted(QTime)),
		   w->mLcd, SLOT(setLineTimer(QTime)));
  QObject::connect(&PhoneLineManager::instance(), SIGNAL(talkingStopped()),
		   w->mLcd, SLOT(clearLineTimer()));


  //Volume connections
  QObject::connect(w->mVolume, SIGNAL(valueUpdated(int)),
		   &PhoneLineManager::instance(), SLOT(setVolume(int)));
  QObject::connect(w->mMicVolume, SIGNAL(valueUpdated(int)),
		   &PhoneLineManager::instance(), SLOT(setMicVolume(int)));
  QObject::connect(&PhoneLineManager::instance(), SIGNAL(volumeUpdated(int)),
		   w->mVolume, SLOT(setValue(int)));
  QObject::connect(&PhoneLineManager::instance(), SIGNAL(micVolumeUpdated(int)),
		   w->mMicVolume, SLOT(setValue(int)));


  //Line events connections
  QObject::connect(&PhoneLineManager::instance(), SIGNAL(disconnected()),
		   w, SLOT(askReconnect()));
  QObject::connect(&PhoneLineManager::instance(), SIGNAL(disconnected()),
		   w, SLOT(show()));
  QObject::connect(&PhoneLineManager::instance(), SIGNAL(connected()),
		   w, SLOT(show()));
  QObject::connect(w, SIGNAL(reconnectAsked()),
		   &PhoneLineManager::instance(), SLOT(connect()));
  QObject::connect(&PhoneLineManager::instance(), SIGNAL(stopped()),
		   w, SLOT(close()));
  QObject::connect(w, SIGNAL(needToCloseDaemon()),
		   &PhoneLineManager::instance(), SLOT(stop()));

  //sflphoned launch
  QObject::connect(w, SIGNAL(launchAsked()),
		   mLauncher, SLOT(start()));
  QObject::connect(mLauncher, SIGNAL(error()),
		   w, SLOT(askLaunch()));
  QObject::connect(mLauncher, SIGNAL(started()),
		   &PhoneLineManager::instance(), SLOT(connect()));

  QObject::connect(&PhoneLineManager::instance(), SIGNAL(gotErrorOnCallStatus(QString)),
		   w, SLOT(askResendStatus(QString)));
  QObject::connect(&PhoneLineManager::instance(), SIGNAL(gotErrorOnGetEvents(QString)),
		   w, SLOT(askResendStatus(QString)));
  QObject::connect(w, SIGNAL(resendStatusAsked()),
		   &PhoneLineManager::instance(), SIGNAL(readyToSendStatus()));

  
  //Configuration events.
  QObject::connect(&ConfigurationManager::instance(), SIGNAL(updated()),
		   w, SLOT(showSetup()));
  QObject::connect(&ConfigurationManager::instance(), SIGNAL(ringtonesUpdated()),
		   w, SIGNAL(ringtonesUpdated()));
  QObject::connect(&ConfigurationManager::instance(), SIGNAL(audioDevicesUpdated()),
		   w, SIGNAL(audioDevicesUpdated()));
yanmorin's avatar
   
yanmorin committed
243
244
245
246
  QObject::connect(&ConfigurationManager::instance(), SIGNAL(audioDevicesInUpdated()),
		   w, SIGNAL(audioDevicesInUpdated()));
  QObject::connect(&ConfigurationManager::instance(), SIGNAL(audioDevicesOutUpdated()),
		   w, SIGNAL(audioDevicesOutUpdated()));
jpbl's avatar
jpbl committed
247
248
249
250
251
252
253
254
255
256
257
258
259
  QObject::connect(&ConfigurationManager::instance(), SIGNAL(codecsUpdated()),
		   w, SIGNAL(codecsUpdated()));
  //QObject::connect(&ConfigurationManager::instance(), SIGNAL(saved()),
  //		   w, SLOT(hideSetup()));

  QObject::connect(w->mLcd, SIGNAL(midClicked()), this, SLOT(paste()));

}

void 
SFLPhoneApp::loadSkin() 
{
  SkinManager::instance().setApplication(this);
jpbl's avatar
jpbl committed
260
  SkinManager::instance().load();
jpbl's avatar
jpbl committed
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
}

void
SFLPhoneApp::paste()
{
// Seen on: http://doc.trolltech.com/3.3/showimg-example.html#x1078
#ifndef QT_NO_MIMECLIPBOARD
  QString text = QApplication::clipboard()->text();
  if ( !text.isEmpty() && !text.isNull() ) {
    DebugOutput::instance() << QObject::tr("paste %1\n").arg(text);
    PhoneLineManager::instance().makeNewCall(text);
  }
#endif
}

void
SFLPhoneApp::shortcutPressed(QKeyEvent* e) {
  if (e && e->state() & Qt::ControlButton) {
    switch(e->key()) {
      case Qt::Key_V:
        paste();
      break;
    }
  }
}