commit 97bd2978bb89c38397ad8516a37b29adaf1b28fe Author: Dan Williams Date: Mon May 11 20:48:04 2009 -0400 stats stuff diff --git a/configure.ac b/configure.ac index 49d9314..e78a7a8 100644 --- a/configure.ac +++ b/configure.ac @@ -161,6 +161,14 @@ else fi AC_SUBST(POLKIT_CFLAGS) + +AC_ARG_WITH(sqlite, AS_HELP_STRING([--with-sqlite], [sqlite support for mobile broadband statistics]),ac_sqlite=$withval,ac_sqlite=yes) +if test x"$ac_sqlite" = xyes; then + PKG_CHECK_MODULES(sqlite3, [sqlite3 >= 3.5]) +fi +AM_CONDITIONAL(WITH_SQLITE, test x"$with_sqlite" != xno) +AC_SUBST(SQLITE_CFLAGS) + AC_MSG_CHECKING([Linux Wireless Extensions >= 18]) AC_TRY_COMPILE([#ifndef __user #define __user diff --git a/src/Makefile.am b/src/Makefile.am index d85c5f5..7e9017f 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -20,6 +20,10 @@ nm_applet_CPPFLAGS = \ -I${top_srcdir}/src/gconf-helpers \ -I${top_srcdir}/src/wireless-security +if WITH_SQLITE +nm_applet_CPPFLAGS += -DWITH_SQLITE +endif + nm_applet_SOURCES = \ main.c \ wireless-helper.h \ @@ -44,7 +48,12 @@ nm_applet_SOURCES = \ applet-device-gsm.h \ applet-device-gsm.c \ applet-device-cdma.h \ - applet-device-cdma.c + applet-device-cdma.c \ + applet-database.h + +if WITH_SQLITE +nm_applet_SOURCES += applet-database.c +endif nm_applet_LDADD = \ $(NMA_LIBS) \ @@ -54,6 +63,10 @@ nm_applet_LDADD = \ ${top_builddir}/src/gconf-helpers/libgconf-helpers.la \ ${top_builddir}/src/wireless-security/libwireless-security.la +if WITH_SQLITE +nm_applet_LDADD += -lsqlite3 +endif + gladedir = $(datadir)/nm-applet glade_DATA = applet.glade keyring.png diff --git a/src/applet-database.c b/src/applet-database.c new file mode 100644 index 0000000..fc50ec8 --- /dev/null +++ b/src/applet-database.c @@ -0,0 +1,187 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ +/* NetworkManager Wireless Applet -- Display wireless access points and allow user control + * + * Kushal Das + * Dan Williams + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * (C) Copyright 2009 Red Hat, Inc. + */ + +#include +#include +#include +#include +#include +#include +#include + +#include "applet-database.h" + +static guint32 +usage_start (const char *cid, const char *cuuid, GError **error) +{ + sqlite3 *db = NULL; + sqlite3_stmt *stmt; + int rc; + guint32 id = 0; + time_t start_time; + char *db_file, *sql, *err_msg = NULL; + const char *home, *unused; + + home = getenv ("HOME"); + if (home == NULL) { + g_set_error (error, 0, 0, "$HOME not set"); + return 0; + } + + db_file = g_strdup_printf ("%s/.nm.db", home); + rc = sqlite3_open (db_file, &db); + if (rc != SQLITE_OK) { + g_set_error (error, 0, 0, "Could not open sqlite database '%s': %d", + db_file, rc); + g_free (db_file); + return 0; + } + g_free (db_file); + + sql = g_strdup_printf ("CREATE TABLE IF NOT EXISTS mobile_broadband_usage (" + "id INTEGER PRIMARY KEY AUTOINCREMENT, cid TEXT, cuuid TEXT," + "datain TEXT NOT NULL, dataout TEXT NOT NULL," + "starttime TEXT UNIQUE, endtime TEXT)"); + rc = sqlite3_exec (db, sql, 0, 0, &err_msg); + g_free (sql); + if (rc != SQLITE_OK) { + g_set_error (error, 0, 0, "Failed to create mobile broadband usage table: %d", rc); + goto done; + } + + start_time = time (0); + sql = g_strdup_printf ("INSERT INTO mobile_broadband_usage ('cid'," + "'cuuid','datain','dataout','starttime') VALUES('%s','%s'," + "'0','0',%ld)",cid, cuuid, start_time); + rc = sqlite3_exec (db, sql, 0, 0, &err_msg); + g_free (sql); + if (rc != SQLITE_OK) { + g_set_error (error, 0, 0, "Failed to update mobile broadband usage table: %d", rc); + goto done; + } + + sql = g_strdup_printf ("SELECT id FROM mobile_broadband_usage WHERE starttime = '%ld'", start_time); + rc = sqlite3_prepare_v2 (db, sql, -1, &stmt, &unused); + g_free (sql); + if (rc) { + g_set_error (error, 0, 0, "Failed to re-read mobile broadband usage table: %d", rc); + goto done; + } + + rc = sqlite3_step (stmt); + if (rc == SQLITE_ROW) + id = (guint32) sqlite3_column_int64 (stmt, 0); + else + g_set_error (error, 0, 0, "Failed to read mobile broadband usage entry id: %d", rc); + + sqlite3_finalize (stmt); + +done: + if (db) + sqlite3_close (db); + return id; +} + +void +applet_mb_usage_start (NMSerialDevice *device, NMConnection *connection) +{ + NMSettingConnection *s_con = NULL; + const char *id = NULL, *uuid = NULL; + guint32 usage_id; + GError *error = NULL; + + g_assert (s_con); + id = nm_setting_connection_get_id (s_con); + g_assert (id); + uuid = nm_setting_connection_get_uuid (s_con); + g_assert (uuid); + + usage_id = usage_start (id, uuid, &error); + if (usage_id) + g_object_set_data (G_OBJECT (device), "usage-id", GUINT_TO_POINTER (usage_id)); + else { + g_object_set_data (G_OBJECT (device), "usage-id", NULL); + g_warning ("Statistics error: %s", error->message); + g_clear_error (&error); + } +} + +static gboolean +usage_update (guint32 id, guint32 in, guint32 out, GError **error) +{ + sqlite3 *db = NULL; + int rc; + time_t current_time; + char *db_file, *sql, *err_msg = NULL; + const char *home = getenv("HOME"); + gboolean success = FALSE; + + if (home == NULL) { + g_set_error (error, 0, 0, "$HOME not set"); + return FALSE; + } + + db_file = g_strdup_printf ("%s/.nm.db", home); + rc = sqlite3_open (db_file, &db); + if (rc != SQLITE_OK) { + g_set_error (error, 0, 0, "Could not open sqlite database '%s': %d", + db_file, rc); + g_free (db_file); + return FALSE; + } + g_free (db_file); + + current_time = time (0); + sql = g_strdup_printf ("UPDATE mobile_broadband_usage set datain =" + " '%u', dataout = '%u', endtime = '%ld' where id = %ld", + in, out, current_time, (long) id); + rc = sqlite3_exec (db, sql, 0, 0, &err_msg); + g_free (sql); + if (rc == SQLITE_OK) + success = TRUE; + else + g_set_error (error, 0, 0, "Failed to update mobile broadband usage table: %d", rc); + + sqlite3_close (db); + return success; +} + +void +applet_mb_usage_stop (NMSerialDevice *device) +{ + guint32 in_bytes = nm_serial_device_get_bytes_received (device); + guint32 out_bytes = nm_serial_device_get_bytes_sent (device); + guint32 usage_id = (guint32) g_object_get_data (G_OBJECT (device), "usage-id"); + GError *error = NULL; + + if (!usage_id) + return; + + usage_update (usage_id, in_bytes, out_bytes, &error); + g_object_set_data (G_OBJECT (device), "usage-id", NULL); + if (error) { + g_warning ("Statistics error: %s", error->message); + g_clear_error (&error); + } +} + diff --git a/src/applet-database.h b/src/applet-database.h new file mode 100644 index 0000000..adc96fe --- /dev/null +++ b/src/applet-database.h @@ -0,0 +1,39 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ +/* NetworkManager Wireless Applet -- Display wireless access points and allow user control + * + * Kushal das + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * (C) Copyright 2009 Red Hat, Inc. + */ + +#ifndef __APPLET_DATABASE_H__ +#define __APPLET_DATABASE_H__ + +#include +#include + +#ifdef WITH_SQLITE +void applet_mb_usage_start (NMSerialDevice *device, NMConnection *connection); +void applet_mb_usage_stop (NMSerialDevice *device); +#else + +static inline void applet_mb_usage_start (NMSerialDevice *device, NMConnection *connection) {} +static inline void applet_mb_usage_stop (NMSerialDevice *device) {} + +#endif + +#endif /* __APPLET_DATABASE_H__ */ diff --git a/src/applet-device-cdma.c b/src/applet-device-cdma.c index 58742fe..8ae3a97 100644 --- a/src/applet-device-cdma.c +++ b/src/applet-device-cdma.c @@ -2,6 +2,7 @@ /* NetworkManager Wireless Applet -- Display wireless access points and allow user control * * Dan Williams + * Kushal Das * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -17,7 +18,7 @@ * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * - * (C) Copyright 2008 Red Hat, Inc. + * (C) Copyright 2008-2009 Red Hat, Inc. */ #ifdef HAVE_CONFIG_H @@ -35,9 +36,14 @@ #include #include +#include + #include "applet.h" #include "applet-device-cdma.h" #include "utils.h" +#include "applet-database.h" + + typedef struct { NMApplet *applet; @@ -296,6 +302,8 @@ cdma_device_state_changed (NMDevice *device, id = s_con ? nm_setting_connection_get_id (s_con) : NULL; if (id) str = g_strdup_printf (_("You are now connected to '%s'."), id); + + applet_mb_usage_start (NM_SERIAL_DEVICE (device), connection); } applet_do_notify_with_pref (applet, @@ -305,6 +313,9 @@ cdma_device_state_changed (NMDevice *device, PREF_DISABLE_CONNECTED_NOTIFICATIONS); g_free (str); } + + if ((old_state == NM_DEVICE_STATE_ACTIVATED) && (new_state != NM_DEVICE_STATE_ACTIVATED)) + applet_mb_usage_stop (NM_SERIAL_DEVICE (device)); } static GdkPixbuf * diff --git a/src/applet-device-gsm.c b/src/applet-device-gsm.c index a01d57c..8d6f47e 100644 --- a/src/applet-device-gsm.c +++ b/src/applet-device-gsm.c @@ -2,6 +2,7 @@ /* NetworkManager Wireless Applet -- Display wireless access points and allow user control * * Dan Williams + * Kushal Das * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -17,7 +18,7 @@ * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * - * (C) Copyright 2008 Red Hat, Inc. + * (C) Copyright 2008-2009 Red Hat, Inc. * (C) Copyright 2008 Novell, Inc. */ @@ -36,6 +37,8 @@ #include #include +#include "applet-database.h" + #include "applet.h" #include "applet-device-gsm.h" #include "utils.h" @@ -291,12 +294,14 @@ gsm_device_state_changed (NMDevice *device, connection = applet_find_active_connection_for_device (device, applet, NULL); if (connection) { - const char *id; + const char *id = NULL; s_con = NM_SETTING_CONNECTION (nm_connection_get_setting (connection, NM_TYPE_SETTING_CONNECTION)); id = s_con ? nm_setting_connection_get_id (s_con) : NULL; if (id) str = g_strdup_printf (_("You are now connected to '%s'."), id); + + applet_mb_usage_start (NM_SERIAL_DEVICE (device), connection); } applet_do_notify_with_pref (applet, @@ -306,6 +311,9 @@ gsm_device_state_changed (NMDevice *device, PREF_DISABLE_CONNECTED_NOTIFICATIONS); g_free (str); } + + if ((old_state == NM_DEVICE_STATE_ACTIVATED) && (new_state != NM_DEVICE_STATE_ACTIVATED)) + applet_mb_usage_stop (NM_SERIAL_DEVICE (device)); } static GdkPixbuf *