File indexing completed on 2022-11-23 11:29:38

0001 /*
0002   Copyright © 2011 Rohan Garg <rohan16garg@gmail.com>
0003 
0004   This program is free software; you can redistribute it and/or
0005   modify it under the terms of the GNU General Public License as
0006   published by the Free Software Foundation; either version 2 of
0007   the License or (at your option) version 3 or any later version
0008   accepted by the membership of KDE e.V. (or its successor approved
0009   by the membership of KDE e.V.), which shall act as a proxy
0010   defined in Section 14 of version 3 of the license.
0011 
0012   This program 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
0015   GNU General Public License for more details.
0016 
0017   You should have received a copy of the GNU General Public License
0018   along with this program.  If not, see <http://www.gnu.org/licenses/>.
0019 */
0020 
0021 #include "custom-presence-dialog.h"
0022 #include "global-presence-chooser.h"
0023 
0024 #include <QListView>
0025 #include <QHBoxLayout>
0026 #include <QVBoxLayout>
0027 #include <QPushButton>
0028 #include <QSortFilterProxyModel>
0029 #include <QModelIndex>
0030 #include <QDialogButtonBox>
0031 #include <QComboBox>
0032 
0033 #include <KLocalizedString>
0034 #include <KConfig>
0035 #include <KSharedConfig>
0036 
0037 #include <TelepathyQt/Presence>
0038 #include <QLineEdit>
0039 #include <QKeyEvent>
0040 
0041 #include <KTp/Models/presence-model.h>
0042 
0043 class FilteredModel : public QSortFilterProxyModel
0044 {
0045     Q_OBJECT
0046 public:
0047     FilteredModel(QObject *parent);
0048     bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const;
0049 };
0050 
0051 FilteredModel::FilteredModel(QObject *parent)
0052     : QSortFilterProxyModel(parent)
0053 {
0054 }
0055 
0056 bool FilteredModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
0057 {
0058     QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent);
0059     KTp::Presence presence = index.data(KTp::PresenceModel::PresenceRole).value<KTp::Presence>();
0060     return ! presence.statusMessage().isEmpty();
0061 }
0062 
0063 CustomPresenceDialog::CustomPresenceDialog(KTp::PresenceModel *model, QWidget *parent)
0064     : QDialog(parent),
0065       m_model(model)
0066 {
0067     setupDialog();
0068 }
0069 
0070 void CustomPresenceDialog::setupDialog()
0071 {
0072     setWindowTitle(i18n("Edit Custom Presences"));
0073 
0074     QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Close, this);
0075     connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
0076 
0077     FilteredModel *filteredModel = new FilteredModel(this);
0078     filteredModel->setSourceModel(m_model);
0079 
0080     m_listView = new QListView(this);
0081     m_listView->setModel(filteredModel);
0082 
0083     connect(m_listView, SIGNAL(clicked(QModelIndex)),
0084             this, SLOT(presenceViewSelectionChanged(QModelIndex)));
0085 
0086     connect(m_listView, SIGNAL(activated(QModelIndex)),
0087             this, SLOT(presenceViewSelectionChanged(QModelIndex)));
0088 
0089     m_statusMessage = new QComboBox(this);
0090     m_statusMessage->setEditable(true);
0091 
0092     m_statusMessage->addItem(QIcon::fromTheme("user-online"), i18n("Set custom available message..."), qVariantFromValue<KTp::Presence>(Tp::Presence::available()));
0093     m_statusMessage->addItem(QIcon::fromTheme("user-busy"), i18n("Set custom busy message..."), qVariantFromValue<KTp::Presence>(Tp::Presence::busy()));
0094     m_statusMessage->addItem(QIcon::fromTheme("user-away"), i18n("Set custom away message..."), qVariantFromValue<KTp::Presence>(Tp::Presence::away()));
0095     m_statusMessage->addItem(QIcon::fromTheme("user-away-extended"), i18n("Set custom extended away message..."), qVariantFromValue<KTp::Presence>(Tp::Presence::xa()));
0096 
0097     m_statusMessage->setAutoCompletion(false);
0098     m_statusMessage->show();
0099 
0100     m_statusMessage->lineEdit()->setPlaceholderText(m_statusMessage->currentText());
0101     m_statusMessage->lineEdit()->setWhatsThis(KDED_STATUS_MESSAGE_PARSER_WHATSTHIS);
0102 
0103     connect(m_statusMessage, SIGNAL(editTextChanged(QString)),
0104             this, SLOT(presenceMessageTextChanged(QString)));
0105 
0106     m_addStatus = new QPushButton(QIcon::fromTheme("list-add"), i18n("Add Presence"), this);
0107     m_removeStatus = new QPushButton(QIcon::fromTheme("list-remove"), i18n("Remove Presence"), this);
0108     m_removeStatus->setEnabled(false);
0109 
0110     //this triggers the presenceMessageTextChanged() slot and disables the m_addStatus button
0111     m_statusMessage->lineEdit()->setText(QString());
0112 
0113     QVBoxLayout *vLayout = new QVBoxLayout();
0114     vLayout->addWidget(m_statusMessage);
0115 
0116     QHBoxLayout *hLayout = new QHBoxLayout();
0117     hLayout->addWidget(m_listView);
0118 
0119     QVBoxLayout *vLayout2 = new QVBoxLayout();
0120     vLayout2->addWidget(m_addStatus);
0121     vLayout2->addWidget(m_removeStatus);
0122     vLayout2->addStretch(1);
0123 
0124     hLayout->addLayout(vLayout2);
0125     vLayout->addLayout(hLayout);
0126 
0127     vLayout->addWidget(buttonBox);
0128     setLayout(vLayout);
0129 
0130     connect(m_addStatus, SIGNAL(clicked()), SLOT(addCustomPresence()));
0131     connect(m_removeStatus, SIGNAL(clicked()), SLOT(removeCustomPresence()));
0132     connect(m_statusMessage, SIGNAL(currentIndexChanged(QString)), SLOT(comboboxIndexChanged(QString)));
0133 
0134     m_statusMessage->installEventFilter(this);
0135 }
0136 
0137 void CustomPresenceDialog::addCustomPresence()
0138 {
0139     int presenceIndex = m_statusMessage->currentIndex();
0140     KTp::Presence presence = m_statusMessage->itemData(presenceIndex).value<KTp::Presence>();
0141     presence.setStatus(presence.type(), QString(), m_statusMessage->currentText());
0142 
0143     m_listView->setCurrentIndex(qobject_cast<FilteredModel*>(m_listView->model())->mapFromSource(m_model->addPresence(presence)));
0144     m_statusMessage->lineEdit()->clear();
0145     m_listView->setFocus();
0146     m_removeStatus->setEnabled(true);
0147 
0148     m_model->syncCustomPresencesToDisk();
0149 }
0150 
0151 void CustomPresenceDialog::removeCustomPresence()
0152 {
0153     if (! m_listView->currentIndex().isValid()) {
0154         return;
0155     }
0156 
0157     KTp::Presence presence = m_listView->currentIndex().data(KTp::PresenceModel::PresenceRole).value<KTp::Presence>();
0158     m_model->removePresence(presence);
0159 
0160     if (m_listView->model()->rowCount(QModelIndex()) == 0) {
0161         m_removeStatus->setEnabled(false);
0162     }
0163 
0164     m_model->syncCustomPresencesToDisk();
0165 }
0166 
0167 void CustomPresenceDialog::comboboxIndexChanged(const QString& text)
0168 {
0169     m_statusMessage->lineEdit()->setText(QString());
0170     m_statusMessage->lineEdit()->setPlaceholderText(text);
0171 }
0172 
0173 bool CustomPresenceDialog::eventFilter(QObject* obj, QEvent* event)
0174 {
0175     if (obj == m_statusMessage && event->type() == QEvent::KeyPress) {
0176         QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
0177         if (keyEvent->modifiers() == Qt::NoModifier && (keyEvent->key() == Qt::Key_Return || keyEvent->key() == Qt::Key_Enter)) {
0178             addCustomPresence();
0179             m_statusMessage->lineEdit()->clear();
0180             m_listView->setFocus();
0181             return true;
0182         } else {
0183             return false;
0184         }
0185     } else {
0186         // standard event processing
0187         return QObject::eventFilter(obj, event);
0188     }
0189 }
0190 
0191 void CustomPresenceDialog::presenceMessageTextChanged(const QString& text)
0192 {
0193     if (text.isEmpty()) {
0194         m_addStatus->setEnabled(false);
0195     } else {
0196         m_addStatus->setEnabled(true);
0197     }
0198 }
0199 
0200 void CustomPresenceDialog::presenceViewSelectionChanged(const QModelIndex& index)
0201 {
0202     if (index.isValid()) {
0203         m_removeStatus->setEnabled(true);
0204     } else {
0205         m_removeStatus->setEnabled(false);
0206     }
0207 
0208     KTp::Presence presence = index.data(KTp::PresenceModel::PresenceRole).value<KTp::Presence>();
0209     m_statusMessage->lineEdit()->setText(presence.statusMessage());
0210 }
0211 
0212 #include "custom-presence-dialog.moc"
0213 #include "moc_custom-presence-dialog.cpp" //hack because we have two QObejcts in teh same file