~mardy/signon-ui/lp1171853

« back to all changes in this revision

Viewing changes to src/embed-manager.cpp

  • Committer: Alberto Mardegan
  • Date: 2012-11-13 09:46:57 UTC
  • mfrom: (66.3.4 reauthentication)
  • Revision ID: alberto.mardegan@canonical.com-20121113094657-d8goas34wnw9u034
New ReauthenticateAccount D-Bus API

This method will allow reauthenticating an account by replaying the
authentication session which caused the failure.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * This file is part of signon-ui
 
3
 *
 
4
 * Copyright (C) 2012 Canonical Ltd.
 
5
 *
 
6
 * Contact: Alberto Mardegan <alberto.mardegan@canonical.com>
 
7
 *
 
8
 * This program is free software: you can redistribute it and/or modify it
 
9
 * under the terms of the GNU General Public License version 3, as published
 
10
 * by the Free Software Foundation.
 
11
 *
 
12
 * This program is distributed in the hope that it will be useful, but
 
13
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 
14
 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 
15
 * PURPOSE.  See the GNU General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU General Public License along
 
18
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 
19
 */
 
20
 
 
21
#include "debug.h"
 
22
#include "embed-manager.h"
 
23
 
 
24
#include <QApplication>
 
25
#include <QX11Info>
 
26
#include <X11/Xatom.h>
 
27
#include <X11/Xlib.h>
 
28
 
 
29
using namespace SignOnUi;
 
30
 
 
31
static EmbedManager *staticInstance = 0;
 
32
 
 
33
namespace SignOnUi {
 
34
 
 
35
class EmbedManagerPrivate
 
36
{
 
37
public:
 
38
    EmbedManagerPrivate();
 
39
    ~EmbedManagerPrivate();
 
40
 
 
41
private:
 
42
    friend class EmbedManager;
 
43
    QMap<WId,QX11EmbedWidget*> m_embedWidgets;
 
44
};
 
45
 
 
46
} // namespace
 
47
 
 
48
/* Workaround for https://bugreports.qt-project.org/browse/QTBUG-3617
 
49
 * Send XEMBED_REQUEST_FOCUS manually.
 
50
 */
 
51
#define XEMBED_REQUEST_FOCUS 3
 
52
 
 
53
// Sends an XEmbed message.
 
54
static void sendXEmbedMessage(WId window, Display *display, long message,
 
55
                  long detail = 0, long data1 = 0, long data2 = 0)
 
56
{
 
57
    XClientMessageEvent c;
 
58
    memset(&c, 0, sizeof(c));
 
59
    c.type = ClientMessage;
 
60
    c.message_type = XInternAtom(display, "_XEMBED", false);
 
61
    c.format = 32;
 
62
    c.display = display;
 
63
    c.window = window;
 
64
 
 
65
    c.data.l[0] = QX11Info::appTime();
 
66
    c.data.l[1] = message;
 
67
    c.data.l[2] = detail;
 
68
    c.data.l[3] = data1;
 
69
    c.data.l[4] = data2;
 
70
 
 
71
    XSendEvent(display, window, false, NoEventMask, (XEvent *) &c);
 
72
}
 
73
 
 
74
static bool x11EventFilter(void *message, long *)
 
75
{
 
76
    XEvent *event = reinterpret_cast<XEvent*>(message);
 
77
    if (event->type == ButtonPress)
 
78
    {
 
79
        QWidget *w = QWidget::find(event->xbutton.window);
 
80
        if (w && w->window()->objectName() == "request-widget") {
 
81
            QX11EmbedWidget *embed = static_cast<QX11EmbedWidget*>(w->window());
 
82
            QApplication::setActiveWindow(w->window());
 
83
            sendXEmbedMessage(embed->containerWinId(),
 
84
                              w->x11Info().display(),
 
85
                              XEMBED_REQUEST_FOCUS);
 
86
        }
 
87
    }
 
88
    return false;
 
89
}
 
90
 
 
91
EmbedManagerPrivate::EmbedManagerPrivate()
 
92
{
 
93
}
 
94
 
 
95
EmbedManagerPrivate::~EmbedManagerPrivate()
 
96
{
 
97
}
 
98
 
 
99
EmbedManager *EmbedManager::instance()
 
100
{
 
101
    if (staticInstance == 0) {
 
102
        staticInstance = new EmbedManager();
 
103
    }
 
104
 
 
105
    return staticInstance;
 
106
}
 
107
 
 
108
EmbedManager::EmbedManager(QObject *parent):
 
109
    QObject(parent),
 
110
    d_ptr(new EmbedManagerPrivate)
 
111
{
 
112
    QCoreApplication::instance()->setEventFilter(x11EventFilter);
 
113
}
 
114
 
 
115
EmbedManager::~EmbedManager()
 
116
{
 
117
    delete d_ptr;
 
118
}
 
119
 
 
120
QX11EmbedWidget *EmbedManager::widgetFor(WId windowId)
 
121
{
 
122
    Q_D(EmbedManager);
 
123
 
 
124
    if (!d->m_embedWidgets.contains(windowId)) {
 
125
        /* Create a new embed widget */
 
126
        QX11EmbedWidget *embed = new QX11EmbedWidget;
 
127
        QObject::connect(embed, SIGNAL(containerClosed()),
 
128
                         embed, SLOT(deleteLater()));
 
129
        embed->embedInto(windowId);
 
130
        embed->setObjectName("request-widget");
 
131
        d->m_embedWidgets[windowId] = embed;
 
132
    }
 
133
 
 
134
    return d->m_embedWidgets[windowId];
 
135
}