File indexing completed on 2023-11-26 04:55:35
0001 /* 0002 * This file is part of telepathy-accounts-kcm 0003 * 0004 * Copyright (C) 2009 Collabora Ltd. <info@collabora.com> 0005 * Copyright (C) 2011 Dominik Schmidt <kde@dominik-schmidt.de> 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 #include "edit-account-dialog.h" 0023 0024 #include <KTp/wallet-interface.h> 0025 #include <KTp/pending-wallet.h> 0026 #include <KTp/wallet-utils.h> 0027 0028 #include "KCMTelepathyAccounts/dictionary.h" 0029 #include "KCMTelepathyAccounts/abstract-account-parameters-widget.h" 0030 #include "KCMTelepathyAccounts/abstract-account-ui.h" 0031 #include "KCMTelepathyAccounts/account-edit-widget.h" 0032 #include "KCMTelepathyAccounts/parameter-edit-widget.h" 0033 #include "KCMTelepathyAccounts/parameter-edit-model.h" 0034 #include "KCMTelepathyAccounts/plugin-manager.h" 0035 0036 #include <KDebug> 0037 #include <KLocale> 0038 #include <KTabWidget> 0039 0040 #include <QtCore/QList> 0041 0042 #include <TelepathyQt/Account> 0043 #include <TelepathyQt/ConnectionManager> 0044 #include <TelepathyQt/PendingStringList> 0045 0046 class EditAccountDialog::Private 0047 { 0048 public: 0049 Private() 0050 : widget(0), reconnectRequired(false), kwalletReady(false) 0051 { 0052 } 0053 0054 Tp::AccountPtr account; 0055 AccountEditWidget *widget; 0056 bool reconnectRequired; 0057 bool kwalletReady; 0058 }; 0059 0060 EditAccountDialog::EditAccountDialog(const Tp::AccountPtr &account, QWidget *parent) 0061 : KDialog(parent), 0062 d(new Private) 0063 { 0064 d->account = account; 0065 0066 connect(KTp::WalletInterface::openWallet(), SIGNAL(finished(Tp::PendingOperation*)), SLOT(onWalletOpened(Tp::PendingOperation*))); 0067 0068 setMinimumWidth(400); 0069 } 0070 0071 EditAccountDialog::~EditAccountDialog() 0072 { 0073 delete d; 0074 } 0075 0076 void EditAccountDialog::onWalletOpened(Tp::PendingOperation *op) 0077 { 0078 KTp::PendingWallet *walletOp = qobject_cast<KTp::PendingWallet*>(op); 0079 Q_ASSERT(walletOp); 0080 0081 KTp::WalletInterface *walletInterface = walletOp->walletInterface(); 0082 0083 // Get the protocol's parameters and values. 0084 Tp::ProtocolInfo protocolInfo = d->account->protocolInfo(); 0085 Tp::ProtocolParameterList parameters = protocolInfo.parameters(); 0086 QVariantMap parameterValues = d->account->parameters(); 0087 0088 // Add the parameters to the model. 0089 ParameterEditModel *parameterModel = new ParameterEditModel(this); 0090 parameterModel->addItems(parameters, d->account->profile()->parameters(), parameterValues); 0091 0092 //update the parameter model with the password from kwallet (if applicable) 0093 Tp::ProtocolParameter passwordParameter = parameterModel->parameter(QLatin1String("password")); 0094 0095 if (passwordParameter.isValid() && walletInterface->hasPassword(d->account)) { 0096 QModelIndex index = parameterModel->indexForParameter(passwordParameter); 0097 QString password = walletInterface->password(d->account); 0098 parameterModel->setData(index, password, Qt::EditRole); 0099 } 0100 0101 // Set up the interface 0102 d->widget = new AccountEditWidget(d->account->profile(), 0103 d->account->displayName(), 0104 parameterModel, 0105 doNotConnectOnAdd, 0106 this); 0107 0108 setMainWidget(d->widget); 0109 0110 d->kwalletReady = true; 0111 show(); 0112 } 0113 0114 void EditAccountDialog::accept() 0115 { 0116 if (!d->widget) { 0117 kWarning() << "missing d->widget, not saving parameters"; 0118 return; 0119 } 0120 0121 QVariantMap setParameters = d->widget->parametersSet(); 0122 QStringList unsetParameters = d->widget->parametersUnset(); 0123 0124 // Check all pages of parameters pass validation. 0125 if (!d->widget->validateParameterValues()) { 0126 kDebug() << "A widget failed parameter validation. Not accepting wizard."; 0127 return; 0128 } 0129 0130 //remove password from setParameters as this is now stored by kwallet instead 0131 setParameters.remove(QLatin1String("password")); 0132 unsetParameters.append(QLatin1String("password")); //remove the password from mission control 0133 0134 Tp::PendingStringList *psl = d->account->updateParameters(setParameters, unsetParameters); 0135 0136 kDebug() << "Set parameters:" << setParameters; 0137 kDebug() << "Unset parameters:" << unsetParameters; 0138 0139 connect(psl, 0140 SIGNAL(finished(Tp::PendingOperation*)), 0141 SLOT(onParametersUpdated(Tp::PendingOperation*))); 0142 } 0143 0144 void EditAccountDialog::onParametersUpdated(Tp::PendingOperation *op) 0145 { 0146 if (op->isError()) { 0147 // FIXME: Visual feedback in GUI to user. 0148 kWarning() << "Could not update parameters:" << op->errorName() << op->errorMessage(); 0149 return; 0150 } 0151 0152 Tp::PendingStringList *psl = qobject_cast<Tp::PendingStringList*>(op); 0153 0154 Q_ASSERT(psl); 0155 if (!psl) { 0156 kWarning() << "Something weird happened"; 0157 } 0158 0159 if (psl->result().size() > 0) { 0160 kDebug() << "The following parameters won't be updated until reconnection: " << psl->result(); 0161 d->reconnectRequired = true; 0162 } 0163 0164 QVariantMap values = d->widget->parametersSet(); 0165 0166 if (values.contains(QLatin1String("password"))) { 0167 KTp::WalletUtils::setAccountPassword(d->account, values[QLatin1String("password")].toString()); 0168 } else { 0169 KTp::WalletUtils::setAccountPassword(d->account, QString()); 0170 } 0171 0172 if(d->widget->updateDisplayName()) { 0173 connect(d->account->setDisplayName(d->widget->displayName()), 0174 SIGNAL(finished(Tp::PendingOperation*)), 0175 SLOT(onDisplayNameUpdated(Tp::PendingOperation*))); 0176 } else { 0177 onFinished(); 0178 } 0179 } 0180 0181 void EditAccountDialog::onDisplayNameUpdated(Tp::PendingOperation *op) 0182 { 0183 if (op->isError()) { 0184 // FIXME: Visual feedback in GUI to user. 0185 kWarning() << "Could not update display name:" << op->errorName() << op->errorMessage(); 0186 return; 0187 } 0188 onFinished(); 0189 } 0190 0191 void EditAccountDialog::onFinished() 0192 { 0193 Q_EMIT finished(); 0194 0195 if (d->reconnectRequired) { 0196 d->account->reconnect(); 0197 } 0198 0199 // set the dialog as accepted and exit 0200 done(KDialog::Accepted); 0201 } 0202 0203 void EditAccountDialog::setVisible(bool visible) 0204 { 0205 if (visible && d->kwalletReady) { 0206 KDialog::setVisible(visible); 0207 return; 0208 } 0209 0210 KDialog::setVisible(false); 0211 } 0212 0213 #include "edit-account-dialog.moc"