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 
25 #include "applet-struct.h"
26 #include "applet-wifi.h"
27 #include "applet-draw.h"
28 
29 
30 static const gchar *s_cLevelQualityName[WIFI_NB_QUALITY] = {N_("None"), N_("Very Low"), N_("Low"), N_("Middle"), N_("Good"), N_("Excellent")};
31 
32 
cd_wifi_draw_no_wireless_extension(void)33 void cd_wifi_draw_no_wireless_extension (void)
34 {
35 	cd_debug ("No Wireless: %d, %d", myData.iPreviousQuality, myData.iQuality);
36 	if (myData.iPreviousQuality != myData.iQuality)
37 	{
38 		if (myDesklet != NULL)
39 			CD_APPLET_SET_DESKLET_RENDERER ("Simple");
40 		myData.iPreviousQuality = myData.iQuality;
41 
42 		// reset label
43 		if (myConfig.defaultTitle) // has another default name
44 			CD_APPLET_SET_NAME_FOR_MY_ICON (myConfig.defaultTitle);
45 		else
46 			CD_APPLET_SET_NAME_FOR_MY_ICON (myApplet->pModule->pVisitCard->cTitle);
47 
48 		// reset quick-info
49 		if (myConfig.quickInfoType != WIFI_INFO_NONE) // if we want to have a quick info
50 			CD_APPLET_SET_QUICK_INFO_ON_MY_ICON ("N/A");
51 
52 		// reset the data-renderer
53 		if (myConfig.iDisplayType == CD_WIFI_BAR)
54 			CD_APPLET_SET_USER_IMAGE_ON_MY_ICON (myConfig.cNoSignalIcon, "no-signal.svg");
55 		double fValue = CAIRO_DATA_RENDERER_UNDEF_VALUE;
56 		CD_APPLET_RENDER_NEW_DATA_ON_MY_ICON (&fValue);
57 	}
58 	else if (myConfig.iDisplayType == CD_WIFI_GRAPH)
59 	{
60 		 double fValue = CAIRO_DATA_RENDERER_UNDEF_VALUE;
61 		CD_APPLET_RENDER_NEW_DATA_ON_MY_ICON (&fValue);
62 	}
63 }
64 
cd_wifi_draw_icon(void)65 void cd_wifi_draw_icon (void)
66 {
67 	cd_debug ("Draw Wireless: %d, %d", myData.iPreviousQuality, myData.iQuality);
68 	if (myData.iPercent <= 0)
69 	{
70 		cd_wifi_draw_no_wireless_extension (); // not connected
71 		return;
72 	}
73 
74 	// update quick-info
75 	gboolean bNeedRedraw = FALSE;
76 	switch (myConfig.quickInfoType)
77 	{
78 		case WIFI_INFO_SIGNAL_STRENGTH_LEVEL :
79 			if (myData.iQuality != myData.iPreviousQuality && myData.iQuality < WIFI_NB_QUALITY)
80 			{
81 				CD_APPLET_SET_QUICK_INFO_ON_MY_ICON (D_(s_cLevelQualityName[myData.iQuality]));
82 				bNeedRedraw = TRUE;
83 			}
84 		break;
85 		case WIFI_INFO_SIGNAL_STRENGTH_PERCENT :
86 			if (myData.iPrevPercent != myData.iPercent)
87 			{
88 				myData.iPrevPercent = myData.iPercent;
89 				CD_APPLET_SET_QUICK_INFO_ON_MY_ICON_PRINTF ("%d%%", myData.iPercent);
90 				bNeedRedraw = TRUE;
91 			}
92 		break;
93 		case WIFI_INFO_SIGNAL_STRENGTH_DB :
94 			if (myData.iPrevSignalLevel != myData.iSignalLevel || myData.iPrevNoiseLevel != myData.iNoiseLevel)
95 			{
96 				myData.iPrevSignalLevel = myData.iSignalLevel;
97 				myData.iPrevNoiseLevel = myData.iNoiseLevel;
98 				CD_APPLET_SET_QUICK_INFO_ON_MY_ICON_PRINTF ("%d/%d", myData.iSignalLevel, myData.iNoiseLevel);
99 				bNeedRedraw = TRUE;
100 			}
101 		break;
102 		default: // WIFI_INFO_NONE
103 		break;
104 	}
105 
106 	if (myData.iQuality != myData.iPreviousQuality || myConfig.iDisplayType == CD_WIFI_GRAPH)
107 	{
108 		myData.iPreviousQuality = myData.iQuality;
109 		//cd_debug ("Wifi - Value have changed, redraw. (Use Gauge: %d)", myConfig.bUseGauge);
110 		double fValue = (double) myData.iPercent / 100.;
111 		CD_APPLET_RENDER_NEW_DATA_ON_MY_ICON (&fValue);
112 		bNeedRedraw = FALSE;
113 	}
114 
115 
116 	if (myData.cESSID != NULL && myConfig.defaultTitle == NULL && cairo_dock_strings_differ (myData.cESSID, myIcon->cName))
117 	{
118 		CD_APPLET_SET_NAME_FOR_MY_ICON (myData.cESSID);
119 	}
120 
121 	if (bNeedRedraw)
122 		CD_APPLET_REDRAW_MY_ICON;
123 }
124 
125 
cd_wifi_bubble(void)126 void cd_wifi_bubble (void)
127 {
128 	if (gldi_task_is_running (myData.pTask))
129 	{
130 		gldi_dialog_show_temporary  (D_("Checking connection...\nPlease retry in a few seconds"), myIcon, myContainer, 3000);
131 		return ;
132 	}
133 	GString *sInfo = g_string_new ("");
134 	const gchar *cIconPath;
135 	if (! myData.bWirelessExt)
136 	{
137 		cIconPath = MY_APPLET_SHARE_DATA_DIR"/no-signal.svg";
138 		g_string_assign (sInfo, D_("WiFi disabled."));
139 	}
140 	else
141 	{
142 		cIconPath = MY_APPLET_SHARE_DATA_DIR"/default.svg";
143 		g_string_assign (sInfo, D_("Wifi enabled."));
144 		g_string_printf (sInfo, "%s : %s\n%s : %s\n%s : %s\n%s : %d/%d",
145 			D_ ("Network ID"), myData.cESSID ? myData.cESSID : D_("unknown"),
146 			D_ ("Access point"), myData.cAccessPoint,
147 			D_ ("Interface"), myData.cInterface,
148 			D_ ("Signal Quality"), myData.iQuality, WIFI_NB_QUALITY-1);
149 	}
150 
151 	//cd_debug ("%s (%s)", sInfo->str, cIconPath);
152 	gldi_dialog_show_temporary_with_icon (sInfo->str, myIcon, myContainer, 6000, cIconPath);
153 	g_string_free (sInfo, TRUE);
154 }
155