Implemented entry into power saving state when application is in background,
authorlampehe-local <henri.lampela@ixonos.com>
Thu, 8 Jul 2010 13:03:25 +0000 (16:03 +0300)
committerlampehe-local <henri.lampela@ixonos.com>
Thu, 8 Jul 2010 13:03:25 +0000 (16:03 +0300)
menu or some application dialog is open too long.
Added doxygen comment for setAvatarImage

Reviewed by: Jussi Laitinen

src/application.cpp [new file with mode: 0644]
src/application.h [new file with mode: 0644]
src/engine/engine.cpp
src/engine/engine.h
src/engine/mce.h
src/engine/mceprivate.cpp
src/gps/gpspositionprivateliblocation.cpp
src/main.cpp
src/src.pro
src/ui/friendlistitem.h

diff --git a/src/application.cpp b/src/application.cpp
new file mode 100644 (file)
index 0000000..f474d18
--- /dev/null
@@ -0,0 +1,108 @@
+/*
+   Situare - A location system for Facebook
+   Copyright (C) 2010  Ixonos Plc. Authors:
+
+      Henri Lampela - henri.lampela@ixonos.com
+
+   Situare is free software; you can redistribute it and/or
+   modify it under the terms of the GNU General Public License
+   version 2 as published by the Free Software Foundation.
+
+   Situare is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with Situare; if not, write to the Free Software
+   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
+   USA.
+*/
+
+#include <QX11Info>
+#include <QDebug>
+
+#include "application.h"
+
+#include <X11/Xlib.h>
+#include <X11/Xatom.h>
+#include <X11/Xutil.h>
+
+#ifdef Q_WS_HILDON
+    #define ACTIVE_APP_ATOM "_MB_CURRENT_APP_WINDOW"
+#else
+    #define ACTIVE_APP_ATOM "_NET_ACTIVE_WINDOW"
+#endif
+
+Application::Application(int &argc, char **argv)
+    : QApplication(argc, argv),
+    m_topmost(false)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+}
+
+void Application::registerWindow(WId window)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+   m_windows.append(window);
+}
+
+void Application::unregisterWindow(WId window)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    m_windows.removeOne(window);
+}
+
+WId Application::activeDesktopWindow(Display *display, WId rootWindow)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    Atom realType;
+    int format, status;
+    unsigned long n, extra;
+    unsigned char *data = 0;
+    WId activeWindow;
+    Atom atom = XInternAtom(display, ACTIVE_APP_ATOM, false);
+
+    status = XGetWindowProperty(display, rootWindow, atom, 0L, 16L,
+                        0, XA_WINDOW, &realType, &format,
+                        &n, &extra, &data);
+    if (status == Success && realType == XA_WINDOW &&
+        format == 32 && n == 1 && data != NULL)
+        activeWindow = ((WId*)data)[0];
+    else
+        activeWindow = None;
+
+    if (data != 0)
+        XFree(data);
+
+    return activeWindow;
+}
+
+bool Application::x11EventFilter(XEvent *event)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    static Qt::HANDLE rootWindow = QX11Info::appRootWindow();
+    static Atom activeAppAtom = XInternAtom(QX11Info::display(),
+                                            ACTIVE_APP_ATOM, false);
+
+    if (event->type == PropertyNotify) {
+        XPropertyEvent *pevent = (XPropertyEvent*)event;
+        if (pevent->window == rootWindow) {
+            if (pevent->atom == activeAppAtom) {
+                WId activeWindow = activeDesktopWindow(pevent->display,
+                                                       rootWindow);
+
+                if(m_topmost != m_windows.contains(activeWindow)) {
+                    m_topmost = !m_topmost;
+                    qWarning() << "Application is topmost " << m_topmost;
+                    emit topmostChanged(!m_topmost);
+                }
+            }
+        }
+    }
+    return false;
+}
diff --git a/src/application.h b/src/application.h
new file mode 100644 (file)
index 0000000..4c6deff
--- /dev/null
@@ -0,0 +1,95 @@
+/*
+   Situare - A location system for Facebook
+   Copyright (C) 2010  Ixonos Plc. Authors:
+
+      Henri Lampela - henri.lampela@ixonos.com
+
+   Situare is free software; you can redistribute it and/or
+   modify it under the terms of the GNU General Public License
+   version 2 as published by the Free Software Foundation.
+
+   Situare is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with Situare; if not, write to the Free Software
+   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
+   USA.
+*/
+
+#ifndef APPLICATION_H
+#define APPLICATION_H
+
+#include <QApplication>
+
+/**
+* @brief Application class is a subclass of QApplication
+*
+* This class handles X11 events
+*/
+class Application : public QApplication
+{
+    Q_OBJECT
+public:
+
+    /**
+    * @brief Constructor
+    *
+    * @param argc number of arguments
+    * @param argv arguments
+    */
+    Application(int &argc, char **argv);
+
+    /**
+    * @brief Registers window
+    *
+    * @param wId window id
+    */
+    void registerWindow(WId wId);
+
+    /**
+    * @brief Unregisters window
+    *
+    * @param wId window id
+    */
+    void unregisterWindow(WId wId);
+
+private:
+
+    /**
+    * @brief Return active window
+    *
+    * @param display display
+    * @param rootWindow rootwindow
+    * @return WId window id
+    */
+    WId activeDesktopWindow(Display *display, WId rootWindow);
+
+    /**
+    * @brief Event filter for window
+    *
+    * @param event window event
+    * @return bool Return true if you want to stop the event from being processed.
+    *         Return false for normal event dispatching
+    */
+    bool x11EventFilter(XEvent *event);
+
+signals:
+
+    /**
+    * @brief Signals when window state is changed
+    *
+    * @param topmost true for topmost either false
+    */
+    void topmostChanged(bool topmost);
+
+private:
+
+    bool m_topmost; ///< Flag for topmost
+
+    QList<WId> m_windows; ///< List of window ids
+};
+
+#endif // APPLICATION_H
index 289a578..1fe7989 100644 (file)
@@ -25,6 +25,9 @@
 #include <QMessageBox>
 #include <QNetworkReply>
 
+#ifdef Q_WS_MAEMO_5
+#include "application.h"
+#endif
 #include "common.h"
 #include "facebookservice/facebookauthentication.h"
 #include "gps/gpsposition.h"
@@ -44,9 +47,8 @@ const qreal USER_MOVEMENT_MINIMUM_LONGITUDE_DIFFERENCE = 0.003;///< Min value fo
 const qreal USER_MOVEMENT_MINIMUM_LATITUDE_DIFFERENCE = 0.001;///< Min value for user move longitude
 const int MIN_UPDATE_INTERVAL_MSECS = 5*60*1000;
 
-SituareEngine::SituareEngine(QMainWindow *parent)
-    : QObject(parent),
-      m_autoCenteringEnabled(false),
+SituareEngine::SituareEngine()
+    : m_autoCenteringEnabled(false),
       m_automaticUpdateFirstStart(true),
       m_automaticUpdateRequest(false),
       m_userMoved(false),
@@ -54,9 +56,18 @@ SituareEngine::SituareEngine(QMainWindow *parent)
       m_lastUpdatedGPSPosition(QPointF())
 {
     qDebug() << __PRETTY_FUNCTION__;
+
     m_ui = new MainWindow;
     m_ui->updateItemVisibility();
 
+#ifdef Q_WS_MAEMO_5
+    m_app = static_cast<Application *>(qApp);
+    m_app->registerWindow(m_ui->winId());
+
+    connect(m_app, SIGNAL(topmostChanged(bool)),
+            this, SLOT(enablePowerSave(bool)));
+#endif
+
     m_networkAccessManager = NetworkAccessManager::instance();
 
     // build MapEngine
@@ -112,7 +123,7 @@ SituareEngine::SituareEngine(QMainWindow *parent)
     initializeGpsAndAutocentering();
 
     m_mce = new MCE(this);
-    connect(m_mce, SIGNAL(displayStateChanged(bool)), this, SLOT(displayStateChanged(bool)));
+    connect(m_mce, SIGNAL(displayOff(bool)), this, SLOT(enablePowerSave(bool)));
 }
 
 SituareEngine::~SituareEngine()
@@ -142,16 +153,6 @@ void SituareEngine::disableAutoCentering()
     m_ui->buildInformationBox(tr("Auto centering disabled"));
 }
 
-void SituareEngine::displayStateChanged(bool enabled)
-{
-    qDebug() << __PRETTY_FUNCTION__;
-
-    m_gps->enablePowerSave(!enabled);
-
-    if (m_autoCenteringEnabled)
-        enableAutoCentering(enabled);
-}
-
 void SituareEngine::enableAutoCentering(bool enabled)
 {
     qDebug() << __PRETTY_FUNCTION__;
@@ -227,6 +228,16 @@ void SituareEngine::enableAutomaticLocationUpdate(bool enabled, int updateInterv
     }
 }
 
+void SituareEngine::enablePowerSave(bool enabled)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    m_gps->enablePowerSave(enabled);
+
+    if(m_autoCenteringEnabled)
+        m_mapEngine->setAutoCentering(!enabled);
+}
+
 void SituareEngine::error(const int context, const int error)
 {
     qDebug() << __PRETTY_FUNCTION__;
index bf88208..9abeab1 100644 (file)
@@ -30,8 +30,9 @@
 #include <QTime>
 #include <QPointF>
 
-class QMainWindow;
-
+#ifdef Q_WS_MAEMO_5
+class Application;
+#endif
 class FacebookAuthentication;
 class FacebookCredentials;
 class GPSPosition;
@@ -59,7 +60,7 @@ public:
     *
     * @param parent
     */
-    SituareEngine(QMainWindow *parent = 0);
+    SituareEngine();
 
     /**
     * @brief Destructor
@@ -193,13 +194,6 @@ private slots:
     void disableAutoCentering();
 
     /**
-    * @brief Slot for display state changed.
-    *
-    * @param enabled true if enabled, false otherwise
-    */
-    void displayStateChanged(bool enabled);
-
-    /**
     * @brief Slot for auto centering enabling.
     *
     * Calls gps to send last known position
@@ -224,6 +218,13 @@ private slots:
     void enableAutomaticLocationUpdate(bool enabled, int updateIntervalMsecs = 0);
 
     /**
+    * @brief Slot for enabling power saving.
+    *
+    * @param enabled true if enabled, false otherwise
+    */
+    void enablePowerSave(bool enabled);
+
+    /**
     * @brief Slot to intercept signal when user's/friend's image is downloaded
     *
     * @param user Instance of user/friend
@@ -302,7 +303,9 @@ private:
     bool m_automaticUpdateRequest;      ///< Flag for automatic update request
     bool m_userMoved;                   ///< Flag for user move
 
-
+#ifdef Q_WS_MAEMO_5
+    Application *m_app;                              ///< Pointer to Application
+#endif
     FacebookAuthentication *m_facebookAuthenticator; ///< Instance for facebook authenticator
     GPSPosition *m_gps;                              ///< Instance of the gps position
     MainWindow *m_ui;                                ///< Instance of the MainWindow UI
@@ -313,6 +316,7 @@ private:
 
     QTimer *m_automaticUpdateIntervalTimer; ///< Automatic update interval timer
     QPointF m_lastUpdatedGPSPosition;       ///< Last updated GPS position
+
 };
 
 #endif // ENGINE_H
index 637d5ff..d07f8d8 100644 (file)
@@ -64,11 +64,11 @@ public:
  ******************************************************************************/
 signals:
     /**
-    * @brief Signal for the display state change.
+    * @brief Signal for the display off.
     *
-    * @param on true if on, false if off
+    * @param on true if off, false if on
     */
-    void displayStateChanged(bool on);
+    void displayOff(bool off);
 
 /*******************************************************************************
  * DATA MEMBERS
index e1e026a..1cd3141 100644 (file)
@@ -81,11 +81,11 @@ void MCEPrivate::setDisplayState(const QString &state)
     if (!state.isEmpty()) {
         if (state == MCE_DISPLAY_ON_STRING) {
             m_displayOn = true;
-            emit m_parent->displayStateChanged(true);
+            emit m_parent->displayOff(false);
         }
         else if (state == MCE_DISPLAY_OFF_STRING) {
             m_displayOn = false;
-            emit m_parent->displayStateChanged(false);
+            emit m_parent->displayOff(true);
         }
     }
 }
index 9d5cceb..cd8bf80 100644 (file)
@@ -31,7 +31,7 @@
 #include "gpspositionprivateliblocation.h"
 #include "liblocationwrapper.h"
 
-const int POWER_SAVE_START_DELAY_MS = 1000*10;
+const int POWER_SAVE_START_DELAY_MS = 1000*60; // 60 seconds
 
 GPSPositionPrivate::GPSPositionPrivate(QObject *parent)
     : QObject(parent),
index ae229df..67a34c4 100644 (file)
 #include <QTranslator>
 #include <QLocale>
 #include "engine/engine.h"
+#ifdef Q_WS_MAEMO_5
+#include "application.h"
+#endif
 
 
 int main(int argc, char *argv[])
 {
+#ifdef Q_WS_MAEMO_5
+    Application a(argc, argv);
+#else
     QApplication a(argc, argv);
+#endif
+    SituareEngine engine;
 
 //    QTranslator translator;
 //    bool loaded = translator.load(":/res/languages/situare_" + QLocale::system().name());
@@ -35,7 +43,5 @@ int main(int argc, char *argv[])
 //    QString locale = QLocale::system().name();
 //    qDebug() << "Localization" << locale << "loaded:" << loaded;
 
-    SituareEngine engine;
-    int appRet = a.exec();
-    return appRet;
+    return a.exec();
 }
index 577dca8..1469bca 100644 (file)
@@ -9,6 +9,7 @@ RESOURCES += ../images.qrc \
     ../languages.qrc
 TRANSLATIONS += ../res/languages/situare_fi.ts
 SOURCES += main.cpp \
+    application.cpp \
     engine/engine.cpp \
     facebookservice/facebookauthentication.cpp \
     facebookservice/facebookcredentials.cpp \
@@ -54,7 +55,8 @@ SOURCES += main.cpp \
     user/user.cpp \
     ui/fullscreenbutton.cpp \
     engine/mce.cpp
-HEADERS += common.h \
+HEADERS += application.h \
+    common.h \
     engine/engine.h \
     facebookservice/facebookauthentication.h \
     facebookservice/facebookcommon.h \
index 9121bec..53c402f 100644 (file)
@@ -87,6 +87,11 @@ public:
     */
     void setData(User *user);
 
+    /**
+    * @brief Sets avatar image for this item.
+    *
+    * @param image image
+    */
     void setAvatarImage(const QPixmap &image);
 
 private: