2
* This file is a part of the Cairo-Dock project
4
* Copyright : (C) see the 'copyright' file.
5
* E-mail : see the 'copyright' file.
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.
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/>.
23
#include <glib/gi18n.h>
25
#include <glib/gprintf.h>
27
#include "applet-struct.h"
28
#include "applet-notifications.h"
29
#include "applet-netspeed.h"
30
#include "cairo-dock.h"
33
#define NETSPEED_DATA_PIPE "/proc/net/dev"
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) {
43
g_sprintf(debit, "0 %s/s", D_("B"));
45
g_sprintf(debit, "0");
51
g_sprintf(debit, "%i %s/s", smallRate, D_("B"));
53
g_sprintf(debit, "%iB", smallRate);
55
else if (rate < (1<<20))
57
smallRate = rate >> 10;
59
g_sprintf(debit, "%i %s/s", smallRate, D_("KB"));
61
g_sprintf(debit, "%iK", smallRate);
63
else if (rate < (1<<30))
65
smallRate = rate >> 20;
67
g_sprintf(debit, "%i %s/s", smallRate, D_("MB"));
69
g_sprintf(debit, "%iM", smallRate);
71
else if (rate < ((unsigned long long)1<<40))
73
smallRate = rate >> 30;
75
g_sprintf(debit, "%i %s/s", smallRate, D_("GB"));
77
g_sprintf(debit, "%iG", smallRate);
79
else // c'est vraiment pour dire qu'on est exhaustif :-)
81
smallRate = rate >> 40;
83
g_sprintf(debit, "%i %s/s", smallRate, D_("TB"));
85
g_sprintf(debit, "%iT", smallRate);
90
void cd_netspeed_get_data (CairoDockModuleInstance *myApplet)
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);
97
gchar *cContent = NULL;
99
GError *erreur = NULL;
100
g_file_get_contents (NETSPEED_DATA_PIPE, &cContent, &length, &erreur);
103
cd_warning("NetSpeed : %s", erreur->message);
104
g_error_free(erreur);
106
myData.bAcquisitionOK = FALSE;
111
gchar *tmp = cContent;
112
long long int iReceivedBytes, iTransmittedBytes;
115
if (iNumLine > 3) // les 2 premieres lignes sont les noms des champs, la 3eme est la loopback.
117
while (*tmp == ' ') // on saute les espaces.
120
if (strncmp (tmp, myConfig.cInterface, myConfig.iStringLen) == 0 && *(tmp+myConfig.iStringLen) == ':') // c'est l'interface qu'on veut.
122
tmp += myConfig.iStringLen+1; // on saute le ':' avec.
123
iReceivedBytes = atoll (tmp);
126
for (i = 0; i < 8; i ++) // on saute les 8 valeurs suivantes.
128
while (*tmp != ' ') // saute le chiffre courant.
130
while (*tmp == ' ') // saute les espaces.
133
iTransmittedBytes = atoll (tmp);
135
if (myData.bInitialized) // la 1ere iteration on ne peut pas calculer le debit.
137
myData.iDownloadSpeed = (iReceivedBytes - myData.iReceivedBytes) / fTimeElapsed;
138
myData.iUploadSpeed = (iTransmittedBytes - myData.iTransmittedBytes) / fTimeElapsed;
141
myData.iReceivedBytes = iReceivedBytes;
142
myData.iTransmittedBytes = iTransmittedBytes;
146
tmp = strchr (tmp+1, '\n');
152
myData.bAcquisitionOK = (tmp != NULL);
154
if (! myData.bInitialized)
155
myData.bInitialized = TRUE;
159
gboolean cd_netspeed_update_from_data (CairoDockModuleInstance *myApplet)
161
static double s_fValues[CD_NETSPEED_NB_MAX_VALUES];
163
if ( ! myData.bAcquisitionOK)
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");
170
memset (s_fValues, 0, sizeof (s_fValues));
171
CD_APPLET_RENDER_NEW_DATA_ON_MY_ICON (s_fValues);
173
cairo_dock_downgrade_task_frequency (myData.pPeriodicTask);
177
cairo_dock_set_normal_task_frequency (myData.pPeriodicTask);
179
if (! myData.bInitialized)
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);
188
if (myConfig.iInfoDisplay != CAIRO_DOCK_INFO_NONE)
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)
196
CD_APPLET_SET_QUICK_INFO_ON_MY_ICON_PRINTF ("↓%s\n↑%s", downRateFormatted, upRateFormatted);
200
CD_APPLET_SET_NAME_FOR_MY_ICON_PRINTF ("↓%s\n↑%s", downRateFormatted, upRateFormatted);
204
if(myData.iUploadSpeed > myData.iMaxUpRate) {
205
myData.iMaxUpRate = myData.iUploadSpeed;
207
if(myData.iDownloadSpeed > myData.iMaxDownRate) {
208
myData.iMaxDownRate = myData.iDownloadSpeed;
211
double fUpValue, fDownValue;
212
if (myData.iMaxUpRate != 0)
213
fUpValue = (double) myData.iUploadSpeed / myData.iMaxUpRate;
216
if (myData.iMaxDownRate != 0)
217
fDownValue = (double) myData.iDownloadSpeed / myData.iMaxDownRate;
222
s_fValues[i++] = fUpValue;
223
s_fValues[i] = fDownValue;
224
CD_APPLET_RENDER_NEW_DATA_ON_MY_ICON (s_fValues);