~ubuntu-branches/ubuntu/precise/cairo-dock/precise-proposed

« back to all changes in this revision

Viewing changes to src/help/applet-composite.c

  • Committer: Kees Cook
  • Date: 2011-08-11 19:31:26 UTC
  • mfrom: (19.1.1 cairo-dock)
  • Revision ID: kees@outflux.net-20110811193126-wh97aamdqx5gaf2f
Tags: 2.4.0~0beta2-0ubuntu1
releasing version 2.4.0~0beta2-0ubuntu1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
* This file is a part of the Cairo-Dock project
 
3
*
 
4
* Copyright : (C) see the 'copyright' file.
 
5
* E-mail    : see the 'copyright' file.
 
6
*
 
7
* This program is free software; you can redistribute it and/or
 
8
* modify it under the terms of the GNU General Public License
 
9
* as published by the Free Software Foundation; either version 3
 
10
* of the License, or (at your option) any later version.
 
11
*
 
12
* This program is distributed in the hope that it will be useful,
 
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
* GNU General Public License for more details.
 
16
* You should have received a copy of the GNU General Public License
 
17
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
*/
 
19
 
 
20
#include <stdlib.h>
 
21
#include <string.h>
 
22
 
 
23
#include "applet-struct.h"
 
24
#include "applet-composite.h"
 
25
 
 
26
static void (*s_activate_composite) (gboolean) = NULL;
 
27
 
 
28
  /////////////////////
 
29
 ///  WM Composite ///
 
30
/////////////////////
 
31
 
 
32
static void _set_metacity_composite (gboolean bActive)
 
33
{
 
34
        int r;
 
35
        if (bActive)
 
36
                r = system ("gconftool-2 -s '/apps/metacity/general/compositing_manager' --type bool true");
 
37
        else
 
38
                r = system ("gconftool-2 -s '/apps/metacity/general/compositing_manager' --type bool false");
 
39
}
 
40
 
 
41
static void _set_xfwm_composite (gboolean bActive)
 
42
{
 
43
        int r;
 
44
        if (bActive)
 
45
                r = system ("xfconf-query -c xfwm4 -p '/general/use_compositing' -t 'bool' -s 'true'");
 
46
        else
 
47
                r = system ("xfconf-query -c xfwm4 -p '/general/use_compositing' -t 'bool' -s 'false'");
 
48
}
 
49
 
 
50
static void _set_kwin_composite (gboolean bActive)
 
51
{
 
52
        int r;
 
53
        if (bActive)
 
54
                r = system ("[ \"$(qdbus org.kde.kwin /KWin compositingActive)\" == \"false\" ] && qdbus org.kde.kwin /KWin toggleCompositing");  // not active, so activating
 
55
        else
 
56
                r = system ("[ \"$(qdbus org.kde.kwin /KWin compositingActive)\" == \"true\" ] && qdbus org.kde.kwin /KWin toggleCompositing");  // active, so deactivating
 
57
}
 
58
 
 
59
  ///////////////////////
 
60
 /// Welcome message ///
 
61
///////////////////////
 
62
 
 
63
static void cd_help_show_welcome_message (void)
 
64
{
 
65
        cairo_dock_show_dialog_full (D_("Welcome in Cairo-Dock2 !\n"
 
66
                "This applet is here to help you start using the dock; just click on it.\n"
 
67
                "If you have any question/request/remark, please pay us a visit at http://glx-dock.org.\n"
 
68
                "Hope you will enjoy this soft !\n"
 
69
                "  (you can now click on this dialog to close it)"),
 
70
                myIcon, myContainer,
 
71
                0,
 
72
                "same icon",
 
73
                NULL, NULL, NULL, NULL);
 
74
        myData.bFirstLaunch = FALSE;
 
75
}
 
76
 
 
77
  ////////////////////////
 
78
 /// Composite dialog ///
 
79
////////////////////////
 
80
 
 
81
static inline void _cancel_wm_composite (void)
 
82
{
 
83
        s_activate_composite (FALSE);
 
84
}
 
85
static void _accept_wm_composite (int iClickedButton, GtkWidget *pInteractiveWidget, gpointer data, CairoDialog *pDialog)
 
86
{
 
87
        cd_debug ("%s (%d)", __func__, iClickedButton);
 
88
        if (iClickedButton == 1 || iClickedButton == -2)  // clic explicite sur "cancel", ou Echap ou auto-delete.
 
89
        {
 
90
                _cancel_wm_composite ();
 
91
        }
 
92
        gboolean *bAccepted = data;
 
93
        *bAccepted = TRUE;  // l'utilisateur a valide son choix.
 
94
}
 
95
static void _on_free_wm_dialog (gpointer data)
 
96
{
 
97
        gboolean *bAccepted = data;
 
98
        cd_debug ("%s (%d)", __func__, *bAccepted);
 
99
        if (! *bAccepted)  // le dialogue s'est detruit sans que l'utilisateur n'ait valide la question => on annule tout.
 
100
        {
 
101
                _cancel_wm_composite ();
 
102
        }
 
103
        g_free (data);
 
104
        
 
105
        if (myData.bFirstLaunch)
 
106
        {
 
107
                cd_help_show_welcome_message ();
 
108
        }
 
109
}
 
110
 
 
111
static void _toggle_remember_choice (GtkCheckButton *pButton, GtkWidget *pDialog)
 
112
{
 
113
        g_object_set_data (G_OBJECT (pDialog), "remember", GINT_TO_POINTER (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (pButton))));
 
114
}
 
115
 
 
116
static void _on_free_info_dialog (gpointer data)
 
117
{
 
118
        if (myData.bFirstLaunch)
 
119
        {
 
120
                cd_help_show_welcome_message ();
 
121
        }
 
122
}
 
123
 
 
124
  ///////////////////////
 
125
 /// Check composite ///
 
126
///////////////////////
 
127
 
 
128
void cd_help_enable_composite (void)
 
129
{
 
130
        // find the current WM.
 
131
        s_activate_composite = NULL;
 
132
        gchar *cPsef = cairo_dock_launch_command_sync ("pgrep metacity");  // 'ps | grep' ne marche pas, il faut le lancer dans un script :-/
 
133
        g_print ("cPsef: '%s'\n", cPsef);
 
134
        if (cPsef != NULL && *cPsef != '\0')
 
135
        {
 
136
                s_activate_composite = _set_metacity_composite;
 
137
        }
 
138
        else
 
139
        {
 
140
                cPsef = cairo_dock_launch_command_sync ("pgrep xfwm");
 
141
                if (cPsef != NULL && *cPsef != '\0')
 
142
                {
 
143
                        s_activate_composite = _set_xfwm_composite;
 
144
                }
 
145
                else
 
146
                {
 
147
                        cPsef = cairo_dock_launch_command_sync ("pgrep kwin");
 
148
                        if (cPsef != NULL && *cPsef != '\0')
 
149
                        {
 
150
                                s_activate_composite = _set_kwin_composite;
 
151
                        }
 
152
                }
 
153
        }
 
154
 
 
155
        // if the WM can handle the composite, ask the user if he wants to enable it.
 
156
        if (s_activate_composite != NULL)  // the WM can activate the composite.
 
157
        {
 
158
                Icon *pIcon = cairo_dock_get_dialogless_icon ();
 
159
                GtkWidget *pAskBox = gtk_hbox_new (FALSE, 3);
 
160
                GtkWidget *label = gtk_label_new (D_("Don't ask me any more"));
 
161
                cairo_dock_set_dialog_widget_text_color (label);
 
162
                GtkWidget *pCheckBox = gtk_check_button_new ();
 
163
                gtk_box_pack_end (GTK_BOX (pAskBox), pCheckBox, FALSE, FALSE, 0);
 
164
                gtk_box_pack_end (GTK_BOX (pAskBox), label, FALSE, FALSE, 0);
 
165
                g_signal_connect (G_OBJECT (pCheckBox), "toggled", G_CALLBACK(_toggle_remember_choice), pAskBox);
 
166
                int iClickedButton = cairo_dock_show_dialog_and_wait (D_("To remove the black rectangle around the dock, you need to activate a composite manager.\nDo you want to activate it now?"), myIcon, myContainer, 0., NULL, pAskBox);
 
167
 
 
168
                gboolean bRememberChoice = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (pCheckBox));
 
169
                gtk_widget_destroy (pAskBox);  // le widget survit a un dialogue bloquant.
 
170
                if (bRememberChoice)  // if not ticked, we'll check the composite on the next startup, and if it's ok, we'll not check it any more.
 
171
                {
 
172
                        myData.bTestComposite = FALSE;
 
173
                }
 
174
                if (iClickedButton == 0 || iClickedButton == -1)  // ok or Enter.
 
175
                {
 
176
                        s_activate_composite (TRUE);
 
177
                        cairo_dock_show_dialog_full (D_("Do you want to keep this setting?\nIn 15 seconds, the previous setting will be restored."),
 
178
                                myIcon, myContainer,
 
179
                                15e3,
 
180
                                "same icon",
 
181
                                NULL,
 
182
                                (CairoDockActionOnAnswerFunc) _accept_wm_composite,
 
183
                                g_new0 (gboolean, 1),
 
184
                                (GFreeFunc)_on_free_wm_dialog);
 
185
                }
 
186
                else if (myData.bFirstLaunch)
 
187
                {
 
188
                        cd_help_show_welcome_message ();
 
189
                }
 
190
        }
 
191
        else  // just notify him about the problem and its solution.
 
192
        {
 
193
                cairo_dock_show_dialog_full (D_("To remove the black rectangle around the dock, you will need to activate a composite manager.\nFor instance, this can be done by activating desktop effects, launching Compiz, or activating the composition in Metacity.\nIf your machine can't support composition, Cairo-Dock can emulate it. This option is in the 'System' module of the configuration, at the bottom of the page."),
 
194
                        myIcon, myContainer,
 
195
                        0,
 
196
                        "same icon",
 
197
                        NULL,
 
198
                        NULL,
 
199
                        NULL,
 
200
                        (GFreeFunc)_on_free_info_dialog);
 
201
        }
 
202
        g_free (cPsef);
 
203
}
 
204
 
 
205
static gboolean cd_help_check_composite (gpointer data)
 
206
{
 
207
        GdkScreen *pScreen = gdk_screen_get_default ();
 
208
        if (! gdk_screen_is_composited (pScreen))  // no composite yet.
 
209
        {
 
210
                g_print ("no composite (%d)\n", myData.iNbTestComposite);
 
211
                myData.iNbTestComposite ++;
 
212
                if (myData.iNbTestComposite < 4)  // check during 4 seconds.
 
213
                        return TRUE;
 
214
                
 
215
                cd_help_enable_composite ();
 
216
        }
 
217
        else  // composite is active => everything is ok on startup => no need to test it in the future.
 
218
        {
 
219
                myData.bTestComposite = FALSE;
 
220
                if (myData.bFirstLaunch)
 
221
                {
 
222
                        cd_help_show_welcome_message ();
 
223
                }
 
224
        }
 
225
        
 
226
        // remember to not test it any more.
 
227
        if (! myData.bTestComposite)
 
228
        {
 
229
                gchar *cConfFilePath = g_strdup_printf ("%s/.help", g_cCairoDockDataDir);
 
230
                cairo_dock_update_conf_file (cConfFilePath,
 
231
                        G_TYPE_BOOLEAN, "Launch", "test composite", myData.bTestComposite,
 
232
                        G_TYPE_INT, "Last Tip", "group", myData.iLastTipGroup,
 
233
                        G_TYPE_INT, "Last Tip", "key", myData.iLastTipKey,
 
234
                        G_TYPE_INVALID);  // write the tip too, in case it creates the file.
 
235
                g_free (cConfFilePath);
 
236
        }
 
237
        myData.iSidTestComposite = 0;
 
238
        return FALSE;
 
239
}
 
240
 
 
241
gboolean cd_help_get_params (gpointer data)
 
242
{
 
243
        // read our file.
 
244
        gchar *cConfFilePath = g_strdup_printf ("%s/.help", g_cCairoDockDataDir);
 
245
        if (g_file_test (cConfFilePath, G_FILE_TEST_EXISTS))  // file already exists, get the last read tip.
 
246
        {
 
247
                GKeyFile *pKeyFile = cairo_dock_open_key_file (cConfFilePath);
 
248
                if (pKeyFile != NULL)
 
249
                {
 
250
                        myData.iLastTipGroup = g_key_file_get_integer (pKeyFile, "Last Tip", "group", NULL);
 
251
                        myData.iLastTipKey = g_key_file_get_integer (pKeyFile, "Last Tip", "key", NULL);
 
252
                        myData.bTestComposite = g_key_file_get_boolean (pKeyFile, "Launch", "test composite", NULL);
 
253
                        
 
254
                        g_key_file_free (pKeyFile);
 
255
                }
 
256
        }
 
257
        else  // no file, means it's the first time we are launched.
 
258
        {
 
259
                myData.bFirstLaunch = TRUE;
 
260
                myData.bTestComposite = TRUE;
 
261
        }
 
262
        
 
263
        // test the composite for a few seconds (the Composite Manager may not be active yet).
 
264
        if (myData.bTestComposite && ! myContainersParam.bUseFakeTransparency)
 
265
        {
 
266
                myData.iSidTestComposite = g_timeout_add_seconds (1, cd_help_check_composite, NULL);
 
267
        }
 
268
        else if (myData.bFirstLaunch)
 
269
        {
 
270
                cd_help_show_welcome_message ();
 
271
        }
 
272
        
 
273
        g_free (cConfFilePath);
 
274
        myData.iSidGetParams = 0;
 
275
        return FALSE;
 
276
}