I found more as on way..
one is subclass QApplication
/* target find if bool Apps_Open become true */
windowTitle
= QString ( "Layer edit sample" );
Apps_Open = false; /* default not open */
const QString single_key
= QString("ID 29385129512818123 by %1").
arg(windowTitle
);
#if defined Q_WS_WIN
m_mutex = (void *) CreateMutexW( 0, false, (LPCWSTR) single_key.utf16() );
m_prevInstance = ( GetLastError() == ERROR_ALREADY_EXISTS );
if (m_prevInstance) {
Apps_Open = true;
}
#endif
#ifdef Q_OS_UNIX
unix_sock = make_local_app_socket();
Apps_Open = unix_sock == 0 ? true : false;
qDebug() << "### sock connect res " << Apps_Open << unix_sock;
#endif
/* on destroy ~QApplication remove socked file */
/* target find if bool Apps_Open become true */
windowTitle = QString ( "Layer edit sample" );
Apps_Open = false; /* default not open */
const QString single_key = QString("ID 29385129512818123 by %1").arg(windowTitle);
#if defined Q_WS_WIN
m_mutex = (void *) CreateMutexW( 0, false, (LPCWSTR) single_key.utf16() );
m_prevInstance = ( GetLastError() == ERROR_ALREADY_EXISTS );
if (m_prevInstance) {
Apps_Open = true;
}
#endif
#ifdef Q_OS_UNIX
unix_sock = make_local_app_socket();
Apps_Open = unix_sock == 0 ? true : false;
qDebug() << "### sock connect res " << Apps_Open << unix_sock;
#endif
/* on destroy ~QApplication remove socked file */
To copy to clipboard, switch view to plain text mode
unix socked connect function ...
#ifdef Q_OS_UNIX
#include <stddef.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
/* Unix SOCKET to comunicate on parent apps friend and send file or other msg! */
#define _UNIX_SOCKET_ "/tmp/layeres.sock"
int make_local_app_socket()
{
struct sockaddr_un name;
int sock;
size_t size;
sock = socket (PF_LOCAL, SOCK_DGRAM, 0);
if (sock < 0)
{
return 0;
}
name.sun_family = AF_LOCAL;
strncpy (name.sun_path, _UNIX_SOCKET_, sizeof (name.sun_path));
name.sun_path[sizeof (name.sun_path) - 1] = '\0';
size = (offsetof (struct sockaddr_un, sun_path) + strlen (name.sun_path) + 1);
if (bind (sock, (struct sockaddr *) &name, size) < 0)
{
return 0;
}
return sock;
}
#endif
#ifdef Q_OS_UNIX
#include <stddef.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
/* Unix SOCKET to comunicate on parent apps friend and send file or other msg! */
#define _UNIX_SOCKET_ "/tmp/layeres.sock"
int make_local_app_socket()
{
struct sockaddr_un name;
int sock;
size_t size;
sock = socket (PF_LOCAL, SOCK_DGRAM, 0);
if (sock < 0)
{
return 0;
}
name.sun_family = AF_LOCAL;
strncpy (name.sun_path, _UNIX_SOCKET_, sizeof (name.sun_path));
name.sun_path[sizeof (name.sun_path) - 1] = '\0';
size = (offsetof (struct sockaddr_un, sun_path) + strlen (name.sun_path) + 1);
if (bind (sock, (struct sockaddr *) &name, size) < 0)
{
return 0;
}
return sock;
}
#endif
To copy to clipboard, switch view to plain text mode
Focus to preview apps if is Apps_Open having true
/* focus preview process ! */
bool FocusInstance() const
{
#if defined Q_WS_WIN
HWND hWnd = FindWindowW( 0, (LPCWSTR) windowTitle.utf16() );
if( hWnd )
{
long id = GetWindowThreadProcessId( hWnd, 0 );
AttachThreadInput( id, 0, true );
if ( IsIconic( hWnd ) )
ShowWindow( hWnd, SW_RESTORE );
SetForegroundWindow( hWnd );
AttachThreadInput( id, 0, false );
return true;
}
#endif
#ifdef Q_OS_UNIX
qDebug() << "### having " << sockets.exists();
if (unix_sock == 0) {
qDebug() << "### send file to socket parent here... ";
return true;
}
#endif
return false;
}
/* focus preview process ! */
bool FocusInstance() const
{
#if defined Q_WS_WIN
HWND hWnd = FindWindowW( 0, (LPCWSTR) windowTitle.utf16() );
if( hWnd )
{
long id = GetWindowThreadProcessId( hWnd, 0 );
AttachThreadInput( id, 0, true );
if ( IsIconic( hWnd ) )
ShowWindow( hWnd, SW_RESTORE );
SetForegroundWindow( hWnd );
AttachThreadInput( id, 0, false );
return true;
}
#endif
#ifdef Q_OS_UNIX
QFileInfo sockets(QString(_UNIX_SOCKET_));
qDebug() << "### having " << sockets.exists();
if (unix_sock == 0) {
qDebug() << "### send file to socket parent here... ";
return true;
}
#endif
return false;
}
To copy to clipboard, switch view to plain text mode
All in one ...
http://fop-miniscribus.googlecode.co...wEdit/main.cpp
On mac i not having a solution
IMO one commercial QT version a QtSingleApplication is inside ... but i not know if you can send file or message to the open apps by unix socked is possibel
http://www.google.com/codesearch having a copy of sample commercial "QtSingleApplication"
Bookmarks