#!/usr/bin/python

# Copyright (C) 2008 Valmantas Paliksa <walmis at balticum-tv dot lt>
# Copyright (C) 2008 Tadas Dailyda <tadas at dailyda dot com>
#
# Licensed under the GNU General Public License Version 3
#
# 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 3 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, see <http://www.gnu.org/licenses/>.
# 

import gobject

import dbus
import dbus.glib
import os
import datetime
import time

from gtraffic.Gconf import Gconf


class main():

	def __init__(self):
		self.storage = Gconf()
		
		self.dl = self.storage.get("download") or 0
		self.ul = self.storage.get("upload") or 0
		
		if self.dl == 0 and self.ul == 0:
			self.storage.set("start_time", int(time.time()))
		
		self.last_dl = -1
		self.last_ul = -1
	
	
		bus = dbus.SystemBus()
		bus.add_signal_receiver(self.ppp_stats, "PppStats", "org.freedesktop.NetworkManager.Device.Serial", "org.freedesktop.NetworkManager")
		
		self.storage.connect("property-changed", self.value_changed)


		gobject.MainLoop().run()
		
	def ppp_stats(self, down, up):
		#print down, up
		if self.last_dl != -1:
			delta = down - self.last_dl
			if delta > 0:
				self.dl += delta
		
		if self.last_ul != -1:
			delta = up - self.last_ul
			if delta > 0:
				self.ul += delta

		print self.dl, self.ul
		self.storage.set("download", self.dl)
		self.storage.set("upload", self.ul)
		
		self.last_dl = down
		self.last_ul = up
		
	def value_changed(self, storage, key, value):
		if key == "download":
			self.dl = value
			
		elif key == "upload":
			self.ul = value
		
		
		
		
		
		
		
		
main()
