File indexing completed on 2022-11-29 18:25:18

0001 /* This file is part of the KDE libraries
0002     Copyright (c) 1999 Waldo Bastian <bastian@kde.org>
0003 
0004     This library is free software; you can redistribute it and/or
0005     modify it under the terms of the GNU Library General Public
0006     License version 2 as published by the Free Software Foundation.
0007 
0008     This library is distributed in the hope that it will be useful,
0009     but WITHOUT ANY WARRANTY; without even the implied warranty of
0010     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0011     Library General Public License for more details.
0012 
0013     You should have received a copy of the GNU Library General Public License
0014     along with this library; see the file COPYING.LIB.  If not, write to
0015     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0016     Boston, MA 02110-1301, USA.
0017 */
0018 
0019 #include <unistd.h>
0020 #include "kuniqueapplication.h"
0021 
0022 #include <stdio.h>
0023 #include <kcmdlineargs.h>
0024 #include <k4aboutdata.h>
0025 #include <kdebug.h>
0026 
0027 #include <QTimer>
0028 #include <QFile>
0029 #include <QProcess>
0030 
0031 class TestApp : public KUniqueApplication
0032 {
0033     Q_OBJECT
0034 public:
0035     TestApp() : KUniqueApplication("TestApp"), m_callCount(0) { }
0036     int newInstance() override;
0037     int callCount() const
0038     {
0039         return m_callCount;
0040     }
0041 
0042 private Q_SLOTS:
0043     void executeNewChild()
0044     {
0045         // Duplicated from kglobalsettingstest.cpp - make a shared helper method?
0046         QProcess *proc = new QProcess(this);
0047         QString appName = "kuniqueapptest";
0048 #ifdef Q_OS_WIN
0049         appName = appName + ".exe";
0050 #else
0051         if (QFile::exists(appName + ".shell")) {
0052             appName = "./" + appName + ".shell";
0053         } else {
0054             Q_ASSERT(QFile::exists(appName));
0055             appName = "./" + appName;
0056         }
0057 #endif
0058         proc->start(appName);
0059     }
0060 private:
0061     int m_callCount;
0062 };
0063 
0064 int TestApp::newInstance()
0065 {
0066     ++m_callCount;
0067     KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
0068     kDebug() << "NewInstance";
0069     for (int i = 0; i < args->count(); i++) {
0070         kDebug() << "argument " << i << " : " << args->arg(i);
0071     }
0072 
0073     if (m_callCount == 2) { // OK, all done, quit
0074         quit();
0075     }
0076 
0077     return 0;
0078 }
0079 
0080 int main(int argc, char *argv[])
0081 {
0082     KCmdLineOptions options;
0083     options.add("!+[argument]", ki18n("arguments passed to new instance"));
0084 
0085     K4AboutData about("kuniqueapptest", nullptr, ki18n("kuniqueapptest"), "version");
0086     KCmdLineArgs::init(argc, argv, &about);
0087     KCmdLineArgs::addCmdLineOptions(options);
0088     KUniqueApplication::addCmdLineOptions();
0089 
0090     if (!TestApp::start()) {
0091         return 1;
0092     }
0093     TestApp a;
0094 
0095     // Testcase for the problem coming from the old fork-on-startup solution:
0096     // the "newInstance" D-Bus call would time out if the app took too much time
0097     // to be ready.
0098     //printf("Sleeping.\n");
0099     //sleep(200);
0100 
0101     QTimer::singleShot(400, &a, SLOT(executeNewChild()));
0102 
0103     printf("Running.\n");
0104     kapp->exec();
0105     printf("Terminating.\n");
0106 
0107     Q_ASSERT(a.callCount() == 2);
0108     const bool ok = a.callCount() == 2;
0109 
0110     return ok ? 0 : 1;
0111 }
0112 
0113 #include "kuniqueapptest.moc"