File indexing completed on 2024-05-05 04:46:59

0001 /*
0002  * <one line to give the program's name and a brief idea of what it does.>
0003  * Copyright (C) 2021  <copyright holder> <email>
0004  *
0005  * This program is free software: you can redistribute it and/or modify
0006  * it under the terms of the GNU 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  * This program 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 General Public License for more details.
0014  *
0015  * You should have received a copy of the GNU General Public License
0016  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0017  */
0018 
0019 #include "notify.h"
0020 #include <KNotification>
0021 #include <QPixmap>
0022 #include <QDebug>
0023 
0024 NotifyAction::NotifyAction(QObject* parent) : QObject(parent)
0025 {
0026 }
0027 
0028 void NotifyAction::setText(const QString& text)
0029 {
0030     if(text == m_text)
0031     {
0032         return;
0033     }
0034     
0035     m_text = text;
0036     Q_EMIT textChanged();
0037 }
0038 
0039 QString NotifyAction::text() const
0040 {
0041     return m_text;
0042 }
0043 
0044 Notify::Notify(QObject* parent) : QObject(parent)
0045 ,m_defaultAction(nullptr)
0046 {
0047 }
0048 
0049 QQmlListProperty<NotifyAction> Notify::actions()
0050 {
0051 //  return {this, this,
0052 //      &Notify::appendAction,
0053 //          &Notify::actionsCount,
0054 //          &Notify::action,
0055 //          &Notify::clearActions,
0056 //          &Notify::replaceAction,
0057 //          &Notify::removeLastAction
0058 //    };
0059 }
0060 
0061 void Notify::appendAction(NotifyAction* action)
0062 {
0063   m_actions.append(action);
0064 }
0065 
0066 int Notify::actionsCount() const
0067 {
0068   return m_actions.count();
0069 }
0070 
0071 NotifyAction * Notify::action(int index) const
0072 {
0073   return m_actions.at(index);
0074 }
0075 
0076 void Notify::clearActions()
0077 {
0078   m_actions.clear();
0079 }
0080 
0081 void Notify::replaceAction(int index, NotifyAction* action)
0082 {
0083   m_actions[index] = action;
0084 }
0085 
0086 void Notify::removeLastAction()
0087 {
0088   m_actions.removeLast();
0089 }
0090 
0091 void Notify::appendAction(QQmlListProperty<NotifyAction>* list, NotifyAction* action)
0092 {
0093   reinterpret_cast< Notify* >(list->data)->appendAction(action);
0094 }
0095 
0096 int Notify::actionsCount(QQmlListProperty<NotifyAction>* list)
0097 {
0098   return reinterpret_cast< Notify* >(list->data)->actionsCount();
0099 }
0100 
0101 NotifyAction * Notify::action(QQmlListProperty<NotifyAction>* list, int index)
0102 {
0103   return reinterpret_cast< Notify* >(list->data)->action(index);
0104 }
0105 
0106 void Notify::clearActions(QQmlListProperty<NotifyAction>* list)
0107 {
0108   reinterpret_cast< Notify* >(list->data)->clearActions();
0109 }
0110 
0111 void Notify::replaceAction(QQmlListProperty<NotifyAction>* list, int index, NotifyAction* action)
0112 {
0113   reinterpret_cast< Notify* >(list->data)->replaceAction(index, action);
0114 }
0115 
0116 void Notify::removeLastAction(QQmlListProperty<NotifyAction>* list)
0117 {
0118   reinterpret_cast< Notify* >(list->data)->removeLastAction();
0119 }
0120 
0121 void Notify::send()
0122 {
0123 
0124   //const auto groups = contact->groups();
0125   //for (const QString &group : groups) {
0126   //    m_notification->addContext("group", group);
0127   //}
0128 
0129 
0130   auto notification = new KNotification(m_eventId);
0131 
0132 
0133   QStringList actionsLabels;
0134   // for(const auto &action : qAsConst(m_actions))
0135   //   {
0136   //     actionsLabels << action->text ();
0137   //     qDebug() << "Setting notify actions first" << actionsLabels;
0138   //   }
0139   // notification->setActions (actionsLabels);
0140   // 
0141   // if(m_defaultAction)
0142   //   {
0143   //     notification->setDefaultAction (m_defaultAction->text ());
0144   //   }
0145 
0146   notification->setComponentName (m_componentName);
0147   notification->setText (m_message);
0148   notification->setTitle (m_title);
0149   notification->setIconName (m_iconName);
0150   notification->setPixmap (QPixmap(m_imageSource.toString()));  
0151  notification->setUrls (m_urls);
0152 
0153   qDebug() << notification->eventId ();
0154   
0155 
0156   // connect(notification, QOverload<unsigned int>::of(&KNotification::activated), this, &Notify::actionActivated);
0157   // 
0158   // connect(notification, &KNotification::defaultActivated,[this]()
0159   // {
0160   //     if(m_defaultAction)
0161   //      Q_EMIT m_defaultAction->triggered (this);
0162   //   });
0163 
0164   notification->sendEvent();
0165 }
0166 
0167 const QString &Notify::componentName() const
0168 {
0169   return m_componentName;
0170 }
0171 
0172 void Notify::setComponentName(const QString &newComponentName)
0173 {
0174   if (m_componentName == newComponentName)
0175     return;
0176   m_componentName = newComponentName;
0177   Q_EMIT componentNameChanged(m_componentName);
0178 }
0179 
0180 void Notify::actionActivated(int index)
0181 {
0182   qDebug() << "notify action was activated at <<" << index;
0183 if(index == 0)
0184 {
0185       return;
0186   }
0187 
0188   if(index >= 1 && index-1 < m_actions.count ())
0189     {
0190       Q_EMIT m_actions.at (index-1)->triggered (this);
0191     }
0192 }
0193 
0194 const QString &Notify::eventId() const
0195 {
0196   return m_eventId;
0197 }
0198 
0199 void Notify::setEventId(const QString &newEventId)
0200 {
0201   m_eventId = newEventId;
0202 }
0203 
0204 const QString &Notify::title() const
0205 {
0206   return m_title;
0207 }
0208 
0209 void Notify::setTitle(const QString &newTitle)
0210 {
0211   if (m_title == newTitle)
0212     return;
0213   m_title = newTitle;
0214   Q_EMIT titleChanged(m_title);
0215 }
0216 
0217 const QString &Notify::message() const
0218 {
0219   return m_message;
0220 }
0221 
0222 void Notify::setMessage(const QString &newMessage)
0223 {
0224   if (m_message == newMessage)
0225     return;
0226   m_message = newMessage;
0227   Q_EMIT messageChanged(m_message);
0228 }
0229 
0230 const QString &Notify::iconName() const
0231 {
0232   return m_iconName;
0233 }
0234 
0235 void Notify::setIconName(const QString &newIconName)
0236 {
0237   if (m_iconName == newIconName)
0238     return;
0239   m_iconName = newIconName;
0240   Q_EMIT iconNameChanged(m_iconName);
0241 }
0242 
0243 const QUrl &Notify::imageSource() const
0244 {
0245   return m_imageSource;
0246 }
0247 
0248 void Notify::setImageSource(const QUrl &newImageSource)
0249 {
0250   if (m_imageSource == newImageSource)
0251     return;
0252   m_imageSource = newImageSource;
0253   Q_EMIT imageSourceChanged(m_imageSource);
0254 }
0255 
0256 NotifyAction *Notify::defaultAction() const
0257 {
0258   return m_defaultAction;
0259 }
0260 
0261 void Notify::setDefaultAction(NotifyAction *newDefaultAction)
0262 {
0263   if (m_defaultAction == newDefaultAction)
0264     return;
0265   m_defaultAction = newDefaultAction;
0266   Q_EMIT defaulActionChanged();
0267 }
0268 
0269 const QList<QUrl> &Notify::urls() const
0270 {
0271   return m_urls;
0272 }
0273 
0274 void Notify::setUrls(const QList<QUrl> &newUrls)
0275 {
0276   if (m_urls == newUrls)
0277     return;
0278   m_urls = newUrls;
0279   Q_EMIT urlsChanged(m_urls);
0280 }