From 99f933806ca171fe2e1efee27afb2640fd090e07 Mon Sep 17 00:00:00 2001 From: "Jasper St. Pierre" Date: Tue, 1 May 2012 15:31:31 -0400 Subject: [PATCH] Use spaces for indents PEP8 compliance --- Alacarte/MainWindow.py | 1324 +++++++++++++++++++------------------- Alacarte/MenuEditor.py | 1642 ++++++++++++++++++++++++------------------------ Alacarte/util.py | 468 +++++++------- 3 files changed, 1717 insertions(+), 1717 deletions(-) rewrite Alacarte/MainWindow.py (94%) rewrite Alacarte/MenuEditor.py (95%) rewrite Alacarte/util.py (82%) diff --git a/Alacarte/MainWindow.py b/Alacarte/MainWindow.py dissimilarity index 94% index b5fc35b..2313e73 100644 --- a/Alacarte/MainWindow.py +++ b/Alacarte/MainWindow.py @@ -1,662 +1,662 @@ -# -*- coding: utf-8 -*- -# vim: set noexpandtab: -# Alacarte Menu Editor - Simple fd.o Compliant Menu Editor -# Copyright (C) 2006 Travis Watkins -# -# This library is free software; you can redistribute it and/or -# modify it under the terms of the GNU Library General Public -# License as published by the Free Software Foundation; either -# version 2 of the License, or (at your option) any later version. -# -# This library is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -# Library General Public License for more details. -# -# You should have received a copy of the GNU Library General Public -# License along with this library; if not, write to the Free Software -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - -from gi.repository import Gtk, GObject, Gio, GdkPixbuf, Gdk, GMenu -import cgi, os -import gettext -import subprocess -import urllib -try: - from Alacarte import config - gettext.bindtextdomain(config.GETTEXT_PACKAGE,config.localedir) - gettext.textdomain(config.GETTEXT_PACKAGE) -except: - pass -_ = gettext.gettext -from Alacarte.MenuEditor import MenuEditor -from Alacarte import util -import sys -from traceback import print_stack - -class MainWindow: - timer = None - #hack to make editing menu properties work - allow_update = True - #drag-and-drop stuff - dnd_items = [('ALACARTE_ITEM_ROW', Gtk.TargetFlags.SAME_APP, 0), ('text/plain', 0, 1)] - dnd_menus = [('ALACARTE_MENU_ROW', Gtk.TargetFlags.SAME_APP, 0)] - dnd_both = [dnd_items[0],] + dnd_menus - drag_data = None - edit_pool = [] - - def __init__(self, datadir, version, argv): - self.file_path = datadir - self.version = version - self.editor = MenuEditor() - Gtk.Window.set_default_icon_name('alacarte') - self.tree = Gtk.Builder() - self.tree.set_translation_domain(config.GETTEXT_PACKAGE) - self.tree.add_from_file(os.path.join(self.file_path, 'alacarte.ui')) - self.tree.connect_signals(self) - self.setupMenuTree() - self.setupItemTree() - self.tree.get_object('edit_delete').set_sensitive(False) - self.tree.get_object('edit_revert_to_original').set_sensitive(False) - self.tree.get_object('edit_properties').set_sensitive(False) - self.tree.get_object('move_up_button').set_sensitive(False) - self.tree.get_object('move_down_button').set_sensitive(False) - self.tree.get_object('new_separator_button').set_sensitive(False) - accelgroup = Gtk.AccelGroup() - keyval, modifier = Gtk.accelerator_parse('Z') - accelgroup.connect(keyval, modifier, Gtk.AccelFlags.VISIBLE, self.on_mainwindow_undo) - keyval, modifier = Gtk.accelerator_parse('Z') - accelgroup.connect(keyval, modifier, Gtk.AccelFlags.VISIBLE, self.on_mainwindow_redo) - keyval, modifier = Gtk.accelerator_parse('F1') - accelgroup.connect(keyval, modifier, Gtk.AccelFlags.VISIBLE, self.on_help_button_clicked) - self.tree.get_object('mainwindow').add_accel_group(accelgroup) - - def run(self): - self.loadMenus() - self.editor.applications.tree.connect("changed", self.menuChanged) - self.tree.get_object('mainwindow').show_all() - Gtk.main() - - def menuChanged(self, *a): - print >> sys.stderr, "changed!\n" - if self.timer: - GObject.source_remove(self.timer) - self.timer = None - self.timer = GObject.timeout_add(3, self.loadUpdates) - - def loadUpdates(self): - print >> sys.stderr, "%d\n" % self.editor.applications.tree.disconnect_by_func(self.menuChanged) - self.editor.reloadMenus() - self.editor.applications.tree.connect("changed", self.menuChanged) - - if not self.allow_update: - return False - menu_tree = self.tree.get_object('menu_tree') - item_tree = self.tree.get_object('item_tree') - items, iter = item_tree.get_selection().get_selected() - update_items = False - update_type = None - item_id, separator_path = None, None - if iter: - update_items = True - if isinstance(items[iter][3], GMenu.TreeEntry): - item_id = items[iter][3].get_desktop_file_id() - update_type = GMenu.TreeItemType.ENTRY - elif isinstance(items[iter][3], GMenu.TreeDirectory): - item_id = os.path.split(items[iter][3].get_desktop_file_path())[1] - update_type = GMenu.TreeItemType.DIRECTORY - elif isinstance(items[iter][3], GMenu.Tree.Separator): - item_id = items.get_path(iter) - update_type = GMenu.TreeItemType.SEPARATOR - menus, iter = menu_tree.get_selection().get_selected() - update_menus = False - menu_id = None - if iter: - if menus[iter][2].get_desktop_file_path(): - menu_id = os.path.split(menus[iter][2].get_desktop_file_path())[1] - else: - menu_id = menus[iter][2].get_menu_id() - update_menus = True - self.loadMenus() - #find current menu in new tree - if update_menus: - menu_tree.get_model().foreach(self.findMenu, menu_id) - menus, iter = menu_tree.get_selection().get_selected() - if iter: - self.on_menu_tree_cursor_changed(menu_tree) - #find current item in new list - if update_items: - i = 0 - for item in item_tree.get_model(): - found = False - if update_type != GMenu.TreeItemType.SEPARATOR: - if isinstance (item[3], GMenu.TreeEntry) and item[3].get_desktop_file_id() == item_id: - found = True - if isinstance (item[3], GMenu.TreeDirectory) and item[3].get_desktop_file_path() and update_type == GMenu.TreeItemType.DIRECTORY: - if os.path.split(item[3].get_desktop_file_path())[1] == item_id: - found = True - if isinstance(item[3], GMenu.TreeSeparator): - if not isinstance(item_id, tuple): - #we may not skip the increment via "continue" - i += 1 - continue - #separators have no id, have to find them manually - #probably won't work with two separators together - if (item_id[0] - 1,) == (i,): - found = True - elif (item_id[0] + 1,) == (i,): - found = True - elif (item_id[0],) == (i,): - found = True - if found: - item_tree.get_selection().select_path((i,)) - self.on_item_tree_cursor_changed(item_tree) - break - i += 1 - return False - - def findMenu(self, menus, path, iter, menu_id): - if not menus[path][2].get_desktop_file_path(): - if menu_id == menus[path][2].get_menu_id(): - menu_tree = self.tree.get_object('menu_tree') - menu_tree.expand_to_path(path) - menu_tree.get_selection().select_path(path) - return True - return False - if os.path.split(menus[path][2].get_desktop_file_path())[1] == menu_id: - menu_tree = self.tree.get_object('menu_tree') - menu_tree.expand_to_path(path) - menu_tree.get_selection().select_path(path) - return True - - def setupMenuTree(self): - self.menu_store = Gtk.TreeStore(GdkPixbuf.Pixbuf, str, object) - menus = self.tree.get_object('menu_tree') - column = Gtk.TreeViewColumn(_('Name')) - column.set_spacing(4) - cell = Gtk.CellRendererPixbuf() - column.pack_start(cell, False) - column.add_attribute(cell, 'pixbuf', 0) - cell = Gtk.CellRendererText() - column.pack_start(cell, True) - column.add_attribute(cell, 'markup', 1) - menus.append_column(column) - menus.enable_model_drag_source(Gdk.ModifierType.BUTTON1_MASK, self.dnd_menus, Gdk.DragAction.COPY) - menus.enable_model_drag_dest(self.dnd_both, Gdk.DragAction.PRIVATE) - menus.get_selection().set_mode(Gtk.SelectionMode.BROWSE) - - def setupItemTree(self): - items = self.tree.get_object('item_tree') - column = Gtk.TreeViewColumn(_('Show')) - cell = Gtk.CellRendererToggle() - cell.connect('toggled', self.on_item_tree_show_toggled) - column.pack_start(cell, True) - column.add_attribute(cell, 'active', 0) - #hide toggle for separators - column.set_cell_data_func(cell, self._cell_data_toggle_func) - items.append_column(column) - column = Gtk.TreeViewColumn(_('Item')) - column.set_spacing(4) - cell = Gtk.CellRendererPixbuf() - column.pack_start(cell, False) - column.add_attribute(cell, 'pixbuf', 1) - cell = Gtk.CellRendererText() - column.pack_start(cell, True) - column.add_attribute(cell, 'markup', 2) - items.append_column(column) - self.item_store = Gtk.ListStore(bool, GdkPixbuf.Pixbuf, str, object) - items.set_model(self.item_store) - items.enable_model_drag_source(Gdk.ModifierType.BUTTON1_MASK, self.dnd_items, Gdk.DragAction.COPY) - items.enable_model_drag_dest(self.dnd_items, Gdk.DragAction.PRIVATE) - - def _cell_data_toggle_func(self, tree_column, renderer, model, treeiter, data=None): - if isinstance(model[treeiter][3], GMenu.TreeSeparator): - renderer.set_property('visible', False) - else: - renderer.set_property('visible', True) - - def loadMenus(self): - self.menu_store.clear() - for menu in self.editor.getMenus(): - iters = [None]*20 - self.loadMenu(iters, menu) - menu_tree = self.tree.get_object('menu_tree') - menu_tree.set_model(self.menu_store) - for menu in self.menu_store: - #this might not work for some reason - try: - menu_tree.expand_to_path(menu.path) - except: - pass - menu_tree.get_selection().select_path((0,)) - self.on_menu_tree_cursor_changed(menu_tree) - - def loadMenu(self, iters, parent, depth=0): - if depth == 0: - icon = util.getIcon(parent) - iters[depth] = self.menu_store.append(None, (icon, cgi.escape(parent.get_name()), parent)) - depth += 1 - for menu, show in self.editor.getMenus(parent): - if show: - name = cgi.escape(menu.get_name()) - else: - name = '' + cgi.escape(menu.get_name()) + '' - icon = util.getIcon(menu) - iters[depth] = self.menu_store.append(iters[depth-1], (icon, name, menu)) - self.loadMenu(iters, menu, depth) - depth -= 1 - - def loadItems(self, menu, menu_path): - self.item_store.clear() - for item, show in self.editor.getItems(menu): - menu_icon = None - if isinstance(item, GMenu.TreeSeparator): - name = '---' - icon = None - elif isinstance(item, GMenu.TreeEntry): - app_info = item.get_app_info() - if show: - name = cgi.escape(app_info.get_display_name()) - else: - name = '' + cgi.escape(app_info.get_display_name()) + '' - icon = util.getIcon(item) - else: - if show: - name = cgi.escape(item.get_name()) - else: - name = '' + cgi.escape(item.get_name()) + '' - icon = util.getIcon(item) - self.item_store.append((show, icon, name, item)) - - #this is a little timeout callback to insert new items after - #gnome-desktop-item-edit has finished running - def waitForNewItemProcess(self, process, parent_id, file_path): - if process.poll() != None: - if os.path.isfile(file_path): - self.editor.insertExternalItem(os.path.split(file_path)[1], parent_id) - return False - return True - - def waitForNewMenuProcess(self, process, parent_id, file_path): - if process.poll() != None: - #hack for broken gnome-desktop-item-edit - broken_path = os.path.join(os.path.split(file_path)[0], '.directory') - if os.path.isfile(broken_path): - os.rename(broken_path, file_path) - if os.path.isfile(file_path): - self.editor.insertExternalMenu(os.path.split(file_path)[1], parent_id) - return False - return True - - #this callback keeps you from editing the same item twice - def waitForEditProcess(self, process, file_path): - if process.poll() != None: - self.edit_pool.remove(file_path) - return False - return True - - def on_new_menu_button_clicked(self, button): - menu_tree = self.tree.get_object('menu_tree') - menus, iter = menu_tree.get_selection().get_selected() - if not iter: - parent = menus[(0,)][2] - menu_tree.expand_to_path((0,)) - menu_tree.get_selection().select_path((0,)) - else: - parent = menus[iter][2] - file_path = os.path.join(util.getUserDirectoryPath(), util.getUniqueFileId('alacarte-made', '.directory')) - process = subprocess.Popen(['gnome-desktop-item-edit', file_path], env=os.environ) - GObject.timeout_add(100, self.waitForNewMenuProcess, process, parent.get_menu_id(), file_path) - - def on_new_item_button_clicked(self, button): - menu_tree = self.tree.get_object('menu_tree') - menus, iter = menu_tree.get_selection().get_selected() - if not iter: - parent = menus[(0,)][2] - menu_tree.expand_to_path((0,)) - menu_tree.get_selection().select_path((0,)) - else: - parent = menus[iter][2] - file_path = os.path.join(util.getUserItemPath(), util.getUniqueFileId('alacarte-made', '.desktop')) - process = subprocess.Popen(['gnome-desktop-item-edit', file_path], env=os.environ) - GObject.timeout_add(100, self.waitForNewItemProcess, process, parent.get_menu_id(), file_path) - - def on_new_separator_button_clicked(self, button): - item_tree = self.tree.get_object('item_tree') - items, iter = item_tree.get_selection().get_selected() - if not iter: - return - else: - after = items[iter][3] - menu_tree = self.tree.get_object('menu_tree') - menus, iter = menu_tree.get_selection().get_selected() - parent = menus[iter][2] - self.editor.createSeparator(parent, after=after) - - def on_edit_delete_activate(self, menu): - item_tree = self.tree.get_object('item_tree') - items, iter = item_tree.get_selection().get_selected() - if not iter: - return - item = items[iter][3] - if isinstance(item, GMenu.TreeEntry): - self.editor.deleteItem(item) - elif isinstance(item, GMenu.TreeDirectory): - self.editor.deleteMenu(item) - elif isinstance(item, GMenu.TreeSeparator): - self.editor.deleteSeparator(item) - - def on_edit_revert_to_original_activate(self, menu): - item_tree = self.tree.get_object('item_tree') - items, iter = item_tree.get_selection().get_selected() - if not iter: - return - item = items[iter][3] - if isinstance(item, GMenu.TreeEntry): - self.editor.revertItem(item) - elif isinstance(item, GMenu.TreeDirectory): - self.editor.revertMenu(item) - - def on_edit_properties_activate(self, menu): - item_tree = self.tree.get_object('item_tree') - items, iter = item_tree.get_selection().get_selected() - if not iter: - return - item = items[iter][3] - if not isinstance(item, GMenu.TreeEntry) and not isinstance(item, GMenu.TreeDirectory): - return - - if isinstance(item, GMenu.TreeEntry): - file_path = os.path.join(util.getUserItemPath(), item.get_desktop_file_id()) - file_type = 'Item' - elif isinstance(item, GMenu.TreeDirectory): - if item.get_desktop_file_path() == None: - file_path = util.getUniqueFileId('alacarte-made', '.directory') - parser = util.DesktopParser(file_path, 'Directory') - parser.set('Name', item.get_name()) - parser.set('Comment', item.get_comment()) - parser.set('Icon', item.get_icon()) - parser.write(open(file_path)) - else: - file_path = os.path.join(util.getUserDirectoryPath(), os.path.split(item.get_desktop_file_path())[1]) - file_type = 'Menu' - - if not os.path.isfile(file_path): - data = open(item.get_desktop_file_path()).read() - open(file_path, 'w').write(data) - self.editor._MenuEditor__addUndo([(file_type, os.path.split(file_path)[1]),]) - else: - self.editor._MenuEditor__addUndo([item,]) - if file_path not in self.edit_pool: - self.edit_pool.append(file_path) - process = subprocess.Popen(['gnome-desktop-item-edit', file_path], env=os.environ) - GObject.timeout_add(100, self.waitForEditProcess, process, file_path) - - def on_menu_tree_cursor_changed(self, treeview): - menus, iter = treeview.get_selection().get_selected() - menu_path = menus.get_path(iter) - item_tree = self.tree.get_object('item_tree') - item_tree.get_selection().unselect_all() - self.loadItems(self.menu_store[menu_path][2], menu_path) - self.tree.get_object('edit_delete').set_sensitive(False) - self.tree.get_object('edit_revert_to_original').set_sensitive(False) - self.tree.get_object('edit_properties').set_sensitive(False) - self.tree.get_object('move_up_button').set_sensitive(False) - self.tree.get_object('move_down_button').set_sensitive(False) - self.tree.get_object('new_separator_button').set_sensitive(False) - self.tree.get_object('properties_button').set_sensitive(False) - self.tree.get_object('delete_button').set_sensitive(False) - - def on_menu_tree_drag_data_get(self, treeview, context, selection, target_id, etime): - menus, iter = treeview.get_selection().get_selected() - self.drag_data = menus[iter][2] - - def on_menu_tree_drag_data_received(self, treeview, context, x, y, selection, info, etime): - menus = treeview.get_model() - drop_info = treeview.get_dest_row_at_pos(x, y) - if drop_info: - path, position = drop_info - types_before = (Gtk.TreeViewDropPosition.INTO_OR_BEFORE, Gtk.TreeViewDropPosition.INTO_OR_AFTER) - types_into = (Gtk.TreeViewDropPosition.INTO_OR_BEFORE, Gtk.TreeViewDropPosition.INTO_OR_AFTER) - types_after = (Gtk.TreeViewDropPosition.AFTER, Gtk.TreeViewDropPosition.INTO_OR_AFTER) - if position not in types: - context.finish(False, False, etime) - return False - if selection.target in ('ALACARTE_ITEM_ROW', 'ALACARTE_MENU_ROW'): - if self.drag_data == None: - return False - item = self.drag_data - new_parent = menus[path][2] - treeview.get_selection().select_path(path) - if isinstance(item, GMenu.TreeEntry): - self.editor.copyItem(item, new_parent) - elif isinstance(item, GMenu.TreeDirectory): - if self.editor.moveMenu(item, new_parent) == False: - self.loadUpdates() - elif isinstance(item, GMenu.TreeSeparator): - self.editor.moveSeparator(item, new_parent) - else: - context.finish(False, False, etime) - context.finish(True, True, etime) - self.drag_data = None - - def on_item_tree_show_toggled(self, cell, path): - item = self.item_store[path][3] - if isinstance(item, GMenu.TreeSeparator): - return - if self.item_store[path][0]: - self.editor.setVisible(item, False) - else: - self.editor.setVisible(item, True) - self.item_store[path][0] = not self.item_store[path][0] - - def on_item_tree_cursor_changed(self, treeview): - items, iter = treeview.get_selection().get_selected() - if iter is None: - return - item = items[iter][3] - self.tree.get_object('edit_delete').set_sensitive(True) - self.tree.get_object('new_separator_button').set_sensitive(True) - self.tree.get_object('delete_button').set_sensitive(True) - if self.editor.canRevert(item): - self.tree.get_object('edit_revert_to_original').set_sensitive(True) - else: - self.tree.get_object('edit_revert_to_original').set_sensitive(False) - if not isinstance(item, GMenu.TreeSeparator): - self.tree.get_object('edit_properties').set_sensitive(True) - self.tree.get_object('properties_button').set_sensitive(True) - else: - self.tree.get_object('edit_properties').set_sensitive(False) - self.tree.get_object('properties_button').set_sensitive(False) - - # If first item... - if items.get_path(iter).get_indices()[0] == 0: - self.tree.get_object('move_up_button').set_sensitive(False) - else: - self.tree.get_object('move_up_button').set_sensitive(True) - - # If last item... - if items.get_path(iter).get_indices()[0] == (len(items)-1): - self.tree.get_object('move_down_button').set_sensitive(False) - else: - self.tree.get_object('move_down_button').set_sensitive(True) - - def on_item_tree_row_activated(self, treeview, path, column): - self.on_edit_properties_activate(None) - - def on_item_tree_popup_menu(self, item_tree, event=None): - model, iter = item_tree.get_selection().get_selected() - if event: - #don't show if it's not the right mouse button - if event.button != 3: - return - button = event.button - event_time = event.time - info = item_tree.get_path_at_pos(int(event.x), int(event.y)) - if info != None: - path, col, cellx, celly = info - item_tree.grab_focus() - item_tree.set_cursor(path, col, 0) - else: - path = model.get_path(iter) - button = 0 - event_time = 0 - item_tree.grab_focus() - item_tree.set_cursor(path, item_tree.get_columns()[0], 0) - popup = self.tree.get_object('edit_menu') - popup.popup(None, None, None, button, event_time) - #without this shift-f10 won't work - return True - - def on_item_tree_drag_data_get(self, treeview, context, selection, target_id, etime): - items, iter = treeview.get_selection().get_selected() - self.drag_data = items[iter][3] - - def on_item_tree_drag_data_received(self, treeview, context, x, y, selection, info, etime): - items = treeview.get_model() - types = (Gtk.TreeViewDropPosition.BEFORE, Gtk.TreeViewDropPosition.INTO_OR_BEFORE) - if selection.target == 'ALACARTE_ITEM_ROW': - drop_info = treeview.get_dest_row_at_pos(x, y) - before = None - after = None - if self.drag_data == None: - return False - item = self.drag_data - # by default we assume, that the items stays in the same menu - destination = item.get_parent() - if drop_info: - path, position = drop_info - target = items[path][3] - # move the item to the directory, if the item was dropped into it - if isinstance(target, GMenu.TreeDirectory) and (position in types_into): - # append the selected item to the choosen menu - destination = target - elif position in types_before: - before = target - elif position in types_after: - after = target - else: - # this does not happen - pass - else: - path = (len(items) - 1,) - after = items[path][3] - if isinstance(item, GMenu.TreeEntry): - self.editor.moveItem(item, destination, before, after) - elif isinstance(item, GMenu.TreeDirectory): - if self.editor.moveMenu(item, destination, before, after) == False: - self.loadUpdates() - elif isinstance(item, GMenu.TreeSeparator): - self.editor.moveSeparator(item, destination, before, after) - context.finish(True, True, etime) - elif selection.target == 'text/plain': - if selection.data == None: - return False - menus, iter = self.tree.get_object('menu_tree').get_selection().get_selected() - parent = menus[iter][2] - drop_info = treeview.get_dest_row_at_pos(x, y) - before = None - after = None - if drop_info: - path, position = drop_info - if position in types: - before = items[path][3] - else: - after = items[path][3] - else: - path = (len(items) - 1,) - after = items[path][3] - file_path = urllib.unquote(selection.data).strip() - if not file_path.startswith('file:'): - return - myfile = Gio.File(uri=file_path) - file_info = myfile.query_info(Gio.FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE) - content_type = file_info.get_content_type() - if content_type == 'application/x-desktop': - input_stream = myfile.read() - (fd, tmppath) = tempfile.mkstemp(prefix='alacarte-dnd', suffix='.desktop') - os.close(fd) - f = os.open(tmppath, 'w') - f.write(input_stream.read()) - f.close() - parser = util.DesktopParser(tmppath) - self.editor.createItem(parent, parser.get('Icon'), parser.get('Name', self.editor.locale), parser.get('Comment', self.editor.locale), parser.get('Exec'), parser.get('Terminal'), before, after) - elif content_type in ('application/x-shellscript', 'application/x-executable'): - self.editor.createItem(parent, None, os.path.split(file_path)[1].strip(), None, file_path.replace('file://', '').strip(), False, before, after) - self.drag_data = None - - def on_item_tree_key_press_event(self, item_tree, event): - if event.keyval == Gdk.KEY_Delete: - self.on_edit_delete_activate(item_tree) - - def on_move_up_button_clicked(self, button): - item_tree = self.tree.get_object('item_tree') - items, iter = item_tree.get_selection().get_selected() - if not iter: - return - path = items.get_path(iter) - #at top, can't move up - if path.get_indices()[0] == 0: - return - item = items[path][3] - before = items[(path.get_indices()[0] - 1,)][3] - if isinstance(item, GMenu.TreeEntry): - self.editor.moveItem(item, item.get_parent(), before=before) - elif isinstance(item, GMenu.TreeDirectory): - self.editor.moveMenu(item, item.get_parent(), before=before) - elif isinstance(item, GMenu.TreeSeparator): - self.editor.moveSeparator(item, item.get_parent(), before=before) - - def on_move_down_button_clicked(self, button): - item_tree = self.tree.get_object('item_tree') - items, iter = item_tree.get_selection().get_selected() - if not iter: - return - path = items.get_path(iter) - #at bottom, can't move down - if path.get_indices()[0] == (len(items) - 1): - return - item = items[path][3] - after = items[path][3] - if isinstance(item, GMenu.TreeEntry): - self.editor.moveItem(item, item.get_parent(), after=after) - elif isinstance(item, GMenu.TreeDirectory): - self.editor.moveMenu(item, item.get_parent(), after=after) - elif isinstance(item, GMenu.TreeSeparator): - self.editor.moveSeparator(item, item.get_parent(), after=after) - - def on_mainwindow_undo(self, accelgroup, window, keyval, modifier): - self.editor.undo() - - def on_mainwindow_redo(self, accelgroup, window, keyval, modifier): - self.editor.redo() - - def on_help_button_clicked(self, *args): - Gtk.show_uri(Gdk.Screen.get_default(), "ghelp:user-guide#menu-editor", Gtk.get_current_event_time()) - - def on_revert_button_clicked(self, button): - dialog = self.tree.get_object('revertdialog') - dialog.set_transient_for(self.tree.get_object('mainwindow')) - dialog.show_all() - if dialog.run() == Gtk.ResponseType.YES: - self.editor.revert() - dialog.hide() - - def on_close_button_clicked(self, button): - try: - self.tree.get_object('mainwindow').hide() - except: - pass - GObject.timeout_add(10, self.quit) - - def on_properties_button_clicked(self, button): - self.on_edit_properties_activate(None) - def on_delete_button_clicked(self, button): - self.on_edit_delete_activate(None) - - def on_style_set(self, *args): - self.loadUpdates() - - def quit(self): - self.editor.quit() - Gtk.main_quit() +# -*- coding: utf-8 -*- +# vim: set noexpandtab: +# Alacarte Menu Editor - Simple fd.o Compliant Menu Editor +# Copyright (C) 2006 Travis Watkins +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Library General Public +# License as published by the Free Software Foundation; either +# version 2 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Library General Public License for more details. +# +# You should have received a copy of the GNU Library General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +from gi.repository import Gtk, GObject, Gio, GdkPixbuf, Gdk, GMenu +import cgi, os +import gettext +import subprocess +import urllib +try: + from Alacarte import config + gettext.bindtextdomain(config.GETTEXT_PACKAGE,config.localedir) + gettext.textdomain(config.GETTEXT_PACKAGE) +except: + pass +_ = gettext.gettext +from Alacarte.MenuEditor import MenuEditor +from Alacarte import util +import sys +from traceback import print_stack + +class MainWindow: + timer = None + #hack to make editing menu properties work + allow_update = True + #drag-and-drop stuff + dnd_items = [('ALACARTE_ITEM_ROW', Gtk.TargetFlags.SAME_APP, 0), ('text/plain', 0, 1)] + dnd_menus = [('ALACARTE_MENU_ROW', Gtk.TargetFlags.SAME_APP, 0)] + dnd_both = [dnd_items[0],] + dnd_menus + drag_data = None + edit_pool = [] + + def __init__(self, datadir, version, argv): + self.file_path = datadir + self.version = version + self.editor = MenuEditor() + Gtk.Window.set_default_icon_name('alacarte') + self.tree = Gtk.Builder() + self.tree.set_translation_domain(config.GETTEXT_PACKAGE) + self.tree.add_from_file(os.path.join(self.file_path, 'alacarte.ui')) + self.tree.connect_signals(self) + self.setupMenuTree() + self.setupItemTree() + self.tree.get_object('edit_delete').set_sensitive(False) + self.tree.get_object('edit_revert_to_original').set_sensitive(False) + self.tree.get_object('edit_properties').set_sensitive(False) + self.tree.get_object('move_up_button').set_sensitive(False) + self.tree.get_object('move_down_button').set_sensitive(False) + self.tree.get_object('new_separator_button').set_sensitive(False) + accelgroup = Gtk.AccelGroup() + keyval, modifier = Gtk.accelerator_parse('Z') + accelgroup.connect(keyval, modifier, Gtk.AccelFlags.VISIBLE, self.on_mainwindow_undo) + keyval, modifier = Gtk.accelerator_parse('Z') + accelgroup.connect(keyval, modifier, Gtk.AccelFlags.VISIBLE, self.on_mainwindow_redo) + keyval, modifier = Gtk.accelerator_parse('F1') + accelgroup.connect(keyval, modifier, Gtk.AccelFlags.VISIBLE, self.on_help_button_clicked) + self.tree.get_object('mainwindow').add_accel_group(accelgroup) + + def run(self): + self.loadMenus() + self.editor.applications.tree.connect("changed", self.menuChanged) + self.tree.get_object('mainwindow').show_all() + Gtk.main() + + def menuChanged(self, *a): + print >> sys.stderr, "changed!\n" + if self.timer: + GObject.source_remove(self.timer) + self.timer = None + self.timer = GObject.timeout_add(3, self.loadUpdates) + + def loadUpdates(self): + print >> sys.stderr, "%d\n" % self.editor.applications.tree.disconnect_by_func(self.menuChanged) + self.editor.reloadMenus() + self.editor.applications.tree.connect("changed", self.menuChanged) + + if not self.allow_update: + return False + menu_tree = self.tree.get_object('menu_tree') + item_tree = self.tree.get_object('item_tree') + items, iter = item_tree.get_selection().get_selected() + update_items = False + update_type = None + item_id, separator_path = None, None + if iter: + update_items = True + if isinstance(items[iter][3], GMenu.TreeEntry): + item_id = items[iter][3].get_desktop_file_id() + update_type = GMenu.TreeItemType.ENTRY + elif isinstance(items[iter][3], GMenu.TreeDirectory): + item_id = os.path.split(items[iter][3].get_desktop_file_path())[1] + update_type = GMenu.TreeItemType.DIRECTORY + elif isinstance(items[iter][3], GMenu.Tree.Separator): + item_id = items.get_path(iter) + update_type = GMenu.TreeItemType.SEPARATOR + menus, iter = menu_tree.get_selection().get_selected() + update_menus = False + menu_id = None + if iter: + if menus[iter][2].get_desktop_file_path(): + menu_id = os.path.split(menus[iter][2].get_desktop_file_path())[1] + else: + menu_id = menus[iter][2].get_menu_id() + update_menus = True + self.loadMenus() + #find current menu in new tree + if update_menus: + menu_tree.get_model().foreach(self.findMenu, menu_id) + menus, iter = menu_tree.get_selection().get_selected() + if iter: + self.on_menu_tree_cursor_changed(menu_tree) + #find current item in new list + if update_items: + i = 0 + for item in item_tree.get_model(): + found = False + if update_type != GMenu.TreeItemType.SEPARATOR: + if isinstance (item[3], GMenu.TreeEntry) and item[3].get_desktop_file_id() == item_id: + found = True + if isinstance (item[3], GMenu.TreeDirectory) and item[3].get_desktop_file_path() and update_type == GMenu.TreeItemType.DIRECTORY: + if os.path.split(item[3].get_desktop_file_path())[1] == item_id: + found = True + if isinstance(item[3], GMenu.TreeSeparator): + if not isinstance(item_id, tuple): + #we may not skip the increment via "continue" + i += 1 + continue + #separators have no id, have to find them manually + #probably won't work with two separators together + if (item_id[0] - 1,) == (i,): + found = True + elif (item_id[0] + 1,) == (i,): + found = True + elif (item_id[0],) == (i,): + found = True + if found: + item_tree.get_selection().select_path((i,)) + self.on_item_tree_cursor_changed(item_tree) + break + i += 1 + return False + + def findMenu(self, menus, path, iter, menu_id): + if not menus[path][2].get_desktop_file_path(): + if menu_id == menus[path][2].get_menu_id(): + menu_tree = self.tree.get_object('menu_tree') + menu_tree.expand_to_path(path) + menu_tree.get_selection().select_path(path) + return True + return False + if os.path.split(menus[path][2].get_desktop_file_path())[1] == menu_id: + menu_tree = self.tree.get_object('menu_tree') + menu_tree.expand_to_path(path) + menu_tree.get_selection().select_path(path) + return True + + def setupMenuTree(self): + self.menu_store = Gtk.TreeStore(GdkPixbuf.Pixbuf, str, object) + menus = self.tree.get_object('menu_tree') + column = Gtk.TreeViewColumn(_('Name')) + column.set_spacing(4) + cell = Gtk.CellRendererPixbuf() + column.pack_start(cell, False) + column.add_attribute(cell, 'pixbuf', 0) + cell = Gtk.CellRendererText() + column.pack_start(cell, True) + column.add_attribute(cell, 'markup', 1) + menus.append_column(column) + menus.enable_model_drag_source(Gdk.ModifierType.BUTTON1_MASK, self.dnd_menus, Gdk.DragAction.COPY) + menus.enable_model_drag_dest(self.dnd_both, Gdk.DragAction.PRIVATE) + menus.get_selection().set_mode(Gtk.SelectionMode.BROWSE) + + def setupItemTree(self): + items = self.tree.get_object('item_tree') + column = Gtk.TreeViewColumn(_('Show')) + cell = Gtk.CellRendererToggle() + cell.connect('toggled', self.on_item_tree_show_toggled) + column.pack_start(cell, True) + column.add_attribute(cell, 'active', 0) + #hide toggle for separators + column.set_cell_data_func(cell, self._cell_data_toggle_func) + items.append_column(column) + column = Gtk.TreeViewColumn(_('Item')) + column.set_spacing(4) + cell = Gtk.CellRendererPixbuf() + column.pack_start(cell, False) + column.add_attribute(cell, 'pixbuf', 1) + cell = Gtk.CellRendererText() + column.pack_start(cell, True) + column.add_attribute(cell, 'markup', 2) + items.append_column(column) + self.item_store = Gtk.ListStore(bool, GdkPixbuf.Pixbuf, str, object) + items.set_model(self.item_store) + items.enable_model_drag_source(Gdk.ModifierType.BUTTON1_MASK, self.dnd_items, Gdk.DragAction.COPY) + items.enable_model_drag_dest(self.dnd_items, Gdk.DragAction.PRIVATE) + + def _cell_data_toggle_func(self, tree_column, renderer, model, treeiter, data=None): + if isinstance(model[treeiter][3], GMenu.TreeSeparator): + renderer.set_property('visible', False) + else: + renderer.set_property('visible', True) + + def loadMenus(self): + self.menu_store.clear() + for menu in self.editor.getMenus(): + iters = [None]*20 + self.loadMenu(iters, menu) + menu_tree = self.tree.get_object('menu_tree') + menu_tree.set_model(self.menu_store) + for menu in self.menu_store: + #this might not work for some reason + try: + menu_tree.expand_to_path(menu.path) + except: + pass + menu_tree.get_selection().select_path((0,)) + self.on_menu_tree_cursor_changed(menu_tree) + + def loadMenu(self, iters, parent, depth=0): + if depth == 0: + icon = util.getIcon(parent) + iters[depth] = self.menu_store.append(None, (icon, cgi.escape(parent.get_name()), parent)) + depth += 1 + for menu, show in self.editor.getMenus(parent): + if show: + name = cgi.escape(menu.get_name()) + else: + name = '' + cgi.escape(menu.get_name()) + '' + icon = util.getIcon(menu) + iters[depth] = self.menu_store.append(iters[depth-1], (icon, name, menu)) + self.loadMenu(iters, menu, depth) + depth -= 1 + + def loadItems(self, menu, menu_path): + self.item_store.clear() + for item, show in self.editor.getItems(menu): + menu_icon = None + if isinstance(item, GMenu.TreeSeparator): + name = '---' + icon = None + elif isinstance(item, GMenu.TreeEntry): + app_info = item.get_app_info() + if show: + name = cgi.escape(app_info.get_display_name()) + else: + name = '' + cgi.escape(app_info.get_display_name()) + '' + icon = util.getIcon(item) + else: + if show: + name = cgi.escape(item.get_name()) + else: + name = '' + cgi.escape(item.get_name()) + '' + icon = util.getIcon(item) + self.item_store.append((show, icon, name, item)) + + #this is a little timeout callback to insert new items after + #gnome-desktop-item-edit has finished running + def waitForNewItemProcess(self, process, parent_id, file_path): + if process.poll() != None: + if os.path.isfile(file_path): + self.editor.insertExternalItem(os.path.split(file_path)[1], parent_id) + return False + return True + + def waitForNewMenuProcess(self, process, parent_id, file_path): + if process.poll() != None: + #hack for broken gnome-desktop-item-edit + broken_path = os.path.join(os.path.split(file_path)[0], '.directory') + if os.path.isfile(broken_path): + os.rename(broken_path, file_path) + if os.path.isfile(file_path): + self.editor.insertExternalMenu(os.path.split(file_path)[1], parent_id) + return False + return True + + #this callback keeps you from editing the same item twice + def waitForEditProcess(self, process, file_path): + if process.poll() != None: + self.edit_pool.remove(file_path) + return False + return True + + def on_new_menu_button_clicked(self, button): + menu_tree = self.tree.get_object('menu_tree') + menus, iter = menu_tree.get_selection().get_selected() + if not iter: + parent = menus[(0,)][2] + menu_tree.expand_to_path((0,)) + menu_tree.get_selection().select_path((0,)) + else: + parent = menus[iter][2] + file_path = os.path.join(util.getUserDirectoryPath(), util.getUniqueFileId('alacarte-made', '.directory')) + process = subprocess.Popen(['gnome-desktop-item-edit', file_path], env=os.environ) + GObject.timeout_add(100, self.waitForNewMenuProcess, process, parent.get_menu_id(), file_path) + + def on_new_item_button_clicked(self, button): + menu_tree = self.tree.get_object('menu_tree') + menus, iter = menu_tree.get_selection().get_selected() + if not iter: + parent = menus[(0,)][2] + menu_tree.expand_to_path((0,)) + menu_tree.get_selection().select_path((0,)) + else: + parent = menus[iter][2] + file_path = os.path.join(util.getUserItemPath(), util.getUniqueFileId('alacarte-made', '.desktop')) + process = subprocess.Popen(['gnome-desktop-item-edit', file_path], env=os.environ) + GObject.timeout_add(100, self.waitForNewItemProcess, process, parent.get_menu_id(), file_path) + + def on_new_separator_button_clicked(self, button): + item_tree = self.tree.get_object('item_tree') + items, iter = item_tree.get_selection().get_selected() + if not iter: + return + else: + after = items[iter][3] + menu_tree = self.tree.get_object('menu_tree') + menus, iter = menu_tree.get_selection().get_selected() + parent = menus[iter][2] + self.editor.createSeparator(parent, after=after) + + def on_edit_delete_activate(self, menu): + item_tree = self.tree.get_object('item_tree') + items, iter = item_tree.get_selection().get_selected() + if not iter: + return + item = items[iter][3] + if isinstance(item, GMenu.TreeEntry): + self.editor.deleteItem(item) + elif isinstance(item, GMenu.TreeDirectory): + self.editor.deleteMenu(item) + elif isinstance(item, GMenu.TreeSeparator): + self.editor.deleteSeparator(item) + + def on_edit_revert_to_original_activate(self, menu): + item_tree = self.tree.get_object('item_tree') + items, iter = item_tree.get_selection().get_selected() + if not iter: + return + item = items[iter][3] + if isinstance(item, GMenu.TreeEntry): + self.editor.revertItem(item) + elif isinstance(item, GMenu.TreeDirectory): + self.editor.revertMenu(item) + + def on_edit_properties_activate(self, menu): + item_tree = self.tree.get_object('item_tree') + items, iter = item_tree.get_selection().get_selected() + if not iter: + return + item = items[iter][3] + if not isinstance(item, GMenu.TreeEntry) and not isinstance(item, GMenu.TreeDirectory): + return + + if isinstance(item, GMenu.TreeEntry): + file_path = os.path.join(util.getUserItemPath(), item.get_desktop_file_id()) + file_type = 'Item' + elif isinstance(item, GMenu.TreeDirectory): + if item.get_desktop_file_path() == None: + file_path = util.getUniqueFileId('alacarte-made', '.directory') + parser = util.DesktopParser(file_path, 'Directory') + parser.set('Name', item.get_name()) + parser.set('Comment', item.get_comment()) + parser.set('Icon', item.get_icon()) + parser.write(open(file_path)) + else: + file_path = os.path.join(util.getUserDirectoryPath(), os.path.split(item.get_desktop_file_path())[1]) + file_type = 'Menu' + + if not os.path.isfile(file_path): + data = open(item.get_desktop_file_path()).read() + open(file_path, 'w').write(data) + self.editor._MenuEditor__addUndo([(file_type, os.path.split(file_path)[1]),]) + else: + self.editor._MenuEditor__addUndo([item,]) + if file_path not in self.edit_pool: + self.edit_pool.append(file_path) + process = subprocess.Popen(['gnome-desktop-item-edit', file_path], env=os.environ) + GObject.timeout_add(100, self.waitForEditProcess, process, file_path) + + def on_menu_tree_cursor_changed(self, treeview): + menus, iter = treeview.get_selection().get_selected() + menu_path = menus.get_path(iter) + item_tree = self.tree.get_object('item_tree') + item_tree.get_selection().unselect_all() + self.loadItems(self.menu_store[menu_path][2], menu_path) + self.tree.get_object('edit_delete').set_sensitive(False) + self.tree.get_object('edit_revert_to_original').set_sensitive(False) + self.tree.get_object('edit_properties').set_sensitive(False) + self.tree.get_object('move_up_button').set_sensitive(False) + self.tree.get_object('move_down_button').set_sensitive(False) + self.tree.get_object('new_separator_button').set_sensitive(False) + self.tree.get_object('properties_button').set_sensitive(False) + self.tree.get_object('delete_button').set_sensitive(False) + + def on_menu_tree_drag_data_get(self, treeview, context, selection, target_id, etime): + menus, iter = treeview.get_selection().get_selected() + self.drag_data = menus[iter][2] + + def on_menu_tree_drag_data_received(self, treeview, context, x, y, selection, info, etime): + menus = treeview.get_model() + drop_info = treeview.get_dest_row_at_pos(x, y) + if drop_info: + path, position = drop_info + types_before = (Gtk.TreeViewDropPosition.INTO_OR_BEFORE, Gtk.TreeViewDropPosition.INTO_OR_AFTER) + types_into = (Gtk.TreeViewDropPosition.INTO_OR_BEFORE, Gtk.TreeViewDropPosition.INTO_OR_AFTER) + types_after = (Gtk.TreeViewDropPosition.AFTER, Gtk.TreeViewDropPosition.INTO_OR_AFTER) + if position not in types: + context.finish(False, False, etime) + return False + if selection.target in ('ALACARTE_ITEM_ROW', 'ALACARTE_MENU_ROW'): + if self.drag_data == None: + return False + item = self.drag_data + new_parent = menus[path][2] + treeview.get_selection().select_path(path) + if isinstance(item, GMenu.TreeEntry): + self.editor.copyItem(item, new_parent) + elif isinstance(item, GMenu.TreeDirectory): + if self.editor.moveMenu(item, new_parent) == False: + self.loadUpdates() + elif isinstance(item, GMenu.TreeSeparator): + self.editor.moveSeparator(item, new_parent) + else: + context.finish(False, False, etime) + context.finish(True, True, etime) + self.drag_data = None + + def on_item_tree_show_toggled(self, cell, path): + item = self.item_store[path][3] + if isinstance(item, GMenu.TreeSeparator): + return + if self.item_store[path][0]: + self.editor.setVisible(item, False) + else: + self.editor.setVisible(item, True) + self.item_store[path][0] = not self.item_store[path][0] + + def on_item_tree_cursor_changed(self, treeview): + items, iter = treeview.get_selection().get_selected() + if iter is None: + return + item = items[iter][3] + self.tree.get_object('edit_delete').set_sensitive(True) + self.tree.get_object('new_separator_button').set_sensitive(True) + self.tree.get_object('delete_button').set_sensitive(True) + if self.editor.canRevert(item): + self.tree.get_object('edit_revert_to_original').set_sensitive(True) + else: + self.tree.get_object('edit_revert_to_original').set_sensitive(False) + if not isinstance(item, GMenu.TreeSeparator): + self.tree.get_object('edit_properties').set_sensitive(True) + self.tree.get_object('properties_button').set_sensitive(True) + else: + self.tree.get_object('edit_properties').set_sensitive(False) + self.tree.get_object('properties_button').set_sensitive(False) + + # If first item... + if items.get_path(iter).get_indices()[0] == 0: + self.tree.get_object('move_up_button').set_sensitive(False) + else: + self.tree.get_object('move_up_button').set_sensitive(True) + + # If last item... + if items.get_path(iter).get_indices()[0] == (len(items)-1): + self.tree.get_object('move_down_button').set_sensitive(False) + else: + self.tree.get_object('move_down_button').set_sensitive(True) + + def on_item_tree_row_activated(self, treeview, path, column): + self.on_edit_properties_activate(None) + + def on_item_tree_popup_menu(self, item_tree, event=None): + model, iter = item_tree.get_selection().get_selected() + if event: + #don't show if it's not the right mouse button + if event.button != 3: + return + button = event.button + event_time = event.time + info = item_tree.get_path_at_pos(int(event.x), int(event.y)) + if info != None: + path, col, cellx, celly = info + item_tree.grab_focus() + item_tree.set_cursor(path, col, 0) + else: + path = model.get_path(iter) + button = 0 + event_time = 0 + item_tree.grab_focus() + item_tree.set_cursor(path, item_tree.get_columns()[0], 0) + popup = self.tree.get_object('edit_menu') + popup.popup(None, None, None, button, event_time) + #without this shift-f10 won't work + return True + + def on_item_tree_drag_data_get(self, treeview, context, selection, target_id, etime): + items, iter = treeview.get_selection().get_selected() + self.drag_data = items[iter][3] + + def on_item_tree_drag_data_received(self, treeview, context, x, y, selection, info, etime): + items = treeview.get_model() + types = (Gtk.TreeViewDropPosition.BEFORE, Gtk.TreeViewDropPosition.INTO_OR_BEFORE) + if selection.target == 'ALACARTE_ITEM_ROW': + drop_info = treeview.get_dest_row_at_pos(x, y) + before = None + after = None + if self.drag_data == None: + return False + item = self.drag_data + # by default we assume, that the items stays in the same menu + destination = item.get_parent() + if drop_info: + path, position = drop_info + target = items[path][3] + # move the item to the directory, if the item was dropped into it + if isinstance(target, GMenu.TreeDirectory) and (position in types_into): + # append the selected item to the choosen menu + destination = target + elif position in types_before: + before = target + elif position in types_after: + after = target + else: + # this does not happen + pass + else: + path = (len(items) - 1,) + after = items[path][3] + if isinstance(item, GMenu.TreeEntry): + self.editor.moveItem(item, destination, before, after) + elif isinstance(item, GMenu.TreeDirectory): + if self.editor.moveMenu(item, destination, before, after) == False: + self.loadUpdates() + elif isinstance(item, GMenu.TreeSeparator): + self.editor.moveSeparator(item, destination, before, after) + context.finish(True, True, etime) + elif selection.target == 'text/plain': + if selection.data == None: + return False + menus, iter = self.tree.get_object('menu_tree').get_selection().get_selected() + parent = menus[iter][2] + drop_info = treeview.get_dest_row_at_pos(x, y) + before = None + after = None + if drop_info: + path, position = drop_info + if position in types: + before = items[path][3] + else: + after = items[path][3] + else: + path = (len(items) - 1,) + after = items[path][3] + file_path = urllib.unquote(selection.data).strip() + if not file_path.startswith('file:'): + return + myfile = Gio.File(uri=file_path) + file_info = myfile.query_info(Gio.FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE) + content_type = file_info.get_content_type() + if content_type == 'application/x-desktop': + input_stream = myfile.read() + (fd, tmppath) = tempfile.mkstemp(prefix='alacarte-dnd', suffix='.desktop') + os.close(fd) + f = os.open(tmppath, 'w') + f.write(input_stream.read()) + f.close() + parser = util.DesktopParser(tmppath) + self.editor.createItem(parent, parser.get('Icon'), parser.get('Name', self.editor.locale), parser.get('Comment', self.editor.locale), parser.get('Exec'), parser.get('Terminal'), before, after) + elif content_type in ('application/x-shellscript', 'application/x-executable'): + self.editor.createItem(parent, None, os.path.split(file_path)[1].strip(), None, file_path.replace('file://', '').strip(), False, before, after) + self.drag_data = None + + def on_item_tree_key_press_event(self, item_tree, event): + if event.keyval == Gdk.KEY_Delete: + self.on_edit_delete_activate(item_tree) + + def on_move_up_button_clicked(self, button): + item_tree = self.tree.get_object('item_tree') + items, iter = item_tree.get_selection().get_selected() + if not iter: + return + path = items.get_path(iter) + #at top, can't move up + if path.get_indices()[0] == 0: + return + item = items[path][3] + before = items[(path.get_indices()[0] - 1,)][3] + if isinstance(item, GMenu.TreeEntry): + self.editor.moveItem(item, item.get_parent(), before=before) + elif isinstance(item, GMenu.TreeDirectory): + self.editor.moveMenu(item, item.get_parent(), before=before) + elif isinstance(item, GMenu.TreeSeparator): + self.editor.moveSeparator(item, item.get_parent(), before=before) + + def on_move_down_button_clicked(self, button): + item_tree = self.tree.get_object('item_tree') + items, iter = item_tree.get_selection().get_selected() + if not iter: + return + path = items.get_path(iter) + #at bottom, can't move down + if path.get_indices()[0] == (len(items) - 1): + return + item = items[path][3] + after = items[path][3] + if isinstance(item, GMenu.TreeEntry): + self.editor.moveItem(item, item.get_parent(), after=after) + elif isinstance(item, GMenu.TreeDirectory): + self.editor.moveMenu(item, item.get_parent(), after=after) + elif isinstance(item, GMenu.TreeSeparator): + self.editor.moveSeparator(item, item.get_parent(), after=after) + + def on_mainwindow_undo(self, accelgroup, window, keyval, modifier): + self.editor.undo() + + def on_mainwindow_redo(self, accelgroup, window, keyval, modifier): + self.editor.redo() + + def on_help_button_clicked(self, *args): + Gtk.show_uri(Gdk.Screen.get_default(), "ghelp:user-guide#menu-editor", Gtk.get_current_event_time()) + + def on_revert_button_clicked(self, button): + dialog = self.tree.get_object('revertdialog') + dialog.set_transient_for(self.tree.get_object('mainwindow')) + dialog.show_all() + if dialog.run() == Gtk.ResponseType.YES: + self.editor.revert() + dialog.hide() + + def on_close_button_clicked(self, button): + try: + self.tree.get_object('mainwindow').hide() + except: + pass + GObject.timeout_add(10, self.quit) + + def on_properties_button_clicked(self, button): + self.on_edit_properties_activate(None) + def on_delete_button_clicked(self, button): + self.on_edit_delete_activate(None) + + def on_style_set(self, *args): + self.loadUpdates() + + def quit(self): + self.editor.quit() + Gtk.main_quit() diff --git a/Alacarte/MenuEditor.py b/Alacarte/MenuEditor.py dissimilarity index 95% index 3ca8a0e..21255ba 100644 --- a/Alacarte/MenuEditor.py +++ b/Alacarte/MenuEditor.py @@ -1,821 +1,821 @@ -# -*- coding: utf-8 -*- -# Alacarte Menu Editor - Simple fd.o Compliant Menu Editor -# Copyright (C) 2006 Travis Watkins, Heinrich Wendel -# -# This library is free software; you can redistribute it and/or -# modify it under the terms of the GNU Library General Public -# License as published by the Free Software Foundation; either -# version 2 of the License, or (at your option) any later version. -# -# This library is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -# Library General Public License for more details. -# -# You should have received a copy of the GNU Library General Public -# License along with this library; if not, write to the Free Software -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - -import os, sys, re, xml.dom.minidom, locale -from gi.repository import GMenu -from Alacarte import util - -class Menu: - tree = None - visible_tree = None - path = None - dom = None - -class MenuEditor: - #lists for undo/redo functionality - __undo = [] - __redo = [] - - def __init__(self): - self.locale = locale.getdefaultlocale()[0] - self.__loadMenus() - - def reloadMenus(self): - self.applications = Menu() - self.applications.tree = GMenu.Tree.new('applications.menu', GMenu.TreeFlags.SHOW_EMPTY|GMenu.TreeFlags.INCLUDE_EXCLUDED|GMenu.TreeFlags.INCLUDE_NODISPLAY|GMenu.TreeFlags.SHOW_ALL_SEPARATORS|GMenu.TreeFlags.SORT_DISPLAY_NAME) - if not self.applications.tree.load_sync(): - self.applications = None - return - self.applications.visible_tree = GMenu.Tree.new('applications.menu', GMenu.TreeFlags.SORT_DISPLAY_NAME) - if not self.applications.visible_tree.load_sync(): - self.applications = None - return - self.applications.path = os.path.join(util.getUserMenuPath(), self.applications.tree.props.menu_basename) - if not os.path.isfile(self.applications.path): - self.applications.dom = xml.dom.minidom.parseString(util.getUserMenuXml(self.applications.tree)) - else: - self.applications.dom = xml.dom.minidom.parse(self.applications.path) - self.__remove_whilespace_nodes(self.applications.dom) - - def __menuChanged(self, *a): - print >> sys.stderr, "changed!\n" - self.applications.visible_tree.load_sync() - - def __loadMenus(self): - self.reloadMenus(); - self.save(True) - self.applications.visible_tree.connect("changed", self.__menuChanged) - - def save(self, from_loading=False): - for menu in ('applications',): - fd = open(getattr(self, menu).path, 'w') - fd.write(re.sub("\n[\s]*([^\n<]*)\n[\s]*\n', ''))) - fd.close() - if not from_loading: - self.__loadMenus() - - def quit(self): - for file_name in os.listdir(util.getUserItemPath()): - if file_name[-6:-2] in ('redo', 'undo'): - file_path = os.path.join(util.getUserItemPath(), file_name) - os.unlink(file_path) - for file_name in os.listdir(util.getUserDirectoryPath()): - if file_name[-6:-2] in ('redo', 'undo'): - file_path = os.path.join(util.getUserDirectoryPath(), file_name) - os.unlink(file_path) - for file_name in os.listdir(util.getUserMenuPath()): - if file_name[-6:-2] in ('redo', 'undo'): - file_path = os.path.join(util.getUserMenuPath(), file_name) - os.unlink(file_path) - - def revert(self): - for name in ('applications',): - menu = getattr(self, name) - self.revertTree(menu.tree.get_root_directory()) - path = os.path.join(util.getUserMenuPath(), os.path.basename(menu.tree.get_canonical_menu_path())) - try: - os.unlink(path) - except OSError: - pass - #reload DOM for each menu - if not os.path.isfile(menu.path): - menu.dom = xml.dom.minidom.parseString(util.getUserMenuXml(menu.tree)) - else: - menu.dom = xml.dom.minidom.parse(menu.path) - self.__remove_whilespace_nodes(menu.dom) - #reset undo/redo, no way to recover from this - self.__undo, self.__redo = [], [] - self.save() - - def revertTree(self, menu): - item_iter = menu.iter() - item_type = item_iter.next() - while item_type != GMenu.TreeItemType.INVALID: - if item_type == GMenu.TreeItemType.DIRECTORY: - item = item_iter.get_directory(); - self.revertTree(item) - elif item_type == GMenu.TreeItemType.ENTRY: - item = item_iter.get_entry(); - self.revertItem(item) - item_type = item_iter.next() - self.revertMenu(menu) - - def undo(self): - if len(self.__undo) == 0: - return - files = self.__undo.pop() - redo = [] - for file_path in files: - new_path = file_path.rsplit('.', 1)[0] - redo_path = util.getUniqueRedoFile(new_path) - data = open(new_path).read() - open(redo_path, 'w').write(data) - data = open(file_path).read() - open(new_path, 'w').write(data) - os.unlink(file_path) - redo.append(redo_path) - #reload DOM to make changes stick - for name in ('applications',): - menu = getattr(self, name) - if not os.path.isfile(menu.path): - menu.dom = xml.dom.minidom.parseString(util.getUserMenuXml(menu.tree)) - else: - menu.dom = xml.dom.minidom.parse(menu.path) - self.__remove_whilespace_nodes(menu.dom) - self.__redo.append(redo) - - def redo(self): - if len(self.__redo) == 0: - return - files = self.__redo.pop() - undo = [] - for file_path in files: - new_path = file_path.rsplit('.', 1)[0] - undo_path = util.getUniqueUndoFile(new_path) - data = open(new_path).read() - open(undo_path, 'w').write(data) - data = open(file_path).read() - open(new_path, 'w').write(data) - os.unlink(file_path) - undo.append(undo_path) - #reload DOM to make changes stick - for name in ('applications',): - menu = getattr(self, name) - if not os.path.isfile(menu.path): - menu.dom = xml.dom.minidom.parseString(util.getUserMenuXml(menu.tree)) - else: - menu.dom = xml.dom.minidom.parse(menu.path) - self.__remove_whilespace_nodes(menu.dom) - self.__undo.append(undo) - - def getMenus(self, parent=None): - if parent == None: - yield self.applications.tree.get_root_directory() - else: - item_iter = parent.iter() - item_type = item_iter.next() - while item_type != GMenu.TreeItemType.INVALID: - if item_type == GMenu.TreeItemType.DIRECTORY: - item = item_iter.get_directory() - yield (item, self.__isVisible(item)) - item_type = item_iter.next() - - def getContents(self, item): - contents = [] - item_iter = item.iter() - item_type = item_iter.next() - - while item_type != GMenu.TreeItemType.INVALID: - if item_type == GMenu.TreeItemType.DIRECTORY: - item = item_iter.get_directory() - elif item_type == GMenu.TreeItemType.ENTRY: - item = item_iter.get_entry() - elif item_type == GMenu.TreeItemType.HEADER: - item = item_iter.get_header() - elif item_type == GMenu.TreeItemType.ALIAS: - item = item_iter.get_alias() - if item: - contents.append(item) - item_type = item_iter.next() - return contents; - - def getItems(self, menu): - item_iter = menu.iter() - item_type = item_iter.next() - while item_type != GMenu.TreeItemType.INVALID: - if item_type == GMenu.TreeItemType.SEPARATOR: - item = item_iter.get_separator() - yield (item, True) - else: - if item_type == GMenu.TreeItemType.ENTRY: - item = item_iter.get_entry() - if item.get_desktop_file_id()[-19:] == '-usercustom.desktop': - continue - elif item_type == GMenu.TreeItemType.DIRECTORY: - item = item_iter.get_directory() - elif item_type == GMenu.TreeItemType.HEADER: - item = item_iter.get_header() - elif item_type == GMenu.TreeItemType.ALIAS: - item = item_iter.get_alias() - yield (item, self.__isVisible(item)) - item_type = item_iter.next() - - def canRevert(self, item): - if isinstance(item, GMenu.TreeEntry): - if util.getItemPath(item.get_desktop_file_id()): - path = util.getUserItemPath() - if os.path.isfile(os.path.join(path, item.get_desktop_file_id())): - return True - elif isinstance(item, GMenu.TreeDirectory): - if item.get_desktop_file_path(): - file_id = os.path.split(item.get_desktop_file_path())[1] - else: - file_id = item.get_menu_id() + '.directory' - if util.getDirectoryPath(file_id): - path = util.getUserDirectoryPath() - if os.path.isfile(os.path.join(path, file_id)): - return True - return False - - def setVisible(self, item, visible): - dom = self.__getMenu(item).dom - if isinstance(item, GMenu.TreeEntry): - self.__addUndo([self.__getMenu(item), item]) - menu_xml = self.__getXmlMenu(self.__getPath(item.get_parent()), dom, dom) - if visible: - self.__addXmlFilename(menu_xml, dom, item.get_desktop_file_id(), 'Include') - self.__writeItem(item, no_display=False) - else: - self.__addXmlFilename(menu_xml, dom, item.get_desktop_file_id(), 'Exclude') - self.__addXmlTextElement(menu_xml, 'AppDir', util.getUserItemPath(), dom) - elif isinstance(item, GMenu.TreeDirectory): - self.__addUndo([self.__getMenu(item), item]) - item_iter = item.iter() - first_child_type = item_iter.next() - #don't mess with it if it's empty - if first_child_type == GMenu.TreeItemType.INVALID: - return - menu_xml = self.__getXmlMenu(self.__getPath(item), dom, dom) - for node in self.__getXmlNodesByName(['Deleted', 'NotDeleted'], menu_xml): - node.parentNode.removeChild(node) - if visible: - self.__writeMenu(item, no_display=False) - else: - self.__writeMenu(item, no_display=True) - self.__addXmlTextElement(menu_xml, 'DirectoryDir', util.getUserDirectoryPath(), dom) - self.save() - - def createItem(self, parent, icon, name, comment, command, use_term, before=None, after=None): - file_id = self.__writeItem(None, icon, name, comment, command, use_term) - self.insertExternalItem(file_id, parent.get_menu_id(), before, after) - - def insertExternalItem(self, file_id, parent_id, before=None, after=None): - self.applications.tree.get_root_directory() - parent = self.__findMenu(parent_id) - self.applications.tree.get_root_directory() - dom = self.__getMenu(parent).dom - self.applications.tree.get_root_directory() - self.__addItem(parent, file_id, dom) - self.applications.tree.get_root_directory() - self.__positionItem(parent, ('Item', file_id), before, after) - self.applications.tree.get_root_directory() - self.__addUndo([self.__getMenu(parent), ('Item', file_id)]) - self.applications.tree.get_root_directory() - self.save() - self.applications.tree.get_root_directory() - - def createMenu(self, parent, icon, name, comment, before=None, after=None): - file_id = self.__writeMenu(None, icon, name, comment) - self.insertExternalMenu(file_id, parent.get_menu_id(), before, after) - - def insertExternalMenu(self, file_id, parent_id, before=None, after=None): - menu_id = file_id.rsplit('.', 1)[0] - parent = self.__findMenu(parent_id) - dom = self.__getMenu(parent).dom - self.__addXmlDefaultLayout(self.__getXmlMenu(self.__getPath(parent), dom, dom) , dom) - menu_xml = self.__getXmlMenu(self.__getPath(parent) + '/' + menu_id, dom, dom) - self.__addXmlTextElement(menu_xml, 'Directory', file_id, dom) - self.__positionItem(parent, ('Menu', menu_id), before, after) - self.__addUndo([self.__getMenu(parent), ('Menu', file_id)]) - self.save() - - def createSeparator(self, parent, before=None, after=None): - self.__positionItem(parent, ('Separator',), before, after) - self.__addUndo([self.__getMenu(parent), ('Separator',)]) - self.save() - - def editItem(self, item, icon, name, comment, command, use_term, parent=None, final=True): - #if nothing changed don't make a user copy - app_info = item.get_app_info() - if icon == app_info.get_icon() and name == app_info.get_display_name() and comment == item.get_comment() and command == item.get_exec() and use_term == item.get_launch_in_terminal(): - return - #hack, item.get_parent() seems to fail a lot - if not parent: - parent = item.get_parent() - if final: - self.__addUndo([self.__getMenu(parent), item]) - self.__writeItem(item, icon, name, comment, command, use_term) - if final: - dom = self.__getMenu(parent).dom - menu_xml = self.__getXmlMenu(self.__getPath(parent), dom, dom) - self.__addXmlTextElement(menu_xml, 'AppDir', util.getUserItemPath(), dom) - self.save() - - def editMenu(self, menu, icon, name, comment, final=True): - #if nothing changed don't make a user copy - if icon == menu.get_icon() and name == menu.get_name() and comment == menu.get_comment(): - return - #we don't use this, we just need to make sure the exists - #otherwise changes won't show up - dom = self.__getMenu(menu).dom - menu_xml = self.__getXmlMenu(self.__getPath(menu), dom, dom) - file_id = self.__writeMenu(menu, icon, name, comment) - if final: - self.__addXmlTextElement(menu_xml, 'DirectoryDir', util.getUserDirectoryPath(), dom) - self.__addUndo([self.__getMenu(menu), menu]) - self.save() - - def copyItem(self, item, new_parent, before=None, after=None): - dom = self.__getMenu(new_parent).dom - file_path = item.get_desktop_file_path() - keyfile = util.DesktopParser(file_path) - #erase Categories in new file - keyfile.set('Categories', ('',)) - keyfile.set('Hidden', False) - app_info = item.get_info() - file_id = util.getUniqueFileId(app_info.get_name().replace(os.sep, '-'), '.desktop') - out_path = os.path.join(util.getUserItemPath(), file_id) - keyfile.write(open(out_path, 'w')) - self.__addItem(new_parent, file_id, dom) - self.__positionItem(new_parent, ('Item', file_id), before, after) - self.__addUndo([self.__getMenu(new_parent), ('Item', file_id)]) - self.save() - return file_id - - def moveItem(self, item, new_parent, before=None, after=None): - undo = [] - if item.get_parent() != new_parent: - #hide old item - self.deleteItem(item) - undo.append(item) - file_id = self.copyItem(item, new_parent) - item = ('Item', file_id) - undo.append(item) - self.__positionItem(new_parent, item, before, after) - undo.append(self.__getMenu(new_parent)) - self.__addUndo(undo) - self.save() - - def moveMenu(self, menu, new_parent, before=None, after=None): - parent = new_parent - #don't move a menu into it's child - while parent.get_parent(): - parent = parent.get_parent() - if parent == menu: - return False - - #don't move a menu into itself - if new_parent == menu: - return False - - #can't move between top-level menus - if self.__getMenu(menu) != self.__getMenu(new_parent): - return False - if menu.get_parent() != new_parent: - dom = self.__getMenu(menu).dom - root_path = self.__getPath(menu).split('/', 1)[0] - xml_root = self.__getXmlMenu(root_path, dom, dom) - old_path = self.__getPath(menu).split('/', 1)[1] - #root menu's path has no / - if '/' in self.__getPath(new_parent): - new_path = self.__getPath(new_parent).split('/', 1)[1] + '/' + menu.get_menu_id() - else: - new_path = menu.get_menu_id() - self.__addXmlMove(xml_root, old_path, new_path, dom) - self.__positionItem(new_parent, menu, before, after) - self.__addUndo([self.__getMenu(new_parent),]) - self.save() - - def moveSeparator(self, separator, new_parent, before=None, after=None): - undo = [] - # remove the original separator if its parent is not the new destination - if separator.get_parent() != new_parent: - self.deleteSeparator(separator) - undo.append(separator) - # this adds the new separator to the specified position - self.__positionItem(new_parent, separator, before, after) - undo.append(self.__getMenu(new_parent)) - self.__addUndo(undo) - self.save() - - def deleteItem(self, item): - self.__addUndo([item,]) - self.__writeItem(item, hidden=True) - self.save() - - def deleteMenu(self, menu): - dom = self.__getMenu(menu).dom - menu_xml = self.__getXmlMenu(self.__getPath(menu), dom, dom) - self.__addDeleted(menu_xml, dom) - self.__addUndo([self.__getMenu(menu),]) - self.save() - - def deleteSeparator(self, item): - parent = item.get_parent() - contents = self.getContents(parent) - contents.remove(item) - layout = self.__createLayout(contents) - dom = self.__getMenu(parent).dom - menu_xml = self.__getXmlMenu(self.__getPath(parent), dom, dom) - self.__addXmlLayout(menu_xml, layout, dom) - self.__addUndo([self.__getMenu(item.get_parent()),]) - self.save() - - def revertItem(self, item): - if not self.canRevert(item): - return - self.__addUndo([item,]) - try: - os.remove(item.get_desktop_file_path()) - except OSError: - pass - self.save() - - def revertMenu(self, menu): - if not self.canRevert(menu): - return - #wtf happened here? oh well, just bail - if not menu.get_desktop_file_path(): - return - self.__addUndo([menu,]) - file_id = os.path.split(menu.get_desktop_file_path())[1] - path = os.path.join(util.getUserDirectoryPath(), file_id) - try: - os.remove(path) - except OSError: - pass - self.save() - - #private stuff - def __addUndo(self, items): - self.__undo.append([]) - for item in items: - if isinstance(item, Menu): - file_path = item.path - elif isinstance(item, tuple): - if item[0] == 'Item': - file_path = os.path.join(util.getUserItemPath(), item[1]) - if not os.path.isfile(file_path): - file_path = util.getItemPath(item[1]) - elif item[0] == 'Menu': - file_path = os.path.join(util.getUserDirectoryPath(), item[1]) - if not os.path.isfile(file_path): - file_path = util.getDirectoryPath(item[1]) - else: - continue - elif isinstance(item, GMenu.TreeDirectory): - if item.get_desktop_file_path() == None: - continue - file_path = os.path.join(util.getUserDirectoryPath(), os.path.split(item.get_desktop_file_path())[1]) - if not os.path.isfile(file_path): - file_path = item.get_desktop_file_path() - elif isinstance(item, GMenu.TreeEntry): - file_path = os.path.join(util.getUserItemPath(), item.get_desktop_file_id()) - if not os.path.isfile(file_path): - file_path = item.get_desktop_file_path() - else: - continue - data = open(file_path).read() - undo_path = util.getUniqueUndoFile(file_path) - open(undo_path, 'w').write(data) - self.__undo[-1].append(undo_path) - - def __getMenu(self, item): - return self.applications - - def __findMenu(self, menu_id, parent=None): - root_directory = self.applications.tree.get_root_directory() - if parent == None and root_directory != None: - return self.__findMenu(menu_id, root_directory) - if menu_id == root_directory.get_menu_id(): - return root_directory - item_iter = parent.iter() - item_type = item_iter.next() - while item_type != GMenu.TreeItemType.INVALID: - if item_type == GMenu.TreeItemType.DIRECTORY: - item = item_iter.get_directory() - if item.get_menu_id() == menu_id: - return item - menu = self.__findMenu(menu_id, item) - if menu != None: - return menu - item_type = item_iter.next() - - def __isVisible(self, item): - if isinstance(item, GMenu.TreeEntry): - app_info = item.get_app_info() - return not (item.get_is_excluded() or app_info.get_nodisplay()) - menu = self.__getMenu(item) - if menu == self.applications: - root = self.applications.visible_tree.get_root_directory() - if isinstance(item, GMenu.TreeDirectory): - if self.__findMenu(item.get_menu_id(), root) == None: - return False - return True - - def __getPath(self, menu, path=None): - if not path: - path = menu.get_menu_id() - if menu.get_parent(): - path = self.__getPath(menu.get_parent(), path) - path += '/' - path += menu.get_menu_id() - print "%s\n" % path - return path - - def __getXmlMenu(self, path, element, dom): - if '/' in path: - (name, path) = path.split('/', 1) - else: - name = path - path = '' - - found = None - for node in self.__getXmlNodesByName('Menu', element): - for child in self.__getXmlNodesByName('Name', node): - if child.childNodes[0].nodeValue == name: - if path: - found = self.__getXmlMenu(path, node, dom) - else: - found = node - break - if found: - break - if not found: - node = self.__addXmlMenuElement(element, name, dom) - if path: - found = self.__getXmlMenu(path, node, dom) - else: - found = node - - return found - - def __addXmlMenuElement(self, element, name, dom): - node = dom.createElement('Menu') - self.__addXmlTextElement(node, 'Name', name, dom) - return element.appendChild(node) - - def __addXmlTextElement(self, element, name, text, dom): - for temp in element.childNodes: - if temp.nodeName == name: - if temp.childNodes[0].nodeValue == text: - return - node = dom.createElement(name) - text = dom.createTextNode(text) - node.appendChild(text) - return element.appendChild(node) - - def __addXmlFilename(self, element, dom, filename, type = 'Include'): - # remove old filenames - for node in self.__getXmlNodesByName(['Include', 'Exclude'], element): - if node.childNodes[0].nodeName == 'Filename' and node.childNodes[0].childNodes[0].nodeValue == filename: - element.removeChild(node) - - # add new filename - node = dom.createElement(type) - node.appendChild(self.__addXmlTextElement(node, 'Filename', filename, dom)) - return element.appendChild(node) - - def __addDeleted(self, element, dom): - node = dom.createElement('Deleted') - return element.appendChild(node) - - def __writeItem(self, item=None, icon=None, name=None, comment=None, command=None, use_term=None, no_display=None, startup_notify=None, hidden=None): - if item: - file_path = item.get_desktop_file_path() - file_id = item.get_desktop_file_id() - keyfile = util.DesktopParser(file_path) - elif item == None and name == None: - raise Exception('New menu items need a name') - else: - file_id = util.getUniqueFileId(name, '.desktop') - keyfile = util.DesktopParser() - if icon: - keyfile.set('Icon', icon) - keyfile.set('Icon', icon, self.locale) - if name: - keyfile.set('Name', name) - keyfile.set('Name', name, self.locale) - if comment: - keyfile.set('Comment', comment) - keyfile.set('Comment', comment, self.locale) - if command: - keyfile.set('Exec', command) - if use_term != None: - keyfile.set('Terminal', use_term) - if no_display != None: - keyfile.set('NoDisplay', no_display) - if startup_notify != None: - keyfile.set('StartupNotify', startup_notify) - if hidden != None: - keyfile.set('Hidden', hidden) - out_path = os.path.join(util.getUserItemPath(), file_id) - keyfile.write(open(out_path, 'w')) - return file_id - - def __writeMenu(self, menu=None, icon=None, name=None, comment=None, no_display=None): - if menu: - file_id = os.path.split(menu.get_desktop_file_path())[1] - file_path = menu.get_desktop_file_path() - keyfile = util.DesktopParser(file_path) - elif menu == None and name == None: - raise Exception('New menus need a name') - else: - file_id = util.getUniqueFileId(name, '.directory') - keyfile = util.DesktopParser(file_type='Directory') - if icon: - keyfile.set('Icon', icon) - if name: - keyfile.set('Name', name) - keyfile.set('Name', name, self.locale) - if comment: - keyfile.set('Comment', comment) - keyfile.set('Comment', comment, self.locale) - if no_display != None: - keyfile.set('NoDisplay', no_display) - out_path = os.path.join(util.getUserDirectoryPath(), file_id) - keyfile.write(open(out_path, 'w')) - return file_id - - def __getXmlNodesByName(self, name, element): - for child in element.childNodes: - if child.nodeType == xml.dom.Node.ELEMENT_NODE: - if isinstance(name, str) and child.nodeName == name: - yield child - elif isinstance(name, list) or isinstance(name, tuple): - if child.nodeName in name: - yield child - - def __remove_whilespace_nodes(self, node): - remove_list = [] - for child in node.childNodes: - if child.nodeType == xml.dom.minidom.Node.TEXT_NODE: - child.data = child.data.strip() - if not child.data.strip(): - remove_list.append(child) - elif child.hasChildNodes(): - self.__remove_whilespace_nodes(child) - for node in remove_list: - node.parentNode.removeChild(node) - - def __addXmlMove(self, element, old, new, dom): - if not self.__undoMoves(element, old, new, dom): - node = dom.createElement('Move') - node.appendChild(self.__addXmlTextElement(node, 'Old', old, dom)) - node.appendChild(self.__addXmlTextElement(node, 'New', new, dom)) - #are parsed in reverse order, need to put at the beginning - return element.insertBefore(node, element.firstChild) - - def __addXmlLayout(self, element, layout, dom): - # remove old layout - for node in self.__getXmlNodesByName('Layout', element): - element.removeChild(node) - - # add new layout - node = dom.createElement('Layout') - for order in layout.order: - if order[0] == 'Separator': - child = dom.createElement('Separator') - node.appendChild(child) - elif order[0] == 'Filename': - child = self.__addXmlTextElement(node, 'Filename', order[1], dom) - elif order[0] == 'Menuname': - child = self.__addXmlTextElement(node, 'Menuname', order[1], dom) - elif order[0] == 'Merge': - child = dom.createElement('Merge') - child.setAttribute('type', order[1]) - node.appendChild(child) - return element.appendChild(node) - - def __addXmlDefaultLayout(self, element, dom): - # remove old default layout - for node in self.__getXmlNodesByName('DefaultLayout', element): - element.removeChild(node) - - # add new layout - node = dom.createElement('DefaultLayout') - node.setAttribute('inline', 'false') - return element.appendChild(node) - - def __createLayout(self, items): - layout = Layout() - layout.order = [] - - layout.order.append(['Merge', 'menus']) - for item in items: - if isinstance(item, tuple): - if item[0] == 'Separator': - layout.parseSeparator() - elif item[0] == 'Menu': - layout.parseMenuname(item[1]) - elif item[0] == 'Item': - layout.parseFilename(item[1]) - elif isinstance(item, GMenu.TreeDirectory): - layout.parseMenuname(item.get_menu_id()) - elif isinstance(item, GMenu.TreeEntry): - layout.parseFilename(item.get_desktop_file_id()) - elif isinstance(item, GMenu.TreeSeparator): - layout.parseSeparator() - layout.order.append(['Merge', 'files']) - return layout - - def __addItem(self, parent, file_id, dom): - xml_parent = self.__getXmlMenu(self.__getPath(parent), dom, dom) - self.__addXmlFilename(xml_parent, dom, file_id, 'Include') - - def __deleteItem(self, parent, file_id, dom, before=None, after=None): - xml_parent = self.__getXmlMenu(self.__getPath(parent), dom, dom) - self.__addXmlFilename(xml_parent, dom, file_id, 'Exclude') - - def __positionItem(self, parent, item, before=None, after=None): - contents = self.getContents(parent) - if after: - index = contents.index(after) + 1 - elif before: - index = contents.index(before) - else: - # append the item to the list - index = len(contents) - #if this is a move to a new parent you can't remove the item - if item in contents: - # decrease the destination index, if we shorten the list - if (before and (contents.index(item) < index)) \ - or (after and (contents.index(item) < index - 1)): - index -= 1 - contents.remove(item) - contents.insert(index, item) - layout = self.__createLayout(contents) - dom = self.__getMenu(parent).dom - menu_xml = self.__getXmlMenu(self.__getPath(parent), dom, dom) - self.__addXmlLayout(menu_xml, layout, dom) - - def __undoMoves(self, element, old, new, dom): - nodes = [] - matches = [] - original_old = old - final_old = old - #get all elements - for node in self.__getXmlNodesByName(['Move'], element): - nodes.insert(0, node) - #if the matches our old parent we've found a stage to undo - for node in nodes: - xml_old = node.getElementsByTagName('Old')[0] - xml_new = node.getElementsByTagName('New')[0] - if xml_new.childNodes[0].nodeValue == old: - matches.append(node) - #we should end up with this path when completed - final_old = xml_old.childNodes[0].nodeValue - #undoing s - for node in matches: - element.removeChild(node) - if len(matches) > 0: - for node in nodes: - xml_old = node.getElementsByTagName('Old')[0] - xml_new = node.getElementsByTagName('New')[0] - path = os.path.split(xml_new.childNodes[0].nodeValue) - if path[0] == original_old: - element.removeChild(node) - for node in dom.getElementsByTagName('Menu'): - name_node = node.getElementsByTagName('Name')[0] - name = name_node.childNodes[0].nodeValue - if name == os.path.split(new)[1]: - #copy app and dir directory info from old - root_path = dom.getElementsByTagName('Menu')[0].getElementsByTagName('Name')[0].childNodes[0].nodeValue - xml_menu = self.__getXmlMenu(root_path + '/' + new, dom, dom) - for app_dir in node.getElementsByTagName('AppDir'): - xml_menu.appendChild(app_dir) - for dir_dir in node.getElementsByTagName('DirectoryDir'): - xml_menu.appendChild(dir_dir) - parent = node.parentNode - parent.removeChild(node) - node = dom.createElement('Move') - node.appendChild(self.__addXmlTextElement(node, 'Old', xml_old.childNodes[0].nodeValue, dom)) - node.appendChild(self.__addXmlTextElement(node, 'New', os.path.join(new, path[1]), dom)) - element.appendChild(node) - if final_old == new: - return True - node = dom.createElement('Move') - node.appendChild(self.__addXmlTextElement(node, 'Old', final_old, dom)) - node.appendChild(self.__addXmlTextElement(node, 'New', new, dom)) - return element.appendChild(node) - -class Layout: - def __init__(self, node=None): - self.order = [] - - def parseMenuname(self, value): - self.order.append(['Menuname', value]) - - def parseSeparator(self): - self.order.append(['Separator']) - - def parseFilename(self, value): - self.order.append(['Filename', value]) - - def parseMerge(self, merge_type='all'): - self.order.append(['Merge', merge_type]) +# -*- coding: utf-8 -*- +# Alacarte Menu Editor - Simple fd.o Compliant Menu Editor +# Copyright (C) 2006 Travis Watkins, Heinrich Wendel +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Library General Public +# License as published by the Free Software Foundation; either +# version 2 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Library General Public License for more details. +# +# You should have received a copy of the GNU Library General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +import os, sys, re, xml.dom.minidom, locale +from gi.repository import GMenu +from Alacarte import util + +class Menu: + tree = None + visible_tree = None + path = None + dom = None + +class MenuEditor: + #lists for undo/redo functionality + __undo = [] + __redo = [] + + def __init__(self): + self.locale = locale.getdefaultlocale()[0] + self.__loadMenus() + + def reloadMenus(self): + self.applications = Menu() + self.applications.tree = GMenu.Tree.new('applications.menu', GMenu.TreeFlags.SHOW_EMPTY|GMenu.TreeFlags.INCLUDE_EXCLUDED|GMenu.TreeFlags.INCLUDE_NODISPLAY|GMenu.TreeFlags.SHOW_ALL_SEPARATORS|GMenu.TreeFlags.SORT_DISPLAY_NAME) + if not self.applications.tree.load_sync(): + self.applications = None + return + self.applications.visible_tree = GMenu.Tree.new('applications.menu', GMenu.TreeFlags.SORT_DISPLAY_NAME) + if not self.applications.visible_tree.load_sync(): + self.applications = None + return + self.applications.path = os.path.join(util.getUserMenuPath(), self.applications.tree.props.menu_basename) + if not os.path.isfile(self.applications.path): + self.applications.dom = xml.dom.minidom.parseString(util.getUserMenuXml(self.applications.tree)) + else: + self.applications.dom = xml.dom.minidom.parse(self.applications.path) + self.__remove_whilespace_nodes(self.applications.dom) + + def __menuChanged(self, *a): + print >> sys.stderr, "changed!\n" + self.applications.visible_tree.load_sync() + + def __loadMenus(self): + self.reloadMenus(); + self.save(True) + self.applications.visible_tree.connect("changed", self.__menuChanged) + + def save(self, from_loading=False): + for menu in ('applications',): + fd = open(getattr(self, menu).path, 'w') + fd.write(re.sub("\n[\s]*([^\n<]*)\n[\s]*\n', ''))) + fd.close() + if not from_loading: + self.__loadMenus() + + def quit(self): + for file_name in os.listdir(util.getUserItemPath()): + if file_name[-6:-2] in ('redo', 'undo'): + file_path = os.path.join(util.getUserItemPath(), file_name) + os.unlink(file_path) + for file_name in os.listdir(util.getUserDirectoryPath()): + if file_name[-6:-2] in ('redo', 'undo'): + file_path = os.path.join(util.getUserDirectoryPath(), file_name) + os.unlink(file_path) + for file_name in os.listdir(util.getUserMenuPath()): + if file_name[-6:-2] in ('redo', 'undo'): + file_path = os.path.join(util.getUserMenuPath(), file_name) + os.unlink(file_path) + + def revert(self): + for name in ('applications',): + menu = getattr(self, name) + self.revertTree(menu.tree.get_root_directory()) + path = os.path.join(util.getUserMenuPath(), os.path.basename(menu.tree.get_canonical_menu_path())) + try: + os.unlink(path) + except OSError: + pass + #reload DOM for each menu + if not os.path.isfile(menu.path): + menu.dom = xml.dom.minidom.parseString(util.getUserMenuXml(menu.tree)) + else: + menu.dom = xml.dom.minidom.parse(menu.path) + self.__remove_whilespace_nodes(menu.dom) + #reset undo/redo, no way to recover from this + self.__undo, self.__redo = [], [] + self.save() + + def revertTree(self, menu): + item_iter = menu.iter() + item_type = item_iter.next() + while item_type != GMenu.TreeItemType.INVALID: + if item_type == GMenu.TreeItemType.DIRECTORY: + item = item_iter.get_directory(); + self.revertTree(item) + elif item_type == GMenu.TreeItemType.ENTRY: + item = item_iter.get_entry(); + self.revertItem(item) + item_type = item_iter.next() + self.revertMenu(menu) + + def undo(self): + if len(self.__undo) == 0: + return + files = self.__undo.pop() + redo = [] + for file_path in files: + new_path = file_path.rsplit('.', 1)[0] + redo_path = util.getUniqueRedoFile(new_path) + data = open(new_path).read() + open(redo_path, 'w').write(data) + data = open(file_path).read() + open(new_path, 'w').write(data) + os.unlink(file_path) + redo.append(redo_path) + #reload DOM to make changes stick + for name in ('applications',): + menu = getattr(self, name) + if not os.path.isfile(menu.path): + menu.dom = xml.dom.minidom.parseString(util.getUserMenuXml(menu.tree)) + else: + menu.dom = xml.dom.minidom.parse(menu.path) + self.__remove_whilespace_nodes(menu.dom) + self.__redo.append(redo) + + def redo(self): + if len(self.__redo) == 0: + return + files = self.__redo.pop() + undo = [] + for file_path in files: + new_path = file_path.rsplit('.', 1)[0] + undo_path = util.getUniqueUndoFile(new_path) + data = open(new_path).read() + open(undo_path, 'w').write(data) + data = open(file_path).read() + open(new_path, 'w').write(data) + os.unlink(file_path) + undo.append(undo_path) + #reload DOM to make changes stick + for name in ('applications',): + menu = getattr(self, name) + if not os.path.isfile(menu.path): + menu.dom = xml.dom.minidom.parseString(util.getUserMenuXml(menu.tree)) + else: + menu.dom = xml.dom.minidom.parse(menu.path) + self.__remove_whilespace_nodes(menu.dom) + self.__undo.append(undo) + + def getMenus(self, parent=None): + if parent == None: + yield self.applications.tree.get_root_directory() + else: + item_iter = parent.iter() + item_type = item_iter.next() + while item_type != GMenu.TreeItemType.INVALID: + if item_type == GMenu.TreeItemType.DIRECTORY: + item = item_iter.get_directory() + yield (item, self.__isVisible(item)) + item_type = item_iter.next() + + def getContents(self, item): + contents = [] + item_iter = item.iter() + item_type = item_iter.next() + + while item_type != GMenu.TreeItemType.INVALID: + if item_type == GMenu.TreeItemType.DIRECTORY: + item = item_iter.get_directory() + elif item_type == GMenu.TreeItemType.ENTRY: + item = item_iter.get_entry() + elif item_type == GMenu.TreeItemType.HEADER: + item = item_iter.get_header() + elif item_type == GMenu.TreeItemType.ALIAS: + item = item_iter.get_alias() + if item: + contents.append(item) + item_type = item_iter.next() + return contents; + + def getItems(self, menu): + item_iter = menu.iter() + item_type = item_iter.next() + while item_type != GMenu.TreeItemType.INVALID: + if item_type == GMenu.TreeItemType.SEPARATOR: + item = item_iter.get_separator() + yield (item, True) + else: + if item_type == GMenu.TreeItemType.ENTRY: + item = item_iter.get_entry() + if item.get_desktop_file_id()[-19:] == '-usercustom.desktop': + continue + elif item_type == GMenu.TreeItemType.DIRECTORY: + item = item_iter.get_directory() + elif item_type == GMenu.TreeItemType.HEADER: + item = item_iter.get_header() + elif item_type == GMenu.TreeItemType.ALIAS: + item = item_iter.get_alias() + yield (item, self.__isVisible(item)) + item_type = item_iter.next() + + def canRevert(self, item): + if isinstance(item, GMenu.TreeEntry): + if util.getItemPath(item.get_desktop_file_id()): + path = util.getUserItemPath() + if os.path.isfile(os.path.join(path, item.get_desktop_file_id())): + return True + elif isinstance(item, GMenu.TreeDirectory): + if item.get_desktop_file_path(): + file_id = os.path.split(item.get_desktop_file_path())[1] + else: + file_id = item.get_menu_id() + '.directory' + if util.getDirectoryPath(file_id): + path = util.getUserDirectoryPath() + if os.path.isfile(os.path.join(path, file_id)): + return True + return False + + def setVisible(self, item, visible): + dom = self.__getMenu(item).dom + if isinstance(item, GMenu.TreeEntry): + self.__addUndo([self.__getMenu(item), item]) + menu_xml = self.__getXmlMenu(self.__getPath(item.get_parent()), dom, dom) + if visible: + self.__addXmlFilename(menu_xml, dom, item.get_desktop_file_id(), 'Include') + self.__writeItem(item, no_display=False) + else: + self.__addXmlFilename(menu_xml, dom, item.get_desktop_file_id(), 'Exclude') + self.__addXmlTextElement(menu_xml, 'AppDir', util.getUserItemPath(), dom) + elif isinstance(item, GMenu.TreeDirectory): + self.__addUndo([self.__getMenu(item), item]) + item_iter = item.iter() + first_child_type = item_iter.next() + #don't mess with it if it's empty + if first_child_type == GMenu.TreeItemType.INVALID: + return + menu_xml = self.__getXmlMenu(self.__getPath(item), dom, dom) + for node in self.__getXmlNodesByName(['Deleted', 'NotDeleted'], menu_xml): + node.parentNode.removeChild(node) + if visible: + self.__writeMenu(item, no_display=False) + else: + self.__writeMenu(item, no_display=True) + self.__addXmlTextElement(menu_xml, 'DirectoryDir', util.getUserDirectoryPath(), dom) + self.save() + + def createItem(self, parent, icon, name, comment, command, use_term, before=None, after=None): + file_id = self.__writeItem(None, icon, name, comment, command, use_term) + self.insertExternalItem(file_id, parent.get_menu_id(), before, after) + + def insertExternalItem(self, file_id, parent_id, before=None, after=None): + self.applications.tree.get_root_directory() + parent = self.__findMenu(parent_id) + self.applications.tree.get_root_directory() + dom = self.__getMenu(parent).dom + self.applications.tree.get_root_directory() + self.__addItem(parent, file_id, dom) + self.applications.tree.get_root_directory() + self.__positionItem(parent, ('Item', file_id), before, after) + self.applications.tree.get_root_directory() + self.__addUndo([self.__getMenu(parent), ('Item', file_id)]) + self.applications.tree.get_root_directory() + self.save() + self.applications.tree.get_root_directory() + + def createMenu(self, parent, icon, name, comment, before=None, after=None): + file_id = self.__writeMenu(None, icon, name, comment) + self.insertExternalMenu(file_id, parent.get_menu_id(), before, after) + + def insertExternalMenu(self, file_id, parent_id, before=None, after=None): + menu_id = file_id.rsplit('.', 1)[0] + parent = self.__findMenu(parent_id) + dom = self.__getMenu(parent).dom + self.__addXmlDefaultLayout(self.__getXmlMenu(self.__getPath(parent), dom, dom) , dom) + menu_xml = self.__getXmlMenu(self.__getPath(parent) + '/' + menu_id, dom, dom) + self.__addXmlTextElement(menu_xml, 'Directory', file_id, dom) + self.__positionItem(parent, ('Menu', menu_id), before, after) + self.__addUndo([self.__getMenu(parent), ('Menu', file_id)]) + self.save() + + def createSeparator(self, parent, before=None, after=None): + self.__positionItem(parent, ('Separator',), before, after) + self.__addUndo([self.__getMenu(parent), ('Separator',)]) + self.save() + + def editItem(self, item, icon, name, comment, command, use_term, parent=None, final=True): + #if nothing changed don't make a user copy + app_info = item.get_app_info() + if icon == app_info.get_icon() and name == app_info.get_display_name() and comment == item.get_comment() and command == item.get_exec() and use_term == item.get_launch_in_terminal(): + return + #hack, item.get_parent() seems to fail a lot + if not parent: + parent = item.get_parent() + if final: + self.__addUndo([self.__getMenu(parent), item]) + self.__writeItem(item, icon, name, comment, command, use_term) + if final: + dom = self.__getMenu(parent).dom + menu_xml = self.__getXmlMenu(self.__getPath(parent), dom, dom) + self.__addXmlTextElement(menu_xml, 'AppDir', util.getUserItemPath(), dom) + self.save() + + def editMenu(self, menu, icon, name, comment, final=True): + #if nothing changed don't make a user copy + if icon == menu.get_icon() and name == menu.get_name() and comment == menu.get_comment(): + return + #we don't use this, we just need to make sure the exists + #otherwise changes won't show up + dom = self.__getMenu(menu).dom + menu_xml = self.__getXmlMenu(self.__getPath(menu), dom, dom) + file_id = self.__writeMenu(menu, icon, name, comment) + if final: + self.__addXmlTextElement(menu_xml, 'DirectoryDir', util.getUserDirectoryPath(), dom) + self.__addUndo([self.__getMenu(menu), menu]) + self.save() + + def copyItem(self, item, new_parent, before=None, after=None): + dom = self.__getMenu(new_parent).dom + file_path = item.get_desktop_file_path() + keyfile = util.DesktopParser(file_path) + #erase Categories in new file + keyfile.set('Categories', ('',)) + keyfile.set('Hidden', False) + app_info = item.get_info() + file_id = util.getUniqueFileId(app_info.get_name().replace(os.sep, '-'), '.desktop') + out_path = os.path.join(util.getUserItemPath(), file_id) + keyfile.write(open(out_path, 'w')) + self.__addItem(new_parent, file_id, dom) + self.__positionItem(new_parent, ('Item', file_id), before, after) + self.__addUndo([self.__getMenu(new_parent), ('Item', file_id)]) + self.save() + return file_id + + def moveItem(self, item, new_parent, before=None, after=None): + undo = [] + if item.get_parent() != new_parent: + #hide old item + self.deleteItem(item) + undo.append(item) + file_id = self.copyItem(item, new_parent) + item = ('Item', file_id) + undo.append(item) + self.__positionItem(new_parent, item, before, after) + undo.append(self.__getMenu(new_parent)) + self.__addUndo(undo) + self.save() + + def moveMenu(self, menu, new_parent, before=None, after=None): + parent = new_parent + #don't move a menu into it's child + while parent.get_parent(): + parent = parent.get_parent() + if parent == menu: + return False + + #don't move a menu into itself + if new_parent == menu: + return False + + #can't move between top-level menus + if self.__getMenu(menu) != self.__getMenu(new_parent): + return False + if menu.get_parent() != new_parent: + dom = self.__getMenu(menu).dom + root_path = self.__getPath(menu).split('/', 1)[0] + xml_root = self.__getXmlMenu(root_path, dom, dom) + old_path = self.__getPath(menu).split('/', 1)[1] + #root menu's path has no / + if '/' in self.__getPath(new_parent): + new_path = self.__getPath(new_parent).split('/', 1)[1] + '/' + menu.get_menu_id() + else: + new_path = menu.get_menu_id() + self.__addXmlMove(xml_root, old_path, new_path, dom) + self.__positionItem(new_parent, menu, before, after) + self.__addUndo([self.__getMenu(new_parent),]) + self.save() + + def moveSeparator(self, separator, new_parent, before=None, after=None): + undo = [] + # remove the original separator if its parent is not the new destination + if separator.get_parent() != new_parent: + self.deleteSeparator(separator) + undo.append(separator) + # this adds the new separator to the specified position + self.__positionItem(new_parent, separator, before, after) + undo.append(self.__getMenu(new_parent)) + self.__addUndo(undo) + self.save() + + def deleteItem(self, item): + self.__addUndo([item,]) + self.__writeItem(item, hidden=True) + self.save() + + def deleteMenu(self, menu): + dom = self.__getMenu(menu).dom + menu_xml = self.__getXmlMenu(self.__getPath(menu), dom, dom) + self.__addDeleted(menu_xml, dom) + self.__addUndo([self.__getMenu(menu),]) + self.save() + + def deleteSeparator(self, item): + parent = item.get_parent() + contents = self.getContents(parent) + contents.remove(item) + layout = self.__createLayout(contents) + dom = self.__getMenu(parent).dom + menu_xml = self.__getXmlMenu(self.__getPath(parent), dom, dom) + self.__addXmlLayout(menu_xml, layout, dom) + self.__addUndo([self.__getMenu(item.get_parent()),]) + self.save() + + def revertItem(self, item): + if not self.canRevert(item): + return + self.__addUndo([item,]) + try: + os.remove(item.get_desktop_file_path()) + except OSError: + pass + self.save() + + def revertMenu(self, menu): + if not self.canRevert(menu): + return + #wtf happened here? oh well, just bail + if not menu.get_desktop_file_path(): + return + self.__addUndo([menu,]) + file_id = os.path.split(menu.get_desktop_file_path())[1] + path = os.path.join(util.getUserDirectoryPath(), file_id) + try: + os.remove(path) + except OSError: + pass + self.save() + + #private stuff + def __addUndo(self, items): + self.__undo.append([]) + for item in items: + if isinstance(item, Menu): + file_path = item.path + elif isinstance(item, tuple): + if item[0] == 'Item': + file_path = os.path.join(util.getUserItemPath(), item[1]) + if not os.path.isfile(file_path): + file_path = util.getItemPath(item[1]) + elif item[0] == 'Menu': + file_path = os.path.join(util.getUserDirectoryPath(), item[1]) + if not os.path.isfile(file_path): + file_path = util.getDirectoryPath(item[1]) + else: + continue + elif isinstance(item, GMenu.TreeDirectory): + if item.get_desktop_file_path() == None: + continue + file_path = os.path.join(util.getUserDirectoryPath(), os.path.split(item.get_desktop_file_path())[1]) + if not os.path.isfile(file_path): + file_path = item.get_desktop_file_path() + elif isinstance(item, GMenu.TreeEntry): + file_path = os.path.join(util.getUserItemPath(), item.get_desktop_file_id()) + if not os.path.isfile(file_path): + file_path = item.get_desktop_file_path() + else: + continue + data = open(file_path).read() + undo_path = util.getUniqueUndoFile(file_path) + open(undo_path, 'w').write(data) + self.__undo[-1].append(undo_path) + + def __getMenu(self, item): + return self.applications + + def __findMenu(self, menu_id, parent=None): + root_directory = self.applications.tree.get_root_directory() + if parent == None and root_directory != None: + return self.__findMenu(menu_id, root_directory) + if menu_id == root_directory.get_menu_id(): + return root_directory + item_iter = parent.iter() + item_type = item_iter.next() + while item_type != GMenu.TreeItemType.INVALID: + if item_type == GMenu.TreeItemType.DIRECTORY: + item = item_iter.get_directory() + if item.get_menu_id() == menu_id: + return item + menu = self.__findMenu(menu_id, item) + if menu != None: + return menu + item_type = item_iter.next() + + def __isVisible(self, item): + if isinstance(item, GMenu.TreeEntry): + app_info = item.get_app_info() + return not (item.get_is_excluded() or app_info.get_nodisplay()) + menu = self.__getMenu(item) + if menu == self.applications: + root = self.applications.visible_tree.get_root_directory() + if isinstance(item, GMenu.TreeDirectory): + if self.__findMenu(item.get_menu_id(), root) == None: + return False + return True + + def __getPath(self, menu, path=None): + if not path: + path = menu.get_menu_id() + if menu.get_parent(): + path = self.__getPath(menu.get_parent(), path) + path += '/' + path += menu.get_menu_id() + print "%s\n" % path + return path + + def __getXmlMenu(self, path, element, dom): + if '/' in path: + (name, path) = path.split('/', 1) + else: + name = path + path = '' + + found = None + for node in self.__getXmlNodesByName('Menu', element): + for child in self.__getXmlNodesByName('Name', node): + if child.childNodes[0].nodeValue == name: + if path: + found = self.__getXmlMenu(path, node, dom) + else: + found = node + break + if found: + break + if not found: + node = self.__addXmlMenuElement(element, name, dom) + if path: + found = self.__getXmlMenu(path, node, dom) + else: + found = node + + return found + + def __addXmlMenuElement(self, element, name, dom): + node = dom.createElement('Menu') + self.__addXmlTextElement(node, 'Name', name, dom) + return element.appendChild(node) + + def __addXmlTextElement(self, element, name, text, dom): + for temp in element.childNodes: + if temp.nodeName == name: + if temp.childNodes[0].nodeValue == text: + return + node = dom.createElement(name) + text = dom.createTextNode(text) + node.appendChild(text) + return element.appendChild(node) + + def __addXmlFilename(self, element, dom, filename, type = 'Include'): + # remove old filenames + for node in self.__getXmlNodesByName(['Include', 'Exclude'], element): + if node.childNodes[0].nodeName == 'Filename' and node.childNodes[0].childNodes[0].nodeValue == filename: + element.removeChild(node) + + # add new filename + node = dom.createElement(type) + node.appendChild(self.__addXmlTextElement(node, 'Filename', filename, dom)) + return element.appendChild(node) + + def __addDeleted(self, element, dom): + node = dom.createElement('Deleted') + return element.appendChild(node) + + def __writeItem(self, item=None, icon=None, name=None, comment=None, command=None, use_term=None, no_display=None, startup_notify=None, hidden=None): + if item: + file_path = item.get_desktop_file_path() + file_id = item.get_desktop_file_id() + keyfile = util.DesktopParser(file_path) + elif item == None and name == None: + raise Exception('New menu items need a name') + else: + file_id = util.getUniqueFileId(name, '.desktop') + keyfile = util.DesktopParser() + if icon: + keyfile.set('Icon', icon) + keyfile.set('Icon', icon, self.locale) + if name: + keyfile.set('Name', name) + keyfile.set('Name', name, self.locale) + if comment: + keyfile.set('Comment', comment) + keyfile.set('Comment', comment, self.locale) + if command: + keyfile.set('Exec', command) + if use_term != None: + keyfile.set('Terminal', use_term) + if no_display != None: + keyfile.set('NoDisplay', no_display) + if startup_notify != None: + keyfile.set('StartupNotify', startup_notify) + if hidden != None: + keyfile.set('Hidden', hidden) + out_path = os.path.join(util.getUserItemPath(), file_id) + keyfile.write(open(out_path, 'w')) + return file_id + + def __writeMenu(self, menu=None, icon=None, name=None, comment=None, no_display=None): + if menu: + file_id = os.path.split(menu.get_desktop_file_path())[1] + file_path = menu.get_desktop_file_path() + keyfile = util.DesktopParser(file_path) + elif menu == None and name == None: + raise Exception('New menus need a name') + else: + file_id = util.getUniqueFileId(name, '.directory') + keyfile = util.DesktopParser(file_type='Directory') + if icon: + keyfile.set('Icon', icon) + if name: + keyfile.set('Name', name) + keyfile.set('Name', name, self.locale) + if comment: + keyfile.set('Comment', comment) + keyfile.set('Comment', comment, self.locale) + if no_display != None: + keyfile.set('NoDisplay', no_display) + out_path = os.path.join(util.getUserDirectoryPath(), file_id) + keyfile.write(open(out_path, 'w')) + return file_id + + def __getXmlNodesByName(self, name, element): + for child in element.childNodes: + if child.nodeType == xml.dom.Node.ELEMENT_NODE: + if isinstance(name, str) and child.nodeName == name: + yield child + elif isinstance(name, list) or isinstance(name, tuple): + if child.nodeName in name: + yield child + + def __remove_whilespace_nodes(self, node): + remove_list = [] + for child in node.childNodes: + if child.nodeType == xml.dom.minidom.Node.TEXT_NODE: + child.data = child.data.strip() + if not child.data.strip(): + remove_list.append(child) + elif child.hasChildNodes(): + self.__remove_whilespace_nodes(child) + for node in remove_list: + node.parentNode.removeChild(node) + + def __addXmlMove(self, element, old, new, dom): + if not self.__undoMoves(element, old, new, dom): + node = dom.createElement('Move') + node.appendChild(self.__addXmlTextElement(node, 'Old', old, dom)) + node.appendChild(self.__addXmlTextElement(node, 'New', new, dom)) + #are parsed in reverse order, need to put at the beginning + return element.insertBefore(node, element.firstChild) + + def __addXmlLayout(self, element, layout, dom): + # remove old layout + for node in self.__getXmlNodesByName('Layout', element): + element.removeChild(node) + + # add new layout + node = dom.createElement('Layout') + for order in layout.order: + if order[0] == 'Separator': + child = dom.createElement('Separator') + node.appendChild(child) + elif order[0] == 'Filename': + child = self.__addXmlTextElement(node, 'Filename', order[1], dom) + elif order[0] == 'Menuname': + child = self.__addXmlTextElement(node, 'Menuname', order[1], dom) + elif order[0] == 'Merge': + child = dom.createElement('Merge') + child.setAttribute('type', order[1]) + node.appendChild(child) + return element.appendChild(node) + + def __addXmlDefaultLayout(self, element, dom): + # remove old default layout + for node in self.__getXmlNodesByName('DefaultLayout', element): + element.removeChild(node) + + # add new layout + node = dom.createElement('DefaultLayout') + node.setAttribute('inline', 'false') + return element.appendChild(node) + + def __createLayout(self, items): + layout = Layout() + layout.order = [] + + layout.order.append(['Merge', 'menus']) + for item in items: + if isinstance(item, tuple): + if item[0] == 'Separator': + layout.parseSeparator() + elif item[0] == 'Menu': + layout.parseMenuname(item[1]) + elif item[0] == 'Item': + layout.parseFilename(item[1]) + elif isinstance(item, GMenu.TreeDirectory): + layout.parseMenuname(item.get_menu_id()) + elif isinstance(item, GMenu.TreeEntry): + layout.parseFilename(item.get_desktop_file_id()) + elif isinstance(item, GMenu.TreeSeparator): + layout.parseSeparator() + layout.order.append(['Merge', 'files']) + return layout + + def __addItem(self, parent, file_id, dom): + xml_parent = self.__getXmlMenu(self.__getPath(parent), dom, dom) + self.__addXmlFilename(xml_parent, dom, file_id, 'Include') + + def __deleteItem(self, parent, file_id, dom, before=None, after=None): + xml_parent = self.__getXmlMenu(self.__getPath(parent), dom, dom) + self.__addXmlFilename(xml_parent, dom, file_id, 'Exclude') + + def __positionItem(self, parent, item, before=None, after=None): + contents = self.getContents(parent) + if after: + index = contents.index(after) + 1 + elif before: + index = contents.index(before) + else: + # append the item to the list + index = len(contents) + #if this is a move to a new parent you can't remove the item + if item in contents: + # decrease the destination index, if we shorten the list + if (before and (contents.index(item) < index)) \ + or (after and (contents.index(item) < index - 1)): + index -= 1 + contents.remove(item) + contents.insert(index, item) + layout = self.__createLayout(contents) + dom = self.__getMenu(parent).dom + menu_xml = self.__getXmlMenu(self.__getPath(parent), dom, dom) + self.__addXmlLayout(menu_xml, layout, dom) + + def __undoMoves(self, element, old, new, dom): + nodes = [] + matches = [] + original_old = old + final_old = old + #get all elements + for node in self.__getXmlNodesByName(['Move'], element): + nodes.insert(0, node) + #if the matches our old parent we've found a stage to undo + for node in nodes: + xml_old = node.getElementsByTagName('Old')[0] + xml_new = node.getElementsByTagName('New')[0] + if xml_new.childNodes[0].nodeValue == old: + matches.append(node) + #we should end up with this path when completed + final_old = xml_old.childNodes[0].nodeValue + #undoing s + for node in matches: + element.removeChild(node) + if len(matches) > 0: + for node in nodes: + xml_old = node.getElementsByTagName('Old')[0] + xml_new = node.getElementsByTagName('New')[0] + path = os.path.split(xml_new.childNodes[0].nodeValue) + if path[0] == original_old: + element.removeChild(node) + for node in dom.getElementsByTagName('Menu'): + name_node = node.getElementsByTagName('Name')[0] + name = name_node.childNodes[0].nodeValue + if name == os.path.split(new)[1]: + #copy app and dir directory info from old + root_path = dom.getElementsByTagName('Menu')[0].getElementsByTagName('Name')[0].childNodes[0].nodeValue + xml_menu = self.__getXmlMenu(root_path + '/' + new, dom, dom) + for app_dir in node.getElementsByTagName('AppDir'): + xml_menu.appendChild(app_dir) + for dir_dir in node.getElementsByTagName('DirectoryDir'): + xml_menu.appendChild(dir_dir) + parent = node.parentNode + parent.removeChild(node) + node = dom.createElement('Move') + node.appendChild(self.__addXmlTextElement(node, 'Old', xml_old.childNodes[0].nodeValue, dom)) + node.appendChild(self.__addXmlTextElement(node, 'New', os.path.join(new, path[1]), dom)) + element.appendChild(node) + if final_old == new: + return True + node = dom.createElement('Move') + node.appendChild(self.__addXmlTextElement(node, 'Old', final_old, dom)) + node.appendChild(self.__addXmlTextElement(node, 'New', new, dom)) + return element.appendChild(node) + +class Layout: + def __init__(self, node=None): + self.order = [] + + def parseMenuname(self, value): + self.order.append(['Menuname', value]) + + def parseSeparator(self): + self.order.append(['Separator']) + + def parseFilename(self, value): + self.order.append(['Filename', value]) + + def parseMerge(self, merge_type='all'): + self.order.append(['Merge', merge_type]) diff --git a/Alacarte/util.py b/Alacarte/util.py dissimilarity index 82% index 49a8132..0cba0ec 100644 --- a/Alacarte/util.py +++ b/Alacarte/util.py @@ -1,234 +1,234 @@ -# -*- coding: utf-8 -*- -# Alacarte Menu Editor - Simple fd.o Compliant Menu Editor -# Copyright (C) 2006 Travis Watkins -# -# This library is free software; you can redistribute it and/or -# modify it under the terms of the GNU Library General Public -# License as published by the Free Software Foundation; either -# version 2 of the License, or (at your option) any later version. -# -# This library is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -# Library General Public License for more details. -# -# You should have received a copy of the GNU Library General Public -# License along with this library; if not, write to the Free Software -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - -import os -from gi.repository import Gtk, GdkPixbuf, GMenu -from ConfigParser import ConfigParser - -class DesktopParser(ConfigParser): - def __init__(self, filename=None, file_type='Application'): - ConfigParser.__init__(self) - self.filename = filename - self.file_type = file_type - if filename: - if len(self.read(filename)) == 0: - #file doesn't exist - self.add_section('Desktop Entry') - else: - self.add_section('Desktop Entry') - self._list_separator = ';' - - def optionxform(self, option): - #makes keys not be lowercase - return option - - def get(self, option, locale=None): - locale_option = option + '[%s]' % locale - try: - value = ConfigParser.get(self, 'Desktop Entry', locale_option) - except: - try: - value = ConfigParser.get(self, 'Desktop Entry', option) - except: - return None - if self._list_separator in value: - value = value.split(self._list_separator) - if value == 'true': - value = True - if value == 'false': - value = False - return value - - def set(self, option, value, locale=None): - if locale: - option = option + '[%s]' % locale - if value == True: - value = 'true' - if value == False: - value = 'false' - if isinstance(value, tuple) or isinstance(value, list): - value = self._list_separator.join(value) + ';' - ConfigParser.set(self, 'Desktop Entry', option, value) - - def write(self, file_object): - file_object.write('[Desktop Entry]\n') - items = [] - if not self.filename: - file_object.write('Encoding=UTF-8\n') - file_object.write('Type=' + str(self.file_type) + '\n') - for item in self.items('Desktop Entry'): - items.append(item) - items.sort() - for item in items: - file_object.write(item[0] + '=' + item[1] + '\n') - -def getUniqueFileId(name, extension): - append = 0 - while 1: - if append == 0: - filename = name + extension - else: - filename = name + '-' + str(append) + extension - if extension == '.desktop': - path = getUserItemPath() - if not os.path.isfile(os.path.join(path, filename)) and not getItemPath(filename): - break - elif extension == '.directory': - path = getUserDirectoryPath() - if not os.path.isfile(os.path.join(path, filename)) and not getDirectoryPath(filename): - break - append += 1 - return filename - -def getUniqueRedoFile(filepath): - append = 0 - while 1: - new_filepath = filepath + '.redo-' + str(append) - if not os.path.isfile(new_filepath): - break - else: - append += 1 - return new_filepath - -def getUniqueUndoFile(filepath): - filename, extension = os.path.split(filepath)[1].rsplit('.', 1) - append = 0 - while 1: - if extension == 'desktop': - path = getUserItemPath() - elif extension == 'directory': - path = getUserDirectoryPath() - elif extension == 'menu': - path = getUserMenuPath() - new_filepath = os.path.join(path, filename + '.' + extension + '.undo-' + str(append)) - if not os.path.isfile(new_filepath): - break - else: - append += 1 - return new_filepath - -def getUserMenuPath(): - menu_dir = None - if os.environ.has_key('XDG_CONFIG_HOME'): - menu_dir = os.path.join(os.environ['XDG_CONFIG_HOME'], 'menus') - else: - menu_dir = os.path.join(os.environ['HOME'], '.config', 'menus') - #move .config out of the way if it's not a dir, it shouldn't be there - if os.path.isfile(os.path.split(menu_dir)[0]): - os.rename(os.path.split(menu_dir)[0], os.path.split(menu_dir)[0] + '.old') - if not os.path.isdir(menu_dir): - os.makedirs(menu_dir) - return menu_dir - -def getItemPath(file_id): - if os.environ.has_key('XDG_DATA_DIRS'): - for system_path in os.environ['XDG_DATA_DIRS'].split(':'): - file_path = os.path.join(system_path, 'applications', file_id) - if os.path.isfile(file_path): - return file_path - file_path = os.path.join('/', 'usr', 'share', 'applications', file_id) - if os.path.isfile(file_path): - return file_path - return False - -def getUserItemPath(): - item_dir = None - if os.environ.has_key('XDG_DATA_HOME'): - item_dir = os.path.join(os.environ['XDG_DATA_HOME'], 'applications') - else: - item_dir = os.path.join(os.environ['HOME'], '.local', 'share', 'applications') - if not os.path.isdir(item_dir): - os.makedirs(item_dir) - return item_dir - -def getDirectoryPath(file_id): - home = getUserDirectoryPath() - file_path = os.path.join(home, file_id) - if os.path.isfile(file_path): - return file_path - if os.environ.has_key('XDG_DATA_DIRS'): - for system_path in os.environ['XDG_DATA_DIRS'].split(':'): - file_path = os.path.join(system_path, 'desktop-directories', file_id) - if os.path.isfile(file_path): - return file_path - file_path = os.path.join('/', 'usr', 'share', 'desktop-directories', file_id) - if os.path.isfile(file_path): - return file_path - return False - -def getUserDirectoryPath(): - menu_dir = None - if os.environ.has_key('XDG_DATA_HOME'): - menu_dir = os.path.join(os.environ['XDG_DATA_HOME'], 'desktop-directories') - else: - menu_dir = os.path.join(os.environ['HOME'], '.local', 'share', 'desktop-directories') - if not os.path.isdir(menu_dir): - os.makedirs(menu_dir) - return menu_dir - -def getSystemMenuPath(file_name): - if os.environ.has_key('XDG_CONFIG_DIRS'): - for system_path in os.environ['XDG_CONFIG_DIRS'].split(':'): - file_path = os.path.join(system_path, 'menus', file_name) - if os.path.isfile(file_path): - return file_path - file_path = os.path.join('/', 'etc', 'xdg', 'menus', file_name) - if os.path.isfile(file_path): - return file_path - return False - -def getUserMenuXml(tree): - system_file = getSystemMenuPath(os.path.basename(tree.get_canonical_menu_path())) - name = tree.get_root_directory().get_menu_id() - menu_xml = "\n" - menu_xml += "\n " + name + "\n " - menu_xml += "" + system_file + "\n\n" - return menu_xml - -def getIcon(item): - pixbuf, path = None, None - if item == None: - return None - - if isinstance(item, GMenu.TreeDirectory): - gicon = item.get_icon() - else: - app_info = item.get_app_info() - gicon = app_info.get_icon() - - icon_theme = Gtk.IconTheme.get_default() - try: - info = icon_theme.lookup_by_gicon(icon_theme, gicon, 24, 0) - pixbuf = icon.load_icon() - path = info.get_filename() - except: - if pixbuf is None: - if isinstance(item, GMenu.TreeDirectory): - iconName = 'gnome-fs-directory' - else: - iconName = 'application-default-icon' - try: - pixbuf = icon_theme.load_icon(iconName, 24, 0) - path = icon_theme.lookup_icon(iconName, 24, 0).get_filename() - except: - return None - if pixbuf == None: - return None - if pixbuf.get_width() != 24 or pixbuf.get_height() != 24: - pixbuf = pixbuf.scale_simple(24, 24, GdkPixbuf.InterpType.HYPER) - return pixbuf +# -*- coding: utf-8 -*- +# Alacarte Menu Editor - Simple fd.o Compliant Menu Editor +# Copyright (C) 2006 Travis Watkins +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Library General Public +# License as published by the Free Software Foundation; either +# version 2 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Library General Public License for more details. +# +# You should have received a copy of the GNU Library General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +import os +from gi.repository import Gtk, GdkPixbuf, GMenu +from ConfigParser import ConfigParser + +class DesktopParser(ConfigParser): + def __init__(self, filename=None, file_type='Application'): + ConfigParser.__init__(self) + self.filename = filename + self.file_type = file_type + if filename: + if len(self.read(filename)) == 0: + #file doesn't exist + self.add_section('Desktop Entry') + else: + self.add_section('Desktop Entry') + self._list_separator = ';' + + def optionxform(self, option): + #makes keys not be lowercase + return option + + def get(self, option, locale=None): + locale_option = option + '[%s]' % locale + try: + value = ConfigParser.get(self, 'Desktop Entry', locale_option) + except: + try: + value = ConfigParser.get(self, 'Desktop Entry', option) + except: + return None + if self._list_separator in value: + value = value.split(self._list_separator) + if value == 'true': + value = True + if value == 'false': + value = False + return value + + def set(self, option, value, locale=None): + if locale: + option = option + '[%s]' % locale + if value == True: + value = 'true' + if value == False: + value = 'false' + if isinstance(value, tuple) or isinstance(value, list): + value = self._list_separator.join(value) + ';' + ConfigParser.set(self, 'Desktop Entry', option, value) + + def write(self, file_object): + file_object.write('[Desktop Entry]\n') + items = [] + if not self.filename: + file_object.write('Encoding=UTF-8\n') + file_object.write('Type=' + str(self.file_type) + '\n') + for item in self.items('Desktop Entry'): + items.append(item) + items.sort() + for item in items: + file_object.write(item[0] + '=' + item[1] + '\n') + +def getUniqueFileId(name, extension): + append = 0 + while 1: + if append == 0: + filename = name + extension + else: + filename = name + '-' + str(append) + extension + if extension == '.desktop': + path = getUserItemPath() + if not os.path.isfile(os.path.join(path, filename)) and not getItemPath(filename): + break + elif extension == '.directory': + path = getUserDirectoryPath() + if not os.path.isfile(os.path.join(path, filename)) and not getDirectoryPath(filename): + break + append += 1 + return filename + +def getUniqueRedoFile(filepath): + append = 0 + while 1: + new_filepath = filepath + '.redo-' + str(append) + if not os.path.isfile(new_filepath): + break + else: + append += 1 + return new_filepath + +def getUniqueUndoFile(filepath): + filename, extension = os.path.split(filepath)[1].rsplit('.', 1) + append = 0 + while 1: + if extension == 'desktop': + path = getUserItemPath() + elif extension == 'directory': + path = getUserDirectoryPath() + elif extension == 'menu': + path = getUserMenuPath() + new_filepath = os.path.join(path, filename + '.' + extension + '.undo-' + str(append)) + if not os.path.isfile(new_filepath): + break + else: + append += 1 + return new_filepath + +def getUserMenuPath(): + menu_dir = None + if os.environ.has_key('XDG_CONFIG_HOME'): + menu_dir = os.path.join(os.environ['XDG_CONFIG_HOME'], 'menus') + else: + menu_dir = os.path.join(os.environ['HOME'], '.config', 'menus') + #move .config out of the way if it's not a dir, it shouldn't be there + if os.path.isfile(os.path.split(menu_dir)[0]): + os.rename(os.path.split(menu_dir)[0], os.path.split(menu_dir)[0] + '.old') + if not os.path.isdir(menu_dir): + os.makedirs(menu_dir) + return menu_dir + +def getItemPath(file_id): + if os.environ.has_key('XDG_DATA_DIRS'): + for system_path in os.environ['XDG_DATA_DIRS'].split(':'): + file_path = os.path.join(system_path, 'applications', file_id) + if os.path.isfile(file_path): + return file_path + file_path = os.path.join('/', 'usr', 'share', 'applications', file_id) + if os.path.isfile(file_path): + return file_path + return False + +def getUserItemPath(): + item_dir = None + if os.environ.has_key('XDG_DATA_HOME'): + item_dir = os.path.join(os.environ['XDG_DATA_HOME'], 'applications') + else: + item_dir = os.path.join(os.environ['HOME'], '.local', 'share', 'applications') + if not os.path.isdir(item_dir): + os.makedirs(item_dir) + return item_dir + +def getDirectoryPath(file_id): + home = getUserDirectoryPath() + file_path = os.path.join(home, file_id) + if os.path.isfile(file_path): + return file_path + if os.environ.has_key('XDG_DATA_DIRS'): + for system_path in os.environ['XDG_DATA_DIRS'].split(':'): + file_path = os.path.join(system_path, 'desktop-directories', file_id) + if os.path.isfile(file_path): + return file_path + file_path = os.path.join('/', 'usr', 'share', 'desktop-directories', file_id) + if os.path.isfile(file_path): + return file_path + return False + +def getUserDirectoryPath(): + menu_dir = None + if os.environ.has_key('XDG_DATA_HOME'): + menu_dir = os.path.join(os.environ['XDG_DATA_HOME'], 'desktop-directories') + else: + menu_dir = os.path.join(os.environ['HOME'], '.local', 'share', 'desktop-directories') + if not os.path.isdir(menu_dir): + os.makedirs(menu_dir) + return menu_dir + +def getSystemMenuPath(file_name): + if os.environ.has_key('XDG_CONFIG_DIRS'): + for system_path in os.environ['XDG_CONFIG_DIRS'].split(':'): + file_path = os.path.join(system_path, 'menus', file_name) + if os.path.isfile(file_path): + return file_path + file_path = os.path.join('/', 'etc', 'xdg', 'menus', file_name) + if os.path.isfile(file_path): + return file_path + return False + +def getUserMenuXml(tree): + system_file = getSystemMenuPath(os.path.basename(tree.get_canonical_menu_path())) + name = tree.get_root_directory().get_menu_id() + menu_xml = "\n" + menu_xml += "\n " + name + "\n " + menu_xml += "" + system_file + "\n\n" + return menu_xml + +def getIcon(item): + pixbuf, path = None, None + if item == None: + return None + + if isinstance(item, GMenu.TreeDirectory): + gicon = item.get_icon() + else: + app_info = item.get_app_info() + gicon = app_info.get_icon() + + icon_theme = Gtk.IconTheme.get_default() + try: + info = icon_theme.lookup_by_gicon(icon_theme, gicon, 24, 0) + pixbuf = icon.load_icon() + path = info.get_filename() + except: + if pixbuf is None: + if isinstance(item, GMenu.TreeDirectory): + iconName = 'gnome-fs-directory' + else: + iconName = 'application-default-icon' + try: + pixbuf = icon_theme.load_icon(iconName, 24, 0) + path = icon_theme.lookup_icon(iconName, 24, 0).get_filename() + except: + return None + if pixbuf == None: + return None + if pixbuf.get_width() != 24 or pixbuf.get_height() != 24: + pixbuf = pixbuf.scale_simple(24, 24, GdkPixbuf.InterpType.HYPER) + return pixbuf -- 2.11.4.GIT