File indexing completed on 2023-10-01 08:41:45

0001 /*
0002 * Copyright (C) 2011 Daniele E. Domenichelli <daniele.domenichelli@gmail.com>
0003 * Copyright (C) 1999 Preston Brown <pbrown@kde.org>
0004 *
0005 * This library is free software; you can redistribute it and/or
0006 * modify it under the terms of the GNU Lesser General Public
0007 * License as published by the Free Software Foundation; either
0008 * version 2.1 of the License, or (at your option) any later version.
0009 *
0010 * This library 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 GNU
0013 * Lesser General Public License for more details.
0014 *
0015 * You should have received a copy of the GNU Lesser General Public
0016 * License along with this library; if not, write to the Free Software
0017 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
0018 */
0019 
0020 #include "telepathy-handler-application.h"
0021 #include "ktp-debug.h"
0022 
0023 #include <cstdlib>
0024 
0025 #include <QTimer>
0026 #include <KLocalizedString>
0027 #include "debug.h"
0028 
0029 #include <TelepathyQt/Types>
0030 #include <TelepathyQt/Debug>
0031 
0032 
0033 extern bool kde_kdebug_enable_dbus_interface;
0034 
0035 namespace KTp
0036 {
0037 
0038 class TelepathyHandlerApplication::Private
0039 {
0040 public:
0041     Private(TelepathyHandlerApplication *q);
0042     ~Private();
0043 
0044     void _k_onInitialTimeout();
0045     void _k_onTimeout();
0046 
0047     void init(int initialTimeout, int timeout);
0048 
0049     TelepathyHandlerApplication *q;
0050 
0051     static bool s_persist;
0052     static bool s_debug;
0053 
0054     int initialTimeout;
0055     int timeout;
0056     QTimer *timer;
0057     bool firstJobStarted;
0058     QAtomicInt jobCount;
0059 };
0060 
0061 TelepathyHandlerApplication::Private::Private(TelepathyHandlerApplication *q)
0062     : q(q),
0063       firstJobStarted(false),
0064       jobCount(0)
0065 {
0066 }
0067 
0068 TelepathyHandlerApplication::Private::~Private()
0069 {
0070 }
0071 
0072 void TelepathyHandlerApplication::Private::_k_onInitialTimeout()
0073 {
0074 #if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
0075     if (jobCount.load() == 0 && jobCount.fetchAndAddOrdered(-1) == 0) {
0076 #else
0077     if (jobCount.loadRelaxed() == 0 && jobCount.fetchAndAddOrdered(-1) == 0) {
0078 #endif
0079         // m_jobCount is now -1
0080         qDebug() << "No job received. Exiting";
0081         QCoreApplication::quit();
0082     }
0083 }
0084 
0085 void TelepathyHandlerApplication::Private::_k_onTimeout()
0086 {
0087 #if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
0088     if (jobCount.load() == 0 && jobCount.fetchAndAddOrdered(-1) == 0) {
0089 #else
0090     if (jobCount.loadRelaxed() == 0 && jobCount.fetchAndAddOrdered(-1) == 0) {
0091 #endif
0092         // m_jobCount is now -1
0093         qDebug() << "Timeout. Exiting";
0094         QCoreApplication::quit();
0095     }
0096 }
0097 
0098 void TelepathyHandlerApplication::Private::init(int initialTimeout, int timeout)
0099 {
0100     this->initialTimeout = initialTimeout;
0101     this->timeout = timeout;
0102 
0103     // If timeout < 0 we let the application exit when the last window is closed,
0104     // Otherwise we handle it with the timeout
0105     if (timeout >= 0) {
0106         q->setQuitOnLastWindowClosed(false);
0107     }
0108 
0109     // Register TpQt4 types
0110     Tp::registerTypes();
0111 
0112     // Install TpQt4 debug callback
0113     KTp::Debug::installCallback(s_debug);
0114 
0115     if (!Private::s_persist) {
0116         timer = new QTimer(q);
0117         if (initialTimeout >= 0) {
0118             q->connect(timer, SIGNAL(timeout()), q, SLOT(_k_onInitialTimeout()));
0119             timer->start(initialTimeout);
0120         }
0121     }
0122 }
0123 
0124 bool TelepathyHandlerApplication::Private::s_persist = false;
0125 bool TelepathyHandlerApplication::Private::s_debug = false;
0126 
0127 
0128 TelepathyHandlerApplication::TelepathyHandlerApplication(int &argc, char *argv[],
0129                                                          int initialTimeout,
0130                                                          int timeout)
0131     : QApplication(argc, argv),
0132       d(new Private(this))
0133 {
0134     QCommandLineOption persistOption(QStringLiteral("persist"), i18n("Persistent mode (do not exit on timeout)"));
0135     QCommandLineOption debugOption(QStringLiteral("debug"), i18n("Show Telepathy debugging information"));
0136 
0137     QCommandLineParser cmdParser;
0138     cmdParser.addHelpOption();
0139     cmdParser.addOption(persistOption);
0140     cmdParser.addOption(debugOption);
0141     cmdParser.process(qApp->arguments());
0142 
0143     Private::s_persist = cmdParser.isSet(QStringLiteral("persist"));
0144     Private::s_debug = cmdParser.isSet(QStringLiteral("debug"));
0145 
0146 
0147     d->init(initialTimeout, timeout);
0148 }
0149 
0150 TelepathyHandlerApplication::~TelepathyHandlerApplication()
0151 {
0152     delete d;
0153 }
0154 
0155 int TelepathyHandlerApplication::newJob()
0156 {
0157     TelepathyHandlerApplication *app = qobject_cast<TelepathyHandlerApplication*>(qApp);
0158     TelepathyHandlerApplication::Private *d = app->d;
0159 
0160     int ret = d->jobCount.fetchAndAddOrdered(1);
0161     if (!Private::s_persist) {
0162         if (d->timer->isActive()) {
0163             d->timer->stop();
0164         }
0165         if (!d->firstJobStarted) {
0166             if (d->initialTimeout) {
0167                 disconnect(d->timer, SIGNAL(timeout()), app, SLOT(_k_onInitialTimeout()));
0168             }
0169             if (d->timeout >= 0) {
0170                 connect(d->timer, SIGNAL(timeout()), app, SLOT(_k_onTimeout()));
0171             }
0172             d->firstJobStarted = true;
0173         }
0174     }
0175 #if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
0176     qDebug() << "New job started." << d->jobCount.load() << "jobs currently running";
0177 #else
0178     qDebug() << "New job started." << d->jobCount.loadRelaxed() << "jobs currently running";
0179 #endif
0180     return ret;
0181 }
0182 
0183 void TelepathyHandlerApplication::jobFinished()
0184 {
0185     TelepathyHandlerApplication *app = qobject_cast<TelepathyHandlerApplication*>(qApp);
0186     TelepathyHandlerApplication::Private *d = app->d;
0187 
0188     if (d->jobCount.fetchAndAddOrdered(-1) <= 1) {
0189         if (!Private::s_persist && d->timeout >= 0) {
0190             qDebug() << "No other jobs at the moment. Starting timer.";
0191             d->timer->start(d->timeout);
0192         }
0193     }
0194 #if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
0195     qDebug() << "Job finished." << d->jobCount.load() << "jobs currently running";
0196 #else
0197     qDebug() << "Job finished." << d->jobCount.loadRelaxed() << "jobs currently running";
0198 #endif
0199 }
0200 
0201 } // namespace KTp
0202 
0203 #include "moc_telepathy-handler-application.cpp"