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

« back to all changes in this revision

Viewing changes to wifi/src/applet-wifi.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
/******************************************************************************
 
21
 
 
22
This file is a part of the cairo-dock program, 
 
23
released under the terms of the GNU General Public License.
 
24
 
 
25
Written by Rémy Robertson (for any bug report, please mail me to changfu@cairo-dock.org)
 
26
Fabrice Rey <fabounet@users.berlios.de>
 
27
 
 
28
******************************************************************************/
 
29
#define _BSD_SOURCE
 
30
#include <stdlib.h>
 
31
#include <stdio.h>
 
32
#include <unistd.h>
 
33
#include <string.h>
 
34
#include <glib/gi18n.h>
 
35
#include <glib/gstdio.h>
 
36
 
 
37
#include "applet-struct.h"
 
38
#include "applet-notifications.h"
 
39
#include "applet-draw.h"
 
40
#include "applet-wifi.h"
 
41
 
 
42
 
 
43
#define _pick_string(cValueName, cValue) \
 
44
        str = g_strstr_len (cOneInfopipe, -1, cValueName);\
 
45
        if (str) {\
 
46
                str += strlen (cValueName) + 1;\
 
47
                if (*str == ' ')\
 
48
                        str ++;\
 
49
                if (*str == '"') {\
 
50
                        str ++;\
 
51
                        str2 = strchr (str, '"'); }\
 
52
                else {\
 
53
                        str2 = strchr (str, ' '); }\
 
54
                if (str2) {\
 
55
                        cValue = g_strndup (str, str2 - str);\
 
56
                        cd_debug ("%s : %s", cValueName, cValue); } }
 
57
#define _pick_value(cValueName, iValue, iMaxValue)\
 
58
        str = g_strstr_len (cOneInfopipe, -1, cValueName);\
 
59
        if (str) {\
 
60
                str += strlen (cValueName) + 1;\
 
61
                iValue = atoi (str);\
 
62
                str2 = strchr (str, '/');\
 
63
                if (str2)\
 
64
                        iMaxValue = atoi (str2+1);\
 
65
                cd_debug ("%s : %d (/%d)", cValueName, iValue, iMaxValue); }
 
66
 
 
67
void cd_wifi_get_data (gpointer data)
 
68
{
 
69
        myData.iPreviousQuality = myData.iQuality;
 
70
        myData.iQuality = -1;
 
71
        myData.iPrevPercent = myData.iPercent;
 
72
        myData.iPercent = -1;
 
73
        myData.iPrevSignalLevel = myData.iSignalLevel;
 
74
        myData.iSignalLevel = -1;
 
75
        myData.iPrevNoiseLevel = myData.iNoiseLevel;
 
76
        myData.iNoiseLevel = -1;
 
77
        g_free (myData.cESSID);
 
78
        myData.cESSID = NULL;
 
79
        g_free (myData.cInterface);
 
80
        myData.cInterface = NULL;
 
81
        g_free (myData.cAccessPoint);
 
82
        myData.cAccessPoint = NULL;
 
83
        
 
84
        gchar *cResult = cairo_dock_launch_command_sync (MY_APPLET_SHARE_DATA_DIR"/wifi");
 
85
        if (cResult == NULL || *cResult == '\0')  //   // erreur a l'execution d'iwconfig (probleme de droit d'execution ou iwconfig pas installe) ou aucune interface wifi presente
 
86
        { 
 
87
                g_free (cResult);
 
88
                return ;
 
89
        }
 
90
        
 
91
        gchar **cInfopipesList = g_strsplit (cResult, "\n", -1);
 
92
        g_free (cResult);
 
93
        gchar *cOneInfopipe, *str, *str2;
 
94
        int i, iMaxValue;
 
95
        for (i = 0; cInfopipesList[i] != NULL; i ++)
 
96
        {
 
97
                cOneInfopipe = cInfopipesList[i];
 
98
                if (*cOneInfopipe == '\0' || *cOneInfopipe == '\n' )
 
99
                        continue;
 
100
                
 
101
                if (myData.cInterface != NULL && *cOneInfopipe != ' ')  // nouvelle interface, on n'en veut qu'une.
 
102
                        break ;
 
103
                
 
104
                if (myData.cInterface == NULL && *cOneInfopipe != ' ')
 
105
                {
 
106
                        str = cOneInfopipe;  // le nom de l'interface est en debut de ligne.
 
107
                        str2 = strchr (str, ' ');
 
108
                        if (str2)
 
109
                        {
 
110
                                myData.cInterface = g_strndup (cOneInfopipe, str2 - str);
 
111
                                cd_debug ("interface : %s", myData.cInterface);
 
112
                        }
 
113
                }
 
114
                
 
115
                if (myData.cESSID == NULL)
 
116
                {
 
117
                        _pick_string ("ESSID", myData.cESSID);  // eth1 IEEE 802.11g ESSID:"bla bla bla"
 
118
                }
 
119
                /*if (myData.cNickName == NULL)
 
120
                {
 
121
                        _pick_string ("Nickname", myData.cNickName);
 
122
                }*/
 
123
                if (myData.cAccessPoint == NULL)
 
124
                {
 
125
                        _pick_string ("Access Point", myData.cAccessPoint);
 
126
                }
 
127
                
 
128
                if (myData.iQuality == -1)  // Link Quality=54/100 Signal level=-76 dBm Noise level=-78 dBm OU Link Quality:5  Signal level:219  Noise level:177
 
129
                {
 
130
                        iMaxValue = 0;
 
131
                        _pick_value ("Link Quality", myData.iQuality, iMaxValue);
 
132
                        if (iMaxValue != 0)  // vieille version, qualite indiquee en %
 
133
                        {
 
134
                                myData.iPercent = 100. * myData.iQuality / iMaxValue;
 
135
                                if (myData.iPercent <= 0)
 
136
                                        myData.iQuality = WIFI_QUALITY_NO_SIGNAL;
 
137
                                else if (myData.iPercent < 20)
 
138
                                        myData.iQuality = WIFI_QUALITY_VERY_LOW;
 
139
                                else if (myData.iPercent < 40)
 
140
                                        myData.iQuality = WIFI_QUALITY_LOW;
 
141
                                else if (myData.iPercent < 60)
 
142
                                        myData.iQuality = WIFI_QUALITY_MIDDLE;
 
143
                                else if (myData.iPercent < 80)
 
144
                                        myData.iQuality = WIFI_QUALITY_GOOD;
 
145
                                else
 
146
                                        myData.iQuality = WIFI_QUALITY_EXCELLENT;
 
147
                        }
 
148
                        else
 
149
                        {
 
150
                                myData.iPercent = 100. * myData.iQuality / (WIFI_NB_QUALITY-1);
 
151
                        }
 
152
                }
 
153
                if (myData.iSignalLevel == -1)
 
154
                {
 
155
                        _pick_value ("Signal level", myData.iSignalLevel, iMaxValue);
 
156
                }
 
157
                if (myData.iNoiseLevel == -1)
 
158
                {
 
159
                        _pick_value ("Noise level", myData.iNoiseLevel, iMaxValue);
 
160
                }
 
161
        }
 
162
        g_strfreev (cInfopipesList);
 
163
}
 
164
 
 
165
 
 
166
gboolean cd_wifi_update_from_data (gpointer data)
 
167
{
 
168
        if (myData.cInterface != NULL)
 
169
        {
 
170
                myData.bWirelessExt = TRUE;
 
171
                cd_wifi_draw_icon ();
 
172
                cairo_dock_set_normal_task_frequency (myData.pTask);
 
173
        }
 
174
        else
 
175
        {
 
176
                myData.bWirelessExt = FALSE;
 
177
                cd_wifi_draw_no_wireless_extension ();
 
178
                cairo_dock_downgrade_task_frequency (myData.pTask);
 
179
        }
 
180
        return TRUE;
 
181
}