#!/usr/bin/env python
#

import os
import sys
import getopt
import signal
import string
from gi.repository import Gtk, GObject

global Path
global Mac
global Host

class Links(object):
	def __init__(self):
		global Mac
		global Host

		builder = Gtk.Builder()
		builder.add_from_file("links_gui.glade")
		self.window = builder.get_object("mainWindow")

		if Host == None:
			print "[ links_gui][00000000] Host:", Mac
			title = Mac + " - Web history"
		else:
			print "[ links_gui][00000000] Host:", Host
			title = Host + " - Web history"

		self.window.set_title(title)
		self.window.connect("delete-event", Gtk.main_quit)
		self.scroll = builder.get_object("mainScrolledWindow")
		self.liststore = builder.get_object("liststore1")
		self.treeview = builder.get_object("mainTree")

               	self.init_entries()

              	renderer_text0 = Gtk.CellRendererText() 
               	column_text0 = Gtk.TreeViewColumn("Timestamp", renderer_text0, text = 0)
               	self.treeview.append_column(column_text0)

		renderer_text1 = Gtk.CellRendererText()
                column_text1 = Gtk.TreeViewColumn("Resource", renderer_text1, text = 1)
                self.treeview.append_column(column_text1)

		self.timeout_id = GObject.timeout_add(1000, self.on_timeout, None)
                
               	self.treeview.show()
		self.scroll.show()
		self.window.show()
		
	def init_entries(self):
                global Path

                f = open(Path, 'a')
                f.close()

                f = open(Path, 'r')

                for line in f:
			index = string.find(line, "!")
			timestamp = line[0:index]

			line = line[index + 1:]
                        index = string.find(line, "\n")
                        link = line[0:index]

                        self.liststore.prepend([timestamp, link])

                f.close()

        def update_entries(self):
                global Path

                f = open(Path, 'r')
		self.liststore.clear()

                for line in f:
			index = string.find(line, "!")
                        timestamp = line[0:index]

                        line = line[index + 1:]
                        index = string.find(line, "\n")
                        link = line[0:index]

                        self.liststore.prepend([timestamp, link])

                f.close()

        def on_timeout(self, user_data):
                self.update_entries()

                return True

def usage():
        print " "
        print "  Web link analyzer (GUI) usage:"
        print " "
        print "  -m <MAC>      : The MAC you want to see"
	print "  -H <Hostname> : The Hostname you want to see"
        print " "
        sys.exit()

        return

def signal_handler(signum, frame):
        print "[ links_gui][00000000] Signal caught."
        sys.exit()

        return

def main():
        global Path
	global Mac
	global Host

        try:
                opts, args = getopt.getopt(sys.argv[1:],"m:H:")
        except:
                usage()
                sys.exit(1)

        Mac = None
	Host = None

        for o, a in opts:
                if o == '-m':
                        Mac = a
		elif o == '-H':
			Host = a

        if args or not Mac:
                usage()
                sys.exit(1)

	Mac = Mac.upper()
	Path = '/opt/td-config/run/links/' + Mac

        signal.signal(signal.SIGINT, signal_handler)
        signal.signal(signal.SIGTERM, signal_handler)

	app = Links()
	Gtk.main()

	return

if __name__ == "__main__":
	main()
