~helene-verhaeghe27/cairo-dock-core/bugfix

« back to all changes in this revision

Viewing changes to src/cairo-dock-applet-facility.c

  • Committer: fabounet
  • Date: 2008-11-14 01:51:17 UTC
  • Revision ID: vcs-imports@canonical.com-20081114015117-854dznkw3lfva52x
The commit of the year \!

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/******************************************************************************
 
2
 
 
3
This file is a part of the cairo-dock program,
 
4
released under the terms of the GNU General Public License.
 
5
 
 
6
Written by Fabrice Rey (for any bug report, please mail me to fabounet@users.berlios.de)
 
7
 
 
8
******************************************************************************/
 
9
#include <math.h>
 
10
#include <string.h>
 
11
#include <stdlib.h>
 
12
#include <cairo.h>
 
13
 
 
14
#include "cairo-dock-load.h"
 
15
#include "cairo-dock-draw.h"
 
16
#include "cairo-dock-config.h"
 
17
#include "cairo-dock-launcher-factory.h"
 
18
#include "cairo-dock-surface-factory.h"
 
19
#include "cairo-dock-animations.h"
 
20
#include "cairo-dock-themes-manager.h"
 
21
#include "cairo-dock-keyfile-utilities.h"
 
22
#include "cairo-dock-applet-factory.h"
 
23
#include "cairo-dock-log.h"
 
24
#include "cairo-dock-dock-factory.h"
 
25
#include "cairo-dock-callbacks.h"
 
26
#include "cairo-dock-dock-manager.h"
 
27
#include "cairo-dock-draw-opengl.h"
 
28
#include "cairo-dock-applet-facility.h"
 
29
 
 
30
extern gchar *g_cCurrentThemePath;
 
31
extern gchar *g_cCairoDockDataDir;
 
32
 
 
33
extern double g_fAmplitude;
 
34
extern double g_fAlbedo;
 
35
 
 
36
extern CairoDockLabelDescription g_iconTextDescription;
 
37
extern CairoDockLabelDescription g_quickInfoTextDescription;
 
38
extern gboolean g_bTextAlwaysHorizontal;
 
39
 
 
40
 
 
41
void cairo_dock_set_icon_surface_full (cairo_t *pIconContext, cairo_surface_t *pSurface, double fScale, double fAlpha, Icon *pIcon, CairoContainer *pContainer)
 
42
{
 
43
        g_return_if_fail (cairo_status (pIconContext) == CAIRO_STATUS_SUCCESS);
 
44
        
 
45
        //\________________ On efface l'ancienne image.
 
46
        cairo_set_source_rgba (pIconContext, 0.0, 0.0, 0.0, 0.0);
 
47
        cairo_set_operator (pIconContext, CAIRO_OPERATOR_SOURCE);
 
48
        cairo_paint (pIconContext);
 
49
        cairo_set_operator (pIconContext, CAIRO_OPERATOR_OVER);
 
50
        
 
51
        //\________________ On applique la nouvelle image.
 
52
        if (pSurface != NULL && fScale > 0)
 
53
        {
 
54
                cairo_save (pIconContext);
 
55
                if (fScale != 1 && pIcon != NULL)
 
56
                {
 
57
                        double fMaxScale = cairo_dock_get_max_scale (pContainer);
 
58
                        cairo_translate (pIconContext, pIcon->fWidth * fMaxScale / 2 * (1 - fScale) , pIcon->fHeight * fMaxScale / 2 * (1 - fScale));
 
59
                        cairo_scale (pIconContext, fScale, fScale);
 
60
                }
 
61
                
 
62
                cairo_set_source_surface (
 
63
                        pIconContext,
 
64
                        pSurface,
 
65
                        0.,
 
66
                        0.);
 
67
                
 
68
                if (fAlpha != 1)
 
69
                        cairo_paint_with_alpha (pIconContext, fAlpha);
 
70
                else
 
71
                        cairo_paint (pIconContext);
 
72
                cairo_restore (pIconContext);
 
73
        }
 
74
        cairo_dock_update_icon_texture (pIcon);
 
75
}
 
76
 
 
77
void cairo_dock_add_reflection_to_icon (cairo_t *pIconContext, Icon *pIcon, CairoContainer *pContainer)
 
78
{
 
79
        g_return_if_fail (pIcon != NULL && pContainer!= NULL);
 
80
        if (pIcon->pReflectionBuffer != NULL)
 
81
        {
 
82
                cairo_surface_destroy (pIcon->pReflectionBuffer);
 
83
                pIcon->pReflectionBuffer = NULL;
 
84
        }
 
85
        
 
86
        double fMaxScale = (CAIRO_DOCK_IS_DOCK (pContainer) ? (1 + g_fAmplitude) / CAIRO_DOCK (pContainer)->fRatio : 1);
 
87
        gboolean bIsHorizontal = pContainer->bIsHorizontal;
 
88
        pIcon->pReflectionBuffer = cairo_dock_create_reflection_surface (pIcon->pIconBuffer,
 
89
                pIconContext,
 
90
                (bIsHorizontal ? pIcon->fWidth : pIcon->fHeight) * fMaxScale,
 
91
                (bIsHorizontal ? pIcon->fHeight : pIcon->fWidth) * fMaxScale,
 
92
                bIsHorizontal,
 
93
                fMaxScale,
 
94
                pContainer->bDirectionUp);
 
95
 
 
96
        if (pIcon->pFullIconBuffer != NULL)
 
97
        {
 
98
                cairo_surface_destroy (pIcon->pFullIconBuffer);
 
99
                pIcon->pFullIconBuffer = NULL;
 
100
        }
 
101
        pIcon->pFullIconBuffer = cairo_dock_create_icon_surface_with_reflection (pIcon->pIconBuffer,
 
102
                pIcon->pReflectionBuffer,
 
103
                pIconContext,
 
104
                (bIsHorizontal ? pIcon->fWidth : pIcon->fHeight) * fMaxScale,
 
105
                (bIsHorizontal ? pIcon->fHeight : pIcon->fWidth) * fMaxScale,
 
106
                bIsHorizontal,
 
107
                fMaxScale,
 
108
                pContainer->bDirectionUp);
 
109
}
 
110
 
 
111
void cairo_dock_set_icon_surface_with_reflect (cairo_t *pIconContext, cairo_surface_t *pSurface, Icon *pIcon, CairoContainer *pContainer)
 
112
{
 
113
        cairo_dock_set_icon_surface (pIconContext, pSurface);
 
114
        
 
115
        cairo_dock_add_reflection_to_icon (pIconContext, pIcon, pContainer);
 
116
}
 
117
 
 
118
void cairo_dock_set_image_on_icon (cairo_t *pIconContext, gchar *cImagePath, Icon *pIcon, CairoContainer *pContainer)
 
119
{
 
120
        double fMaxScale = (CAIRO_DOCK_IS_DOCK (pContainer) ? (1 + g_fAmplitude) / CAIRO_DOCK (pContainer)->fRatio : 1);
 
121
        cairo_surface_t *pImageSurface = cairo_dock_create_surface_for_icon (cImagePath,
 
122
                pIconContext,
 
123
                pIcon->fWidth * fMaxScale,
 
124
                pIcon->fHeight * fMaxScale);
 
125
        
 
126
        cairo_dock_set_icon_surface_with_reflect (pIconContext, pImageSurface, pIcon, pContainer);
 
127
        
 
128
        cairo_surface_destroy (pImageSurface);
 
129
}
 
130
 
 
131
void cairo_dock_draw_bar_on_icon (cairo_t *pIconContext, double fValue, Icon *pIcon, CairoContainer *pContainer)
 
132
{
 
133
        double fMaxScale = (CAIRO_DOCK_IS_DOCK (pContainer) ? (1 + g_fAmplitude) / CAIRO_DOCK (pContainer)->fRatio : 1);
 
134
        cairo_pattern_t *pGradationPattern = cairo_pattern_create_linear (0.,
 
135
                0.,
 
136
                pIcon->fWidth * fMaxScale,
 
137
                0.);  // de gauche a droite.
 
138
        g_return_if_fail (cairo_pattern_status (pGradationPattern) == CAIRO_STATUS_SUCCESS);
 
139
        
 
140
        cairo_pattern_set_extend (pGradationPattern, CAIRO_EXTEND_NONE);
 
141
        cairo_pattern_add_color_stop_rgba (pGradationPattern,
 
142
                0.,
 
143
                1.,
 
144
                0.,
 
145
                0.,
 
146
                1.);
 
147
        cairo_pattern_add_color_stop_rgba (pGradationPattern,
 
148
                1.,
 
149
                0.,
 
150
                1.,
 
151
                0.,
 
152
                1.);
 
153
        
 
154
        cairo_save (pIconContext);
 
155
        cairo_set_operator (pIconContext, CAIRO_OPERATOR_OVER);
 
156
        
 
157
        cairo_set_line_width (pIconContext, 6);
 
158
        cairo_set_line_cap (pIconContext, CAIRO_LINE_CAP_ROUND);
 
159
        
 
160
        cairo_move_to (pIconContext, 3, pIcon->fHeight * fMaxScale - 3);
 
161
        cairo_rel_line_to (pIconContext, (pIcon->fWidth * fMaxScale - 6) * fValue, 0);
 
162
        
 
163
        cairo_set_source (pIconContext, pGradationPattern);
 
164
        cairo_stroke (pIconContext);
 
165
        
 
166
        cairo_pattern_destroy (pGradationPattern);
 
167
        cairo_restore (pIconContext);
 
168
}
 
169
 
 
170
void cairo_dock_set_icon_name (cairo_t *pSourceContext, const gchar *cIconName, Icon *pIcon, CairoContainer *pContainer)  // fonction proposee par Necropotame.
 
171
{
 
172
        g_return_if_fail (pIcon != NULL && pContainer != NULL);  // le contexte sera verifie plus loin.
 
173
        gchar *cUniqueName = NULL;
 
174
        
 
175
        if (pIcon->pSubDock != NULL)
 
176
        {
 
177
                cUniqueName = cairo_dock_get_unique_dock_name (cIconName);
 
178
                cIconName = cUniqueName;
 
179
                cairo_dock_rename_dock (pIcon->acName, pIcon->pSubDock, cUniqueName);
 
180
        }
 
181
        
 
182
        if (pIcon->acName != cIconName)
 
183
        {
 
184
                g_free (pIcon->acName);
 
185
                pIcon->acName = g_strdup (cIconName);
 
186
        }
 
187
        
 
188
        g_free (cUniqueName);
 
189
        
 
190
        cairo_dock_fill_one_text_buffer(
 
191
                pIcon,
 
192
                pSourceContext,
 
193
                &g_iconTextDescription,
 
194
                (g_bTextAlwaysHorizontal ? CAIRO_DOCK_HORIZONTAL : pContainer->bIsHorizontal),
 
195
                pContainer->bDirectionUp);
 
196
        cairo_dock_update_label_texture (pIcon);
 
197
}
 
198
void cairo_dock_set_icon_name_full (cairo_t *pSourceContext, Icon *pIcon, CairoContainer *pContainer, const gchar *cIconNameFormat, ...)
 
199
{
 
200
        va_list args;
 
201
        va_start (args, cIconNameFormat);
 
202
        gchar *cFullText = g_strdup_vprintf (cIconNameFormat, args);
 
203
        cairo_dock_set_icon_name (pSourceContext, cFullText, pIcon, pContainer);
 
204
        g_free (cFullText);
 
205
        va_end (args);
 
206
}
 
207
 
 
208
void cairo_dock_set_quick_info (cairo_t *pSourceContext, const gchar *cQuickInfo, Icon *pIcon, double fMaxScale)
 
209
{
 
210
        g_return_if_fail (pIcon != NULL);  // le contexte sera verifie plus loin.
 
211
 
 
212
        if (pIcon->cQuickInfo != cQuickInfo)
 
213
        {
 
214
                g_free (pIcon->cQuickInfo);
 
215
                pIcon->cQuickInfo = g_strdup (cQuickInfo);
 
216
        }
 
217
        
 
218
        cairo_dock_fill_one_quick_info_buffer (pIcon,
 
219
                pSourceContext,
 
220
                &g_quickInfoTextDescription,
 
221
                fMaxScale);
 
222
        cairo_dock_update_quick_info_texture (pIcon);
 
223
}
 
224
 
 
225
void cairo_dock_set_quick_info_full (cairo_t *pSourceContext, Icon *pIcon, CairoContainer *pContainer, const gchar *cQuickInfoFormat, ...)
 
226
{
 
227
        va_list args;
 
228
        va_start (args, cQuickInfoFormat);
 
229
        gchar *cFullText = g_strdup_vprintf (cQuickInfoFormat, args);
 
230
        cairo_dock_set_quick_info (pSourceContext, cFullText, pIcon, (CAIRO_DOCK_IS_DOCK (pContainer) ? (1 + g_fAmplitude) / 1 : 1));
 
231
        g_free (cFullText);
 
232
        va_end (args);
 
233
}
 
234
 
 
235
void cairo_dock_set_hours_minutes_as_quick_info (cairo_t *pSourceContext, Icon *pIcon, CairoContainer *pContainer, int iTimeInSeconds)
 
236
{
 
237
        int hours = iTimeInSeconds / 3600;
 
238
        int minutes = (iTimeInSeconds % 3600) / 60;
 
239
        if (hours != 0)
 
240
                cairo_dock_set_quick_info_full (pSourceContext, pIcon, pContainer, "%dh%02d", hours, abs (minutes));
 
241
        else
 
242
                cairo_dock_set_quick_info_full (pSourceContext, pIcon, pContainer, "%dmn", minutes);
 
243
}
 
244
 
 
245
void cairo_dock_set_minutes_secondes_as_quick_info (cairo_t *pSourceContext, Icon *pIcon, CairoContainer *pContainer, int iTimeInSeconds)
 
246
{
 
247
        int minutes = iTimeInSeconds / 60;
 
248
        int secondes = iTimeInSeconds % 60;
 
249
        if (minutes != 0)
 
250
                cairo_dock_set_quick_info_full (pSourceContext, pIcon, pContainer, "%d:%02d", minutes, abs (secondes));
 
251
        else
 
252
                cairo_dock_set_quick_info_full (pSourceContext, pIcon, pContainer, "%s0:%02d", (secondes < 0 ? "-" : ""), abs (secondes));
 
253
}
 
254
 
 
255
void cairo_dock_set_size_as_quick_info (cairo_t *pSourceContext, Icon *pIcon, CairoContainer *pContainer, long long int iSizeInBytes)
 
256
{
 
257
        if (iSizeInBytes < 1024)
 
258
        {
 
259
                cairo_dock_set_quick_info_full (pSourceContext, pIcon, pContainer, "%dB", iSizeInBytes);
 
260
        }
 
261
        else if (iSizeInBytes < (1 << 20))
 
262
        {
 
263
                cairo_dock_set_quick_info_full (pSourceContext, pIcon, pContainer, "%dK", (int) (iSizeInBytes>>10));
 
264
        }
 
265
        else if (iSizeInBytes < (1 << 30))
 
266
        {
 
267
                cairo_dock_set_quick_info_full (pSourceContext, pIcon, pContainer, "%dM", (int) (iSizeInBytes>>20));
 
268
        }
 
269
        else
 
270
        {
 
271
                cairo_dock_set_quick_info_full (pSourceContext, pIcon, pContainer, "%dG", (int) (iSizeInBytes>>30));
 
272
        }
 
273
}
 
274
 
 
275
void cairo_dock_animate_icon (Icon *pIcon, CairoDock *pDock, CairoDockAnimationType iAnimationType, int iNbRounds)
 
276
{
 
277
        cairo_dock_arm_animation (pIcon, iAnimationType, iNbRounds);
 
278
        cairo_dock_start_animation (pIcon, pDock);
 
279
}
 
280
 
 
281
 
 
282
gchar* cairo_dock_manage_themes_for_applet (gchar *cAppletShareDataDir, gchar *cThemeDirName, gchar *cAppletConfFilePath, GKeyFile *pKeyFile, gchar *cGroupName, gchar *cKeyName, gboolean *bFlushConfFileNeeded, gchar *cDefaultThemeName, const gchar *cExtraDirName)
 
283
{
 
284
        GError *erreur = NULL;
 
285
        gchar *cThemesDirPath = g_strdup_printf ("%s/%s", cAppletShareDataDir, cThemeDirName);
 
286
        GHashTable *pThemeTable = cairo_dock_list_themes (cThemesDirPath, NULL, &erreur);
 
287
        if (erreur != NULL)
 
288
        {
 
289
                cd_warning (erreur->message);
 
290
                g_error_free (erreur);
 
291
                erreur = NULL;
 
292
        }
 
293
        g_free (cThemesDirPath);
 
294
        
 
295
        if (cExtraDirName != NULL)
 
296
        {
 
297
                cThemesDirPath = g_strdup_printf ("%s/%s/%s", g_cCairoDockDataDir, CAIRO_DOCK_EXTRAS_DIR, cExtraDirName);
 
298
                pThemeTable = cairo_dock_list_themes (cThemesDirPath, pThemeTable, NULL);  // le repertoire peut ne pas exister, donc on ignore l'erreur.
 
299
                g_free (cThemesDirPath);
 
300
        }
 
301
        
 
302
        gchar *cThemePath = NULL;
 
303
        if (pThemeTable != NULL)
 
304
        {
 
305
                cairo_dock_update_conf_file_with_themes (pKeyFile, cAppletConfFilePath, pThemeTable, cGroupName, cKeyName);
 
306
                
 
307
                gchar *cChosenThemeName = cairo_dock_get_string_key_value (pKeyFile, cGroupName, cKeyName, bFlushConfFileNeeded, cDefaultThemeName, NULL, NULL);
 
308
                if (cChosenThemeName != NULL)
 
309
                        cThemePath = g_strdup (g_hash_table_lookup (pThemeTable, cChosenThemeName));
 
310
                g_free (cChosenThemeName);
 
311
                
 
312
                if (cThemePath == NULL && cDefaultThemeName != NULL)
 
313
                        cThemePath = g_strdup (g_hash_table_lookup (pThemeTable, cDefaultThemeName));
 
314
 
 
315
                g_hash_table_destroy (pThemeTable);
 
316
        }
 
317
        return cThemePath;
 
318
}
 
319
 
 
320
GtkWidget *cairo_dock_create_sub_menu (gchar *cLabel, GtkWidget *pMenu)
 
321
{
 
322
        GtkWidget *pSubMenu = gtk_menu_new ();
 
323
        GtkWidget *pMenuItem = gtk_menu_item_new_with_label (cLabel);
 
324
        gtk_menu_shell_append (GTK_MENU_SHELL (pMenu), pMenuItem);
 
325
        gtk_menu_item_set_submenu (GTK_MENU_ITEM (pMenuItem), pSubMenu);
 
326
        return pSubMenu;
 
327
}
 
328
 
 
329
 
 
330
 
 
331
static gboolean _cairo_dock_timer (CairoDockMeasure *pMeasureTimer)
 
332
{
 
333
        cairo_dock_launch_measure (pMeasureTimer);
 
334
        return TRUE;
 
335
}
 
336
static gpointer _cairo_dock_threaded_calculation (CairoDockMeasure *pMeasureTimer)
 
337
{
 
338
        //cd_debug ("*** debut du thread (%d)", g_atomic_int_get (&pMeasureTimer->iThreadIsRunning));
 
339
        if (pMeasureTimer->acquisition != NULL)
 
340
                pMeasureTimer->acquisition (pMeasureTimer->pUserData);
 
341
        
 
342
        g_mutex_lock (pMeasureTimer->pMutexData);
 
343
        pMeasureTimer->read (pMeasureTimer->pUserData);
 
344
        g_mutex_unlock (pMeasureTimer->pMutexData);
 
345
        
 
346
        g_atomic_int_set (&pMeasureTimer->iThreadIsRunning, 0);
 
347
        return NULL;
 
348
}
 
349
static gboolean _cairo_dock_check_for_redraw (CairoDockMeasure *pMeasureTimer)
 
350
{
 
351
        int iThreadIsRunning = g_atomic_int_get (&pMeasureTimer->iThreadIsRunning);
 
352
        //cd_debug ("%s (%d)", __func__, iThreadIsRunning);
 
353
        if (! iThreadIsRunning)
 
354
        {
 
355
                //\_______________________ On recharge ce qu'il faut avec ces nouvelles donnees.
 
356
                g_mutex_lock (pMeasureTimer->pMutexData);
 
357
                gboolean bContinue = pMeasureTimer->update (pMeasureTimer->pUserData);
 
358
                g_mutex_unlock (pMeasureTimer->pMutexData);
 
359
                
 
360
                //\_______________________ On lance/arrete le timer si necessaire.
 
361
                if (! bContinue)
 
362
                {
 
363
                        if (pMeasureTimer->iSidTimer != 0)
 
364
                        {
 
365
                                g_source_remove (pMeasureTimer->iSidTimer);
 
366
                                pMeasureTimer->iSidTimer = 0;
 
367
                        }
 
368
                }
 
369
                else if (pMeasureTimer->iSidTimer == 0 && pMeasureTimer->iCheckInterval != 0)
 
370
                {
 
371
                        pMeasureTimer->iFrequencyState = CAIRO_DOCK_FREQUENCY_NORMAL;
 
372
                        pMeasureTimer->iSidTimer = g_timeout_add_seconds (pMeasureTimer->iCheckInterval, (GSourceFunc) _cairo_dock_timer, pMeasureTimer);
 
373
                }
 
374
                
 
375
                pMeasureTimer->iSidTimerRedraw = 0;
 
376
                return FALSE;
 
377
        }
 
378
        return TRUE;
 
379
}
 
380
void cairo_dock_launch_measure (CairoDockMeasure *pMeasureTimer)
 
381
{
 
382
        g_return_if_fail (pMeasureTimer != NULL);
 
383
        if (pMeasureTimer->pMutexData == NULL)
 
384
        {
 
385
                if (pMeasureTimer->acquisition != NULL)
 
386
                        pMeasureTimer->acquisition (pMeasureTimer->pUserData);
 
387
                if (pMeasureTimer->read != NULL)
 
388
                        pMeasureTimer->read (pMeasureTimer->pUserData);
 
389
                gboolean bContinue = pMeasureTimer->update (pMeasureTimer->pUserData);
 
390
                
 
391
                if (! bContinue)
 
392
                {
 
393
                        if (pMeasureTimer->iSidTimer != 0)
 
394
                        {
 
395
                                g_source_remove (pMeasureTimer->iSidTimer);
 
396
                                pMeasureTimer->iSidTimer = 0;
 
397
                        }
 
398
                }
 
399
                else if (pMeasureTimer->iSidTimer == 0 && pMeasureTimer->iCheckInterval != 0)
 
400
                {
 
401
                        pMeasureTimer->iFrequencyState = CAIRO_DOCK_FREQUENCY_NORMAL;
 
402
                        pMeasureTimer->iSidTimer = g_timeout_add_seconds (pMeasureTimer->iCheckInterval, (GSourceFunc) _cairo_dock_timer, pMeasureTimer);
 
403
                }
 
404
        }
 
405
        else if (g_atomic_int_compare_and_exchange (&pMeasureTimer->iThreadIsRunning, 0, 1))  // il etait egal a 0, on lui met 1 et on lance le thread.
 
406
        {
 
407
                //cd_debug (" ==> lancement du thread de calcul");
 
408
                
 
409
                if (pMeasureTimer->iSidTimerRedraw == 0) 
 
410
                        pMeasureTimer->iSidTimerRedraw = g_timeout_add (MAX (150, MIN (0.15 * pMeasureTimer->iCheckInterval, 333)), (GSourceFunc) _cairo_dock_check_for_redraw, pMeasureTimer);
 
411
                
 
412
                GError *erreur = NULL;
 
413
                GThread* pThread = g_thread_create ((GThreadFunc) _cairo_dock_threaded_calculation, pMeasureTimer, FALSE, &erreur);
 
414
                if (erreur != NULL)
 
415
                {
 
416
                        cd_warning (erreur->message);
 
417
                        g_error_free (erreur);
 
418
                }
 
419
        }
 
420
        else if (pMeasureTimer->iSidTimer == 0 && pMeasureTimer->iCheckInterval != 0)
 
421
        {
 
422
                pMeasureTimer->iFrequencyState = CAIRO_DOCK_FREQUENCY_NORMAL;
 
423
                pMeasureTimer->iSidTimer = g_timeout_add_seconds (pMeasureTimer->iCheckInterval, (GSourceFunc) _cairo_dock_timer, pMeasureTimer);
 
424
        }
 
425
}
 
426
 
 
427
static gboolean _cairo_dock_one_shot_timer (CairoDockMeasure *pMeasureTimer)
 
428
{
 
429
        pMeasureTimer->iSidTimerRedraw = 0;
 
430
        cairo_dock_launch_measure (pMeasureTimer);
 
431
        return FALSE;
 
432
}
 
433
void cairo_dock_launch_measure_delayed (CairoDockMeasure *pMeasureTimer, double fDelay)
 
434
{
 
435
        pMeasureTimer->iSidTimerRedraw = g_timeout_add (fDelay, (GSourceFunc) _cairo_dock_one_shot_timer, pMeasureTimer);
 
436
}
 
437
 
 
438
CairoDockMeasure *cairo_dock_new_measure_timer (int iCheckInterval, CairoDockAquisitionTimerFunc acquisition, CairoDockReadTimerFunc read, CairoDockUpdateTimerFunc update, gpointer pUserData)
 
439
{
 
440
        CairoDockMeasure *pMeasureTimer = g_new0 (CairoDockMeasure, 1);
 
441
        if (read != NULL || acquisition != NULL)
 
442
                pMeasureTimer->pMutexData = g_mutex_new ();
 
443
        pMeasureTimer->iCheckInterval = iCheckInterval;
 
444
        pMeasureTimer->acquisition = acquisition;
 
445
        pMeasureTimer->read = read;
 
446
        pMeasureTimer->update = update;
 
447
        pMeasureTimer->pUserData = pUserData;
 
448
        return pMeasureTimer;
 
449
}
 
450
 
 
451
static void _cairo_dock_pause_measure_timer (CairoDockMeasure *pMeasureTimer)
 
452
{
 
453
        if (pMeasureTimer == NULL)
 
454
                return ;
 
455
        if (pMeasureTimer->iSidTimerRedraw != 0)
 
456
        {
 
457
                g_source_remove (pMeasureTimer->iSidTimerRedraw);
 
458
                pMeasureTimer->iSidTimerRedraw = 0;
 
459
        }
 
460
        if (pMeasureTimer->iSidTimer!= 0)
 
461
        {
 
462
                g_source_remove (pMeasureTimer->iSidTimer);
 
463
                pMeasureTimer->iSidTimer= 0;
 
464
        }
 
465
}
 
466
 
 
467
void cairo_dock_stop_measure_timer (CairoDockMeasure *pMeasureTimer)
 
468
{
 
469
        if (pMeasureTimer == NULL)
 
470
                return ;
 
471
        
 
472
        _cairo_dock_pause_measure_timer (pMeasureTimer);
 
473
        
 
474
        cd_message ("***on attend que le thread termine...(%d)", g_atomic_int_get (&pMeasureTimer->iThreadIsRunning));
 
475
        while (g_atomic_int_get (&pMeasureTimer->iThreadIsRunning))
 
476
                ///g_usleep (10);
 
477
                gtk_main_iteration ();
 
478
        cd_message ("***temine.");
 
479
}
 
480
 
 
481
void cairo_dock_free_measure_timer (CairoDockMeasure *pMeasureTimer)
 
482
{
 
483
        if (pMeasureTimer == NULL)
 
484
                return ;
 
485
        cairo_dock_stop_measure_timer (pMeasureTimer);
 
486
        
 
487
        if (pMeasureTimer->pMutexData != NULL)
 
488
                g_mutex_free (pMeasureTimer->pMutexData);
 
489
        g_free (pMeasureTimer);
 
490
}
 
491
 
 
492
gboolean cairo_dock_measure_is_active (CairoDockMeasure *pMeasureTimer)
 
493
{
 
494
        return (pMeasureTimer != NULL && pMeasureTimer->iSidTimer != 0);
 
495
}
 
496
 
 
497
static void _cairo_dock_restart_timer_with_frequency (CairoDockMeasure *pMeasureTimer, int iNewCheckInterval)
 
498
{
 
499
        gboolean bNeedsRestart = (pMeasureTimer->iSidTimer != 0);
 
500
        _cairo_dock_pause_measure_timer (pMeasureTimer);
 
501
        
 
502
        if (bNeedsRestart && iNewCheckInterval != 0)
 
503
                pMeasureTimer->iSidTimer = g_timeout_add_seconds (iNewCheckInterval, (GSourceFunc) _cairo_dock_timer, pMeasureTimer);
 
504
}
 
505
 
 
506
void cairo_dock_change_measure_frequency (CairoDockMeasure *pMeasureTimer, int iNewCheckInterval)
 
507
{
 
508
        g_return_if_fail (pMeasureTimer != NULL);
 
509
        pMeasureTimer->iCheckInterval = iNewCheckInterval;
 
510
        
 
511
        _cairo_dock_restart_timer_with_frequency (pMeasureTimer, iNewCheckInterval);
 
512
}
 
513
 
 
514
void cairo_dock_relaunch_measure_immediately (CairoDockMeasure *pMeasureTimer, int iNewCheckInterval)
 
515
{
 
516
        cairo_dock_stop_measure_timer (pMeasureTimer);  // on stoppe avant car on ne veut pas attendre la prochaine iteration.
 
517
        cairo_dock_change_measure_frequency (pMeasureTimer, iNewCheckInterval); // nouvelle frequence eventuelement.
 
518
        cairo_dock_launch_measure (pMeasureTimer);  // mesure immediate.
 
519
}
 
520
 
 
521
void cairo_dock_downgrade_frequency_state (CairoDockMeasure *pMeasureTimer)
 
522
{
 
523
        if (pMeasureTimer->iFrequencyState < CAIRO_DOCK_FREQUENCY_SLEEP)
 
524
        {
 
525
                pMeasureTimer->iFrequencyState ++;
 
526
                int iNewCheckInterval;
 
527
                switch (pMeasureTimer->iFrequencyState)
 
528
                {
 
529
                        case CAIRO_DOCK_FREQUENCY_LOW :
 
530
                                iNewCheckInterval = 2 * pMeasureTimer->iCheckInterval;
 
531
                        break ;
 
532
                        case CAIRO_DOCK_FREQUENCY_VERY_LOW :
 
533
                                iNewCheckInterval = 4 * pMeasureTimer->iCheckInterval;
 
534
                        break ;
 
535
                        case CAIRO_DOCK_FREQUENCY_SLEEP :
 
536
                                iNewCheckInterval = 10 * pMeasureTimer->iCheckInterval;
 
537
                        break ;
 
538
                        default :  // ne doit pas arriver.
 
539
                                iNewCheckInterval = pMeasureTimer->iCheckInterval;
 
540
                        break ;
 
541
                }
 
542
                
 
543
                cd_message ("degradation de la mesure (etat <- %d/%d)", pMeasureTimer->iFrequencyState, CAIRO_DOCK_NB_FREQUENCIES-1);
 
544
                _cairo_dock_restart_timer_with_frequency (pMeasureTimer, iNewCheckInterval);
 
545
        }
 
546
}
 
547
 
 
548
void cairo_dock_set_normal_frequency_state (CairoDockMeasure *pMeasureTimer)
 
549
{
 
550
        if (pMeasureTimer->iFrequencyState != CAIRO_DOCK_FREQUENCY_NORMAL)
 
551
        {
 
552
                pMeasureTimer->iFrequencyState = CAIRO_DOCK_FREQUENCY_NORMAL;
 
553
                _cairo_dock_restart_timer_with_frequency (pMeasureTimer, pMeasureTimer->iCheckInterval);
 
554
        }
 
555
}
 
556
 
 
557
//Utile pour jouer des fichiers son depuis le dock.
 
558
//A utiliser avec l'Objet UI 'u' dans les .conf
 
559
void cairo_dock_play_sound (const gchar *cSoundPath)
 
560
{
 
561
        cd_debug ("%s (%s)", __func__, cSoundPath);
 
562
        if (cSoundPath == NULL)
 
563
        {
 
564
                cd_warning ("No sound to play, halt.");
 
565
                return;
 
566
        }
 
567
        
 
568
        GError *erreur = NULL;
 
569
        gchar *cSoundCommand = NULL;
 
570
        if (g_file_test ("/usr/bin/play", G_FILE_TEST_EXISTS))
 
571
                cSoundCommand = g_strdup_printf("play \"%s\"", cSoundPath);
 
572
                
 
573
        else if (g_file_test ("/usr/bin/aplay", G_FILE_TEST_EXISTS))
 
574
                cSoundCommand = g_strdup_printf("aplay \"%s\"", cSoundPath);
 
575
        
 
576
        else if (g_file_test ("/usr/bin/paplay", G_FILE_TEST_EXISTS))
 
577
                cSoundCommand = g_strdup_printf("paplay \"%s\"", cSoundPath);
 
578
        
 
579
        cairo_dock_launch_command (cSoundCommand);
 
580
        
 
581
        g_free (cSoundCommand);
 
582
}
 
583
 
 
584
void cairo_dock_get_gnome_version (int *iMajor, int *iMinor, int *iMicro) {
 
585
        gchar *cContent = NULL;
 
586
        gsize length = 0;
 
587
        GError *erreur = NULL;
 
588
        g_file_get_contents ("/usr/share/gnome-about/gnome-version.xml", &cContent, &length, &erreur);
 
589
        
 
590
        if (erreur != NULL) {
 
591
                cd_warning ("Attention : %s", erreur->message);
 
592
                g_error_free (erreur);
 
593
                erreur = NULL;
 
594
                *iMajor = 0;
 
595
                *iMinor = 0;
 
596
                *iMicro = 0;
 
597
                return;
 
598
        }
 
599
        
 
600
        gchar **cLineList = g_strsplit (cContent, "\n", -1);
 
601
        gchar *cOneLine = NULL, *cMajor = NULL, *cMinor = NULL, *cMicro = NULL;
 
602
        int i, iMaj = 0, iMin = 0, iMic = 0;
 
603
        for (i = 0; cLineList[i] != NULL; i ++) {
 
604
                cOneLine = cLineList[i];
 
605
                if (*cOneLine == '\0')
 
606
                        continue;
 
607
                
 
608
                //Seeking for Major
 
609
                cMajor = g_strstr_len (cOneLine, -1, "<platform>");  //<platform>2</platform>
 
610
                if (cMajor != NULL) {
 
611
                        cMajor += 10; //On saute <platform>
 
612
                        gchar *str = strchr (cMajor, '<');
 
613
                        if (str != NULL)
 
614
                                *str = '\0'; //On bloque a </platform>
 
615
                        iMaj = atoi (cMajor);
 
616
                }
 
617
                else { //Gutsy xml's doesn't have <platform> but <major>
 
618
                        cMajor = g_strstr_len (cOneLine, -1, "<major>");  //<major>2</major>
 
619
                        if (cMajor != NULL) {
 
620
                                cMajor += 7; //On saute <major>
 
621
                                gchar *str = strchr (cMajor, '<');
 
622
                                if (str != NULL)
 
623
                                        *str = '\0'; //On bloque a </major>
 
624
                                iMaj = atoi (cMajor);
 
625
                        }
 
626
                }
 
627
                
 
628
                //Seeking for Minor
 
629
                cMinor = g_strstr_len (cOneLine, -1, "<minor>");  //<minor>22</minor>
 
630
                if (cMinor != NULL) {
 
631
                        cMinor += 7; //On saute <minor>
 
632
                        gchar *str = strchr (cMinor, '<');
 
633
                        if (str != NULL)
 
634
                                *str = '\0'; //On bloque a </minor>
 
635
                        iMin = atoi (cMinor);
 
636
                }
 
637
                
 
638
                //Seeking for Micro
 
639
                cMicro = g_strstr_len (cOneLine, -1, "<micro>");  //<micro>3</micro>
 
640
                if (cMicro != NULL) {
 
641
                        cMicro += 7; //On saute <micro>
 
642
                        gchar *str = strchr (cMicro, '<');
 
643
                        if (str != NULL)
 
644
                                *str = '\0'; //On bloque a </micro>
 
645
                        iMic = atoi (cMicro);
 
646
                }
 
647
                
 
648
                if (iMaj != 0 && iMin != 0 && iMic != 0)
 
649
                        break; //On s'enfou du reste
 
650
        }
 
651
        
 
652
        cd_debug ("Gnome Version %d.%d.%d", iMaj, iMin, iMic);
 
653
        
 
654
        *iMajor = iMaj;
 
655
        *iMinor = iMin;
 
656
        *iMicro = iMic;
 
657
        
 
658
        g_free (cContent);
 
659
        g_strfreev (cLineList);
 
660
}