Home · All Classes · Main Classes · Deprecated
Public Slots | Public Member Functions | Protected Attributes

MApplicationService Class Reference

Adaptor class for interface com.nokia.MApplicationIf. More...

Inheritance diagram for MApplicationService:
Inheritance graph
[legend]
Collaboration diagram for MApplicationService:
Collaboration graph
[legend]

List of all members.

Public Slots

virtual void launch ()
virtual void launch (const QStringList &parameters)
virtual void close ()
virtual void exit ()
virtual void launchAnotherWithQProcess ()
virtual void handleServiceRegistrationFailure ()
virtual void incrementAndRegister ()
virtual QString registeredName ()
virtual bool isRegistered ()
virtual bool registerService ()
void setServiceName (const QString &serviceName)

Public Member Functions

 MApplicationService (const QString &serviceName, QObject *parent=0)
virtual ~MApplicationService ()

Protected Attributes

MApplicationServicePrivate *const d_ptr

Detailed Description

Adaptor class for interface com.nokia.MApplicationIf.

This is the implementation for the MApplication QDBus service. Derive from this class to implement alternative behaviour.

If multiple processes are required, override the launch() method and from there call launchAnotherWithQProcess().

If it is desired for the subsequent processes to also have a MApplicationIf, then override handleServiceRegistrationFailure() and from there call incrementAndRegister().

If it is desired that there be no MApplicationIf interface, override registeredName() so that it returns QString(), isRegistered() to return false, and registerService() to return true.

Here is a code sample that allows multiple instances, each with its own DBus service :

class MyApplicationService: public MApplicationService
{
public:
    MyApplicationService(QObject *parent = 0) :
        MApplicationService("com.nokia.multipleinstances", parent) {
    }

    void launch() {
        launchAnotherWithQProcess();
    }

    void handleServiceRegistrationFailure() {
        qDebug() << "MyApplicationService::handleServiceRegistrationFailure()";

        incrementAndRegister();
    }
};

int main(int argc, char **argv)
{
    /* The base class of all DirectUI applications */
    MApplication app(argc, argv, "multipleinstances", new MyApplicationService());

Tip: You can use qdbusviewer from the qt4-dev-tools package to see the services appearing and disappearing when you run/kill multiple instances.

Definition at line 57 of file corelib/core/mapplicationservice.h.


Constructor & Destructor Documentation

MApplicationService::MApplicationService ( const QString serviceName,
QObject parent = 0 
)

Definition at line 29 of file mapplicationservice.cpp.

                                                                                      :
    d_ptr(new MApplicationServicePrivate(newServiceName))
{
    Q_UNUSED(parent);
}

MApplicationService::~MApplicationService (  )  [virtual]

Definition at line 35 of file mapplicationservice.cpp.

{
    delete d_ptr;
}


Member Function Documentation

void MApplicationService::close (  )  [virtual, slot]

Close the GUI.

Returns:
void

Definition at line 91 of file mapplicationservice.cpp.

{
    Q_D(MApplicationService);

    // Return to prestarted state if in LazyShutdown mode
    if (d->prestartModeIsLazyShutdown()) {
        d->restorePrestart();
    }

    mDebug("MApplicationService") << "close()";
    d->closeAllWindows();
}

void MApplicationService::exit (  )  [virtual, slot]

Exit the application.

Returns:
void

Definition at line 104 of file mapplicationservice.cpp.

{
    Q_D(MApplicationService);

    // Do not really exit the app if it was prestarted (lazy shutdown)
    if (d->prestartModeIsLazyShutdown()) {
        d->restorePrestart();
    } else {
        d->stdExit(0);
    }

    mDebug("MApplicationService") << "exit()";
}

void MApplicationService::handleServiceRegistrationFailure (  )  [virtual, slot]

Handles the situation when a service is already registered with this application's name.

Returns:
void

This method implements the default behaviour when there is already a service registrated with our service name, which is to call 'launch()' on that service and then exit. This method can be overridden and alternative behaviour implemented. One alternative behaviour is to register with another name and this is implemented in incrementAndRegister() which can be called from an overridden handleServiceRegistrationFailure().

Definition at line 118 of file mapplicationservice.cpp.

{
    Q_D(MApplicationService);

    MApplication *mApp = MApplication::instance();

    MApplicationIfProxy mApplicationIfProxy(registeredName(), mApp);

    if (mApplicationIfProxy.connection().isConnected()) {
        mDebug("MApplicationService") << "Calling launch() in other application with service :" << registeredName();
        mApplicationIfProxy.launch();
        d->stdExit(0);
    } else {
        mDebug("MApplicationService") << "DBus not connected; not launching";
    }
}

Here is the call graph for this function:

void MApplicationService::incrementAndRegister (  )  [virtual, slot]

increment service name and register

Returns:
void

It is intended that this method be called from within an overridden handleServiceRegistrationFailure() method. The behaviour is to add an integer to the name of the service being registered until registration succeeds, or the maximum number of attempts (10) is exceeded.

Definition at line 135 of file mapplicationservice.cpp.

{
    Q_D(MApplicationService);

    QString appName = d->appName();

    const int MaxNoInstances = 10;

    if (++d->instanceCounter == MaxNoInstances) {
        mDebug("MApplicationService") << "Reached maximum number instances of this application.";
        d->stdExit(0);
    }

    QString baseServiceName = "com.nokia." + appName;
    QString serviceName = baseServiceName + QString::number(d->instanceCounter);

    // register dbus service
    new MApplicationIfAdaptor(this);

    bool anotherAppRunning = !d->registerService(serviceName);
    while (anotherAppRunning) {
        mDebug("MApplicationService") << "registering failed";

        if (++d->instanceCounter == MaxNoInstances) {
            mDebug("MApplicationService") << "Reached maximum number instances of this application.";
            d->stdExit(0);
        }

        serviceName = baseServiceName + QString::number(d->instanceCounter);

        mDebug("MApplicationService") << "attempting to register service :" << serviceName;
        anotherAppRunning = !d->registerService(serviceName);
    }

    d->unregisterObject("/org/maemo/m");
    d->registerObject("/org/maemo/m", this);

    setServiceName(serviceName);
}

Here is the call graph for this function:

bool MApplicationService::isRegistered (  )  [virtual, slot]

return if the service is registered

Returns:
true if the service is registered

If you want no MApplicationIf, override this to always return false.

Definition at line 182 of file mapplicationservice.cpp.

{
    Q_D(MApplicationService);

    return !d->serviceName.isEmpty();
}

void MApplicationService::launch ( const QStringList parameters  )  [virtual, slot]

Launch the application with parameters.

Returns:
void By default, this method will ignore the parameters and call launch()

If you want to process the parameters, derive a class from MApplicationService and override launch(const QStringList&).

Definition at line 58 of file mapplicationservice.cpp.

{
    Q_UNUSED(parameters);
    mDebug("MApplicationService") << "launch(QStringList)";

    // Default implementation: ignore the parameters and just launch()
    launch();
}

Here is the call graph for this function:

void MApplicationService::launch (  )  [virtual, slot]

Launch the application.

Returns:
void By default, this method will raise the application's window

If it is required that it launch another instance of this application, derive a class from MApplicationService and override launch() to call launchAnotherWithQProcess().

Definition at line 40 of file mapplicationservice.cpp.

{
    Q_D(MApplicationService);
    mDebug("MApplicationService") << "launch()";

    if (d->isPrestarted()) {
        d->releasePrestart();
    } else {
        if (d->activeWindowSet()) {
            mDebug("MApplicationService") << "launch() raising window";
            d->activateActiveWindow();
            d->raiseActiveWindow();
        } else {
            mDebug("MApplicationService") << "launch() no window to raise";
        }
    }
}

void MApplicationService::launchAnotherWithQProcess (  )  [virtual, slot]

launch another process using QProcess

Returns:
void

Definition at line 67 of file mapplicationservice.cpp.

{
    Q_D(MApplicationService);

    static bool firstCall = true;
    bool thisAppRunWithDBus = d->thisAppRunWithDBus();

    mDebug("MApplicationService") << "launch()";
    mDebug("MApplicationService") << "thisAppRunWithDBus = " << thisAppRunWithDBus;

    if (firstCall && thisAppRunWithDBus) {
        mDebug("MApplicationService") << "launch() : first dbus call so doing nothing";
    } else {
        QString binaryName = d->binaryName();
        QStringList arguments = d->arguments();

        arguments.removeFirst(); // remove program name

        d->launchNewProcess(binaryName, arguments);
    }

    firstCall = false;
}

QString MApplicationService::registeredName (  )  [virtual, slot]

return the registered service name

Returns:
registered service name

If you want no MApplicationIf, override this to return QString().

Definition at line 175 of file mapplicationservice.cpp.

{
    Q_D(MApplicationService);

    return d->serviceName;
}

bool MApplicationService::registerService (  )  [virtual, slot]

registers the service

Returns:
true is registration was successful

If you want no MApplicationIf, override this to always return true.

Definition at line 189 of file mapplicationservice.cpp.

{
    Q_D(MApplicationService);

    bool success = false;

    if (isRegistered()) {
        new MApplicationIfAdaptor(this);

        bool anotherAppRunning = !d->registerService(d->serviceName);
        if (anotherAppRunning) {
            mDebug("MApplicationService") << "registerService() registering failed";
            handleServiceRegistrationFailure();
            success = false;
        } else {
            d->unregisterObject("/org/maemo/m");
            d->registerObject("/org/maemo/m", this);
            success = true;
        }
    }

    return success;
}

Here is the call graph for this function:

void MApplicationService::setServiceName ( const QString serviceName  )  [slot]

sets the service name

Returns:
void

Definition at line 213 of file mapplicationservice.cpp.

{
    Q_D(MApplicationService);

    d->serviceName = newServiceName;
}


Member Data Documentation

MApplicationServicePrivate* const MApplicationService::d_ptr [protected]

Definition at line 170 of file corelib/core/mapplicationservice.h.


Copyright © 2010 Nokia Corporation Generated on Thu Nov 4 2010 18:14:24 (PDT)
Doxygen 1.7.1
MeeGo Touch