Remove "All rights reserved" line from license headers.
[profile/ivi/qtdeclarative.git] / tools / qmlviewer / deviceorientation_harmattan.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the tools applications of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
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 QStringLiteral("com.nokia.SensorService")
47 #define ORIENTATION_PATH QStringLiteral("/org/maemo/contextkit/Screen/TopEdge")
48 #define CONTEXT_INTERFACE QStringLiteral("org.maemo.contextkit.Property")
49 #define CONTEXT_CHANGED QStringLiteral("ValueChanged")
50 #define CONTEXT_SUBSCRIBE QStringLiteral("Subscribe")
51 #define CONTEXT_UNSUBSCRIBE QStringLiteral("Unsubscribe")
52 #define CONTEXT_GET QStringLiteral("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 == QLatin1String("top"))
140             return Landscape;
141         else if (nativeOrientation == QLatin1String("left"))
142             return Portrait;
143         else if (nativeOrientation == QLatin1String("bottom"))
144             return LandscapeInverted;
145         else if (nativeOrientation == QLatin1String("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"