@@ -44,7 +44,7 @@ def ask(msg):
4444 buttons = Gtk .ButtonsType .YES_NO ,
4545 )
4646 dialog .set_markup (msg )
47-
47+
4848 response = dialog .run ()
4949 dialog .destroy ()
5050 return response == Gtk .ResponseType .YES
@@ -90,7 +90,7 @@ def fillKeyFile(keyfile, items):
9090 keyfile .set_string (DESKTOP_GROUP , key , item )
9191 if key in LOCALIZABLE_KEYS and locale :
9292 keyfile .set_locale_string (DESKTOP_GROUP , key , locale , item )
93- elif isinstance (item , Sequence ):
93+ elif isinstance (item , list ):
9494 keyfile .set_string_list (DESKTOP_GROUP , key , item )
9595 if key in LOCALIZABLE_KEYS and locale :
9696 keyfile .set_locale_string_list (DESKTOP_GROUP , key , locale , item )
@@ -99,13 +99,12 @@ def fillKeyFile(keyfile, items):
9999class ItemEditor (object ):
100100 ui_file = None
101101
102- def __init__ (self , item_path = None , callback = None , destdir = None , icon_size = 24 , is_nemo_launcher = False ):
102+ def __init__ (self , item_path = None , callback = None , destdir = None , icon_size = 24 ):
103103 self .builder = Gtk .Builder ()
104104 self .builder .set_translation_domain ('cinnamon' ) # let it translate!
105105 self .builder .add_from_file (self .ui_file )
106106 self .callback = callback
107107 self .destdir = destdir
108- self .is_nemo_launcher = is_nemo_launcher
109108 self .dialog = self .builder .get_object ('editor' )
110109
111110 self .dialog .connect ('response' , self .on_response )
@@ -175,6 +174,22 @@ def validate_exec_line(self, string):
175174 def get_keyfile_edits (self ):
176175 raise NotImplementedError ()
177176
177+ def pick_exec (self , button ):
178+ chooser = Gtk .FileChooserDialog (
179+ title = _ ("Choose a command" ),
180+ parent = self .dialog ,
181+ action = Gtk .FileChooserAction .OPEN
182+ )
183+ chooser .add_buttons (
184+ Gtk .STOCK_CANCEL , Gtk .ResponseType .REJECT ,
185+ Gtk .STOCK_OK , Gtk .ResponseType .ACCEPT
186+ )
187+ response = chooser .run ()
188+
189+ if response == Gtk .ResponseType .ACCEPT :
190+ self .builder .get_object ('exec-entry' ).set_text (escape_space (chooser .get_filename ()))
191+ chooser .destroy ()
192+
178193 def set_text (self , ctl , name ):
179194 try :
180195 val = self .keyfile .get_locale_string (DESKTOP_GROUP , name , None )
@@ -208,6 +223,71 @@ def load(self):
208223 except GLib .GError :
209224 pass
210225
226+ def save (self ):
227+ fillKeyFile (self .keyfile , self .get_keyfile_edits ())
228+ contents , length = self .keyfile .to_data ()
229+
230+ try :
231+ with open (self .item_path , 'w' ) as f :
232+ f .write (contents )
233+
234+ except IOError as e :
235+ print ("Error writing file:" , e )
236+
237+ def run (self ):
238+ self .dialog .present ()
239+
240+ def on_response (self , dialog , response ):
241+ if response == Gtk .ResponseType .OK :
242+ self .save ()
243+ self .callback (True , self .item_path )
244+ else :
245+ self .callback (False , self .item_path )
246+
247+ class NemoLauncherEditor (ItemEditor ):
248+ ui_file = '/usr/share/cinnamon/cinnamon-desktop-editor/launcher-editor.ui'
249+
250+ def build_ui (self ):
251+ self .builder .get_object ('exec-browse' ).connect ('clicked' , self .pick_exec )
252+ self .builder .get_object ('name-entry' ).connect ('changed' , self .resync_validity )
253+ self .builder .get_object ('exec-entry' ).connect ('changed' , self .resync_validity )
254+
255+ # Hide LauncherEditor widgets not relevant to NemoLauncherEditor
256+ self .builder .get_object ('nodisplay-check' ).set_visible (False )
257+ self .builder .get_object ('nodisplay-check' ).set_no_show_all (True )
258+ self .builder .get_object ('category-section' ).set_visible (False )
259+ self .builder .get_object ('category-section' ).set_no_show_all (True )
260+
261+ def resync_validity (self , * args ):
262+ name_text = self .builder .get_object ('name-entry' ).get_text ().strip ()
263+ exec_text = self .builder .get_object ('exec-entry' ).get_text ().strip ()
264+ name_valid = name_text != ""
265+ exec_valid = self .validate_exec_line (exec_text )
266+ self .sync_widgets (name_valid , exec_valid )
267+
268+
269+ def load (self ):
270+ super (NemoLauncherEditor , self ).load ()
271+ self .set_text ('name-entry' , "Name" )
272+ self .set_text ('exec-entry' , "Exec" )
273+ self .set_text ('comment-entry' , "Comment" )
274+ self .set_check ('terminal-check' , "Terminal" )
275+ self .set_check ('offload-gpu-check' , "PrefersNonDefaultGPU" )
276+ self .set_icon ("Icon" )
277+
278+ def get_keyfile_edits (self ):
279+ return dict (Name = self .builder .get_object ('name-entry' ).get_text (),
280+ Exec = self .builder .get_object ('exec-entry' ).get_text (),
281+ Comment = self .builder .get_object ('comment-entry' ).get_text (),
282+ Terminal = self .builder .get_object ('terminal-check' ).get_active (),
283+ PrefersNonDefaultGPU = self .builder .get_object ("offload-gpu-check" ).get_active (),
284+ Icon = self .icon_chooser .get_icon (),
285+ Type = "Application" )
286+
287+ def check_custom_path (self ):
288+ if self .item_path :
289+ self .item_path = os .path .join (getUserItemPath (), os .path .split (self .item_path )[1 ])
290+
211291 def save (self ):
212292 fillKeyFile (self .keyfile , self .get_keyfile_edits ())
213293 contents , length = self .keyfile .to_data ()
@@ -228,16 +308,6 @@ def save(self):
228308 self .destdir = GLib .get_user_special_dir (GLib .UserDirectory .DIRECTORY_DESKTOP )
229309 self .save ()
230310
231- def run (self ):
232- self .dialog .present ()
233-
234- def on_response (self , dialog , response ):
235- if response == Gtk .ResponseType .OK :
236- self .save ()
237- self .callback (True , self .item_path )
238- else :
239- self .callback (False , self .item_path )
240-
241311class LauncherEditor (ItemEditor ):
242312 ui_file = '/usr/share/cinnamon/cinnamon-desktop-editor/launcher-editor.ui'
243313
@@ -246,12 +316,6 @@ def build_ui(self):
246316 self .builder .get_object ('name-entry' ).connect ('changed' , self .resync_validity )
247317 self .builder .get_object ('exec-entry' ).connect ('changed' , self .resync_validity )
248318
249- if self .is_nemo_launcher :
250- self .builder .get_object ('nodisplay-check' ).set_visible (False )
251- self .builder .get_object ('nodisplay-check' ).set_no_show_all (True )
252- self .builder .get_object ('category-section' ).set_visible (False )
253- self .builder .get_object ('category-section' ).set_no_show_all (True )
254-
255319 self .category_widgets = {} # Map ID -> CheckButton
256320 self ._setup_categories_list ()
257321 self .fdo_categories = []
@@ -266,7 +330,7 @@ def resync_validity(self, *args):
266330 def _setup_categories_list (self ):
267331 flowbox = self .builder .get_object ('category-flowbox' )
268332 DONT_SHOWS = ["Other" ]
269-
333+
270334 tree = CMenu .Tree .new ("cinnamon-applications.menu" , CMenu .TreeFlags .INCLUDE_NODISPLAY | CMenu .TreeFlags .SHOW_EMPTY )
271335 if tree .load_sync ():
272336 root = tree .get_root_directory ()
@@ -299,15 +363,15 @@ def load(self):
299363 try :
300364 flowbox = self .builder .get_object ('category-flowbox' )
301365 self .fdo_categories = self .keyfile .get_string_list (DESKTOP_GROUP , "Categories" )
302- cinnamon_categories = self ._convert_to_cinnamon (self .fdo_categories )
366+ cinnamon_categories = self ._fdo_to_cinnamon (self .fdo_categories )
303367 for cat_id in cinnamon_categories :
304368 if cat_id in self .category_widgets :
305369 self .category_widgets [cat_id ].set_active (True )
306370 except GLib .GError :
307371 pass
308372
309373 def get_keyfile_edits (self ):
310- self .fdo_categories = self ._convert_to_fdo (self .fdo_categories )
374+ self .fdo_categories = self ._cinnamon_to_fdo (self .fdo_categories )
311375 categories_val = ";" .join (self .fdo_categories )
312376 if categories_val :
313377 categories_val += ";"
@@ -322,21 +386,11 @@ def get_keyfile_edits(self):
322386 Icon = self .icon_chooser .get_icon (),
323387 Type = "Application" )
324388
325- def pick_exec (self , button ):
326- chooser = Gtk .FileChooserDialog (title = _ ("Choose a command" ),
327- parent = self .dialog ,
328- buttons = (Gtk .STOCK_CANCEL , Gtk .ResponseType .REJECT ,
329- Gtk .STOCK_OK , Gtk .ResponseType .ACCEPT ))
330- response = chooser .run ()
331- if response == Gtk .ResponseType .ACCEPT :
332- self .builder .get_object ('exec-entry' ).set_text (escape_space (chooser .get_filename ()))
333- chooser .destroy ()
334-
335389 def check_custom_path (self ):
336390 if self .item_path :
337391 self .item_path = os .path .join (getUserItemPath (), os .path .split (self .item_path )[1 ])
338392
339- def _convert_to_cinnamon (self , fdo_cats ):
393+ def _fdo_to_cinnamon (self , fdo_cats ):
340394 # These conversions are based on /etc/xdg/menus/cinnamon-applications.menu
341395 cats = list (fdo_cats )
342396
@@ -362,10 +416,9 @@ def _convert_to_cinnamon(self, fdo_cats):
362416 if "System" in cats :
363417 cats .append ("Administration" )
364418
365-
366419 return cats
367420
368- def _convert_to_fdo (self , fdo_cats ):
421+ def _cinnamon_to_fdo (self , fdo_cats ):
369422 # These conversions are based on /etc/xdg/menus/cinnamon-applications.menu
370423 mappings = {
371424 "Accessories" : "Utility" ,
@@ -386,7 +439,7 @@ def _convert_to_fdo(self, fdo_cats):
386439 else :
387440 if fdo_cat in fdo_cats :
388441 fdo_cats .remove (fdo_cat )
389-
442+
390443 if self .category_widgets ["Accessories" ].get_active ():
391444 if "System" in fdo_cats :
392445 fdo_cats .remove ("System" )
@@ -399,7 +452,7 @@ def _convert_to_fdo(self, fdo_cats):
399452 if self .category_widgets ["Preferences" ].get_active ():
400453 if "System" in fdo_cats :
401454 fdo_cats .remove ("System" )
402-
455+
403456 return fdo_cats
404457
405458 def _clear_menu_overrides (self ):
@@ -438,8 +491,8 @@ def _clear_menu_overrides(self):
438491
439492 def save (self ):
440493 super (LauncherEditor , self ).save ()
441- if not self . is_nemo_launcher :
442- self ._clear_menu_overrides ()
494+ subprocess . Popen ([ 'update-desktop-database' , getUserItemPath ()], env = os . environ )
495+ self ._clear_menu_overrides ()
443496
444497class DirectoryEditor (ItemEditor ):
445498 ui_file = '/usr/share/cinnamon/cinnamon-desktop-editor/directory-editor.ui'
@@ -549,17 +602,6 @@ def in_hicolor(self, path):
549602
550603 return False
551604
552- def pick_exec (self , button ):
553- chooser = Gtk .FileChooserDialog (title = _ ("Choose a command" ),
554- parent = self .dialog ,
555- buttons = (Gtk .STOCK_CANCEL , Gtk .ResponseType .REJECT ,
556- Gtk .STOCK_OK , Gtk .ResponseType .ACCEPT ))
557- response = chooser .run ()
558- if response == Gtk .ResponseType .ACCEPT :
559- self .builder .get_object ('exec-entry' ).set_text (escape_space (chooser .get_filename ()))
560- chooser .destroy ()
561-
562-
563605class Main :
564606 def __init__ (self ):
565607 parser = OptionParser ()
@@ -606,7 +648,7 @@ def __init__(self):
606648 editor = CinnamonLauncherEditor (self .orig_file , self .panel_launcher_cb , icon_size = self .icon_size )
607649 editor .dialog .show_all ()
608650 elif self .mode == "nemo-launcher" :
609- editor = LauncherEditor (self .orig_file , self .nemo_launcher_cb , self .dest_dir , is_nemo_launcher = True )
651+ editor = NemoLauncherEditor (self .orig_file , self .nemo_launcher_cb , self .dest_dir )
610652 editor .dialog .show_all ()
611653 else :
612654 print ("Invalid args" )
0 commit comments