[nanny/win32: 10/75] Add initial Win32Chrono



commit c5f4db272a56b0307683886614b8d20587015e4f
Author: Roberto Majadas <roberto majadas openshine com>
Date:   Wed Sep 15 12:56:40 2010 +0200

    Add initial Win32Chrono

 daemon/src/Makefile.am    |    3 +-
 daemon/src/QuarterBack.py |    4 +-
 daemon/src/Win32Chrono.py |  131 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 135 insertions(+), 3 deletions(-)
---
diff --git a/daemon/src/Makefile.am b/daemon/src/Makefile.am
index 677c058..760b236 100644
--- a/daemon/src/Makefile.am
+++ b/daemon/src/Makefile.am
@@ -16,7 +16,8 @@ corelib_PYTHON =	 __init__.py 			\
 			Win32Filtering			\
 			Win32WebContentFiltering.py	\
 			Win32UsersManager.py		\
-			Win32SessionFiltering.py		
+			Win32SessionFiltering.py	\
+			Win32Chrono.py
 
 if HAS_HACHOIR_REGEX
 dgimporterdir = $(pythondir)/nanny/daemon
diff --git a/daemon/src/QuarterBack.py b/daemon/src/QuarterBack.py
index b0d19e3..ab78819 100644
--- a/daemon/src/QuarterBack.py
+++ b/daemon/src/QuarterBack.py
@@ -36,14 +36,14 @@ if os.name == "posix" :
     from LinuxUsersManager import LinuxUsersManager as UsersManager
     from LinuxSessionCKFiltering import LinuxSessionCKFiltering as SessionFilter
     from FilterManager import FilterManager as FilterManager
+    from Chrono import Chrono
 elif os.name == "nt" :
     from Win32Filtering import Win32Filtering as FirewallFilter
     from Win32WebContentFiltering import Win32WebContentFiltering as WebContentFilter
     from Win32UsersManager import Win32UsersManager as UsersManager
     from Win32SessionFiltering import Win32SessionFiltering as SessionFilter
     from FilterManager import FilterManager as FilterManager
-
-from Chrono import Chrono
+    from Win32Chrono import Win32Chrono as Chrono
 
 
 def GetInHM(m):
diff --git a/daemon/src/Win32Chrono.py b/daemon/src/Win32Chrono.py
new file mode 100644
index 0000000..df4fb26
--- /dev/null
+++ b/daemon/src/Win32Chrono.py
@@ -0,0 +1,131 @@
+#!/usr/bin/env python
+
+# Copyright (C) 2009,2010 Junta de Andalucia
+# 
+# Authors:
+#   Roberto Majadas <roberto.majadas at openshine.com>
+#   Cesar Garcia Tapia <cesar.garcia.tapia at openshine.com>
+#   Luis de Bethencourt <luibg at openshine.com>
+#   Pablo Vieytes <pvieytes at openshine.com>
+#
+# 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, 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
+
+import gobject
+import os
+
+import gtop
+import pickle
+import datetime
+
+import dbus
+
+(
+SESSION_APPID,
+WEB_APPID,
+MAIL_APPID,
+IM_APPID) = range(4)
+
+
+class Win32Chrono(gobject.GObject) :
+    '''
+    This class handles the use time of all application categories that
+    Gnome Nanny controls.
+
+    Application list is generated from files in:
+        /var/lib/nanny/applists/
+    There is one file per category and each line in the file is an
+    application name.
+
+    Max time of use per day of the categories is set in Gnome Nannys
+    Admin Console.
+    '''
+    def __init__(self, quarterback): 
+        '''Init function for NannyChrono class.'''
+        gobject.GObject.__init__(self)
+        self.quarterback = quarterback
+
+        self.day = datetime.date.today().day
+        self.categories = ['session', 'browser', 'email', 'im']
+
+        self.quarterback.connect('block-status', self.__update_cb)
+
+    def __update_cb(self, quarterback, block_status, user_id, app_id, next_change, available_time):
+        return 
+
+        '''Callback that updates the used times of the categories.'''
+        if block_status == False:
+            app_list = self.__get_application_list(self.categories)
+            proclist = gtop.proclist(gtop.PROCLIST_KERN_PROC_UID, int(user_id))
+            
+            if app_id == SESSION_APPID :
+                try:
+                    d = dbus.SystemBus()
+                    manager = dbus.Interface(d.get_object("org.freedesktop.ConsoleKit", "/org/freedesktop/ConsoleKit/Manager"), 
+                                             "org.freedesktop.ConsoleKit.Manager")
+                    sessions = manager.GetSessionsForUnixUser(int(user_id))
+                    for session_name in sessions :
+                        session = dbus.Interface(d.get_object("org.freedesktop.ConsoleKit", session_name),
+                                                 "org.freedesktop.ConsoleKit.Session")
+                        x11_display = session.GetX11Display()
+                        if x11_display == "":
+                            continue
+                        self.quarterback.subtract_time(user_id, app_id)
+                        break
+                except:
+                    print "Crash Chrono __update_cb"
+            else:
+                category = self.categories[app_id]
+                found = False
+                for proc in proclist:
+                    if len(gtop.proc_args(proc)) > 0:
+                        process = gtop.proc_args(proc)[0]
+                        if self.is_a_controlled_app(process, category, app_list):
+                            self.quarterback.subtract_time(user_id, app_id)
+                            break
+
+    def is_a_controlled_app(self, process, category, app_list):
+        return False
+        found = False
+
+        for app in app_list:
+            if app[0] == category:
+                if os.path.basename(process) == app[1]:
+                    found = True
+                    break
+
+        return found
+
+    def __get_application_list(self, categories):
+        '''Generate the application list from the app files.
+
+        Format:
+            app_list = [['browser', 'firefox'],
+                        ['email', 'thunderbird']]
+        '''
+
+        app_list = [['session', 'gnome-session']]
+        for category in categories:
+            file_path = '/etc/nanny/applists/' + category
+            if os.path.exists(file_path):
+                file = open(file_path, 'rb')
+                for line in file:
+                    if len(line) > 1:
+                        app_list.append([category, line[:-1]])
+
+        return app_list
+
+
+gobject.type_register(Win32Chrono)



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]