OSDN Git Service

Initial commit
[rebornos/cnchi-gnome-osdn.git] / Cnchi / desktop.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 #
4 #  desktop.py
5 #
6 #  Copyright © 2013-2019 RebornOS
7 #
8 #  This file is part of Cnchi.
9 #
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.
14 #
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.
19 #
20 #  The following additional terms are in effect as per Section 7 of the license:
21 #
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.
25 #
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/>.
28
29
30 """ Desktop screen """
31
32 import os
33 import logging
34
35 import gi
36 gi.require_version('Gtk', '3.0')
37 from gi.repository import Gtk, GdkPixbuf
38
39 import desktop_info
40 from pages.gtkbasebox import GtkBaseBox
41 import misc.extra as misc
42
43 CLASS_NAME = "DesktopAsk"
44
45 class DesktopAsk(GtkBaseBox):
46     """ Class to show the Desktop screen """
47
48     def __init__(self, params, prev_page="keymap", next_page="features"):
49         super().__init__(self, params, "desktop", prev_page, next_page)
50
51         data_dir = self.settings.get('data')
52         self.desktops_dir = os.path.join(data_dir, "images", "desktops")
53
54         self.desktop_info = self.gui.get_object("desktop_info")
55
56         self.desktop_image = None
57         self.icon_desktop_image = None
58
59         # Set up list box
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)
64
65         self.desktop_choice = 'deepin'
66
67         self.enabled_desktops = self.settings.get("desktops")
68
69         self.set_desktop_list()
70
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)
78         label.set_markup(txt)
79
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)
86         else:
87             self.desktop_image.set_from_file(path)
88
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)
94
95         if self.icon_desktop_image is None:
96             if icon_exists:
97                 pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(
98                     icon_path, 48, 48)
99                 self.icon_desktop_image = Gtk.Image.new_from_pixbuf(pixbuf)
100             else:
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)
105                 if icon_exists:
106                     self.icon_desktop_image = Gtk.Image.new_from_file(
107                         icon_path)
108                 else:
109                     self.icon_desktop_image = Gtk.Image.new_from_icon_name(
110                         "image-missing",
111                         Gtk.IconSize.DIALOG)
112
113             overlay = self.gui.get_object("image_overlay")
114             overlay.add_overlay(self.icon_desktop_image)
115         else:
116             if icon_exists:
117                 pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(
118                     icon_path, 48, 48)
119                 self.icon_desktop_image.set_from_pixbuf(pixbuf)
120             else:
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)
125                 if icon_exists:
126                     self.icon_desktop_image.set_from_file(icon_path)
127                 else:
128                     self.icon_desktop_image.set_from_icon_name(
129                         "image-missing", Gtk.IconSize.DIALOG)
130
131         if set_header:
132             # set header text
133             txt = _("Choose Your Desktop")
134             self.header.set_subtitle(txt)
135
136     def prepare(self, direction):
137         """ Prepare screen """
138         self.translate_ui(self.desktop_choice)
139         self.show_all()
140
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:
145                 box = Gtk.HBox()
146
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(
152                         icon_path, 24, 24)
153                     image = Gtk.Image.new_from_pixbuf(pixbuf)
154                 else:
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)
160                     else:
161                         image = Gtk.Image.new_from_icon_name(
162                             "image-missing",
163                             Gtk.IconSize.LARGE_TOOLBAR)
164                 box.pack_start(image, False, False, 2)
165
166                 label = Gtk.Label()
167                 label.set_markup(desktop_info.NAMES[desktop])
168                 box.pack_start(label, False, False, 2)
169
170                 self.listbox.add(box)
171
172         # Set Gnome as default
173         self.select_default_row(desktop_info.NAMES["gnome"])
174
175     @staticmethod
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]
183
184         box2 = row2.get_child()
185         label2 = box2.get_children()[1]
186
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)
190
191         # If strings are already well sorted return < 0
192         if text[0] == sorted_text[0]:
193             return -1
194
195         # Strings must be swaped, return > 0
196         return 1
197
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)
207                     return
208
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)
215                 return
216
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)
226
227     def store_values(self):
228         """ Store desktop """
229         self.settings.set('desktop', self.desktop_choice.lower())
230         logging.info(
231             "Cnchi will install RebornOS with the '%s' desktop",
232             self.desktop_choice.lower())
233         return True
234
235     @staticmethod
236     def scroll_to_cell(treeview, path):
237         """ Scrolls treeview to show the desired cell """
238         treeview.scroll_to_cell(path)
239         return False
240
241
242 # When testing, no _() is available
243 try:
244     _("")
245 except NameError as err:
246     def _(message):
247         return message
248
249 if __name__ == '__main__':
250     from test_screen import _, run
251     run('DesktopAsk')