# -*- coding: UTF-8 -*-
#
# generated by wxGlade 0.6.8 on Fri Feb 14 17:06:48 2014
#

import wx
# begin wxGlade: dependencies
import wx.grid
# end wxGlade

# begin wxGlade: extracode

# end wxGlade

import Frm_NewConn
import time
import sys
sys.path.append('..')
import lib.db as db_c
import lib.gestionUsuarios as ad
import lib.db as db_c

class Frm_Main(wx.Frame):
    
    _Frm_NewConn__server    = ''
    _Frm_NewConn__CN        = ''
    _Frm_NewConn__passwd    = ''
    _ListaUsuarios 	        = {}
    _gdb                    = ''
    _connected	    	    = 0
    _ordered		    = 0
    
    
    def _Frm_NewConn__connectToServer ( self, *args, **kwds ):
        cad_conn =  'try connect %s  in %s ' % ( self._Frm_NewConn__CN, self._Frm_NewConn__server )              
        initFrm_statusbar_fields = [_( cad_conn )]
        self.initFrm_statusbar.SetStatusText(initFrm_statusbar_fields[0], 0)
            
	ListaUsrs 	= ad.gestionUsuarios ( 'administrador@tsc.uc3m.es', self._Frm_NewConn__passwd , 'cn=Users,dc=tsc, dc=uc3m,dc=es', self._Frm_NewConn__server )

	self._ListaUsuarios =  ListaUsrs.getAllUsers ()
	if len ( self._ListaUsuarios) > 0:
	        cad_conn =  'connect to %s' % ( self._Frm_NewConn__server )
	        initFrm_statusbar_fields = [_( cad_conn )]
	        self.initFrm_statusbar.SetStatusText(initFrm_statusbar_fields[0], 0)

		#Save all the users in database table Cache
		for k, v in self._ListaUsuarios.items():
			user_array = {}
			for att,value in v.items():
				user_array[att] = value
			self._gdb.saveUsersCacheTable(k, str (user_array['fechaUltLogin']), str ( v['fechaCaducidad']), v['cn'], v['diasUltimoLogin'], v['diasParaCaducar'])

		#Get all the users from the database and print them
		usersAD = self._gdb.recoverUsersCacheTable()
		self.__printActiveDirectoryUsers (usersAD)

		self._connected = 1
		return 1

	else:
	        cad_conn =  'fail to %s' % ( self._Frm_NewConn__server )
	        initFrm_statusbar_fields = [_( cad_conn )]
	        self.initFrm_statusbar.SetStatusText(initFrm_statusbar_fields[0], 0)

	        return 0

    def __printActiveDirectoryUsers (self, users):

	#Clear the grid
	rows = self.Grd_ListaUsrs.GetNumberRows()
	self.Grd_ListaUsrs.ClearGrid()
	self.Grd_ListaUsrs.DeleteRows(0,rows)

	#Repaint the users information
	cont_user = 0
	for user in users:
		self.Grd_ListaUsrs.InsertRows (cont_user, 1)
		cont_val = 0
		for value in user:
			if value == -150902 or value == 150900:
				value = "NEVER"
			self.Grd_ListaUsrs.SetCellValue(cont_user,cont_val, str(value))
			cont_val = cont_val + 1
		cont_user = cont_user + 1
	self.Grd_ListaUsrs.AutoSize()
	self.Grd_ListaUsrs.SetScrollLineX(500)
	self.Grd_ListaUsrs.SetScrollLineY(500)
        
    
    def __init__(self, *args, **kwds):

	self._gdb= ''

        # begin wxGlade: Frm_Main.__init__
        kwds["style"] = wx.CAPTION|wx.CLOSE_BOX|wx.MAXIMIZE|wx.SYSTEM_MENU|wx.RESIZE_BORDER|wx.FULL_REPAINT_ON_RESIZE
        wx.Frame.__init__(self, *args, **kwds)
        
        # Menu Bar
        self.Frm_Main_menubar = wx.MenuBar()
        self.File = wx.Menu()
        self.opc_newcon = wx.MenuItem(self.File, wx.NewId(), _("&New connection...\tCtrl+N"), "", wx.ITEM_NORMAL)
        self.File.AppendItem(self.opc_newcon)
        self.File.AppendSeparator()
        self.opc_quit = wx.MenuItem(self.File, wx.NewId(), _("&Quit\tCtrl+Q"), "", wx.ITEM_NORMAL)
        self.File.AppendItem(self.opc_quit)
        self.Frm_Main_menubar.Append(self.File, _("File"))
        self.SetMenuBar(self.Frm_Main_menubar)
        # Menu Bar end
        self.initFrm_statusbar = self.CreateStatusBar(1, 0)
        
        # Tool Bar
        self.Frm_Main_toolbar = wx.ToolBar(self, -1, style=wx.TB_HORIZONTAL|wx.TB_TEXT|wx.TB_HORZ_LAYOUT|wx.TB_HORZ_TEXT)
        self.SetToolBar(self.Frm_Main_toolbar)
        # Tool Bar end
        self.Grd_ListaUsrs = wx.grid.Grid(self, -1, size=(1, 1))

        self.__set_properties()
        self.__do_layout()

        self.Bind(wx.EVT_MENU, self.opc_newcon_click, self.opc_newcon)
        self.Bind(wx.EVT_MENU, self.opc_quit_click, self.opc_quit)
        self.Bind(wx.grid.EVT_GRID_CMD_LABEL_LEFT_CLICK, self.label_left_click, self.Grd_ListaUsrs)
        # end wxGlade

        self.Bind(wx.EVT_CLOSE, self.opc_quit_click)

        self._gdb = db_c.db ('./')
        self._gdb.createConnectionsTable()
	self._gdb.createCacheTable()

	if self._connected == 0:
		nc = Frm_NewConn.Frm_NewConn(parent=self)
        	nc.Show(True)
        	nc.MakeModal(True)

        
        

    def __set_properties(self):
        # begin wxGlade: Frm_Main.__set_properties
        self.SetTitle(_("Active Directory Management"))
        self.initFrm_statusbar.SetStatusWidths([-1])
        # statusbar fields
        initFrm_statusbar_fields = [_("initFrm_statusbar")]
        for i in range(len(initFrm_statusbar_fields)):
            self.initFrm_statusbar.SetStatusText(initFrm_statusbar_fields[i], i)
        self.Frm_Main_toolbar.SetToolSeparation(5)
        self.Frm_Main_toolbar.Realize()
        self.Grd_ListaUsrs.CreateGrid(1, 6)
        self.Grd_ListaUsrs.EnableEditing(0)
        self.Grd_ListaUsrs.SetColLabelValue(0, _("Login"))
        self.Grd_ListaUsrs.SetColLabelValue(1, _("Last Access"))
        self.Grd_ListaUsrs.SetColLabelValue(2, _("Expiry Date"))
        self.Grd_ListaUsrs.SetColLabelValue(3, _("CN"))
        self.Grd_ListaUsrs.SetColLabelValue(4, _("Days from Last Access"))
        self.Grd_ListaUsrs.SetColLabelValue(5, _("Days to Expire"))
        self.Grd_ListaUsrs.SetMinSize((600,400))
        self.Grd_ListaUsrs.SetBackgroundColour(wx.Colour(159, 159, 95))
        self.Grd_ListaUsrs.SetForegroundColour(wx.Colour(49, 55, 57))
        # end wxGlade

        initFrm_statusbar_fields = [_('ready...')]
        for i in range(len(initFrm_statusbar_fields)):
            self.initFrm_statusbar.SetStatusText(initFrm_statusbar_fields[i], i)

    def __do_layout(self):
        # begin wxGlade: Frm_Main.__do_layout
        sizer_1 = wx.BoxSizer(wx.VERTICAL)
        sizer_1.Add(self.Grd_ListaUsrs, 1, wx.EXPAND|wx.ALIGN_CENTER_HORIZONTAL, 0)
        self.SetSizer(sizer_1)
        sizer_1.Fit(self)
        self.Layout()
        self.Centre()
        # end wxGlade

    def _Frm_NewConn__do_layout ( self ):
	self.__do_layout ()

    def btn_newConn_Click(self, event):  # wxGlade: Frm_Main.<event_handler>
        nc = Frm_NewConn.Frm_NewConn(parent=self)
        nc.Show(True)
        nc.MakeModal(True)

    def btn_newConn_Click2(self, event):  # wxGlade: Frm_Main.<event_handler>
        nc = Frm_Conn2.Frm_Conn2(parent=self)
        nc.Show(True)
        nc.MakeModal(True)

    def opc_newcon_click(self, event):  # wxGlade: Frm_Main.<event_handler>
        nc = Frm_NewConn.Frm_NewConn(parent=self)
        nc.Show(True)
        nc.MakeModal(True)

    def opc_quit_click(self, event):  # wxGlade: Frm_Main.<event_handler>
        dlg = wx.MessageDialog(self, message='Do you want to quit?', caption='confirm',style=wx.YES_NO)
        result = dlg.ShowModal() 
        dlg.Destroy() 
        if result == wx.ID_YES:
            self.Destroy ()

    def OnOpenFileDialogButton(self, event):  # wxGlade: Frm_Main.<event_handler>
        filename = "" # Use filename as a flag
        dlg = wx.FileDialog(self, message="Choose a file")
        if dlg.ShowModal() == wx.ID_OK:
            # get the new filename from the dialog
            filename = dlg.GetPath()
            dlg.Destroy() # best to do this sooner than later
        if filename:
            print filename
        # use the file name

    def label_left_click(self, event): # wxGlade: Frm_Main.<event_handler>
	label = event.GetCol()
	criteria	= ""
	order 		= ""
	if self._ordered == 0:
		order = "ASC"
		self._ordered = 1
	else:
		order = "DESC"
		self._ordered = 0
	
	if label == 0:
		criteria = "login"
	elif label == 1:
		criteria = "lastAccess"
	elif label == 2:
		criteria = "expiryDate"
	elif label == 3:
		criteria = "cn"
	elif label == 4:
		criteria = "daysFromLastAccess"
	elif label == 5:
		criteria = "daysToExpire"

        usersADSorted = self._gdb.recoverUsersCacheTableSorted (criteria, order)
	self.__printActiveDirectoryUsers (usersADSorted)
	self.__do_layout()

# end of class Frm_Main

2b9fda5a700215ecfc09e0286928be843b3f18b8
# -*- coding: UTF-8 -*-
#
# generated by wxGlade 0.6.8 on Fri Feb 14 17:06:48 2014
#

import wx
# begin wxGlade: dependencies
import wx.grid
# end wxGlade

# begin wxGlade: extracode

# end wxGlade

import Frm_NewConn
import time
import sys
sys.path.append('..')
import lib.db as db_c
import lib.gestionUsuarios as ad
import lib.db as db_c

class Frm_Main(wx.Frame):
    
    _Frm_NewConn__server    = ''
    _Frm_NewConn__CN        = ''
    _Frm_NewConn__passwd    = ''
    _ListaUsuarios 	        = {}
    _gdb                    = ''
    _connected	    	    = 0
    _ordered		    = 0
    
    
    def _Frm_NewConn__connectToServer ( self, *args, **kwds ):
        cad_conn =  'try connect %s  in %s ' % ( self._Frm_NewConn__CN, self._Frm_NewConn__server )              
        initFrm_statusbar_fields = [_( cad_conn )]
        self.initFrm_statusbar.SetStatusText(initFrm_statusbar_fields[0], 0)
            
	ListaUsrs 	= ad.gestionUsuarios ( 'administrador@tsc.uc3m.es', self._Frm_NewConn__passwd , 'cn=Users,dc=tsc, dc=uc3m,dc=es', self._Frm_NewConn__server )

	self._ListaUsuarios =  ListaUsrs.getAllUsers ()
	if len ( self._ListaUsuarios) > 0:
	        cad_conn =  'connect to %s' % ( self._Frm_NewConn__server )
	        initFrm_statusbar_fields = [_( cad_conn )]
	        self.initFrm_statusbar.SetStatusText(initFrm_statusbar_fields[0], 0)

		#Save all the users in database table Cache
		for k, v in self._ListaUsuarios.items():
			user_array = {}
			for att,value in v.items():
				user_array[att] = value
			self._gdb.saveUsersCacheTable(k, str (user_array['fechaUltLogin']), str ( v['fechaCaducidad']), v['cn'], v['diasUltimoLogin'], v['diasParaCaducar'])

		#Get all the users from the database and print them
		usersAD = self._gdb.recoverUsersCacheTable()
		self.__printActiveDirectoryUsers (usersAD)

		self._connected = 1
		return 1

	else:
	        cad_conn =  'fail to %s' % ( self._Frm_NewConn__server )
	        initFrm_statusbar_fields = [_( cad_conn )]
	        self.initFrm_statusbar.SetStatusText(initFrm_statusbar_fields[0], 0)

	        return 0

    def __printActiveDirectoryUsers (self, users):

	#Clear the grid
	rows = self.Grd_ListaUsrs.GetNumberRows()
	self.Grd_ListaUsrs.ClearGrid()
	self.Grd_ListaUsrs.DeleteRows(0,rows)

	#Repaint the users information
	cont_user = 0
	for user in users:
		self.Grd_ListaUsrs.InsertRows (cont_user, 1)
		cont_val = 0
		for value in user:
			if value == -150902 or value == 150900:
				value = "NEVER"
			self.Grd_ListaUsrs.SetCellValue(cont_user,cont_val, str(value))
			cont_val = cont_val + 1
		cont_user = cont_user + 1
	self.Grd_ListaUsrs.AutoSize()
	self.Grd_ListaUsrs.SetScrollLineX(500)
	self.Grd_ListaUsrs.SetScrollLineY(500)
        
    
    def __init__(self, *args, **kwds):

	self._gdb= ''

        # begin wxGlade: Frm_Main.__init__
        kwds["style"] = wx.CAPTION|wx.CLOSE_BOX|wx.MAXIMIZE|wx.SYSTEM_MENU|wx.RESIZE_BORDER|wx.FULL_REPAINT_ON_RESIZE
        wx.Frame.__init__(self, *args, **kwds)
        
        # Menu Bar
        self.Frm_Main_menubar = wx.MenuBar()
        self.File = wx.Menu()
        self.opc_newcon = wx.MenuItem(self.File, wx.NewId(), _("&New connection...\tCtrl+N"), "", wx.ITEM_NORMAL)
        self.File.AppendItem(self.opc_newcon)
        self.File.AppendSeparator()
        self.opc_quit = wx.MenuItem(self.File, wx.NewId(), _("&Quit\tCtrl+Q"), "", wx.ITEM_NORMAL)
        self.File.AppendItem(self.opc_quit)
        self.Frm_Main_menubar.Append(self.File, _("File"))
        self.SetMenuBar(self.Frm_Main_menubar)
        # Menu Bar end
        self.initFrm_statusbar = self.CreateStatusBar(1, 0)
        
        # Tool Bar
        self.Frm_Main_toolbar = wx.ToolBar(self, -1, style=wx.TB_HORIZONTAL|wx.TB_TEXT|wx.TB_HORZ_LAYOUT|wx.TB_HORZ_TEXT)
        self.SetToolBar(self.Frm_Main_toolbar)
        # Tool Bar end
        self.Grd_ListaUsrs = wx.grid.Grid(self, -1, size=(1, 1))

        self.__set_properties()
        self.__do_layout()

        self.Bind(wx.EVT_MENU, self.opc_newcon_click, self.opc_newcon)
        self.Bind(wx.EVT_MENU, self.opc_quit_click, self.opc_quit)
        self.Bind(wx.grid.EVT_GRID_CMD_LABEL_LEFT_CLICK, self.label_left_click, self.Grd_ListaUsrs)
        # end wxGlade

        self.Bind(wx.EVT_CLOSE, self.opc_quit_click)

        self._gdb = db_c.db ('./')
        self._gdb.createConnectionsTable()
	self._gdb.createCacheTable()

	if self._connected == 0:
		nc = Frm_NewConn.Frm_NewConn(parent=self)
        	nc.Show(True)
        	nc.MakeModal(True)

        
        

    def __set_properties(self):
        # begin wxGlade: Frm_Main.__set_properties
        self.SetTitle(_("Active Directory Management"))
        self.initFrm_statusbar.SetStatusWidths([-1])
        # statusbar fields
        initFrm_statusbar_fields = [_("initFrm_statusbar")]
        for i in range(len(initFrm_statusbar_fields)):
            self.initFrm_statusbar.SetStatusText(initFrm_statusbar_fields[i], i)
        self.Frm_Main_toolbar.SetToolSeparation(5)
        self.Frm_Main_toolbar.Realize()
        self.Grd_ListaUsrs.CreateGrid(1, 6)
        self.Grd_ListaUsrs.EnableEditing(0)
        self.Grd_ListaUsrs.SetColLabelValue(0, _("Login"))
        self.Grd_ListaUsrs.SetColLabelValue(1, _("Last Access"))
        self.Grd_ListaUsrs.SetColLabelValue(2, _("Expiry Date"))
        self.Grd_ListaUsrs.SetColLabelValue(3, _("CN"))
        self.Grd_ListaUsrs.SetColLabelValue(4, _("Days from Last Access"))
        self.Grd_ListaUsrs.SetColLabelValue(5, _("Days to Expire"))
        self.Grd_ListaUsrs.SetMinSize((600,400))
        self.Grd_ListaUsrs.SetBackgroundColour(wx.Colour(159, 159, 95))
        self.Grd_ListaUsrs.SetForegroundColour(wx.Colour(49, 55, 57))
        # end wxGlade

        initFrm_statusbar_fields = [_('ready...')]
        for i in range(len(initFrm_statusbar_fields)):
            self.initFrm_statusbar.SetStatusText(initFrm_statusbar_fields[i], i)

    def __do_layout(self):
        # begin wxGlade: Frm_Main.__do_layout
        sizer_1 = wx.BoxSizer(wx.VERTICAL)
        sizer_1.Add(self.Grd_ListaUsrs, 1, wx.EXPAND|wx.ALIGN_CENTER_HORIZONTAL, 0)
        self.SetSizer(sizer_1)
        sizer_1.Fit(self)
        self.Layout()
        self.Centre()
        # end wxGlade

    def _Frm_NewConn__do_layout ( self ):
	self.__do_layout ()

    def btn_newConn_Click(self, event):  # wxGlade: Frm_Main.<event_handler>
        nc = Frm_NewConn.Frm_NewConn(parent=self)
        nc.Show(True)
        nc.MakeModal(True)

    def btn_newConn_Click2(self, event):  # wxGlade: Frm_Main.<event_handler>
        nc = Frm_Conn2.Frm_Conn2(parent=self)
        nc.Show(True)
        nc.MakeModal(True)

    def opc_newcon_click(self, event):  # wxGlade: Frm_Main.<event_handler>
        nc = Frm_NewConn.Frm_NewConn(parent=self)
        nc.Show(True)
        nc.MakeModal(True)

    def opc_quit_click(self, event):  # wxGlade: Frm_Main.<event_handler>
        dlg = wx.MessageDialog(self, message='Do you want to quit?', caption='confirm',style=wx.YES_NO)
        result = dlg.ShowModal() 
        dlg.Destroy() 
        if result == wx.ID_YES:
            self.Destroy ()

    def OnOpenFileDialogButton(self, event):  # wxGlade: Frm_Main.<event_handler>
        filename = "" # Use filename as a flag
        dlg = wx.FileDialog(self, message="Choose a file")
        if dlg.ShowModal() == wx.ID_OK:
            # get the new filename from the dialog
            filename = dlg.GetPath()
            dlg.Destroy() # best to do this sooner than later
        if filename:
            print filename
        # use the file name

    def label_left_click(self, event): # wxGlade: Frm_Main.<event_handler>
	label = event.GetCol()
	criteria	= ""
	order 		= ""
	if self._ordered == 0:
		order = "ASC"
		self._ordered = 1
	else:
		order = "DESC"
		self._ordered = 0
	
	if label == 0:
		criteria = "login"
	elif label == 1:
		criteria = "lastAccess"
	elif label == 2:
		criteria = "expiryDate"
	elif label == 3:
		criteria = "cn"
	elif label == 4:
		criteria = "daysFromLastAccess"
	elif label == 5:
		criteria = "daysToExpire"

        usersADSorted = self._gdb.recoverUsersCacheTableSorted (criteria, order)
	self.__printActiveDirectoryUsers (usersADSorted)
	self.__do_layout()

# end of class Frm_Main
