2 # -*- coding: utf-8 -*-
6 # Copyright © 2013-2019 RebornOS
8 # This file is part of Cnchi.
10 # Cnchi is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 3 of the License, or
13 # (at your option) any later version.
15 # Cnchi is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
20 # The following additional terms are in effect as per Section 7 of the license:
22 # The preservation of all legal notices and author attributions in
23 # the material or in the Appropriate Legal Notices displayed
24 # by works containing it is required.
26 # You should have received a copy of the GNU General Public License
27 # along with Cnchi; If not, see <http://www.gnu.org/licenses/>.
30 """ Desktop screen """
36 gi.require_version('Gtk', '3.0')
37 from gi.repository import Gtk, GdkPixbuf
40 from pages.gtkbasebox import GtkBaseBox
41 import misc.extra as misc
43 CLASS_NAME = "DesktopAsk"
45 class DesktopAsk(GtkBaseBox):
46 """ Class to show the Desktop screen """
48 def __init__(self, params, prev_page="keymap", next_page="features"):
49 super().__init__(self, params, "desktop", prev_page, next_page)
51 data_dir = self.settings.get('data')
52 self.desktops_dir = os.path.join(data_dir, "images", "desktops")
54 self.desktop_info = self.gui.get_object("desktop_info")
56 self.desktop_image = None
57 self.icon_desktop_image = None
60 self.listbox = self.gui.get_object("listbox_desktop")
61 self.listbox.connect("row-selected", self.on_listbox_row_selected)
62 self.listbox.set_selection_mode(Gtk.SelectionMode.BROWSE)
63 self.listbox.set_sort_func(self.listbox_sort_by_name, None)
65 self.desktop_choice = 'deepin'
67 self.enabled_desktops = self.settings.get("desktops")
69 self.set_desktop_list()
71 def translate_ui(self, desktop, set_header=True):
72 """ Translates all ui elements """
73 label = self.gui.get_object("desktop_info")
74 txt = "<span weight='bold'>{0}</span>\n".format(
75 desktop_info.NAMES[desktop])
76 description = desktop_info.DESCRIPTIONS[desktop]
77 txt = txt + _(description)
80 # This sets the desktop's image
81 path = os.path.join(self.desktops_dir, desktop + ".png")
82 if self.desktop_image is None:
83 self.desktop_image = Gtk.Image.new_from_file(path)
84 overlay = self.gui.get_object("image_overlay")
85 overlay.add(self.desktop_image)
87 self.desktop_image.set_from_file(path)
89 # and this sets the icon
90 filename = "desktop-environment-" + desktop.lower() + ".svg"
91 icon_path = os.path.join(
92 desktop_info.DESKTOP_ICONS_PATH, "scalable", filename)
93 icon_exists = os.path.exists(icon_path)
95 if self.icon_desktop_image is None:
97 pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(
99 self.icon_desktop_image = Gtk.Image.new_from_pixbuf(pixbuf)
101 filename = desktop.lower() + ".png"
102 icon_path = os.path.join(
103 desktop_info.DESKTOP_ICONS_PATH, "48x48", filename)
104 icon_exists = os.path.exists(icon_path)
106 self.icon_desktop_image = Gtk.Image.new_from_file(
109 self.icon_desktop_image = Gtk.Image.new_from_icon_name(
113 overlay = self.gui.get_object("image_overlay")
114 overlay.add_overlay(self.icon_desktop_image)
117 pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(
119 self.icon_desktop_image.set_from_pixbuf(pixbuf)
121 filename = desktop.lower() + ".png"
122 icon_path = os.path.join(
123 desktop_info.DESKTOP_ICONS_PATH, "48x48", filename)
124 icon_exists = os.path.exists(icon_path)
126 self.icon_desktop_image.set_from_file(icon_path)
128 self.icon_desktop_image.set_from_icon_name(
129 "image-missing", Gtk.IconSize.DIALOG)
133 txt = _("Choose Your Desktop")
134 self.header.set_subtitle(txt)
136 def prepare(self, direction):
137 """ Prepare screen """
138 self.translate_ui(self.desktop_choice)
141 def set_desktop_list(self):
142 """ Set desktop list in the ListBox """
143 for desktop in sorted(desktop_info.NAMES):
144 if desktop in self.enabled_desktops:
147 filename = "desktop-environment-" + desktop.lower() + ".svg"
148 icon_path = os.path.join(
149 desktop_info.DESKTOP_ICONS_PATH, "scalable", filename)
150 if os.path.exists(icon_path):
151 pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(
153 image = Gtk.Image.new_from_pixbuf(pixbuf)
155 filename = desktop.lower() + ".png"
156 icon_path = os.path.join(
157 desktop_info.DESKTOP_ICONS_PATH, "24x24", filename)
158 if os.path.exists(icon_path):
159 image = Gtk.Image.new_from_file(icon_path)
161 image = Gtk.Image.new_from_icon_name(
163 Gtk.IconSize.LARGE_TOOLBAR)
164 box.pack_start(image, False, False, 2)
167 label.set_markup(desktop_info.NAMES[desktop])
168 box.pack_start(label, False, False, 2)
170 self.listbox.add(box)
172 # Set Gnome as default
173 self.select_default_row(desktop_info.NAMES["gnome"])
176 def listbox_sort_by_name(row1, row2, _user_data):
177 """ Sort function for listbox
178 Returns : < 0 if row1 should be before row2, 0 if they are equal and > 0 otherwise
179 WARNING: IF LAYOUT IS CHANGED IN fill_listbox THEN THIS SHOULD BE
180 CHANGED ACCORDINGLY. """
181 box1 = row1.get_child()
182 label1 = box1.get_children()[1]
184 box2 = row2.get_child()
185 label2 = box2.get_children()[1]
187 text = [label1.get_text(), label2.get_text()]
188 # sorted_text = misc.sort_list(text, self.settings.get("locale"))
189 sorted_text = misc.sort_list(text)
191 # If strings are already well sorted return < 0
192 if text[0] == sorted_text[0]:
195 # Strings must be swaped, return > 0
198 def select_default_row(self, desktop_name):
199 """ Selects default row
200 WARNING: IF LAYOUT IS CHANGED IN desktop.ui THEN THIS SHOULD BE
201 CHANGED ACCORDINGLY. """
202 for listbox_row in self.listbox.get_children():
203 for vbox in listbox_row.get_children():
204 label = vbox.get_children()[1]
205 if desktop_name == label.get_text():
206 self.listbox.select_row(listbox_row)
209 def set_desktop(self, desktop):
210 """ Show desktop info """
211 for key in desktop_info.NAMES:
212 if desktop_info.NAMES[key] == desktop:
213 self.desktop_choice = key
214 self.translate_ui(self.desktop_choice, set_header=False)
217 def on_listbox_row_selected(self, _listbox, listbox_row):
218 """ Someone selected a different row of the listbox
219 WARNING: IF LAYOUT IS CHANGED IN desktop.ui THEN THIS SHOULD BE
220 CHANGED ACCORDINGLY. """
221 if listbox_row is not None:
222 for vbox in listbox_row:
223 label = vbox.get_children()[1]
224 desktop = label.get_text()
225 self.set_desktop(desktop)
227 def store_values(self):
228 """ Store desktop """
229 self.settings.set('desktop', self.desktop_choice.lower())
231 "Cnchi will install RebornOS with the '%s' desktop",
232 self.desktop_choice.lower())
236 def scroll_to_cell(treeview, path):
237 """ Scrolls treeview to show the desired cell """
238 treeview.scroll_to_cell(path)
242 # When testing, no _() is available
245 except NameError as err:
249 if __name__ == '__main__':
250 from test_screen import _, run