1 /**
2 * This file is part of TelepathyQt
3 *
4 * @copyright Copyright (C) 2009-2011 Collabora Ltd. <http://www.collabora.co.uk/>
5 * @license LGPL 2.1
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include "roster-window.h"
23 #include "_gen/roster-window.moc.hpp"
24
25 #include "roster-widget.h"
26
27 #include <TelepathyQt/Types>
28 #include <TelepathyQt/ChannelFactory>
29 #include <TelepathyQt/Connection>
30 #include <TelepathyQt/ConnectionFactory>
31 #include <TelepathyQt/ContactFactory>
32 #include <TelepathyQt/PendingOperation>
33 #include <TelepathyQt/PendingReady>
34
35 #include <QDebug>
36
37 using namespace Tp;
38
RosterWindow(const QString & accountName,QWidget * parent)39 RosterWindow::RosterWindow(const QString &accountName, QWidget *parent)
40 : QMainWindow(parent)
41 {
42 setWindowTitle(QLatin1String("Roster"));
43
44 setupGui();
45
46 ChannelFactoryPtr channelFactory = ChannelFactory::create(
47 QDBusConnection::sessionBus());
48 ConnectionFactoryPtr connectionFactory = ConnectionFactory::create(
49 QDBusConnection::sessionBus(), Connection::FeatureConnected |
50 Connection::FeatureRoster | Connection::FeatureRosterGroups);
51 ContactFactoryPtr contactFactory = ContactFactory::create(
52 Contact::FeatureAlias | Contact::FeatureSimplePresence);
53
54 mAccount = Account::create(TP_QT_ACCOUNT_MANAGER_BUS_NAME,
55 TP_QT_ACCOUNT_OBJECT_PATH_BASE + QLatin1Char('/') + accountName,
56 connectionFactory, channelFactory, contactFactory);
57 connect(mAccount->becomeReady(Account::FeatureCore),
58 SIGNAL(finished(Tp::PendingOperation *)),
59 SLOT(onAccountReady(Tp::PendingOperation *)));
60
61 resize(240, 320);
62 }
63
~RosterWindow()64 RosterWindow::~RosterWindow()
65 {
66 }
67
setupGui()68 void RosterWindow::setupGui()
69 {
70 mRoster = new RosterWidget();
71 setCentralWidget(mRoster);
72 }
73
onAccountReady(Tp::PendingOperation * op)74 void RosterWindow::onAccountReady(Tp::PendingOperation *op)
75 {
76 if (op->isError()) {
77 qWarning() << "Account cannot become ready - " <<
78 op->errorName() << '-' << op->errorMessage();
79 QCoreApplication::exit(1);
80 return;
81 }
82
83 qDebug() << "Account ready";
84 connect(mAccount.data(),
85 SIGNAL(connectionChanged(Tp::ConnectionPtr)),
86 SLOT(onAccountConnectionChanged(Tp::ConnectionPtr)));
87
88 if (mAccount->connection().isNull()) {
89 qDebug() << "The account given has no Connection. Please set it online to continue.";
90 }
91
92 onAccountConnectionChanged(mAccount->connection());
93 }
94
onAccountConnectionChanged(const ConnectionPtr & conn)95 void RosterWindow::onAccountConnectionChanged(const ConnectionPtr &conn)
96 {
97 if (conn) {
98 mRoster->setConnection(conn);
99 } else {
100 mRoster->unsetConnection();
101 }
102 }
103