88import shutil
99import subprocess
1010from setproctitle import setproctitle
11+ from pathlib import Path
1112
1213import gi
1314gi .require_version ("Gtk" , "3.0" )
@@ -56,7 +57,7 @@ def ask(msg):
5657class ItemEditor (object ):
5758 ui_file = None
5859
59- def __init__ (self , item_path = None , callback = None , destdir = None ):
60+ def __init__ (self , item_path = None , callback = None , destdir = None , icon_size = 24 ):
6061 self .builder = Gtk .Builder ()
6162 self .builder .set_translation_domain ('cinnamon' ) # let it translate!
6263 self .builder .add_from_file (self .ui_file )
@@ -66,16 +67,32 @@ def __init__(self, item_path=None, callback=None, destdir=None):
6667
6768 self .dialog .connect ('response' , self .on_response )
6869
70+ self .starting_icon = None
6971 self .icon_chooser = self .builder .get_object ('icon-chooser' )
7072 self .icon_chooser .get_dialog ().set_property ("allow-paths" , True )
7173
74+ self .icon_size = icon_size
75+ self .icon_chooser .set_icon_size (self .get_gtk_size_for_pixels (icon_size ))
76+
7277 self .build_ui ()
7378
7479 self .item_path = item_path
7580 self .load ()
7681 self .check_custom_path ()
7782 self .resync_validity ()
7883
84+ def get_gtk_size_for_pixels (self , icon_size ):
85+ i = 0
86+ while True :
87+ try :
88+ valid , width , height = Gtk .IconSize .lookup (Gtk .IconSize (i ))
89+ if height > icon_size :
90+ return Gtk .IconSize (i )
91+ except ValueError :
92+ return Gtk .IconSize .DIALOG
93+
94+ i += 1
95+
7996 def build_ui (self ):
8097 raise NotImplementedError ()
8198
@@ -139,6 +156,7 @@ def set_icon(self, name):
139156 else :
140157 print (val )
141158 self .icon_chooser .set_icon (val )
159+ self .starting_icon = val
142160 print ('icon:' , self .icon_chooser .get_icon ())
143161
144162 def load (self ):
@@ -178,8 +196,6 @@ def on_response(self, dialog, response):
178196 self .callback (True , self .item_path )
179197 else :
180198 self .callback (False , self .item_path )
181- self .dialog .destroy ()
182-
183199
184200class LauncherEditor (ItemEditor ):
185201 ui_file = '/usr/share/cinnamon/cinnamon-desktop-editor/launcher-editor.ui'
@@ -295,13 +311,37 @@ def load(self):
295311 self .set_icon ("Icon" )
296312
297313 def get_keyfile_edits (self ):
314+ icon_theme = Gtk .IconTheme .get_default ()
315+ icon = self .icon_chooser .get_icon ()
316+
317+ if icon != self .starting_icon :
318+ info = icon_theme .lookup_icon (icon , self .icon_size , 0 )
319+ if info :
320+ filename = info .get_filename ()
321+ if not self .in_hicolor (filename ):
322+ icon = filename
323+
298324 return dict (Name = self .builder .get_object ('name-entry' ).get_text (),
299325 Exec = self .builder .get_object ('exec-entry' ).get_text (),
300326 Comment = self .builder .get_object ('comment-entry' ).get_text (),
301327 Terminal = self .builder .get_object ('terminal-check' ).get_active (),
302- Icon = self . icon_chooser . get_icon () ,
328+ Icon = icon ,
303329 Type = "Application" )
304330
331+ def in_hicolor (self , path ):
332+ datadirs = GLib .get_system_data_dirs ()
333+
334+ for datadir in datadirs :
335+ hicolor_folder = Path (os .path .join (datadir , "icons" , "hicolor" ))
336+ icon_path = Path (path )
337+ try :
338+ if icon_path .relative_to (hicolor_folder ) is not None :
339+ return True
340+ except ValueError :
341+ pass
342+
343+ return False
344+
305345 def pick_exec (self , button ):
306346 chooser = Gtk .FileChooserDialog (title = _ ("Choose a command" ),
307347 parent = self .dialog ,
@@ -320,6 +360,7 @@ def __init__(self):
320360 parser .add_option ("-d" , "--directory" , dest = "destination_directory" , help = "Destination directory of the new launcher" , metavar = "DEST_DIR" )
321361 parser .add_option ("-f" , "--file" , dest = "desktop_file" , help = "Name of desktop file (i.e. gnome-terminal.desktop)" , metavar = "DESKTOP_NAME" )
322362 parser .add_option ("-m" , "--mode" , dest = "mode" , default = None , help = "Mode to run in: launcher, directory, panel-launcher or nemo-launcher" )
363+ parser .add_option ("-i" , "--icon-size" , dest = "icon_size" , type = int , default = 24 , help = "Size to set the icon picker for (panel-launcher only)" )
323364 (options , args ) = parser .parse_args ()
324365
325366 if not options .mode :
@@ -343,6 +384,7 @@ def __init__(self):
343384
344385 if options .mode == "cinnamon-launcher" :
345386 self .json_path = args [0 ]
387+ self .icon_size = options .icon_size
346388
347389 if self .desktop_file is not None :
348390 self .get_desktop_path ()
@@ -354,7 +396,7 @@ def __init__(self):
354396 editor = LauncherEditor (self .orig_file , self .launcher_cb )
355397 editor .dialog .show_all ()
356398 elif self .mode == "cinnamon-launcher" :
357- editor = CinnamonLauncherEditor (self .orig_file , self .panel_launcher_cb )
399+ editor = CinnamonLauncherEditor (self .orig_file , self .panel_launcher_cb , icon_size = self . icon_size )
358400 editor .dialog .show_all ()
359401 elif self .mode == "nemo-launcher" :
360402 editor = LauncherEditor (self .orig_file , self .nemo_launcher_cb , self .dest_dir )
0 commit comments