Emit error signal when login fails
[situare] / src / engine / mceprivate.cpp
1 /*
2    Situare - A location system for Facebook
3    Copyright (C) 2010  Ixonos Plc. Authors:
4
5        Jussi Laitinen - jussi.laitinen@ixonos.com
6
7    Situare is free software; you can redistribute it and/or
8    modify it under the terms of the GNU General Public License
9    version 2 as published by the Free Software Foundation.
10
11    Situare 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
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with Situare; if not, write to the Free Software
18    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
19    USA.
20 */
21
22 #include <QString>
23 #include <QDebug>
24 #include <QTimer>
25 #include <QFile>
26 #include <QtXml/QDomDocument>
27
28 #include <mce/dbus-names.h>
29 #include <mce/mode-names.h>
30
31 #include "mce.h"
32 #include "mceprivate.h"
33
34 const int DISPLAY_STATE_INDEX = 0;
35
36 static QDBusConnection dBusConnection = QDBusConnection::systemBus();
37
38 MCEPrivate::MCEPrivate(QObject *parent)
39     : QObject(parent),
40       m_displayOn(true)
41 {
42     qDebug() << __PRETTY_FUNCTION__;
43
44     m_parent = static_cast<MCE*>(parent);
45
46     m_dBusInterface = new QDBusInterface(MCE_SERVICE, MCE_REQUEST_PATH,
47                                        MCE_REQUEST_IF, dBusConnection, this);
48
49     dBusConnection.connect(MCE_SERVICE, MCE_SIGNAL_PATH, MCE_SIGNAL_IF,
50                            MCE_DISPLAY_SIG, this, SLOT(displayStateChanged(const QDBusMessage &)));
51
52     m_dBusInterface->callWithCallback(MCE_DISPLAY_STATUS_GET, QList<QVariant>(), this,
53                                       SLOT(setDisplayState(QString)),
54                                       SLOT(displayStateError(QDBusError)));
55 }
56
57 void MCEPrivate::displayStateChanged(const QDBusMessage &message)
58 {
59     qDebug() << __PRETTY_FUNCTION__;
60
61     QString state = message.arguments().at(DISPLAY_STATE_INDEX).toString();
62     setDisplayState(state);
63 }
64
65 void MCEPrivate::displayStateError(const QDBusError &error)
66 {
67     qDebug() << __PRETTY_FUNCTION__;
68
69     Q_UNUSED(error);
70 }
71
72 bool MCEPrivate::isDisplayOn()
73 {
74     qDebug() << __PRETTY_FUNCTION__;
75
76     return m_displayOn;
77 }
78
79 void MCEPrivate::setDisplayState(const QString &state)
80 {
81     qDebug() << __PRETTY_FUNCTION__;
82
83     if (!state.isEmpty()) {
84         if (state == MCE_DISPLAY_ON_STRING) {
85             m_displayOn = true;
86             emit m_parent->displayOff(false);
87         }
88         else if (state == MCE_DISPLAY_OFF_STRING) {
89             m_displayOn = false;
90             emit m_parent->displayOff(true);
91         }
92     }
93 }
94
95 bool MCEPrivate::isTouchscreenVibrationEnabled()
96 {
97     QDomDocument doc;
98     QFile file("/var/lib/gconf/system/osso/dsm/vibra/\%gconf.xml");
99
100     if (!file.open(QIODevice::ReadOnly))
101         return false;
102     if (!doc.setContent(&file)) {
103         file.close();
104         return false;
105     }
106
107     file.close();
108
109     QDomNodeList domNodeList = doc.elementsByTagName("entry");
110
111     for (int i = 0; i < domNodeList.count(); ++i) {
112
113         QDomElement element = domNodeList.at(i).toElement();
114
115         if ((!element.isNull()) && (element.hasAttribute("name")) &&
116             (element.attribute("name") == "touchscreen_vibra_enabled")) {
117
118             return QVariant(element.attribute("value", "false")).toBool();
119         }
120     }
121
122     return false;
123 }
124
125 void MCEPrivate::stopVibration()
126 {
127     qDebug() << __PRETTY_FUNCTION__;
128
129     m_dBusInterface->call(MCE_ACTIVATE_VIBRATOR_PATTERN, "PatternPowerKeyPress");
130 }
131
132 void MCEPrivate::vibrationFeedback()
133 {
134     qDebug() << __PRETTY_FUNCTION__;
135
136     if (isTouchscreenVibrationEnabled()) {
137         m_dBusInterface->call(MCE_ACTIVATE_VIBRATOR_PATTERN, "PatternPowerKeyPress");
138
139         const int VIBRATION_TIME = 40;  //Vibration time in milliseconds
140         QTimer::singleShot(VIBRATION_TIME, this, SLOT(stopVibration()));
141     }
142 }