[Ktutorial-commits] SF.net SVN: ktutorial:[320] trunk/ktutorial/ktutorial-editor
Status: Alpha
Brought to you by:
danxuliu
From: <dan...@us...> - 2011-06-10 16:17:17
|
Revision: 320 http://ktutorial.svn.sourceforge.net/ktutorial/?rev=320&view=rev Author: danxuliu Date: 2011-06-10 16:17:10 +0000 (Fri, 10 Jun 2011) Log Message: ----------- Change how the target application is detected. Before, every D-Bus service that appeared in the bus was queried for the applicationFilePath in its ktutorial object. However, an application can provide a D-Bus service before the ktutorial object is registered in it, so a valid application could be seen as invalid just because the KTutorial module was not registered yet. Now, the service of the target application is found using the PID of its process, and if the ktutorial object is not registered yet, the service is queried again until the object is found (or the timer to find the application times out). Modified Paths: -------------- trunk/ktutorial/ktutorial-editor/src/targetapplication/TargetApplication.cpp trunk/ktutorial/ktutorial-editor/src/targetapplication/TargetApplication.h trunk/ktutorial/ktutorial-editor/tests/unit/targetapplication/CMakeLists.txt trunk/ktutorial/ktutorial-editor/tests/unit/targetapplication/TargetApplicationTest.cpp Added Paths: ----------- trunk/ktutorial/ktutorial-editor/tests/unit/targetapplication/TargetApplicationStubWithDelayedRegister.cpp Modified: trunk/ktutorial/ktutorial-editor/src/targetapplication/TargetApplication.cpp =================================================================== --- trunk/ktutorial/ktutorial-editor/src/targetapplication/TargetApplication.cpp 2011-06-08 14:49:14 UTC (rev 319) +++ trunk/ktutorial/ktutorial-editor/src/targetapplication/TargetApplication.cpp 2011-06-10 16:17:10 UTC (rev 320) @@ -1,5 +1,5 @@ /*************************************************************************** - * Copyright (C) 2010 by Daniel Calviño Sánchez * + * Copyright (C) 2010-2011 by Daniel Calviño Sánchez * * dan...@gm... * * * * This program is free software; you can redistribute it and/or modify * @@ -95,11 +95,11 @@ connect(mProcess, SIGNAL(finished(int)), this, SLOT(clean())); - mServiceDiscoveryTimer.setInterval(3000); - mServiceDiscoveryTimer.setSingleShot(true); + mKTutorialSupportModuleDiscoveryTimer.setInterval(3000); + mKTutorialSupportModuleDiscoveryTimer.setSingleShot(true); connect(mProcess, SIGNAL(started()), - &mServiceDiscoveryTimer, SLOT(start())); - connect(&mServiceDiscoveryTimer, SIGNAL(timeout()), + &mKTutorialSupportModuleDiscoveryTimer, SLOT(start())); + connect(&mKTutorialSupportModuleDiscoveryTimer, SIGNAL(timeout()), this, SLOT(handleTargetApplicationDoesNotSupportKTutorial())); mProcess->start(); @@ -118,33 +118,53 @@ //private slots: void TargetApplication::checkNewService(const QString& service) { - QDBusInterface interface(service, "/ktutorial", - "org.kde.ktutorial.EditorSupport"); - if (!interface.isValid()) { + QDBusInterface mainDBusInterface("org.freedesktop.DBus", "/", + "org.freedesktop.DBus"); + if (!mainDBusInterface.isValid()) { return; } - QDBusReply<QString> reply = interface.call("applicationFilePath"); - if (!reply.isValid()) { + QDBusReply<uint> pidReply = + mainDBusInterface.call("GetConnectionUnixProcessID", service); + if (!pidReply.isValid()) { return; } - if (reply.value() != mTargetApplicationFilePath) { + if (pidReply.value() != (uint)mProcess->pid()) { return; } - mServiceDiscoveryTimer.stop(); - - //The bus must be disconnected before executing other code to ensure that - //no other services are handled, as when an application starts it can create - //more than one valid service: one like ":1.42" and another like - //"org.freedesktop.DBus". - //So this method could be called again before finishing its current - //execution if serviceRegistered(QSignal) is emitted, which could happen if - //other event loops are created (like in a message box). disconnect(QDBusConnection::sessionBus().interface(), 0, this, 0); mServiceName = service; + + checkKTutorialSupportModule(); +} + +void TargetApplication::checkKTutorialSupportModule() { + //Prevent pending checks from being executed after the timeout of the + //discovery timer + if (!mKTutorialSupportModuleDiscoveryTimer.isActive()) { + return; + } + + QDBusInterface interface(mServiceName, "/ktutorial", + "org.freedesktop.DBus.Introspectable"); + if (!interface.isValid()) { + QTimer::singleShot(500, this, SLOT(checkKTutorialSupportModule())); + return; + } + + //Just a generic DBus call to check whether /ktutorial is already available + //or not + QDBusReply<QString> reply = interface.call("Introspect"); + if (!reply.isValid()) { + QTimer::singleShot(500, this, SLOT(checkKTutorialSupportModule())); + return; + } + + mKTutorialSupportModuleDiscoveryTimer.stop(); + mMapper = new RemoteObjectMapper(mServiceName); mRemoteEditorSupport = new RemoteEditorSupport(mServiceName, mMapper); @@ -156,7 +176,7 @@ return; } - mServiceDiscoveryTimer.stop(); + mKTutorialSupportModuleDiscoveryTimer.stop(); emit startFailed(InvalidPath); } Modified: trunk/ktutorial/ktutorial-editor/src/targetapplication/TargetApplication.h =================================================================== --- trunk/ktutorial/ktutorial-editor/src/targetapplication/TargetApplication.h 2011-06-08 14:49:14 UTC (rev 319) +++ trunk/ktutorial/ktutorial-editor/src/targetapplication/TargetApplication.h 2011-06-10 16:17:10 UTC (rev 320) @@ -1,5 +1,5 @@ /*************************************************************************** - * Copyright (C) 2010 by Daniel Calviño Sánchez * + * Copyright (C) 2010-2011 by Daniel Calviño Sánchez * * dan...@gm... * * * * This program is free software; you can redistribute it and/or modify * @@ -174,12 +174,12 @@ KProcess* mProcess; /** - * Timer to give time to "/ktutorial" object to appear in some new service - * of the bus. + * Timer to give time to "/ktutorial" object to appear in the service of + * the target application. * If the timer ends and the object was not found, the target application * seems to not to support KTutorial editor. */ - QTimer mServiceDiscoveryTimer; + QTimer mKTutorialSupportModuleDiscoveryTimer; /** * The D-Bus service name provided by the target application, if any. @@ -208,14 +208,23 @@ /** * Checks if the new service found in the session bus is the one from the * started target application. - * When the service is found, the target application has started from - * KTutorial editor point of view. Signal started() is emitted in that case. + * If the service is provided by the target application, it is checked if it + * contains the support module from KTutorial. * * @param service The D-Bus service to check. */ void checkNewService(const QString& service); /** + * Checks if the service of the target application provides the "/ktutorial" + * object. + * If the object is not found, the service will be checked again later. + * When the object is found, the target application has started from + * KTutorial editor point of view. Signal started() is emitted in that case. + */ + void checkKTutorialSupportModule(); + + /** * Checks if the process could not be started. * Signal startFailed(Error), with InvalidPath, is emitted in that case. * @@ -224,7 +233,7 @@ void handleProcessError(QProcess::ProcessError error); /** - * Called when the time out to find the D-Bus service expired. + * Called when the time to find the "/ktutorial" object expired. * Signal startFailed(Error), with InvalidApplication, is emitted in that * case. */ Modified: trunk/ktutorial/ktutorial-editor/tests/unit/targetapplication/CMakeLists.txt =================================================================== --- trunk/ktutorial/ktutorial-editor/tests/unit/targetapplication/CMakeLists.txt 2011-06-08 14:49:14 UTC (rev 319) +++ trunk/ktutorial/ktutorial-editor/tests/unit/targetapplication/CMakeLists.txt 2011-06-10 16:17:10 UTC (rev 320) @@ -14,6 +14,12 @@ add_executable(TargetApplicationStub TargetApplicationStub.cpp ${dbus_interface_stubs_MOC}) target_link_libraries(TargetApplicationStub ${KDE4_KDEUI_LIBS} ${QT_QTDBUS_LIBRARY}) +# Target application stub with delayed register to be executed in TargetApplication test +set(TargetApplicationStubWithDelayedRegister_MOC ${CMAKE_CURRENT_BINARY_DIR}/TargetApplicationStubWithDelayedRegister.moc) +qt4_generate_moc(TargetApplicationStubWithDelayedRegister.cpp ${TargetApplicationStubWithDelayedRegister_MOC}) +add_executable(TargetApplicationStubWithDelayedRegister TargetApplicationStubWithDelayedRegister.cpp ${TargetApplicationStubWithDelayedRegister_MOC} ${dbus_interface_stubs_MOC}) +target_link_libraries(TargetApplicationStubWithDelayedRegister ${KDE4_KDEUI_LIBS} ${QT_QTDBUS_LIBRARY}) + MACRO(UNIT_TESTS) FOREACH(_className ${ARGN}) set(_testName ${_className}Test) Added: trunk/ktutorial/ktutorial-editor/tests/unit/targetapplication/TargetApplicationStubWithDelayedRegister.cpp =================================================================== --- trunk/ktutorial/ktutorial-editor/tests/unit/targetapplication/TargetApplicationStubWithDelayedRegister.cpp (rev 0) +++ trunk/ktutorial/ktutorial-editor/tests/unit/targetapplication/TargetApplicationStubWithDelayedRegister.cpp 2011-06-10 16:17:10 UTC (rev 320) @@ -0,0 +1,60 @@ +/*************************************************************************** + * Copyright (C) 2011 by Daniel Calviño Sánchez * + * dan...@gm... * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 3 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; If not, see <http://www.gnu.org/licenses/>. * + ***************************************************************************/ + +#include <KApplication> +#include <KCmdLineArgs> +#include <KMainWindow> + +#include "RemoteClassStubs.h" + +class MainWindow: public KMainWindow { +Q_OBJECT +public: + + MainWindow() { + QTimer::singleShot(1000, this, SLOT(registerKTutorialInDBus())); + } + +private slots: + + void registerKTutorialInDBus() { + StubEditorSupport* editorSupport = new StubEditorSupport(); + QDBusConnection::sessionBus().registerObject("/ktutorial", + editorSupport, QDBusConnection::ExportAllSlots); + + StubObjectRegister* objectRegister = new StubObjectRegister(); + QDBusConnection::sessionBus().registerObject( + "/ktutorial/ObjectRegister", objectRegister, + QDBusConnection::ExportAdaptors); + } + +}; + +int main (int argc, char *argv[]) { + KCmdLineArgs::init(argc, argv, "TargetApplicationStubWithDelayedRegister", + "", ki18n(""), ""); + + KApplication app; + + KMainWindow* window = new MainWindow(); + window->show(); + + return app.exec(); +} + +#include "TargetApplicationStubWithDelayedRegister.moc" Property changes on: trunk/ktutorial/ktutorial-editor/tests/unit/targetapplication/TargetApplicationStubWithDelayedRegister.cpp ___________________________________________________________________ Added: svn:eol-style + native Modified: trunk/ktutorial/ktutorial-editor/tests/unit/targetapplication/TargetApplicationTest.cpp =================================================================== --- trunk/ktutorial/ktutorial-editor/tests/unit/targetapplication/TargetApplicationTest.cpp 2011-06-08 14:49:14 UTC (rev 319) +++ trunk/ktutorial/ktutorial-editor/tests/unit/targetapplication/TargetApplicationTest.cpp 2011-06-10 16:17:10 UTC (rev 320) @@ -1,5 +1,5 @@ /*************************************************************************** - * Copyright (C) 2010 by Daniel Calviño Sánchez * + * Copyright (C) 2010-2011 by Daniel Calviño Sánchez * * dan...@gm... * * * * This program is free software; you can redistribute it and/or modify * @@ -59,6 +59,7 @@ void testSetTargetApplicationFilePathRelative(); void testStart(); + void testStartApplicationWithDelayedRegister(); void testStartAlreadyStarted(); void testStartAfterSettingAgainTargetApplicationFilePath(); void testStartAfterChangingTargetApplicationFilePath(); @@ -203,6 +204,21 @@ QString("The object name 42")); } +void TargetApplicationTest::testStartApplicationWithDelayedRegister() { + TargetApplication targetApplication; + targetApplication.setTargetApplicationFilePath( + QApplication::applicationDirPath() + + "/TargetApplicationStubWithDelayedRegister"); + + SignalWait startedWait(&targetApplication, SIGNAL(started())); + targetApplication.start(); + + QVERIFY(startedWait.waitForCount(1, 2000)); + QVERIFY(targetApplication.remoteEditorSupport()); + QCOMPARE(targetApplication.remoteEditorSupport()->mainWindow()->name(), + QString("The object name 42")); +} + void TargetApplicationTest::testStartAlreadyStarted() { TargetApplication targetApplication; targetApplication.setTargetApplicationFilePath(mTargetApplicationStubPath); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |