qtGUImainwindow.cpp 32.8 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 19 20 21 22 23
/**
 *  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.
 */


////////////////////////////////////////////////////////////////////////////////
// QtGUIMainWindow Implementation                                             //
////////////////////////////////////////////////////////////////////////////////
24 25

#include "configurationtree.h"
savoirfairelinux's avatar
savoirfairelinux committed
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 60 61 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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 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
#include <stdio.h>

#include <qbitmap.h>
#include <qcheckbox.h>
#include <qcombobox.h>
#include <qevent.h>
#include <qinputdialog.h>
#include <qlineedit.h>
#include <qmessagebox.h>
#include <qpushbutton.h>
#include <qregexp.h>
#include <qsettings.h>
#include <qspinbox.h>
#include <qtimer.h>
#include <qtooltip.h>

#include "audiodrivers.h"
#include "configuration.h"
#include "configurationpanelui.h"
#include "global.h"
#include "jpushbutton.h"
#include "manager.h"
#include "numerickeypadtools.h"
#include "skin.h"
#include "qtGUImainwindow.h"

#define QCHAR_TO_STRIP	"-"
#define REG_EXPR		"(-|\\(|\\)| )"
	

///////////////////////////////////////////////////////////////////////////////
// Tray Icon implementation
///////////////////////////////////////////////////////////////////////////////
MyTrayIcon::MyTrayIcon(const QPixmap &icon, const QString &tooltip, 
		QPopupMenu *mypop, QObject *parent, const char *name)
		: TrayIcon (icon, tooltip, mypop, parent, name)
{
	menu = mypop;
}

void
MyTrayIcon::mousePressEvent (QMouseEvent *e) 
{
	switch ( e->button() ) {
		case RightButton:
			menu->popup( e->globalPos() );
			e->accept();
			break;
			
		case LeftButton:
			emit clickedLeft();
			break;
			
		case MidButton:
			break;
			
		default:
			break;
	}
}

///////////////////////////////////////////////////////////////////////////////
// QtGUIMainWindow implementation
///////////////////////////////////////////////////////////////////////////////

/**
 * Default Constructor
 * Init, Connections
 */
QtGUIMainWindow::QtGUIMainWindow (QWidget *parent, const char *name, WFlags f, 
									Manager *mngr) 
									: TransQWidget (parent, name, f) {

	// Create configuration panel
	panel = new ConfigurationPanel (0, 0, false);

	// Address book dialog
	phonebook = new PhoneBook (0, 0, false);

	// URL input dialog
	urlinput = new URL_Input (this);
	
	// For managing 
	this->callmanager = mngr;

	// For DTMF
	key = new DTMF ();
	buf = new short[SIZEBUF];

	// Load file configuration skin
	QString skinfilename(Skin::getPath(QString(SKINDIR), setPathSkin(),
				QString(FILE_INI)));
	ExistingDF.SetFileName(skinfilename);
	ExistingDF.Load(skinfilename);
	
	// Initialisations
	this->initButtons();
	this->initBlinkTimer();

 	// Initialisation variables for ringing and message
	msgVar = false;
	b_dialtone = false;
	
	// Load background image phone
	setbgPixmap (new QPixmap (Skin::getPath(QString(SKINDIR), 
											setPathSkin(),
											QString(PIXMAP_PHONE))));
	
	// Transform pixmap to QImage
	setSourceImage ();

	this->setMaximumSize (getSourceImage().width(), getSourceImage().height());
	this->setGeometry (MAIN_INITIAL_POSITION, 
					   MAIN_INITIAL_POSITION, 
					   getSourceImage().width(),
					   getSourceImage().height());
		
	// Calculate just one time the transparency mask bit to bit
	transparencyMask ();

	// By default, keyboard mapping mode is numerical mode
	this->setMode(NUM_MODE);
	
	// Connect blinkTimer signals to blink slot
    connect(blinkTimer, SIGNAL(timeout()),this, SLOT(blinkMessageSlot()));
	connect (blinkTimer, SIGNAL(timeout()), this, SLOT(blinkRingSlot()) );
	connect (blinkTimer, SIGNAL(timeout()), this, SLOT(blinkLineSlot()));
	
	// Line pixmaps initialisation
	for (int i = 0; i < NUMBER_OF_LINES; i++) {
		for (int j = 0; j < NUMBER_OF_STATES; j++) {
	 		TabLinePixmap[i][j] = QPixmap(Skin::getPath(QString(SKINDIR),
												setPathSkin(), 
												QString(PIXMAP_LINE(i, j))));
	 	}
	}
	
	currentLineNumber = -1;
	onLine = currentLineNumber;
	callinprogress = false;
	chosenLine = -1;
	choose = false;
	noChoose = false;
	transfer = false;
	
	// Message pixmaps initialisation
	TabMsgPixmap[0] = QPixmap(Skin::getPath(QString(SKINDIR), setPathSkin(), 
						PIXMAP_MESSAGE_OFF));
	TabMsgPixmap[1] = QPixmap(Skin::getPath(QString(SKINDIR), setPathSkin(), 
						PIXMAP_MESSAGE_ON));
	
	// Create new display and numeric keypad
	lcd = new MyDisplay(this, 0, this);
	keypad = new NumericKeypad (this, NULL, Qt::WDestructiveClose |
                    					Qt::WStyle_Customize |
                    					Qt::WStyle_NoBorder);

	this->first = true;
	
	// Move 
	setMainLCD ();

	// Connect to append url in display
	connect (urlinput->buttonOK, SIGNAL(clicked()), this, SLOT(stripSlot()));

	// Connect to apply settings
	connect (panel->buttonApply, SIGNAL(clicked()), this, SLOT(applySlot()));
llea's avatar
llea committed
193 194
	// Connect to save settings
	connect (panel->buttonSave, SIGNAL(clicked()), this, SLOT(save()));
savoirfairelinux's avatar
savoirfairelinux committed
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
	
	// Handle keyboard events
	// Connect for clicked numeric keypad button 
	connect ((QObject*)keypad->key0, SIGNAL(clicked()), this, 
			SLOT(pressedKey0()));
	connect ((QObject*)keypad->key1, SIGNAL(clicked()), this, 
			SLOT(pressedKey1()));
	connect ((QObject*)keypad->key2, SIGNAL(clicked()), this, 
			SLOT(pressedKey2()));
	connect ((QObject*)keypad->key3, SIGNAL(clicked()), this, 
			SLOT(pressedKey3()));
	connect ((QObject*)keypad->key4, SIGNAL(clicked()), this, 
			SLOT(pressedKey4()));
	connect ((QObject*)keypad->key5, SIGNAL(clicked()), this, 
			SLOT(pressedKey5()));
	connect ((QObject*)keypad->key6, SIGNAL(clicked()), this, 
			SLOT(pressedKey6()));
	connect ((QObject*)keypad->key7, SIGNAL(clicked()), this, 
			SLOT(pressedKey7()));
	connect ((QObject*)keypad->key8, SIGNAL(clicked()), this, 
			SLOT(pressedKey8()));
	connect ((QObject*)keypad->key9, SIGNAL(clicked()), this, 
			SLOT(pressedKey9()));
	connect ((QObject*)keypad->keyStar, SIGNAL(clicked()), this, 
			SLOT(pressedKeyStar()));
	connect ((QObject*)keypad->keyHash, SIGNAL(clicked()), this, 
			SLOT(pressedKeyHash()));
	connect ((QObject*)keypad->keyClose, SIGNAL(clicked()), this, 
			SLOT(dtmfKeypad()));

	// Connections for the lines 
	connect (callmanager->phLines[0]->button(), SIGNAL(clicked()), this, 
			SLOT(button_line0()));
	connect (callmanager->phLines[1]->button(), SIGNAL(clicked()), this, 
			SLOT(button_line1()));
	connect (callmanager->phLines[2]->button(), SIGNAL(clicked()), this, 
			SLOT(button_line2()));
	connect (callmanager->phLines[3]->button(), SIGNAL(clicked()), this, 
			SLOT(button_line3()));
	connect (callmanager->phLines[4]->button(), SIGNAL(clicked()), this, 
			SLOT(button_line4()));
	connect (callmanager->phLines[5]->button(), SIGNAL(clicked()), this, 
			SLOT(button_line5()));

	// Misc 
	connect (phoneKey_msg, SIGNAL(clicked()), this, SLOT(button_msg()));
	connect (phoneKey_transf, SIGNAL(clicked()), this, SLOT(button_transfer()));
	connect (phoneKey_conf, SIGNAL(clicked()), this, SLOT(button_conf()));
	connect (dial_button, SIGNAL(clicked()), this, SLOT(dial()));
	connect (mute_button, SIGNAL(clicked()), this, SLOT(button_mute()));
	connect (hangup_button, SIGNAL(clicked()), this, SLOT(hangupLine()));
	connect (configuration_button,SIGNAL(clicked()),this,SLOT(configuration()));
	connect (addr_book_button, SIGNAL(clicked()), this,SLOT(addressBook()));
	connect (dtmf_button, SIGNAL(clicked()), this, SLOT(dtmfKeypad()));

	// Connect to reduce
llea's avatar
llea committed
251
	connect (reduce_button, SIGNAL(clicked()), this, SLOT(reduceHandle()));
savoirfairelinux's avatar
savoirfairelinux committed
252 253 254 255 256 257
	// Connect to quit with keyboard
	connect (this, SIGNAL(keyPressed(int)), this, SLOT(quitApplication()));
   	// Connect to quit with quit button
	connect (quit_button, SIGNAL(clicked()), this, SLOT(quitApplication()));
	
	// To register when program is launched
llea's avatar
llea committed
258 259
	//if (Config::getb("Preferences", "Options.autoregister") 	
	//		and panel->password->text() == "") { 
260
	//	configuration ();
llea's avatar
llea committed
261
	//}
savoirfairelinux's avatar
savoirfairelinux committed
262 263 264 265 266 267 268 269 270 271 272 273

	// Change window title and Icon.
	this->setCaption(PROGNAME);
	this->setIcon(QPixmap(Skin::getPathPixmap(QString(PIXDIR), 
				QString(SFLPHONE_LOGO))));

	// Init ringing state (not ringing)
	//this->ring(false);

	// Show the GUI
	this->show();	

274
	// Handle the tray icon system
savoirfairelinux's avatar
savoirfairelinux committed
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 304 305 306 307 308 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
	mypop = new QPopupMenu(this);
	mypop->insertItem ("Quit", qApp, SLOT(quit()));

	trayicon = new MyTrayIcon(QPixmap(
				Skin::getPathPixmap(QString(PIXDIR), QString(TRAY_ICON))), 
				NULL, mypop, parent, name);
	trayicon->show();
	connect(trayicon, SIGNAL(clickedLeft()), this, SLOT(clickHandle()));
	
}

/**
 * Destructor
 */
QtGUIMainWindow::~QtGUIMainWindow(void) {
	delete  phoneKey_transf;
	delete  phoneKey_msg;
	delete  phoneKey_conf;
	delete  phoneKey_line0;
	delete  phoneKey_line1;
	delete  phoneKey_line2;
	delete  phoneKey_line3;
	delete  reduce_button;
	delete  quit_button;
	delete  addr_book_button;
	delete  configuration_button;
	delete  hangup_button;
	delete  dial_button;
	delete  mute_button;
	delete	dtmf_button;
	delete	vol_mic;
	delete 	vol_spkr;
	delete	panel;
	delete  blinkTimer;
	delete  keypad;
	delete	lcd;
	delete  urlinput;
	delete	callmanager;
	delete 	mypop;
	delete 	trayicon;

	for (int j = 0; j < NUMBER_OF_LINES; j++) {
		delete callmanager->phLines[j]->button();
	}
}



///////////////////////////////////////////////////////////////////////////////
// Private Methods implementations     	                               	     
///////////////////////////////////////////////////////////////////////////////
/**
 * Init and start blink timer for all blinking pixmap, with 500 ms timeout.
 */
void
QtGUIMainWindow::initBlinkTimer(void) {
	blinkTimer = new QTimer(this);
	blinkTimer->start(500);
}

/**
 * Init variable with skin choice
 */
QString
QtGUIMainWindow::setPathSkin (void) {
340 341
	return QString(Config::getchar(
					"Preferences", "Themes.skinChoice", "metal"));
savoirfairelinux's avatar
savoirfairelinux committed
342 343 344 345 346 347 348 349 350 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 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
}

/**
 * Inits all phonekey buttons.
 * Create new QPushButtons, set up tool tip, disable focus, set button geometry
 * set palette.
 */
void
QtGUIMainWindow::initButtons (void) {
	// Buttons initialisation
	phoneKey_msg= new JPushButton(this, NULL, "voicemail");
	phoneKey_transf = new JPushButton(this, NULL, "transfer");
	phoneKey_conf = new JPushButton(this, NULL, "conference");
	reduce_button = new JPushButton(this, NULL, "minimize");
	quit_button = new JPushButton(this, NULL, "close");
	addr_book_button = new JPushButton(this, NULL, "directory");
	configuration_button = new JPushButton(this, NULL, "setup");
	hangup_button = new JPushButton(this, NULL, "hangup");
	dial_button = new JPushButton(this, NULL, "ok");
	mute_button = new JPushButton(this, NULL, "mute");
	dtmf_button = new JPushButton(this, NULL, "dtmf");

	// Set tooltip buttons
	QToolTip::add(reduce_button, tr("Minimize window"));
	QToolTip::add(quit_button, tr("Close window (Ctrl+Q)"));
	QToolTip::add(phoneKey_msg, tr("Get your message"));
	QToolTip::add(phoneKey_transf, tr("Call transfer"));
	QToolTip::add(phoneKey_conf, tr("Conference"));
	QToolTip::add(addr_book_button, tr("Address book"));
	QToolTip::add(configuration_button, tr("Configuration tools (Ctrl+C)"));
	QToolTip::add(hangup_button, tr("Hangup"));
	QToolTip::add(dial_button, tr("Dial"));
	QToolTip::add(mute_button, tr("Mute"));
	QToolTip::add(dtmf_button, tr("Show DTMF keypad"));
	
	// Buttons position
	phoneKey_msg->move (
			ExistingDF.GetInt("msg_x","Positions"), 
			ExistingDF.GetInt("msg_y","Positions"));
	phoneKey_transf->move (
			ExistingDF.GetInt("transf_x","Positions"), 
			ExistingDF.GetInt("transf_y","Positions"));
	phoneKey_conf->move (
			ExistingDF.GetInt("conf_x","Positions"), 
			ExistingDF.GetInt("conf_y","Positions"));
	reduce_button->move (
			ExistingDF.GetInt("reduce_x","Positions"), 
			ExistingDF.GetInt("reduce_y","Positions"));
	addr_book_button->move (
			ExistingDF.GetInt("addr_book_x","Positions"), 
			ExistingDF.GetInt("addr_book_y","Positions"));
	quit_button->move (
			ExistingDF.GetInt("quit_x","Positions"), 
			ExistingDF.GetInt("quit_y","Positions"));		
	configuration_button->move (
			ExistingDF.GetInt("configuration_x","Positions"), 
			ExistingDF.GetInt("configuration_y","Positions"));
	hangup_button->move (
			ExistingDF.GetInt("hangup_x","Positions"), 
			ExistingDF.GetInt("hangup_y","Positions"));
	dial_button->move (
			ExistingDF.GetInt("dial_x","Positions"), 
			ExistingDF.GetInt("dial_y","Positions"));
	mute_button->move (
			ExistingDF.GetInt("mute_x","Positions"), 
			ExistingDF.GetInt("mute_y","Positions"));
	dtmf_button->move (
			ExistingDF.GetInt("dtmf_x","Positions"), 
			ExistingDF.GetInt("dtmf_y","Positions"));
				

	// Loop for line buttons
	//Initialisation, set no focus, set geometry, set palette, pixmap
	for (int j = 0; j < NUMBER_OF_LINES; j++) {
		QString lnum;

		lnum = "l" + lnum.setNum (j + 1);
		callmanager->phLines[j]->setButton(new JPushButton(
					this, NULL, lnum.ascii()));
		callmanager->phLines[j]->button()->move (
					ExistingDF.GetInt(lnum + "_x","Positions"), 
					ExistingDF.GetInt(lnum + "_y","Positions"));
	}

	// Set pixmaps volume TODO:change to thing like slider
	vol_mic = new JPushButton(this, NULL, "volume");
	vol_mic->move(
			ExistingDF.GetInt("vol_mic_x","Positions"), 
			ExistingDF.GetInt("vol_mic_y","Positions"));
	vol_spkr = new JPushButton(this, NULL, "volume");
	vol_spkr->move(
			ExistingDF.GetInt("vol_spkr_x","Positions"), 
			ExistingDF.GetInt("vol_spkr_y","Positions"));
}

/**
 * Returns true if the keyboard mapping returns letters. 
 *
 * @return	bool
 */
bool
QtGUIMainWindow::isInTextMode (void) {
	if (modeType == TEXT_MODE) {
		return true;
	} else {
		return false;
	}
}

/**
 * Returns true if the keyboard mapping returns digits. 
 *
 * @return	bool
 */
bool
QtGUIMainWindow::isInNumMode (void) {
	if (modeType == NUM_MODE) {
		return true;
	} else {
		return false;
	}
}

/**
 * Sets up the keyboard mapping mode.
 */
void
QtGUIMainWindow::setMode (int mode) {
	this->modeType = mode;
}

/**
 * Search the busy line number among all lines and different of current line
 * number.
 *
 * @return	number of busy line
 */
int
QtGUIMainWindow::numLineBusy(void) {
	int temp = -1;
	for (int i = 0; i < NUMBER_OF_LINES; i++) {
		if (callmanager->phLines[i]->isBusy() && i != currentLineNumber) {
			temp = i;	
		} 
	}
	return temp;
}

void
QtGUIMainWindow::stopBlinkingRingPixmap (void) {
	// Initialisation of red bar rectangle region
	QRect rect(ExistingDF.GetInt("ring_x","Positions"), 
			ExistingDF.GetInt("ring_y","Positions"),
			imageRing.width(), imageRing.height());

	// Blit the no-ringing pixmap with update rectangle
	bitBlt (this, ExistingDF.GetInt("ring_x","Positions"), 
			ExistingDF.GetInt("ring_y","Positions"),&imageNoRing, 0, 0,
			imageRing.width(), imageRing.height(), Qt::CopyROP);
	update(rect);
}

void
QtGUIMainWindow::dialTone (bool var) {
	if (this->b_dialtone != var) {
		this->b_dialtone = var;
	}
	callmanager->tonezone = var;
	callmanager->tone->toneHandle(ZT_TONE_DIALTONE); 
	
}
///////////////////////////////////////////////////////////////////////////////
// Public Methods implementations                                            //
///////////////////////////////////////////////////////////////////////////////

/**
 * Initializes LCD display, and move it at the configuration file position.
 *
 * @param	lcd
 */
void
QtGUIMainWindow::setMainLCD (void) {
	// Screen initialisation
	this->lcd->move (ExistingDF.GetInt("display_x","Positions"), 
					ExistingDF.GetInt("display_y","Positions"));
}

void
QtGUIMainWindow::setCurrentLineNumber(int newcur) {
	this->currentLineNumber = newcur;
}

/**
 * Sets the corresponding pixmap button according to its state.
 * Handle operations between lines (on hold, off hold) and dial tone 
 * 
 * @param	num_line: number of the current line
 */
void
QtGUIMainWindow::toggleLine (int num_line) {
	if ( num_line == -1 ){
		qDebug("Should not arrived !!!!!!!%d\n", num_line);
		return ;
	}
	//Current line number
	currentLineNumber = num_line;
	// Change state when click on line button
	callmanager->phLines[currentLineNumber]->toggleState();
	// If another line is busy
	busyNum = numLineBusy();

	if (callmanager->isNotUsedLine(currentLineNumber) and busyNum == -1) {
		qDebug("GUI: PREMIERE LIGNE occupee %d", currentLineNumber);
		lcd->setStatus("Enter Phone Number:");
		chosenLine = currentLineNumber;
		if (!noChoose) {
			choose = true; 
savoirfairelinux's avatar
savoirfairelinux committed
559
			this->dialTone(true);
savoirfairelinux's avatar
savoirfairelinux committed
560 561 562 563 564 565 566 567 568 569 570 571
		}
		callmanager->phLines[currentLineNumber]->setStateLine(BUSY);
	} 

	// Occurs when newly off-hook line replaces another one.
	if (busyNum != currentLineNumber && busyNum != -1) {
		if (callmanager->isNotUsedLine(currentLineNumber)) {
			qDebug("GUI: Prend nouvelle ligne %d", currentLineNumber);
			lcd->clear(QString("Enter Phone Number:"));
			chosenLine = currentLineNumber;
			if (!noChoose) {
				choose = true;
savoirfairelinux's avatar
savoirfairelinux committed
572
				this->dialTone(true);
savoirfairelinux's avatar
savoirfairelinux committed
573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672
			}	
		}
		// Change state to ONHOLD
		callmanager->phLines[busyNum]->setState(ONHOLD);
		callmanager->phLines[busyNum]->setStateLine(ONHOLD);
		callmanager->actionHandle (busyNum, ONHOLD_CALL);
			
		callmanager->phLines[currentLineNumber]->setState(BUSY);
		qDebug("GUI: state ON-HOLD line busyNum %d", busyNum);
	}

	if (callmanager->phLines[currentLineNumber]->isBusy()) {
		qDebug("GUI: isBusy line %d", currentLineNumber);
		// Change line button pixmap to "line in use" state.
		callmanager->phLines[currentLineNumber]->button()->setPixmap(
				TabLinePixmap[currentLineNumber][BUSY]);
		callmanager->phLines[currentLineNumber]->setState(BUSY);
		
		// Answer new call
		if (callmanager->isRingingLine(currentLineNumber) and 
			callmanager->phLines[currentLineNumber]->getStateLine() != ONHOLD){
			qDebug("GUI: -- Nouvel appel repondu %d --", currentLineNumber);
			callmanager->actionHandle (currentLineNumber, ANSWER_CALL);
			stopBlinkingRingPixmap ();
			callmanager->phLines[currentLineNumber]->setState(BUSY);
			callmanager->phLines[currentLineNumber]->setStateLine(BUSY);
		} 
		else if (callmanager->phLines[currentLineNumber]->getStateLine() 
																== ONHOLD) {
			qDebug("GUI: state OFF-HOLD line %d", currentLineNumber);
			lcd->clear(QString("Connected"));
			callmanager->phLines[currentLineNumber]->setStateLine(OFFHOLD);
			callmanager->actionHandle (currentLineNumber, OFFHOLD_CALL);
			lcd->appendText(callmanager->phLines[currentLineNumber]->text);
		}
	} 
	else if (callmanager->phLines[currentLineNumber]->isOnHold()){
		// Change state to ONHOLD
		callmanager->phLines[currentLineNumber]->setState(ONHOLD);
		callmanager->phLines[currentLineNumber]->setStateLine(ONHOLD);
		// Change status to ONHOLD
		qDebug("GUI: state ON-HOLD line %d", currentLineNumber);
		callmanager->actionHandle (currentLineNumber, ONHOLD_CALL);
	}
}

/**
 * Actions occur when click on hang off button. 
 * Use for validate incoming and outgoing call, transfer call.
 */
void
QtGUIMainWindow::dial (void) {
	int i = 0;
	if (transfer and callmanager->sip->call[currentLineNumber] != NULL
			and currentLineNumber != -1) {
		callmanager->actionHandle (currentLineNumber, TRANSFER_CALL);
		transfer = false;
	} else {
		qDebug("GUI: LINE CURRENT %d", currentLineNumber);
		// If new incoming call
		// Stop blinking ring pixmap and answer.
		if (callmanager->ringing()) {
			stopBlinkingRingPixmap();
			currentLineNumber = callmanager->newCallLineNumber();
			toggleLine (currentLineNumber);
		} else {
			// If new outgoing call
			// For new outgoing call with INVITE SIP request
			i = callmanager->outgoingNewCall();
			if (i == 0) {
				qDebug("Choix de la ligne (yes/no) %d",choose); 
				if (!choose) {
					noChoose = true;
					currentLineNumber = callmanager->findLineNumberNotUsedSIP();
				} else {
					currentLineNumber = chosenLine;
				}
				callmanager->phLines[currentLineNumber]->text = 
								callmanager->bufferTextRender();
				toggleLine (currentLineNumber);
				callinprogress = true;	

				// RingTone
				// TODO: callmanager->ringTone(true);
			}
		}	

		if (i == 0) {
			// Set used-state pixmap 'currentLineNumber' line
			callmanager->phLines[currentLineNumber]->button()->setPixmap(
						TabLinePixmap[currentLineNumber][BUSY]);
			callmanager->phLines[currentLineNumber]->setState(BUSY);
		}
	}
}

/**
 * Hangup the current call.
 */
void
savoirfairelinux's avatar
savoirfairelinux committed
673
QtGUIMainWindow::hangupLine (void) {  
savoirfairelinux's avatar
savoirfairelinux committed
674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782
	qDebug("HANGUP: line %d", currentLineNumber);
	// If there is current line opened and state line not onHold
	if (currentLineNumber != -1 and 
			!(callmanager->phLines[currentLineNumber]->isOnHold())) {
		// set free pixmap
		setFreeStateLine (currentLineNumber);
		
		if (callinprogress) {
			callmanager->actionHandle (currentLineNumber, CANCEL_CALL);
		} else {
			// Hang up the call
			this->dialTone(false);
			callmanager->actionHandle (currentLineNumber, CLOSE_CALL);
		}
		lcd->clear(QString("Hung up"));
		setCurrentLineNumber(-1);
	}
	
	choose = false;
	noChoose = false;

	// Just to test when  receive a message
#if 0
	msgVar = true;
#endif
}

/**
 * Stop the blinking message slot and load the message-off button pixmap.
 */
void
QtGUIMainWindow::stopTimerMessage (void) {
	msgVar = false;	
	phoneKey_msg->setPixmap(TabMsgPixmap[FREE]);
}

/**
 * Set pixamp of free state.
 */
void
QtGUIMainWindow::setFreeStateLine (int line) {
	// Set free-status for current line
	callmanager->phLines[line]->setState (FREE);
	// Set free-pixmap
	callmanager->phLines[line]->button()->setPixmap( TabLinePixmap[line][FREE]);
}

/**
 * Stop the call timer.
 * 
 * @param	line: number of line 
 */
void
QtGUIMainWindow::stopCallTimer (int line) {
	// Stop the call timer when hang up
	if (callmanager->phLines[line]->timer != NULL) {
		callmanager->phLines[line]->stopTimer();
	}
	// No display call timer, display current hour
	lcd->inFunction = false;
	callmanager->phLines[line]->first = true;
}

/**
 * Start the call timer.
 * 
 * @param	line: number of line 
 */
void
QtGUIMainWindow::startCallTimer (int line) {
	// Call-timer enable
	lcd->inFunction = true;
	
	// To start the timer for display text just one time
	if (callmanager->phLines[line]->first) {
		callmanager->phLines[line]->startTimer();
		callmanager->phLines[line]->first = false;
	}
}

///////////////////////////////////////////////////////////////////////////////
// Public slot implementations                                               //
///////////////////////////////////////////////////////////////////////////////
/**
 * Slot to blink with free and busy pixmaps when line is hold.
 */
void
QtGUIMainWindow::blinkLineSlot (void) {
	static bool isOn = false;
	int state = BUSY;
	
	if	(!isOn) {
		state = FREE;	
	}

	for (int i = 0; i < NUMBER_OF_LINES; i++) {
		// If lines are hold on, set blinking pixmap
		if (callmanager->phLines[i]->isOnHold()) {
			callmanager->phLines[i]->button()->setPixmap(TabLinePixmap[i][state]);
		}
	}
	isOn = !isOn;
}

// Dial the voicemail Number automatically when button is clicked
void
QtGUIMainWindow::button_msg (void) {
	 stopTimerMessage();
	 lcd->clear("Voicemail");
783
	 lcd->appendText(Config::getchar("Preferences","Options.voicemailNumber",""));
savoirfairelinux's avatar
savoirfairelinux committed
784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804
	 dial();
}

// Allow to enter a phone number to transfer the current call.
// This number is validated by ok-button or typing Enter
void
QtGUIMainWindow::button_transfer (void) {
	if (currentLineNumber != -1) {
		transfer = true;
		callmanager->actionHandle (currentLineNumber, ONHOLD_CALL);
		lcd->clear(QString("Transfer to:"));
	}
}

void
QtGUIMainWindow::button_conf (void) {
//TODO: This feature is not implemented yet
}

void
QtGUIMainWindow::button_line0 (void) {
savoirfairelinux's avatar
savoirfairelinux committed
805
//	this->dialTone(true);
savoirfairelinux's avatar
savoirfairelinux committed
806 807 808 809 810 811
 	toggleLine (0);
	 
}

void
QtGUIMainWindow::button_line1 (void) {
savoirfairelinux's avatar
savoirfairelinux committed
812
//	this->dialTone(true);
savoirfairelinux's avatar
savoirfairelinux committed
813 814 815 816 817
	toggleLine (1);
}

void
QtGUIMainWindow::button_line2 (void) {
savoirfairelinux's avatar
savoirfairelinux committed
818
//	this->dialTone(true);
savoirfairelinux's avatar
savoirfairelinux committed
819 820 821 822 823
	toggleLine (2);
}

void
QtGUIMainWindow::button_line3 (void) {
savoirfairelinux's avatar
savoirfairelinux committed
824
//	this->dialTone(true);
savoirfairelinux's avatar
savoirfairelinux committed
825 826 827 828 829
	toggleLine (3);
}

void
QtGUIMainWindow::button_line4 (void) {
savoirfairelinux's avatar
savoirfairelinux committed
830
//	this->dialTone(true);
savoirfairelinux's avatar
savoirfairelinux committed
831 832 833 834 835
	toggleLine (4);
}

void
QtGUIMainWindow::button_line5 (void) {
savoirfairelinux's avatar
savoirfairelinux committed
836
//	this->dialTone(true);
savoirfairelinux's avatar
savoirfairelinux committed
837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977
	toggleLine (5);
}

void
QtGUIMainWindow::button_mute(void) {
	// Disable micro sound 
	static bool isOn = true;
	
	if(!isOn) {
		callmanager->mute = false;	
		lcd->clear("Mute off");
	} else { 
		callmanager->mute = true;	
		lcd->clear("Mute on");		
	}
	
	isOn = !isOn;
}

// Show the setup panel
void
QtGUIMainWindow::configuration (void) {
	 panel->show();
}

void
QtGUIMainWindow::addressBook (void) {
//	TODO: phonebook->show();
	QMessageBox::information(this, "Information",
		"This feature is not implemented yet", QMessageBox::Yes);
}

// Handle the dtmf-button click
void
QtGUIMainWindow::dtmfKeypad (void) {
	if (keypad->isVisible()) {
		// Hide keypad if it's visible.
		keypad->hide();
	} else {
		
		if (this->first and !getMoved()) {
			// If it's the first time that keypad is shown.
			// The position is fixed with the main initial position.
			this->first = false;
			keypad->setGeometry (MAIN_INITIAL_POSITION + 
									this->getSourceImage().width(), 
							 	MAIN_INITIAL_POSITION, 
							 	keypad->getSourceImage().width(),
							 	keypad->getSourceImage().height());
		} else {
			// If main window is moved, we calculate the keypad new position 
			// to fix it with main window
			if (getMoved()) {
				keypad->setGeometry (positionOffsetX(), 
							 	getGlobalMouseY() - getMouseY(), 
							 	keypad->getSourceImage().width(),
							 	keypad->getSourceImage().height());
			}
			if (keypad->getMoved()) {
				// If keypad is moved, it shows at the previous position
				keypad->setGeometry (
								keypad->getGlobalMouseX()-keypad->getMouseX(), 
							 	keypad->getGlobalMouseY()-keypad->getMouseY(), 
							 	keypad->getSourceImage().width(),
							 	keypad->getSourceImage().height());
			}
		}

		// Show keypad if it's hidden.		
	 	keypad->show();
	}
}

// Get x-position offset, related to the screen size, to show numeric keypad
int
QtGUIMainWindow::positionOffsetX (void) {
	QRect screenRect;
	int offset;

	// Get the screen geometry
	screenRect = (QApplication::desktop())->screenGeometry (0);
	
	offset = this->getSourceImage().width() - getMouseX() + getGlobalMouseX();
	if (offset + keypad->getSourceImage().width() > screenRect.right()) {
		return getGlobalMouseX() - (
								getMouseX() + keypad->getSourceImage().width());
	} else {
		return offset;
	}
}

/**
 * Slot when receive a message, blink pixmap.
 */
void
QtGUIMainWindow::blinkMessageSlot (void) {
	static bool isOn = false;
	int stateMsg = BUSY;
	
	if(!isOn) {
		stateMsg = FREE;
	}
	
	if (msgVar) {
		phoneKey_msg->setPixmap(TabMsgPixmap[stateMsg]);
	}
	isOn = !isOn;
}


/**
 * Slot when phone is ringing, blink pixmap.
 */
void
QtGUIMainWindow::blinkRingSlot (void) {
	static bool isOn = false;
	int state = BUSY;
	int line = callmanager->sip->notUsedLine;
	if ( line == -1 ) 
		return;

	if (callmanager->ringing()) {
		// For the line
		if	(!isOn) {
			state = FREE;	
		}

		callmanager->phLines[line]->button()->setPixmap(
				TabLinePixmap[line][state]);
		isOn = !isOn;
	}
}


/**
 * Slot to quit application with or without QMessageBox confirmation
 */
void
QtGUIMainWindow::quitApplication (void) {
	bool confirm;
	// Show QMessageBox
llea's avatar
llea committed
978
	confirm = Config::get("Preferences", "Options.confirmQuit", (int)true);
979
		
savoirfairelinux's avatar
savoirfairelinux committed
980 981 982
	if (confirm) {
		if (QMessageBox::question(this, "Confirm quit",
			"Are you sure you want to quit SFLPhone ?",
llea's avatar
llea committed
983
			 QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes) {
savoirfairelinux's avatar
savoirfairelinux committed
984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046
				QApplication::exit(0);
				callmanager->quitLibrary();
		}
	} 
	// No QMessageBox
	else {
		QApplication::exit(0);
		callmanager->quitLibrary();
		qDebug("QUIT");
	}
}

/**
 * Slot to strip urlinput.
 */
void
QtGUIMainWindow::stripSlot (void) {
	QRegExp rx(REG_EXPR);
	lcd->appendText(urlinput->url->text().remove(rx));
	urlinput->close();
}

/**
 * Slot when numeric keypad phonekey is pressed, append the key text and setup 
 * the DTMF keypad .
 *
 * @param	button group id 
 */ 
void
QtGUIMainWindow::pressedKeySlot (int id) {
	char code = 0;
	int pulselen = 0;
	
	// Stop dial tone
 	if (b_dialtone) {
		this->dialTone(false);
	}

	switch (id) {
		case KEYPAD_ID_0:		code = '0'; break;
		case KEYPAD_ID_1:		code = '1'; break;
		case KEYPAD_ID_2:		code = '2'; break;
		case KEYPAD_ID_3:		code = '3'; break;
		case KEYPAD_ID_4:		code = '4'; break;
		case KEYPAD_ID_5:		code = '5'; break;
		case KEYPAD_ID_6:		code = '6'; break;
		case KEYPAD_ID_7:		code = '7'; break;
		case KEYPAD_ID_8:		code = '8'; break;
		case KEYPAD_ID_9:		code = '9'; break;
		case KEYPAD_ID_STAR:	code = '*'; break;
		case KEYPAD_ID_HASH:	code = '#'; break;
	}

	onLine = currentLineNumber;
	if (onLine != -1) {
		callmanager->dtmf(onLine, code);
	} 
	
	lcd->appendText (code);

	// Handle dtmf
	key->startTone(code);
	key->generateDTMF(buf, SAMPLING_RATE);
llea's avatar
llea committed
1047
	pulselen = Config::get("Signalisations", "DTMF.pulseLength", 250);
savoirfairelinux's avatar
savoirfairelinux committed
1048 1049 1050 1051 1052 1053 1054 1055 1056
	callmanager->audiodriver->writeBuffer(buf, pulselen * (OCTETS/1000));
}

// Apply new settings
void 
QtGUIMainWindow::applySlot() {
	
}

llea's avatar
llea committed
1057
// Save settings in config-file
1058 1059 1060 1061 1062
void 
QtGUIMainWindow::save() {
	Config::tree()->saveToFile(callmanager->path.data());
}

llea's avatar
llea committed
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072
// Handle operation to minimize the application
void 
QtGUIMainWindow::reduceHandle (void) {
	if (Config::getb("Preferences", "Options.checkedTray")) {
		clickHandle();
	} else {
		showMinimized();
	}
}

savoirfairelinux's avatar
savoirfairelinux committed
1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253
// Handle mouse left-button click to minimize/maximize the application
void
QtGUIMainWindow::clickHandle (void) {
	if (this->isShown()) {
		hide();
	}
	
	else if (this->isMinimized()) {
		showMaximized();
	}
	
	else {
		show();
	}
}

void
QtGUIMainWindow::pressedKey0 (void) {
	pressedKeySlot (KEYPAD_ID_0);
}

void
QtGUIMainWindow::pressedKey1 (void) {
	pressedKeySlot (KEYPAD_ID_1);
}

void
QtGUIMainWindow::pressedKey2 (void) {
	pressedKeySlot (KEYPAD_ID_2);
}
void
QtGUIMainWindow::pressedKey3 (void) {
	pressedKeySlot (KEYPAD_ID_3);
}
void
QtGUIMainWindow::pressedKey4 (void) {
	pressedKeySlot (KEYPAD_ID_4);
}
void
QtGUIMainWindow::pressedKey5 (void) {
	pressedKeySlot (KEYPAD_ID_5);
}
void
QtGUIMainWindow::pressedKey6 (void) {
	pressedKeySlot (KEYPAD_ID_6);
}
void
QtGUIMainWindow::pressedKey7 (void) {
	pressedKeySlot (KEYPAD_ID_7);
}
void
QtGUIMainWindow::pressedKey8 (void) {
	pressedKeySlot (KEYPAD_ID_8);
}
void
QtGUIMainWindow::pressedKey9 (void) {
	pressedKeySlot (KEYPAD_ID_9);
}
void
QtGUIMainWindow::pressedKeyStar  (void) {
	pressedKeySlot (KEYPAD_ID_STAR);
}
void
QtGUIMainWindow::pressedKeyHash (void) {
	pressedKeySlot (KEYPAD_ID_HASH);
}

///////////////////////////////////////////////////////////////////////////////
// Protected Methods implementations                                         //
///////////////////////////////////////////////////////////////////////////////
/**
 * Reimplementation of keyPressEvent() to handle the keyboard mapping.
 */
void
QtGUIMainWindow::keyPressEvent(QKeyEvent *e) {
	// Misc. key	  
	switch (e->key()) {
	case Qt::Key_At:
	case Qt::Key_Colon:
	case Qt::Key_Period:
	case Qt::Key_Comma:
	case Qt::Key_Plus:
	case Qt::Key_Minus:
	case Qt::Key_Slash:
		lcd->appendText(QChar(e->key()));
		return;
	   	break;

	case Qt::Key_Backspace:
		lcd->backspace();
		return;
	   	break;	

	case Qt::Key_Escape:
		lcd->clear();
		return;
	   	break;
	
	case Qt::Key_Return:
	case Qt::Key_Enter:
		dial();
		return;
		break;	

	case Qt::Key_F1:
	case Qt::Key_F2:
	case Qt::Key_F3:
	case Qt::Key_F4:
		this->toggleLine(e->key() - Qt::Key_F1);
		return;
	   	break;

	case Qt::Key_Q :
 		if (e->state() == Qt::ControlButton ) {
			emit keyPressed(e->key());
			return;			
		}			
		break;
	case Qt::Key_L :
 		if (e->state() == Qt::ControlButton ) {
		 	urlinput->show();
			return;			
		}			
		break;
	case Qt::Key_C :
 		if (e->state() == Qt::ControlButton ) {
		 	configuration();
			return;			
		}			
		break;
	case Qt::Key_Space:
		if (this->isInNumMode()) {
			this->setMode(TEXT_MODE);
		} else {
			this->setMode(NUM_MODE);
		}
		return;
		break;

	case Qt::Key_Alt:
	case Qt::Key_CapsLock:
	case Qt::Key_Shift:
	case Qt::Key_Tab:
	case Qt::Key_Control:
		return;
		break;

	default:
		break;
	 }

	if (QChar(e->key()).isDigit() ) {
		// Numeric keypad
		if (e->key() == Qt::Key_0) {
			pressedKeySlot(KEYPAD_ID_0);
		} else {
			pressedKeySlot(e->key() - Qt::Key_0 - 1);
		}
	}
	
	// Handle * and # too.
	else if ((e->key() == Qt::Key_Asterisk)
							or (e->key() == Qt::Key_NumberSign)) {
		(e->key() == Qt::Key_Asterisk) ? 
			pressedKeySlot(KEYPAD_ID_STAR)
			: pressedKeySlot(KEYPAD_ID_HASH);
	}
	
	// If letter keypad and numeric mode, display digit.
	else if (QChar(e->key()).isLetter() && this->isInNumMode() ) {
		pressedKeySlot(
			(NumericKeypadTools::keyToNumber(e->key())- Qt::Key_0) - 1);
	}
	
	// If letter keypad and text mode, display letter.
	else if (QChar(e->key()).isLetter() && this->isInTextMode()) {
		lcd->appendText(QChar(e->key()).lower());
	}  
}

// EOF