File indexing completed on 2023-05-30 11:40:08

0001 /*
0002  * Copyright (C) 2012 Rohan Garg <rohangarg@kubuntu.org>
0003  *
0004  * This library is free software; you can redistribute it and/or
0005  * modify it under the terms of the GNU Lesser General Public
0006  * License as published by the Free Software Foundation; either
0007  * version 2.1 of the License, or (at your option) any later version.
0008  *
0009  * This library is distributed in the hope that it will be useful,
0010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012  * Lesser General Public License for more details.
0013  *
0014  * You should have received a copy of the GNU Lesser General Public
0015  * License along with this library; if not, write to the Free Software
0016  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
0017  */
0018 
0019 #include "conference-auth-op.h"
0020 #include "x-telepathy-password-auth-operation.h"
0021 
0022 #include <TelepathyQt/PendingVariantMap>
0023 
0024 #include <QDebug>
0025 
0026 #include <KLocalizedString>
0027 #include <KPasswordDialog>
0028 
0029 #include <KTp/wallet-interface.h>
0030 #include <KTp/pending-wallet.h>
0031 
0032 ConferenceAuthOp::ConferenceAuthOp(const Tp::AccountPtr &account,
0033         const Tp::ChannelPtr &channel)
0034     : Tp::PendingOperation(channel),
0035       m_walletInterface(0),
0036       m_account(account),
0037       m_channel(channel),
0038       m_passwordIface(channel->interface<Tp::Client::ChannelInterfacePasswordInterface>())
0039 {
0040     connect(KTp::WalletInterface::openWallet(), SIGNAL(finished(Tp::PendingOperation*)), SLOT(onOpenWalletOperationFinished(Tp::PendingOperation*)));
0041 }
0042 
0043 ConferenceAuthOp::~ConferenceAuthOp()
0044 {
0045 }
0046 
0047 void ConferenceAuthOp::onOpenWalletOperationFinished(Tp::PendingOperation *op)
0048 {
0049     KTp::PendingWallet *walletOp = qobject_cast<KTp::PendingWallet*>(op);
0050     Q_ASSERT(walletOp);
0051 
0052     m_walletInterface = walletOp->walletInterface();
0053 
0054     qDebug() << "Wallet is open :" << m_walletInterface->isOpen();
0055 
0056     QDBusPendingReply<uint> reply = m_passwordIface->GetPasswordFlags();
0057     QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(reply, this);
0058     connect(watcher, SIGNAL(finished(QDBusPendingCallWatcher*)),
0059             SLOT(passwordFlagOperationFinished(QDBusPendingCallWatcher*)));
0060 
0061 }
0062 
0063 void ConferenceAuthOp::passwordFlagOperationFinished(QDBusPendingCallWatcher *watcher)
0064 {
0065     QDBusPendingReply<uint> reply = *watcher;
0066     if (reply.isError()) {
0067         qWarning() << "Reply is a error. ABORT!";
0068         return;
0069     }
0070 
0071 
0072     if (reply.argumentAt<0>() == Tp::ChannelPasswordFlagProvide) {
0073         if (m_walletInterface->hasEntry(m_account, m_channel->targetId())) {
0074             providePassword(m_walletInterface->entry(m_account, m_channel->targetId()));
0075         } else {
0076             passwordDialog();
0077         }
0078     }
0079 }
0080 
0081 void ConferenceAuthOp::providePassword(const QString &password)
0082 {
0083     m_password = password;
0084     QDBusPendingReply<bool> reply = m_passwordIface->ProvidePassword(m_password);
0085     QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(reply, this);
0086     connect (watcher, SIGNAL(finished(QDBusPendingCallWatcher*)),
0087              SLOT(onPasswordProvided(QDBusPendingCallWatcher*)));
0088 }
0089 
0090 void ConferenceAuthOp::passwordDialog()
0091 {
0092     KPasswordDialog *passwordDialog = new KPasswordDialog;
0093     passwordDialog->setAttribute(Qt::WA_DeleteOnClose);
0094     passwordDialog->setPrompt(i18n("Please provide a password for the chat room %1", m_channel->targetId()));
0095     passwordDialog->show();
0096 
0097     connect(passwordDialog, SIGNAL(gotPassword(QString,bool)),
0098             SLOT(providePassword(QString)));
0099 
0100 }
0101 
0102 void ConferenceAuthOp::onPasswordProvided(QDBusPendingCallWatcher *watcher)
0103 {
0104     QDBusPendingReply<bool> reply = *watcher;
0105     if (!reply.isValid() || reply.count() < 1) {
0106         return;
0107     }
0108 
0109     if (reply.argumentAt<0>()) {
0110         m_walletInterface->setEntry(m_account,m_channel->targetId(), m_password);
0111         setFinished();
0112     } else {
0113         qDebug() << "Password was incorrect, enter again";
0114         passwordDialog();
0115     }
0116 }