Index: src/Makefile.am =================================================================== --- src/Makefile.am (revision 1278) +++ src/Makefile.am (working copy) @@ -49,6 +49,8 @@ applet-device-gsm.c \ applet-device-cdma.h \ applet-device-cdma.c \ + applet-database.h \ + applet-database.c \ $(NULL) nm_applet_LDADD = \ @@ -57,7 +59,8 @@ ${top_builddir}/src/marshallers/libmarshallers.la \ ${top_builddir}/src/utils/libutils.la \ ${top_builddir}/src/gconf-helpers/libgconf-helpers.la \ - ${top_builddir}/src/wireless-security/libwireless-security.la + ${top_builddir}/src/wireless-security/libwireless-security.la \ + -lsqlite3 gladedir = $(datadir)/nm-applet glade_DATA = applet.glade keyring.png Index: src/applet-database.c =================================================================== --- src/applet-database.c (revision 0) +++ src/applet-database.c (revision 0) @@ -0,0 +1,117 @@ +/* -*- 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. + */ + +#include +#include +#include +#include +#include +#include + +#include "applet-database.h" + + +guint32 applet_mb_usage_start (const char *cid, const char *cuuid) +{ + sqlite3 *db; + sqlite3_stmt *stmt; + int rc; + guint32 id = 0; + char *err_msg = 0; + time_t cdma_start_time; + const char **none = NULL; + + char *sql = NULL; + + char *db_file=NULL; + const char *home = getenv("HOME"); + + if (home == NULL) + return id; + + db_file = g_strdup_printf ("%s/.nm.db", home); + + rc = sqlite3_open(db_file,&db); + g_free(db_file); + if (rc == SQLITE_OK) { + 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) + goto done; + + cdma_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, cdma_start_time); + rc = sqlite3_exec(db, sql, 0, 0, &err_msg); + g_free(sql); + if (rc == SQLITE_OK){ + sql = g_strdup_printf("select id from mobile_broadband_usage where starttime = '%ld'", cdma_start_time); + rc = sqlite3_prepare_v2(db, sql, -1, &stmt, none); + g_free(sql); + if (rc) + goto done; + rc = sqlite3_step(stmt); + if (rc == SQLITE_ROW) { + id = (guint32 )sqlite3_column_int64(stmt, 0); + sqlite3_finalize(stmt); + } + } + } + done: + if(db) + sqlite3_close(db); + return id; +} + + +void applet_mb_usage_update (guint32 id, guint32 in, guint32 out) +{ + sqlite3 *db; + int rc; + char *err_msg = 0, *sql=NULL; + time_t current_time; + char *db_file=NULL; + const char *home = getenv("HOME"); + + if (home == NULL) + return; + db_file = g_strdup_printf ("%s/.nm.db", home); + rc = sqlite3_open(db_file,&db); + g_free(db_file); + if (rc == SQLITE_OK) { + 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 (db) + sqlite3_close(db); + +} Index: src/applet-database.h =================================================================== --- src/applet-database.h (revision 0) +++ src/applet-database.h (revision 0) @@ -0,0 +1,90 @@ +/* -*- 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. + */ + +#include +#include +#include +#include + + +extern guint32 applet_mb_usage_start(const char *cid, const char *cuuid); +extern void applet_mb_usage_update(guint32 id, guint32 in, guint32 out); +/* -*- 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. + */ + +#include +#include +#include +#include + + +extern guint32 applet_mb_usage_start(const char *cid, const char *cuuid); +extern void applet_mb_usage_update(guint32 id, guint32 in, guint32 out); +/* -*- 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. + */ + +#include +#include +#include +#include + + +extern guint32 applet_mb_usage_start(const char *cid, const char *cuuid); +extern void applet_mb_usage_update(guint32 id, guint32 in, guint32 out); Index: src/applet-device-cdma.c =================================================================== --- src/applet-device-cdma.c (revision 1278) +++ src/applet-device-cdma.c (working copy) @@ -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,10 +36,15 @@ #include #include +#include + #include "applet.h" #include "applet-device-cdma.h" #include "utils.h" +#include "applet-database.h" + + typedef struct { NMApplet *applet; NMDevice *device; @@ -290,12 +296,21 @@ connection = applet_find_active_connection_for_device (device, applet, NULL); if (connection) { - const char *id; + const char *id = NULL, *uuid = NULL; + guint32 usage_id; 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); + 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 (id); + usage_id = applet_mb_usage_start (id, uuid); + g_object_set_data (G_OBJECT (device), "usage-id", GUINT_TO_POINTER (usage_id)); + } applet_do_notify_with_pref (applet, @@ -305,6 +320,17 @@ PREF_DISABLE_CONNECTED_NOTIFICATIONS); g_free (str); } + + if ( (old_state == NM_DEVICE_STATE_ACTIVATED) + && (new_state != NM_DEVICE_STATE_ACTIVATED)) { + guint32 in_bytes = nm_serial_device_get_bytes_received (NM_SERIAL_DEVICE (device)); + guint32 out_bytes = nm_serial_device_get_bytes_sent (NM_SERIAL_DEVICE (device)); + guint32 usage_id = (guint32) g_object_get_data (G_OBJECT (device), "usage-id"); + if (usage_id) { + applet_mb_usage_update (usage_id, in_bytes, out_bytes); + g_object_set_data (G_OBJECT (device), "usage-id", NULL); + } + } } static GdkPixbuf * Index: src/applet-device-gsm.c =================================================================== --- src/applet-device-gsm.c (revision 1278) +++ src/applet-device-gsm.c (working copy) @@ -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,20 @@ connection = applet_find_active_connection_for_device (device, applet, NULL); if (connection) { - const char *id; + const char *id = NULL, *uuid = NULL; + guint32 usage_id; 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); + 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 (id); + usage_id = applet_mb_usage_start (id, uuid); + g_object_set_data (G_OBJECT (device), "usage-id", GUINT_TO_POINTER (usage_id)); } applet_do_notify_with_pref (applet, @@ -305,6 +316,18 @@ "nm-device-wwan", PREF_DISABLE_CONNECTED_NOTIFICATIONS); g_free (str); + } + + if ((old_state == NM_DEVICE_STATE_ACTIVATED) + && (new_state != NM_DEVICE_STATE_ACTIVATED)) { + guint32 in_bytes = nm_serial_device_get_bytes_received (NM_SERIAL_DEVICE (device)); + guint32 out_bytes = nm_serial_device_get_bytes_sent (NM_SERIAL_DEVICE (device)); + guint32 usage_id = (guint32) g_object_get_data (G_OBJECT (device), "usage-id"); + + if (usage_id) { + applet_mb_usage_update (usage_id, in_bytes, out_bytes); + g_object_set_data (G_OBJECT (device), "usage-id", NULL); + } } }