Commit a3c37ad1 authored by jpbl's avatar jpbl
Browse files

*** empty log message ***

parent 085bd7ce
......@@ -28,17 +28,6 @@ Account::Account(const std::string &sessionId,
, mId(name)
{}
Call
Account::call(const std::string &to)
{
std::string callId = Requester::instance().generateCallId();
std::list< std::string > args;
args.push_back(mId);
args.push_back(to);
Requester::instance().send(mSessionId, "register", args);
return Call(mSessionId, callId);
}
std::string
Account::registerAccount()
{
......
......@@ -33,7 +33,6 @@ class Account {
/**
* This will generate a call ready to be used.
*/
Call call(const std::string &to);
std::string registerAccount();
std::string unregisterAccount();
......
......@@ -38,6 +38,14 @@ Call::Call(const Session &session,
, mId(callId)
{}
std::string
Call::call(const std::string &to)
{
std::list< std::string> args;
args.push_back(mId);
args.push_back(to);
return Requester::instance().send(mSessionId, "call", args);
}
std::string
Call::answer()
......
......@@ -33,6 +33,8 @@ class Call
Call(const Session &session,
const std::string &callId);
std::string call(const std::string &to);
/**
* This function will answer the call.
*/
......
......@@ -44,14 +44,14 @@ void
JPushButton::release()
{
resize(mImages[0].size());
setPixmap (mImages[0]);
setPixmap(mImages[0]);
}
void
JPushButton::press()
{
resize(mImages[1].size());
setPixmap (mImages[1]);
setPixmap(mImages[1]);
}
// Mouse button released
......
......@@ -26,6 +26,7 @@
#include <QLabel>
#include <QPixmap>
#include <QImage>
/**
* This class Emulate a PushButton but takes two
......@@ -40,14 +41,15 @@ public:
QWidget *parent,
Qt::WFlags flags = 0);
~JPushButton();
public slots:
/**
* This function will switch the button
*/
void press();
void release();
private:
protected:
QPixmap mImages[2];
protected:
......@@ -56,7 +58,7 @@ protected:
void mouseMoveEvent(QMouseEvent *);
signals:
void clicked(void);
void clicked();
};
......
#include <iostream>
#include "globals.h"
#include "PhoneLine.hpp"
#include "Call.hpp"
PhoneLine::PhoneLine()
: mCall(NULL)
PhoneLine::PhoneLine(const Call &call,
unsigned int line)
: mCall(call)
, mLine(line)
, mSelected(false)
, mInUse(false)
{}
unsigned int
PhoneLine::line()
{
return mLine;
}
void
PhoneLine::lock()
{
......@@ -21,11 +33,60 @@ PhoneLine::unlock()
void
PhoneLine::select()
{
_debug("I am selected.\n");
if(!mSelected) {
_debug("PhoneLine %d: I am selected.\n", mLine + 1);
mSelected = true;
emit selected();
}
}
void
PhoneLine::unselect()
{
_debug("I am unselected.\n");
if(mSelected) {
_debug("PhoneLine %d: I am unselected.\n", mLine + 1);
mSelected = false;
if(!mInUse) {
mBuffer.clear();
emit backgrounded();
}
else {
emit unselected();
}
}
}
void
PhoneLine::sendKey(Qt::Key c)
{
_debug("PhoneLine %d: Received the character:%s.\n", mLine + 1, QString(c).toStdString().c_str());
switch(c) {
case Qt::Key_Enter:
case Qt::Key_Return:
if(!mInUse) {
return call();
}
break;
default:
if(!mInUse) {
mBuffer += QString(c).toStdString();
}
}
}
void
PhoneLine::call()
{
call(mBuffer);
}
void
PhoneLine::call(const std::string &to)
{
_debug("PhoneLine %d: Calling %s.\n", mLine, to.c_str());
if(!mInUse) {
mInUse = true;
mCall.call(to);
}
}
#include <QChar>
#include <QObject>
#include <QMutex>
#include <string>
class Call;
#include "Call.hpp"
class PhoneLine : public QObject
{
Q_OBJECT
public:
PhoneLine();
PhoneLine(const Call &call, unsigned int line);
~PhoneLine(){}
void call(const std::string &to);
void call();
unsigned int line();
/**
* This will lock the current phone line.
......@@ -31,6 +35,19 @@ public:
*/
void unlock();
/**
* This function will return true if there's no
* activity on this line. It means that even
* if we typed something on this line, but haven't
* started any communication, this will be available.
*/
bool isAvailable()
{return !mInUse;}
void sendKey(Qt::Key c);
public slots:
/**
* The user selected this line.
*/
......@@ -41,11 +58,19 @@ public:
*/
void unselect();
signals:
void selected();
void unselected();
void backgrounded();
private:
Call *mCall;
Call mCall;
QMutex mPhoneLineMutex;
unsigned int mLine;
bool mSelected;
bool mInUse;
//This is the buffer when the line is not in use;
std::string mBuffer;
};
......@@ -9,18 +9,53 @@ PhoneLineButton::PhoneLineButton(const QPixmap &released,
Qt::WFlags flags)
: JPushButton(released, pressed, parent, flags)
, mLine(line)
{}
, mTimer(this)
, mFace(0)
{
connect(&mTimer, SIGNAL(timeout()),
this, SLOT(swap()));
}
void
PhoneLineButton::suspend()
{
mTimer.start(500);
}
void
PhoneLineButton::swap()
{
mFace = (mFace + 1) / 2;
resize(mImages[mFace].size());
setPixmap(mImages[mFace]);
}
void
PhoneLineButton::press()
{
mTimer.stop();
JPushButton::press();
}
void
PhoneLineButton::release()
{
mTimer.stop();
JPushButton::release();
}
void
PhoneLineButton::mouseReleaseEvent (QMouseEvent *e)
{
switch (e->button()) {
case Qt::LeftButton:
release();
// Emulate the left mouse click
if (this->rect().contains(e->pos())) {
emit clicked(mLine);
}
else {
release();
}
break;
default:
......
......@@ -5,6 +5,7 @@
#include <QLabel>
#include <QObject>
#include <QPixmap>
#include <QTimer>
#include "JPushButton.hpp"
......@@ -25,15 +26,23 @@ public:
virtual ~PhoneLineButton(){}
protected:
void mouseReleaseEvent(QMouseEvent *);
signals:
void clicked(unsigned int);
public slots:
void suspend();
void press();
void release();
protected:
void mouseReleaseEvent(QMouseEvent *);
void swap();
private:
unsigned int mLine;
QTimer mTimer;
unsigned int mFace;
};
#endif // defined(__J_PUSH_BUTTON_H__)
#include <QMutexLocker>
#include <iostream>
#include <stdexcept>
#include "globals.h"
......@@ -11,15 +13,67 @@ PhoneLineManager::PhoneLineManager(unsigned int nbLines)
, mCurrentLine(NULL)
{
for(unsigned int i = 0; i < nbLines; i++) {
mPhoneLines.push_back(new PhoneLine());
mPhoneLines.push_back(new PhoneLine(mSession.createCall(),
i));
}
}
void PhoneLineManager::clicked()
void
PhoneLineManager::selectAvailableLine()
{
std::cout << "Clicked" << std::endl;
PhoneLine *selectedLine = NULL;
mPhoneLinesMutex.lock();
unsigned int i = 0;
while(i < mPhoneLines.size() && !selectedLine) {
PhoneLineLocker guard(mPhoneLines[i]);
if(mPhoneLines[i]->isAvailable()) {
selectedLine = mPhoneLines[i];
}
else {
i++;
}
}
mPhoneLinesMutex.unlock();
if(selectedLine) {
selectLine(i);
}
}
PhoneLine *
PhoneLineManager::getPhoneLine(unsigned int line)
{
QMutexLocker guard(&mPhoneLinesMutex);
if(mPhoneLines.size() <= line) {
throw std::runtime_error("Trying to get an invalid Line");
}
return mPhoneLines[line];
}
void
PhoneLineManager::sendKey(Qt::Key c)
{
PhoneLine *selectedLine = NULL;
mCurrentLineMutex.lock();
selectedLine = mCurrentLine;
mCurrentLineMutex.unlock();
if(!selectedLine) {
selectAvailableLine();
mCurrentLineMutex.lock();
selectedLine = mCurrentLine;
mCurrentLineMutex.unlock();
}
if(selectedLine) {
PhoneLineLocker guard(selectedLine);
selectedLine->sendKey(c);
}
}
/**
* Warning: This function might 'cause a problem if
* we select 2 line in a very short time.
......@@ -52,6 +106,9 @@ PhoneLineManager::selectLine(unsigned int line)
PhoneLineLocker guard(selectedLine);
selectedLine->select();
if(selectedLine->isAvailable()) {
mSession.sendTone();
}
}
}
else {
......@@ -60,6 +117,6 @@ PhoneLineManager::selectLine(unsigned int line)
}
void
PhoneLineManager::call(const QString &to)
PhoneLineManager::call(const QString &)
{
}
#ifndef __PHONELINEMANAGER_HPP__
#define __PHONELINEMANAGER_HPP__
#include <Qt>
#include <QObject>
#include <QMutex>
#include <utility>
......@@ -41,24 +42,22 @@ public:
*/
void hangup(unsigned int line);
/**
* This function hangup the
*/
PhoneLine *getPhoneLine(unsigned int line);
signals:
void unselected(unsigned int);
void selected(unsigned int);
public slots:
void clicked();
void sendKey(Qt::Key c);
/**
* This function will switch the lines. If the line
* is invalid, it just do nothing.
*/
void selectLine(unsigned int line);
private:
/**
* Returns the PhoneLine in position line.
*/
PhoneLine *getPhoneLine(unsigned int line);
void selectAvailableLine();
private:
Session mSession;
......
......@@ -75,7 +75,7 @@ Request::toString()
}
CallRequest::CallRequest(const std::string &sequenceId,
CallRelatedRequest::CallRelatedRequest(const std::string &sequenceId,
const std::string &command,
const std::list< std::string > &args)
: Request(sequenceId, command, args)
......@@ -83,7 +83,7 @@ CallRequest::CallRequest(const std::string &sequenceId,
{}
void
CallRequest::onError(const std::string &code, const std::string &message)
CallRelatedRequest::onError(const std::string &code, const std::string &message)
{
onError(Call(Requester::instance().getSessionIdFromSequenceId(getSequenceId()),
mCallId),
......@@ -92,11 +92,11 @@ CallRequest::onError(const std::string &code, const std::string &message)
}
void
CallRequest::onError(Call, const std::string &, const std::string &)
CallRelatedRequest::onError(Call, const std::string &, const std::string &)
{}
void
CallRequest::onEntry(const std::string &code, const std::string &message)
CallRelatedRequest::onEntry(const std::string &code, const std::string &message)
{
onEntry(Call(Requester::instance().getSessionIdFromSequenceId(getSequenceId()),
mCallId),
......@@ -105,11 +105,11 @@ CallRequest::onEntry(const std::string &code, const std::string &message)
}
void
CallRequest::onEntry(Call, const std::string &, const std::string &)
CallRelatedRequest::onEntry(Call, const std::string &, const std::string &)
{}
void
CallRequest::onSuccess(const std::string &code, const std::string &message)
CallRelatedRequest::onSuccess(const std::string &code, const std::string &message)
{
onSuccess(Call(Requester::instance().getSessionIdFromSequenceId(getSequenceId()),
mCallId),
......@@ -118,7 +118,7 @@ CallRequest::onSuccess(const std::string &code, const std::string &message)
}
void
CallRequest::onSuccess(Call, const std::string &, const std::string &)
CallRelatedRequest::onSuccess(Call, const std::string &, const std::string &)
{}
AccountRequest::AccountRequest(const std::string &sequenceId,
......
......@@ -88,12 +88,12 @@ class Request
const std::list< std::string > mArgs;
};
class CallRequest : public Request
class CallRelatedRequest : public Request
{
public:
CallRequest(const std::string &sequenceId,
const std::string &command,
const std::list< std::string > &args);
CallRelatedRequest(const std::string &sequenceId,
const std::string &command,
const std::list< std::string > &args);
/**
......
......@@ -30,9 +30,7 @@ RequesterImpl::RequesterImpl()
: mCallIdCount(0)
, mSessionIdCount(0)
, mSequenceIdCount(0)
{
registerObject< Request >(std::string("register"));
}
{}
SessionIO *
RequesterImpl::getSessionIO(const std::string &sessionId)
......
......@@ -2,7 +2,9 @@
#include "SFLPhoneApp.hpp"
#include "SFLPhoneWindow.hpp"
#include "PhoneLine.hpp"
#include "PhoneLineButton.hpp"
#include "Requester.hpp"
......@@ -11,17 +13,32 @@ SFLPhoneApp::SFLPhoneApp(int argc, char **argv)
, mPhoneLineManager(NB_PHONELINES)
, mSession()
, mAccount(mSession.getDefaultAccount())
{}
{
Requester::instance().registerObject< Request >(std::string("sendtone"));
Requester::instance().registerObject< CallRelatedRequest >(std::string("call"));
}
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 = mPhoneLineManager.getPhoneLine(i);
QObject::connect(*pos, SIGNAL(clicked(unsigned int)),
&mPhoneLineManager, SLOT(selectLine(unsigned int)));
QObject::connect(line, SIGNAL(selected()),
*pos, SLOT(press()));
QObject::connect(line, SIGNAL(unselected()),
*pos, SLOT(release()));
QObject::connect(line, SIGNAL(backgrounded()),
*pos, SLOT(suspend()));
i++;
}
QObject::connect(w, SIGNAL(keyPressed(Qt::Key)),
&mPhoneLineManager, SLOT(sendKey(Qt::Key)));
}
......@@ -2,36 +2,36 @@
#include <QLabel>
#include <QPixmap>
#include <QKeyEvent>
#include <iostream>
#include "globals.h"