root/middleware-offline/trunk/_src/eidmw/eidgui/qtsingleapplication_mac.cpp @ 5

Revision 5, 4.0 KB (checked in by vsilva, 9 years ago)

Initial comit

Line 
1/****************************************************************************
2**
3** Copyright (C) 2003-2007 Trolltech ASA. All rights reserved.
4**
5** This file is part of a Qt Solutions component.
6**
7** Licensees holding a valid Qt Solutions License Agreement may use this
8** file in accordance with the rights, responsibilities, and obligations
9** contained therein. Please consult your licensing agreement or contact
10** sales@trolltech.com if any conditions of this licensing are not clear
11** to you.
12**
13** Further information about Qt Solutions licensing is available at:
14** http://www.trolltech.com/products/qt/addon/solutions/
15** or by contacting info@trolltech.com.
16**
17** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
18** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19**
20****************************************************************************/
21#include <QtCore/QString>
22#include <Carbon/Carbon.h>
23
24#include "qtsingleapplication.h"
25
26CFStringRef qstring2cfstring(const QString &str)
27{
28    return CFStringCreateWithCharacters(0, (UniChar *)str.unicode(), str.length());
29}
30
31QString cfstring2qstring(CFStringRef str)
32{
33    if(!str)
34        return QString();
35
36    CFIndex length = CFStringGetLength(str);
37    if(const UniChar *chars = CFStringGetCharactersPtr(str))
38        return QString((QChar *)chars, length);
39    UniChar *buffer = (UniChar*)malloc(length * sizeof(UniChar));
40    CFStringGetCharacters(str, CFRangeMake(0, length), buffer);
41    QString ret((QChar *)buffer, length);
42    free(buffer);
43    return ret;
44}
45
46class QtSingletonSysPrivate
47{
48public:
49    inline QtSingletonSysPrivate() { port = 0; }
50    CFMessagePortRef port;
51};
52
53class QtSingletonPrivate
54{
55};
56
57bool QtSingleApplication::isRunning() const
58{
59    CFStringRef cfstr = qstring2cfstring(id());
60    CFMessagePortRef myport = CFMessagePortCreateRemote(kCFAllocatorDefault, cfstr);
61    CFRelease(cfstr);
62    return (myport != 0);
63}
64
65void QtSingleApplication::sysInit()
66{
67    sysd = new QtSingletonSysPrivate;
68}
69
70void QtSingleApplication::sysCleanup()
71{
72    delete sysd;
73}
74
75CFDataRef MyCallBack(CFMessagePortRef, SInt32, CFDataRef data, void *info)
76{
77    CFIndex index = CFDataGetLength(data);
78    const UInt8 *p = CFDataGetBytePtr(data);
79    QByteArray ba(index, 0);
80    for (int i = 0; i < index; ++i)
81        ba[i] = p[i];
82    QString message(ba);
83    emit static_cast<QtSingleApplication *>(info)->messageReceived(message);
84    return 0;
85}
86
87void QtSingleApplication::initialize(bool activate)
88{
89    CFStringRef cfstr = qstring2cfstring(id());
90    CFMessagePortContext context;
91    context.version = 0;
92    context.info = this;
93    context.retain = 0;
94    context.release = 0;
95    context.copyDescription = 0;
96    sysd->port = CFMessagePortCreateLocal(kCFAllocatorDefault, cfstr, MyCallBack, &context, 0);
97    CFRunLoopRef runloop = CFRunLoopGetCurrent();
98    if (sysd->port && runloop) {
99        CFRunLoopSourceRef source = CFMessagePortCreateRunLoopSource(0, sysd->port, 0);
100        if (source)
101            CFRunLoopAddSource(runloop, source, kCFRunLoopCommonModes);
102        CFRelease(source);
103    }
104    CFRelease(cfstr);
105    if (activate) {
106        connect(this, SIGNAL(messageReceived(const QString&)),
107                this, SLOT(activateWindow()));
108    }
109}
110
111bool QtSingleApplication::sendMessage(const QString &message, int timeout)
112{
113    CFStringRef cfstr = qstring2cfstring(id());
114    CFMessagePortRef myport = CFMessagePortCreateRemote(kCFAllocatorDefault, cfstr);
115    CFRelease(cfstr);
116    if (!myport)
117        return false;
118    static SInt32 msgid = 0;
119    const QByteArray latin1Message = message.toLatin1();
120    CFDataRef data = CFDataCreate(0, (UInt8*)latin1Message.constData(), latin1Message.length());
121    CFDataRef reply = 0;
122    SInt32 result = CFMessagePortSendRequest(myport, ++msgid, data, timeout / 1000,
123                                             timeout / 1000, 0, &reply);
124    CFRelease(data);
125    if (reply)
126        CFRelease(reply);
127    return result == kCFMessagePortSuccess;
128
129}
Note: See TracBrowser for help on using the browser.