#!/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-nextcloud')
_ = gettext.gettext
RSRC_PATH="/usr/share/lliurex-nextcloud/rsrc/"
class NextCloud:
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-nextcloud")
builder.add_from_file(RSRC_PATH+"lliurex-nextcloud-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("NextCloud Setup")
self.main_window.resize(400,815)
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.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.progress_window=builder.get_object("window2")
self.pbar=builder.get_object("progressbar")
'''
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.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("nextcloud")
self.dbuser_entry.set_text("nextcloud_dbu")
self.user_entry.set_text("nextcloud_admin")
#def set_defaults
def set_css_info(self):
self.style_provider=Gtk.CssProvider()
f=Gio.File.new_for_path(RSRC_PATH+"lliurex-nextcloud.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.progress_window.set_name("WHITE_BACKGROUND")
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
GObject.threads_init()
#def init_threads
def get_session_locale(self):
language=os.environ["LANGUAGE"]
lang=os.environ["LANG"]
exec_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@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["DEFAULT_LOCALE"]=self.default_locale
#def get_values
def accept_clicked(self,widget):
self.get_values()
ret,msg=self.parse_values()
if not ret:
msg_text=self.get_msg(msg)
self.msg_label.set_markup(""+msg_text+"")
#self.msg_label.set_markup(""+msg+"")
return
'''
ip="server"
c=xmlrpclib.ServerProxy("https://%s:9779"%ip)
n4d_key=self.get_n4d_key()
status,ret=c.initialize_owncloud(n4d_key,"LliurexOwncloud",self.template)
if status:
os.system("zero-center set-configured lliurex-owncloud")
self.msg_label.set_markup(""+"Initialization successful"+"")
self.accept_button.set_sensitive(False)
else:
self.msg_label.set_markup(""+ret+"")
os.system("zero-center set-failed lliurex-owncloud")
'''
self.executing=True
#self.progress_window.show()
self.options_box.set_sensitive(False)
self.progressbar.show()
self.msg_label.show()
self.msg_label.set_text(_("Initializing NextCloud. Wait a moment..."))
self.ret_msg=""
self.ret_status=False
self.init_threads()
'''
t=threading.Thread(target=self.execute,args=())
t.daemon=True
t.start()
'''
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 lliurex-nextcloud")
msg_text=self.get_msg("14")
self.msg_label.set_text(msg_text)
'''
self.msg_label.set_markup(""+_("Initialization successful")+"")
self.accept_button.set_sensitive(False)
'''
else:
msg_text=self.get_msg(self.ret_msg)
self.msg_label.set_markup(""+msg_text+"")
'''
self.msg_label.set_markup(""+self.ret_msg+"")
'''
os.system("zero-center set-failed lliurex-nextcloud")
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()
self.ret_status,self.ret_msg=c.initialize_nextcloud(n4d_key,"LliurexNextcloud",self.template)
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,"11"]
if self.template["DB_PWD"]!=self.template["DB_PWD2"]:
return [False,"12"]
if self.template["ADMIN_PWD"]!=self.template["ADMIN_PWD2"]:
return [False,"13"]
return [True,""]
#def parse_values
def get_msg(self,code):
if code=="1":
msg=_("Error loading or processing template")
elif code=="2":
msg=_("Unable to get NextCloud version")
elif code=="3":
msg=_("Error initializing mysql service")
elif code=="4":
msg=_("Error creating user to database")
elif code=="5":
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 easy-site")
elif code=="10":
msg=_("Error enabling NextCloud cname")
elif code=="?":
msg=_("Error in NextCloud config process")
elif code=="11":
msg=_("There are empty fields to be completed")
elif code=="12":
msg=_("DB Passwords do not match")
elif code=="13":
msg=_("Administrator passwords do not match")
elif code=="14":
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=Inicialitzaci%C3%B3-de-Nextcloud'
else:
exec_lang="LANG=es_ES.UTF-8"
cmd=exec_lang+' xdg-open https://wiki.edu.gva.es/lliurex/tiki-index.php?page=Inicializaci%C3%B3n-de-Nextcloud'
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
#class nextcloud
if __name__=="__main__":
o=NextCloud()