Commit df476cea authored by jpbl's avatar jpbl
Browse files

Lines can behave with call, hold, unhold, hangup

parent e30290b9
......@@ -24,6 +24,8 @@
#include <QEvent>
#include <QMouseEvent>
#include "globals.h"
#include "JPushButton.hpp"
JPushButton::JPushButton(const QPixmap &released,
......@@ -43,6 +45,7 @@ JPushButton::~JPushButton()
void
JPushButton::release()
{
mIsPressed = false;
resize(mImages[0].size());
setPixmap(mImages[0]);
}
......@@ -50,6 +53,8 @@ JPushButton::release()
void
JPushButton::press()
{
_debug("Pressed.\n");
mIsPressed = true;
resize(mImages[1].size());
setPixmap(mImages[1]);
}
......
......@@ -42,15 +42,19 @@ public:
Qt::WFlags flags = 0);
~JPushButton();
bool isPressed()
{return mIsPressed;}
public slots:
/**
* This function will switch the button
*/
void press();
void release();
virtual void press();
virtual void release();
protected:
QPixmap mImages[2];
bool mIsPressed;
protected:
void mousePressEvent(QMouseEvent *);
......
......@@ -4,14 +4,20 @@
#include "PhoneLine.hpp"
#include "Call.hpp"
PhoneLine::PhoneLine(const Call &call,
PhoneLine::PhoneLine(const Session &session,
unsigned int line)
: mCall(call)
: mSession(session)
, mCall(NULL)
, mLine(line)
, mSelected(false)
, mInUse(false)
{}
PhoneLine::~PhoneLine()
{
delete mCall;
mCall = NULL;
}
unsigned int
PhoneLine::line()
{
......@@ -34,8 +40,13 @@ void
PhoneLine::select()
{
if(!mSelected) {
_debug("PhoneLine %d: I am selected.\n", mLine + 1);
_debug("PhoneLine %d: I am selected.\n", mLine);
mSelected = true;
if(mCall) {
mCall->unhold();
}
emit selected();
}
}
......@@ -44,13 +55,14 @@ void
PhoneLine::unselect()
{
if(mSelected) {
_debug("PhoneLine %d: I am unselected.\n", mLine + 1);
_debug("PhoneLine %d: I am unselected.\n", mLine);
mSelected = false;
if(!mInUse) {
mBuffer.clear();
if(mCall) {
mCall->hold();
emit backgrounded();
}
else {
mBuffer.clear();
emit unselected();
}
}
......@@ -59,19 +71,25 @@ PhoneLine::unselect()
void
PhoneLine::sendKey(Qt::Key c)
{
_debug("PhoneLine %d: Received the character:%s.\n", mLine + 1, QString(c).toStdString().c_str());
_debug("PhoneLine %d: Received the character:%s.\n",
mLine,
QString(c).toStdString().c_str());
switch(c) {
case Qt::Key_Enter:
case Qt::Key_Return:
if(!mInUse) {
if(!mCall) {
return call();
}
break;
default:
if(!mInUse) {
if(!mCall) {
mSession.playDtmf(c);
mBuffer += QString(c).toStdString();
}
else {
mCall->sendDtmf(c);
}
}
}
......@@ -85,8 +103,32 @@ void
PhoneLine::call(const std::string &to)
{
_debug("PhoneLine %d: Calling %s.\n", mLine, to.c_str());
if(!mInUse) {
mInUse = true;
mCall.call(to);
if(!mCall) {
mCall = new Call(mSession.createCall());
mCall->call(to);
}
}
void
PhoneLine::hold()
{
if(mCall) {
_debug("PhoneLine %d: Trying to Hold.\n", mLine);
mCall->hold();
}
unselect();
}
void
PhoneLine::hangup()
{
if(mCall) {
_debug("PhoneLine %d: Trying to Hangup.\n", mLine);
mCall->hangup();
delete mCall;
mCall = NULL;
}
unselect();
}
......@@ -3,18 +3,22 @@
#include <QMutex>
#include <string>
#include "Call.hpp"
#include "Session.hpp"
class Call;
class PhoneLine : public QObject
{
Q_OBJECT
public:
PhoneLine(const Call &call, unsigned int line);
~PhoneLine(){}
PhoneLine(const Session &session, unsigned int line);
~PhoneLine();
void call(const std::string &to);
void call();
void hangup();
void hold();
unsigned int line();
......@@ -43,7 +47,7 @@ public:
* started any communication, this will be available.
*/
bool isAvailable()
{return !mInUse;}
{return !mCall;}
void sendKey(Qt::Key c);
......@@ -65,7 +69,8 @@ signals:
void backgrounded();
private:
Call mCall;
Session mSession;
Call *mCall;
QMutex mPhoneLineMutex;
unsigned int mLine;
......
#include "globals.h"
#include "PhoneLineButton.hpp"
#include <QMouseEvent>
#include <QTimer>
PhoneLineButton::PhoneLineButton(const QPixmap &released,
const QPixmap &pressed,
......@@ -9,23 +13,31 @@ PhoneLineButton::PhoneLineButton(const QPixmap &released,
Qt::WFlags flags)
: JPushButton(released, pressed, parent, flags)
, mLine(line)
, mTimer(this)
, mFace(0)
{
connect(&mTimer, SIGNAL(timeout()),
mTimer = new QTimer(this);
connect(mTimer, SIGNAL(timeout()),
this, SLOT(swap()));
}
void
PhoneLineButton::suspend()
{
mTimer.start(500);
_debug("Swapping started.\n");
if(isPressed()) {
mFace = 1;
}
else {
mFace = 0;
}
swap();
mTimer->start(500);
}
void
PhoneLineButton::swap()
{
mFace = (mFace + 1) / 2;
mFace = (mFace + 1) % 2;
resize(mImages[mFace].size());
setPixmap(mImages[mFace]);
}
......@@ -33,14 +45,15 @@ PhoneLineButton::swap()
void
PhoneLineButton::press()
{
mTimer.stop();
_debug("Pressed");
mTimer->stop();
JPushButton::press();
}
void
PhoneLineButton::release()
{
mTimer.stop();
mTimer->stop();
JPushButton::release();
}
......
......@@ -5,10 +5,12 @@
#include <QLabel>
#include <QObject>
#include <QPixmap>
#include <QTimer>
#include "JPushButton.hpp"
class QTimer;
/**
* This class Emulate a PushButton but takes two
* images to display its state.
......@@ -28,19 +30,21 @@ public:
signals:
void clicked(unsigned int);
public slots:
void suspend();
void press();
void release();
public slots:
virtual void suspend();
virtual void press();
virtual void release();
private slots:
void swap();
protected:
void mouseReleaseEvent(QMouseEvent *);
void swap();
private:
unsigned int mLine;
QTimer mTimer;
QTimer *mTimer;
unsigned int mFace;
};
......
......@@ -4,12 +4,16 @@
PhoneLineLocker::PhoneLineLocker(PhoneLine *line)
: mPhoneLine(line)
{
mPhoneLine->lock();
if(mPhoneLine) {
mPhoneLine->lock();
}
}
PhoneLineLocker::~PhoneLineLocker()
{
mPhoneLine->unlock();
if(mPhoneLine) {
mPhoneLine->unlock();
}
}
......@@ -13,36 +13,57 @@ PhoneLineManagerImpl::PhoneLineManagerImpl()
, mCurrentLine(NULL)
{}
PhoneLine *
PhoneLineManagerImpl::getCurrentLine()
{
QMutexLocker guard(&mCurrentLineMutex);
return mCurrentLine;
}
void
PhoneLineManagerImpl::setNbLines(unsigned int nb)
{
mPhoneLines.clear();
for(unsigned int i = 0; i < nb; i++) {
mPhoneLines.push_back(new PhoneLine(mSession.createCall(),i));
mPhoneLines.push_back(new PhoneLine(mSession, i + 1));
}
}
void
PhoneLineManagerImpl::selectAvailableLine()
PhoneLine *
PhoneLineManagerImpl::selectNextAvailableLine()
{
PhoneLine *selectedLine = NULL;
mPhoneLinesMutex.lock();
QMutexLocker guard(&mPhoneLinesMutex);
QMutexLocker guard2(&mCurrentLineMutex);
unsigned int i = 0;
while(i < mPhoneLines.size() && !selectedLine) {
PhoneLineLocker guard(mPhoneLines[i]);
if(mPhoneLines[i]->isAvailable()) {
if(mPhoneLines[i]->isAvailable() &&
mPhoneLines[i] != mCurrentLine) {
selectedLine = mPhoneLines[i];
}
else {
i++;
}
}
mPhoneLinesMutex.unlock();
// If we found one available line.
if(selectedLine) {
selectLine(i);
if(mCurrentLine) {
PhoneLineLocker guard(mCurrentLine);
mCurrentLine->unselect();
}
mCurrentLine = selectedLine;
// select current line.
{
PhoneLineLocker guard(mCurrentLine);
mCurrentLine->select();
}
}
return selectedLine;
}
PhoneLine *
......@@ -59,16 +80,10 @@ PhoneLineManagerImpl::getPhoneLine(unsigned int line)
void
PhoneLineManagerImpl::sendKey(Qt::Key c)
{
PhoneLine *selectedLine = NULL;
mCurrentLineMutex.lock();
selectedLine = mCurrentLine;
mCurrentLineMutex.unlock();
PhoneLine *selectedLine = getCurrentLine();
if(!selectedLine) {
selectAvailableLine();
mCurrentLineMutex.lock();
selectedLine = mCurrentLine;
mCurrentLineMutex.unlock();
selectedLine = selectNextAvailableLine();
}
if(selectedLine) {
......@@ -120,6 +135,51 @@ PhoneLineManagerImpl::selectLine(unsigned int line)
}
void
PhoneLineManagerImpl::call(const QString &)
PhoneLineManagerImpl::call(const QString &to)
{
PhoneLine *current = getCurrentLine();
if(current) {
PhoneLineLocker guard(current);
current->call(to.toStdString());
}
}
void
PhoneLineManagerImpl::call()
{
PhoneLine *current = getCurrentLine();
if(current) {
PhoneLineLocker guard(current);
current->call();
}
}
void
PhoneLineManagerImpl::hold()
{
mCurrentLineMutex.lock();
PhoneLine *selectedLine = mCurrentLine;
PhoneLineLocker guard(selectedLine);
mCurrentLine = NULL;
mCurrentLineMutex.unlock();
if(selectedLine) {
selectedLine->hold();
}
}
void
PhoneLineManagerImpl::hangup()
{
mCurrentLineMutex.lock();
PhoneLine *selectedLine = mCurrentLine;
PhoneLineLocker guard(selectedLine);
mCurrentLine = NULL;
mCurrentLineMutex.unlock();
if(selectedLine) {
selectedLine->hangup();
}
}
......@@ -22,27 +22,11 @@ class PhoneLineManagerImpl : public QObject
public:
PhoneLineManagerImpl();
/**
* This function will make a call on the
* current line. If there's no selected
* line, it will choose the first available.
*/
void call(const QString &to);
/**
* This function hangup the selected line.
* If there's no current line, it does nothing.
*/
void hangup();
/**
* This function hangup the line given in argument.
* If the line is not valid, it doesn't do nothing.
*/
void hangup(unsigned int line);
PhoneLine *getPhoneLine(unsigned int line);
PhoneLine *getCurrentLine();
void setNbLines(unsigned int line);
......@@ -53,13 +37,42 @@ signals:
public slots:
void sendKey(Qt::Key c);
/**
* This function will put the current line
* on hold. If there's no current line,
* it will do nothing.
*/
void hold();
/**
* This function will hanp up the current line
* If there's no current line, it will do nothing.
*/
void hangup();
/**
* This function will make a call on the
* current line. If there's no selected
* line, it will choose the first available.
*/
void call(const QString &to);
/**
* This function will make a call on the
* current line. If there's no selected
* line. It will do nothing. It will call
* the destination contained in the
* PhoneLine buffer, if any.
*/
void call();
/**
* This function will switch the lines. If the line
* is invalid, it just do nothing.
*/
void selectLine(unsigned int line);
void selectAvailableLine();
PhoneLine *selectNextAvailableLine();
private:
Session mSession;
......
......@@ -15,7 +15,12 @@ SFLPhoneApp::SFLPhoneApp(int argc, char **argv)
{
PhoneLineManager::instance().setNbLines(NB_PHONELINES);
Requester::instance().registerObject< Request >(std::string("sendtone"));
Requester::instance().registerObject< Request >(std::string("playdtmf"));
Requester::instance().registerObject< CallRelatedRequest >(std::string("senddtmf"));
Requester::instance().registerObject< CallRelatedRequest >(std::string("call"));
Requester::instance().registerObject< CallRelatedRequest >(std::string("hold"));
Requester::instance().registerObject< CallRelatedRequest >(std::string("unhold"));
Requester::instance().registerObject< CallRelatedRequest >(std::string("hangup"));
}
void
......@@ -29,7 +34,7 @@ SFLPhoneApp::initConnections(SFLPhoneWindow *w)
PhoneLine *line = PhoneLineManager::instance().getPhoneLine(i);
QObject::connect(*pos, SIGNAL(clicked(unsigned int)),
&PhoneLineManager::instance(), SLOT(selectLine(unsigned int)));
QObject::connect(&PhoneLineManager::instance(), SIGNAL(selected()),
QObject::connect(line, SIGNAL(selected()),
*pos, SLOT(press()));
QObject::connect(line, SIGNAL(unselected()),
*pos, SLOT(release()));
......@@ -39,6 +44,12 @@ SFLPhoneApp::initConnections(SFLPhoneWindow *w)
i++;
}
QObject::connect(w->mOk, SIGNAL(clicked()),
&PhoneLineManager::instance(), SLOT(call()));
QObject::connect(w->mHangup, SIGNAL(clicked()),
&PhoneLineManager::instance(), SLOT(hangup()));
QObject::connect(w->mHold, SIGNAL(clicked()),
&PhoneLineManager::instance(), SLOT(hold()));
QObject::connect(w, SIGNAL(keyPressed(Qt::Key)),
&PhoneLineManager::instance(), SLOT(sendKey(Qt::Key)));
}
......@@ -19,6 +19,22 @@ SFLPhoneWindow::SFLPhoneWindow()
resize(main.size());
l->resize(main.size());
mHangup = new JPushButton(QPixmap(":/sflphone/images/hangup_off"),
QPixmap(":/sflphone/images/hangup_on"),
this);
mHangup->move(225,156);
mHold = new JPushButton(QPixmap(":/sflphone/images/hold_off"),
QPixmap(":/sflphone/images/hold_on"),
this);
mHold->move(225,68);
mOk = new JPushButton(QPixmap(":/sflphone/images/ok_off"),
QPixmap(":/sflphone/images/ok_on"),
this);
mOk->move(225,182);
// QLabel *os = new QLabel(this);
// QPixmap overscreen(":/images/overscreen.png");
// os->setPixmap(overscreen);
......@@ -68,9 +84,6 @@ void SFLPhoneWindow::initWindowButtons()
QObject::connect(mMinimizeButton, SIGNAL(clicked()),
this, SLOT(lower()));
mMinimizeButton->move(354,5);
// JPushButton *b = new JPushButton(QPixmap(":/sflphone/images/closeoff.png"),
// QPixmap(":/sflphone/images/closeon.png").
}
void
......
......@@ -30,4 +30,8 @@ private:
JPushButton *mCloseButton;