From e71b7979a236e4017c34a4b482ecd66c518ef4e2 Mon Sep 17 00:00:00 2001 From: "Jasper St. Pierre" Date: Wed, 9 May 2012 14:40:49 -0300 Subject: MenuEditor: fix up XML writing --- Mozo/MenuEditor.py | 87 ++++++++++++++++++++++++------------------------------ 1 file changed, 38 insertions(+), 49 deletions(-) (limited to 'Mozo') diff --git a/Mozo/MenuEditor.py b/Mozo/MenuEditor.py index 339279f..623f3c0 100644 --- a/Mozo/MenuEditor.py +++ b/Mozo/MenuEditor.py @@ -198,7 +198,7 @@ class MenuEditor: dom = self.__getMenu(item).dom if item.get_type() == matemenu.TYPE_ENTRY: self.__addUndo([self.__getMenu(item), item]) - menu_xml = self.__getXmlMenu(self.__getPath(item.get_parent()), dom, dom) + menu_xml = self.__getXmlMenu(self.__getPath(item.get_parent()), dom.documentElement, dom) if visible: self.__addXmlFilename(menu_xml, dom, item.get_desktop_file_id(), 'Include') self.__writeItem(item, NoDisplay=False) @@ -210,7 +210,7 @@ class MenuEditor: #don't mess with it if it's empty if len(item.get_contents()) == 0: return - menu_xml = self.__getXmlMenu(self.__getPath(item), dom, dom) + menu_xml = self.__getXmlMenu(self.__getPath(item), dom.documentElement, dom) for node in self.__getXmlNodesByName(['Deleted', 'NotDeleted'], menu_xml): node.parentNode.removeChild(node) self.__writeMenu(item, NoDisplay=not visible) @@ -233,8 +233,8 @@ class MenuEditor: 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.__addXmlDefaultLayout(self.__getXmlMenu(self.__getPath(parent), dom.documentElement, dom) , dom) + menu_xml = self.__getXmlMenu(self.__getPath(parent) + [menu_id], dom.documentElement, 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)]) @@ -257,7 +257,7 @@ class MenuEditor: self.__writeItem(item, Icon=icon, Name=name, Comment=comment, Exec=command, Terminal=use_term) if final: dom = self.__getMenu(parent).dom - menu_xml = self.__getXmlMenu(self.__getPath(parent), dom, dom) + menu_xml = self.__getXmlMenu(self.__getPath(parent), dom.documentElement, dom) self.__addXmlTextElement(menu_xml, 'AppDir', util.getUserItemPath(), dom) self.save() @@ -268,7 +268,7 @@ class MenuEditor: #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) + menu_xml = self.__getXmlMenu(self.__getPath(menu), dom.documentElement, dom) self.__writeMenu(menu, Icon=icon, Name=name, Comment=comment) if final: self.__addXmlTextElement(menu_xml, 'DirectoryDir', util.getUserDirectoryPath(), dom) @@ -328,15 +328,12 @@ class MenuEditor: 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) + path = self.__getPath(menu) + root_path = path[0] + xml_root = self.__getXmlMenu(root_path, dom.documentElement, dom) + old_path = path[1:] + new_path = self.__getPath(new_parent)[1:] + [menu.get_menu_id()] + self.__addXmlMove(xml_root, '/'.join(old_path), '/'.join(new_path), dom) self.__positionItem(new_parent, menu, before, after) self.__addUndo([self.__getMenu(new_parent),]) self.save() @@ -360,7 +357,7 @@ class MenuEditor: def deleteMenu(self, menu): dom = self.__getMenu(menu).dom - menu_xml = self.__getXmlMenu(self.__getPath(menu), dom, dom) + menu_xml = self.__getXmlMenu(self.__getPath(menu), dom.documentElement, dom) self.__addDeleted(menu_xml, dom) self.__addUndo([self.__getMenu(menu),]) self.save() @@ -371,7 +368,7 @@ class MenuEditor: contents.remove(item) layout = self.__createLayout(contents) dom = self.__getMenu(parent).dom - menu_xml = self.__getXmlMenu(self.__getPath(parent), dom, dom) + menu_xml = self.__getXmlMenu(self.__getPath(parent), dom.documentElement, dom) self.__addXmlLayout(menu_xml, layout, dom) self.__addUndo([self.__getMenu(item.get_parent()),]) self.save() @@ -482,41 +479,33 @@ class MenuEditor: return False return True - def __getPath(self, menu, path=None): - if not path: - path = menu.tree.root.get_menu_id() - if menu.get_parent(): - path = self.__getPath(menu.get_parent(), path) - path += '/' - path += menu.menu_id - return path + def __getPath(self, menu): + names = [] + current = menu + while current is not None: + names.append(current.get_menu_id()) + current = current.get_parent() - def __getXmlMenu(self, path, element, dom): - if '/' in path: - (name, path) = path.split('/', 1) - else: - name = path - path = '' + # XXX - don't append root menu name, mozo doesn't + # expect it. look into this more. + names.pop(-1) + return names[::-1] - found = None + def __getXmlMenuPart(self, element, name): 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 node + return None - return found + def __getXmlMenu(self, path, element, dom): + for name in path: + found = self.__getXmlMenuPart(element, name) + if found is not None: + element = found + else: + element = self.__addXmlMenuElement(element, name, dom) + return element def __addXmlMenuElement(self, element, name, dom): node = dom.createElement('Menu') @@ -682,7 +671,7 @@ class MenuEditor: return layout def __addItem(self, parent, file_id, dom): - xml_parent = self.__getXmlMenu(self.__getPath(parent), dom, dom) + xml_parent = self.__getXmlMenu(self.__getPath(parent), dom.documentElement, dom) self.__addXmlFilename(xml_parent, dom, file_id, 'Include') def __positionItem(self, parent, item, before=None, after=None): @@ -704,7 +693,7 @@ class MenuEditor: contents.insert(index, item) layout = self.__createLayout(contents) dom = self.__getMenu(parent).dom - menu_xml = self.__getXmlMenu(self.__getPath(parent), dom, dom) + menu_xml = self.__getXmlMenu(self.__getPath(parent), dom.documentElement, dom) self.__addXmlLayout(menu_xml, layout, dom) def __undoMoves(self, element, old, new, dom): @@ -739,7 +728,7 @@ class MenuEditor: 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) + xml_menu = self.__getXmlMenu(root_path + '/' + new, dom.documentElement, dom) for app_dir in node.getElementsByTagName('AppDir'): xml_menu.appendChild(app_dir) for dir_dir in node.getElementsByTagName('DirectoryDir'): -- cgit v1.2.1