some UI changes for usability
[makneto-zunavac1.git] / src / ui-kde / roster-widget.cpp
blob1b64c8dc976aa1c7a510b5a932d0dfb2f077641e
1 /*
2 * Inspired by examples from Telepathy-Qt4 by Collabora.co.uk
3 * Copyright (C) 2011 Vojta Kulicka <vojtechkulicka@gmail.com>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library 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 GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include <QObject>
22 #include <TelepathyQt4/AccountManager>
23 #include <TelepathyQt4/PendingReady>
24 #include <TelepathyQt4/AccountSet>
26 #include "../backend/flat-model-proxy.h"
27 #include "../backend/telepathy-client.h"
28 #include "../backend/accounts-model-item.h"
29 #include "../backend/accounts-model.h"
30 //#include <TelepathyQt4/Models/AvatarImageProvider>
32 #include <QAbstractItemModel>
33 #include <QDeclarativeContext>
34 #include <QDeclarativeEngine>
35 #include <QVBoxLayout>
36 #include <QHBoxLayout>
37 #include <QTreeView>
38 #include <QAction>
39 #include <QPoint>
40 #include <QMenu>
41 #include <QInputDialog>
42 #include <qt4/QtGui/qmessagebox.h>
44 #include "roster-widget.h"
45 #include "account-presence-button.h"
46 #include "makneto.h"
47 #include "maknetoview.h"
48 #include "sessiontabmanager.h"
50 RosterWidget::RosterWidget(QWidget *parent)
51 : QWidget(parent) {
52 QVBoxLayout *layout = new QVBoxLayout(this);
53 m_hlayout = new QHBoxLayout(this);
55 m_view = new QTreeView(this);
56 m_view->setIndentation(5);
57 m_view->setUniformRowHeights(true);
58 m_view->alternatingRowColors();
59 m_view->setHeaderHidden(true);
61 layout->addLayout(m_hlayout);
62 layout->addWidget(m_view);
64 setLayout(layout);
65 show();
66 // mView->engine()->addImageProvider(
67 // QString::fromLatin1("avatars"),
68 // new Tp::AvatarImageProvider(am));
71 void RosterWidget::onInitializationFinished(MaknetoBackend::TelepathyClient *client) {
72 //QAbstractItemModel *contactModel = new MaknetoLib::FlatModelProxy(client->accountsModel());
74 MaknetoBackend::AccountsModel* accountsModel = client->accountsModel();
75 m_view->setModel(accountsModel);
76 m_view->setContextMenuPolicy(Qt::CustomContextMenu);
78 //send a signal to sessionTabManager about new session request
79 connect(this, SIGNAL(textChatRequested(QModelIndex)),
80 Makneto::Instance()->getMaknetoMainWindow()->getMaknetoView()->getSessionTabManager(), //sessionTabManager
81 SLOT(onTextChatRequested(QModelIndex)));
83 //neccesary for context menu to work
84 connect(m_view, SIGNAL(customContextMenuRequested(QPoint)),
85 this, SLOT(onCustomContextMenuRequested(QPoint)));
87 connect(accountsModel, SIGNAL(accountConnectionStatusChanged(Tp::AccountPtr, Tp::ConnectionStatus)),
88 this, SLOT(onAccountStatusChanged(Tp::AccountPtr, Tp::ConnectionStatus)));
90 qDebug() << "Telepathy initialization has finished";
91 //qDebug() << "Available accounts by ID: ";
92 /* foreach(Tp::AccountPtr account, client->accountManager()->enabledAccounts()->accounts()){
93 qDebug() << account->uniqueIdentifier();
94 m_hlayout->addWidget(new AccountPresenceButton(account,this));
95 }*/
98 void RosterWidget::onStartTextChat() {
99 qDebug() << "RosterWidget: on start text chat";
101 emit textChatRequested(m_view->currentIndex());
104 void RosterWidget::onAccountStatusChanged(const Tp::AccountPtr &account, Tp::ConnectionStatus status){
105 qDebug() << "on account status changed (" << account->uniqueIdentifier() << ", " << status << ")";
107 if (status == Tp::ConnectionStatusDisconnected) {
108 QString connectionError = account->connectionError();
110 // ignore user disconnect
111 if (connectionError != "org.freedesktop.Telepathy.Error.Cancelled") {
112 handleConnectionError(account, account->connectionStatusReason());
117 void RosterWidget::handleConnectionError(const Tp::AccountPtr &account, Tp::ConnectionStatusReason reason){
118 if (!account->isEnabled()){
119 QMessageBox::critical(this, tr("Error"), tr("Could not connect %1. Account is disabled!").arg( account->displayName()) );
120 }else if (reason == Tp::ConnectionStatusReasonAuthenticationFailed) {
121 QMessageBox::critical(this, tr("Error"), tr("Could not connect %1. Authentication failed (is your password correct?)").arg( account->displayName()) );
122 } else if (reason == Tp::ConnectionStatusReasonNetworkError) {
123 QMessageBox::critical(this, tr("Error"), tr("Could not connect %1. There was a network error, check your connection").arg( account->displayName()) );
124 } else {
125 // other errors
126 QMessageBox::critical(this, tr("Error"), tr("An unexpected error has occurred with %1: '%2'").arg( account->displayName()).arg(account->connectionError()) );
130 void RosterWidget::onStartAudioChat() {
131 // FIXME: implement this
132 qDebug() << "on start audio chat";
135 void RosterWidget::onStartVideoChat(){
136 // FIXME: implement this
137 qDebug() << "on start video chat";
140 void RosterWidget::onCustomContextMenuRequested(QPoint point) {
141 Q_UNUSED(point);
143 QModelIndex index = m_view->currentIndex();
145 QScopedPointer<QMenu> contextMenu(new QMenu());
146 QMenu *presenceSubMenu;
147 QAction *action;
149 //context menu for an account
150 if (index.data(MaknetoBackend::AccountsModel::AliasRole) == QVariant()) {
152 connect(contextMenu.data(), SIGNAL(triggered(QAction *)),
153 this, SLOT(onChangePresence(QAction *)));
156 contextMenu->setTitle(index.data(MaknetoBackend::AccountsModel::DisplayNameRole).toString());
157 presenceSubMenu = contextMenu->addMenu("Set presence");
158 action = presenceSubMenu->addAction("Available");
159 action->setIcon(KIcon("maknetoonline"));
160 action->setData(MaknetoBackend::TelepathyClient::Available);
162 action = presenceSubMenu->addAction("Away");
163 action->setIcon(KIcon("maknetoaway"));
164 action->setData(MaknetoBackend::TelepathyClient::Away);
166 action = presenceSubMenu->addAction("Busy");
167 action->setIcon(KIcon("maknetodnd"));
168 action->setData(MaknetoBackend::TelepathyClient::Busy);
170 action = presenceSubMenu->addAction("Invisible");
171 action->setIcon(KIcon("maknetoinvisible"));
172 action->setData(MaknetoBackend::TelepathyClient::Invisible);
174 action = presenceSubMenu->addAction("Offline");
175 action->setIcon(KIcon("maknetooffline"));
176 action->setData(MaknetoBackend::TelepathyClient::Offline);
178 m_actionMUC = contextMenu->addAction("Start/Join MUC");
180 }//context menu for contact
181 else {
182 contextMenu->setTitle(index.data(MaknetoBackend::AccountsModel::AliasRole).toString());
183 action = contextMenu->addAction("Text Chat");
184 connect(action, SIGNAL(triggered(bool)),
185 this, SLOT(onStartTextChat()));
186 action = contextMenu->addAction("Start Audio Chat");
187 connect(action, SIGNAL(triggered(bool)),
188 this, SLOT(onStartAudioChat()));
189 action = contextMenu->addAction("Start Video Chat");
190 connect(action, SIGNAL(triggered(bool)),
191 this, SLOT(onStartVideoChat()));
195 //TODO figure out why the damn title does not show!!!!
196 contextMenu->exec(QCursor::pos());
199 void RosterWidget::onChangePresence(QAction *action) {
200 bool ok;
201 //start MUC
202 if (action == m_actionMUC) {
204 QString chatRoomName = QInputDialog::getText(this, tr("QInputDialog::getText()"),
205 tr("Chatroom name:"), QLineEdit::Normal,
206 tr(""), &ok);
207 if (ok)
208 MaknetoBackend::TelepathyClient::Instance()->onNewTextChatRoomRequested(m_view->currentIndex(), chatRoomName);
209 }//set presence
210 else {
211 QString statusMsg = QString();
212 if (action->data() != MaknetoBackend::TelepathyClient::Offline) {
213 statusMsg = QInputDialog::getText(this, tr("QInputDialog::getText()"),
214 tr("Status message:"), QLineEdit::Normal,
215 tr(""), &ok);
217 MaknetoBackend::TelepathyClient::Instance()->setAccountPresence(m_view->currentIndex(), action->data().toInt(), statusMsg);