#!/usr/bin/python2 import gi gi.require_version('MateDesktop', '2.0') from gi.repository import Gtk, Gdk, GLib from gi.repository import Pango from gi.repository import GObject from gi.repository import MateDesktop import os.path import shutil import re from execute import * import xdg.DesktopEntry import xdg.Menu from filemonitor import monitor as filemonitor class IconManager(GObject.GObject): __gsignals__ = { "changed" : (GObject.SignalFlags.RUN_LAST, None, () ) } def __init__( self ): GObject.GObject.__init__( self ) self.icons = { } self.count = 0 # Some apps don't put a default icon in the default theme folder, so we will search all themes def createTheme( d ): theme = Gtk.IconTheme() theme.set_custom_theme( d ) return theme # This takes to much time and there are only a very few applications that use icons from different themes #self.themes = map( createTheme, [ d for d in os.listdir( "/usr/share/icons" ) if os.path.isdir( os.path.join( "/usr/share/icons", d ) ) ] ) self.defaultTheme = Gtk.IconTheme.get_default() defaultKdeTheme = createTheme( "kde.default" ) # Setup and clean up the temp icon dir configDir = GLib.get_user_config_dir() self.iconDir = os.path.join(configDir, "mintmenu") if not os.path.exists(self.iconDir): os.makedirs(self.iconDir) contents = os.listdir(self.iconDir) for fn in contents: os.remove(os.path.join(self.iconDir, fn)) self.defaultTheme.append_search_path(self.iconDir) # Themes with the same content as the default them aren't needed #self.themes = [ theme for theme in self.themes if theme.list_icons() != defaultTheme.list_icons() ] self.themes = [ self.defaultTheme, defaultKdeTheme ] # Listen for changes in the themes for theme in self.themes: theme.connect("changed", self.themeChanged ) def getIcon( self, iconName, iconSize ): if not iconName: return None try: iconFileName = "" realIconName = "" needTempFile = False #[ iconWidth, iconHeight ] = self.getIconSize( iconSize ) if iconSize <= 0: return None elif os.path.isabs( iconName ): iconFileName = iconName needTempFile = True else: if iconName[-4:] in [".png", ".xpm", ".svg", ".gif"]: realIconName = iconName[:-4] else: realIconName = iconName if iconFileName and needTempFile and os.path.exists( iconFileName ): tmpIconName = iconFileName.replace("/", "-") realIconName = tmpIconName[:-4] if not os.path.exists(os.path.join(self.iconDir, tmpIconName)): shutil.copyfile(iconFileName, os.path.join(self.iconDir, tmpIconName)) self.defaultTheme.append_search_path(self.iconDir) image = Gtk.Image() icon_found = False for theme in self.themes: if theme.lookup_icon(realIconName, 0, Gtk.IconLookupFlags.FORCE_REGULAR): icon_found = True break if icon_found: image.set_from_icon_name(realIconName, Gtk.IconSize.DND) image.set_pixel_size(iconSize) else: image = None return image except Exception, e: print "Exception " + e.__class__.__name__ + ": " + e.message return None def themeChanged( self, theme ): self.emit( "changed" ) GObject.type_register(IconManager) class easyButton( Gtk.Button ): def __init__( self, iconName, iconSize, labels = None, buttonWidth = -1, buttonHeight = -1 ): GObject.GObject.__init__( self ) self.connections = [ ] self.iconName = iconName self.iconSize = iconSize self.showIcon = True self.set_relief( Gtk.ReliefStyle.NONE ) self.set_size_request( buttonWidth, buttonHeight ) Align1 = Gtk.Alignment.new( 0, 0.5, 1.0, 0 ) HBox1 = Gtk.Box( orientation=Gtk.Orientation.HORIZONTAL ) self.labelBox = Gtk.Box( orientation=Gtk.Orientation.VERTICAL, spacing=2 ) self.buttonImage = Gtk.Image() icon = self.getIcon( self.iconSize ) if icon: self.buttonImage = icon else: #[ iW, iH ] = iconManager.getIconSize( self.iconSize ) self.buttonImage.set_size_request( self.iconSize, self.iconSize ) self.image_box = Gtk.Box( orientation=Gtk.Orientation.HORIZONTAL ) self.image_box.pack_start(self.buttonImage, False, False, 5) self.image_box.show_all() HBox1.pack_start( self.image_box, False, False, 0 ) if labels: for label in labels: if isinstance( label, basestring ): self.addLabel( label ) elif isinstance( label, list ): self.addLabel( label[0], label[1] ) self.labelBox.show() HBox1.pack_start( self.labelBox , True, True, 0) HBox1.show() Align1.add( HBox1 ) Align1.show() self.add( Align1 ) self.connectSelf( "destroy", self.onDestroy ) self.connect( "released", self.onRelease ) # Reload icons when the theme changed self.themeChangedHandlerId = iconManager.connect("changed", self.themeChanged ) def connectSelf( self, event, callback ): self.connections.append( self.connect( event, callback ) ) def onRelease( self, widget ): widget.get_style_context().set_state( Gtk.StateFlags.NORMAL ) def onDestroy( self, widget ): self.buttonImage.clear() iconManager.disconnect( self.themeChangedHandlerId ) for connection in self.connections: self.disconnect( connection ) del self.connections def addLabel( self, text, styles = None ): label = Gtk.Label() if "" in text or " 0 and color[0] == "#": #appName = "%s" % (color, appName); #appComment = "%s" % (color, appComment); #appName = "%s" % (appName); #appComment = "%s" % (appComment); #else: #appName = "%s" % (appName); #appComment = "%s" % (appComment); appName = "%s" % (appName); appComment = "%s" % (appComment); except Exception, detail: print detail pass if self.showComment and self.appComment != "": if self.iconSize <= 2: self.addLabel( '%s' % appName) self.addLabel( '%s' % appComment) else: self.addLabel( appName ) self.addLabel( '%s' % appComment) else: self.addLabel( appName ) def execute( self, *args ): self.highlight = False for child in self.labelBox: child.destroy() self.setupLabels() return super(MenuApplicationLauncher, self).execute(*args) def setShowComment( self, showComment ): self.showComment = showComment for child in self.labelBox: child.destroy() self.setupLabels() class FavApplicationLauncher( ApplicationLauncher ): def __init__( self, desktopFile, iconSize, swapGeneric = False ): self.swapGeneric = swapGeneric ApplicationLauncher.__init__( self, desktopFile, iconSize ) def setupLabels( self ): if self.appGenericName: if self.swapGeneric: self.addLabel( '%s' % self.appName ) self.addLabel( self.appGenericName ) else: self.addLabel( '%s' % self.appGenericName ) self.addLabel( self.appName ) else: self.addLabel( '%s' % self.appName ) if self.appComment != "": self.addLabel( self.appComment ) else: self.addLabel ( "" ) def setSwapGeneric( self, swapGeneric ): self.swapGeneric = swapGeneric for child in self.labelBox: child.destroy() self.setupLabels() class CategoryButton( easyButton ): def __init__( self, iconName, iconSize, labels , f ): easyButton.__init__( self, iconName, iconSize, labels ) self.filter = f iconManager = IconManager()