File indexing completed on 2025-01-26 05:07:43
0001 /* 0002 SPDX-FileCopyrightText: 2013 Sebastian Kügler <sebas@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "statusnotifiertest.h" 0008 #include "pumpjob.h" 0009 0010 #include <QDebug> 0011 #include <kservice.h> 0012 0013 #include <qcommandlineparser.h> 0014 0015 #include <KLocalizedString> 0016 #include <KStatusNotifierItem> 0017 0018 #include <QStringList> 0019 #include <QTimer> 0020 0021 #include <QMenu> 0022 #include <QPushButton> 0023 0024 static QTextStream cout(stdout); 0025 0026 class StatusNotifierTestPrivate 0027 { 0028 public: 0029 QString pluginName; 0030 QTimer *timer; 0031 int interval = 1500; 0032 QStringList loglines; 0033 0034 KStatusNotifierItem *systemNotifier; 0035 PumpJob *job; 0036 }; 0037 0038 StatusNotifierTest::StatusNotifierTest(QWidget *parent) 0039 : QDialog(parent) 0040 { 0041 d = new StatusNotifierTestPrivate; 0042 d->job = nullptr; 0043 0044 init(); 0045 0046 setupUi(this); 0047 connect(updateButton, &QPushButton::clicked, this, &StatusNotifierTest::updateNotifier); 0048 connect(jobEnabledCheck, &QCheckBox::toggled, this, &StatusNotifierTest::enableJob); 0049 updateUi(); 0050 iconName->setText(QStringLiteral("plasma")); 0051 show(); 0052 raise(); 0053 log(QStringLiteral("started")); 0054 } 0055 0056 void StatusNotifierTest::init() 0057 { 0058 QGuiApplication::setDesktopFileName(QStringLiteral("org.kde.statusnotifiertest")); 0059 d->systemNotifier = new KStatusNotifierItem(this); 0060 // d->systemNotifier->setCategory(KStatusNotifierItem::SystemServices); 0061 // d->systemNotifier->setCategory(KStatusNotifierItem::Hardware); 0062 d->systemNotifier->setCategory(KStatusNotifierItem::Communications); 0063 d->systemNotifier->setIconByName(QStringLiteral("plasma")); 0064 d->systemNotifier->setStatus(KStatusNotifierItem::Active); 0065 d->systemNotifier->setToolTipTitle(i18nc("tooltip title", "System Service Item")); 0066 d->systemNotifier->setTitle(i18nc("title", "StatusNotifierTest")); 0067 d->systemNotifier->setToolTipSubTitle(i18nc("tooltip subtitle", "Some explanation from the beach.")); 0068 0069 connect(d->systemNotifier, &KStatusNotifierItem::activateRequested, this, &StatusNotifierTest::activateRequested); 0070 connect(d->systemNotifier, &KStatusNotifierItem::secondaryActivateRequested, this, &StatusNotifierTest::secondaryActivateRequested); 0071 connect(d->systemNotifier, &KStatusNotifierItem::scrollRequested, this, &StatusNotifierTest::scrollRequested); 0072 0073 auto menu = new QMenu(this); 0074 menu->addAction(QIcon::fromTheme(QStringLiteral("document-edit")), QStringLiteral("action 1")); 0075 menu->addAction(QIcon::fromTheme(QStringLiteral("mail-send")), QStringLiteral("action 2")); 0076 auto subMenu = new QMenu(this); 0077 subMenu->setTitle(QStringLiteral("Sub Menu")); 0078 subMenu->addAction(QStringLiteral("subaction1")); 0079 subMenu->addAction(QStringLiteral("subaction2")); 0080 menu->addMenu(subMenu); 0081 0082 d->systemNotifier->setContextMenu(menu); 0083 } 0084 0085 StatusNotifierTest::~StatusNotifierTest() 0086 { 0087 delete d; 0088 } 0089 0090 void StatusNotifierTest::log(const QString &msg) 0091 { 0092 qDebug() << "msg: " << msg; 0093 d->loglines.prepend(msg); 0094 0095 logEdit->setText(d->loglines.join('\n')); 0096 } 0097 0098 void StatusNotifierTest::updateUi() 0099 { 0100 if (!d->systemNotifier) { 0101 return; 0102 } 0103 statusActive->setChecked(d->systemNotifier->status() == KStatusNotifierItem::Active); 0104 statusPassive->setChecked(d->systemNotifier->status() == KStatusNotifierItem::Passive); 0105 statusNeedsAttention->setChecked(d->systemNotifier->status() == KStatusNotifierItem::NeedsAttention); 0106 0107 statusActive->setEnabled(!statusAuto->isChecked()); 0108 statusPassive->setEnabled(!statusAuto->isChecked()); 0109 statusNeedsAttention->setEnabled(!statusAuto->isChecked()); 0110 0111 tooltipText->setText(d->systemNotifier->toolTipTitle()); 0112 tooltipSubtext->setText(d->systemNotifier->toolTipSubTitle()); 0113 } 0114 0115 void StatusNotifierTest::updateNotifier() 0116 { 0117 // log("update"); 0118 if (!enabledCheck->isChecked()) { 0119 delete d->systemNotifier; 0120 d->systemNotifier = nullptr; 0121 return; 0122 } else { 0123 if (!d->systemNotifier) { 0124 init(); 0125 } 0126 } 0127 0128 if (!d->systemNotifier) { 0129 return; 0130 } 0131 if (statusAuto->isChecked()) { 0132 d->timer->start(); 0133 } else { 0134 d->timer->stop(); 0135 } 0136 0137 KStatusNotifierItem::ItemStatus s = KStatusNotifierItem::Passive; 0138 if (statusActive->isChecked()) { 0139 s = KStatusNotifierItem::Active; 0140 } else if (statusNeedsAttention->isChecked()) { 0141 s = KStatusNotifierItem::NeedsAttention; 0142 } 0143 d->systemNotifier->setStatus(s); 0144 0145 iconPixmapCheckbox->isChecked() ? d->systemNotifier->setIconByPixmap(QIcon::fromTheme(iconName->text())) 0146 : d->systemNotifier->setIconByName(iconName->text()); 0147 overlayIconPixmapCheckbox->isChecked() ? d->systemNotifier->setOverlayIconByPixmap(QIcon::fromTheme(overlayIconName->text())) 0148 : d->systemNotifier->setOverlayIconByName(overlayIconName->text()); 0149 attentionIconPixmapCheckbox->isChecked() ? d->systemNotifier->setAttentionIconByPixmap(QIcon::fromTheme(attentionIconName->text())) 0150 : d->systemNotifier->setAttentionIconByName(attentionIconName->text()); 0151 0152 d->systemNotifier->setToolTip(iconName->text(), tooltipText->text(), tooltipSubtext->text()); 0153 0154 updateUi(); 0155 } 0156 0157 int StatusNotifierTest::runMain() 0158 { 0159 d->timer = new QTimer(this); 0160 connect(d->timer, &QTimer::timeout, this, &StatusNotifierTest::timeout); 0161 d->timer->setInterval(d->interval); 0162 // d->timer->start(); 0163 return 0; 0164 } 0165 0166 void StatusNotifierTest::timeout() 0167 { 0168 if (!d->systemNotifier) { 0169 return; 0170 } 0171 0172 if (d->systemNotifier->status() == KStatusNotifierItem::Passive) { 0173 d->systemNotifier->setStatus(KStatusNotifierItem::Active); 0174 qDebug() << " Now Active"; 0175 } else if (d->systemNotifier->status() == KStatusNotifierItem::Active) { 0176 d->systemNotifier->setStatus(KStatusNotifierItem::NeedsAttention); 0177 qDebug() << " Now NeedsAttention"; 0178 } else if (d->systemNotifier->status() == KStatusNotifierItem::NeedsAttention) { 0179 d->systemNotifier->setStatus(KStatusNotifierItem::Passive); 0180 qDebug() << " Now passive"; 0181 } 0182 updateUi(); 0183 } 0184 0185 void StatusNotifierTest::activateRequested(bool active, const QPoint &pos) 0186 { 0187 Q_UNUSED(active); 0188 Q_UNUSED(pos); 0189 log(QStringLiteral("Activated")); 0190 } 0191 0192 void StatusNotifierTest::secondaryActivateRequested(const QPoint &pos) 0193 { 0194 Q_UNUSED(pos); 0195 log(QStringLiteral("secondaryActivateRequested")); 0196 } 0197 0198 void StatusNotifierTest::scrollRequested(int delta, Qt::Orientation orientation) 0199 { 0200 QString msg(QStringLiteral("Scrolled by ")); 0201 msg.append(QString::number(delta)); 0202 msg.append((orientation == Qt::Horizontal) ? " Horizontally" : " Vertically"); 0203 log(msg); 0204 } 0205 0206 // Jobs 0207 0208 void StatusNotifierTest::enableJob(bool enable) 0209 { 0210 qDebug() << "Job enabled." << enable; 0211 if (enable) { 0212 d->job = new PumpJob(speedSlider->value()); 0213 QObject::connect(d->job, SIGNAL(percent(KJob *, unsigned long)), this, SLOT(setJobProgress(KJob *, unsigned long))); 0214 QObject::connect(d->job, &KJob::result, this, &StatusNotifierTest::result); 0215 } else { 0216 if (d->job) { 0217 d->timer->stop(); 0218 jobEnabledCheck->setChecked(Qt::Unchecked); 0219 d->job->kill(); 0220 } 0221 } 0222 } 0223 0224 void StatusNotifierTest::setJobProgress(KJob *j, unsigned long v) 0225 { 0226 Q_UNUSED(j) 0227 jobProgressBar->setValue(v); 0228 } 0229 0230 void StatusNotifierTest::result(KJob *job) 0231 { 0232 if (job->error()) { 0233 qDebug() << "Job Error:" << job->errorText() << job->errorString(); 0234 } else { 0235 qDebug() << "Job finished successfully."; 0236 } 0237 jobEnabledCheck->setCheckState(Qt::Unchecked); 0238 } 0239 0240 #include "moc_statusnotifiertest.cpp"