~dbarth/compiz/reworked-fix-744104

« back to all changes in this revision

Viewing changes to unity/unity_window_decorator/src/forcequit.c

  • Committer: David Barth
  • Date: 2011-04-05 20:20:54 UTC
  • Revision ID: david.barth@canonical.com-20110405202054-fnh0y5t2s228mf4k
re-integrate the unity-window-decorator, for real this time

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "gtk-window-decorator.h"
 
2
 
 
3
static char *
 
4
get_client_machine (Window xwindow)
 
5
{
 
6
    Atom   atom, type;
 
7
    gulong nitems, bytes_after;
 
8
    guchar *str = NULL;
 
9
    int    format, result;
 
10
    char   *retval;
 
11
 
 
12
    atom = XInternAtom (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), "WM_CLIENT_MACHINE", FALSE);
 
13
 
 
14
    gdk_error_trap_push ();
 
15
 
 
16
    result = XGetWindowProperty (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()),
 
17
                                 xwindow, atom,
 
18
                                 0, G_MAXLONG,
 
19
                                 FALSE, XA_STRING, &type, &format, &nitems,
 
20
                                 &bytes_after, &str);
 
21
 
 
22
    gdk_error_trap_pop ();
 
23
 
 
24
    if (result != Success)
 
25
        return NULL;
 
26
 
 
27
    if (type != XA_STRING)
 
28
    {
 
29
        XFree (str);
 
30
        return NULL;
 
31
    }
 
32
 
 
33
    retval = g_strdup ((gchar *) str);
 
34
 
 
35
    XFree (str);
 
36
 
 
37
    return retval;
 
38
}
 
39
 
 
40
static void
 
41
kill_window (WnckWindow *win)
 
42
{
 
43
    WnckApplication *app;
 
44
 
 
45
    app = wnck_window_get_application (win);
 
46
    if (app)
 
47
    {
 
48
        gchar buf[257], *client_machine;
 
49
        int   pid;
 
50
 
 
51
        pid = wnck_application_get_pid (app);
 
52
        client_machine = get_client_machine (wnck_application_get_xid (app));
 
53
 
 
54
        if (client_machine && pid > 0)
 
55
        {
 
56
            if (gethostname (buf, sizeof (buf) - 1) == 0)
 
57
            {
 
58
                if (strcmp (buf, client_machine) == 0)
 
59
                    kill (pid, 9);
 
60
            }
 
61
        }
 
62
 
 
63
        if (client_machine)
 
64
            g_free (client_machine);
 
65
    }
 
66
 
 
67
    gdk_error_trap_push ();
 
68
    XKillClient (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), wnck_window_get_xid (win));
 
69
    gdk_display_sync (gdk_display_get_default ());
 
70
    gdk_error_trap_pop ();
 
71
}
 
72
 
 
73
static void
 
74
force_quit_dialog_realize (GtkWidget *dialog,
 
75
                           void      *data)
 
76
{
 
77
    WnckWindow *win = data;
 
78
 
 
79
    gdk_error_trap_push ();
 
80
    XSetTransientForHint (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()),
 
81
                          GDK_WINDOW_XID (dialog->window),
 
82
                          wnck_window_get_xid (win));
 
83
    gdk_display_sync (gdk_display_get_default ());
 
84
    gdk_error_trap_pop ();
 
85
}
 
86
 
 
87
static void
 
88
force_quit_dialog_response (GtkWidget *dialog,
 
89
                            gint      response,
 
90
                            void      *data)
 
91
{
 
92
    WnckWindow *win = data;
 
93
    decor_t    *d = g_object_get_data (G_OBJECT (win), "decor");
 
94
 
 
95
    if (response == GTK_RESPONSE_ACCEPT)
 
96
        kill_window (win);
 
97
 
 
98
    if (d->force_quit_dialog)
 
99
    {
 
100
        d->force_quit_dialog = NULL;
 
101
        gtk_widget_destroy (dialog);
 
102
    }
 
103
}
 
104
 
 
105
void
 
106
show_force_quit_dialog (WnckWindow *win,
 
107
                        Time        timestamp)
 
108
{
 
109
    decor_t   *d = g_object_get_data (G_OBJECT (win), "decor");
 
110
    GtkWidget *dialog;
 
111
    gchar     *str, *tmp;
 
112
 
 
113
    if (d->force_quit_dialog)
 
114
        return;
 
115
 
 
116
    tmp = g_markup_escape_text (wnck_window_get_name (win), -1);
 
117
    str = g_strdup_printf (_("The window \"%s\" is not responding."), tmp);
 
118
 
 
119
    g_free (tmp);
 
120
 
 
121
    dialog = gtk_message_dialog_new (NULL, 0,
 
122
                                     GTK_MESSAGE_WARNING,
 
123
                                     GTK_BUTTONS_NONE,
 
124
                                     "<b>%s</b>\n\n%s",
 
125
                                     str,
 
126
                                     _("Forcing this application to "
 
127
                                     "quit will cause you to lose any "
 
128
                                     "unsaved changes."));
 
129
    g_free (str);
 
130
 
 
131
    gtk_window_set_icon_name (GTK_WINDOW (dialog), "force-quit");
 
132
 
 
133
    gtk_label_set_use_markup (GTK_LABEL (GTK_MESSAGE_DIALOG (dialog)->label),
 
134
                              TRUE);
 
135
    gtk_label_set_line_wrap (GTK_LABEL (GTK_MESSAGE_DIALOG (dialog)->label),
 
136
                             TRUE);
 
137
 
 
138
    gtk_dialog_add_buttons (GTK_DIALOG (dialog),
 
139
                            GTK_STOCK_CANCEL,
 
140
                            GTK_RESPONSE_REJECT,
 
141
                            _("_Force Quit"),
 
142
                            GTK_RESPONSE_ACCEPT,
 
143
                            NULL);
 
144
 
 
145
    gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_REJECT);
 
146
 
 
147
    g_signal_connect (G_OBJECT (dialog), "realize",
 
148
                      G_CALLBACK (force_quit_dialog_realize),
 
149
                      win);
 
150
 
 
151
    g_signal_connect (G_OBJECT (dialog), "response",
 
152
                      G_CALLBACK (force_quit_dialog_response),
 
153
                      win);
 
154
 
 
155
    gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
 
156
 
 
157
    gtk_widget_realize (dialog);
 
158
 
 
159
    gdk_x11_window_set_user_time (dialog->window, timestamp);
 
160
 
 
161
    gtk_widget_show (dialog);
 
162
 
 
163
    d->force_quit_dialog = dialog;
 
164
}
 
165
 
 
166
void
 
167
hide_force_quit_dialog (WnckWindow *win)
 
168
{
 
169
    decor_t *d = g_object_get_data (G_OBJECT (win), "decor");
 
170
 
 
171
    if (d->force_quit_dialog)
 
172
    {
 
173
        gtk_widget_destroy (d->force_quit_dialog);
 
174
        d->force_quit_dialog = NULL;
 
175
    }
 
176
}