SFLPhone.cpp 14.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
/************************************** *************************************
 *   Copyright (C) 2009-2010 by Savoir-Faire Linux                         *
 *   Author : Jérémy Quentin <jeremy.quentin@savoirfairelinux.com>         *
 *            Emmanuel Lepage Vallee <emmanuel.lepage@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 3 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.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include <unistd.h>
#include "SFLPhone.h"

#include <unistd.h>
#include <KApplication>
#include <KStandardAction>
#include <KMenu>
#include <KAction>
#include <KToolBar>
#include <KStatusBar>
#include <QtGui/QStatusBar>
#include <QtGui/QCursor>
#include <KActionCollection>

35 36 37
#include "lib/sflphone_const.h"
#include "lib/instance_interface_singleton.h"
#include "lib/configurationmanager_interface_singleton.h"
38

39
SFLPhone* SFLPhone::m_sApp = NULL;
40 41 42 43 44 45 46

SFLPhone::SFLPhone(QWidget *parent)
    : KXmlGuiWindow(parent),
      initialized_(false),
      view(new SFLPhoneView(this))
{
    setupActions();
47
    m_sApp = this;
48 49
} 

50 51 52 53 54
SFLPhone* SFLPhone::app()
{
   return m_sApp;
}

55 56 57 58 59 60 61 62 63 64
SFLPhone::~SFLPhone()
{
}

bool SFLPhone::initialize()
{
  if ( initialized_ ) {
    qDebug() << "Already initialized.";
    return false;
  }
65 66
   
   
67 68 69 70 71
  ConfigurationManagerInterface & configurationManager = ConfigurationManagerInterfaceSingleton::getInstance();
  // accept dnd
  setAcceptDrops(true);

  // tell the KXmlGuiWindow that this is indeed the main widget
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
  //setCentralWidget(view);
  m_pCentralDW = new QDockWidget(this);
  m_pCentralDW->setWidget(view);
  m_pCentralDW->setWindowTitle("Call");
  m_pCentralDW->setFeatures(QDockWidget::NoDockWidgetFeatures);
  m_pCentralDW->setStyleSheet("\
  QDockWidget::title {\
     display:none;\
     margin:0px;\
     padding:0px;\
     spacing:0px;\
     max-height:0px;\
 }\
  \
  ");
  
  addDockWidget(Qt::BottomDockWidgetArea,m_pCentralDW);

   m_pContactCD = new ContactDock(this);
   addDockWidget(Qt::LeftDockWidgetArea,m_pContactCD);
   
   m_pHistoryDW  = new HistoryDock(this);
   addDockWidget(Qt::RightDockWidgetArea,m_pHistoryDW);
   m_pBookmarkDW = new BookmarkDock(this);
   addDockWidget(Qt::RightDockWidgetArea,m_pBookmarkDW);
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117

  setWindowIcon(QIcon(ICON_SFLPHONE));
  setWindowTitle(i18n("SFLphone"));

  setupActions();

  statusBarWidget = new QLabel();
  statusBar()->addWidget(statusBarWidget);


  trayIcon = new SFLPhoneTray(this->windowIcon(), this);
  trayIcon->show();

  iconChanged = false;

  setObjectNames();
  QMetaObject::connectSlotsByName(this);
  view->on_stackedWidget_screen_currentChanged(SCREEN_MAIN);
  view->loadWindow();

  move(QCursor::pos().x() - geometry().width()/2, QCursor::pos().y() - geometry().height()/2);
Emmanuel Lepage's avatar
Emmanuel Lepage committed
118 119 120
  //if( ! configurationManager.isStartHidden()) {
  show();
  //}
121 122 123 124 125 126 127 128 129 130 131 132

  if(configurationManager.getAccountList().value().isEmpty()) {
      (new AccountWizard())->show();
  }

  initialized_ = true;

  return true;
}

void SFLPhone::setObjectNames()
{
133 134 135
   view->setObjectName("view");
   statusBar()->setObjectName("statusBar");
   trayIcon->setObjectName("trayIcon");
136 137 138 139 140
}

void SFLPhone::setupActions()
{
   qDebug() << "setupActions";
141
   //ConfigurationManagerInterface & configurationManager = ConfigurationManagerInterfaceSingleton::getInstance();
142 143 144 145 146 147 148 149
   
   action_accept = new KAction(this);
   action_refuse = new KAction(this);
   action_hold = new KAction(this);
   action_transfer = new KAction(this);
   action_record = new KAction(this);
   action_mailBox = new KAction(this);

150 151 152 153 154 155
   action_accept->setShortcut(Qt::CTRL + Qt::Key_A);
   action_refuse->setShortcut(Qt::CTRL + Qt::Key_D);
   action_hold->setShortcut(Qt::CTRL + Qt::Key_H);
   action_transfer->setShortcut(Qt::CTRL + Qt::Key_T);
   action_record->setShortcut(Qt::CTRL + Qt::Key_R);
   action_mailBox->setShortcut(Qt::CTRL + Qt::Key_M);
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

   action_screen = new QActionGroup(this);
   action_screen->setExclusive(true);
   action_main = new KAction(KIcon(QIcon(ICON_SCREEN_MAIN)), i18n("Main screen"), action_screen);
   action_history = new KAction(KIcon(QIcon(ICON_SCREEN_HISTORY)), i18n("Call history"), action_screen);
   action_addressBook = new KAction(KIcon(QIcon(ICON_SCREEN_ADDRESS)), i18n("Address book"), action_screen);
   action_main->setCheckable(true);
   action_history->setCheckable(true);
   action_addressBook->setCheckable(true);
   action_main->setChecked(true);
   action_screen->addAction(action_main);
   action_screen->addAction(action_history);
   action_screen->addAction(action_addressBook);

   action_close = KStandardAction::close(this, SLOT(close()), this);
   action_quit = KStandardAction::quit(this, SLOT(quitButton()), this);
   
   action_configureSflPhone = KStandardAction::preferences(view, SLOT(configureSflPhone()), this);
   action_configureSflPhone->setText(i18n("Configure SFLphone"));
   
   action_displayVolumeControls = new KAction(KIcon(QIcon(ICON_DISPLAY_VOLUME_CONSTROLS)), i18n("Display volume controls"), this);   
   action_displayDialpad = new KAction(KIcon(QIcon(ICON_DISPLAY_DIALPAD)), i18n("Display dialpad"), this);
   action_displayDialpad->setCheckable(true);

   action_displayVolumeControls->setCheckable(true);
Emmanuel Lepage's avatar
Emmanuel Lepage committed
181 182
   action_displayVolumeControls->setChecked(/*configurationManager.getVolumeControls()*/true);
   action_displayDialpad->setChecked(/*configurationManager.getDialpad()*/true);
183 184 185 186 187 188 189 190 191
   action_accountCreationWizard = new KAction(i18n("Account creation wizard"), this);
   
   connect(action_accept,                SIGNAL(triggered()),           view, SLOT(accept()));
   connect(action_refuse,                SIGNAL(triggered()),           view, SLOT(refuse()));
   connect(action_hold,                  SIGNAL(triggered()),           view, SLOT(hold()));
   connect(action_transfer,              SIGNAL(triggered()),           view, SLOT(transfer()));
   connect(action_record,                SIGNAL(triggered()),           view, SLOT(record()));
   connect(action_screen,                SIGNAL(triggered(QAction *)),  this, SLOT(updateScreen(QAction *)));
   connect(action_mailBox,               SIGNAL(triggered()),           view, SLOT(mailBox()));
192 193
   connect(action_displayVolumeControls, SIGNAL(toggled(bool)),           view, SLOT(displayVolumeControls(bool)));
   connect(action_displayDialpad,        SIGNAL(toggled(bool)),           view, SLOT(displayDialpad(bool)));
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
   connect(action_accountCreationWizard, SIGNAL(triggered()),           view, SLOT(accountCreationWizard()));  

   action_screen->addAction(action_main);
   action_screen->addAction(action_history);
   action_screen->addAction(action_addressBook);
   
   actionCollection()->addAction("action_accept", action_accept);
   actionCollection()->addAction("action_refuse", action_refuse);
   actionCollection()->addAction("action_hold", action_hold);
   actionCollection()->addAction("action_transfer", action_transfer);
   actionCollection()->addAction("action_record", action_record);
   actionCollection()->addAction("action_main", action_main);
   actionCollection()->addAction("action_history", action_history);
   actionCollection()->addAction("action_addressBook", action_addressBook);
   actionCollection()->addAction("action_mailBox", action_mailBox);
   actionCollection()->addAction("action_close", action_close);
   actionCollection()->addAction("action_quit", action_quit);
   
   actionCollection()->addAction("action_displayVolumeControls", action_displayVolumeControls);
   actionCollection()->addAction("action_displayDialpad", action_displayDialpad);
   actionCollection()->addAction("action_configureSflPhone", action_configureSflPhone);
   actionCollection()->addAction("action_accountCreationWizard", action_accountCreationWizard);
   
   QString rcFilePath = QString(DATA_INSTALL_DIR) + "/sflphone-client-kde/sflphone-client-kdeui.rc";

   if(! QFile::exists(rcFilePath)) {
      QDir dir;
      dir.cdUp();
      dir.cd("data");
      rcFilePath = dir.filePath("sflphone-client-kdeui.rc");
      qDebug() << "rcFilePath = " << rcFilePath ;

      if(! QFile::exists(rcFilePath)) {
         QDir dir;
         dir.cdUp();
         dir.cdUp();
         dir.cd("data");
         rcFilePath = dir.filePath("sflphone-client-kdeui.rc");
      }
   }
   qDebug() << "rcFilePath = " << rcFilePath ;
   createGUI(rcFilePath);

}

SFLPhoneView * SFLPhone::getView()
{
   return view;
}

bool SFLPhone::queryClose()
{
   qDebug() << "queryClose";
   hide();
   return false;
}

void SFLPhone::quitButton()
{
   InstanceInterface & instance = InstanceInterfaceSingleton::getInstance();
   //qDebug() << "quitButton : " << view->callTree->count() << " calls open.";

   //if(view->callTree->count() > 0 && instance.getRegistrationCount() <= 1) {
      //qDebug() << "Attempting to quit when still having some calls open.";
   //}
   view->saveState();
   instance.Unregister(getpid());
   qApp->quit();
}

void SFLPhone::sendNotif(QString caller)
{
//     notification = new KNotification ( QString("test_notification"), this );
//     notification->setText("messageText")    ;
//     notification->setPixmap( QPixmap( this->windowIcon().pixmap(32, 32) ));
//     notification->setActions( QStringList( i18n( "Open chat" ) ) );
//     notification->addContext(  QString::fromLatin1("call") , "caller" )  ;
//     notification->sendEvent();
//     KNotification::event(QString("test_notification"),
//                          QString("Allo"),
//                          this->windowIcon().pixmap(32, 32),
//                          parentWidget(),
//                          KNotification::CloseOnTimeout,
//                          KGlobal::mainComponent());

279
   KNotification::event(KNotification::Notification, "New incomming call", "New call from: \n" + caller);       
280 281
}

282
void SFLPhone::changeEvent(QEvent* event)
283
{
284 285 286
   if (event->type() == QEvent::ActivationChange && iconChanged && isActiveWindow()) {
     iconChanged = false;
   }
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 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
}

void SFLPhone::on_view_statusMessageChangeAsked(const QString & message)
{
   qDebug() << "on_view_statusMessageChangeAsked : " + message;
   statusBarWidget->setText(message);
}

void SFLPhone::on_view_windowTitleChangeAsked(const QString & message)
{
   qDebug() << "on_view_windowTitleChangeAsked : " + message;
   setWindowTitle(message);
}

void SFLPhone::on_view_enabledActionsChangeAsked(const bool * enabledActions)
{
   qDebug() << "on_view_enabledActionsChangeAsked";
   action_accept->setVisible(enabledActions[SFLPhone::Accept]);
   action_refuse->setVisible(enabledActions[SFLPhone::Refuse]);
   action_hold->setVisible(enabledActions[SFLPhone::Hold]);
   action_transfer->setVisible(enabledActions[SFLPhone::Transfer]);
   action_record->setVisible(enabledActions[SFLPhone::Record]);
   action_mailBox->setVisible(enabledActions[SFLPhone::Mailbox]);
}

void SFLPhone::on_view_actionIconsChangeAsked(const QString * actionIcons)
{
   qDebug() << "on_view_actionIconsChangeAsked";
   action_accept->setIcon(QIcon(actionIcons[SFLPhone::Accept]));
   action_refuse->setIcon(QIcon(actionIcons[SFLPhone::Refuse]));
   action_hold->setIcon(QIcon(actionIcons[SFLPhone::Hold]));
   action_transfer->setIcon(QIcon(actionIcons[SFLPhone::Transfer]));
   action_record->setIcon(QIcon(actionIcons[SFLPhone::Record]));
   action_mailBox->setIcon(QIcon(actionIcons[SFLPhone::Mailbox]));
}

void SFLPhone::on_view_actionTextsChangeAsked(const QString * actionTexts)
{
   qDebug() << "on_view_actionTextsChangeAsked";
   action_accept->setText(actionTexts[SFLPhone::Accept]);
   action_refuse->setText(actionTexts[SFLPhone::Refuse]);
   action_hold->setText(actionTexts[SFLPhone::Hold]);
   action_transfer->setText(actionTexts[SFLPhone::Transfer]);
   action_record->setText(actionTexts[SFLPhone::Record]);
   action_mailBox->setText(actionTexts[SFLPhone::Mailbox]);
}


void SFLPhone::on_view_transferCheckStateChangeAsked(bool transferCheckState)
{
   qDebug() << "Changing transfer action checkState";
   action_transfer->setChecked(transferCheckState);
}

void SFLPhone::on_view_recordCheckStateChangeAsked(bool recordCheckState)
{
   qDebug() << "Changing record action checkState";
   action_record->setChecked(recordCheckState);
}

void SFLPhone::updateScreen(QAction * action)
{
   if(action == action_main)   view->changeScreen(SCREEN_MAIN);
   else if(action == action_history)   view->changeScreen(SCREEN_HISTORY);
   else if(action == action_addressBook)   view->changeScreen(SCREEN_ADDRESS);
}

void SFLPhone::on_view_screenChanged(int screen)
{
   qDebug() << "on_view_screenChanged";
   if(screen == SCREEN_MAIN)   action_main->setChecked(true);
   else if(screen == SCREEN_HISTORY)   action_history->setChecked(true);
   else if(screen == SCREEN_ADDRESS)   action_addressBook->setChecked(true);
}

362
QList<QAction*> SFLPhone::getCallActions()
363
{
364
   QList<QAction*> callActions = QList<QAction *>();
365 366 367 368 369 370 371 372 373 374 375
   callActions.insert((int) Accept, action_accept);
   callActions.insert((int) Refuse, action_refuse);
   callActions.insert((int) Hold, action_hold);
   callActions.insert((int)Transfer, action_transfer);
   callActions.insert((int) Record, action_record);
   callActions.insert((int) Mailbox, action_mailBox);
   return callActions;
}

void SFLPhone::on_view_incomingCall(const Call * call)
{
376
   //ConfigurationManagerInterface & configurationManager = ConfigurationManagerInterfaceSingleton::getInstance();
377 378 379 380 381
    //trayIconSignal();
    /*if(configurationManager.popupMode())
   {
      putForeground();
    }*/
Emmanuel Lepage's avatar
Emmanuel Lepage committed
382 383 384
   //if(configurationManager.getNotify()) {
   sendNotif(call->getPeerName().isEmpty() ? call->getPeerPhoneNumber() : call->getPeerName());
   //}
385 386 387 388 389 390
}

void SFLPhone::on_view_addressBookEnableAsked(bool enabled)
{
   action_addressBook->setVisible(enabled);
}