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