OSDN Git Service

bdcef90b9520069a150305542311590476eeaadd
[qt-creator-jp/qt-creator-jp.git] / src / shared / qtsingleapplication / qtsingleapplication.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights.  These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "qtsingleapplication.h"
35 #include "qtlocalpeer.h"
36
37 #include <QtGui/QWidget>
38 #include <QtGui/QFileOpenEvent>
39
40 namespace SharedTools {
41
42 void QtSingleApplication::sysInit(const QString &appId)
43 {
44     actWin = 0;
45     peer = new QtLocalPeer(this, appId);
46     connect(peer, SIGNAL(messageReceived(const QString&)), SIGNAL(messageReceived(const QString&)));
47 }
48
49
50 QtSingleApplication::QtSingleApplication(int &argc, char **argv, bool GUIenabled)
51     : QApplication(argc, argv, GUIenabled)
52 {
53     sysInit();
54 }
55
56
57 QtSingleApplication::QtSingleApplication(const QString &appId, int &argc, char **argv)
58     : QApplication(argc, argv)
59 {
60     sysInit(appId);
61 }
62
63
64 QtSingleApplication::QtSingleApplication(int &argc, char **argv, Type type)
65     : QApplication(argc, argv, type)
66 {
67     sysInit();
68 }
69
70
71 #if defined(Q_WS_X11)
72 QtSingleApplication::QtSingleApplication(Display* dpy, Qt::HANDLE visual, Qt::HANDLE colormap)
73     : QApplication(dpy, visual, colormap)
74 {
75     sysInit();
76 }
77
78 QtSingleApplication::QtSingleApplication(Display *dpy, int &argc, char **argv, Qt::HANDLE visual, Qt::HANDLE cmap)
79     : QApplication(dpy, argc, argv, visual, cmap)
80 {
81     sysInit();
82 }
83
84 QtSingleApplication::QtSingleApplication(Display* dpy, const QString &appId,
85     int argc, char **argv, Qt::HANDLE visual, Qt::HANDLE colormap)
86     : QApplication(dpy, argc, argv, visual, colormap)
87 {
88     sysInit(appId);
89 }
90 #endif
91
92 bool QtSingleApplication::event(QEvent *event)
93 {
94     if (event->type() == QEvent::FileOpen) {
95         QFileOpenEvent *foe = static_cast<QFileOpenEvent*>(event);
96         emit fileOpenRequest(foe->file());
97         return true;
98     }
99     return QApplication::event(event);
100 }
101
102 bool QtSingleApplication::isRunning()
103 {
104     return peer->isClient();
105 }
106
107 bool QtSingleApplication::sendMessage(const QString &message, int timeout)
108 {
109     return peer->sendMessage(message, timeout);
110 }
111
112
113 QString QtSingleApplication::id() const
114 {
115     return peer->applicationId();
116 }
117
118
119 void QtSingleApplication::setActivationWindow(QWidget *aw, bool activateOnMessage)
120 {
121     actWin = aw;
122     if (activateOnMessage)
123         connect(peer, SIGNAL(messageReceived(QString)), this, SLOT(activateWindow()));
124     else
125         disconnect(peer, SIGNAL(messageReceived(QString)), this, SLOT(activateWindow()));
126 }
127
128
129 QWidget* QtSingleApplication::activationWindow() const
130 {
131     return actWin;
132 }
133
134
135 void QtSingleApplication::activateWindow()
136 {
137     if (actWin) {
138         actWin->setWindowState(actWin->windowState() & ~Qt::WindowMinimized);
139         actWin->raise();
140         actWin->activateWindow();
141     }
142 }
143
144 } // namespace SharedTools