File indexing completed on 2024-04-14 05:38:07

0001 /***************************************************************************
0002  *   Copyright (C) 2008-2011 Daniel Nicoletti                              *
0003  *   dantti12@gmail.com                                                    *
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 2 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, write to the                         *
0017  *   Free Software Foundation, Inc.,                                       *
0018  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA .        *
0019  ***************************************************************************/
0020 
0021 #include "DBusUpdaterInterface.h"
0022 
0023 #include "apperupdatericonadaptor.h"
0024 
0025 #include <QtDBus/QDBusConnection>
0026 
0027 #include <QLoggingCategory>
0028 
0029 DBusUpdaterInterface::DBusUpdaterInterface(QObject *parent) :
0030     QObject(parent),
0031     m_registered(false)
0032 {
0033     (void) new ApperUpdaterIconAdaptor(this);
0034 }
0035 
0036 DBusUpdaterInterface::~DBusUpdaterInterface()
0037 {
0038     if (m_registered) {
0039         unregisterService();
0040     }
0041 }
0042 
0043 bool DBusUpdaterInterface::isRegistered() const
0044 {
0045     return m_registered;
0046 }
0047 
0048 void DBusUpdaterInterface::ReviewUpdates()
0049 {
0050     emit reviewUpdates();
0051 }
0052 
0053 void DBusUpdaterInterface::registerService()
0054 {
0055 //    kDebug();
0056     auto watcher = qobject_cast<QDBusServiceWatcher*>(sender());
0057     if (!m_registered && !QDBusConnection::sessionBus().registerService(QLatin1String("org.kde.ApperUpdaterIcon"))) {
0058 //        kDebug() << "unable to register service to dbus";
0059         if (!watcher) {
0060             // in case registration fails due to another user or application running
0061             // keep an eye on it so we can register when available
0062             watcher = new QDBusServiceWatcher(QLatin1String("org.kde.ApperUpdaterIcon"),
0063                                               QDBusConnection::systemBus(),
0064                                               QDBusServiceWatcher::WatchForUnregistration,
0065                                               this);
0066             connect(watcher, &QDBusServiceWatcher::serviceUnregistered, this, &DBusUpdaterInterface::registeredChanged);
0067         }
0068         m_registered = false;
0069         emit registeredChanged();
0070     } else {
0071         if (!QDBusConnection::sessionBus().registerObject(QLatin1String("/"), this)) {
0072 //            kDebug() << "unable to register service interface to dbus";
0073             return;
0074         }
0075 
0076         m_registered = true;
0077         emit registeredChanged();
0078     }
0079 }
0080 
0081 void DBusUpdaterInterface::unregisterService()
0082 {
0083     // We need to unregister the service since
0084     // plasma-desktop won't exit
0085     if (QDBusConnection::sessionBus().unregisterService(QLatin1String("org.kde.ApperUpdaterIcon"))) {
0086         m_registered = false;
0087         emit registeredChanged();
0088     } else {
0089 //        kDebug() << "unable to unregister service to dbus";
0090     }
0091 }