File indexing completed on 2024-05-05 04:57:29

0001 /*
0002     This file is part of Choqok, the KDE micro-blogging client
0003 
0004     SPDX-FileCopyrightText: 2011-2012 Mehrdad Momeny <mehrdad.momeny@gmail.com>
0005 
0006     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0007 */
0008 
0009 #include "notify.h"
0010 
0011 #include <QDesktopWidget>
0012 
0013 #include <KPluginFactory>
0014 
0015 #include "account.h"
0016 #include "application.h"
0017 #include "choqokuiglobal.h"
0018 #include "mediamanager.h"
0019 #include "postwidget.h"
0020 
0021 #include "notification.h"
0022 #include "notifysettings.h"
0023 
0024 K_PLUGIN_CLASS_WITH_JSON(Notify, "choqok_notify.json")
0025 
0026 Notify::Notify(QObject *parent, const QList< QVariant > &)
0027     : Choqok::Plugin(QLatin1String("choqok_betternotify"), parent), notification(nullptr)
0028 {
0029     NotifySettings set;
0030     accountsList = set.accounts();
0031     timer.setInterval(set.notifyInterval() * 1000);
0032     connect(Choqok::UI::Global::SessionManager::self(), &Choqok::UI::Global::SessionManager::newPostWidgetAdded,
0033             this, &Notify::slotNewPostWidgetAdded);
0034     connect(&timer, &QTimer::timeout, this, &Notify::notifyNextPost);
0035 
0036     notifyPosition = set.position();
0037 }
0038 
0039 Notify::~Notify()
0040 {
0041 }
0042 
0043 void Notify::slotNewPostWidgetAdded(Choqok::UI::PostWidget *pw, Choqok::Account *acc, QString tm)
0044 {
0045 //     qDebug()<<Choqok::Application::isStartingUp()<< Choqok::Application::isShuttingDown();
0046     if (Choqok::Application::isStartingUp() || Choqok::Application::isShuttingDown()) {
0047         //qDebug()<<"Choqok is starting up or going down!";
0048         return;
0049     }
0050     if (pw && !pw->isRead() && accountsList[acc->alias()].contains(tm)) {
0051         //qDebug()<<"POST ADDED TO NOTIFY IT: "<<pw->currentPost()->content;
0052         postQueueToNotify.enqueue(pw);
0053         if (!timer.isActive()) {
0054             notifyNextPost();
0055             timer.start();
0056         }
0057     }
0058 }
0059 
0060 void Notify::notifyNextPost()
0061 {
0062     if (postQueueToNotify.isEmpty()) {
0063         timer.stop();
0064         if (notification) {
0065             hideLastNotificationAndShowThis();
0066         }
0067     } else {
0068         notify(postQueueToNotify.dequeue());
0069     }
0070 }
0071 
0072 void Notify::notify(QPointer< Choqok::UI::PostWidget > post)
0073 {
0074     if (post) {
0075         Notification *notif = new Notification(post);
0076         connect(notif, &Notification::ignored, this, &Notify::stopNotifications);
0077         connect(notif, &Notification::postReaded, this, &Notify::slotPostReaded);
0078         connect(notif, &Notification::mouseEntered, &timer, &QTimer::stop);
0079         connect(notif, &Notification::mouseLeaved, &timer, (void (QTimer::*)())&QTimer::start);
0080         hideLastNotificationAndShowThis(notif);
0081     } else  {
0082         hideLastNotificationAndShowThis();
0083     }
0084 }
0085 
0086 void Notify::slotPostReaded()
0087 {
0088     notifyNextPost();
0089     timer.stop();
0090     timer.start();
0091 }
0092 
0093 void Notify::stopNotifications()
0094 {
0095     postQueueToNotify.clear();
0096     timer.stop();
0097     hideLastNotificationAndShowThis();
0098 }
0099 
0100 void Notify::hideLastNotificationAndShowThis(Notification *nextNotificationToShow)
0101 {
0102     //TODO: Add Animation
0103     notification->deleteLater();
0104     notification = nullptr;
0105     if (nextNotificationToShow) {
0106         notification = nextNotificationToShow;
0107         notification->move(notifyPosition);
0108         notification->show();
0109     }
0110 }
0111 
0112 #include "moc_notify.cpp"
0113 #include "notify.moc"