File indexing completed on 2023-11-26 08:16:46

0001 /*
0002  * Contact Chooser Dialog
0003  *
0004  * Copyright (C) 2011 David Edmundson <kde@davidedmundson.co.uk>
0005  * Copyright (C) 2012 Daniele E. Domenichelli <daniele.domenichelli@gmail.com>
0006  *
0007  * This library is free software; you can redistribute it and/or
0008  * modify it under the terms of the GNU Lesser General Public
0009  * License as published by the Free Software Foundation; either
0010  * version 2.1 of the License, or (at your option) any later version.
0011  *
0012  * This library is distributed in the hope that it will be useful,
0013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0015  * Lesser General Public License for more details.
0016  *
0017  * You should have received a copy of the GNU Lesser General Public
0018  * License along with this library; if not, write to the Free Software
0019  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
0020  */
0021 
0022 
0023 #include "contact-grid-dialog.h"
0024 
0025 #include <KLocalizedString>
0026 
0027 #include <QDialogButtonBox>
0028 #include <QPushButton>
0029 #include <QLineEdit>
0030 #include <QVBoxLayout>
0031 
0032 #include <TelepathyQt/AccountManager>
0033 #include <TelepathyQt/AccountFactory>
0034 #include <TelepathyQt/PendingReady>
0035 #include <TelepathyQt/PendingChannelRequest>
0036 
0037 #include <KTp/debug.h>
0038 #include <KTp/Models/contacts-list-model.h>
0039 #include <KTp/Models/contacts-filter-model.h>
0040 #include <KTp/Widgets/contact-grid-widget.h>
0041 #include <KTp/contact-factory.h>
0042 
0043 class KTp::ContactGridDialog::Private
0044 {
0045 public:
0046     Private(KTp::ContactGridDialog *parent) :
0047         q(parent),
0048         contactsModel(nullptr)
0049     {
0050     }
0051 
0052     KTp::ContactGridDialog * const q;
0053 
0054     Tp::AccountManagerPtr accountManager;
0055     KTp::ContactsListModel *contactsModel;
0056     KTp::ContactGridWidget *contactGridWidget;
0057     QDialogButtonBox *buttonBox;
0058 
0059 public:
0060     void _k_onAccountManagerReady();
0061     void _k_onSelectionChanged();
0062 };
0063 
0064 void KTp::ContactGridDialog::Private::_k_onAccountManagerReady()
0065 {
0066     contactsModel->setAccountManager(accountManager);
0067 }
0068 
0069 void KTp::ContactGridDialog::Private::_k_onSelectionChanged()
0070 {
0071     buttonBox->button(QDialogButtonBox::Ok)->setEnabled(contactGridWidget->hasSelection());
0072 }
0073 
0074 
0075 KTp::ContactGridDialog::ContactGridDialog(QWidget *parent) :
0076     QDialog(parent),
0077     d(new Private(this))
0078 {
0079     resize(500,450);
0080 
0081     Tp::AccountFactoryPtr  accountFactory = Tp::AccountFactory::create(QDBusConnection::sessionBus(),
0082                                                                        Tp::Features() << Tp::Account::FeatureCore
0083                                                                                       << Tp::Account::FeatureAvatar
0084                                                                                       << Tp::Account::FeatureProtocolInfo
0085                                                                                       << Tp::Account::FeatureProfile
0086                                                                                       << Tp::Account::FeatureCapabilities);
0087 
0088     Tp::ConnectionFactoryPtr connectionFactory = Tp::ConnectionFactory::create(QDBusConnection::sessionBus(),
0089                                                                                Tp::Features() << Tp::Connection::FeatureCore
0090                                                                                               << Tp::Connection::FeatureRosterGroups
0091                                                                                               << Tp::Connection::FeatureRoster
0092                                                                                               << Tp::Connection::FeatureSelfContact);
0093 
0094     Tp::ContactFactoryPtr contactFactory = KTp::ContactFactory::create(Tp::Features() << Tp::Contact::FeatureAlias
0095                                                                                       << Tp::Contact::FeatureAvatarData
0096                                                                                       << Tp::Contact::FeatureSimplePresence
0097                                                                                       << Tp::Contact::FeatureCapabilities);
0098 
0099     Tp::ChannelFactoryPtr channelFactory = Tp::ChannelFactory::create(QDBusConnection::sessionBus());
0100 
0101     d->accountManager = Tp::AccountManager::create(QDBusConnection::sessionBus(),
0102                                                    accountFactory,
0103                                                    connectionFactory,
0104                                                    channelFactory,
0105                                                    contactFactory);
0106 
0107     d->contactsModel = new KTp::ContactsListModel(this);
0108     connect(d->accountManager->becomeReady(), SIGNAL(finished(Tp::PendingOperation*)), SLOT(_k_onAccountManagerReady()));
0109 
0110 
0111     d->contactGridWidget = new KTp::ContactGridWidget(d->contactsModel, this);
0112     d->contactGridWidget->contactFilterLineEdit()->setPlaceholderText(i18n("Search in Contacts..."));
0113     d->contactGridWidget->filter()->setPresenceTypeFilterFlags(KTp::ContactsFilterModel::ShowOnlyConnected);
0114 
0115     d->buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this);
0116     connect(d->buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
0117     connect(d->buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
0118 
0119 
0120     QVBoxLayout *mainLayout = new QVBoxLayout(this);
0121     mainLayout->addWidget(d->contactGridWidget);
0122     mainLayout->addWidget(d->buttonBox);
0123 
0124     setLayout(mainLayout);
0125 
0126     connect(d->contactGridWidget,
0127             SIGNAL(contactDoubleClicked(Tp::AccountPtr,KTp::ContactPtr)),
0128             SLOT(accept()));
0129     connect(d->contactGridWidget,
0130             SIGNAL(selectionChanged(Tp::AccountPtr,KTp::ContactPtr)),
0131             SLOT(_k_onSelectionChanged()));
0132 
0133     d->_k_onSelectionChanged();
0134 
0135     connect(this, SIGNAL(rejected()), SLOT(close()));
0136 }
0137 
0138 KTp::ContactGridDialog::~ContactGridDialog()
0139 {
0140     delete d;
0141 }
0142 
0143 Tp::AccountPtr KTp::ContactGridDialog::account()
0144 {
0145     return d->contactGridWidget->selectedAccount();
0146 }
0147 
0148 Tp::ContactPtr KTp::ContactGridDialog::contact()
0149 {
0150     return d->contactGridWidget->selectedContact();
0151 }
0152 
0153 KTp::ContactsFilterModel* KTp::ContactGridDialog::filter() const
0154 {
0155     return d->contactGridWidget->filter();
0156 }
0157 
0158 
0159 #include "moc_contact-grid-dialog.cpp"