File indexing completed on 2024-04-28 04:44:12

0001 /*
0002     SnoreNotify is a Notification Framework based on Qt
0003     Copyright (C) 2013-2015  Hannah von Reth <vonreth@kde.org>
0004 
0005     SnoreNotify is free software: you can redistribute it and/or modify
0006     it under the terms of the GNU Lesser General Public License as published by
0007     the Free Software Foundation, either version 3 of the License, or
0008     (at your option) any later version.
0009 
0010     SnoreNotify is distributed in the hope that it will be useful,
0011     but WITHOUT ANY WARRANTY; without even the implied warranty of
0012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013     GNU Lesser General Public License for more details.
0014 
0015     You should have received a copy of the GNU Lesser General Public License
0016     along with SnoreNotify.  If not, see <http://www.gnu.org/licenses/>.
0017 */
0018 
0019 #include "trayicon.h"
0020 #include "libsnore/settings/settingsdialog.h"
0021 #include "libsnore/snore.h"
0022 #include "libsnore/snore_p.h"
0023 
0024 #include <QAction>
0025 #include <QApplication>
0026 #include <QAbstractButton>
0027 #include <QDialogButtonBox>
0028 #include <QMenu>
0029 #include <QSystemTrayIcon>
0030 #include <QLayout>
0031 
0032 #include "libsnore/version.h"
0033 
0034 using namespace Snore;
0035 
0036 TrayIcon::TrayIcon():
0037     m_trayIcon(new QSystemTrayIcon(QIcon(QStringLiteral(":/root/snore.png"))))
0038 {
0039     SnoreCorePrivate::instance()->defaultApplication().hints().setValue("use-markup", true);
0040     SnoreCorePrivate::instance()->defaultApplication().hints().setValue("tray-icon", QVariant::fromValue(QPointer<QSystemTrayIcon>(m_trayIcon)));
0041 }
0042 
0043 void TrayIcon::initConextMenu()
0044 {
0045     m_settings = new SettingsDialog();
0046     QDialogButtonBox *box = new QDialogButtonBox(QDialogButtonBox::Apply | QDialogButtonBox::Cancel | QDialogButtonBox::Ok | QDialogButtonBox::Reset, m_settings);
0047     connect(box, &QDialogButtonBox::clicked, [ &, box](QAbstractButton * button) {
0048         switch (box->buttonRole(button)) {
0049         case QDialogButtonBox::AcceptRole:
0050             m_settings->accept();
0051             m_settings->setVisible(false);
0052             break;
0053         case QDialogButtonBox::ApplyRole:
0054             m_settings->accept();
0055             break;
0056         case QDialogButtonBox::ResetRole:
0057             m_settings->reset();
0058             break;
0059         case QDialogButtonBox::RejectRole:
0060             m_settings->setVisible(false);
0061             break;
0062         default:
0063             qCWarning(SNORE) << "unhandled role" << button->text() << box->buttonRole(button);
0064         }
0065     });
0066     m_settings->layout()->addWidget(box);
0067 
0068 //    connect(m_settings, &SettingsDialog::finished, m_settings, &SettingsDialog::hide);
0069 
0070     m_trayIcon->setVisible(true);
0071 
0072     m_trayMenu = new QMenu(QStringLiteral("SnoreNotify"));
0073     QString version = QLatin1String("SnoreNotify ") + Version::version();
0074     if (!Version::revision().isEmpty()) {
0075         version += QLatin1String("-") + Version::revision();
0076     }
0077     m_trayMenu->addAction(version);
0078     m_trayMenu->addSeparator();
0079     m_trayMenu->addAction(tr("Display Test Notification"), this, SLOT(slotTestNotification()));
0080     m_trayMenu->addSeparator();
0081     m_trayMenu->addAction(tr("Settings"), this, SLOT(slotSettings()));
0082     m_trayMenu->addSeparator();
0083     m_trayMenu->addAction(tr("Exit"), qApp, SLOT(quit()));
0084 
0085     m_trayIcon->setContextMenu(m_trayMenu);
0086 }
0087 
0088 void TrayIcon::hide()
0089 {
0090     m_trayIcon->setVisible(false);
0091 }
0092 
0093 QSystemTrayIcon *TrayIcon::trayIcon()
0094 {
0095     return m_trayIcon;
0096 }
0097 
0098 void TrayIcon::slotTestNotification()
0099 {
0100     SnoreCore::instance().displayExampleNotification();
0101 }
0102 
0103 void TrayIcon::slotSettings()
0104 {
0105     m_settings->show();
0106 }
0107