~ubuntu-branches/ubuntu/saucy/cairo-dock-plug-ins/saucy

« back to all changes in this revision

Viewing changes to powermanager/src/powermanager-init.c

  • Committer: Bazaar Package Importer
  • Author(s): Didier Roche
  • Date: 2009-08-26 21:07:39 UTC
  • Revision ID: james.westby@ubuntu.com-20090826210739-gyjuuqezrzuluao4
Tags: upstream-2.0.8.1
Import upstream version 2.0.8.1

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 <cairo-dock.h>
 
22
 
 
23
#include "powermanager-draw.h"
 
24
#include "powermanager-config.h"
 
25
#include "powermanager-dbus.h"
 
26
#include "powermanager-menu-functions.h"
 
27
#include "powermanager-struct.h"
 
28
#include "powermanager-init.h"
 
29
 
 
30
 
 
31
CD_APPLET_DEFINITION ("PowerManager",
 
32
        1, 6, 2,
 
33
        CAIRO_DOCK_CATEGORY_ACCESSORY,
 
34
        N_("A power manager for laptop's battery. It works with ACPI and DBus."),
 
35
        "Necropotame (Adrien Pilleboue)")
 
36
 
 
37
 
 
38
CD_APPLET_INIT_BEGIN
 
39
        
 
40
        if (myDesklet)
 
41
                CD_APPLET_SET_DESKLET_RENDERER ("Simple");
 
42
        
 
43
        // on ne charge pas toutes les surfaces, car cela prend trop de memoire, et trop de temps au chargement, alors que ce n'est pas necessaire. En effet, on ne redessine que si il y'a changement. Or la batterie se vide lentement, et la recharge n'est pas non plus fulgurante, donc au total on redesine reellement l'icone 1 fois toutes les 10 minutes peut-etre, ce qui ne justifie pas de pre-charger les surfaces.
 
44
        
 
45
        myData.dbus_enable = dbus_connect_to_bus ();
 
46
        if (myData.dbus_enable)
 
47
        {
 
48
                if(myData.battery_present)
 
49
                {
 
50
                        ///get_on_battery();
 
51
                        
 
52
                        if (myConfig.bUseGauge)
 
53
                        {
 
54
                                double fMaxScale = cairo_dock_get_max_scale (myContainer);
 
55
                                myData.pGauge = cairo_dock_load_gauge (myDrawContext, myConfig.cGThemePath, myIcon->fWidth * fMaxScale, myIcon->fHeight * fMaxScale);
 
56
                        }
 
57
                        
 
58
                        myData.previous_battery_charge = -1;
 
59
                        myData.previous_battery_time = -1;
 
60
                        myData.alerted = TRUE;
 
61
                        myData.bCritical = TRUE;
 
62
                        update_stats();
 
63
                        myData.checkLoop = g_timeout_add_seconds (myConfig.iCheckInterval, (GSourceFunc) update_stats, (gpointer) NULL);
 
64
                }
 
65
                else
 
66
                        CD_APPLET_SET_LOCAL_IMAGE_ON_MY_ICON ("sector.svg");
 
67
 
 
68
        }
 
69
        else  // sinon on signale par l'icone appropriee que le bus n'est pas accessible.
 
70
                CD_APPLET_SET_LOCAL_IMAGE_ON_MY_ICON ("broken.svg");
 
71
        
 
72
        CD_APPLET_REGISTER_FOR_CLICK_EVENT;
 
73
        CD_APPLET_REGISTER_FOR_BUILD_MENU_EVENT;
 
74
CD_APPLET_INIT_END
 
75
 
 
76
 
 
77
CD_APPLET_STOP_BEGIN
 
78
        CD_APPLET_UNREGISTER_FOR_CLICK_EVENT;
 
79
        CD_APPLET_UNREGISTER_FOR_BUILD_MENU_EVENT;
 
80
        
 
81
        dbus_disconnect_from_bus ();
 
82
        
 
83
        if(myData.checkLoop != 0)
 
84
        {
 
85
                g_source_remove (myData.checkLoop);
 
86
                myData.checkLoop = 0;
 
87
        }
 
88
CD_APPLET_STOP_END
 
89
 
 
90
 
 
91
CD_APPLET_RELOAD_BEGIN
 
92
        if (myDesklet)
 
93
                CD_APPLET_SET_DESKLET_RENDERER ("Simple");
 
94
        
 
95
        double fMaxScale = cairo_dock_get_max_scale (myContainer);
 
96
        if (CD_APPLET_MY_CONFIG_CHANGED)
 
97
        {
 
98
                cairo_dock_free_gauge(myData.pGauge);
 
99
                myData.pGauge = cairo_dock_load_gauge(myDrawContext,myConfig.cGThemePath,myIcon->fWidth * fMaxScale,myIcon->fHeight * fMaxScale);
 
100
                
 
101
                if(myData.checkLoop != 0)  // la frequence peut avoir change.
 
102
                {
 
103
                        g_source_remove (myData.checkLoop);
 
104
                        myData.checkLoop = 0;
 
105
                }
 
106
                myData.checkLoop = g_timeout_add_seconds (myConfig.iCheckInterval, (GSourceFunc) update_stats, (gpointer) NULL);
 
107
                
 
108
        }
 
109
        else
 
110
                cairo_dock_reload_gauge (myDrawContext, myData.pGauge, myIcon->fWidth * fMaxScale,myIcon->fHeight * fMaxScale);
 
111
        
 
112
        //\_______________ On redessine notre icone.
 
113
        if (myData.dbus_enable)
 
114
        {
 
115
                if(myData.battery_present)
 
116
                {
 
117
                        if (myConfig.bUseGauge)  // On recharge la jauge.
 
118
                        {
 
119
                                ///double fMaxScale = (myDock != NULL ? 1 + g_fAmplitude : 1);
 
120
                                ///myData.pGauge = cairo_dock_load_gauge(myDrawContext,myConfig.cThemePath,myIcon->fWidth * fMaxScale,myIcon->fHeight * fMaxScale);
 
121
                                
 
122
                                cairo_dock_render_gauge (myDrawContext, myContainer, myIcon, myData.pGauge, (double) myData.battery_charge / 100);
 
123
                                
 
124
                                //Embleme sur notre icône
 
125
                                CD_APPLET_DRAW_EMBLEM ((myData.on_battery ? CAIRO_DOCK_EMBLEM_BLANK : CAIRO_DOCK_EMBLEM_CHARGE), CAIRO_DOCK_EMBLEM_MIDDLE);
 
126
                        }
 
127
                        else  // on redessine juste l'icone actuelle.
 
128
                                cd_powermanager_draw_icon_with_effect (myData.on_battery);
 
129
                        
 
130
                        if (!myData.on_battery && myData.battery_charge < 100)
 
131
                                myData.alerted = FALSE; //We will alert when battery charge reach 100%
 
132
                        if (myData.on_battery)
 
133
                        {
 
134
                                if (myData.battery_charge > myConfig.lowBatteryValue)
 
135
                                        myData.alerted = FALSE; //We will alert when battery charge is under myConfig.lowBatteryValue
 
136
                                
 
137
                                if (myData.battery_charge > 4)
 
138
                                        myData.bCritical = FALSE; //We will alert when battery charge is critical (under 4%)
 
139
                        }
 
140
                        
 
141
                        myData.previous_battery_charge = -1;
 
142
                        myData.previous_battery_time = -1;
 
143
                        update_icon();
 
144
                }
 
145
                else
 
146
                        CD_APPLET_SET_LOCAL_IMAGE_ON_MY_ICON ("sector.svg");
 
147
 
 
148
        }
 
149
        else  // sinon on signale par l'icone appropriee que le bus n'est pas accessible.
 
150
                CD_APPLET_SET_LOCAL_IMAGE_ON_MY_ICON ("broken.svg");
 
151
        
 
152
CD_APPLET_RELOAD_END