manager.cpp 10.2 KB
Newer Older
savoirfairelinux's avatar
savoirfairelinux committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/**
 *  Copyright (C) 2004 Savoir-Faire Linux inc.
 *  Author : Laurielle Lea <laurielle.lea@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.
 */
19 20 21 22 23
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
savoirfairelinux's avatar
savoirfairelinux committed
24 25 26 27 28 29 30 31 32

#include <cstdio>
#include <cstdlib>
#include <ccrtp/rtp.h>

#include <qapplication.h>
#include <qhostaddress.h>
#include <qwidget.h>

33 34 35
#include "../stund/udp.h"
#include "../stund/stun.h"

savoirfairelinux's avatar
savoirfairelinux committed
36 37
#include "audiodriversoss.h"
#include "configuration.h"
38 39
#include "configurationtree.h"
#include "global.h"
savoirfairelinux's avatar
savoirfairelinux committed
40 41 42 43 44
#include "manager.h"
#include "audiortp.h"
#include "sip.h"
#include "qtGUImainwindow.h"

45 46 47 48
#include <string>
#ifdef  CCXX_NAMESPACES
using namespace std;
#endif
savoirfairelinux's avatar
savoirfairelinux committed
49

savoirfairelinux's avatar
 
savoirfairelinux committed
50 51
Manager::Manager (QString *Dc = NULL) {
	DirectCall = Dc;
savoirfairelinux's avatar
savoirfairelinux committed
52 53 54
	for (int i = 0; i < NUMBER_OF_LINES; i++) {
		phLines[i] = new PhoneLine ();
	}
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75

	// Create .sflphone directory in home user
	bool exist = true;
	char * buffer;
	// Get variable $HOME
  	buffer = getenv ("HOME");                                                   	path = string(buffer);
  	path = path + "/." + PROGNAME;
             
  	if (mkdir (path.data(), 0755) != 0) {
		// If directory	creation failed
    	if (errno != EEXIST) {
        	printf ("Cannot create directory: %s\n", strerror(errno));
      	} 
  	}
  
	// Load user's config
	path = path + "/" + PROGNAME + "rc";
	if (Config::tree()->populateFromFile(path.data()) == 0){
		exist = false;
	}
	
savoirfairelinux's avatar
savoirfairelinux committed
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
	phonegui = new QtGUIMainWindow (0, 0 ,  
					Qt::WDestructiveClose | 
					Qt::WStyle_Customize |
					Qt::WStyle_NoBorder,this);

	sip = new SIP(this);
	audioRTP = new AudioRtp(this->sip, this);
	tone = new ToneGenerator(this);
	
	sip_rtp_init();
	
	selectAudioDriver();

	// Init variables
	b_ringing = false;
	mute = false;
	b_ringtone = false;
savoirfairelinux's avatar
 
savoirfairelinux committed
93 94 95 96 97
	if (! DirectCall->isNull()) { 
		qWarning ("Direct call.....");
		phonegui->lcd->textBuffer = DirectCall ;
		phonegui->dial();
	}
98 99 100 101 102 103 104

	if (!exist){
		// If config file $HOME/.PROGNAME/PROGNAMErc doesn't exist,
  		// show configuration panel
		gui()->configuration();
	} 

savoirfairelinux's avatar
savoirfairelinux committed
105 106 107 108 109 110 111 112 113 114 115 116 117
}

Manager::~Manager (void) {
	delete phonegui;
	delete sip;
	delete audioRTP;
	delete audiodriver;
	delete tone;
	delete[] phLines;
}

void
Manager::selectAudioDriver (void) {
savoirfairelinux's avatar
misc...  
savoirfairelinux committed
118 119
	this->audiodriver = new AudioDriversOSS ();
	/* if (Config::getb(QString("Audio/Drivers.driverOSS"))) {
savoirfairelinux's avatar
savoirfairelinux committed
120
		this->audiodriver = new AudioDriversOSS ();
savoirfairelinux's avatar
misc...  
savoirfairelinux committed
121
	} else if (Config::get(QString("Audio/Drivers.driverALSA"), false)) {
savoirfairelinux's avatar
savoirfairelinux committed
122 123 124 125 126 127 128
		audiodriver = new AudioDriversALSA ();
	}*/
}

void
Manager::sip_rtp_init (void) {
	// Init the SIP and RTP stacks.
129 130 131
	if ( sip->initSIP () != -1) {
		 sip->initRtpmapCodec ();
	}
savoirfairelinux's avatar
savoirfairelinux committed
132
	
133 134
//	if (Config::getb(QString("Preferences/Options.autoregister"))) {
	if (Config::getb("Preferences", "Options.autoregister")) {
savoirfairelinux's avatar
savoirfairelinux committed
135
		// Register to the known proxies if available
136 137
		//if (Config::gets("Signalisations/SIP.password").length() > 0) {
		if (Config::gets("Signalisations", "SIP.password").length() > 0) {
savoirfairelinux's avatar
savoirfairelinux committed
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 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 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 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 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
			sip->setRegister ();
		}
	} 
}

void
Manager::quitLibrary (void) {
	sip->quitSIP();
	audioRTP->rtpexit();
}

int
Manager::outgoingNewCall (void) {
	return sip->outgoingInvite();
}

bool
Manager::ringing (void) {
	return this->b_ringing;
}

// Broadcast ring message to every component
void
Manager::ring (bool var) {
	if (this->b_ringing != var) {
		this->b_ringing = var;
		
		// Blinking bar ring signalisation
	//	gui()->ring(var);

		// Blinking not-used line 
		
		// bruitagesproot->ring(var);
		 
	}
}

void
Manager::ringTone (bool var) {
	if (this->b_ringtone != var) {
		this->b_ringtone = var;
	}
	tonezone = var;
	tone->toneHandle(ZT_TONE_RINGTONE);
}

bool
Manager::getCallInProgress (void) {
	return gui()->callinprogress;
}

void
Manager::setCallInProgress (bool inprogress) {
	gui()->callinprogress = inprogress;
}

bool
Manager::transferedCall(void) {
	return gui()->transfer;
}

/**
 * Handle IP_Phone user's actions
 *
 * @param	lineNumber: line where occurs the action
 * @param 	action:		type of action
 */
void
Manager::actionHandle (int lineNumber, int action) {
	switch (action) {
	case ANSWER_CALL:
		// TODO
		// Stopper l'etat "ringing" du main (audio, signalisation, gui)
		sip->manageActions (lineNumber, ANSWER_CALL);		
		this->ring(false);
		gui()->lcd->setStatus("Connected");	
		qDebug("Start timer call");
		gui()->startCallTimer(lineNumber);
		break;

	case CLOSE_CALL:
		// If the call is active TODO: or is ringing for the callee
		if (sip->call[lineNumber] != NULL) {
			// stop call timer			
			gui()->stopCallTimer(lineNumber);
			sip->manageActions (lineNumber, CLOSE_CALL);
			sip->notUsedLine = -1;	
		}		
		break;

	case ONHOLD_CALL:
		if (sip->call[lineNumber] != NULL) {
			qDebug("Manager ONHOLD:  call[%d] = 0x%X", 
				(unsigned int)lineNumber, sip->call[lineNumber]);
			gui()->lcd->setStatus(ONHOLD_STATUS);
			sip->manageActions (lineNumber, ONHOLD_CALL);
		}
		break;
		
	case OFFHOLD_CALL:
		if (sip->call[lineNumber] != NULL) {
			sip->manageActions (lineNumber, OFFHOLD_CALL);
		}
		break;
		
	case TRANSFER_CALL:
		sip->manageActions (lineNumber, TRANSFER_CALL);
		break;
		
	case CANCEL_CALL:
		sip->manageActions (lineNumber, CANCEL_CALL);
		sip->notUsedLine = -1;
		break;

	default:
		break;
	}
}

int
Manager::newCallLineNumber (void) {
	return sip->notUsedLine;
}

bool
Manager::isRingingLine (int line) {
	if (line == this->newCallLineNumber()) {
		return true;
	}
	return false;
}

bool
Manager::isNotUsedLine (int line) {
	for (int k = 0; k < NUMBER_OF_LINES; k++) {
		if (line == k) {
			if (sip->call[line] == NULL) {
				return true;
			}
		}
	}
	return false;
}

int
Manager::findLineNumberNotUsedSIP (void) {
	return sip->findLineNumberNotUsed();
}

/**
 * Handle the remote callee's events
 *
 * @param	code:		event code
 * @param	reason:		event reason
 * @param	remotetype:	event type
 */
void
Manager::handleRemoteEvent (int code, char * reason, int remotetype) {
	QString qinfo;
	
	switch (remotetype) {
		// Registration success
		case EXOSIP_REGISTRATION_SUCCESS:
			gui()->lcd->setStatus("Logged in");				
			break;

llea's avatar
llea committed
304 305 306 307 308
		// Registration failure
		case EXOSIP_REGISTRATION_FAILURE:
			gui()->lcd->setStatus("Registration failure");				
			break;
			
savoirfairelinux's avatar
savoirfairelinux committed
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
		// Remote callee answered
		case EXOSIP_CALL_ANSWERED:
			if (!gui()->transfer)
				gui()->lcd->setStatus("Connected");
			// Start call timer
			gui()->startCallTimer(gui()->currentLineNumber);
			break;

		// Remote callee hangup
		case EXOSIP_CALL_CLOSED:
			if (getCallInProgress()) {
				this->ring(false);
				gui()->setFreeStateLine(newCallLineNumber());
				setCallInProgress(false);
			} else {
				// Stop call timer
				gui()->stopCallTimer(gui()->currentLineNumber);
				// set state free pixmap line
				gui()->setFreeStateLine(gui()->currentLineNumber);
				// set free line
				gui()->setCurrentLineNumber(-1);
			}
			
			gui()->lcd->clear(QString("Hung up"));
			sip->notUsedLine = -1;
			break;

		// Remote call ringing
		case EXOSIP_CALL_RINGING:
			gui()->lcd->setStatus("Ringing");
			break;
			
		default:
			break;
	}

	// If dialog is established
	if (code == 101) {
		gui()->lcd->setStatus("Ringing");
		
	// if error code
llea's avatar
llea committed
350
	} else { 	
savoirfairelinux's avatar
savoirfairelinux committed
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
		if (code > 399) {
			qinfo = QString::number(code, 10) + " " + 
				QString(reason);
			gui()->lcd->setStatus(qinfo);
		} else if (0 < code < 400) {
			qinfo = QString(reason);
		}
	}
}

int
Manager::startSound (SipCall *ca) {
	return audioRTP->createNewSession(ca);
}

void
Manager::closeSound (SipCall *ca) {
	audioRTP->closeRtpSession (ca);
}

QString
Manager::bufferTextRender (void) {
	return QString(*phonegui->lcd->textBuffer);
}

void
Manager::getInfoStun (StunAddress4& stunSvrAddr) {
	StunAddress4 mappedAddr;
	
	int fd3, fd4; 
    bool ok = stunOpenSocketPair(stunSvrAddr,
                                      &mappedAddr,
                                      &fd3,
                                      &fd4);
	if (ok) {
    	closesocket(fd3);
        closesocket(fd4);
        qDebug("Got port pair at %d", mappedAddr.port);
		firewallPort = mappedAddr.port;
		QHostAddress ha(mappedAddr.addr);
		firewallAddr = ha.toString();
		qDebug("address firewall = %s",firewallAddr.ascii());
	} else {
    	qDebug("Opened a stun socket pair FAILED");
    }
}

int
Manager::getFirewallPort (void) {
	return firewallPort;
}

void
Manager::setFirewallPort (int port) {
	firewallPort = port;
}

QString
Manager::getFirewallAddress (void) {
	return firewallAddr;
}

/**
 * Returs true if the current line replaces another one
 */
bool
Manager::otherLine (void) {
	if (gui()->busyNum != -1) {
		if (gui()->busyNum != gui()->currentLineNumber) {
			return true;
		}
	}
	return false;
}

bool
Manager::isChosenLine (void) {
	return gui()->choose;
}

int
Manager::chosenLine (void) {
	return gui()->chosenLine;
}

void
Manager::setChoose (bool b, bool b2) {
	gui()->choose = b;
	gui()->noChoose = b2;
}

bool
Manager::useStun () {
444 445
//	if (Config::getb("Signalisations/STUN.useStunYes")) {
	if (Config::getb("Signalisations", "STUN.useStunYes")) {
savoirfairelinux's avatar
savoirfairelinux committed
446 447 448 449 450 451 452 453 454
		return true;
	} else {
		return false;
	}
}

// Handle choice of the DTMF-send-way
void
Manager::dtmf (int line, char digit) {
455 456
//	int sendType = Config::geti ("Signalisations/DTMF.sendDTMFas");
	int sendType = Config::geti ("Signalisations", "DTMF.sendDTMFas");
savoirfairelinux's avatar
savoirfairelinux committed
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
	
	switch (sendType) {
		// Audio way
		case 0:
			break;
			
		// SIP INFO
		case 1:
			if (sip->call[line] != NULL) {
				sip->carryingDTMFdigits(line, digit);
			}
			break;

		// rfc 2833
		case 2:
			break;
			
		default:
			break;
	}
}