source: qutecom-2.2/wengophone/src/presentation/qt/QtWengoPhone.cpp @ 499:1bf0eda7d082

Last change on this file since 499:1bf0eda7d082 was 499:1bf0eda7d082, checked in by laurent@…, 3 years ago

use webkit with chat

File size: 27.1 KB
Line 
1/*
2 * WengoPhone, a voice over Internet phone
3 * Copyright (C) 2004-2007  Wengo
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 */
19
20#include "QtWengoPhone.h"
21
22#include "ui_WengoPhoneWindow.h"
23
24#include <presentation/PFactory.h>
25#include <presentation/qt/chat/QtChatWindow.h>
26
27#include <cutil/global.h>
28
29#include "QtSystray.h"
30#include "QtHttpProxyLogin.h"
31#include "QtDialpad.h"
32#include "QtIdle.h"
33#include "QtLanguage.h"
34#include "QtToolBar.h"
35#include "QtBrowserWidget.h"
36#include "QtIMAccountMonitor.h"
37#include "callbar/QtCallBar.h"
38#include "callbar/QtPhoneComboBox.h"
39#include "contactlist/QtContactList.h"
40#include "filetransfer/QtFileTransfer.h"
41#include "history/QtHistoryWidget.h"
42#include "imaccount/QtIMAccountManager.h"
43#include "phonecall/QtContactCallListWidget.h"
44#include "phonecall/QtPhoneCall.h"
45#include "profile/QtProfileDetails.h"
46#include "profilebar/QtProfileBar.h"
47#include "statusbar/QtStatusBar.h"
48#include "webservices/sms/QtSms.h"
49
50#include "webdirectory/QtWebDirectory.h"
51
52#include <control/CWengoPhone.h>
53#include <control/history/CHistory.h>
54#include <control/phoneline/CPhoneLine.h>
55#include <control/profile/CUserProfile.h>
56#include <control/profile/CUserProfileHandler.h>
57
58#include <model/WengoPhone.h>
59#include <model/commandserver/ContactInfo.h>
60#include <model/config/ConfigManager.h>
61#include <model/config/Config.h>
62#include <model/connect/ConnectHandler.h>
63#include <model/contactlist/ContactList.h>
64#include <model/contactlist/Contact.h>
65#include <model/history/History.h>
66#include <model/phonecall/PhoneCall.h>
67#include <model/phonecall/SipAddress.h>
68#include <model/phonecall/ConferenceCall.h>
69#include <model/phoneline/IPhoneLine.h>
70#include <model/profile/AvatarList.h>
71#include <model/profile/UserProfile.h>
72
73#include <imwrapper/EnumPresenceState.h>
74
75#include <qtutil/CloseEventFilter.h>
76#include <qtutil/Widget.h>
77#include <qtutil/SafeConnect.h>
78#include <qtutil/LanguageChangeEventFilter.h>
79
80#include <cutil/global.h>
81#include <thread/Thread.h>
82#include <util/Logger.h>
83#include <util/SafeDelete.h>
84#include <webcam/WebcamDriver.h>
85
86#include <QtGui/QtGui>
87
88#if defined(OS_MACOSX)
89        #include <Carbon/Carbon.h>
90#endif
91
92static const char * CSS_DIR = "css";
93
94using namespace std;
95
96QtWengoPhone::QtWengoPhone(CWengoPhone & cWengoPhone)
97        : QObjectThreadSafe(NULL),
98        _cWengoPhone(cWengoPhone) {
99
100        _wengoPhoneWindow = NULL;
101        _qtSystray = NULL;
102        _qtSms = NULL;
103        _qtContactList = NULL;
104        _qtHistoryWidget = NULL;
105        _qtContactCallListWidget = NULL;
106        _qtIdle = NULL;
107        _qtLanguage = NULL;
108        _qtStatusBar = NULL;
109        _qtFileTransfer = NULL;
110        _qtBrowserWidget = NULL;
111        _chatWindow = NULL;
112        _activeTabBeforeCall = NULL;
113        _qtWebDirectory = NULL;
114
115        NetworkProxyDiscovery::getInstance().proxyNeedsAuthenticationEvent +=
116                boost::bind(&QtWengoPhone::proxyNeedsAuthenticationEventHandler, this, _1, _2);
117        NetworkProxyDiscovery::getInstance().wrongProxyAuthenticationEvent +=
118                boost::bind(&QtWengoPhone::wrongProxyAuthenticationEventHandler, this, _1, _2);
119
120        //Check if the event has not already been sent
121        if (NetworkProxyDiscovery::getInstance().getState() ==
122                NetworkProxyDiscovery::NetworkProxyDiscoveryStateNeedsAuthentication) {
123                proxyNeedsAuthenticationEventHandler(NetworkProxyDiscovery::getInstance(),
124                        NetworkProxyDiscovery::getInstance().getNetworkProxy());
125        }
126
127        qRegisterMetaType<QVariant>("QVariant");
128
129        //Initialize libwebcam for the Qt thread.
130        WebcamDriver::apiInitialize();
131        ////
132
133        initUi();
134
135        typedef PostEvent0<void ()> MyPostEvent;
136        MyPostEvent * event = new MyPostEvent(boost::bind(&QtWengoPhone::initThreadSafe, this));
137        postEvent(event);
138}
139
140QtWengoPhone::~QtWengoPhone() {
141        //TODO delete created objects
142}
143
144void QtWengoPhone::initUi() {
145        // Init parts of the UI which do not rely on Control layer
146        QApplication::setQuitOnLastWindowClosed(false);
147
148        loadStyleSheets();
149
150        //Translation
151        _qtLanguage = new QtLanguage(this);
152
153        _wengoPhoneWindow = new QMainWindow(NULL);
154
155        _ui = new Ui::WengoPhoneWindow();
156        _ui->setupUi(_wengoPhoneWindow);
157        _ui->tabWidget->setDocumentMode(true);
158
159#ifndef CUSTOM_ACCOUNT
160        _ui->actionShowWengoAccount->setVisible(false);
161#endif
162
163#ifdef DISABLE_FILETRANSFER
164        _ui->actionShowFileTransfer->setVisible(false);
165#endif
166
167#ifdef OS_MACOSX
168        _ui->actionShowAbout->setMenuRole(QAction::AboutRole);
169        _ui->actionShowConfig->setMenuRole(QAction::PreferencesRole);
170        _ui->actionQuit->setMenuRole(QAction::QuitRole);
171        //fixMacOSXMenus();
172#endif
173       
174       LANGUAGE_CHANGE(_wengoPhoneWindow);
175
176#ifdef OS_LINUX
177        const std::string &applicationIconPath = ConfigManager::getInstance().getCurrentConfig().getApplicationIconPath();
178        _wengoPhoneWindow->setWindowIcon(QIcon(QString::fromStdString(applicationIconPath)));
179#endif
180
181        //Install the close event filter
182        CloseEventFilter * closeEventFilter = new CloseEventFilter(this, SLOT(closeWindow()));
183        _wengoPhoneWindow->installEventFilter(closeEventFilter);
184
185        //QtCallBar
186        _ui->callBar->getQtPhoneComboBox()->setQtWengoPhone(this);
187
188        //QtToolBar
189        _qtToolBar = new QtToolBar(*this, _ui, _wengoPhoneWindow);
190
191        //phoneComboBox
192        SAFE_CONNECT(_ui->callBar, SIGNAL(phoneComboBoxClicked()), SLOT(phoneComboBoxClicked()));
193
194        //Buttons initialization
195        initCallButtons();
196
197        //QtStatusBar
198        _qtStatusBar = new QtStatusBar(this, _ui->statusBar);
199
200#if defined(CUSTOM_ACCOUNT)
201        _qtWebDirectory = new QtWebDirectory(0);
202#endif
203
204        installQtBrowserWidget();
205       
206#if defined(OS_MACOSX)
207        _wengoPhoneWindow->setUnifiedTitleAndToolBarOnMac(true);
208#endif
209       
210        Config & config = ConfigManager::getInstance().getCurrentConfig();
211        mainWindowGeometry(config);
212}
213
214void QtWengoPhone::initThreadSafe() {
215        // The code in this method relies on the Control layer
216        _qtSystray = new QtSystray(this);
217
218        updatePresentation();
219}
220
221void QtWengoPhone::loadStyleSheets() {
222        Config & config = ConfigManager::getInstance().getCurrentConfig();
223        QDir dir(QString::fromStdString(config.getResourcesDir()) + CSS_DIR);
224
225        QStringList filters;
226        filters << "*.css";
227        QStringList cssList;
228        Q_FOREACH(QFileInfo fileInfo, dir.entryInfoList(filters)) {
229                QString path = fileInfo.absoluteFilePath();
230                QFile file(path);
231                if (!file.open(QIODevice::ReadOnly)) {
232                        LOG_WARN("Can't open " + path.toStdString());
233                        continue;
234                }
235                QByteArray content = file.readAll();
236                cssList << QString::fromUtf8(content);
237        }
238        QString styleSheet = cssList.join("\n");
239        qApp->setStyleSheet(styleSheet);
240}
241
242void QtWengoPhone::mainWindowGeometry(Config & config) {
243        //default position and size given by Qt
244        QPoint defaultPos = _wengoPhoneWindow->pos();
245        QSize defaultSize = _wengoPhoneWindow->size();
246
247        int profileWidth = config.getProfileWidth();
248        if (profileWidth == 0) {
249                // Make sure the toolbar is fully visible
250                profileWidth = qMax(
251                        _ui->toolBar->sizeHint().width(),
252                        _wengoPhoneWindow->sizeHint().width()
253                        );
254        }
255
256        int profileHeight = config.getProfileHeight();
257
258        //Main window size and position saved
259        _wengoPhoneWindow->resize(profileWidth, profileHeight);
260        _wengoPhoneWindow->move(QPoint(config.getProfilePosX(), config.getProfilePoxY()));
261
262        //tests if the Wengophone is visible, if not sends it back to its default position and size
263        QDesktopWidget* desktop = QApplication::desktop();
264        if (desktop->screenNumber(_wengoPhoneWindow) == -1) {
265                LOG_DEBUG("Main window is NOT visible !!");
266                _wengoPhoneWindow->resize(defaultSize);
267                _wengoPhoneWindow->move(defaultPos);
268        }
269}
270
271QWidget * QtWengoPhone::getWidget() const {
272        return _wengoPhoneWindow;
273}
274
275QtProfileBar * QtWengoPhone::getQtProfileBar() const {
276        return _ui->profileBar;
277}
278
279QtBrowserWidget * QtWengoPhone::getQtBrowserWidget() const {
280        return _qtBrowserWidget;
281}
282
283CWengoPhone & QtWengoPhone::getCWengoPhone() const {
284        return _cWengoPhone;
285}
286
287void QtWengoPhone::setQtSms(QtSms * qtSms) {
288        _qtSms = qtSms;
289}
290
291QtSms * QtWengoPhone::getQtSms() const {
292        return _qtSms;
293}
294
295QtWebDirectory * QtWengoPhone::getQtWebDirectory() const {
296        return _qtWebDirectory;
297}
298
299QtToolBar & QtWengoPhone::getQtToolBar() const {
300        return *_qtToolBar;
301}
302
303QtStatusBar & QtWengoPhone::getQtStatusBar() const {
304        return *_qtStatusBar;
305}
306
307QtSystray & QtWengoPhone::getQtSystray() const {
308        return *_qtSystray;
309}
310
311QtCallBar & QtWengoPhone::getQtCallBar() const {
312        return *_ui->callBar;
313}
314
315void QtWengoPhone::setChatWindow(QWidget * chatWindow) {
316        if (!chatWindow) {
317                _chatWindow = NULL;
318                _ui->actionOpenChatWindow->setEnabled(false);
319        }
320        else {
321                _chatWindow = chatWindow;
322                _ui->actionOpenChatWindow->setEnabled(true);
323        }
324}
325
326QWidget * QtWengoPhone::getChatWindow() const {
327        return _chatWindow;
328}
329
330void QtWengoPhone::closeChatWindow() {
331        if (_chatWindow)
332        {
333            ((QtChatWindow *)_chatWindow)->closeAllTabs ();
334        }
335}
336
337void QtWengoPhone::setQtContactList(QtContactList * qtContactList) {
338        _qtContactList = qtContactList;
339
340        if (!_ui->tabContactList->layout()) {
341                Widget::createLayout(_ui->tabContactList);
342        }
343
344        _ui->tabContactList->layout()->addWidget(_qtContactList->getWidget());
345
346        LOG_DEBUG("QtContactList added");
347}
348
349QtContactList * QtWengoPhone::getQtContactList() const {
350        return _qtContactList;
351}
352
353void QtWengoPhone::setQtHistoryWidget(QtHistoryWidget * qtHistoryWidget) {
354        _qtHistoryWidget = qtHistoryWidget;
355        _qtHistoryWidget->setQtToolBar(_qtToolBar);
356        _qtHistoryWidget->setQtCallBar(_ui->callBar);
357
358        if (!_ui->tabHistory->layout()) {
359                Widget::createLayout(_ui->tabHistory);
360        }
361
362        _qtHistoryWidget->getWidget()->setParent(_ui->tabHistory);
363        _ui->tabHistory->layout()->addWidget(_qtHistoryWidget->getWidget());
364
365        LOG_DEBUG("QtHistoryWidget added");
366}
367
368void QtWengoPhone::setQtDialpad(QtDialpad * qtDialpad) {
369        Widget::createLayout(_ui->tabDialpad)->addWidget(qtDialpad);
370        LOG_DEBUG("QtDialpad added");
371}
372
373void QtWengoPhone::initCallButtons() {
374        //callButton
375        SAFE_CONNECT(_ui->callBar, SIGNAL(callButtonClicked()), SLOT(callButtonClicked()));
376        enableCallButton();
377
378        //hangUpButton
379        SAFE_CONNECT(_ui->callBar, SIGNAL(hangUpButtonClicked()), SLOT(hangUpButtonClicked()));
380        _ui->callBar->setEnabledHangUpButton(false);
381
382        //phoneComboBox
383        SAFE_CONNECT(_ui->callBar, SIGNAL(phoneComboBoxReturnPressed()), SLOT(callButtonClicked()));
384        SAFE_CONNECT(_ui->callBar, SIGNAL(phoneComboBoxEditTextChanged(const QString &)), SLOT(enableCallButton()));
385}
386
387void QtWengoPhone::enableCallButton() {
388        std::string phoneNumber = _ui->callBar->getPhoneComboBoxCurrentText();
389        _ui->callBar->setEnabledCallButton(!phoneNumber.empty());
390}
391
392void QtWengoPhone::hangUpButtonClicked() {
393        QtContactCallListWidget * widget =
394                dynamic_cast<QtContactCallListWidget *>(_ui->tabWidget->currentWidget());
395        if (widget) {
396                widget->hangup();
397        }
398}
399
400void QtWengoPhone::callButtonClicked() {
401        if (_cWengoPhone.getCUserProfileHandler().getCUserProfile()) {
402                std::string phoneNumber = _ui->callBar->getPhoneComboBoxCurrentText();
403                if (!phoneNumber.empty()) {
404                        CUserProfile * cUserProfile = _cWengoPhone.getCUserProfileHandler().getCUserProfile();
405                        cUserProfile->makeCallErrorEvent += boost::bind(&QtWengoPhone::makeCallErrorEventHandler, this, _2);
406                        cUserProfile->makeCall(phoneNumber);
407                }
408                _ui->callBar->clearPhoneComboBoxEditText();
409        }
410}
411
412void QtWengoPhone::setActiveTabBeforeCallCurrent() {
413        _ui->tabWidget->setCurrentWidget(_activeTabBeforeCall);
414}
415
416void QtWengoPhone::makeCallErrorEventHandler(EnumMakeCallError::MakeCallError error) {
417        typedef PostEvent1<void (EnumMakeCallError::MakeCallError), EnumMakeCallError::MakeCallError> MyPostEvent;
418        MyPostEvent * event = new MyPostEvent(boost::bind(&QtWengoPhone::makeCallErrorEventHandlerThreadSafe, this, _1), error);
419        postEvent(event);
420}
421
422void QtWengoPhone::makeCallErrorEventHandlerThreadSafe(EnumMakeCallError::MakeCallError error) {
423        QString message;
424        switch (error) {
425        case EnumMakeCallError::CallNotHeld:
426                message = tr("Please hold all the phone calls before placing a new one");
427                break;
428        case EnumMakeCallError::NotConnected:
429                message = tr("Can't place a call, you are not connected");
430                break;
431        case EnumMakeCallError::EmptyPhoneNumber:
432                message = tr("You must enter a phone number");
433                break;
434        case EnumMakeCallError::SipError:
435                message = tr("An error has occured trying to place the call");
436                break;
437        case EnumMakeCallError::NoError:
438                LOG_FATAL("Should not happen");
439                break;
440        }
441        QMessageBox::warning(_wengoPhoneWindow, tr("@product@ - Call Error"),
442                message, QMessageBox::Ok);
443}
444
445void QtWengoPhone::addPhoneCall(QtPhoneCall * qtPhoneCall) {
446        _activeTabBeforeCall = _ui->tabWidget->currentWidget();
447
448        QtContactCallListWidget * qtContactCallListWidget = new QtContactCallListWidget(_cWengoPhone, NULL);
449        SAFE_CONNECT(qtContactCallListWidget, SIGNAL(aboutToClose()), SLOT(setActiveTabBeforeCallCurrent()) );
450        _ui->tabWidget->addTab(qtContactCallListWidget, tr("Call"));
451        _ui->tabWidget->setCurrentWidget(qtContactCallListWidget);
452        qtContactCallListWidget->addPhoneCall(qtPhoneCall);
453
454        _ui->callBar->setEnabledHangUpButton(true);
455
456        if (qtPhoneCall->isIncoming()) {
457                _ui->callBar->setEnabledCallButton(true);
458        }
459}
460
461void QtWengoPhone::addToConference(QString phoneNumber, PhoneCall * targetCall) {
462        //FIXME conference has to be 100% rewritten...
463        /*bool conferenceAlreadyStarted = false;
464
465        if (_cWengoPhone.getCUserProfileHandler().getCUserProfile()) {
466                int nbtab = _ui->tabWidget->count();
467
468                for (int i = 0; i < nbtab; i++) {
469                        if (_ui->tabWidget->tabText(i) == QString(tr("Conference"))) {
470                                return;
471                        }
472
473                        for (int j = 0; j < _ui->tabWidget->count(); j++) {
474                                QtContactCallListWidget * qtContactCallListWidget = dynamic_cast<QtContactCallListWidget *>(_ui->tabWidget->widget(j));
475                                if (qtContactCallListWidget) {
476                                        if (qtContactCallListWidget->hasPhoneCall(targetCall)) {
477                                                _ui->tabWidget->setTabText(j, tr("Conference"));
478                                                IPhoneLine * phoneLine = _cWengoPhone.getCUserProfileHandler().getCUserProfile()->getUserProfile().getActivePhoneLine();
479
480                                                if (phoneLine != NULL) {
481                                                        if (!conferenceAlreadyStarted) {
482                                                                conferenceAlreadyStarted = true;
483                                                                ConferenceCall * confCall = new ConferenceCall(*phoneLine);
484                                                                confCall->addPhoneCall(*targetCall);
485                                                                confCall->addPhoneNumber(phoneNumber.toStdString());
486                                                        }
487                                                } else {
488                                                        LOG_DEBUG("phoneLine is NULL");
489                                                }
490                                        }
491                                }
492                        }
493                }
494        }*/
495
496        if (!_cWengoPhone.getCUserProfileHandler().getCUserProfile())
497                return;
498
499        IPhoneLine * phoneLine = _cWengoPhone.getCUserProfileHandler().getCUserProfile()->getUserProfile().getActivePhoneLine();
500        if(!phoneLine)
501                return;
502
503        for (int i = 0; i < _ui->tabWidget->count(); i++) 
504        {
505                if (_ui->tabWidget->tabText(i) == QString(tr("Conference"))) 
506                        return;
507        }
508
509        ConferenceCall * confCall = new ConferenceCall(*phoneLine);
510        targetCall->setConferenceCall(confCall);
511
512        PhoneCall * phoneCall2 = confCall->getPhoneCall(phoneNumber.toStdString());
513
514        QtContactCallListWidget * qtContactCallListWidgetTarget = 0;
515        QtPhoneCall * qtPhoneTarget = 0;
516        QtPhoneCall * qtPhoneCall2 = 0;
517        int index2 = -1;
518
519        for (int j = 0; j < _ui->tabWidget->count(); j++) 
520        {
521                QtContactCallListWidget * qtContactCallListWidget = dynamic_cast<QtContactCallListWidget *>(_ui->tabWidget->widget(j));
522                if (qtContactCallListWidget) 
523                {
524                        if (!qtContactCallListWidgetTarget && qtContactCallListWidget->hasPhoneCall(targetCall)) 
525                        {
526                                _ui->tabWidget->setTabText(j, tr("Conference"));
527
528                                qtContactCallListWidgetTarget = qtContactCallListWidget;
529                                qtPhoneTarget = qtContactCallListWidgetTarget->takeQtPhoneCall(targetCall);
530                        }
531                        else if(!qtPhoneCall2 && qtContactCallListWidget->hasPhoneCall(phoneCall2))
532                        {
533                                qtPhoneCall2 = qtContactCallListWidget->takeQtPhoneCall(phoneCall2);
534                                index2 = j;
535                               
536                        }
537                }
538        }
539
540        if( qtContactCallListWidgetTarget)
541        {
542                if(qtPhoneCall2)
543                {
544                        qtPhoneCall2->hideConfButton();
545                        qtContactCallListWidgetTarget->addPhoneCall(qtPhoneCall2);
546                        _ui->tabWidget->removeTab(index2);
547                }
548                if(qtPhoneTarget)
549                {
550                        qtPhoneTarget->hideConfButton();
551                        qtContactCallListWidgetTarget->addPhoneCall(qtPhoneTarget);
552                }
553        }
554
555        QString address = QString::fromStdString(targetCall->getPeerSipAddress().getSipAddress());
556       
557        if(address.contains("@"))
558                address = address.section("@",0,0);
559
560        if (address.startsWith("sip:")) {
561                address = address.mid(4);
562        }
563
564        confCall->addPhoneNumber(address.toStdString());
565        confCall->addPhoneNumber(phoneNumber.toStdString());
566       
567        if(phoneCall2)
568                phoneCall2->setConferenceCall(confCall);
569
570        if(qtContactCallListWidgetTarget)
571                qtContactCallListWidgetTarget->slotStartedTalking(qtPhoneTarget);
572}
573
574void QtWengoPhone::addToConference(QtPhoneCall * qtPhoneCall) {
575
576        QtContactCallListWidget * qtContactCallListWidget;
577
578        int nbtab = _ui->tabWidget->count();
579
580        for (int i = 0; i < nbtab; i++) {
581                if (_ui->tabWidget->tabText(i) == QString(tr("Conference"))) {
582                        //i is the index of the conference tab
583                        qtContactCallListWidget = (QtContactCallListWidget *) _ui->tabWidget->widget(i);
584                        qtContactCallListWidget->addPhoneCall(qtPhoneCall);
585                        _ui->tabWidget->setCurrentWidget(qtContactCallListWidget);
586                        return;
587                }
588        }
589
590        _activeTabBeforeCall = _ui->tabWidget->currentWidget();
591
592        //conference tab not found, create a new one
593        qtContactCallListWidget = new QtContactCallListWidget(_cWengoPhone,_wengoPhoneWindow);
594        SAFE_CONNECT(qtContactCallListWidget, SIGNAL(aboutToClose()), SLOT(setActiveTabBeforeCallCurrent()) );
595        _ui->tabWidget->addTab(qtContactCallListWidget, tr("Conference"));
596        _ui->tabWidget->setCurrentWidget(qtContactCallListWidget);
597        qtContactCallListWidget->addPhoneCall(qtPhoneCall);
598        _ui->callBar->setEnabledHangUpButton(true);
599}
600
601QtFileTransfer * QtWengoPhone::getFileTransfer() const {
602        return _qtFileTransfer;
603}
604
605void QtWengoPhone::updatePresentation() {
606        typedef PostEvent0<void ()> MyPostEvent;
607        MyPostEvent * event = new MyPostEvent(boost::bind(&QtWengoPhone::updatePresentationThreadSafe, this));
608        postEvent(event);
609}
610
611void QtWengoPhone::updatePresentationThreadSafe() {
612        if (_cWengoPhone.getCUserProfileHandler().getCUserProfile()) {
613                //disabled some actions if no SIP Account is used
614                bool hasSipAccount = _cWengoPhone.getCUserProfileHandler().getCUserProfile()->getUserProfile().hasSipAccount();
615
616                _ui->actionShowWengoAccount->setEnabled(_cWengoPhone.getCUserProfileHandler().getCUserProfile()->getUserProfile().hasWengoAccount());
617                _ui->actionSendSms->setEnabled(hasSipAccount);
618                _ui->actionCreateConferenceCall->setEnabled(hasSipAccount);
619                _ui->actionSearchWengoContact->setEnabled(hasSipAccount);
620        }
621}
622
623void QtWengoPhone::dialpad(const std::string & tone) {
624        if (_cWengoPhone.getCUserProfileHandler().getCUserProfile()) {
625                _ui->callBar->setPhoneComboBoxEditText(_ui->callBar->getPhoneComboBoxCurrentText() + tone);
626        }
627}
628
629void QtWengoPhone::dialpadBackspace() {
630  int s;
631        if (_cWengoPhone.getCUserProfileHandler().getCUserProfile()) {
632          s = _ui->callBar->getPhoneComboBoxCurrentText().size();
633          if(s>0)
634          {
635            std::string str = _ui->callBar->getPhoneComboBoxCurrentText();
636            str.resize(s-1);
637                  _ui->callBar->setPhoneComboBoxEditText(str);
638                }
639        }
640}
641
642void QtWengoPhone::dialpadEnter() {
643  callButtonClicked();
644}
645
646void QtWengoPhone::prepareToExitApplication() {
647        Config & config = ConfigManager::getInstance().getCurrentConfig();
648
649        // check for pending calls
650        CUserProfile *cUserProfile = _cWengoPhone.getCUserProfileHandler().getCUserProfile();
651        if (cUserProfile) {
652                CPhoneLine *cPhoneLine = cUserProfile->getCPhoneLine();
653                if (cPhoneLine) {
654                        if (cPhoneLine->hasPendingCalls()) {
655
656                                if (QMessageBox::question(
657                                        getWidget(),
658                                        tr("@product@ - Warning"),
659                                        tr("You have unfinished call(s).") + "\n" +
660                                                tr("Are you sure you want to exit the application?"),
661                                        tr("&Exit"),
662                                        tr("&Cancel")
663                                ) == 1)  {
664                                        return;
665                                }
666                        }
667                }
668        }
669        ////
670
671
672        _qtSystray->hide();
673
674        //Save the window size
675        QSize winsize = _wengoPhoneWindow->size();
676        config.set(Config::PROFILE_WIDTH_KEY, winsize.width());
677        config.set(Config::PROFILE_HEIGHT_KEY, winsize.height());
678
679        //Save the window position
680        QPoint winpos = _wengoPhoneWindow->pos();
681        config.set(Config::PROFILE_POSX_KEY, winpos.x());
682        config.set(Config::PROFILE_POSY_KEY, winpos.y());
683
684        QApplication::closeAllWindows();
685        QCoreApplication::processEvents();
686
687        //destroyed chatWindow so that chats are saved
688        OWSAFE_DELETE(_chatWindow);
689        ////
690
691        _cWengoPhone.terminate();
692}
693
694void QtWengoPhone::phoneComboBoxClicked() {
695
696        _ui->callBar->clearPhoneComboBox();
697        _ui->callBar->clearPhoneComboBoxEditText();
698}
699
700void QtWengoPhone::exitEvent() {
701        QCoreApplication::exit(EXIT_SUCCESS);
702}
703
704void QtWengoPhone::showHistory() {
705        _ui->tabWidget->setCurrentWidget(_ui->tabHistory);
706}
707
708void QtWengoPhone::currentUserProfileWillDieEventHandlerSlot() {
709        //Signal for re-initializing graphical components
710        //when there is no user profile anymore
711        userProfileDeleted();
712
713        // Reset _qtIMAccountMonitor so that it does not emit signals anymore
714        _qtIMAccountMonitor.reset(0);
715
716        OWSAFE_DELETE(_qtFileTransfer);
717        OWSAFE_DELETE(_qtIdle);
718        _ui->profileBar->reset();
719
720        if (_qtContactList) {
721                _ui->tabContactList->layout()->removeWidget(_qtContactList->getWidget());
722                OWSAFE_DELETE(_qtContactList);
723        }
724
725        if (_qtHistoryWidget) {
726                _ui->tabHistory->layout()->removeWidget(_qtHistoryWidget->getWidget());
727                OWSAFE_DELETE(_qtHistoryWidget);
728        }
729
730        uninstallQtBrowserWidget();
731}
732
733void QtWengoPhone::userProfileInitializedEventHandlerSlot() {
734        CUserProfile * cUserProfile = _cWengoPhone.getCUserProfileHandler().getCUserProfile();
735
736        //Idle detection
737        //FIXME: QtIdle must not use UserProfile but CUserProfile
738        _qtIdle = new QtIdle(cUserProfile->getUserProfile(), _wengoPhoneWindow);
739
740        // Create account monitor
741        _qtIMAccountMonitor.reset( new QtIMAccountMonitor(0, cUserProfile) );
742
743        //Create the profilebar
744        _ui->profileBar->init(&_cWengoPhone, cUserProfile, _qtIMAccountMonitor.get());
745
746        // Systray
747        SAFE_CONNECT_RECEIVER_TYPE(_qtIMAccountMonitor.get(), SIGNAL(imAccountAdded(QString)),
748                _qtSystray, SLOT(updateSystrayIcon()), Qt::QueuedConnection);
749        SAFE_CONNECT_RECEIVER_TYPE(_qtIMAccountMonitor.get(), SIGNAL(imAccountUpdated(QString)),
750                _qtSystray, SLOT(updateSystrayIcon()), Qt::QueuedConnection);
751        SAFE_CONNECT_RECEIVER_TYPE(_qtIMAccountMonitor.get(), SIGNAL(imAccountRemoved(QString)),
752                _qtSystray, SLOT(updateSystrayIcon()), Qt::QueuedConnection);
753
754        _qtSystray->setTrayMenu();
755        _qtSystray->updateSystrayIcon();
756
757        _qtFileTransfer = new QtFileTransfer(this, _cWengoPhone.getWengoPhone().getCoIpManager());
758
759        //menu
760        _qtToolBar->userProfileIsInitialized();
761}
762
763void QtWengoPhone::proxyNeedsAuthenticationEventHandler(NetworkProxyDiscovery & sender, NetworkProxy networkProxy) {
764        typedef PostEvent1<void (NetworkProxy networkProxy), NetworkProxy> MyPostEvent;
765        MyPostEvent * event =
766                new MyPostEvent(boost::bind(&QtWengoPhone::proxyNeedsAuthenticationEventHandlerThreadSafe, this, _1), networkProxy);
767        postEvent(event);
768}
769
770void QtWengoPhone::wrongProxyAuthenticationEventHandler(NetworkProxyDiscovery & sender, NetworkProxy networkProxy) {
771        typedef PostEvent1<void (NetworkProxy networkProxy), NetworkProxy> MyPostEvent;
772        MyPostEvent * event =
773                new MyPostEvent(boost::bind(&QtWengoPhone::proxyNeedsAuthenticationEventHandlerThreadSafe, this, _1), networkProxy);
774        postEvent(event);
775}
776
777void QtWengoPhone::proxyNeedsAuthenticationEventHandlerThreadSafe(NetworkProxy networkProxy) {
778        static QtHttpProxyLogin * httpProxy =
779                new QtHttpProxyLogin(getWidget(),
780                        networkProxy.getServer(), networkProxy.getServerPort());
781
782        int ret = httpProxy->show();
783
784        if (ret == QDialog::Accepted) {
785                NetworkProxy myNetworkProxy;
786                myNetworkProxy.setServer(httpProxy->getProxyAddress());
787                myNetworkProxy.setServerPort(httpProxy->getProxyPort());
788                myNetworkProxy.setLogin(httpProxy->getLogin());
789                myNetworkProxy.setPassword(httpProxy->getPassword());
790
791                NetworkProxyDiscovery::getInstance().setProxySettings(myNetworkProxy);
792        }
793}
794
795void QtWengoPhone::closeWindow() {
796        _wengoPhoneWindow->hide();
797}
798
799//#if defined(OS_MACOSX)
800//void QtWengoPhone::fixMacOSXMenus() {
801        // Avoids translation of these menus on Mac OS X. Thus Qt
802        // will put these under the Application menu
803        //_ui->actionShowConfig->setText("Preferences");
804        //_ui->actionShowAbout->setText("About");
805        //_ui->actionShowAbout->setText("Close");
806//}
807//#endif
808
809void QtWengoPhone::languageChanged() {
810//#if defined(OS_MACOSX)
811        //fixMacOSXMenus();
812//#endif
813        LOG_DEBUG("retranslate main window ui");
814        _ui->retranslateUi(_wengoPhoneWindow);
815        _qtToolBar->retranslateUi();
816
817        Config & config = ConfigManager::getInstance().getCurrentConfig();
818        if (_qtBrowserWidget) {
819                _ui->tabWidget->setTabText(_qtBrowserWidgetTabIndex, tr("Home"));
820        }
821
822        if (_qtHistoryWidget) {
823                _qtHistoryWidget->retranslateUi();
824        }
825       
826         _ui->menuWengo->setTitle(tr("Account"));
827}
828
829void QtWengoPhone::showAddContact(ContactInfo contactInfo) {
830
831        ensureVisible();
832
833        if (_cWengoPhone.getCUserProfileHandler().getCUserProfile()) {
834
835                //FIXME this method should not be called if no UserProfile has been set
836                ContactProfile contactProfile;
837                QtProfileDetails qtProfileDetails(*_cWengoPhone.getCUserProfileHandler().getCUserProfile(),
838                        contactProfile, _wengoPhoneWindow, tr("Add a Contact"));
839
840                //FIXME to remove when wdeal will be able to handle SIP presence
841                if (contactInfo.group == "WDeal") {
842                        qtProfileDetails.setHomePhone(QUrl::fromPercentEncoding(QByteArray(contactInfo.sip.c_str())));
843                } else {
844                        qtProfileDetails.setWengoName(QUrl::fromPercentEncoding(QByteArray(contactInfo.wengoName.c_str())));
845                }
846                ///
847
848                if (contactInfo.group == "WDeal") {
849                        qtProfileDetails.setFirstName(QUrl::fromPercentEncoding(QByteArray(contactInfo.wdealServiceTitle.c_str())));
850                } else {
851                        qtProfileDetails.setFirstName(QUrl::fromPercentEncoding(QByteArray(contactInfo.firstname.c_str())));
852                }
853
854                qtProfileDetails.setLastName(QUrl::fromPercentEncoding(QByteArray(contactInfo.lastname.c_str())));
855                qtProfileDetails.setCountry(QUrl::fromPercentEncoding(QByteArray(contactInfo.country.c_str())));
856                qtProfileDetails.setCity(QUrl::fromPercentEncoding(QByteArray(contactInfo.city.c_str())));
857                qtProfileDetails.setState(QUrl::fromPercentEncoding(QByteArray(contactInfo.state.c_str())));
858                qtProfileDetails.setGroup(QUrl::fromPercentEncoding(QByteArray(contactInfo.group.c_str())));
859                qtProfileDetails.setWebsite(QUrl::fromPercentEncoding(QByteArray(contactInfo.website.c_str())));
860
861                if (qtProfileDetails.show()) {
862                        _cWengoPhone.getCUserProfileHandler().getCUserProfile()->getCContactList().addContact(contactProfile);
863                }
864        }
865}
866
867void QtWengoPhone::bringMainWindowToFront() {
868        ensureVisible();
869}
870
871void QtWengoPhone::ensureVisible() {
872        _wengoPhoneWindow->activateWindow();
873        _wengoPhoneWindow->showNormal();
874        _wengoPhoneWindow->raise();
875}
876
877void QtWengoPhone::installQtBrowserWidget() 
878{       
879        _qtBrowserWidget = new QtBrowserWidget(*this);
880        _qtBrowserWidgetTabIndex = _ui->tabWidget->insertTab(0,_qtBrowserWidget->getWidget(), tr("Home"));
881        _ui->tabWidget->setCurrentIndex(0);
882}
883
884void QtWengoPhone::uninstallQtBrowserWidget() 
885{
886        //if (_qtBrowserWidget) {
887        //      _ui->tabWidget->widget(_qtBrowserWidgetTabIndex)->layout()->removeWidget(_qtBrowserWidget->getWidget());
888        //      _ui->tabWidget->removeTab(_qtBrowserWidgetTabIndex);
889        //      OWSAFE_DELETE(_qtBrowserWidget);
890        //}
891}
892
893void QtWengoPhone::setCurrentUserProfileEventHandlerSlot()
894{
895        _qtToolBar->tryingToConnect();
896}
Note: See TracBrowser for help on using the repository browser.