#!/usr/bin/env python3
# -*- coding: utf-8 -*
import os
import xmlrpc.client as client
import gi
gi.require_version('Gtk','3.0')
gi.require_version('PangoCairo','1.0')
from gi.repository import Gtk, Gdk, GObject, GLib, Gio, PangoCairo, Pango
import threading
import pwd
import ssl
import signal
signal.signal(signal.SIGINT, signal.SIG_DFL)
import gettext
gettext.textdomain('lliurex-news-setup')
_ = gettext.gettext
RSRC_PATH="/usr/share/lliurex-news-setup/rsrc/"
class News:
def __init__(self):
self.executing=False
user=pwd.getpwuid(int(os.environ["PKEXEC_UID"])).pw_name
home_path="/home/"+user+"/.config"
os.environ["XDG_CONFIG_DIRS"]="/etc/xdg/xdg-/usr/share/xsessions/plasma:/etc/xdg:"+home_path
self.start_gui()
#def __ini__
def start_gui(self):
builder=Gtk.Builder()
builder.set_translation_domain("lliurex-news-setup")
builder.add_from_file(RSRC_PATH+"lliurex-news-setup.ui")
max_height=Gdk.Display.get_default().get_monitor(0).get_geometry().height
width=Gdk.Display.get_default().get_monitor(0).get_geometry().width
self.stack_window=Gtk.Stack()
self.stack_window.set_transition_type(Gtk.StackTransitionType.SLIDE_LEFT)
self.stack_window.set_margin_top(0)
self.stack_window.set_margin_bottom(0)
self.main_window=builder.get_object("main_window")
self.main_window.set_title("News Server Setup")
self.main_window.resize(400,850)
g=Gdk.Geometry()
g.min_width=400
g.max_width=400
g.min_height=200
g.max_height=max_height
self.main_window.set_geometry_hints(None,g,Gdk.WindowHints.MAX_SIZE)
self.main_box=builder.get_object("main_box")
self.banner_box=builder.get_object("banner_box")
self.options_box=builder.get_object("options_box")
self.accept_button=builder.get_object("accept_button")
self.help_button=builder.get_object("help_button")
self.remove_button=builder.get_object("remove_button")
self.dbname_entry=builder.get_object("dbname_entry")
self.dbuser_entry=builder.get_object("dbuser_entry")
self.dbpwd_entry=builder.get_object("dbpwd_entry")
self.dbpwd_entry2=builder.get_object("dbpwd_entry2")
self.user_entry=builder.get_object("user_entry")
self.pwd_entry=builder.get_object("pwd_entry")
self.pwd_entry2=builder.get_object("pwd_entry2")
self.entry_list=[self.dbname_entry,self.dbuser_entry,self.dbpwd_entry,self.dbpwd_entry2,self.user_entry,self.pwd_entry,self.pwd_entry2]
self.progressbar=builder.get_object("progressbar")
self.msg_label=builder.get_object("msg_label")
self.stack_window.add_titled(self.options_box, "optionsBox", "Options Box")
self.stack_window.show_all()
self.main_box.pack_start(self.stack_window,True,True,0)
self.progressbar.hide()
self.main_window.connect("destroy",self.exit)
self.accept_button.connect("clicked",self.accept_clicked)
self.help_button.connect("clicked",self.show_help)
self.remove_button.connect("clicked",self.remove_clicked)
database_label=builder.get_object("database_label")
database_separator=builder.get_object("database_separator")
dbname_label=builder.get_object("dbname_label")
dbuser_label=builder.get_object("dbuser_label")
dbpwd_label=builder.get_object("dbpwd_label")
dbpwdret_label=builder.get_object("dbpwdret_label")
username_label=builder.get_object("username_label")
username_separator=builder.get_object("username_separator")
user_label=builder.get_object("user_label")
userpwd_label=builder.get_object("userpwd_label")
userpwdret_label=builder.get_object("userpwdret_label")
self.pbar_label=builder.get_object("pbar_label")
self.blocks_list=[database_label,username_label]
self.separators=[database_separator,username_separator]
self.labels_list=[dbname_label,dbuser_label,dbpwd_label,dbpwdret_label,user_label,userpwd_label,userpwdret_label]
self.set_defaults()
self.set_css_info()
self.init_threads()
self.get_session_locale()
self.main_window.show()
self.stack_window.set_transition_type(Gtk.StackTransitionType.NONE)
self.stack_window.set_visible_child_name("optionsBox")
Gtk.main()
#def start_gui
def set_defaults(self):
self.dbname_entry.set_text("news")
self.dbuser_entry.set_text("news_dbu")
self.user_entry.set_text("news_admin")
#def set_defaults
def set_css_info(self):
self.style_provider=Gtk.CssProvider()
f=Gio.File.new_for_path(RSRC_PATH+"lliurex-news-setup.css")
self.style_provider.load_from_file(f)
Gtk.StyleContext.add_provider_for_screen(Gdk.Screen.get_default(),self.style_provider,Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)
self.main_window.set_name("WHITE_BACKGROUND")
self.banner_box.set_name("BANNER-BOX")
self.msg_label.set_name("MSG-LABEL")
self.progressbar.set_name("HEADER-LABEL")
for entry in self.blocks_list:
entry.set_name("HEADER-LABEL")
for entry in self.separators:
entry.set_name("HEADER-SEPARATOR")
for entry in self.labels_list:
entry.set_name("INFO-ENTRY")
for entry in self.entry_list:
entry.set_name("CUSTOM-ENTRY")
#def set_css_info
def init_threads(self):
self.setup_t=threading.Thread(target=self.execute,args=())
self.setup_t.daemon=True
self.open_help_t=threading.Thread(target=self.open_help)
self.open_help_t.daemon=True
self.remove_news_t=threading.Thread(target=self.remove_news)
self.remove_news_t.daemon=True
#GObject.threads_init()
#def init_threads
def get_session_locale(self):
language=os.environ["LANGUAGE"]
lang=os.environ["LANG"]
app_lang=""
if language=="":
app_lang=lang
else:
language=language.split(":")[0]
app_lang=language
if 'valencia' in app_lang:
self.default_locale="ca-es-valencia"
elif 'es' in lang:
self.default_locale="es"
else:
self.default_locale="en"
#def get_session_locale
def exit(self,widget):
Gtk.main_quit()
#def exit
def get_values(self):
self.template={}
self.template["DB_NAME"]=self.dbname_entry.get_text()
self.template["DB_USER"]=self.dbuser_entry.get_text()
self.template["DB_PWD"]=self.dbpwd_entry.get_text()
self.template["DB_PWD2"]=self.dbpwd_entry2.get_text()
self.template["ADMIN_USER"]=self.user_entry.get_text()
self.template["ADMIN_PWD"]=self.pwd_entry.get_text()
self.template["ADMIN_PWD2"]=self.pwd_entry2.get_text()
self.template["ADMIN_EMAIL"]=self.user_entry.get_text()+"@llxnews.com"
self.template["DEFAULT_LOCALE"]=self.default_locale
#def get_values
def accept_clicked(self,widget):
exec_setup=True
message=_("The News server is already configured.\nIf you continue the current content will be removed.\nDo you want to reconfigure the News server?")
self.msg_label.set_text("")
self.get_values()
ret,msg=self.parse_values()
if not ret:
msg_text=self.get_msg(msg)
self.msg_label.set_markup(""+msg_text+"")
return
if os.path.exists("/var/www/news/.ghost-cli"):
dialog = Gtk.MessageDialog(parent=None,flags=0,message_type=Gtk.MessageType.WARNING,buttons=Gtk.ButtonsType.YES_NO, text="News Server Configuration")
dialog.format_secondary_text(message)
response = dialog.run()
if response==Gtk.ResponseType.NO:
exec_setup=False
dialog.destroy()
if exec_setup:
self.executing=True
self.options_box.set_sensitive(False)
self.progressbar.show()
self.msg_label.show()
self.msg_label.set_text(_("Initializing News. Wait a moment..."))
self.ret_msg=""
self.ret_status=False
self.init_threads()
#t=threading.Thread(target=self.execute,args=())
#t.daemon=True
self.setup_t.start()
GLib.timeout_add(50,self.pulsate)
#def accept_clicked
def pulsate(self):
if self.executing:
self.progressbar.pulse()
else:
self.options_box.set_sensitive(True)
if self.ret_status:
os.system("zero-center set-configured zero-lliurex-news-setup")
msg_text=self.get_msg("16")
self.msg_label.set_text(msg_text)
else:
msg_text=self.get_msg(self.ret_msg)
self.msg_label.set_markup(""+msg_text+"")
os.system("zero-center set-failed zero-lliurex-news-setup")
return self.executing
#def pulsate
def execute(self):
context=ssl._create_unverified_context()
ip="server"
c=client.ServerProxy("https://%s:9779"%ip,context=context,allow_none=True)
n4d_key=self.get_n4d_key()
ret=c.initialize_news(n4d_key,"LliurexNews",self.template).get('return',None)
self.ret_status=ret[0]
self.ret_msg=ret[1]
self.executing=False
self.progressbar.hide()
return False
#def execute
def get_n4d_key(self):
try:
f=open("/etc/n4d/key")
key=f.readline().strip("\n")
f.close()
return key
except:
return None
#def get_n4d_key
def parse_values(self):
for item in self.template:
if self.template[item]=="" or self.template[item]==None:
return [False,"13"]
if self.template["DB_PWD"]!=self.template["DB_PWD2"]:
return [False,"14"]
if self.template["ADMIN_PWD"]!=self.template["ADMIN_PWD2"]:
return [False,"15"]
return [True,""]
#def parse_values
def get_msg(self,code):
msg=""
if code=="1":
msg=_("Error loading or processing template")
elif code=="2":
msg=_("Error initializing mysql service")
elif code=="3":
msg=_("Error creating user to database")
elif code=="4":
msg=_("Error creating database")
elif code=="6":
msg=_("Error cleaning old files")
elif code=="7":
msg=_("Error copyng news files")
elif code=="8":
msg=_("Error processing config file")
elif code=="9":
msg=_("Error enabling apache2 config")
elif code=="10":
msg=_("Error enabling news-server docker")
elif code=="11":
msg=_("Error enabling easy-site")
elif code=="12":
msg=_("Error enabling news cname")
elif code=="?":
msg=_("Error in News config process")
elif code=="13":
msg=_("There are empty fields to be completed")
elif code=="14":
msg=_("DB Passwords do not match")
elif code=="15":
msg=_("Administrator passwords do not match")
elif code=="16":
msg=_("Initialization successful")
return msg
#def get_msg
def show_help(self,widget):
run_pkexec=False
if "PKEXEC_UID" in os.environ:
run_pkexec=True
if 'valencia' in self.default_locale:
exec_lang="LANG=ca_ES.UTF-8@valencia"
cmd=exec_lang+' xdg-open https://wiki.edu.gva.es/lliurex/tiki-index.php?page=LliureX-News:-Gestiona-i-publica-les-not%C3%ADcies-del-centre'
else:
exec_lang="LANG=es_ES.UTF-8"
cmd='xdg-open https://wiki.edu.gva.es/lliurex/tiki-index.php?page=LliureX-News:-Gestiona-y-publica-las-noticias-del-centro'
if not run_pkexec:
self.help_cmd="su -c '%s' $USER" %cmd
else:
user=pwd.getpwuid(int(os.environ["PKEXEC_UID"])).pw_name
self.help_cmd="su -c '" +cmd+ "' "+ user
self.init_threads()
self.open_help_t.start()
#def show_help
def open_help(self):
os.system(self.help_cmd)
#def open_help
def remove_clicked(self,widget):
self.remove_cmd="epi-gtk /usr/share/zero-lliurex-news-installer/news-installer.epi"
self.init_threads()
self.remove_news_t.start()
self.exit(widget)
#def remove_clicked
def remove_news(self):
os.system(self.remove_cmd)
#def remove_news
#class news
if __name__=="__main__":
o=News()