Initial commit
[tietoopcom] / src / TocUi / TocMainWindow.cpp
1 /** \file TocMainWindow.cpp
2  * \brief Implementation of TocMainWindow class
3  * 
4  * Tieto Open Communicator - Client for the Telepathy communications framework.
5  * Copyright (c) 2010, Tieto Corporation
6  *
7  * All rights reserved.
8  * Redistribution and use in source and binary forms, with or without modification,
9  * are permitted provided that the following conditions are met:
10  *
11  *      Redistributions of source code must retain the above copyright notice,
12  *      this list of conditions and the following disclaimer.
13  *      Redistributions in binary form must reproduce the above copyright notice,
14  *      this list of conditions and the following disclaimer in the documentation
15  *      and/or other materials provided with the distribution.
16  *      Neither the name of the Tieto Corporation nor the names of its contributors 
17  *      may be used to endorse or promote products derived from this software without
18  *      specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  * 
30  */
31
32 #include "TocMainWindow"
33
34 #include "TocMainToolbar"
35 #include "TocContactListWidget"
36 #include "TocChatWidget"
37 #include "TocUserInfoWidget"
38 #include "TocSettingsWidget"
39 #include "TocGeneralSettingsWidget"
40 #include "TocAccountSettingsWidget"
41 #include "TocAccountIntermediateWidget"
42 #include "TocThemesSettingsWidget"
43 #include "TocSettings"
44 #include "macros.h"
45 #include "version.h"
46
47 #include <QStackedLayout>
48 #include <QList>
49 #include <QDebug>
50 #include <QMessageBox>
51 #include <QMenuBar>
52 #include <QFile>
53 #include <QCloseEvent>
54
55
56 Q_DECLARE_METATYPE(TocContact*);
57
58 TocMainWindow::TocMainWindow(TocBaseEngine* engine, QWidget * parent, Qt::WindowFlags flags) :
59         TocBaseMainWindow(engine, parent, flags), _register(false) {
60         
61         setWindowTitle("Tieto Open Communicator");
62         setContentsMargins( -20, 0, -20, -5 );
63         _pCentralWidget = new QWidget( this );
64         _pCentralWidget->setObjectName("_pCentralWidget");
65
66         QAction* pAboutAct = new QAction(tr("About"), this);
67         pAboutAct->setStatusTip(tr("Show About info"));
68         CONNECT(pAboutAct, SIGNAL(triggered()), this, SLOT(showAbout()));
69
70         QAction* pExitAct = new QAction(tr("Exit"), this);
71         pExitAct->setStatusTip(tr("Close application"));
72         CONNECT(pExitAct, SIGNAL(triggered()), this, SLOT(close()));
73
74         menuBar()->addAction(pAboutAct);
75         menuBar()->addAction(pExitAct);
76
77         setCentralWidget( _pCentralWidget );
78         QVBoxLayout* verticalLayout = new QVBoxLayout( _pCentralWidget );
79         verticalLayout->setSpacing( 0 );
80
81         _pMainToolbar = new TocMainToolbar( _pCentralWidget );
82         CONNECT( _pMainToolbar, SIGNAL(contactsClicked()), this, SLOT(showContacts()) );
83         CONNECT( _pMainToolbar, SIGNAL(settingsClicked()), this, SLOT(showSettings()) );
84         CONNECT( _pMainToolbar, SIGNAL(finishedSession(const QString&)), this, SLOT(closeChat(const QString&)) );
85         CONNECT( _pMainToolbar, SIGNAL(finishedAllSessions()), this, SLOT(closeAllOpenChats()) );
86         CONNECT( _pMainToolbar, SIGNAL(activeSessionTriggered(QString,QString)), this, SLOT(showChat(QString,QString)) );
87         CONNECT( _pMainToolbar, SIGNAL(presenceUpdate(Presence,QString)), this, SIGNAL(presenceChanged(Presence,QString)) );
88
89         verticalLayout->addWidget( _pMainToolbar );
90         _pMainLayout = new QStackedLayout( verticalLayout );
91         _pMainLayout->setSpacing( 0 );
92
93         _pChatList = new QList<TocChatWidget*>();
94         _pOpenChatList = new QList<TocChatWidget*>();
95
96         _pSettingsWidget = new TocSettingsWidget( _pCentralWidget );
97         _pMainLayout->addWidget( _pSettingsWidget );
98         CONNECT(_pSettingsWidget, SIGNAL(triggered(int)), this, SLOT(showView(int)));
99
100         _pContactList = new TocContactListWidget( _pCentralWidget );
101         _pMainLayout->addWidget( _pContactList );
102
103         CONNECT( _pContactList, SIGNAL(triggered(QString,QString,Presence)), _pMainToolbar, SLOT(onNewSession(QString,QString,Presence)) );
104         CONNECT( _pContactList, SIGNAL(contactInfoRequest(QListWidgetItem*)), this, SLOT(showUserInfo(QListWidgetItem*)) );
105         CONNECT( _pContactList, SIGNAL(triggered(QString,QString,Presence)), this, SLOT(showChat(QString,QString,Presence)) );
106         CONNECT( _pContactList, SIGNAL(addContact(TocContact*)),
107                         this, SIGNAL(addContact(TocContact*)));
108         CONNECT( _pContactList, SIGNAL(removedContact(QString)),
109                         this, SIGNAL(removeContact(QString)));  
110         CONNECT( _pContactList, SIGNAL(editContact(TocContact*,QString)),
111                         this, SIGNAL(editContact(TocContact*,QString)));
112         CONNECT( _pContactList, SIGNAL(displayedNameChanged(QString,QString)),
113                         this, SLOT(onDisplayedNameChange(QString,QString)) );
114
115         _pUserInfoWidget = 0;
116         _pGeneralWidget = 0;
117         _pAccountWidget = 0;
118         _pIntermediateWidget = 0;
119         _pThemesWidget = 0;
120
121         if(isFtu())
122                 showView( static_cast<int>( FTU ) );
123         else
124                 _pMainLayout->setCurrentWidget( _pContactList );
125
126         setStyleSheet(
127 //                      "TocMainToolbar > QPushButton { background-color: qlineargradient(x1: 0, y1: 1, x2: 0, y2: 0,"
128 //                      "stop: 0 #aa0, stop: 0.55 #bb0, stop: 0.56 #bb0, stop: 1 #ff0); }"
129 //                      "QPushButton#okButton { background-color: qlineargradient(x1: 0, y1: 1, x2: 0, y2: 0,"
130 //                      "stop: 0 #0aa, stop: 0.55 #0bb, stop: 0.56 #0bb, stop: 1 #0ff); }"
131 //                      "QPushButton#cancelButton { background-color: qlineargradient(x1: 0, y1: 1, x2: 0, y2: 0,"
132 //                      "stop: 0 #0aa, stop: 0.55 #0bb, stop: 0.56 #0bb, stop: 1 #0ff); }"
133 //                      "QPushButton#generalButton { background-color: qlineargradient(x1: 0, y1: 1, x2: 0, y2: 0,"
134 //                      "stop: 0 #0aa, stop: 0.55 #0bb, stop: 0.56 #0bb, stop: 1 #0ff); }"
135 //                      "QPushButton#themesButton { background-color: qlineargradient(x1: 0, y1: 1, x2: 0, y2: 0,"
136 //                      "stop: 0 #0aa, stop: 0.55 #0bb, stop: 0.56 #0bb, stop: 1 #0ff); }"
137 //                      "QPushButton#accountButton { background-color: qlineargradient(x1: 0, y1: 1, x2: 0, y2: 0,"
138 //                      "stop: 0 #0aa, stop: 0.55 #0bb, stop: 0.56 #0bb, stop: 1 #0ff); }"
139 //                      "QPushButton#networkButton { background-color: qlineargradient(x1: 0, y1: 1, x2: 0, y2: 0,"
140 //                      "stop: 0 #0aa, stop: 0.55 #0bb, stop: 0.56 #0bb, stop: 1 #0ff); }"
141 //                      "QPushButton#statusButton { background-color: qlineargradient(x1: 0, y1: 1, x2: 0, y2: 0,"
142 //                      "stop: 0 #aaa, stop: 0.55 #bbb, stop: 0.56 #bbb, stop: 1 #fff); }"
143 //                      "QPushButton#settingsButton { background-color: qlineargradient(x1: 0, y1: 1, x2: 0, y2: 0,"
144 //                      "stop: 0 #0aa, stop: 0.55 #0bb, stop: 0.56 #0bb, stop: 1 #0ff); }"
145 //                      "*[mandatoryField=\"true\"] {background-color: qlineargradient(x1: 0, y1: 1, x2: 0, y2: 0,"
146 //                      "stop: 0 #aa0, stop: 0.55 #bb0, stop: 0.56 #bb0, stop: 1 #ff0); }"
147
148                         "QMessageBox QPushButton {"
149                         "border: none;"
150                         "min-width: 80px;"
151                         "min-height: 40px;"
152                         "}"
153                         "QMenuBar {"
154                         "max-height: 0px;"
155                         "}"
156         );
157
158         _defaultStyle = styleSheet();
159         reloadTheme();
160 }
161
162 TocMainWindow::~TocMainWindow() {
163
164         delete _pChatList;
165         delete _pOpenChatList;
166 }
167
168 TocChatWidget* TocMainWindow::chatForUid(const QString& uid) {
169         foreach( TocChatWidget* chat, *_pChatList )
170                 if ( chat->uid() == uid )
171                         return chat;
172         return 0;
173 }
174
175 TocChatWidget* TocMainWindow::openChatForUid(const QString& uid) {
176         foreach( TocChatWidget* chat, *_pOpenChatList )
177                 if ( chat->uid() == uid )
178                         return chat;
179         return 0;
180 }
181
182 void TocMainWindow::onPresenceUpdate(Presence presence, QString desc) {
183         _pMainToolbar->onPresenceUpdate( presence, desc );
184         if( presence == Offline )
185                 _pContactList->clearStatuses();
186 }
187
188 void TocMainWindow::onContactPresenceUpdate(QString uid, Presence presence, QString desc) {
189         if ( TocChatWidget* chat = openChatForUid( uid ) ) {
190                 chat->onContactPresenceUpdate( presence, desc );
191                 _pMainToolbar->onContactPresenceUpdate( uid, presence );
192         }       
193
194         _pContactList->onContactPresenceUpdate( uid, presence, desc );
195 }
196
197 void TocMainWindow::onIncomingMessage(QString uid, Message message) {
198
199         TocChatWidget* chat;
200         if ( chat = chatForUid( uid ) ) {
201                 _pOpenChatList->append( chat );
202                 chat->appendMessage( message );
203                 QListWidgetItem* pItem = _pContactList->findItem( uid );
204                 if( pItem ) {
205                         QString name = pItem->data( DisplayRole ).toString();
206                         TocContact *pContact = pItem->data(ContactRole).value<TocContact*>();
207                         _pMainToolbar->onNewMessage( uid, name, pContact->presence );
208                 } else {
209                         _pMainToolbar->onNewMessage( uid, uid, static_cast<Presence>(0) );
210                 }
211         } else {
212                 QListWidgetItem* pItem = _pContactList->findItem( uid );
213                 if( !pItem ) {
214                         chat = new TocChatWidget( uid, uid, static_cast<Presence>(0), _pCentralWidget, message );
215                         _pChatList->append( chat );
216                         _pOpenChatList->append( chat );
217                         _pMainLayout->addWidget( chat );
218                         _pMainToolbar->onNewMessage( uid, uid, static_cast<Presence>(0) );
219                 } else {
220                         QString name = pItem->data( DisplayRole ).toString();
221                         TocContact *pContact = pItem->data(ContactRole).value<TocContact*>();
222                         chat = new TocChatWidget( uid, name, pContact->presence, _pCentralWidget, message );
223                         _pChatList->append( chat );
224                         _pOpenChatList->append( chat );
225                         _pMainLayout->addWidget( chat );
226                         _pMainToolbar->onNewMessage( uid, name, pContact->presence );
227                 }
228                 CONNECT(chat, SIGNAL(newMessageReady(QString,Message)), this, SIGNAL(newMessageReady(QString,Message)));
229         }
230 }
231
232 void TocMainWindow::closeChat(const QString& uid) {
233
234         if ( TocChatWidget* chat = openChatForUid( uid ) ) {
235                 _pOpenChatList->removeOne( chat );
236                 emit sessionClosed( uid );
237         }
238 }
239
240 void TocMainWindow::closeAllOpenChats() {
241
242         foreach( TocChatWidget* chat, *_pOpenChatList ) {
243                 emit sessionClosed( chat->uid() );
244         }
245         _pOpenChatList->clear();
246 }
247
248 void TocMainWindow::clearAllChats() {
249
250         // First close open chats
251         closeAllOpenChats();
252
253         foreach( TocChatWidget* chat, *_pChatList ) {
254                 _pMainLayout->removeWidget( chat );
255                 delete chat;
256         }
257
258         _pChatList->clear();
259 }
260
261 void TocMainWindow::showChat(const QString& uid, const QString& name, Presence presence) {
262
263         if ( TocChatWidget* chat = chatForUid( uid ) ) {
264                 if(!openChatForUid(uid)) {
265                         _pOpenChatList->append( chat );
266                         chat->updateContactPresence( presence );
267                 }
268
269                 _pMainLayout->setCurrentWidget( chat );
270         } else {
271                 chat = new TocChatWidget( uid, name, presence, _pCentralWidget );
272                 _pChatList->append( chat );
273                 _pOpenChatList->append( chat );
274                 _pMainLayout->addWidget( chat );
275                 _pMainLayout->setCurrentWidget( chat );
276                 CONNECT(chat, SIGNAL(newMessageReady(QString,Message)), this, SIGNAL(newMessageReady(QString,Message)));
277         }
278 }
279
280 void TocMainWindow::showContacts() {
281         _pMainLayout->setCurrentWidget( _pContactList );
282 }
283
284 void TocMainWindow::returnToContacts() {
285         _pMainLayout->setCurrentWidget( _pContactList );
286         _pMainToolbar->onCurrentView( Contacts );
287 }
288
289 void TocMainWindow::showSettings() {
290         _pMainLayout->setCurrentWidget( _pSettingsWidget );
291 }
292
293 void TocMainWindow::returnToSettings() {
294         _pMainLayout->setCurrentWidget( _pSettingsWidget );
295         _pMainToolbar->onCurrentView( Settings, tr("Settings") );
296 }
297
298 void TocMainWindow::reloadTheme() {
299         QString current = styleSheet();
300         QString theme;
301
302         TocSettings* pSettings = TocSettings::getInstance();
303
304         QFile file(pSettings->currentTheme());
305
306         file.open(QIODevice::ReadOnly | QIODevice::Text);
307         if (file.exists()) {
308                 theme = file.readAll();
309                 file.close();
310         } else
311                 return;
312
313         if (theme + _defaultStyle != current) {
314                 if (theme.isEmpty())
315                         setStyleSheet(_defaultStyle);
316                 else
317                         setStyleSheet(theme + _defaultStyle);
318         }
319 }
320
321 void TocMainWindow::returnToIntermediate() {
322         if(isFtu())
323                 showView( static_cast<int>( FTU ) );
324         else
325                 showView( static_cast<int>( AccountIntermediate ) );
326 }
327
328 void TocMainWindow::showAccount() {
329         showView( static_cast<int>( Account ) );
330 }
331
332 void TocMainWindow::showAccountCreate() {
333         showView( static_cast<int>( AccountCreate ) );
334 }
335
336 void TocMainWindow::showView(int type) {
337         ViewType viewType = static_cast<ViewType>( type );
338
339         switch( viewType ) {
340
341         case Contacts:
342                 showContacts();
343                 _pMainToolbar->onCurrentView( Contacts );
344                 break;
345
346         case Chat:
347                 /* Do nothing */
348                 break;
349
350         case UserInfo:
351                 showUserInfo( 0 );
352                 break;
353
354         case Settings:
355                 showSettings();
356                 break;
357
358         case General:
359                 if( !_pGeneralWidget ) {
360                         _pGeneralWidget = new TocGeneralSettingsWidget( _pCentralWidget );
361                         CONNECT(_pGeneralWidget, SIGNAL(backClicked()), this, SLOT(returnToSettings()));
362                         CONNECT(_pGeneralWidget, SIGNAL(finished()), this, SLOT(returnToContacts()));
363                         _pMainLayout->addWidget( _pGeneralWidget );
364                 }
365                 _pMainToolbar->onCurrentView( viewType, tr("General Settings") );
366                 _pMainLayout->setCurrentWidget( _pGeneralWidget );
367                 break;
368
369         case FTU:
370                 if( !_pIntermediateWidget ) {
371                         _pIntermediateWidget = new TocAccountIntermediateWidget( _pCentralWidget );
372                         CONNECT(_pIntermediateWidget, SIGNAL(backClicked()), this, SLOT(returnToSettings()));
373                         CONNECT(_pIntermediateWidget, SIGNAL(editClicked()), this, SLOT(showAccount()));
374                         CONNECT(_pIntermediateWidget, SIGNAL(createClicked()), this, SLOT(showAccountCreate()));
375                         _pMainLayout->addWidget( _pIntermediateWidget );
376                 }
377                 _pIntermediateWidget->enableFTUMode( true );
378                 _pMainToolbar->onCurrentView( viewType, tr("Account Settings") );
379                 _pMainLayout->setCurrentWidget( _pIntermediateWidget );
380                 break;
381         
382         case AccountIntermediate:
383                 if( !_pIntermediateWidget ) {
384                         _pIntermediateWidget = new TocAccountIntermediateWidget( _pCentralWidget );
385                         CONNECT(_pIntermediateWidget, SIGNAL(backClicked()), this, SLOT(returnToSettings()));
386                         CONNECT(_pIntermediateWidget, SIGNAL(editClicked()), this, SLOT(showAccount()));
387                         CONNECT(_pIntermediateWidget, SIGNAL(createClicked()), this, SLOT(showAccountCreate()));
388                         _pMainLayout->addWidget( _pIntermediateWidget );
389                 }
390                 _pIntermediateWidget->enableFTUMode( false );
391                 _pMainToolbar->onCurrentView( viewType, tr("Account Settings") );
392                 _pMainLayout->setCurrentWidget( _pIntermediateWidget );
393                 break;
394
395         case AccountCreate:
396                 if( !_pAccountWidget ) {
397                         _pAccountWidget = new TocAccountSettingsWidget( _pCentralWidget );
398                         CONNECT(_pAccountWidget, SIGNAL(newAccountSettingsCreated()), this, SLOT(onAccountSettingsCreated()));
399                         CONNECT(_pAccountWidget, SIGNAL(accountSettingsChanged()), this, SLOT(onAccountSettingsChanged()));                     CONNECT(_pAccountWidget, SIGNAL(finished()), this, SLOT(returnToContacts()));
400                         CONNECT(_pAccountWidget, SIGNAL(finished()), this, SLOT(returnToContacts()));
401                         CONNECT(_pAccountWidget, SIGNAL(backClicked()), this, SLOT(returnToIntermediate()));
402                         _pMainLayout->addWidget( _pAccountWidget );
403                 }
404                 _pAccountWidget->setCreateMode(true);
405                 _pMainToolbar->onCurrentView( viewType, tr("Create Account") );
406                 _pMainLayout->setCurrentWidget( _pAccountWidget );
407                 break;
408
409         case Account:
410                 if( !_pAccountWidget ) {
411                         _pAccountWidget = new TocAccountSettingsWidget( _pCentralWidget );
412                         CONNECT(_pAccountWidget, SIGNAL(newAccountSettingsCreated()), this, SLOT(onAccountSettingsCreated()));
413                         CONNECT(_pAccountWidget, SIGNAL(accountSettingsChanged()), this, SLOT(onAccountSettingsChanged()));
414                         CONNECT(_pAccountWidget, SIGNAL(finished()), this, SLOT(returnToContacts()));
415                         CONNECT(_pAccountWidget, SIGNAL(backClicked()), this, SLOT(returnToIntermediate()));
416                         _pMainLayout->addWidget( _pAccountWidget );
417                 }
418                 _pAccountWidget->setCreateMode(false);
419                 _pMainToolbar->onCurrentView( viewType, tr("Account Settings") );
420                 _pMainLayout->setCurrentWidget( _pAccountWidget );
421                 break;
422
423         case Themes:
424                 if( !_pThemesWidget ) {
425                         _pThemesWidget = new TocThemesSettingsWidget( _pCentralWidget );
426                         _pMainLayout->addWidget( _pThemesWidget );
427                         CONNECT(_pThemesWidget, SIGNAL(backClicked()), this, SLOT(returnToSettings()));
428                         CONNECT(_pThemesWidget, SIGNAL(currentThemeChanged()), this, SLOT(reloadTheme()));
429                 }       
430                 _pMainToolbar->onCurrentView( viewType, tr("Themes Settings") );
431                 _pMainLayout->setCurrentWidget( _pThemesWidget );
432                 break;
433
434         default:
435                 /* Do nothing */
436                 break;
437         }
438 }
439
440 void TocMainWindow::showUserInfo(QListWidgetItem* item) {
441         
442         _pMainLayout->removeWidget( _pUserInfoWidget );
443         delete _pUserInfoWidget;
444         
445         _pUserInfoWidget = new TocUserInfoWidget( item, _pContactList );
446
447         if ( item == 0 ) {
448                 CONNECT(_pUserInfoWidget, SIGNAL(contactChanged(QListWidgetItem*)),
449                                 _pContactList, SLOT(commitAdd(QListWidgetItem*)));
450
451                 _pMainToolbar->onCurrentView( UserInfo, tr("Create Contact") );
452
453         } else {
454                 CONNECT(_pUserInfoWidget, SIGNAL(contactChanged(QListWidgetItem*)),
455                                 _pContactList, SLOT(commitEdit(QListWidgetItem*)));
456
457                 QString name = item->data( DisplayRole ).toString();
458                 _pMainToolbar->onCurrentView( UserInfo, tr("Edit %1").arg( name ) );
459         }
460
461         CONNECT(_pUserInfoWidget, SIGNAL(finished()), this, SLOT(returnToContacts()));
462
463         _pMainLayout->addWidget( _pUserInfoWidget );
464         _pMainLayout->setCurrentWidget( _pUserInfoWidget );
465 }
466
467 void TocMainWindow::onDisplayedNameChange(const QString& uid, const QString& name) {
468
469         if ( TocChatWidget* chat = chatForUid( uid ) ) {
470                 chat->onDisplayedNameChange( name );
471                 _pMainToolbar->onDisplayedNameChange( uid, name );
472         }
473 }
474
475 void TocMainWindow::onContactListReceived(const TocContactList& cList)
476 {
477         _pContactList->onNewContactList(cList);
478 }
479
480 void TocMainWindow::onAuthorizationRequest(QString /*uid*/) {
481         //TODO: finish implementation
482 }
483
484 void TocMainWindow::onSubscriptionAccepted(QString /*uid*/) {
485         //TODO: finish implementation
486 }
487
488 void TocMainWindow::onStatusChange(Status status, Reason reason) {
489         switch(status) {
490         case Disconnected:
491                 if(reason != Requested)
492                         if(_register) {
493                                 QMessageBox::critical(this, tr("TEC"), errorString(reason) + "\nCannot create account");
494                                 _register = false;
495                         } else
496                                 QMessageBox::critical(this, tr("TEC"), errorString(reason));
497
498                 _pMainToolbar->onPresenceUpdate( Offline, "" );
499
500                 break;
501         case Connected:
502                 if(_register) {
503                         QMessageBox::information(this, tr("TEC"), tr("Account created successfully!"));
504                         _register = false;
505                         returnToContacts();
506                 } break;
507         case Connecting:
508                 /* Do nothing */
509                 break;
510         default:
511                 /* Do nothing */
512                 break;
513         }
514 }
515
516 QString TocMainWindow::errorString(Reason reason) {
517         switch(reason) {
518         case NoneSpecified:
519                 return tr("Disconnected because of unknown reason.");
520
521         case Requested:
522                 return tr("Disconnected because it was requested.");
523
524         case NetworkError:
525                 return tr("Disconnected because of network error.");
526
527         case AuthenticationFailed:
528                 return tr("Disconnected because the username or password was invalid.");
529
530         case EncryptionError:
531                 return tr("Disconnected because of encryption error.");
532
533         case NameInUse:
534                 return tr("Could not create account or connect because username is already in use.");
535
536         case CertificateNotProvided:
537                 return tr("Disconnected because certificate was not provided.");
538
539         case CertificateUntrusted:
540                 return tr("Disconnected because certificate is untrusted.");
541
542         case CertificateExpired:
543                 return tr("Disconnected because certificate has expired.");
544
545         case CertificateNotActivated:
546                 return tr("Disconnected because certificate has not been activated.");
547
548         case CertificateHostnameMismatch:
549                 return tr("Disconnected because of certificate hostname mismatch.");
550
551         case CertificateFingerPrintMismatch:
552                 return tr("Disconnected because of certificate finger print mismatch.");
553
554         case CertificateSigned:
555                 return tr("Disconnected because certificate was self-signed.");
556
557         case CertificateOtherError:
558                 return tr("Disconnected because of non specified certificate error.");
559
560         default:
561                 return tr("Disconnected because of unknown reason.");
562         }
563 }
564
565 bool TocMainWindow::isFtu() {
566         TocSettings *set = TocSettings::getInstance();
567         if(!set->accountUid().length() && !set->accountPasswd().length())
568                 return true;
569         return false;
570 }
571
572 void TocMainWindow::showAbout() {
573         QMessageBox::about(this, tr("About Tieto Open Communicator"), tr(       
574                                 "Tieto Open Communicator %1 - Instant Message Communicator for Telepathy framework\n\n"
575                                 "Copyright: (c) 2010, Tieto Corporation\n"
576                                 "License: see `/usr/share/%2/licence/copyright`."
577                                                 ).arg(VERSION).arg(TARGET));
578 }
579
580 void TocMainWindow::onAccountSettingsChanged() {
581         _register = false;
582         emit presenceChanged(Offline, "");
583
584         // Clear chat widgets and active sessions
585         clearAllChats();
586         _pMainToolbar->clearAllSessions();
587
588         // Empty the contact list
589         TocSettings *pSettings = TocSettings::getInstance();
590         pSettings->contactList()->clear();
591         _pContactList->onNewContactList(*pSettings->contactList());
592 }
593
594 void TocMainWindow::onAccountSettingsCreated() {
595         onAccountSettingsChanged();
596
597         _pMainToolbar->onCurrentView(AccountCreate, tr("Create Account"));
598         _register = true;
599         // Request to create account
600         emit presenceChanged(Available, "");
601         QMessageBox::information(this, tr("TEC"),
602                 tr("To be able to fully use Your new account:\nplease reconnect."));
603 }
604
605 void TocMainWindow::closeEvent( QCloseEvent* event ) {
606         Q_UNUSED(event)
607         hide();
608 }
609
610 int TocMainWindow::top_application() {
611     show();
612     return 0;
613 }