#!/usr/bin/python # -*- Mode: Python; coding: utf-8; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- # # main.py # Copyright (C) 2015 Miguel Angel # # shutdowner 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. # # shutdowner 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 . import gi gi.require_version('Gtk', '3.0') from gi.repository import Gtk, GdkPixbuf, Gdk, GObject import os, sys from datetime import time import time as t import xmlrpclib as x import threading import gettext gettext.textdomain('lliurex-shutdowner') _ = gettext.gettext #Comment the first line and uncomment the second before installing #or making the tarball (alternatively, use project variables) UI_FILE = "/usr/share/lliurex-shutdowner/shutdowner.ui" #UI_FILE = "/usr/local/share/shutdowner/ui/shutdowner.ui" GObject.threads_init() class Hilo(threading.Thread): def __init__(self,window,window2,n4d,spin,label): super(Hilo,self).__init__() print('inithilo') self.n4d = n4d self.win = window self.win2 = window2 self.quit = False self.user = None self.pwd = None self.spin = spin self.pause=True self.label=label def do_login(self,win,win2,n4d,user,pwd,spin,label): GObject.idle_add(self.show_msg,label,'') out=n4d.list_clients((user,pwd),"Shutdowner") print(out) if 'PASSWORD ERROR' in out or 'USER DOES NOT EXIST' in out: print('ERROR') GObject.idle_add(self.hide_spin,spin) GObject.idle_add(self.show_msg,label,out) else: print('NOERROR OK!') GObject.idle_add(self.open_win,self.win,self.win2) self.pause = True return False def show_msg(self,label,msg): label.set_text(msg) def hide_spin(self,spin): spin.hide() def open_win(self,win,win2): win.hide() win2.show() return False def run(self): while not self.quit: if not self.pause: self.do_login(self.win,self.win2,self.n4d,self.user,self.pwd,self.spin,self.label) t.sleep(0.1) class GUI: window = None window2 = None window3 = None #liststore1 = Gtk.ListStore(GObject.TYPE_STRING) #liststore2 = Gtk.ListStore(GObject.TYPE_STRING) days = { _('Monday'): False, _('Tuesday') : False, _('Wednesday'): False, _('Thursday'): False, _('Friday'): False} daynames = [_('Monday'),_('Tuesday'),_('Wednesday'),_('Thursday'),_('Friday')] hora = None clients_file="/run/clients_tmp" def __init__(self): self.builder = Gtk.Builder() self.builder.set_translation_domain('lliurex-shutdowner') self.builder.add_from_file(UI_FILE) self.builder.connect_signals(self) self.window = self.builder.get_object('window') self.window2 = self.builder.get_object('window1') self.window3 = self.builder.get_object('window2') self.spin = self.builder.get_object('spinner1') #mins=['','5','10','15','20','25','30','35','40','45','50','55'] #horas=['','8','9','10','11','12','13','14','15','16','17','18','19','20','21'] #for x in horas: # self.liststore1.append([x]); #for x in mins: # self.liststore2.append([x]); self.label8 = self.builder.get_object('label8') self.n4dcli = x.ServerProxy("https://localhost:9779") self.hilo=Hilo(self.window,self.window2,self.n4dcli,self.spin,self.label8) self.hilo.start() self.window.show_all() self.spin.hide() def destroy(self, window): self.hilo.quit = True Gtk.main_quit() def on_button1_clicked(self,obj): self.spin.show() entry1=self.builder.get_object('entry1') entry2=self.builder.get_object('entry2') self.user=entry1.get_text() self.pwd=entry2.get_text() self.usuario=(self.user,self.pwd) self.hilo.user=self.user self.hilo.pwd=self.pwd self.hilo.pause=False def do_spin(self): self.spin.show() return True def update_textarea(self): with open(self.clients_file) as f: textarea=self.builder.get_object('textview1'); self.txtbuff=textarea.get_buffer() #start, end = self.txtbuff.get_bounds() self.txtbuff.set_text(_('Clientes detectados:\n')+f.read()) def on_button2_clicked(self,obj): #entry1=self.builder.get_object('entry1') #entry2=self.builder.get_object('entry2') #texto=entry1.get_text() #entry2.set_text(texto) pass def on_button2_activate(self,obj): #textarea=self.builder.get_object('textview1'); #self.txtbuff=textarea.get_buffer() #buff=textarea.get_buffer() #start, end = self.txtbuff.get_bounds() #self.txtbuff.set_text(self.txtbuff.get_text(start,end,True)+'hola\n') print self.n4dcli.shutdownlist(self.usuario,'Shutdowner',self.user,self.pwd) def on_button3_activate(self,obj): self.window3 = self.builder.get_object('window2') self.window3.show_all() #self.combo = self.builder.get_object('combobox1') #self.combo2 = self.builder.get_object('combobox2') #self.combo.set_model(self.liststore1) #self.combo2.set_model(self.liststore2) #self.combo.set_active(0) #self.combo2.set_active(0) #self.cell = Gtk.CellRendererText() #self.combo.pack_start(self.cell, True) #self.combo.add_attribute(self.cell, 'text', 0) #self.combo2.pack_start(self.cell, True) #self.combo2.add_attribute(self.cell, 'text', 0) self.scroll = self.builder.get_object('scrollbar1') self.adjustment1=Gtk.Adjustment(0,0,25,1,2,2) self.scroll.set_adjustment(self.adjustment1) self.scroll2 = self.builder.get_object('scrollbar2') self.adjustment2=Gtk.Adjustment(0,0,60,5,5,5) self.scroll2.set_adjustment(self.adjustment2) def hide_window(self,widget,event): print('delete called') #textarea=self.builder.get_object('textview1'); #self.buff=textarea.get_buffer() #start, end = self.buff.get_bounds() #self.buff.set_text(self.buff.get_text(start,end,True)+'hola'); widget.hide() #self.window.show() return True def scroll_action(self,obj,event,data): self.etiq = self.builder.get_object('label7') self.selector=int(obj.get_value()); hora=self.etiq.get_text().split(':') if obj==self.scroll: self.hora="%02d:%02d" % (self.selector,int(hora[1])) self.etiq.set_text(self.hora ) else: self.hora="%02d:%02d" % (int(hora[0]),self.selector) self.etiq.set_text(self.hora ) def dayToggle(self,obj): label=str(obj.get_label()) self.days[label] = not self.days[label] def on_button_programado(self,obj): for key in self.daynames: if self.days[key]: print "%s: %s" % (key, self.days[key]) self.etiq = self.builder.get_object('label7') print(self.etiq.get_text()) hora=self.etiq.get_text().split(':') dow="" for key in self.daynames: if self.days[key]: if dow == '': dow=str(self.daynames.index(key)+1) else: dow=dow+','+str(self.daynames.index(key)+1) print(dow) if dow == '': print self.n4dcli.noshutdowncronlist(self.usuario,'Shutdowner',self.user,self.pwd) else: print self.n4dcli.shutdowncronlist(self.usuario,'Shutdowner',self.user,self.pwd,hora[1],hora[0],'*','*',dow) self.window3.hide() def on_list_show(self,obj): #out=self.n4dcli.list_clients(self.usuario,"Shutdowner") #print(out) #if 'PASSWORD ERROR' in out or 'USER DOES NOT EXIST' in out: # print('ERROR') # errorbox = self.builder.get_object('label8') # errorbox.set_text(out) # self.spin.hide() # else: self.update_textarea() # self.window2.show_all() # self.window.hide() def remove_cron_cb(self,obj): print self.n4dcli.noshutdowncronlist(self.usuario,'Shutdowner',self.user,self.pwd) def main(): app = GUI() Gtk.main() if __name__ == "__main__": sys.exit(main())