# kdecore Library API Documentation

Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Namespaces | Source Files

# KUniqueApplication Class Reference

Maintains only a single instance of a running application at a time. More...

`#include <kuniqueapplication.h>`

Inheritance diagram for KUniqueApplication:

[legend]
Collaboration diagram for KUniqueApplication:

[legend]
List of all members.

## Public Member Functions

| KUniqueApplication (bool allowStyles=true, bool GUIenabled=true, bool configUnique=false)
| KUniqueApplication (Display *display, Qt::HANDLE visual=0, Qt::HANDLE colormap=0, bool allowStyles=true, bool configUnique=false)
virtual | ~KUniqueApplication ()
bool | process (const QCString &fun, const QByteArray &data, QCString &replyType, QByteArray &replyData)
virtual int | newInstance ()
bool | restoringSession ()

## Static Public Member Functions

static void | addCmdLineOptions ()
static bool | start ()
static void | setHandleAutoStarted ()

## Protected Member Functions

virtual void | virtual_hook (int id, void *data)

## Detailed Description

Maintains only a single instance of a running application at a time.

If another instance is started, it will determine (via DCOP) whether it is the first instance or a second instance. If it is a second instance, it will forward on the information to the first instance and then quit.

The .desktop file for the application should state X-DCOP-ServiceType=Unique, see kapplication.h

If your application is used to open files, it should also support the --tempfile option (see KCmdLineArgs::addTempFileOption()), to delete tempfiles after use. Add X-KDE-HasTempFileOption=true to the .desktop file to indicate this.

See also:
  KApplication DCOPObject

Author:
  Preston Brown <pbrown@kde.org>

Definition at line 48 of file kuniqueapplication.h.

## Constructor & Destructor Documentation

KUniqueApplication::KUniqueApplication | ( | bool | allowStyles = `true`,
|  | bool | GUIenabled = `true`,
|  | bool | configUnique = `false`
| ) |

Constructor.

Takes command line arguments from KCmdLineArgs

Parameters:

  | allowStyles | Set to false to disable the loading on plugin based styles. This is only useful to applications that do not display a GUI normally. If you do create an application with `allowStyles` set to false it normally runs in the background but under special circumstances displays widgets. Call KApplication::enableStyles() before displaying any widgets.
  | GUIenabled | Set to false to disable all GUI stuff. This implies no styles either.
  | configUnique | If true, the uniqueness of the application will depend on the value of the "MultipleInstances" key in the "KDE" group of the application config file.

Definition at line 317 of file kuniqueapplication.cpp.

References QTimer::singleShot().

KUniqueApplication::KUniqueApplication | ( | Display * | display,
|  | Qt::HANDLE | visual = `0`,
|  | Qt::HANDLE | colormap = `0`,
|  | bool | allowStyles = `true`,
|  | bool | configUnique = `false`
| ) |

Constructor.

Takes command line arguments from KCmdLineArgs

Parameters:

  | display | Will be passed to Qt as the X display. The display must be valid and already opened.
  | visual | Pointer to the X11 visual that should be used by the application. If NULL, the default visual will be used instead.
  | colormap | The colormap that should be used by the application. If 0, the default colormap will be used instead.
  | allowStyles | Set to false to disable the loading on plugin based styles. This is only useful to applications that do not display a GUI normally. If you do create an application with `allowStyles` set to false it normally runs in the background but under special circumstances displays widgets. Call KApplication::enableStyles() before displaying any widgets.
  | configUnique | If true, the uniqueness of the application will depend on the value of the "MultipleInstances" key in the "KDE" group of the application config file.

Since:
  KDE 3.3

Definition at line 332 of file kuniqueapplication.cpp.

References QTimer::singleShot().

KUniqueApplication::~KUniqueApplication | ( |  | ) | `[virtual]`

| Destructor. Definition at line 348 of file kuniqueapplication.cpp.

## Member Function Documentation

void KUniqueApplication::addCmdLineOptions | ( |  | ) | `[static]`

| Adds command line options specific for KUniqueApplication. Should be called before calling KUniqueApplication constructor and / or start(). Reimplemented from KApplication. Definition at line 87 of file kuniqueapplication.cpp. References KCmdLineArgs::addCmdLineOptions(). Referenced by start().

bool KUniqueApplication::start | ( |  | ) | `[static]`

Forks and registers with dcop.

The command line arguments are being sent via DCOP to newInstance() and will be received once the application enters the event loop.

Typically this is used like:

     int main(int argc, char **argv) {
        KAboutData about("myappname", "myAppName", .....);
        KCmdLineArgs::init(argc, argv, &about);
        KCmdLineArgs::addCmdLineOptions( myCmdOptions );
        KUniqueApplication::addCmdLineOptions();

        if (!KUniqueApplication::start()) {
           fprintf(stderr, "myAppName is already running!\n");
           exit(0);
        }
        KUniqueApplication a;
        a.exec();
     }

Note that it's not necessary to call start() explicitly. It will be called automatically before creating KUniqueApplication if it hasn't been called yet, without any performance impact.

Returns:
  true if registration is successful. false if another process was already running.

Definition at line 93 of file kuniqueapplication.cpp.

References addCmdLineOptions(), KStartupInfoData::addPid(), QCString::append(), KAboutData::appName(), DCOPClient::attach(), DCOPClient::call(), KStartupInfo::currentStartupIdEnv(), KApplication::dcopClient(), endl(), QApplication::exit(), DCOPClient::isApplicationRegistered(), QCString::isEmpty(), KCmdLineArgs::isSet(), KCmdLineArgs::parsedArgs(), DCOPClient::registerAs(), KStartupInfo::sendChangeX(), KStartupInfo::sendFinishX(), QCString::setNum(), and DCOPClient::setPriorityCall().

bool KUniqueApplication::process | ( | const QCString & | fun,
|  | const QByteArray & | data,
|  | QCString & | replyType,
|  | QByteArray & | replyData
| ) | `[virtual]`

Dispatches any incoming DCOP message for a new instance.

If it is not a request for a new instance, return false. Overloaded from DCOPObject to make sure that the application stays unique.

Parameters:

  | fun | DCOP function signature
  | data | the data for the arguments
  | replyType | the type of the reply value
  | replyData | the reply

See also:
  DCOPObject

Reimplemented from DCOPObject.

Definition at line 393 of file kuniqueapplication.cpp.

References DCOPObject::process().

int KUniqueApplication::newInstance | ( |  | ) | `[virtual]`

Creates a new "instance" of the application.

Usually this will involve making some calls into the GUI portion of your application asking for a new window to be created, possibly with some data already loaded based on the arguments received.

Command line arguments have been passed to KCmdLineArgs before this function is called and can be checked in the usual way.

The default implementation ensures the mainwindow of the already running instance is shown and activated if necessary. You should prefer using it from your overridden method instead of doing it directly.

Note that newInstance() is called also in the first started application process.

Returns:
  An exit value. The calling process will exit with this value.

Definition at line 466 of file kuniqueapplication.cpp.

References QApplication::mainWidget(), and KStartupInfo::setNewStartupId().

bool KUniqueApplication::restoringSession | ( |  | ) |

Returns whether newInstance() is being called while session restoration is in progress.

Since:
  KDE 3.3

Definition at line 461 of file kuniqueapplication.cpp.

References KApplication::isRestored().

The documentation for this class was generated from the following files:

- kuniqueapplication.h
- kuniqueapplication.cpp

This file is part of the documentation for kdecore Library Version 3.4.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat Jun 11 20:19:10 2005 by doxygen 1.4.1 written by Dimitri van Heesch, © 1997-2003