Emit error signal when login fails
[situare] / src / application.cpp
1 /*
2    Situare - A location system for Facebook
3    Copyright (C) 2010  Ixonos Plc. Authors:
4
5       Henri Lampela - henri.lampela@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 <QX11Info>
23 #include <QDebug>
24
25 #include "application.h"
26
27 #include <X11/Xlib.h>
28 #include <X11/Xatom.h>
29 #include <X11/Xutil.h>
30
31 #ifdef Q_WS_HILDON
32     #define ACTIVE_APP_ATOM "_MB_CURRENT_APP_WINDOW"
33 #else
34     #define ACTIVE_APP_ATOM "_NET_ACTIVE_WINDOW"
35 #endif
36
37 Application::Application(int &argc, char **argv)
38     : QApplication(argc, argv),
39     m_topmost(false)
40 {
41     qDebug() << __PRETTY_FUNCTION__;
42 }
43
44 void Application::registerWindow(WId window)
45 {
46     qDebug() << __PRETTY_FUNCTION__;
47
48    m_windows.append(window);
49 }
50
51 void Application::unregisterWindow(WId window)
52 {
53     qDebug() << __PRETTY_FUNCTION__;
54
55     m_windows.removeOne(window);
56 }
57
58 #ifdef Q_WS_X11
59 WId Application::activeDesktopWindow(Display *display, WId rootWindow)
60 {
61     qDebug() << __PRETTY_FUNCTION__;
62
63     Atom realType;
64     int format, status;
65     unsigned long n, extra;
66     unsigned char *data = 0;
67     WId activeWindow;
68     Atom atom = XInternAtom(display, ACTIVE_APP_ATOM, false);
69
70     status = XGetWindowProperty(display, rootWindow, atom, 0L, 16L,
71                         0, XA_WINDOW, &realType, &format,
72                         &n, &extra, &data);
73     if (status == Success && realType == XA_WINDOW &&
74         format == 32 && n == 1 && data != NULL)
75         activeWindow = ((WId*)data)[0];
76     else
77         activeWindow = None;
78
79     if (data != 0)
80         XFree(data);
81
82     return activeWindow;
83 }
84
85 bool Application::x11EventFilter(XEvent *event)
86 {
87     qDebug() << __PRETTY_FUNCTION__;
88
89     static Qt::HANDLE rootWindow = QX11Info::appRootWindow();
90     static Atom activeAppAtom = XInternAtom(QX11Info::display(), ACTIVE_APP_ATOM, false);
91
92     switch (event->type) {
93     case PropertyNotify: {
94             XPropertyEvent *pevent = (XPropertyEvent*)event;
95             if (pevent->window == rootWindow) {
96                 if (pevent->atom == activeAppAtom) {
97                     WId activeWindow = activeDesktopWindow(pevent->display, rootWindow);
98
99                     if(m_topmost != m_windows.contains(activeWindow)) {
100                         m_topmost = !m_topmost;
101                         emit topmostWindowChanged(m_topmost);
102                     }
103                 }
104             }
105         }
106         break;
107     case KeyPress:
108     case KeyRelease:
109     case ButtonPress:
110     case ButtonRelease:
111     case MotionNotify:
112     case EnterNotify: // required when returning from statusbar menu
113     case FocusIn: // required when returning from update location dialog
114         emit showFullScreenButton();
115         break;
116     }
117
118     // don't block any events here, so false is returned
119     return false;
120 }
121 #endif