2016-12-04 18:09:15 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2016-12-11 00:05:32 +01:00
|
|
|
import gettext
|
2016-12-08 18:11:08 +01:00
|
|
|
import gi
|
|
|
|
import json
|
|
|
|
import locale
|
2016-12-04 18:09:15 +01:00
|
|
|
import os
|
2017-05-25 17:05:49 +02:00
|
|
|
import subprocess
|
2016-12-06 18:54:02 +01:00
|
|
|
import sys
|
2016-12-05 18:41:28 +01:00
|
|
|
import webbrowser
|
2016-12-04 18:09:15 +01:00
|
|
|
gi.require_version("Gtk", "3.0")
|
2016-12-11 18:12:01 +01:00
|
|
|
from gi.repository import Gtk, GdkPixbuf
|
2016-12-04 18:09:15 +01:00
|
|
|
|
2016-12-21 15:35:54 +01:00
|
|
|
|
2017-05-25 16:13:44 +02:00
|
|
|
class Hello():
|
|
|
|
"""Hello"""
|
2017-02-04 11:00:52 +01:00
|
|
|
|
2016-12-04 18:09:15 +01:00
|
|
|
def __init__(self):
|
2016-12-05 21:49:29 +01:00
|
|
|
self.app = "manjaro-hello"
|
2017-05-25 16:13:44 +02:00
|
|
|
self.dev = "--dev" in sys.argv
|
|
|
|
|
|
|
|
# Load preferences
|
|
|
|
if self.dev:
|
|
|
|
self.preferences = read_json("data/preferences.json")
|
|
|
|
self.preferences["data_path"] = "data/"
|
|
|
|
self.preferences["desktop_path"] = os.getcwd() + "/{}.desktop".format(self.app)
|
|
|
|
self.preferences["locale_path"] = "locale/"
|
|
|
|
self.preferences["ui_path"] = "ui/{}.glade".format(self.app)
|
2016-12-10 17:34:29 +01:00
|
|
|
else:
|
2017-05-25 16:13:44 +02:00
|
|
|
self.preferences = read_json("/usr/share/{}/data/preferences.json".format(self.app))
|
2017-05-07 18:33:51 +02:00
|
|
|
|
2017-05-25 16:13:44 +02:00
|
|
|
# Get saved infos
|
|
|
|
self.save = read_json(self.preferences["save_path"])
|
|
|
|
if not self.save:
|
|
|
|
self.save = {"locale": None}
|
2016-12-18 00:05:56 +01:00
|
|
|
|
2016-12-11 00:05:32 +01:00
|
|
|
# Init window
|
2017-05-25 16:13:44 +02:00
|
|
|
self.builder = Gtk.Builder.new_from_file(self.preferences["ui_path"])
|
2016-12-11 00:05:32 +01:00
|
|
|
self.builder.connect_signals(self)
|
|
|
|
self.window = self.builder.get_object("window")
|
2017-05-09 20:00:16 +02:00
|
|
|
|
|
|
|
# Subtitle of headerbar
|
2017-06-16 16:30:08 +02:00
|
|
|
self.builder.get_object("headerbar").props.subtitle = ' '.join(get_lsb_infos())
|
2016-12-11 00:05:32 +01:00
|
|
|
|
2017-05-25 16:59:03 +02:00
|
|
|
# Load images
|
2017-05-25 16:13:44 +02:00
|
|
|
if os.path.isfile(self.preferences["logo_path"]):
|
|
|
|
logo = GdkPixbuf.Pixbuf.new_from_file(self.preferences["logo_path"])
|
2017-05-07 17:59:17 +02:00
|
|
|
self.window.set_icon(logo)
|
2017-05-07 18:41:28 +02:00
|
|
|
self.builder.get_object("distriblogo").set_from_pixbuf(logo)
|
2017-05-07 17:59:17 +02:00
|
|
|
self.builder.get_object("aboutdialog").set_logo(logo)
|
2016-12-11 18:12:01 +01:00
|
|
|
|
2017-05-25 16:59:03 +02:00
|
|
|
for btn in self.builder.get_object("social").get_children():
|
|
|
|
icon_path = self.preferences["data_path"] + "img/" + btn.get_name() + ".png"
|
|
|
|
self.builder.get_object(btn.get_name()).set_from_file(icon_path)
|
|
|
|
|
|
|
|
for widget in self.builder.get_object("homepage").get_children():
|
|
|
|
if isinstance(widget, Gtk.Button) and widget.get_image_position() is Gtk.PositionType.RIGHT:
|
2017-05-25 17:03:35 +02:00
|
|
|
img = Gtk.Image.new_from_file(
|
|
|
|
self.preferences["data_path"] + "img/external-link.png")
|
2017-05-25 16:59:03 +02:00
|
|
|
img.set_margin_left(2)
|
|
|
|
widget.set_image(img)
|
|
|
|
|
2016-12-26 18:12:17 +01:00
|
|
|
# Create pages
|
2017-05-25 16:34:18 +02:00
|
|
|
self.pages = os.listdir("{}/pages/{}".format(self.preferences["data_path"],
|
2017-05-25 17:03:35 +02:00
|
|
|
self.preferences["default_locale"]))
|
2016-12-26 18:12:17 +01:00
|
|
|
for page in self.pages:
|
|
|
|
scrolled_window = Gtk.ScrolledWindow()
|
2017-02-04 16:18:08 +01:00
|
|
|
viewport = Gtk.Viewport(border_width=10)
|
2016-12-26 18:12:17 +01:00
|
|
|
label = Gtk.Label(wrap=True)
|
|
|
|
viewport.add(label)
|
|
|
|
scrolled_window.add(viewport)
|
2016-12-26 18:19:16 +01:00
|
|
|
scrolled_window.show_all()
|
2016-12-26 18:12:17 +01:00
|
|
|
self.builder.get_object("stack").add_named(scrolled_window, page + "page")
|
|
|
|
|
2016-12-09 17:28:22 +01:00
|
|
|
# Init translation
|
2016-12-11 00:05:32 +01:00
|
|
|
self.default_texts = {}
|
2017-05-25 16:13:44 +02:00
|
|
|
gettext.bindtextdomain(self.app, self.preferences["locale_path"])
|
2016-12-11 00:05:32 +01:00
|
|
|
gettext.textdomain(self.app)
|
2016-12-28 14:53:32 +01:00
|
|
|
self.builder.get_object("languages").set_active_id(self.get_best_locale())
|
2016-12-09 17:28:22 +01:00
|
|
|
|
2016-12-11 00:37:11 +01:00
|
|
|
# Set autostart switcher state
|
2017-05-25 16:13:44 +02:00
|
|
|
self.autostart = os.path.isfile(self.preferences["autostart_path"])
|
2016-12-18 15:34:43 +01:00
|
|
|
self.builder.get_object("autostart").set_active(self.autostart)
|
2016-12-11 00:37:11 +01:00
|
|
|
|
2016-12-08 17:39:52 +01:00
|
|
|
# Live systems
|
2017-05-25 16:13:44 +02:00
|
|
|
if os.path.exists(self.preferences["live_path"]) and os.path.isfile(self.preferences["installer_path"]):
|
2016-12-22 22:32:30 +01:00
|
|
|
self.builder.get_object("installlabel").set_visible(True)
|
2016-12-26 17:29:24 +01:00
|
|
|
self.builder.get_object("install").set_visible(True)
|
2016-12-08 17:39:52 +01:00
|
|
|
|
2016-12-26 18:19:16 +01:00
|
|
|
self.window.show()
|
2016-12-04 18:09:15 +01:00
|
|
|
|
2016-12-20 00:17:40 +01:00
|
|
|
def get_best_locale(self):
|
2016-12-20 00:29:03 +01:00
|
|
|
"""Choose best locale, based on user's preferences.
|
|
|
|
:return: locale to use
|
|
|
|
:rtype: str
|
|
|
|
"""
|
2017-05-25 16:13:44 +02:00
|
|
|
path = self.preferences["locale_path"] + "{}/LC_MESSAGES/" + self.app + ".mo"
|
2017-05-25 21:08:35 +02:00
|
|
|
if os.path.isfile(path.format(self.save["locale"])):
|
2017-05-25 16:13:44 +02:00
|
|
|
return self.save["locale"]
|
2017-05-25 21:08:35 +02:00
|
|
|
elif self.save["locale"] == self.preferences["default_locale"]:
|
|
|
|
return self.preferences["default_locale"]
|
2016-12-20 00:17:40 +01:00
|
|
|
else:
|
2016-12-28 15:58:14 +01:00
|
|
|
sys_locale = locale.getdefaultlocale()[0]
|
2016-12-20 00:17:40 +01:00
|
|
|
# If user's locale is supported
|
2016-12-28 15:58:14 +01:00
|
|
|
if os.path.isfile(path.format(sys_locale)):
|
2017-02-05 11:15:04 +01:00
|
|
|
if "_" in sys_locale:
|
|
|
|
return sys_locale.replace("_", "-")
|
|
|
|
else:
|
|
|
|
return sys_locale
|
2016-12-20 00:17:40 +01:00
|
|
|
# If two first letters of user's locale is supported (ex: en_US -> en)
|
2016-12-28 15:58:14 +01:00
|
|
|
elif os.path.isfile(path.format(sys_locale[:2])):
|
|
|
|
return sys_locale[:2]
|
2016-12-20 00:17:40 +01:00
|
|
|
else:
|
2017-05-25 16:13:44 +02:00
|
|
|
return self.preferences["default_locale"]
|
2016-12-20 00:17:40 +01:00
|
|
|
|
2016-12-11 00:38:36 +01:00
|
|
|
def set_locale(self, locale):
|
2016-12-17 23:19:05 +01:00
|
|
|
"""Set locale of ui and pages.
|
|
|
|
:param locale: locale to use
|
|
|
|
:type locale: str
|
|
|
|
"""
|
2016-12-21 18:32:16 +01:00
|
|
|
try:
|
2017-05-25 17:03:35 +02:00
|
|
|
tr = gettext.translation(self.app, self.preferences[
|
|
|
|
"locale_path"], [locale], fallback=True)
|
2016-12-21 18:32:16 +01:00
|
|
|
tr.install()
|
|
|
|
except OSError:
|
2016-12-21 18:39:15 +01:00
|
|
|
return
|
2016-12-11 00:05:32 +01:00
|
|
|
|
2017-05-25 16:13:44 +02:00
|
|
|
self.save["locale"] = locale
|
2017-03-19 21:29:53 +01:00
|
|
|
|
2016-12-17 23:07:03 +01:00
|
|
|
# Dirty code to fix an issue with gettext that can't translate strings from glade files
|
|
|
|
# Redfining all translatables strings
|
2016-12-11 00:05:32 +01:00
|
|
|
# TODO: Find a better solution
|
|
|
|
elts = {
|
2016-12-26 21:15:04 +01:00
|
|
|
"comments": {
|
|
|
|
"aboutdialog"
|
|
|
|
},
|
|
|
|
"label": {
|
|
|
|
"autostartlabel",
|
2017-01-08 22:20:13 +01:00
|
|
|
"development",
|
2016-12-26 21:15:04 +01:00
|
|
|
"chat",
|
|
|
|
"donate",
|
|
|
|
"firstcategory",
|
2017-05-25 16:14:54 +02:00
|
|
|
"forum",
|
2016-12-26 21:15:04 +01:00
|
|
|
"install",
|
|
|
|
"installlabel",
|
|
|
|
"involved",
|
|
|
|
"mailling",
|
|
|
|
"readme",
|
|
|
|
"release",
|
|
|
|
"secondcategory",
|
|
|
|
"thirdcategory",
|
|
|
|
"welcomelabel",
|
|
|
|
"welcometitle",
|
|
|
|
"wiki"
|
|
|
|
},
|
|
|
|
"tooltip_text": {
|
|
|
|
"about",
|
|
|
|
"home"
|
|
|
|
}
|
2016-12-11 00:05:32 +01:00
|
|
|
}
|
2016-12-26 21:15:04 +01:00
|
|
|
for method in elts:
|
|
|
|
for elt in elts[method]:
|
|
|
|
if elt not in self.default_texts:
|
2017-05-25 17:03:35 +02:00
|
|
|
self.default_texts[elt] = getattr(
|
|
|
|
self.builder.get_object(elt), "get_" + method)()
|
2016-12-26 21:15:04 +01:00
|
|
|
getattr(self.builder.get_object(elt), "set_" + method)(_(self.default_texts[elt]))
|
2016-12-11 00:05:32 +01:00
|
|
|
|
2016-12-26 18:12:17 +01:00
|
|
|
# Change content of pages
|
|
|
|
for page in self.pages:
|
|
|
|
child = self.builder.get_object("stack").get_child_by_name(page + "page")
|
|
|
|
label = child.get_children()[0].get_children()[0]
|
|
|
|
label.set_markup(self.get_page(page))
|
2016-12-17 00:07:49 +01:00
|
|
|
|
2016-12-26 15:50:54 +01:00
|
|
|
def set_autostart(self, autostart):
|
2016-12-17 23:19:05 +01:00
|
|
|
"""Set state of autostart.
|
|
|
|
:param autostart: wanted autostart state
|
|
|
|
:type autostart: bool
|
|
|
|
"""
|
2016-12-17 11:55:00 +01:00
|
|
|
try:
|
2017-05-25 17:24:31 +02:00
|
|
|
if autostart and not os.path.isfile(fix_path(self.preferences["autostart_path"])):
|
|
|
|
os.symlink(self.preferences["desktop_path"],
|
|
|
|
fix_path(self.preferences["autostart_path"]))
|
|
|
|
elif not autostart and os.path.isfile(fix_path(self.preferences["autostart_path"])):
|
|
|
|
os.unlink(fix_path(self.preferences["autostart_path"]))
|
2016-12-26 15:50:54 +01:00
|
|
|
# Specific to i3
|
2017-05-25 17:24:31 +02:00
|
|
|
i3_config = fix_path("~/.i3/config")
|
2016-12-26 15:50:54 +01:00
|
|
|
if os.path.isfile(i3_config):
|
2016-12-26 20:27:16 +01:00
|
|
|
i3_autostart = "exec --no-startup-id " + self.app
|
2016-12-26 15:50:54 +01:00
|
|
|
with open(i3_config, "r+") as f:
|
|
|
|
content = f.read()
|
|
|
|
f.seek(0)
|
|
|
|
if autostart:
|
|
|
|
f.write(content.replace("#" + i3_autostart, i3_autostart))
|
|
|
|
else:
|
|
|
|
f.write(content.replace(i3_autostart, "#" + i3_autostart))
|
|
|
|
f.truncate()
|
2016-12-28 23:20:36 +01:00
|
|
|
self.autostart = autostart
|
2017-02-04 11:00:52 +01:00
|
|
|
except OSError as error:
|
|
|
|
print(error)
|
2016-12-04 18:09:15 +01:00
|
|
|
|
2016-12-26 17:30:23 +01:00
|
|
|
def get_page(self, name):
|
2016-12-17 23:19:05 +01:00
|
|
|
"""Read page according to language.
|
|
|
|
:param name: name of page (filename)
|
|
|
|
:type name: str
|
|
|
|
:return: text to load
|
|
|
|
:rtype: str
|
|
|
|
"""
|
2017-05-25 16:13:44 +02:00
|
|
|
filename = self.preferences["data_path"] + "pages/{}/{}".format(self.save["locale"], name)
|
2016-12-04 20:40:34 +01:00
|
|
|
if not os.path.isfile(filename):
|
2017-05-25 17:03:35 +02:00
|
|
|
filename = self.preferences["data_path"] + \
|
|
|
|
"pages/{}/{}".format(self.preferences["default_locale"], name)
|
2016-12-04 18:09:15 +01:00
|
|
|
try:
|
|
|
|
with open(filename, "r") as f:
|
|
|
|
return f.read()
|
2016-12-21 18:34:34 +01:00
|
|
|
except OSError:
|
2016-12-17 12:15:27 +01:00
|
|
|
return _("Can't load page.")
|
2016-12-04 18:09:15 +01:00
|
|
|
|
|
|
|
# Handlers
|
2016-12-09 17:28:22 +01:00
|
|
|
def on_languages_changed(self, combobox):
|
2016-12-17 23:07:03 +01:00
|
|
|
"""Event for selected language."""
|
2016-12-28 14:53:32 +01:00
|
|
|
self.set_locale(combobox.get_active_id())
|
2016-12-09 17:28:22 +01:00
|
|
|
|
2016-12-17 12:21:15 +01:00
|
|
|
def on_action_clicked(self, action, _=None):
|
2016-12-17 23:07:03 +01:00
|
|
|
"""Event for differents actions."""
|
2016-12-17 12:21:15 +01:00
|
|
|
name = action.get_name()
|
2016-12-26 17:29:24 +01:00
|
|
|
if name == "install":
|
2017-01-29 04:57:10 +01:00
|
|
|
subprocess.Popen(["pkexec", "calamares"])
|
2016-12-17 12:21:15 +01:00
|
|
|
elif name == "autostart":
|
2016-12-28 16:04:09 +01:00
|
|
|
self.set_autostart(action.get_active())
|
2016-12-17 12:08:40 +01:00
|
|
|
elif name == "about":
|
|
|
|
dialog = self.builder.get_object("aboutdialog")
|
|
|
|
dialog.run()
|
|
|
|
dialog.hide()
|
2016-12-04 18:09:15 +01:00
|
|
|
|
2016-12-17 23:55:19 +01:00
|
|
|
def on_btn_clicked(self, btn):
|
|
|
|
"""Event for clicked button."""
|
2016-12-26 19:25:39 +01:00
|
|
|
name = btn.get_name()
|
2016-12-26 21:05:38 +01:00
|
|
|
self.builder.get_object("home").set_sensitive(not name == "home")
|
2016-12-26 19:25:39 +01:00
|
|
|
self.builder.get_object("stack").set_visible_child_name(name + "page")
|
2016-12-17 23:55:19 +01:00
|
|
|
|
2016-12-11 00:58:59 +01:00
|
|
|
def on_link_clicked(self, link, _=None):
|
2016-12-17 23:07:03 +01:00
|
|
|
"""Event for clicked link."""
|
2017-05-25 16:28:59 +02:00
|
|
|
webbrowser.open_new_tab(self.preferences["urls"][link.get_name()])
|
2016-12-05 18:41:28 +01:00
|
|
|
|
2016-12-04 18:09:15 +01:00
|
|
|
def on_delete_window(self, *args):
|
2016-12-17 23:07:03 +01:00
|
|
|
"""Event to quit app."""
|
2017-05-25 16:13:44 +02:00
|
|
|
write_json(self.preferences["save_path"], self.save)
|
2016-12-04 18:09:15 +01:00
|
|
|
Gtk.main_quit(*args)
|
|
|
|
|
2016-12-21 15:35:54 +01:00
|
|
|
|
2017-05-25 16:13:44 +02:00
|
|
|
def fix_path(path):
|
2017-05-25 20:45:00 +02:00
|
|
|
"""Make good paths.
|
|
|
|
:param path: path to fix
|
|
|
|
:type path: str
|
|
|
|
:return: fixed path
|
|
|
|
:rtype: str
|
|
|
|
"""
|
2017-05-25 16:13:44 +02:00
|
|
|
if "~" in path:
|
|
|
|
path = path.replace("~", os.path.expanduser("~"))
|
|
|
|
return path
|
|
|
|
|
|
|
|
|
2016-12-18 00:05:56 +01:00
|
|
|
def read_json(path):
|
2017-02-04 11:00:52 +01:00
|
|
|
"""Read content of a json file.
|
2017-05-25 20:45:00 +02:00
|
|
|
:param path: path to read
|
2017-05-21 20:38:21 +02:00
|
|
|
:type path: str
|
2017-02-04 11:00:52 +01:00
|
|
|
:return: json content
|
|
|
|
:rtype: str
|
|
|
|
"""
|
2017-05-25 16:13:44 +02:00
|
|
|
path = fix_path(path)
|
2016-12-18 00:05:56 +01:00
|
|
|
try:
|
|
|
|
with open(path, "r") as f:
|
|
|
|
return json.load(f)
|
2016-12-21 18:34:34 +01:00
|
|
|
except OSError:
|
2016-12-18 00:05:56 +01:00
|
|
|
return None
|
|
|
|
|
2017-05-25 16:13:44 +02:00
|
|
|
|
2017-05-21 20:38:21 +02:00
|
|
|
def write_json(path, content):
|
|
|
|
"""Write content in a json file.
|
2017-05-25 20:45:00 +02:00
|
|
|
:param path: path to write
|
2017-05-21 20:38:21 +02:00
|
|
|
:type path: str
|
|
|
|
:param content: content to write
|
|
|
|
:type path: str
|
|
|
|
"""
|
2017-05-25 16:13:44 +02:00
|
|
|
path = fix_path(path)
|
2017-05-21 20:38:21 +02:00
|
|
|
try:
|
|
|
|
with open(path, "w") as f:
|
|
|
|
json.dump(content, f)
|
|
|
|
except OSError as error:
|
|
|
|
print(error)
|
2016-12-21 15:35:54 +01:00
|
|
|
|
2017-06-16 16:30:08 +02:00
|
|
|
def get_lsb_infos():
|
|
|
|
"""Read informations from the lsb-release file.
|
|
|
|
:return: args from lsb-release file
|
|
|
|
:rtype: dict"""
|
|
|
|
lsb = {}
|
|
|
|
try:
|
|
|
|
with open("/etc/lsb-release") as f:
|
|
|
|
for line in f:
|
|
|
|
if "=" in line:
|
|
|
|
var, arg = line.rstrip().split("=")
|
|
|
|
if var.startswith("DISTRIB_"):
|
|
|
|
var = var[8:]
|
|
|
|
if arg.startswith("\"") and arg.endswith("\""):
|
|
|
|
arg = arg[1:-1]
|
|
|
|
if arg:
|
|
|
|
lsb[var] = arg
|
|
|
|
except OSError as error:
|
|
|
|
print(error)
|
|
|
|
return lsb["CODENAME"], lsb["RELEASE"]
|
|
|
|
|
2017-05-25 16:13:44 +02:00
|
|
|
|
2016-12-05 22:18:23 +01:00
|
|
|
if __name__ == "__main__":
|
2017-05-25 16:13:44 +02:00
|
|
|
Hello()
|
2016-12-05 22:18:23 +01:00
|
|
|
Gtk.main()
|