1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
2  *
3  * Copyright (C) 2005-2007 Richard Hughes <richard@hughsie.com>
4  * Copyright (C) 2012-2021 MATE Developers
5  *
6  * Licensed under the GNU General Public License Version 2
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22 
23 #include "config.h"
24 
25 #include <glib.h>
26 #include <string.h>
27 #include <glib/gi18n.h>
28 #include <gdk/gdk.h>
29 #include <gtk/gtk.h>
30 
31 #include "gpm-common.h"
32 
33 /**
34  * gpm_get_timestring:
35  * @time_secs: The time value to convert in seconds
36  * @cookie: The cookie we are looking for
37  *
38  * Returns a localised timestring
39  *
40  * Return value: The time string, e.g. "2 hours 3 minutes"
41  **/
42 gchar *
gpm_get_timestring(guint time_secs)43 gpm_get_timestring (guint time_secs)
44 {
45 	char* timestring = NULL;
46 	gint  hours;
47 	gint  minutes;
48 
49 	/* Add 0.5 to do rounding */
50 	minutes = (int) ( ( time_secs / 60.0 ) + 0.5 );
51 
52 	if (minutes == 0) {
53 		timestring = g_strdup (_("Unknown time"));
54 		return timestring;
55 	}
56 
57 	if (minutes < 60) {
58 		timestring = g_strdup_printf (ngettext ("%i minute",
59 							"%i minutes",
60 							minutes), minutes);
61 		return timestring;
62 	}
63 
64 	hours = minutes / 60;
65 	minutes = minutes % 60;
66 
67 	if (minutes == 0)
68 		timestring = g_strdup_printf (ngettext (
69 				"%i hour",
70 				"%i hours",
71 				hours), hours);
72 	else
73 		/* TRANSLATOR: "%i %s %i %s" are "%i hours %i minutes"
74 		 * Swap order with "%2$s %2$i %1$s %1$i if needed */
75 		timestring = g_strdup_printf (_("%i %s %i %s"),
76 				hours, ngettext ("hour", "hours", hours),
77 				minutes, ngettext ("minute", "minutes", minutes));
78 	return timestring;
79 }
80 
81 /**
82  * gpm_discrete_from_percent:
83  * @percentage: The percentage to convert
84  * @levels: The number of discrete levels
85  *
86  * We have to be carefull when converting from %->discrete as precision is very
87  * important if we want the highest value.
88  *
89  * Return value: The discrete value for this percentage.
90  **/
91 guint
gpm_discrete_from_percent(guint percentage,guint levels)92 gpm_discrete_from_percent (guint percentage, guint levels)
93 {
94     /* for levels < 10 min value is 0 */
95     gint factor;
96     factor = levels < 10 ? 0 : 1;
97     /* check we are in range */
98     if (percentage > 100)
99         return levels;
100     if (levels == 0) {
101         g_warning ("levels is 0!");
102         return 0;
103     }
104     return (guint) ((((gfloat) percentage * (gfloat) (levels - factor)) / 100.0f) + 0.5f);
105 }
106 
107 /**
108  * gpm_discrete_to_percent:
109  * @hw: The discrete level
110  * @levels: The number of discrete levels
111  *
112  * We have to be carefull when converting from discrete->%.
113  *
114  * Return value: The percentage for this discrete value.
115  **/
116 guint
gpm_discrete_to_percent(guint discrete,guint levels)117 gpm_discrete_to_percent (guint discrete, guint levels)
118 {
119     /* for levels < 10 min value is 0 */
120     gint factor;
121     factor = levels < 10 ? 0 : 1;
122     /* check we are in range */
123     if (discrete > levels)
124         return 100;
125     if (levels == 0) {
126         g_warning ("levels is 0!");
127         return 0;
128     }
129     return (guint) (((gfloat) discrete * (100.0f / (gfloat) (levels - factor))) + 0.5f);
130 }
131 
132 
133 /**
134  * gpm_help_display:
135  * @link_id: Subsection of mate-power-manager help section
136  **/
137 void
gpm_help_display(const gchar * link_id)138 gpm_help_display (const gchar *link_id)
139 {
140 	GError *error = NULL;
141 	gchar *uri;
142 
143 	if (link_id != NULL)
144 		uri = g_strconcat ("help:mate-power-manager/", link_id, NULL);
145 	else
146 		uri = g_strdup ("help:mate-power-manager");
147 
148 	gtk_show_uri_on_window (NULL, uri, GDK_CURRENT_TIME, &error);
149 
150 	if (error != NULL) {
151 		GtkWidget *d;
152 		d = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
153 					    GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "%s", error->message);
154 		gtk_dialog_run (GTK_DIALOG(d));
155 		gtk_widget_destroy (d);
156 		g_error_free (error);
157 	}
158 	g_free (uri);
159 }
160 
161 gboolean
gpm_notebook_scroll_event_cb(GtkWidget * widget,GdkEventScroll * event)162 gpm_notebook_scroll_event_cb (GtkWidget       *widget,
163                               GdkEventScroll  *event)
164 {
165         GtkNotebook *notebook = GTK_NOTEBOOK (widget);
166         GtkWidget *child, *event_widget, *action_widget;
167 
168         child = gtk_notebook_get_nth_page (notebook, gtk_notebook_get_current_page (notebook));
169         if (child == NULL)
170                 return FALSE;
171 
172         event_widget = gtk_get_event_widget ((GdkEvent *) event);
173 
174         /* Ignore scroll events from the content of the page */
175         if (event_widget == NULL || event_widget == child || gtk_widget_is_ancestor (event_widget, child))
176                 return FALSE;
177 
178         /* And also from the action widgets */
179         action_widget = gtk_notebook_get_action_widget (notebook, GTK_PACK_START);
180         if (event_widget == action_widget || (action_widget != NULL && gtk_widget_is_ancestor (event_widget, action_widget)))
181                 return FALSE;
182 
183         action_widget = gtk_notebook_get_action_widget (notebook, GTK_PACK_END);
184         if (event_widget == action_widget || (action_widget != NULL && gtk_widget_is_ancestor (event_widget, action_widget)))
185                 return FALSE;
186 
187         switch (event->direction) {
188                 case GDK_SCROLL_RIGHT:
189                 case GDK_SCROLL_DOWN:
190                         gtk_notebook_next_page (notebook);
191                         break;
192                 case GDK_SCROLL_LEFT:
193                 case GDK_SCROLL_UP:
194                         gtk_notebook_prev_page (notebook);
195                         break;
196                 case GDK_SCROLL_SMOOTH:
197                         switch (gtk_notebook_get_tab_pos (notebook)) {
198                                 case GTK_POS_LEFT:
199                                 case GTK_POS_RIGHT:
200                                         if (event->delta_y > 0)
201                                                 gtk_notebook_next_page (notebook);
202                                         else if (event->delta_y < 0)
203                                                 gtk_notebook_prev_page (notebook);
204                                         break;
205                                 case GTK_POS_TOP:
206                                 case GTK_POS_BOTTOM:
207                                         if (event->delta_x > 0)
208                                                 gtk_notebook_next_page (notebook);
209                                         else if (event->delta_x < 0)
210                                                 gtk_notebook_prev_page (notebook);
211                                         break;
212                         }
213                         break;
214         }
215 
216         return TRUE;
217 }
218 
219 /***************************************************************************
220  ***                          MAKE CHECK TESTS                           ***
221  ***************************************************************************/
222 #ifdef EGG_TEST
223 #include "egg-test.h"
224 
225 void
gpm_common_test(gpointer data)226 gpm_common_test (gpointer data)
227 {
228 	EggTest *test = (EggTest *) data;
229 	if (egg_test_start (test, "GpmCommon") == FALSE)
230 		return;
231 
232 	egg_test_end (test);
233 }
234 
235 #endif
236 
237