OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / share / qtcreator / qml / qmlobserver / deviceorientation_maemo5.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
35 #include <mce/mode-names.h>
36 #include <mce/dbus-names.h>
37
38 class MaemoOrientation : public DeviceOrientation
39 {
40     Q_OBJECT
41 public:
42     MaemoOrientation()
43         : o(UnknownOrientation), sensorEnabled(false)
44     {
45         resumeListening();
46         // connect to the orientation change signal
47         QDBusConnection::systemBus().connect(QString(), MCE_SIGNAL_PATH, MCE_SIGNAL_IF,
48                 MCE_DEVICE_ORIENTATION_SIG,
49                 this,
50                 SLOT(deviceOrientationChanged(QString)));
51     }
52
53     ~MaemoOrientation()
54     {
55         // disable the orientation sensor
56         QDBusConnection::systemBus().call(
57                 QDBusMessage::createMethodCall(MCE_SERVICE, MCE_REQUEST_PATH,
58                                                MCE_REQUEST_IF, MCE_ACCELEROMETER_DISABLE_REQ));
59     }
60
61     inline Orientation orientation() const
62     {
63         return o;
64     }
65
66     void setOrientation(Orientation o)
67     {
68     }
69
70     void pauseListening() {
71         if (sensorEnabled) {
72             // disable the orientation sensor
73             QDBusConnection::systemBus().call(
74                     QDBusMessage::createMethodCall(MCE_SERVICE, MCE_REQUEST_PATH,
75                                                    MCE_REQUEST_IF, MCE_ACCELEROMETER_DISABLE_REQ));
76             sensorEnabled = false;
77         }
78     }
79
80     void resumeListening() {
81         if (!sensorEnabled) {
82             // enable the orientation sensor
83             QDBusConnection::systemBus().call(
84                     QDBusMessage::createMethodCall(MCE_SERVICE, MCE_REQUEST_PATH,
85                                                    MCE_REQUEST_IF, MCE_ACCELEROMETER_ENABLE_REQ));
86
87             QDBusMessage reply = QDBusConnection::systemBus().call(
88                     QDBusMessage::createMethodCall(MCE_SERVICE, MCE_REQUEST_PATH,
89                                                    MCE_REQUEST_IF, MCE_DEVICE_ORIENTATION_GET));
90
91             if (reply.type() == QDBusMessage::ErrorMessage) {
92                 qWarning("Unable to retrieve device orientation: %s", qPrintable(reply.errorMessage()));
93             } else {
94                 Orientation orientation = toOrientation(reply.arguments().value(0).toString());
95                 if (o != orientation) {
96                     o = orientation;
97                     emit orientationChanged();
98                 }
99                 sensorEnabled = true;
100             }
101         }
102     }
103
104 private Q_SLOTS:
105     void deviceOrientationChanged(const QString &newOrientation)
106     {
107         o = toOrientation(newOrientation);
108
109         emit orientationChanged();
110 //        printf("%d\n", o);
111     }
112
113 private:
114     static Orientation toOrientation(const QString &nativeOrientation)
115     {
116         if (nativeOrientation == MCE_ORIENTATION_LANDSCAPE)
117             return Landscape;
118         else if (nativeOrientation == MCE_ORIENTATION_LANDSCAPE_INVERTED)
119             return LandscapeInverted;
120         else if (nativeOrientation == MCE_ORIENTATION_PORTRAIT)
121             return Portrait;
122         else if (nativeOrientation == MCE_ORIENTATION_PORTRAIT_INVERTED)
123             return PortraitInverted;
124         return UnknownOrientation;
125     }
126
127 private:
128     Orientation o;
129     bool sensorEnabled;
130 };
131
132 DeviceOrientation* DeviceOrientation::instance()
133 {
134     static MaemoOrientation *o = new MaemoOrientation;
135     return o;
136 }
137
138 #include "deviceorientation_maemo5.moc"