File indexing completed on 2023-11-26 04:55:36

0001 /*
0002  * This file is part of telepathy-accounts-kcm
0003  *
0004  * Copyright (C) 2012 Daniele E. Domenichelli <daniele.domenichelli@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 "edit-display-name-button.h"
0022 
0023 #include <QtGui/QHBoxLayout>
0024 #include <QtGui/QVBoxLayout>
0025 #include <QtGui/QLabel>
0026 
0027 #include <KDE/KDialog>
0028 #include <KDE/KLineEdit>
0029 #include <KDE/KLocalizedString>
0030 #include <KDE/KDebug>
0031 
0032 #include <TelepathyQt/Account>
0033 #include <TelepathyQt/PendingOperation>
0034 
0035 
0036 class EditDisplayNameDialog : public KDialog
0037 {
0038     Q_OBJECT
0039     Q_DISABLE_COPY(EditDisplayNameDialog)
0040 
0041 public:
0042     EditDisplayNameDialog(const Tp::AccountPtr &account,
0043                           QWidget* parent = 0,
0044                           Qt::WFlags flags = 0);
0045     virtual ~EditDisplayNameDialog();
0046 
0047     QString displayName() const;
0048 
0049 private:
0050     Tp::AccountPtr m_account;
0051     KLineEdit *m_displayNameLineEdit;
0052 };
0053 
0054 
0055 EditDisplayNameDialog::EditDisplayNameDialog(const Tp::AccountPtr &account,
0056                                              QWidget* parent,
0057                                              Qt::WFlags flags)
0058     : KDialog(parent, flags),
0059       m_account(account)
0060 {
0061     setCaption(i18n("Edit Display Name"));
0062     setButtons( KDialog::Ok | KDialog::Cancel );
0063     setWindowIcon(KIcon(QLatin1String("telepathy-kde")));
0064     setFixedSize(400, 150);
0065 
0066     QWidget * mainWidget = new QWidget(this);
0067     QVBoxLayout *mainLayout = new QVBoxLayout();
0068     QHBoxLayout *topLayout = new QHBoxLayout();
0069 
0070     QLabel *topLabel = new QLabel(i18n("Choose a new display name for your account"), this);
0071     topLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);
0072     topLabel->setWordWrap(true);
0073     QFont font = topLabel->font();
0074     font.setBold(true);
0075     topLabel->setFont(font);
0076 
0077     QLabel *bottomLabel = new QLabel(i18n("A display name is your local alias for the account, only you will see it."), this);
0078     bottomLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);
0079     bottomLabel->setWordWrap(true);
0080 
0081     QLabel *icon = new QLabel;
0082     icon->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
0083     icon->setPixmap(KIcon(account->iconName()).pixmap(KIconLoader::SizeMedium));
0084 
0085     m_displayNameLineEdit = new KLineEdit(account->displayName(), this);
0086     m_displayNameLineEdit->setToolTip(i18n("New display name"));
0087     m_displayNameLineEdit->setWhatsThis(i18n("A display name is your local alias for the account, only you will see it."));
0088 
0089 
0090     topLayout->addWidget(topLabel);
0091     topLayout->addWidget(icon);
0092 
0093     mainLayout->addLayout(topLayout);
0094     mainLayout->addStretch();
0095 
0096     mainLayout->addWidget(m_displayNameLineEdit);
0097     mainLayout->addWidget(bottomLabel);
0098     mainLayout->addStretch();
0099 
0100     mainWidget->setLayout(mainLayout);
0101     setMainWidget(mainWidget);
0102 }
0103 
0104 EditDisplayNameDialog::~EditDisplayNameDialog()
0105 {
0106 }
0107 
0108 
0109 QString EditDisplayNameDialog::displayName() const
0110 {
0111     return m_displayNameLineEdit->text();
0112 }
0113 
0114 
0115 
0116 EditDisplayNameButton::EditDisplayNameButton(QWidget* parent) :
0117     KPushButton(parent)
0118 {
0119     connect(this,
0120             SIGNAL(clicked(bool)),
0121             SLOT(onClicked()));
0122 }
0123 
0124 EditDisplayNameButton::~EditDisplayNameButton()
0125 {
0126 }
0127 
0128 void EditDisplayNameButton::setAccount(const Tp::AccountPtr &account)
0129 {
0130     m_account = account;
0131 }
0132 
0133 Tp::AccountPtr EditDisplayNameButton::account() const
0134 {
0135     return m_account;
0136 }
0137 
0138 void EditDisplayNameButton::onClicked()
0139 {
0140     if (!m_account.isNull() && m_account->isValid()) {
0141         QWeakPointer<EditDisplayNameDialog> dialog = new EditDisplayNameDialog(m_account, this);
0142         dialog.data()->exec();
0143         if (!dialog.isNull()) {
0144             if (dialog.data()->result() == KDialog::Accepted && dialog.data()->displayName() != m_account->displayName()) {
0145                 kDebug() << "Setting display name" << dialog.data()->displayName() << "for account" << account()->uniqueIdentifier();
0146                 Tp::PendingOperation *op = m_account->setDisplayName(dialog.data()->displayName());
0147                 connect(op, SIGNAL(finished(Tp::PendingOperation*)), SLOT(onFinished(Tp::PendingOperation*)));
0148             }
0149             dialog.data()->deleteLater();
0150         }
0151     }
0152 }
0153 
0154 void EditDisplayNameButton::onFinished(Tp::PendingOperation* op)
0155 {
0156     if (op->isError()) {
0157         kWarning() << "Cannot set display name" << op->errorName() << op->errorMessage();
0158     }
0159 }
0160 
0161 
0162 #include "edit-display-name-button.moc"
0163 #include "moc_edit-display-name-button.cpp"