File indexing completed on 2023-12-03 08:28:38

0001 /*
0002  * Start chat dialog
0003  *
0004  * Copyright (C) 2013 Anant Kamath <kamathanant@gmail.com>
0005  *
0006  * This library is free software; you can redistribute it and/or
0007  * modify it under the terms of the GNU Lesser General Public
0008  * License as published by the Free Software Foundation; either
0009  * version 2.1 of the License, or (at your option) any later version.
0010  *
0011  * This library is distributed in the hope that it will be useful,
0012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0014  * Lesser General Public License for more details.
0015  *
0016  * You should have received a copy of the GNU Lesser General Public
0017  * License along with this library; if not, write to the Free Software
0018  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
0019  */
0020 
0021 #include "start-chat-dialog.h"
0022 #include "ui_start-chat-dialog.h"
0023 
0024 #include <QObject>
0025 #include <QCloseEvent>
0026 #include <QDialogButtonBox>
0027 #include <QPushButton>
0028 #include <QDebug>
0029 
0030 #include <KMessageBox>
0031 
0032 #include <TelepathyQt/AccountManager>
0033 #include <TelepathyQt/Account>
0034 #include <TelepathyQt/Connection>
0035 #include <TelepathyQt/ContactManager>
0036 #include <TelepathyQt/PendingChannelRequest>
0037 #include <TelepathyQt/PendingContacts>
0038 #include <TelepathyQt/AccountSet>
0039 
0040 #include <KTp/actions.h>
0041 #include <KTp/contact.h>
0042 #include <KLocalizedString>
0043 
0044 namespace KTp {
0045 
0046 struct KTPCOMMONINTERNALS_NO_EXPORT StartChatDialog::Private
0047 {
0048     Private() :
0049         ui(new Ui::StartChatDialog),
0050         acceptInProgress(false)
0051     {}
0052 
0053     Ui::StartChatDialog *ui;
0054     bool acceptInProgress;
0055     QPointer<Tp::PendingContacts> pendingContact;
0056     QDialogButtonBox *buttonBox;
0057 };
0058 
0059 StartChatDialog::StartChatDialog(const Tp::AccountManagerPtr &accountManager, QWidget *parent) :
0060     QDialog(parent),
0061     d(new Private)
0062 {
0063     setWindowTitle(i18n("Start a chat"));
0064     setWindowIcon(QIcon::fromTheme(QLatin1String("telepathy-kde")));
0065 
0066     d->buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
0067 
0068     connect(d->buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
0069     connect(d->buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
0070 
0071     QWidget *widget = new QWidget(this);
0072     d->ui->setupUi(widget);
0073 
0074     QVBoxLayout *mainLayout = new QVBoxLayout(this);
0075     mainLayout->addWidget(widget);
0076     mainLayout->addWidget(d->buttonBox);
0077     setLayout(mainLayout);
0078 
0079     d->ui->accountCombo->setAccountSet(accountManager->onlineAccounts());
0080 
0081     d->ui->screenNameLineEdit->setFocus();
0082 }
0083 
0084 StartChatDialog::~StartChatDialog()
0085 {
0086     delete d->ui;
0087     delete d;
0088 }
0089 
0090 void StartChatDialog::accept()
0091 {
0092     Tp::AccountPtr account = d->ui->accountCombo->currentAccount();
0093     const QString contactIdentifier = d->ui->screenNameLineEdit->text();
0094     if (account.isNull()) {
0095         KMessageBox::sorry(this, i18n("No account selected."));
0096     } else if (account->connection().isNull()) {
0097         KMessageBox::sorry(this, i18n("The requested account has been disconnected "
0098                                       "and so a chat could not be initiated."));
0099     } else if (contactIdentifier.isEmpty()) {
0100         KMessageBox::sorry(this, i18n("You did not specify the name of the contact to start a chat with."));
0101     } else {
0102         d->pendingContact = account->connection()->contactManager()->contactsForIdentifiers(
0103             QStringList() << contactIdentifier, Tp::Contact::FeatureCapabilities);
0104 
0105         connect(d->pendingContact, SIGNAL(finished(Tp::PendingOperation*)),
0106                 SLOT(_k_onPendingContactFinished(Tp::PendingOperation*)));
0107 
0108         setInProgress(true);
0109     }
0110 }
0111 
0112 void StartChatDialog::_k_onPendingContactFinished(Tp::PendingOperation *op)
0113 {
0114     Tp::PendingContacts *pc = qobject_cast<Tp::PendingContacts*>(op);
0115     Q_ASSERT(pc);
0116 
0117     if (pc->isError()) {
0118         KMessageBox::sorry(this, i18n("The contact Screen Name you provided is invalid or does not accept text chats."));
0119         return;
0120     }
0121 
0122     if (pc == d->pendingContact && !pc->isError() && pc->contacts().size() > 0) {
0123         KTp::ContactPtr currentContact = KTp::ContactPtr::qObjectCast(pc->contacts().at(0));
0124 
0125         Tp::PendingChannelRequest *op = KTp::Actions::startChat(d->ui->accountCombo->currentAccount(), currentContact, true);
0126         connect(op, SIGNAL(finished(Tp::PendingOperation*)),
0127                 SLOT(_k_onStartChatFinished(Tp::PendingOperation*)));
0128     }
0129 }
0130 
0131 void StartChatDialog::closeEvent(QCloseEvent *e)
0132 {
0133     // ignore close event if we are in the middle of an operation
0134     if (!d->acceptInProgress) {
0135         QDialog::closeEvent(e);
0136     }
0137 }
0138 
0139 void StartChatDialog::_k_onStartChatFinished(Tp::PendingOperation *op)
0140 {
0141     if (op->isError()) {
0142         qWarning() << "Failed to start a text channel with the contact for the given identifier"
0143                    << op->errorName() << op->errorMessage();
0144         KMessageBox::sorry(this, i18n("Failed to start a chat with the contact."));
0145         setInProgress(false);
0146     } else {
0147         QDialog::accept();
0148     }
0149 }
0150 
0151 void StartChatDialog::setInProgress(bool inProgress)
0152 {
0153     d->acceptInProgress = inProgress;
0154     layout()->itemAt(0)->widget()->setEnabled(!inProgress);
0155     d->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(!inProgress);
0156     d->buttonBox->button(QDialogButtonBox::Cancel)->setEnabled(!inProgress);
0157 }
0158 
0159 } //namespace KTp