Add missing capability.cpp
[harmattan/cameraplus] / src / sailfish / orientation.cpp
1 /*!
2  * This file is part of CameraPlus.
3  *
4  * Copyright (C) 2012-2013 Mohammed Sameer <msameer@foolab.org>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19  */
20
21 #include "orientation.h"
22 #include <qmorientation.h>
23 #include <QDebug>
24
25 Orientation::Orientation(QObject *parent) :
26   QObject(parent),
27   m_orientation(new MeeGo::QmOrientation(this)),
28   m_direction(Unknown) {
29
30   QObject::connect(m_orientation, SIGNAL(orientationChanged(const MeeGo::QmOrientationReading&)),
31                    this, SLOT(onOrientationChanged(const MeeGo::QmOrientationReading&)));
32
33   if (m_orientation->requestSession(MeeGo::QmSensor::SessionTypeListen)
34       == MeeGo::QmSensor::SessionTypeNone) {
35     qDebug() << "Failed to get listen session:" << m_orientation->lastError();
36   }
37 }
38
39 Orientation::~Orientation() {
40   m_orientation->stop();
41 }
42
43 bool Orientation::isActive() const {
44   return m_orientation->isRunning();
45 }
46
47 void Orientation::setActive(bool active) {
48   if (active == isActive()) {
49     return;
50   }
51
52   if (active) {
53     m_orientation->start();
54     onOrientationChanged(m_orientation->orientation());
55   }
56   else {
57     m_orientation->stop();
58     m_direction = Unknown;
59
60     emit orientationChanged();
61   }
62
63   emit activeChanged();
64 }
65
66 Orientation::OrientationDirection Orientation::orientation() const {
67   return m_direction;
68 }
69
70 void Orientation::onOrientationChanged(const MeeGo::QmOrientationReading& value) {
71   OrientationDirection direction = Unknown;
72
73   switch (value.value) {
74   case MeeGo::QmOrientation::BottomUp:
75     direction = InvertedLandscape;
76     break;
77
78   case MeeGo::QmOrientation::BottomDown:
79     direction = Landscape;
80     break;
81
82   case MeeGo::QmOrientation::LeftUp:
83     direction = Portrait;
84     break;
85
86   case MeeGo::QmOrientation::RightUp:
87     direction = InvertedPortrait;
88     break;
89
90   default:
91     direction = Unknown;
92     break;
93   }
94
95   if (direction != m_direction) {
96     m_direction = direction;
97
98     emit orientationChanged();
99   }
100 }