Commit e30290b9 authored by jpbl's avatar jpbl
Browse files

PhoneLineManager is a singleton now

parent b0220990
#ifndef __PHONELINEMANAGER_HPP__
#define __PHONELINEMANAGER_HPP__
#include <Qt>
#include <QObject>
#include <QMutex>
#include <utility>
#include <vector>
class PhoneLine;
#include "Account.hpp"
#include "Session.hpp"
/**
* This is the class that manages phone lines
* Copyright (C) 2004-2005 Savoir-Faire Linux inc.
* 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.
*/
class PhoneLineManager : public QObject
{
Q_OBJECT
public:
PhoneLineManager(unsigned int nbLines);
/**
* 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);
signals:
void unselected(unsigned int);
void selected(unsigned int);
public slots:
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);
void selectAvailableLine();
private:
Session mSession;
Account mAccount;
std::vector< PhoneLine * > mPhoneLines;
QMutex mPhoneLinesMutex;
#ifndef __PHONELINEMANAGER_HPP__
#define __PHONELINEMANAGER_HPP__
PhoneLine *mCurrentLine;
QMutex mCurrentLineMutex;
};
#include "utilspp/Singleton.hpp"
#include "PhoneLineManagerImpl.hpp"
typedef utilspp::SingletonHolder< PhoneLineManagerImpl > PhoneLineManager;
#endif
#include <QMutexLocker>
#include <iostream>
#include <stdexcept>
#include "globals.h"
#include "PhoneLine.hpp"
#include "PhoneLineLocker.hpp"
#include "PhoneLineManager.hpp"
PhoneLineManagerImpl::PhoneLineManagerImpl()
: mAccount(mSession.getDefaultAccount())
, mCurrentLine(NULL)
{}
void
PhoneLineManagerImpl::setNbLines(unsigned int nb)
{
mPhoneLines.clear();
for(unsigned int i = 0; i < nb; i++) {
mPhoneLines.push_back(new PhoneLine(mSession.createCall(),i));
}
}
void
PhoneLineManagerImpl::selectAvailableLine()
{
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 *
PhoneLineManagerImpl::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
PhoneLineManagerImpl::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.
*/
void
PhoneLineManagerImpl::selectLine(unsigned int line)
{
PhoneLine *selectedLine = NULL;
// getting the wanted line;
{
mPhoneLinesMutex.lock();
if(mPhoneLines.size() > line) {
selectedLine = mPhoneLines[line];
}
mPhoneLinesMutex.unlock();
}
if(selectedLine != NULL) {
mCurrentLineMutex.lock();
PhoneLine *oldLine = mCurrentLine;
mCurrentLine = selectedLine;
mCurrentLineMutex.unlock();
if(oldLine != selectedLine) {
if(oldLine != NULL) {
PhoneLineLocker guard(oldLine);
oldLine->unselect();
}
PhoneLineLocker guard(selectedLine);
selectedLine->select();
if(selectedLine->isAvailable()) {
mSession.sendTone();
}
}
}
else {
_debug("Tried to selected line %d, which appears to be invalid.\n", line);
}
}
void
PhoneLineManagerImpl::call(const QString &)
{
}
#ifndef __PHONELINEMANAGERIMPL_HPP__
#define __PHONELINEMANAGERIMPL_HPP__
#include <Qt>
#include <QObject>
#include <QMutex>
#include <utility>
#include <vector>
class PhoneLine;
#include "Account.hpp"
#include "Session.hpp"
/**
* This is the class that manages phone lines
*/
class PhoneLineManagerImpl : public QObject
{
Q_OBJECT
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);
void setNbLines(unsigned int line);
signals:
void unselected(unsigned int);
void selected(unsigned int);
public slots:
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);
void selectAvailableLine();
private:
Session mSession;
Account mAccount;
std::vector< PhoneLine * > mPhoneLines;
QMutex mPhoneLinesMutex;
PhoneLine *mCurrentLine;
QMutex mCurrentLineMutex;
};
#endif
......@@ -10,10 +10,10 @@
SFLPhoneApp::SFLPhoneApp(int argc, char **argv)
: QApplication(argc, argv)
, mPhoneLineManager(NB_PHONELINES)
, mSession()
, mAccount(mSession.getDefaultAccount())
{
PhoneLineManager::instance().setNbLines(NB_PHONELINES);
Requester::instance().registerObject< Request >(std::string("sendtone"));
Requester::instance().registerObject< CallRelatedRequest >(std::string("call"));
}
......@@ -26,10 +26,10 @@ SFLPhoneApp::initConnections(SFLPhoneWindow *w)
for(std::list< PhoneLineButton * >::iterator pos = w->mPhoneLineButtons.begin();
pos != w->mPhoneLineButtons.end();
pos++) {
PhoneLine *line = mPhoneLineManager.getPhoneLine(i);
PhoneLine *line = PhoneLineManager::instance().getPhoneLine(i);
QObject::connect(*pos, SIGNAL(clicked(unsigned int)),
&mPhoneLineManager, SLOT(selectLine(unsigned int)));
QObject::connect(line, SIGNAL(selected()),
&PhoneLineManager::instance(), SLOT(selectLine(unsigned int)));
QObject::connect(&PhoneLineManager::instance(), SIGNAL(selected()),
*pos, SLOT(press()));
QObject::connect(line, SIGNAL(unselected()),
*pos, SLOT(release()));
......@@ -40,5 +40,5 @@ SFLPhoneApp::initConnections(SFLPhoneWindow *w)
}
QObject::connect(w, SIGNAL(keyPressed(Qt::Key)),
&mPhoneLineManager, SLOT(sendKey(Qt::Key)));
&PhoneLineManager::instance(), SLOT(sendKey(Qt::Key)));
}
......@@ -21,7 +21,6 @@ public:
void initConnections(SFLPhoneWindow *w);
private:
PhoneLineManager mPhoneLineManager;
Session mSession;
Account mAccount;
};
......
......@@ -20,6 +20,7 @@ HEADERS += Account.hpp \
PhoneLineButton.hpp \
PhoneLineLocker.hpp \
PhoneLineManager.hpp \
PhoneLineManagerImpl.hpp \
Request.hpp \
Requester.hpp \
RequesterImpl.hpp \
......@@ -40,7 +41,7 @@ SOURCES += Account.cpp \
PhoneLine.cpp \
PhoneLineButton.cpp \
PhoneLineLocker.cpp \
PhoneLineManager.cpp \
PhoneLineManagerImpl.cpp \
Request.cpp \
RequesterImpl.cpp \
Session.cpp \
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment