OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / share / qtcreator / qml / qmlobserver / deviceorientation_harmattan.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 (info@qt.nokia.com)
8 **
9 ** GNU Lesser General Public License Usage
10 **
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
14 ** Please review the following information to ensure the GNU Lesser General
15 ** Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 **
22 ** Other Usage
23 **
24 ** Alternatively, this file may be used in accordance with the terms and
25 ** conditions contained in a signed written agreement between you and Nokia.
26 **
27 ** If you have questions regarding the use of this file, please contact
28 ** Nokia at qt-info@nokia.com.
29 **
30 **************************************************************************/
31
32 #include "deviceorientation.h"
33 #include <QtDBus>
34 #include <QDebug>
35
36 #define ORIENTATION_SERVICE "com.nokia.SensorService"
37 #define ORIENTATION_PATH "/org/maemo/contextkit/Screen/TopEdge"
38 #define CONTEXT_INTERFACE "org.maemo.contextkit.Property"
39 #define CONTEXT_CHANGED "ValueChanged"
40 #define CONTEXT_SUBSCRIBE "Subscribe"
41 #define CONTEXT_UNSUBSCRIBE "Unsubscribe"
42 #define CONTEXT_GET "Get"
43
44
45 class HarmattanOrientation : public DeviceOrientation
46 {
47     Q_OBJECT
48 public:
49     HarmattanOrientation()
50         : o(UnknownOrientation), sensorEnabled(false)
51     {
52         resumeListening();
53         // connect to the orientation change signal
54         bool ok = QDBusConnection::systemBus().connect(ORIENTATION_SERVICE, ORIENTATION_PATH,
55                 CONTEXT_INTERFACE,
56                 CONTEXT_CHANGED,
57                 this,
58                 SLOT(deviceOrientationChanged(QList<QVariant>,quint64)));
59 //        qDebug() << "connection OK" << ok;
60         QDBusMessage reply = QDBusConnection::systemBus().call(
61                 QDBusMessage::createMethodCall(ORIENTATION_SERVICE, ORIENTATION_PATH,
62                                                CONTEXT_INTERFACE, CONTEXT_GET));
63         if (reply.type() != QDBusMessage::ErrorMessage) {
64             QList<QVariant> args;
65             qvariant_cast<QDBusArgument>(reply.arguments().at(0)) >> args;
66             deviceOrientationChanged(args, 0);
67         }
68     }
69
70     ~HarmattanOrientation()
71     {
72         // unsubscribe from the orientation sensor
73         if (sensorEnabled)
74             QDBusConnection::systemBus().call(
75                 QDBusMessage::createMethodCall(ORIENTATION_SERVICE, ORIENTATION_PATH,
76                                                CONTEXT_INTERFACE, CONTEXT_UNSUBSCRIBE));
77     }
78
79     inline Orientation orientation() const
80     {
81         return o;
82     }
83
84     void setOrientation(Orientation)
85     {
86     }
87
88     void pauseListening() {
89         if (sensorEnabled) {
90             // unsubscribe from the orientation sensor
91             QDBusConnection::systemBus().call(
92                     QDBusMessage::createMethodCall(ORIENTATION_SERVICE, ORIENTATION_PATH,
93                                                    CONTEXT_INTERFACE, CONTEXT_UNSUBSCRIBE));
94             sensorEnabled = false;
95         }
96     }
97
98     void resumeListening() {
99         if (!sensorEnabled) {
100             // subscribe to the orientation sensor
101             QDBusMessage reply = QDBusConnection::systemBus().call(
102                     QDBusMessage::createMethodCall(ORIENTATION_SERVICE, ORIENTATION_PATH,
103                                                    CONTEXT_INTERFACE, CONTEXT_SUBSCRIBE));
104
105             if (reply.type() == QDBusMessage::ErrorMessage) {
106                 qWarning("Unable to retrieve device orientation: %s", qPrintable(reply.errorMessage()));
107             } else {
108                 sensorEnabled = true;
109             }
110         }
111     }
112
113 private Q_SLOTS:
114     void deviceOrientationChanged(QList<QVariant> args,quint64)
115     {
116         if (args.count() == 0)
117             return;
118         Orientation newOrientation = toOrientation(args.at(0).toString());
119         if (newOrientation != o) {
120             o = newOrientation;
121             emit orientationChanged();
122         }
123 //        qDebug() << "orientation" << args.at(0).toString();
124     }
125
126 private:
127     static Orientation toOrientation(const QString &nativeOrientation)
128     {
129         if (nativeOrientation == "top")
130             return Landscape;
131         else if (nativeOrientation == "left")
132             return Portrait;
133         else if (nativeOrientation == "bottom")
134             return LandscapeInverted;
135         else if (nativeOrientation == "right")
136             return PortraitInverted;
137         return UnknownOrientation;
138     }
139
140 private:
141     Orientation o;
142     bool sensorEnabled;
143 };
144
145 DeviceOrientation* DeviceOrientation::instance()
146 {
147     static HarmattanOrientation *o = new HarmattanOrientation;
148     return o;
149 }
150
151 #include "deviceorientation_harmattan.moc"