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

« back to all changes in this revision

Viewing changes to netspeed/src/applet-netspeed.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 <stdio.h>
 
22
#include <string.h>
 
23
#include <glib/gi18n.h>
 
24
#include <glib.h>
 
25
#include <glib/gprintf.h>
 
26
 
 
27
#include "applet-struct.h"
 
28
#include "applet-notifications.h"
 
29
#include "applet-netspeed.h"
 
30
#include "cairo-dock.h"
 
31
 
 
32
 
 
33
#define NETSPEED_DATA_PIPE "/proc/net/dev"
 
34
 
 
35
 
 
36
// Prend un debit en octet par seconde et le transforme en une chaine de la forme : xxx yB/s
 
37
static void cd_netspeed_formatRate (CairoDockModuleInstance *myApplet, unsigned long long rate, gchar* debit) {
 
38
        int smallRate;
 
39
        
 
40
        if (rate <= 0)
 
41
        {
 
42
                if (myDesklet)
 
43
                        g_sprintf(debit, "0 %s/s", D_("B"));
 
44
                else
 
45
                        g_sprintf(debit, "0");
 
46
        }
 
47
        else if (rate < 1024)
 
48
        {
 
49
                smallRate = rate;
 
50
                if (myDesklet)
 
51
                        g_sprintf(debit, "%i %s/s", smallRate, D_("B"));
 
52
                else
 
53
                        g_sprintf(debit, "%iB", smallRate);
 
54
        }
 
55
        else if (rate < (1<<20))
 
56
        {
 
57
                smallRate = rate >> 10;
 
58
                if (myDesklet)
 
59
                        g_sprintf(debit, "%i %s/s", smallRate, D_("KB"));
 
60
                else
 
61
                        g_sprintf(debit, "%iK", smallRate);
 
62
        }
 
63
        else if (rate < (1<<30))
 
64
        {
 
65
                smallRate = rate >> 20;
 
66
                if (myDesklet)
 
67
                        g_sprintf(debit, "%i %s/s", smallRate, D_("MB"));
 
68
                else
 
69
                        g_sprintf(debit, "%iM", smallRate);
 
70
        }
 
71
        else if (rate < ((unsigned long long)1<<40))
 
72
        {
 
73
                smallRate = rate >> 30;
 
74
                if (myDesklet)
 
75
                        g_sprintf(debit, "%i %s/s", smallRate, D_("GB"));
 
76
                else
 
77
                        g_sprintf(debit, "%iG", smallRate);
 
78
        }
 
79
        else  // c'est vraiment pour dire qu'on est exhaustif :-)
 
80
        {
 
81
                smallRate = rate >> 40;
 
82
                if (myDesklet)
 
83
                        g_sprintf(debit, "%i %s/s", smallRate, D_("TB"));
 
84
                else
 
85
                        g_sprintf(debit, "%iT", smallRate);
 
86
        }
 
87
}
 
88
 
 
89
 
 
90
void cd_netspeed_get_data (CairoDockModuleInstance *myApplet)
 
91
{
 
92
        g_timer_stop (myData.pClock);
 
93
        double fTimeElapsed = g_timer_elapsed (myData.pClock, NULL);
 
94
        g_timer_start (myData.pClock);
 
95
        g_return_if_fail (fTimeElapsed > 0.1);
 
96
        
 
97
        gchar *cContent = NULL;
 
98
        gsize length=0;
 
99
        GError *erreur = NULL;
 
100
        g_file_get_contents (NETSPEED_DATA_PIPE, &cContent, &length, &erreur);
 
101
        if (erreur != NULL)
 
102
        {
 
103
                cd_warning("NetSpeed : %s", erreur->message);
 
104
                g_error_free(erreur);
 
105
                erreur = NULL;
 
106
                myData.bAcquisitionOK = FALSE;
 
107
        }
 
108
        else
 
109
        {
 
110
                int iNumLine = 1;
 
111
                gchar *tmp = cContent;
 
112
                long long int iReceivedBytes, iTransmittedBytes;
 
113
                while (TRUE)
 
114
                {
 
115
                        if (iNumLine > 3)  // les 2 premieres lignes sont les noms des champs, la 3eme est la loopback.
 
116
                        {
 
117
                                while (*tmp == ' ')  // on saute les espaces.
 
118
                                        tmp ++;
 
119
                                
 
120
                                if (strncmp (tmp, myConfig.cInterface, myConfig.iStringLen) == 0 && *(tmp+myConfig.iStringLen) == ':')  // c'est l'interface qu'on veut.
 
121
                                {
 
122
                                        tmp += myConfig.iStringLen+1;  // on saute le ':' avec.
 
123
                                        iReceivedBytes = atoll (tmp);
 
124
                                        
 
125
                                        int i = 0;
 
126
                                        for (i = 0; i < 8; i ++)  // on saute les 8 valeurs suivantes.
 
127
                                        {
 
128
                                                while (*tmp != ' ')  // saute le chiffre courant.
 
129
                                                        tmp ++;
 
130
                                                while (*tmp == ' ')  // saute les espaces.
 
131
                                                        tmp ++;
 
132
                                        }
 
133
                                        iTransmittedBytes = atoll (tmp);
 
134
                                        
 
135
                                        if (myData.bInitialized)  // la 1ere iteration on ne peut pas calculer le debit.
 
136
                                        {
 
137
                                                myData.iDownloadSpeed = (iReceivedBytes - myData.iReceivedBytes) / fTimeElapsed;
 
138
                                                myData.iUploadSpeed = (iTransmittedBytes - myData.iTransmittedBytes) / fTimeElapsed;
 
139
                                        }
 
140
                                        
 
141
                                        myData.iReceivedBytes = iReceivedBytes;
 
142
                                        myData.iTransmittedBytes = iTransmittedBytes;
 
143
                                        break ;
 
144
                                }
 
145
                        }
 
146
                        tmp = strchr (tmp+1, '\n');
 
147
                        if (tmp == NULL)
 
148
                                break;
 
149
                        tmp ++;
 
150
                        iNumLine ++;
 
151
                }
 
152
                myData.bAcquisitionOK = (tmp != NULL);
 
153
                g_free (cContent);
 
154
                if (! myData.bInitialized)
 
155
                        myData.bInitialized = TRUE;
 
156
        }
 
157
}
 
158
 
 
159
gboolean cd_netspeed_update_from_data (CairoDockModuleInstance *myApplet)
 
160
{
 
161
        static double s_fValues[CD_NETSPEED_NB_MAX_VALUES];
 
162
        
 
163
        if ( ! myData.bAcquisitionOK)
 
164
        {
 
165
                if (myConfig.iInfoDisplay == CAIRO_DOCK_INFO_ON_LABEL)
 
166
                        CD_APPLET_SET_NAME_FOR_MY_ICON (myConfig.defaultTitle);
 
167
                else if (myConfig.iInfoDisplay == CAIRO_DOCK_INFO_ON_ICON)
 
168
                        CD_APPLET_SET_QUICK_INFO_ON_MY_ICON ("N/A");
 
169
                
 
170
                memset (s_fValues, 0, sizeof (s_fValues));
 
171
                CD_APPLET_RENDER_NEW_DATA_ON_MY_ICON (s_fValues);
 
172
                
 
173
                cairo_dock_downgrade_task_frequency (myData.pPeriodicTask);
 
174
        }
 
175
        else
 
176
        {
 
177
                cairo_dock_set_normal_task_frequency (myData.pPeriodicTask);
 
178
                
 
179
                if (! myData.bInitialized)
 
180
                {
 
181
                        if (myConfig.iInfoDisplay == CAIRO_DOCK_INFO_ON_ICON)
 
182
                                CD_APPLET_SET_QUICK_INFO_ON_MY_ICON (myDock ? "..." : D_("Loading"));
 
183
                        memset (s_fValues, 0, sizeof (s_fValues));
 
184
                        CD_APPLET_RENDER_NEW_DATA_ON_MY_ICON (s_fValues);
 
185
                }
 
186
                else
 
187
                {
 
188
                        if (myConfig.iInfoDisplay != CAIRO_DOCK_INFO_NONE)
 
189
                        {
 
190
                                gchar upRateFormatted[11];
 
191
                                gchar downRateFormatted[11];
 
192
                                cd_netspeed_formatRate (myApplet, myData.iUploadSpeed, upRateFormatted);
 
193
                                cd_netspeed_formatRate (myApplet, myData.iDownloadSpeed, downRateFormatted);
 
194
                                if (myConfig.iInfoDisplay == CAIRO_DOCK_INFO_ON_ICON)
 
195
                                {
 
196
                                        CD_APPLET_SET_QUICK_INFO_ON_MY_ICON_PRINTF ("↓%s\n↑%s", downRateFormatted, upRateFormatted);
 
197
                                }
 
198
                                else
 
199
                                {
 
200
                                        CD_APPLET_SET_NAME_FOR_MY_ICON_PRINTF ("↓%s\n↑%s", downRateFormatted, upRateFormatted);
 
201
                                }
 
202
                        }
 
203
                        
 
204
                        if(myData.iUploadSpeed > myData.iMaxUpRate) {
 
205
                                myData.iMaxUpRate = myData.iUploadSpeed;
 
206
                        }
 
207
                        if(myData.iDownloadSpeed > myData.iMaxDownRate) {
 
208
                                myData.iMaxDownRate = myData.iDownloadSpeed;
 
209
                        }
 
210
                        
 
211
                        double fUpValue, fDownValue;
 
212
                        if (myData.iMaxUpRate != 0)
 
213
                                fUpValue = (double) myData.iUploadSpeed / myData.iMaxUpRate;
 
214
                        else
 
215
                                fUpValue = 0.;
 
216
                        if (myData.iMaxDownRate != 0)
 
217
                                fDownValue = (double) myData.iDownloadSpeed / myData.iMaxDownRate;
 
218
                        else
 
219
                                fDownValue = 0.;
 
220
                        
 
221
                        int i = 0;
 
222
                        s_fValues[i++] = fUpValue;
 
223
                        s_fValues[i] = fDownValue;
 
224
                        CD_APPLET_RENDER_NEW_DATA_ON_MY_ICON (s_fValues);
 
225
                }
 
226
        }
 
227
        return TRUE;
 
228
}