File indexing completed on 2024-04-21 04:55:26

0001 /*
0002     This file is part of Choqok, the KDE micro-blogging client
0003 
0004     SPDX-FileCopyrightText: 2010-2012 Andrey Esin <gmlastik@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 "indicatormanager.h"
0010 
0011 #include <QIcon>
0012 #include <QTimer>
0013 
0014 #include "account.h"
0015 #include "accountmanager.h"
0016 #include "choqokuiglobal.h"
0017 #include "microblog.h"
0018 #include "microblogwidget.h"
0019 #include "choqokbehaviorsettings.h"
0020 
0021 #define STR(x) #x
0022 #define XSTR(x) STR(x)
0023 
0024 namespace Choqok
0025 {
0026 
0027 MessageIndicatorManager::MessageIndicatorManager()
0028 {
0029 
0030     iServer = QIndicate::Server::defaultInstance();
0031     iServer->setType("message.irc");
0032     QString desktopFile = QString("%1/%2.desktop")
0033                           .arg(XSTR(XDG_APPS_INSTALL_DIR))
0034                           .arg(QCoreApplication::applicationFilePath().section('/', -1));
0035     iServer->setDesktopFile(desktopFile);
0036     connect(iServer, SIGNAL(serverDisplay()), SLOT(slotShowMainWindow()));
0037     if (Choqok::BehaviorSettings::libindicate()) {
0038         iServer->show();
0039     }
0040     connect(Choqok::AccountManager::self(), SIGNAL(allAccountsLoaded()), SLOT(slotCanWorkWithAccs()));
0041     //QTimer::singleShot ( 500, this, SLOT (slotCanWorkWithAccs()) );
0042 
0043     connect(Choqok::BehaviorSettings::self(), SIGNAL(configChanged()), SLOT(slotConfigChanged()));
0044 }
0045 
0046 MessageIndicatorManager::~MessageIndicatorManager()
0047 {
0048 }
0049 
0050 void MessageIndicatorManager::slotCanWorkWithAccs()
0051 {
0052     QList<Choqok::UI::MicroBlogWidget *> lst = choqokMainWindow->microBlogsWidgetsList();
0053     if (lst.count() == Choqok::AccountManager::self()->accounts().count()) {
0054         for (Choqok::UI::MicroBlogWidget *microBlog, lst) {
0055             connect(microBlog, SIGNAL(updateUnreadCount(int,int)), SLOT(slotupdateUnreadCount(int,int)));
0056         }
0057     } else {
0058         QTimer::singleShot(500, this, SLOT(slotCanWorkWithAccs()));
0059     }
0060 }
0061 
0062 void MessageIndicatorManager::slotConfigChanged()
0063 {
0064     if (!Choqok::BehaviorSettings::libindicate()) {
0065         iServer->hide();
0066     }
0067     if (Choqok::BehaviorSettings::libindicate()) {
0068         iServer->show();
0069     }
0070 }
0071 void MessageIndicatorManager::slotupdateUnreadCount(int change, int sum)
0072 {
0073     Q_UNUSED(change);
0074     QString alias = qobject_cast<Choqok::UI::MicroBlogWidget *> (sender())->currentAccount()->alias();
0075     if (Choqok::BehaviorSettings::libindicate()) {
0076         newPostInc(sum, alias, QString());
0077     }
0078 }
0079 
0080 QImage MessageIndicatorManager::getIconByAlias(const QString &alias)
0081 {
0082     Choqok::Account *acc = Choqok::AccountManager::self()->findAccount(alias);
0083     return QIcon::fromTheme(acc->microblog()->pluginIcon()).pixmap(QSize(16, 16), QIcon::Normal, QIcon::On).toImage();
0084 }
0085 
0086 void MessageIndicatorManager::newPostInc(int unread, const QString &alias, const QString &timeline)
0087 {
0088     Q_UNUSED(timeline);
0089 
0090     if (!iList.contains(alias)) {
0091         QIndicate::Indicator *newIndicator = new QIndicate::Indicator(this);
0092         newIndicator->setNameProperty(alias);
0093         newIndicator->setIconProperty(getIconByAlias(alias));
0094         iList.insert(alias, newIndicator);
0095         connect(iList.value(alias), SIGNAL(display(QIndicate::Indicator*)), SLOT(slotDisplay(QIndicate::Indicator*)));
0096     }
0097     iList.value(alias)->setCountProperty(unread);
0098     iList.value(alias)->setDrawAttentionProperty(unread != 0);
0099     if (unread == 0) {
0100         iList.value(alias)->hide();
0101     } else {
0102         iList.value(alias)->show();
0103     }
0104 
0105 }
0106 
0107 void MessageIndicatorManager::slotDisplay(QIndicate::Indicator *indicator)
0108 {
0109     QString alias = indicator->nameProperty();
0110     Choqok::Account *acc = Choqok::AccountManager::self()->findAccount(alias);
0111     choqokMainWindow->activateTab(acc->priority());
0112     slotShowMainWindow();
0113 }
0114 
0115 void MessageIndicatorManager::slotShowMainWindow()
0116 {
0117     choqokMainWindow->activateChoqok();
0118 }
0119 
0120 MessageIndicatorManager *MessageIndicatorManager::mSelf = nullptr;
0121 
0122 MessageIndicatorManager *MessageIndicatorManager::self()
0123 {
0124     if (!mSelf) {
0125         mSelf = new MessageIndicatorManager;
0126     }
0127     return mSelf;
0128 }
0129 
0130 }
0131 
0132 #include "moc_indicatormanager.cpp"