Applications: Port to GI MateMenu

This commit is contained in:
Clement Lefebvre 2019-07-05 11:31:30 +02:00
parent bc3c9078b6
commit b5f4789090
2 changed files with 65 additions and 37 deletions

2
debian/control vendored
View File

@ -21,7 +21,7 @@ Depends:
xdg-utils, xdg-utils,
libglib2.0-bin, libglib2.0-bin,
mate-menus, mate-menus,
python-mate-menu, gir1.2-matemenu-2.0,
gir1.2-matepanelapplet-4.0, gir1.2-matepanelapplet-4.0,
gir1.2-gtk-3.0, gir1.2-gtk-3.0,
gir1.2-mate-desktop, gir1.2-mate-desktop,

View File

@ -11,9 +11,9 @@ import urllib
import gi import gi
gi.require_version("Gtk", "3.0") gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, Gdk, GdkPixbuf, Gio, GLib gi.require_version("MateMenu", "2.0")
from gi.repository import Gtk, Gdk, GdkPixbuf, Gio, GLib, MateMenu
import matemenu
import plugins.recentHelper as RecentHelper import plugins.recentHelper as RecentHelper
from plugins.easybuttons import (ApplicationLauncher, CategoryButton, from plugins.easybuttons import (ApplicationLauncher, CategoryButton,
FavApplicationLauncher, FavApplicationLauncher,
@ -82,10 +82,33 @@ def rel_path(target, base=os.curdir):
rel_list = [os.pardir] * (len(base_list) - i) + target_list[i:] rel_list = [os.pardir] * (len(base_list) - i) + target_list[i:]
return os.path.join(*rel_list) return os.path.join(*rel_list)
def get_contents(item):
contents = []
item_iter = item.iter()
item_type = item_iter.next()
while item_type != MateMenu.TreeItemType.INVALID:
item = None
if item_type == MateMenu.TreeItemType.DIRECTORY:
item = item_iter.get_directory()
elif item_type == MateMenu.TreeItemType.ENTRY:
item = item_iter.get_entry()
elif item_type == MateMenu.TreeItemType.HEADER:
item = item_iter.get_header()
elif item_type == MateMenu.TreeItemType.ALIAS:
item = item_iter.get_alias()
elif item_type == MateMenu.TreeItemType.SEPARATOR:
item = item_iter.get_separator()
if item:
contents.append(item)
item_type = item_iter.next()
return contents
class Menu: class Menu:
def __init__(self, MenuToLookup): def __init__(self, MenuToLookup):
self.tree = matemenu.lookup_tree(MenuToLookup) self.tree = MateMenu.Tree.new(MenuToLookup, MateMenu.TreeFlags.SORT_DISPLAY_NAME)
self.tree.load_sync()
self.directory = self.tree.get_root_directory() self.directory = self.tree.get_root_directory()
def getMenus(self, parent=None): def getMenus(self, parent=None):
@ -93,21 +116,21 @@ class Menu:
#gives top-level "Applications" item #gives top-level "Applications" item
yield self.tree.root yield self.tree.root
else: else:
for menu in parent.get_contents(): for menu in get_contents(parent):
if menu.get_type() == matemenu.TYPE_DIRECTORY and self.__isVisible(menu): if isinstance(menu, MateMenu.TreeDirectory) and self.__isVisible(menu):
yield menu yield menu
def getItems(self, menu): def getItems(self, menu):
for item in menu.get_contents(): for item in get_contents(menu):
if item.get_type() == matemenu.TYPE_ENTRY and \ if isinstance(item, MateMenu.TreeEntry) and \
item.get_desktop_file_id()[-19:] != '-usercustom.desktop' and \ item.get_desktop_file_id()[-19:] != '-usercustom.desktop' and \
self.__isVisible(item): self.__isVisible(item):
yield item yield item
def __isVisible(self, item): def __isVisible(self, item):
if item.get_type() == matemenu.TYPE_ENTRY: if isinstance(item, MateMenu.TreeEntry):
return not(item.get_is_excluded() or item.get_is_nodisplay()) return not(item.get_is_excluded() or item.get_is_nodisplay())
if item.get_type() == matemenu.TYPE_DIRECTORY and len(item.get_contents()): if isinstance(item, MateMenu.TreeDirectory) and len(get_contents(item)):
return True return True
class SuggestionButton(Gtk.Button): class SuggestionButton(Gtk.Button):
@ -162,6 +185,7 @@ class pluginclass(object):
RecentHelper.mintMenuWin = mintMenuWin RecentHelper.mintMenuWin = mintMenuWin
self.mainMenus = [] self.mainMenus = []
self.toggleButton = toggleButton self.toggleButton = toggleButton
self.menuFiles = []
self.de = de self.de = de
# Detect the locale (this is used for the Wikipedia search) # Detect the locale (this is used for the Wikipedia search)
@ -279,7 +303,8 @@ class pluginclass(object):
for mainitems in ["mate-applications.menu", "mate-settings.menu"]: for mainitems in ["mate-applications.menu", "mate-settings.menu"]:
mymenu = Menu(mainitems) mymenu = Menu(mainitems)
mymenu.tree.add_monitor(self.menuChanged, None) mymenu.tree.connect("changed", self.menuChanged, None)
self.menuFiles.append(mymenu)
self.refresh_apt_cache() self.refresh_apt_cache()
self.suggestions = [] self.suggestions = []
@ -1584,9 +1609,14 @@ class pluginclass(object):
# Reload the menufiles from the filesystem # Reload the menufiles from the filesystem
def loadMenuFiles(self): def loadMenuFiles(self):
self.menuFiles = [] if len(self.menuFiles) > 0:
for menu in self.menuFiles:
menu.tree.disconnect_by_func(self.menuChanged)
self.menuFiles = []
for mainitems in ["mate-applications.menu", "mate-settings.menu"]: for mainitems in ["mate-applications.menu", "mate-settings.menu"]:
self.menuFiles.append(Menu(mainitems)) mymenu = Menu(mainitems )
mymenu.tree.connect("changed", self.menuChanged, None)
self.menuFiles.append(mymenu)
# Build a list of all categories in the menu ([{"name", "icon", tooltip"}] # Build a list of all categories in the menu ([{"name", "icon", tooltip"}]
def buildCategoryList(self): def buildCategoryList(self):
@ -1596,16 +1626,14 @@ class pluginclass(object):
"filter":"", "index": 0}] "filter":"", "index": 0}]
num = 1 num = 1
for menu in self.menuFiles: for menu in self.menuFiles:
for child in menu.directory.get_contents(): for child in get_contents(menu.directory):
if child.get_type() == matemenu.TYPE_DIRECTORY: if isinstance(child, MateMenu.TreeDirectory):
icon = str(child.icon) name = child.get_name()
#if (icon == "preferences-system"): icon = child.get_icon().to_string()
# self.adminMenu = child.name newCategoryList.append({"name": name,
#if (icon != "applications-system" and icon != "applications-other"): "icon": icon,
newCategoryList.append({"name": child.name, "tooltip": name,
"icon": child.icon, "filter": name,
"tooltip": child.name,
"filter": child.name,
"index": num}) "index": num})
num += 1 num += 1
return newCategoryList return newCategoryList
@ -1615,29 +1643,29 @@ class pluginclass(object):
newApplicationsList = [] newApplicationsList = []
def find_applications_recursively(app_list, directory, catName): def find_applications_recursively(app_list, directory, catName):
for item in directory.get_contents(): for item in get_contents(directory):
if item.get_type() == matemenu.TYPE_ENTRY: if isinstance(item, MateMenu.TreeEntry):
app_list.append({"entry": item, "category": catName}) app_list.append({"entry": item, "category": catName})
elif item.get_type() == matemenu.TYPE_DIRECTORY: elif isinstance(item, MateMenu.TreeDirectory):
find_applications_recursively(app_list, item, catName) find_applications_recursively(app_list, item, catName)
for menu in self.menuFiles: for menu in self.menuFiles:
directory = menu.directory directory = menu.directory
for entry in directory.get_contents(): for entry in get_contents(directory):
if entry.get_type() == matemenu.TYPE_DIRECTORY and len(entry.get_contents()): if isinstance(entry, MateMenu.TreeDirectory) and len(get_contents(entry)):
#Entry is a top-level category #Entry is a top-level category
#catName = entry.name #catName = entry.get_name()
#icon = str(entry.icon) #icon = str(entry.get_icon().to_string())
#if (icon == "applications-system" or icon == "applications-other"): #if (icon == "applications-system" or icon == "applications-other"):
# catName = self.adminMenu # catName = self.adminMenu
for item in entry.get_contents(): for item in get_contents(entry):
if item.get_type() == matemenu.TYPE_DIRECTORY: if isinstance(item, MateMenu.TreeDirectory):
find_applications_recursively(newApplicationsList, item, entry.name) find_applications_recursively(newApplicationsList, item, entry.get_name())
elif item.get_type() == matemenu.TYPE_ENTRY: elif isinstance(item, MateMenu.TreeEntry):
newApplicationsList.append({"entry": item, "category": entry.name}) newApplicationsList.append({"entry": item, "category": entry.get_name()})
#elif entry.get_type() == matemenu.TYPE_ENTRY: #elif isinstance(entry, MateMenu.TreeEntry):
# if not (entry.get_is_excluded() or entry.get_is_nodisplay()): # if not (entry.get_is_excluded() or entry.get_is_nodisplay()):
# print "=======>>> " + item.name + " = top level" # print "=======>>> " + item.get_name() + " = top level"
# newApplicationsList.append({"entry": item, "category": ""}) # newApplicationsList.append({"entry": item, "category": ""})
return newApplicationsList return newApplicationsList