def set_online(self): ntmtools.dbg_msg("NTM.set_online") self.session_start = datetime.datetime.now() self.update_event_th(self.session_start, self.session_start, self.update_interval, 0, 0, 1) ris = self.get_proc_net_dev_default() if ris != None: self.last_traffic_in, self.last_traffic_out = ris else: ntmtools.dbg_msg(_("The interface") + " " + self.interface + " " +_("is not detected or inactive.")) self.last_traffic_in, self.last_traffic_out = 0, 0 self.online = True if self.logTraffic: print(_('Total\tReceive\tTransm.\tMean Speed of the last {0}"').format(self.update_interval)) print('KByte\tKByte\tKByte\tKByte/sec') if not self.versionChecked: if not self.check_version("http://luigit.altervista.org/ntm/ntm_update.php", globaldef.VERSION): self.versionChecked = True ntmtools.set_db_var(self.db_conn, "general.last_version_check", str(int(time.time()))) if not self.info_win_load: self.info_win.load() self.info_win_load = True ntmtools.dbg_msg("END - NTM.set_online")
def update_period(self, timestamp): last_day = ntmtools.get_last_day(self.first_day, self.period_length, self.custom_days) date_timestamp = timestamp.date() if (date_timestamp < self.first_day): return False if self.period_autoupdate: ch = False while date_timestamp > last_day: self.first_day = last_day + datetime.timedelta(1) last_day = ntmtools.get_last_day(self.first_day, self.period_length, self.custom_days) ch = True if ch: ntmtools.set_db_var(self.db_conn, "time_slot.first_day", self.first_day.isoformat()) ret = MTimeSlot.load_sessions( self.db_conn, ntmtools.date_to_datetime_start(self.first_day), ntmtools.date_to_datetime_end(last_day), self.slotLength) self.slotsUsed = ret[0] self.tsTime = ret[1] return True else: return (date_timestamp <= last_day)
def update_period(self, timestamp): last_day = ntmtools.get_last_day(self.first_day, self.period_length, self.custom_days) date_timestamp = timestamp.date() if (date_timestamp < self.first_day): return False if self.period_autoupdate: ch = False while date_timestamp > last_day: self.first_day = last_day + datetime.timedelta(1) last_day = ntmtools.get_last_day(self.first_day, self.period_length, self.custom_days) ch = True if ch: ntmtools.set_db_var(self.db_conn, "time.first_day", self.first_day.isoformat()) last_day = ntmtools.get_last_day(self.first_day, self.period_length, self.custom_days) self.used_sec = MTime.load_time_used( self.ntm.db_conn, ntmtools.date_to_datetime_start(self.first_day), ntmtools.date_to_datetime_end(last_day)) self.connStartTime = timestamp self.this_slot_sec = 0 return True else: return (date_timestamp <= last_day)
def update_period(self, timestamp): last_day = ntmtools.get_last_day(self.first_day, self.period_length, self.custom_days) date_timestamp = timestamp.date() if (date_timestamp < self.first_day): return False if self.period_autoupdate: ch = False while date_timestamp > last_day: self.first_day = last_day + datetime.timedelta(1) last_day = ntmtools.get_last_day(self.first_day, self.period_length, self.custom_days) ch = True if ch: ntmtools.set_db_var(self.db_conn, "traffic.first_day", self.first_day.isoformat()) ret = MTraffic.load_traffic(self.ntm.db_conn, self.first_day, last_day) self.rec_traffic = ret[0] self.tra_traffic = ret[1] return True else: return (date_timestamp <= last_day)
def check_version(self, url, version): ntmtools.dbg_msg("NTM.check_version : {0} , {1}".format(url, version)) itime = ntmtools.read_db_var(self.db_conn, "itime") try: ntmtools.str_to_date_time(itime) except: ntmtools.dbg_msg("itime: wrong format or absent. Rigenerate.") itime = str(datetime.datetime.today()) ntmtools.set_db_var(self.db_conn, "itime", itime) envInfo = urllib2.quote("{0}\t{1}".format(itime, ntmtools.get_env_info())) fullurl = url + "?cver={0}&sys={1}".format(urllib2.quote(version), envInfo) dfile = None try: dfile = urllib2.urlopen(fullurl, timeout=10) except: ntmtools.dbg_msg(_("Connection Error") + " (" + fullurl + ").") ntmtools.dbg_msg("END - NTM.check_version -> True") return True if dfile != None: str_data = dfile.read() dic_data = ntmtools.prop2dic(str_data) newVer = dic_data["lastversion"] compare = ntmtools.version_compare(version, newVer) if compare < 0: dialog = gtk.Dialog( _("NTM - New Version"), self.ntmgui.statusIconMenu.get_toplevel(), gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT, (gtk.STOCK_OK, gtk.RESPONSE_OK)) icon = dialog.render_icon(gtk.STOCK_DIALOG_WARNING, gtk.ICON_SIZE_DIALOG) dialog.set_icon(icon) linkButton = gtk.LinkButton( dic_data["suggestedurl"], _("Your version") + ": {0}. ".format(version) + _("Last version") + ": {0}\n{1}\n{2}".format(newVer, dic_data["updatemessage"], dic_data["extramessage"])) dialog.vbox.pack_start(linkButton) dialog.show_all() dialog.run() dialog.destroy() else: ntmtools.dbg_msg("END - NTM.check_version -> True") return True ntmtools.dbg_msg("END - NTM.check_version -> False") return False
def check_version(self, url, version): ntmtools.dbg_msg("NTM.check_version : {0} , {1}".format(url, version)) itime = ntmtools.read_db_var(self.db_conn, "itime") try: ntmtools.str_to_date_time(itime) except: ntmtools.dbg_msg("itime: wrong format or absent. Rigenerate.") itime = str(datetime.datetime.today()) ntmtools.set_db_var(self.db_conn, "itime", itime) envInfo = urllib2.quote("{0}\t{1}".format(itime, ntmtools.get_env_info())) fullurl = url + "?cver={0}&sys={1}".format(urllib2.quote(version), envInfo) dfile = None try: dfile = urllib2.urlopen(fullurl, timeout=10) except: ntmtools.dbg_msg(_("Connection Error") + " (" + fullurl + ").") ntmtools.dbg_msg("END - NTM.check_version -> True") return True if dfile != None: str_data = dfile.read() dic_data = ntmtools.prop2dic(str_data) newVer = dic_data["lastversion"] compare = ntmtools.version_compare(version, newVer) if compare < 0: dialog = gtk.Dialog( _("NTM - New Version"), self.ntmgui.statusIconMenu.get_toplevel(), gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT, (gtk.STOCK_OK, gtk.RESPONSE_OK) ) icon = dialog.render_icon(gtk.STOCK_DIALOG_WARNING, gtk.ICON_SIZE_DIALOG) dialog.set_icon(icon) linkButton = gtk.LinkButton( dic_data["suggestedurl"], _("Your version") + ": {0}. ".format(version) + _("Last version") + ": {0}\n{1}\n{2}". format(newVer, dic_data["updatemessage"], dic_data["extramessage"] ) ) dialog.vbox.pack_start(linkButton) dialog.show_all() dialog.run() dialog.destroy() else: ntmtools.dbg_msg("END - NTM.check_version -> True") return True ntmtools.dbg_msg("END - NTM.check_version -> False") return False
def update_period(self, timestamp): last_day = ntmtools.get_last_day(self.first_day, self.period_length, self.custom_days) date_timestamp = timestamp.date() if (date_timestamp < self.first_day): return False if self.period_autoupdate: ch = False while date_timestamp > last_day: self.first_day = last_day + datetime.timedelta(1) last_day = ntmtools.get_last_day(self.first_day, self.period_length, self.custom_days) ch = True if ch: ntmtools.set_db_var(self.db_conn, "time_slot.first_day", self.first_day.isoformat()) ret = MTimeSlot.load_sessions(self.db_conn, ntmtools.date_to_datetime_start(self.first_day), ntmtools.date_to_datetime_end(last_day), self.slotLength) self.slotsUsed = ret[0] self.tsTime = ret[1] return True else: return (date_timestamp <= last_day)
def set_online(self): ntmtools.dbg_msg("NTM.set_online") self.session_start = datetime.datetime.now() self.update_event_th(self.session_start, self.session_start, self.update_interval, 0, 0, 1) ris = self.get_proc_net_dev_default() if ris != None: self.last_traffic_in, self.last_traffic_out = ris else: ntmtools.dbg_msg( _("The interface") + " " + self.interface + " " + _("is not detected or inactive.")) self.last_traffic_in, self.last_traffic_out = 0, 0 self.online = True if self.logTraffic: print( _('Total\tReceive\tTransm.\tMean Speed of the last {0}"'). format(self.update_interval)) print('KByte\tKByte\tKByte\tKByte/sec') if not self.versionChecked: if not self.check_version( "http://luigit.altervista.org/ntm/ntm_update.php", globaldef.VERSION): self.versionChecked = True ntmtools.set_db_var(self.db_conn, "general.last_version_check", str(int(time.time()))) if not self.info_win_load: self.info_win.load() self.info_win_load = True ntmtools.dbg_msg("END - NTM.set_online")
def update_period(self, timestamp): last_day = ntmtools.get_last_day(self.first_day, self.period_length, self.custom_days) date_timestamp = timestamp.date() if (date_timestamp < self.first_day): return False if self.period_autoupdate: ch = False while date_timestamp > last_day: self.first_day = last_day + datetime.timedelta(1) last_day = ntmtools.get_last_day(self.first_day, self.period_length, self.custom_days) ch = True if ch: ntmtools.set_db_var(self.db_conn, "time.first_day", self.first_day.isoformat()) last_day = ntmtools.get_last_day(self.first_day, self.period_length, self.custom_days) self.used_sec = MTime.load_time_used(self.ntm.db_conn, ntmtools.date_to_datetime_start(self.first_day), ntmtools.date_to_datetime_end(last_day)) self.connStartTime = timestamp self.this_slot_sec = 0 return True else: return (date_timestamp <= last_day)
def set_preferences(self, active, total_sec, auto_disconnect, period_length, custom_days, first_day, period_autoupdate): self.dialog_showed = False update_time = False if (self.active != active): self.active = active ntmtools.set_db_var(self.db_conn, "time.active", ntmtools.bool_to_str_int(active)) update_time = True if (self.total_sec != total_sec): self.total_sec = total_sec ntmtools.set_db_var(self.db_conn, "time.total_sec", str(total_sec)) if (self.auto_disconnect != auto_disconnect): self.auto_disconnect = auto_disconnect ntmtools.set_db_var(self.db_conn, "time.auto_disconnect", ntmtools.bool_to_str_int(auto_disconnect)) if (self.period_length != period_length): self.period_length = period_length ntmtools.set_db_var(self.db_conn, "time.period_length", str(int(period_length))) update_time = True if (self.custom_days != custom_days): self.custom_days = custom_days ntmtools.set_db_var(self.db_conn, "time.custom_days", str(int(custom_days))) #if (period_length == 0): update_timeslot = True update_time = True if (self.first_day != first_day): self.first_day = first_day ntmtools.set_db_var(self.db_conn, "time.first_day", first_day.isoformat()) update_time = True if (self.period_autoupdate != period_autoupdate): self.period_autoupdate = period_autoupdate ntmtools.set_db_var(self.db_conn, "time.period_autoupdate", ntmtools.bool_to_str_int(period_autoupdate)) update_time = True if (update_time): if self.period_autoupdate: self.update_period(self.now) else: last_day = ntmtools.get_last_day(self.first_day, self.period_length, self.custom_days) self.used_sec = MTime.load_time_used( self.db_conn, ntmtools.date_to_datetime_start(self.first_day), ntmtools.date_to_datetime_end(last_day)) if self.active: self.gui_main.show() self.update_main_gui() self.check_limit() else: self.gui_main.hide()
def make_from_db(ntm): conn = ntm.db_conn res = ntmtools.read_db_var( conn, "traffic.active") # 0:Disable; Other:Enable; try: active = (int(res) != 0) except: ntmtools.dbg_msg( _("Wrong value for the param") + " 'traffic.active' " + _("or is not stored. Default value") + " '1'") ntmtools.set_db_var(conn, "traffic.active", "1") active = True res = ntmtools.read_db_var(conn, "traffic.limit") # bytes try: traffic_limit = float(res) except: ntmtools.dbg_msg( _("Wrong value for the param") + " 'traffic.limit' " + _("or is not stored. Default value") + " '98MB'") ntmtools.set_db_var(conn, "traffic.limit", str(98 * 1024 * 1024)) traffic_limit = 98 * 1024 * 1024 res = ntmtools.read_db_var( conn, "traffic.auto_disconnect") # 0:Disable; Other:Enable; try: auto_disconnect = (int(res) != 0) except: ntmtools.dbg_msg( _("Wrong value for the param") + " 'traffic.auto_disconnect' " + _("or is not stored. Default value") + " '1'") ntmtools.set_db_var(conn, "traffic.auto_disconnect", "1") auto_disconnect = True val_str = ntmtools.read_db_var(conn, "traffic.period_length") try: period_length = int(val_str) except: ntmtools.dbg_msg( _("Wrong value for the param") + " 'traffic.period_length' " + _("or is not stored. Default value") + " '" + _("Daily") + "'") period_length = 0 # 0 -> Daily ntmtools.set_db_var(conn, "traffic.period_length", str(int(period_length))) val_str = ntmtools.read_db_var(conn, "traffic.custom_days") try: custom_days = int(val_str) except: ntmtools.dbg_msg( _("Wrong value for the param") + " 'traffic.custom_days' " + _("or is not stored. Default value") + " 30") custom_days = 30 ntmtools.set_db_var(conn, "traffic.custom_days", str(int(custom_days))) val_str = ntmtools.read_db_var(conn, "traffic.first_day") try: first_day = ntmtools.str_to_date(val_str) except: ntmtools.dbg_msg( _("Wrong value for the param") + " 'traffic.first_day' " + _("or is not stored. Default value") + " '2009-10-01'") first_day = datetime.date(2009, 10, 01) ntmtools.set_db_var(conn, "traffic.first_day", first_day) val_str = ntmtools.read_db_var(conn, "traffic.period_autoupdate") try: period_autoupdate = (ntmtools.str_to_int(val_str, 1) != 0) except: ntmtools.dbg_msg( _("Wrong value for the param") + " 'traffic.period_autoupdate' " + _("or is not stored. Default value") + " '" + _("True") + "'") period_autoupdate = True ntmtools.set_db_var(conn, "traffic.period_autoupdate", "1") last_day = ntmtools.get_last_day(first_day, period_length, custom_days) ret = MTraffic.load_traffic(conn, first_day, last_day) return MTraffic(ntm, active, ret[0], ret[1], traffic_limit, auto_disconnect, period_length, custom_days, first_day, period_autoupdate)
def set_preferences(self, interface, updateInterval, keep_above, opacity, autorun, online_check, tray_activate_action, importexport_file, iconfile, iconfile_active): ntmtools.dbg_msg("NTM.set_preferences") ris = self.get_proc_net_dev(interface) if (ris == None) and self.online: dia = gtk.Dialog( _('NTM - Interface'), self.ntmgui.statusIconMenu.get_toplevel( ), #the toplevel wgt of your app gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT, #binary flags or'ed together (_("Change the interface"), 77, gtk.STOCK_CLOSE, gtk.RESPONSE_CLOSE)) dia.vbox.pack_start( gtk.Label( _('The interface "{0}" is not detected or inactive.\nDo you want to confirm the change?' ).format(interface))) dia.show_all() result = dia.run() doChangeInterface = (result == 77) dia.destroy() else: doChangeInterface = True if doChangeInterface: interface_changed = (self.interface != interface) if interface_changed & self.online: self.set_offline() self.interface = interface self.interfaceProcNetDev = interface self.set_online() else: self.interface = interface self.interfaceProcNetDev = interface ntmtools.set_db_var(self.db_conn, "general.interface", interface) if interface_changed: self.net_man.set_interface(self.interface) self.ntmMainWindow_keep_above = keep_above if self.ntmMainWindow_keep_above: ntmtools.set_db_var(self.db_conn, "general.keep_above", "1") else: ntmtools.set_db_var(self.db_conn, "general.keep_above", "0") self.ntmMainWindow_opacity = opacity ntmtools.set_db_var(self.db_conn, "general.opacity", opacity) self.timeout_changed = (self.update_interval != updateInterval) self.update_interval = int(updateInterval) ntmtools.set_db_var(self.db_conn, "general.update_interval", str(int(updateInterval))) self.general_pref_autorun = autorun if autorun: ntmtools.set_db_var(self.db_conn, "general.autorun", "1") else: ntmtools.set_db_var(self.db_conn, "general.autorun", "0") self.set_autorun(autorun) if self.general_pref_online_check != online_check: if online_check == 0: self.general_pref_online_check = 0 self.net_man.set_mode(online_check) ntmtools.set_db_var(self.db_conn, "general.online_check", "0") elif online_check == 1: self.general_pref_online_check = 1 self.net_man.set_mode(online_check) ntmtools.set_db_var(self.db_conn, "general.online_check", "1") else: ntmtools.dbg_msg("Error: Invald online_check value.\n") if self.general_pref_tray_activate_action != tray_activate_action: if tray_activate_action == 0: self.general_pref_tray_activate_action = 0 ntmtools.set_db_var(self.db_conn, "general.tray_activate_action", "0") elif tray_activate_action == 1: self.general_pref_tray_activate_action = 1 ntmtools.set_db_var(self.db_conn, "general.tray_activate_action", "1") else: ntmtools.dbg_msg("Error: Invald tray_activate_action value.\n") if self.importexport_file != importexport_file: self.importexport_file = importexport_file ntmtools.set_db_var(self.db_conn, "general.importexport_file", importexport_file) if self.trayicon_file != iconfile: self.trayicon_file = iconfile ntmtools.set_db_var(self.db_conn, "general.trayicon_file", iconfile) self.ntmgui.set_trayicon_file(iconfile) if self.trayicon_active_file != iconfile_active: self.trayicon_active_file = iconfile_active ntmtools.set_db_var(self.db_conn, "general.trayicon_active_file", iconfile_active) self.ntmgui.set_trayicon_active_file(iconfile_active) ntmtools.dbg_msg("END - NTM.set_preferences")
def __init__(self): self.stop = False self.session_start = None self.last_traffic_in, self.last_traffic_out = None, None self.online = False self.timeout_changed = False self.versionChecked = False self.logTraffic = False self.discMsgDialog = False self.last_update = None self.pinger = pinger.Pinger("google.com", self.rtt_callback) ## db update self.db_update_interval = 20 # sec self.last_db_update = datetime.datetime.now() self.d_rb_db = self.d_tb_db = 0 self.sys_info = ntmtools.getSysInfo() #print(self.sys_info) self.d_rb, self.d_tb = 0, 0 self.home_path = os.getenv("HOME") self.profile_path = self.home_path + "/" + globaldef.NTM_PROFILE_RELPATH if not os.path.exists(self.profile_path): os.makedirs(self.profile_path) db_file_path = self.profile_path + "/" + globaldef.NTM_DB_NAME self.db_conn = sqlite3.connect(db_file_path, check_same_thread=False) self.update_event = Event() ## Create tables self.create_tables(self.db_conn) res = ntmtools.read_db_var(self.db_conn, "general.interface") if res != None: self.interface = res else: ntmtools.dbg_msg( _("Wrong value for the param") + " 'general.interface' " + _("or is not stored. Default value") + " 'ttyACM0'.") ntmtools.set_db_var(self.db_conn, "general.interface", "ttyACM0") self.interface = "ttyACM0" self.interfaceProcNetDev = self.interface res = ntmtools.read_db_var(self.db_conn, "general.update_interval") # sec try: self.update_interval = int(float(res)) except: ntmtools.dbg_msg( _("Wrong value for the param") + " 'general.update_interval' " + _("or is not stored. Default value") + " '2'.") ntmtools.set_db_var(self.db_conn, "general.update_interval", "2") self.update_interval = 2 res = ntmtools.read_db_var(self.db_conn, "general.last_version_check") # datetime try: self.last_version_check = int(float(res)) self.versionChecked = ((time.time() - self.last_version_check) < (5 * 24 * 60 * 60)) except: ntmtools.dbg_msg( _("Wrong value for the param") + " 'general.last_version_check' " + _("or is not stored. Default value") + " '" + _("now") + "'.") ntmtools.set_db_var(self.db_conn, "general.last_version_check", str(int(time.time()))) self.last_version_check = int(time.time()) self.versionChecked = False res = ntmtools.read_db_var(self.db_conn, "general.keep_above") # 0 or 1 try: self.ntmMainWindow_keep_above = (int(float(res)) != 0) except: ntmtools.dbg_msg( _("Wrong value for the param") + " 'general.keep_above' " + _("or is not stored. Default value") + " '0.") ntmtools.set_db_var(self.db_conn, "general.keep_above", "0") self.ntmMainWindow_keep_above = False res = ntmtools.read_db_var(self.db_conn, "general.opacity") # 0 to 100 try: self.ntmMainWindow_opacity = int(float(res)) except: ntmtools.dbg_msg( _("Wrong value for the param") + " 'general.opacity' " + _("or is not stored. Default value") + " '100.") ntmtools.set_db_var(self.db_conn, "general.opacity", "100") self.ntmMainWindow_opacity = 100 res = ntmtools.read_db_var(self.db_conn, "general.autorun") # 0 or 1 try: self.general_pref_autorun = (int(float(res)) != 0) except: ntmtools.dbg_msg( _("Wrong value for the param") + " 'general.autorun' " + _("or is not stored. Default value") + " '" + _("True") + "'.") ntmtools.set_db_var(self.db_conn, "general.autorun", "1") self.general_pref_autorun = True self.set_autorun(self.general_pref_autorun) res = ntmtools.read_db_var( self.db_conn, "general.online_check") # 0->NetworkManager; 1->Ping try: self.general_pref_online_check = int(float(res)) except: ntmtools.dbg_msg( _("Wrong value for the param") + " 'general.online_check' " + _("or is not stored. Default value") + " 0 (NetworkManager).") self.general_pref_online_check = 0 ntmtools.set_db_var(self.db_conn, "general.online_check", "0") res = ntmtools.read_db_var(self.db_conn, "general.tray_activate_action" ) # 0->Show Main Window; 1->Show Nptify; try: self.general_pref_tray_activate_action = int(float(res)) except: ntmtools.dbg_msg( _("Wrong value for the param") + " 'general.tray_activate_action' " + _("or is not stored. Default value") + " 0 (Show Main Window).") self.general_pref_tray_activate_action = 0 ntmtools.set_db_var(self.db_conn, "general.tray_activate_action", "0") res = ntmtools.read_db_var(self.db_conn, "general.trayicon_file") if res != None: ntmtools.dbg_msg("general.trayicon_file : " + res) self.trayicon_file = res else: ntmtools.dbg_msg( _("Wrong value for the param") + " 'general.trayicon_file' " + _("or is not stored. Default value") + " " + globaldef.DEFAULT_NTM_ICON_OFF) ntmtools.set_db_var(self.db_conn, "general.trayicon_file", globaldef.DEFAULT_NTM_ICON_OFF) self.trayicon_file = globaldef.DEFAULT_NTM_ICON_OFF res = ntmtools.read_db_var(self.db_conn, "general.trayicon_active_file") if res != None: ntmtools.dbg_msg("general.trayicon_active_file : " + res) self.trayicon_active_file = res else: ntmtools.dbg_msg( _("Wrong value for the param") + " 'general.trayicon_active_file' " + _("or is not stored. Default value") + " " + globaldef.DEFAULT_NTM_ICON_ON) ntmtools.set_db_var(self.db_conn, "general.trayicon_active_file", globaldef.DEFAULT_NTM_ICON_ON) self.trayicon_active_file = globaldef.DEFAULT_NTM_ICON_ON res = ntmtools.read_db_var(self.db_conn, "general.importexport_file") if res != None: self.importexport_file = res else: ntmtools.dbg_msg( _("Wrong value for the param") + " 'general.importexport_file' " + _("or is not stored. Default value") + " ''.") ntmtools.set_db_var(self.db_conn, "general.importexport_file", "") self.importexport_file = "" # NetMan self.net_man = netman.NetMan(self.general_pref_online_check, self.interface) self.net_man.add_online_handler(self.set_online) self.net_man.add_offline_handler(self.set_offline) # GUI self.ntmgui = ntmgui.NtmGui(self) self.ntmgui.apply_prop(self.ntmMainWindow_keep_above, self.ntmMainWindow_opacity) self.set_autorun(self.general_pref_autorun) self.ntmgui.set_general_preferences( self.interface, self.update_interval, self.ntmMainWindow_keep_above, self.ntmMainWindow_opacity, self.general_pref_autorun, self.general_pref_online_check, self.general_pref_tray_activate_action, self.importexport_file) self.update_event += self.ntmgui.update_h self.session_start = datetime.datetime.now() self.info_win_load = True if self.net_man.get_state(): ntmtools.dbg_msg("State : Online") self.set_online() else: ntmtools.dbg_msg("State : Offline") self.set_offline() # Traffic Module self.mtraffic = MTraffic.make_from_db(self) self.update_event += self.mtraffic.update_h self.ntmgui.set_traffic_module(self.mtraffic) # Time Slot Module self.mtimeslot = MTimeSlot.make_from_db(self) self.update_event += self.mtimeslot.update_h self.ntmgui.set_timeslot_module(self.mtimeslot) # Time Module self.mtime = MTime.make_from_db(self) self.update_event += self.mtime.update_h self.ntmgui.set_time_module(self.mtime) # Info/News self.info_win = ntminfo.NtmInfo(self.net_man.get_state()) self.info_win_load = False gobject.timeout_add(self.update_interval * 1000, self.update_count) self.update_count(True) self.pinger.start()
def set_preferences(self, active, totalSlots, slotLength, auto_disconnect, period_length, custom_days, first_day, period_autoupdate, alert_active, alert_predate): self.dialog_showed = False update_timeslot = False if (self.active != active): self.active = active ntmtools.set_db_var(self.db_conn, "time_slot.active", ntmtools.bool_to_str_int(active)) update_timeslot = True if (self.totalSlots != totalSlots): self.totalSlots = totalSlots ntmtools.set_db_var(self.db_conn, "time_slot.total_slots", str(int(totalSlots))) if (self.slotLength != slotLength): self.slotLength = slotLength ntmtools.set_db_var(self.db_conn, "time_slot.slot_length", str(int(slotLength))) update_timeslot = True if (self.auto_disconnect != auto_disconnect): self.auto_disconnect = auto_disconnect ntmtools.set_db_var(self.db_conn, "time_slot.auto_disconnect", ntmtools.bool_to_str_int(auto_disconnect)) update_timeslot = True if (self.period_length != period_length): self.period_length = period_length ntmtools.set_db_var(self.db_conn, "time_slot.period_length", str(int(period_length))) update_timeslot = True if (self.custom_days != custom_days): self.custom_days = custom_days ntmtools.set_db_var(self.db_conn, "time_slot.custom_days", str(int(custom_days))) if (period_length == 0): update_timeslot = True if (self.first_day != first_day): self.first_day = first_day ntmtools.set_db_var(self.db_conn, "time_slot.first_day", first_day.isoformat()) update_timeslot = True if (self.period_autoupdate != period_autoupdate): self.period_autoupdate = period_autoupdate ntmtools.set_db_var(self.db_conn, "time_slot.period_autoupdate", ntmtools.bool_to_str_int(period_autoupdate)) if self.period_autoupdate: self.update_period(self.now) update_timeslot = True if (update_timeslot): last_day = ntmtools.get_last_day(first_day, period_length, custom_days) ret = MTimeSlot.load_sessions( self.db_conn, ntmtools.date_to_datetime_start(first_day), ntmtools.date_to_datetime_end(last_day), slotLength) self.slotsUsed = ret[0] self.tsTime = ret[1] if (self.alert_active != alert_active): self.alert_active = alert_active ntmtools.set_db_var(self.db_conn, "time_slot.alert.active", ntmtools.bool_to_str_int(alert_active)) if (self.alert_predate != alert_predate): self.alert_predate = alert_predate ntmtools.set_db_var(self.db_conn, "time_slot.alert.predate", str(int(alert_predate))) if self.notify_ok: self.notifyalert_predate = pynotify.Notification( _("NTM - Time Slot"), "{0} ".format(self.alert_predate) + _("seconds before timeslot end!")) if self.active: self.gui_main.show() self.update_main_gui() self.check_limit() else: self.gui_main.hide()
def make_from_db(ntm): conn = ntm.db_conn val_str = ntmtools.read_db_var(conn, "time_slot.active") try: active = (ntmtools.str_to_int(val_str, 1) != 0) except: ntmtools.dbg_msg( _("Wrong value for the param") + " 'time_slot.active' " + _("or is not stored. Default value") + " '" + _("True") + "'") active = True ntmtools.set_db_var(conn, "time_slot.active", "1") val_str = ntmtools.read_db_var(conn, "time_slot.total_slots") try: total_slots = ntmtools.str_to_int(val_str, 200) except: ntmtools.dbg_msg( _("Wrong value for the param") + " 'time_slot.total_slots' " + _("or is not stored. Default value") + " '200'") total_slots = 200 ntmtools.set_db_var(conn, "time_slot.total_slots", total_slots) val_str = ntmtools.read_db_var(conn, "time_slot.slot_length") try: slot_length = ntmtools.str_to_int(val_str, 15 * 60) except: ntmtools.dbg_msg( _("Wrong value for the param") + " 'time_slot.slot_length' " + _("or is not stored. Default value") + " 15'") slot_length = 15 * 60 ntmtools.set_db_var(conn, "time_slot.slot_length", slot_length) val_str = ntmtools.read_db_var(conn, "time_slot.auto_disconnect") try: auto_disconnect = (ntmtools.str_to_int(val_str, 1) != 0) except: ntmtools.dbg_msg( _("Wrong value for the param") + " 'time_slot.auto_disconnect' " + _("or is not stored. Default value") + " '" + _("True") + "'") auto_disconnect = True ntmtools.set_db_var(conn, "time_slot.auto_disconnect", "1") val_str = ntmtools.read_db_var(conn, "time_slot.period_length") try: period_length = int(val_str) except: ntmtools.dbg_msg( _("Wrong value for the param") + " 'time_slot.period_length' " + _("or is not stored. Default value") + " '" + _("Month") + "'") period_length = 3 # 3 -> Month ntmtools.set_db_var(conn, "time_slot.period_length", str(int(period_length))) val_str = ntmtools.read_db_var(conn, "time_slot.custom_days") try: custom_days = int(val_str) except: ntmtools.dbg_msg( _("Wrong value for the param") + " 'time_slot.custom_days' " + _("or is not stored. Default value") + " 30") custom_days = 30 ntmtools.set_db_var(conn, "time_slot.custom_days", str(int(custom_days))) val_str = ntmtools.read_db_var(conn, "time_slot.first_day") try: first_day = ntmtools.str_to_date(val_str) except: ntmtools.dbg_msg( _("Wrong value for the param") + " 'time_slot.first_day' " + _("or is not stored. Default value") + " '2009-10-01'") first_day = datetime.date(2009, 10, 01) ntmtools.set_db_var(conn, "time_slot.first_day", first_day) val_str = ntmtools.read_db_var(conn, "time_slot.period_autoupdate") try: period_autoupdate = (ntmtools.str_to_int(val_str, 1) != 0) except: ntmtools.dbg_msg( _("Wrong value for the param") + " 'time_slot.period_autoupdate' " + _("or is not stored. Default value") + " '" + _("True") + "'") period_autoupdate = True ntmtools.set_db_var(conn, "time_slot.period_autoupdate", "1") val_str = ntmtools.read_db_var(conn, "time_slot.alert.active") try: alert_active = (ntmtools.str_to_int(val_str, 1) != 0) except: ntmtools.dbg_msg( _("Wrong value for the param") + " 'time_slot.alert.active' " + _("or is not stored. Default value") + " '" + _("True") + "'") alert_active = True ntmtools.set_db_var(conn, "time_slot.alert.active", "1") val_str = ntmtools.read_db_var(conn, "time_slot.alert.predate") try: alert_predate = int(val_str) except: ntmtools.dbg_msg( _("Wrong value for the param") + " 'time_slot.alert.predate' " + _("or is not stored. Default value") + ' 30"') alert_predate = 30 ntmtools.set_db_var(conn, "time_slot.alert.predate", str(int(alert_predate))) last_day = ntmtools.get_last_day(first_day, period_length, custom_days) ret = MTimeSlot.load_sessions( conn, ntmtools.date_to_datetime_start(first_day), ntmtools.date_to_datetime_end(last_day), slot_length) slots_used = ret[0] ts_time = ret[1] return MTimeSlot(ntm, active, slots_used, total_slots, ts_time, slot_length, auto_disconnect, period_length, custom_days, first_day, period_autoupdate, alert_active, alert_predate)
def set_preferences( self, interface, updateInterval, keep_above, opacity, autorun, online_check, tray_activate_action, importexport_file, iconfile, iconfile_active): ntmtools.dbg_msg("NTM.set_preferences") ris = self.get_proc_net_dev(interface) if (ris == None) and self.online: dia = gtk.Dialog(_('NTM - Interface'), self.ntmgui.statusIconMenu.get_toplevel(), #the toplevel wgt of your app gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT, #binary flags or'ed together (_("Change the interface"), 77, gtk.STOCK_CLOSE, gtk.RESPONSE_CLOSE)) dia.vbox.pack_start(gtk.Label(_('The interface "{0}" is not detected or inactive.\nDo you want to confirm the change?').format(interface))) dia.show_all() result = dia.run() doChangeInterface = (result == 77) dia.destroy() else: doChangeInterface = True if doChangeInterface: interface_changed = (self.interface != interface) if interface_changed & self.online: self.set_offline() self.interface = interface self.interfaceProcNetDev = interface self.set_online() else: self.interface = interface self.interfaceProcNetDev = interface ntmtools.set_db_var(self.db_conn, "general.interface", interface) if interface_changed: self.net_man.set_interface(self.interface) self.ntmMainWindow_keep_above = keep_above if self.ntmMainWindow_keep_above: ntmtools.set_db_var(self.db_conn, "general.keep_above", "1") else: ntmtools.set_db_var(self.db_conn, "general.keep_above", "0") self.ntmMainWindow_opacity = opacity ntmtools.set_db_var(self.db_conn, "general.opacity", opacity) self.timeout_changed = (self.update_interval != updateInterval) self.update_interval = int(updateInterval) ntmtools.set_db_var(self.db_conn, "general.update_interval", str(int(updateInterval))) self.general_pref_autorun = autorun if autorun: ntmtools.set_db_var(self.db_conn, "general.autorun", "1") else: ntmtools.set_db_var(self.db_conn, "general.autorun", "0") self.set_autorun(autorun) if self.general_pref_online_check != online_check: if online_check==0: self.general_pref_online_check = 0 self.net_man.set_mode(online_check) ntmtools.set_db_var(self.db_conn, "general.online_check", "0") elif online_check==1: self.general_pref_online_check = 1 self.net_man.set_mode(online_check) ntmtools.set_db_var(self.db_conn, "general.online_check", "1") else: ntmtools.dbg_msg("Error: Invald online_check value.\n") if self.general_pref_tray_activate_action != tray_activate_action: if tray_activate_action==0: self.general_pref_tray_activate_action = 0 ntmtools.set_db_var(self.db_conn, "general.tray_activate_action", "0") elif tray_activate_action==1: self.general_pref_tray_activate_action = 1 ntmtools.set_db_var(self.db_conn, "general.tray_activate_action", "1") else: ntmtools.dbg_msg("Error: Invald tray_activate_action value.\n") if self.importexport_file != importexport_file: self.importexport_file = importexport_file ntmtools.set_db_var(self.db_conn, "general.importexport_file", importexport_file) if self.trayicon_file != iconfile: self.trayicon_file = iconfile ntmtools.set_db_var(self.db_conn, "general.trayicon_file", iconfile) self.ntmgui.set_trayicon_file(iconfile) if self.trayicon_active_file != iconfile_active: self.trayicon_active_file = iconfile_active ntmtools.set_db_var(self.db_conn, "general.trayicon_active_file", iconfile_active) self.ntmgui.set_trayicon_active_file(iconfile_active) ntmtools.dbg_msg("END - NTM.set_preferences")
def __init__(self): self.stop = False self.session_start = None self.last_traffic_in, self.last_traffic_out = None, None self.online = False self.timeout_changed = False self.versionChecked = False self.logTraffic = False self.discMsgDialog = False self.last_update = None self.pinger = pinger.Pinger("google.com", self.rtt_callback) ## db update self.db_update_interval = 20 # sec self.last_db_update = datetime.datetime.now() self.d_rb_db = self.d_tb_db = 0 self.sys_info = ntmtools.getSysInfo() #print(self.sys_info) self.d_rb, self.d_tb = 0, 0 self.home_path = os.getenv("HOME") self.profile_path = self.home_path + "/" + globaldef.NTM_PROFILE_RELPATH if not os.path.exists(self.profile_path): os.makedirs(self.profile_path) db_file_path = self.profile_path + "/" + globaldef.NTM_DB_NAME self.db_conn = sqlite3.connect(db_file_path, check_same_thread = False) self.update_event = Event() ## Create tables self.create_tables(self.db_conn) res = ntmtools.read_db_var(self.db_conn, "general.interface") if res != None: self.interface = res else: ntmtools.dbg_msg(_("Wrong value for the param") + " 'general.interface' " + _("or is not stored. Default value") + " 'ttyACM0'.") ntmtools.set_db_var(self.db_conn, "general.interface", "ttyACM0") self.interface = "ttyACM0" self.interfaceProcNetDev = self.interface res = ntmtools.read_db_var(self.db_conn, "general.update_interval") # sec try: self.update_interval = int(float(res)) except: ntmtools.dbg_msg(_("Wrong value for the param") + " 'general.update_interval' " + _("or is not stored. Default value") + " '2'.") ntmtools.set_db_var(self.db_conn, "general.update_interval", "2") self.update_interval = 2 res = ntmtools.read_db_var(self.db_conn, "general.last_version_check") # datetime try: self.last_version_check = int(float(res)) self.versionChecked = ((time.time() - self.last_version_check) < (5*24*60*60)) except: ntmtools.dbg_msg(_("Wrong value for the param") + " 'general.last_version_check' " + _("or is not stored. Default value") + " '" + _("now") + "'.") ntmtools.set_db_var(self.db_conn, "general.last_version_check", str(int(time.time()))) self.last_version_check = int(time.time()) self.versionChecked = False res = ntmtools.read_db_var(self.db_conn, "general.keep_above") # 0 or 1 try: self.ntmMainWindow_keep_above = ( int(float(res)) != 0) except: ntmtools.dbg_msg(_("Wrong value for the param") + " 'general.keep_above' " + _("or is not stored. Default value") + " '0.") ntmtools.set_db_var(self.db_conn, "general.keep_above", "0") self.ntmMainWindow_keep_above = False res = ntmtools.read_db_var(self.db_conn, "general.opacity") # 0 to 100 try: self.ntmMainWindow_opacity = int(float(res)) except: ntmtools.dbg_msg(_("Wrong value for the param") + " 'general.opacity' " + _("or is not stored. Default value") + " '100.") ntmtools.set_db_var(self.db_conn, "general.opacity", "100") self.ntmMainWindow_opacity = 100 res = ntmtools.read_db_var(self.db_conn, "general.autorun") # 0 or 1 try: self.general_pref_autorun = ( int(float(res)) != 0) except: ntmtools.dbg_msg(_("Wrong value for the param") + " 'general.autorun' " + _("or is not stored. Default value") + " '" + _("True") + "'.") ntmtools.set_db_var(self.db_conn, "general.autorun", "1") self.general_pref_autorun = True self.set_autorun(self.general_pref_autorun) res = ntmtools.read_db_var(self.db_conn, "general.online_check") # 0->NetworkManager; 1->Ping try: self.general_pref_online_check = int(float(res)) except: ntmtools.dbg_msg(_("Wrong value for the param") + " 'general.online_check' " + _("or is not stored. Default value") + " 0 (NetworkManager).") self.general_pref_online_check = 0 ntmtools.set_db_var(self.db_conn, "general.online_check", "0") res = ntmtools.read_db_var(self.db_conn, "general.tray_activate_action") # 0->Show Main Window; 1->Show Nptify; try: self.general_pref_tray_activate_action = int(float(res)) except: ntmtools.dbg_msg(_("Wrong value for the param") + " 'general.tray_activate_action' " + _("or is not stored. Default value") + " 0 (Show Main Window).") self.general_pref_tray_activate_action = 0 ntmtools.set_db_var(self.db_conn, "general.tray_activate_action", "0") res = ntmtools.read_db_var(self.db_conn, "general.trayicon_file") if res != None: ntmtools.dbg_msg("general.trayicon_file : " + res) self.trayicon_file = res else: ntmtools.dbg_msg(_("Wrong value for the param") + " 'general.trayicon_file' " + _("or is not stored. Default value") + " " + globaldef.DEFAULT_NTM_ICON_OFF) ntmtools.set_db_var(self.db_conn, "general.trayicon_file", globaldef.DEFAULT_NTM_ICON_OFF) self.trayicon_file = globaldef.DEFAULT_NTM_ICON_OFF res = ntmtools.read_db_var(self.db_conn, "general.trayicon_active_file") if res != None: ntmtools.dbg_msg("general.trayicon_active_file : " + res) self.trayicon_active_file = res else: ntmtools.dbg_msg(_("Wrong value for the param") + " 'general.trayicon_active_file' " + _("or is not stored. Default value") + " " + globaldef.DEFAULT_NTM_ICON_ON) ntmtools.set_db_var(self.db_conn, "general.trayicon_active_file", globaldef.DEFAULT_NTM_ICON_ON) self.trayicon_active_file = globaldef.DEFAULT_NTM_ICON_ON res = ntmtools.read_db_var(self.db_conn, "general.importexport_file") if res != None: self.importexport_file = res else: ntmtools.dbg_msg(_("Wrong value for the param") + " 'general.importexport_file' " + _("or is not stored. Default value") + " ''.") ntmtools.set_db_var(self.db_conn, "general.importexport_file", "") self.importexport_file = "" # NetMan self.net_man = netman.NetMan(self.general_pref_online_check, self.interface) self.net_man.add_online_handler(self.set_online) self.net_man.add_offline_handler(self.set_offline) # GUI self.ntmgui = ntmgui.NtmGui(self) self.ntmgui.apply_prop(self.ntmMainWindow_keep_above, self.ntmMainWindow_opacity) self.set_autorun(self.general_pref_autorun) self.ntmgui.set_general_preferences( self.interface, self.update_interval, self.ntmMainWindow_keep_above, self.ntmMainWindow_opacity, self.general_pref_autorun, self.general_pref_online_check, self.general_pref_tray_activate_action, self.importexport_file ) self.update_event += self.ntmgui.update_h self.session_start = datetime.datetime.now() self.info_win_load = True if self.net_man.get_state(): ntmtools.dbg_msg("State : Online") self.set_online() else: ntmtools.dbg_msg("State : Offline") self.set_offline() # Traffic Module self.mtraffic = MTraffic.make_from_db(self) self.update_event += self.mtraffic.update_h self.ntmgui.set_traffic_module(self.mtraffic) # Time Slot Module self.mtimeslot = MTimeSlot.make_from_db(self) self.update_event += self.mtimeslot.update_h self.ntmgui.set_timeslot_module(self.mtimeslot) # Time Module self.mtime = MTime.make_from_db(self) self.update_event += self.mtime.update_h self.ntmgui.set_time_module(self.mtime) # Info/News self.info_win = ntminfo.NtmInfo(self.net_man.get_state()) self.info_win_load = False gobject.timeout_add(self.update_interval*1000, self.update_count) self.update_count(True) self.pinger.start()
def set_preferences(self, active, traffic_limit, auto_disconnect, period_length, custom_days, first_day, period_autoupdate): #ntmtools.dbg_msg("mtraffic.setPreferences({0},{1},{2},{3},{4})\n".format(active, traffic_limit, auto_disconnect, period_length, first_day)) update = False if (self.active != active): self.active = active ntmtools.set_db_var(self.db_conn, "traffic.active", ntmtools.bool_to_str_int(active)) update = True if (self.traffic_limit != traffic_limit): self.traffic_limit = traffic_limit ntmtools.set_db_var(self.db_conn, "traffic.limit", str(int(traffic_limit))) update = True if (self.auto_disconnect != auto_disconnect): self.auto_disconnect = auto_disconnect ntmtools.set_db_var(self.db_conn, "traffic.auto_disconnect", ntmtools.bool_to_str_int(auto_disconnect)) update_traffic = False if (self.period_length != period_length): self.period_length = period_length ntmtools.set_db_var(self.db_conn, "traffic.period_length", str(int(period_length))) update_traffic = True if (self.custom_days != custom_days): self.custom_days = custom_days ntmtools.set_db_var(self.db_conn, "traffic.custom_days", str(int(custom_days))) update_traffic = True if (self.first_day != first_day): self.first_day = first_day ntmtools.set_db_var(self.db_conn, "traffic.first_day", first_day.isoformat()) update_traffic = True if (self.period_autoupdate != period_autoupdate): self.period_autoupdate = period_autoupdate ntmtools.set_db_var(self.db_conn, "traffic.period_autoupdate", ntmtools.bool_to_str_int(period_autoupdate)) if self.period_autoupdate: self.update_period(self.now) update_traffic = True if update_traffic: last_day = ntmtools.get_last_day(self.first_day, self.period_length, self.custom_days) ret = MTraffic.load_traffic(self.db_conn, self.first_day, last_day) self.rec_traffic = ret[0] self.tra_traffic = ret[1] update = True if update: if self.active: self.gui_main.show() self.update_main_gui() self.check_limit() else: self.gui_main.hide()
def set_preferences(self, active, totalSlots, slotLength, auto_disconnect, period_length, custom_days, first_day, period_autoupdate, alert_active, alert_predate ): self.dialog_showed = False update_timeslot = False if (self.active != active): self.active = active ntmtools.set_db_var(self.db_conn, "time_slot.active", ntmtools.bool_to_str_int(active)) update_timeslot = True if (self.totalSlots != totalSlots): self.totalSlots = totalSlots ntmtools.set_db_var(self.db_conn, "time_slot.total_slots", str(int(totalSlots))) if (self.slotLength != slotLength): self.slotLength = slotLength ntmtools.set_db_var(self.db_conn, "time_slot.slot_length", str(int(slotLength))) update_timeslot = True if (self.auto_disconnect != auto_disconnect): self.auto_disconnect = auto_disconnect ntmtools.set_db_var(self.db_conn, "time_slot.auto_disconnect", ntmtools.bool_to_str_int(auto_disconnect)) update_timeslot = True if (self.period_length != period_length): self.period_length = period_length ntmtools.set_db_var(self.db_conn, "time_slot.period_length", str(int(period_length))) update_timeslot = True if (self.custom_days != custom_days): self.custom_days = custom_days ntmtools.set_db_var(self.db_conn, "time_slot.custom_days", str(int(custom_days))) if (period_length == 0): update_timeslot = True if (self.first_day != first_day): self.first_day = first_day ntmtools.set_db_var(self.db_conn, "time_slot.first_day", first_day.isoformat()) update_timeslot = True if (self.period_autoupdate != period_autoupdate): self.period_autoupdate = period_autoupdate ntmtools.set_db_var(self.db_conn, "time_slot.period_autoupdate", ntmtools.bool_to_str_int(period_autoupdate)) if self.period_autoupdate: self.update_period(self.now) update_timeslot = True if (update_timeslot): last_day = ntmtools.get_last_day(first_day, period_length, custom_days) ret = MTimeSlot.load_sessions(self.db_conn, ntmtools.date_to_datetime_start(first_day), ntmtools.date_to_datetime_end(last_day), slotLength) self.slotsUsed = ret[0] self.tsTime = ret[1] if (self.alert_active != alert_active): self.alert_active = alert_active ntmtools.set_db_var(self.db_conn, "time_slot.alert.active", ntmtools.bool_to_str_int(alert_active)) if (self.alert_predate != alert_predate): self.alert_predate = alert_predate ntmtools.set_db_var(self.db_conn, "time_slot.alert.predate", str(int(alert_predate))) if self.notify_ok: self.notifyalert_predate = pynotify.Notification (_("NTM - Time Slot"), "{0} ".format(self.alert_predate) + _("seconds before timeslot end!")) if self.active: self.gui_main.show() self.update_main_gui() self.check_limit() else: self.gui_main.hide()
def make_from_db(ntm): conn = ntm.db_conn val_str = ntmtools.read_db_var(conn, "time.active") try: active = (ntmtools.str_to_int(val_str, 1) != 0) except: ntmtools.dbg_msg(_("Wrong value for the param") + " 'time.active' " + _("or is not stored. Default value") + " 'True'") active = True ntmtools.set_db_var(conn, "time.active", "1") val_str = ntmtools.read_db_var(conn, "time.total_sec") try: total_sec = ntmtools.str_to_int(val_str, 30*60*60) # 30h except: ntmtools.dbg_msg(_("Wrong value for the param") + " 'time.total_sec' " + _("or is not stored. Default value") + " '30h'") total_sec = 30*60*60 ntmtools.set_db_var(conn, "time.total_sec", total_sec) val_str = ntmtools.read_db_var(conn, "time.auto_disconnect") try: auto_disconnect = (ntmtools.str_to_int(val_str, 1) != 0) except: ntmtools.dbg_msg(_("Wrong value for the param") + " 'time.auto_disconnect' " + _("or is not stored. Default value") + " '" +_("True") + "'") auto_disconnect = True ntmtools.set_db_var(conn, "time.auto_disconnect", "1") val_str = ntmtools.read_db_var(conn, "time.period_length") try: period_length = int(val_str) except: ntmtools.dbg_msg(_("Wrong value for the param") + " 'time.period_length' " + _("or is not stored. Default value") + " '" + _("Month") + "'") period_length = 3 # 3 -> Month ntmtools.set_db_var(conn, "time.period_length", str(int(period_length))) val_str = ntmtools.read_db_var(conn, "time.custom_days") try: custom_days = int(val_str) except: ntmtools.dbg_msg(_("Wrong value for the param") + " 'time.custom_days' " + _("or is not stored. Default value") + " '30'") custom_days = 30 ntmtools.set_db_var(conn, "time.custom_days", str(int(custom_days))) val_str = ntmtools.read_db_var(conn, "time.first_day") try: first_day = ntmtools.str_to_date(val_str) except: ntmtools.dbg_msg(_("Wrong value for the param") + " 'time.first_day' " + _("or is not stored. Default value") + " '2009-10-01'") first_day = datetime.date(2009, 10, 01) ntmtools.set_db_var(conn, "time.first_day", first_day) val_str = ntmtools.read_db_var(conn, "time.period_autoupdate") try: period_autoupdate = (ntmtools.str_to_int(val_str, 1) != 0) except: ntmtools.dbg_msg(_("Wrong value for the param") + " 'time.period_autoupdate' " + _("or is not stored. Default value") + " '" + _("True") + "'") period_autoupdate = True ntmtools.set_db_var(conn, "time.period_autoupdate", "1") last_day = ntmtools.get_last_day(first_day, period_length, custom_days) used_sec = MTime.load_time_used(conn, ntmtools.date_to_datetime_start(first_day), ntmtools.date_to_datetime_end(last_day)) return MTime(ntm, active, used_sec, total_sec, auto_disconnect, period_length, custom_days, first_day, period_autoupdate)
def set_preferences(self, active, total_sec, auto_disconnect, period_length, custom_days, first_day, period_autoupdate): self.dialog_showed = False update_time = False if (self.active != active): self.active = active ntmtools.set_db_var(self.db_conn, "time.active", ntmtools.bool_to_str_int(active)) update_time = True if (self.total_sec != total_sec): self.total_sec = total_sec ntmtools.set_db_var(self.db_conn, "time.total_sec", str(total_sec)) if (self.auto_disconnect != auto_disconnect): self.auto_disconnect = auto_disconnect ntmtools.set_db_var(self.db_conn, "time.auto_disconnect", ntmtools.bool_to_str_int(auto_disconnect)) if (self.period_length != period_length): self.period_length = period_length ntmtools.set_db_var(self.db_conn, "time.period_length", str(int(period_length))) update_time = True if (self.custom_days != custom_days): self.custom_days = custom_days ntmtools.set_db_var(self.db_conn, "time.custom_days", str(int(custom_days))) #if (period_length == 0): update_timeslot = True update_time = True if (self.first_day != first_day): self.first_day = first_day ntmtools.set_db_var(self.db_conn, "time.first_day", first_day.isoformat()) update_time = True if (self.period_autoupdate != period_autoupdate): self.period_autoupdate = period_autoupdate ntmtools.set_db_var(self.db_conn, "time.period_autoupdate", ntmtools.bool_to_str_int(period_autoupdate)) update_time = True if (update_time): if self.period_autoupdate: self.update_period(self.now) else: last_day = ntmtools.get_last_day(self.first_day, self.period_length, self.custom_days) self.used_sec = MTime.load_time_used(self.db_conn, ntmtools.date_to_datetime_start(self.first_day), ntmtools.date_to_datetime_end(last_day)) if self.active: self.gui_main.show() self.update_main_gui() self.check_limit() else: self.gui_main.hide()
def make_from_db(ntm): conn = ntm.db_conn val_str = ntmtools.read_db_var(conn, "time_slot.active") try: active = (ntmtools.str_to_int(val_str, 1) != 0) except: ntmtools.dbg_msg(_("Wrong value for the param") + " 'time_slot.active' " + _("or is not stored. Default value") + " '" + _("True") + "'") active = True ntmtools.set_db_var(conn, "time_slot.active", "1") val_str = ntmtools.read_db_var(conn, "time_slot.total_slots") try: total_slots = ntmtools.str_to_int(val_str, 200) except: ntmtools.dbg_msg(_("Wrong value for the param") + " 'time_slot.total_slots' " + _("or is not stored. Default value") + " '200'") total_slots = 200 ntmtools.set_db_var(conn, "time_slot.total_slots", total_slots) val_str = ntmtools.read_db_var(conn, "time_slot.slot_length") try: slot_length = ntmtools.str_to_int(val_str, 15*60) except: ntmtools.dbg_msg(_("Wrong value for the param") + " 'time_slot.slot_length' " + _("or is not stored. Default value") + " 15'") slot_length = 15*60 ntmtools.set_db_var(conn, "time_slot.slot_length", slot_length) val_str = ntmtools.read_db_var(conn, "time_slot.auto_disconnect") try: auto_disconnect = (ntmtools.str_to_int(val_str, 1) != 0) except: ntmtools.dbg_msg(_("Wrong value for the param") + " 'time_slot.auto_disconnect' " + _("or is not stored. Default value") + " '" + _("True") + "'") auto_disconnect = True ntmtools.set_db_var(conn, "time_slot.auto_disconnect", "1") val_str = ntmtools.read_db_var(conn, "time_slot.period_length") try: period_length = int(val_str) except: ntmtools.dbg_msg(_("Wrong value for the param") + " 'time_slot.period_length' " + _("or is not stored. Default value") + " '" + _("Month") + "'") period_length = 3 # 3 -> Month ntmtools.set_db_var(conn, "time_slot.period_length", str(int(period_length))) val_str = ntmtools.read_db_var(conn, "time_slot.custom_days") try: custom_days = int(val_str) except: ntmtools.dbg_msg(_("Wrong value for the param") + " 'time_slot.custom_days' " + _("or is not stored. Default value") + " 30") custom_days = 30 ntmtools.set_db_var(conn, "time_slot.custom_days", str(int(custom_days))) val_str = ntmtools.read_db_var(conn, "time_slot.first_day") try: first_day = ntmtools.str_to_date(val_str) except: ntmtools.dbg_msg(_("Wrong value for the param") + " 'time_slot.first_day' " + _("or is not stored. Default value") + " '2009-10-01'") first_day = datetime.date(2009, 10, 01) ntmtools.set_db_var(conn, "time_slot.first_day", first_day) val_str = ntmtools.read_db_var(conn, "time_slot.period_autoupdate") try: period_autoupdate = (ntmtools.str_to_int(val_str, 1) != 0) except: ntmtools.dbg_msg(_("Wrong value for the param") + " 'time_slot.period_autoupdate' " + _("or is not stored. Default value") + " '" + _("True") + "'") period_autoupdate = True ntmtools.set_db_var(conn, "time_slot.period_autoupdate", "1") val_str = ntmtools.read_db_var(conn, "time_slot.alert.active") try: alert_active = (ntmtools.str_to_int(val_str, 1) != 0) except: ntmtools.dbg_msg(_("Wrong value for the param") + " 'time_slot.alert.active' " + _("or is not stored. Default value") + " '" + _("True") + "'") alert_active = True ntmtools.set_db_var(conn, "time_slot.alert.active", "1") val_str = ntmtools.read_db_var(conn, "time_slot.alert.predate") try: alert_predate = int(val_str) except: ntmtools.dbg_msg(_("Wrong value for the param") + " 'time_slot.alert.predate' " + _("or is not stored. Default value") + ' 30"') alert_predate = 30 ntmtools.set_db_var(conn, "time_slot.alert.predate", str(int(alert_predate))) last_day = ntmtools.get_last_day(first_day, period_length, custom_days) ret = MTimeSlot.load_sessions(conn, ntmtools.date_to_datetime_start(first_day), ntmtools.date_to_datetime_end(last_day), slot_length) slots_used = ret[0] ts_time = ret[1] return MTimeSlot(ntm, active, slots_used, total_slots, ts_time, slot_length, auto_disconnect, period_length, custom_days, first_day, period_autoupdate, alert_active, alert_predate)
def make_from_db(ntm): conn = ntm.db_conn val_str = ntmtools.read_db_var(conn, "time.active") try: active = (ntmtools.str_to_int(val_str, 1) != 0) except: ntmtools.dbg_msg( _("Wrong value for the param") + " 'time.active' " + _("or is not stored. Default value") + " 'True'") active = True ntmtools.set_db_var(conn, "time.active", "1") val_str = ntmtools.read_db_var(conn, "time.total_sec") try: total_sec = ntmtools.str_to_int(val_str, 30 * 60 * 60) # 30h except: ntmtools.dbg_msg( _("Wrong value for the param") + " 'time.total_sec' " + _("or is not stored. Default value") + " '30h'") total_sec = 30 * 60 * 60 ntmtools.set_db_var(conn, "time.total_sec", total_sec) val_str = ntmtools.read_db_var(conn, "time.auto_disconnect") try: auto_disconnect = (ntmtools.str_to_int(val_str, 1) != 0) except: ntmtools.dbg_msg( _("Wrong value for the param") + " 'time.auto_disconnect' " + _("or is not stored. Default value") + " '" + _("True") + "'") auto_disconnect = True ntmtools.set_db_var(conn, "time.auto_disconnect", "1") val_str = ntmtools.read_db_var(conn, "time.period_length") try: period_length = int(val_str) except: ntmtools.dbg_msg( _("Wrong value for the param") + " 'time.period_length' " + _("or is not stored. Default value") + " '" + _("Month") + "'") period_length = 3 # 3 -> Month ntmtools.set_db_var(conn, "time.period_length", str(int(period_length))) val_str = ntmtools.read_db_var(conn, "time.custom_days") try: custom_days = int(val_str) except: ntmtools.dbg_msg( _("Wrong value for the param") + " 'time.custom_days' " + _("or is not stored. Default value") + " '30'") custom_days = 30 ntmtools.set_db_var(conn, "time.custom_days", str(int(custom_days))) val_str = ntmtools.read_db_var(conn, "time.first_day") try: first_day = ntmtools.str_to_date(val_str) except: ntmtools.dbg_msg( _("Wrong value for the param") + " 'time.first_day' " + _("or is not stored. Default value") + " '2009-10-01'") first_day = datetime.date(2009, 10, 01) ntmtools.set_db_var(conn, "time.first_day", first_day) val_str = ntmtools.read_db_var(conn, "time.period_autoupdate") try: period_autoupdate = (ntmtools.str_to_int(val_str, 1) != 0) except: ntmtools.dbg_msg( _("Wrong value for the param") + " 'time.period_autoupdate' " + _("or is not stored. Default value") + " '" + _("True") + "'") period_autoupdate = True ntmtools.set_db_var(conn, "time.period_autoupdate", "1") last_day = ntmtools.get_last_day(first_day, period_length, custom_days) used_sec = MTime.load_time_used( conn, ntmtools.date_to_datetime_start(first_day), ntmtools.date_to_datetime_end(last_day)) return MTime(ntm, active, used_sec, total_sec, auto_disconnect, period_length, custom_days, first_day, period_autoupdate)