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

0001 /*
0002  * This file is part of KDE Telepathy Common Internals
0003  *
0004  * Copyright (C) 2012 Rohan Garg <rohangarg@kubuntu.org>
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 "notification-config-dialog.h"
0022 
0023 #include <QVBoxLayout>
0024 #include <QHBoxLayout>
0025 #include <QLabel>
0026 #include <QDialogButtonBox>
0027 #include <QPushButton>
0028 #include <QDBusInterface>
0029 #include <QComboBox>
0030 
0031 #include <TelepathyQt/Contact>
0032 
0033 #include <KNotifyConfigWidget>
0034 #include <KConfig>
0035 #include <KSharedConfig>
0036 #include <KConfigGroup>
0037 #include <KLocalizedString>
0038 
0039 KTp::NotificationConfigDialog::NotificationConfigDialog(const Tp::ContactPtr &contact, QWidget *parent)
0040     : QDialog(parent)
0041     , m_notifyWidget(new KNotifyConfigWidget(this))
0042 {
0043     Q_ASSERT(contact);
0044     m_contact = contact;
0045     m_currentSelection = 0;
0046     setWindowTitle(i18n("Configure notifications for %1", m_contact.data()->alias()));
0047     setAttribute(Qt::WA_DeleteOnClose);
0048 
0049     m_buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Apply | QDialogButtonBox::Cancel | QDialogButtonBox::RestoreDefaults, this);
0050     m_buttonBox->button(QDialogButtonBox::Apply)->setDisabled(true);
0051 
0052     QWidget *centralWidget = new QWidget(this);
0053     QVBoxLayout *vboxLayout = new QVBoxLayout(centralWidget);
0054     QHBoxLayout *hboxLayout = new QHBoxLayout(centralWidget);
0055     QLabel *label = new QLabel(i18n("Configure notifications for"), centralWidget);
0056     QComboBox *comboBox = new QComboBox(centralWidget);
0057 
0058     comboBox->setEditable(false);
0059     comboBox->addItem(m_contact.data()->alias());
0060     comboBox->addItem(i18n("All users"));
0061     hboxLayout->addWidget(label);
0062     hboxLayout->addWidget(comboBox);
0063     vboxLayout->addLayout(hboxLayout);
0064     vboxLayout->addWidget(m_notifyWidget);
0065     centralWidget->setLayout(vboxLayout);
0066 
0067     QVBoxLayout *mainLayout = new QVBoxLayout(this);
0068     mainLayout->addWidget(centralWidget);
0069     mainLayout->addWidget(m_buttonBox);
0070     setLayout(mainLayout);
0071 
0072     m_notifyWidget->setApplication(QLatin1String("ktelepathy"),
0073                                    QLatin1String("contact"),
0074                                    m_contact.data()->id());
0075 
0076     connect(m_buttonBox, SIGNAL(clicked(QAbstractButton*)),
0077             this, SLOT(onButtonBoxClicked(QAbstractButton*)));
0078     connect(comboBox, SIGNAL(currentIndexChanged(int)),
0079             SLOT(updateNotifyWidget(int)));
0080 
0081     connect(m_notifyWidget, &KNotifyConfigWidget::changed, [=](bool changed) {
0082         m_buttonBox->button(QDialogButtonBox::Apply)->setEnabled(changed);
0083     });
0084 }
0085 
0086 KTp::NotificationConfigDialog::~NotificationConfigDialog()
0087 {
0088 }
0089 
0090 void KTp::NotificationConfigDialog::onButtonBoxClicked(QAbstractButton *button)
0091 {
0092     switch (m_buttonBox->standardButton(button)) {
0093         case QDialogButtonBox::Ok:
0094             onOkClicked();
0095             break;
0096         case QDialogButtonBox::Apply:
0097             m_notifyWidget->save();
0098             break;
0099         case QDialogButtonBox::RestoreDefaults:
0100             defaults();
0101             break;
0102         case QDialogButtonBox::Cancel:
0103             reject();
0104             break;
0105         default:
0106             break;
0107     }
0108 }
0109 
0110 void KTp::NotificationConfigDialog::updateNotifyWidget(const int selection)
0111 {
0112     if (selection == 0) {
0113         m_notifyWidget->setApplication(QLatin1String("ktelepathy"),
0114                                        QLatin1String("contact"),
0115                                        m_contact.data()->id());
0116         setWindowTitle(i18n("Configure notifications for %1", m_contact.data()->alias()));
0117     } else if (selection == 1) {
0118         m_notifyWidget->setApplication(QLatin1String("ktelepathy"));
0119         setWindowTitle(i18n("Configure notifications for all users"));
0120     }
0121 
0122     m_currentSelection = selection;
0123 }
0124 
0125 void KTp::NotificationConfigDialog::defaults()
0126 {
0127     KSharedConfigPtr config = KSharedConfig::openConfig(QLatin1String("ktelepathy.notifyrc"));
0128     KConfigGroup *configGroup;
0129 
0130     if (m_currentSelection == 0) {
0131         Q_FOREACH(const QString &group, config->groupList()) {
0132             if (group.endsWith(m_contact.data()->id())) {
0133                 configGroup = new KConfigGroup(config, group);
0134                 configGroup->deleteGroup();
0135                 delete configGroup;
0136             }
0137         }
0138     } else if (m_currentSelection == 1) {
0139         Q_FOREACH(const QString &group, config->groupList()) {
0140             if (group.startsWith(QLatin1String("Event"))) {
0141                 configGroup = new KConfigGroup(config, group);
0142                 configGroup->deleteGroup();
0143                 delete configGroup;
0144             }
0145         }
0146     }
0147     config->sync();
0148     updateNotifyWidget(m_currentSelection);
0149 }
0150 
0151 void KTp::NotificationConfigDialog::onOkClicked()
0152 {
0153     m_notifyWidget->save();
0154     accept();
0155 }