home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyo (Python 2.4)
-
- import pygtk
- pygtk.require('2.0')
- import gtk
- import gtk.gdk as gtk
- import gobject
- import xdg.Menu as xdg
- import sys
- import os
- import gettext
- import common
- from warnings import warn
- from gettext import gettext as _
- from Util import *
- (SHOW_ALL, SHOW_ALL_SUPPORTED, SHOW_ALL_FREE, SHOW_ONLY_MAIN, SHOW_ONLY_PROPRIETARY, SHOW_ONLY_THIRD_PARTY) = range(6)
-
- class MenuItem(object):
- ''' base class for a object in the menu '''
-
- def __init__(self, name, iconname = None):
- self.name = name
- self.iconname = iconname
- self.icontheme = None
-
-
- def __repr__(self):
- return 'MenuItem: %s' % self.name
-
-
-
- class Category(MenuItem):
- ''' represents a category '''
-
- def __init__(self, parent, name, iconname = None):
- MenuItem.__init__(self, name, iconname)
-
-
- def initListStores(self, parent):
- self.icontheme = parent.icons
- self.all_applications = gtk.ListStore(gobject.TYPE_STRING, gobject.TYPE_PYOBJECT, gobject.TYPE_INT)
- self.filtered_applications = self.all_applications.filter_new()
- self.filtered_applications.set_visible_func(parent._visible_filter)
- self.applications = gtk.TreeModelSort(self.filtered_applications)
-
-
-
- class Application(MenuItem):
- ''' this class represents a application '''
-
- def __init__(self, name, iconname = None):
- MenuItem.__init__(self, name, iconname)
- self.pkgname = None
- self.description = ''
- self.html = None
- self.mime = None
- self.execCmd = None
- self.needsTerminal = False
- self.available = False
- self.component = None
- self.channel = None
- self.isInstalled = False
- self.toInstall = False
- self.popcon = 1
- self.rank = 1
- self.free = False
- self.licenseUri = None
- self.supported = False
- self.thirdparty = False
- self.architectures = []
- self.menupath = ''
-
-
-
- class ApplicationMenu(object):
- ''' this represents the application menu, the interessting bits are:
- - store that can be attached to a TreeView
- - pkg_to_app a dictionary that maps the apt pkgname to the application
- items
- '''
- debug = 0
-
- def __init__(self, datadir, cachedir, cache, treeview_categories, treeview_packages, progress, filter = SHOW_ONLY_MAIN, dontPopulate = False):
- self.menudir = datadir + '/desktop'
- self.cache = cache
- self.treeview_categories = treeview_categories
- self.treeview_packages = treeview_packages
- self.popcon_max = 1
- self.pickle = { }
- self.icons = common.ToughIconTheme()
- self.icons.prepend_search_path(os.path.join(datadir, 'icons'))
-
- try:
- gtk.window_set_default_icon(self.icons.load_icon('gnome-settings-default-applications', 32, 0))
- except gobject.GError:
- pass
-
- self.searchTerms = []
- self.mimeSearch = None
- self.filter = filter
- self.pkg_to_app = { }
- self.desktopEntriesSeen = set()
- self.real_categories_store = gtk.ListStore(gobject.TYPE_STRING, gobject.TYPE_PYOBJECT)
- if dontPopulate:
- return None
-
- if os.path.exists('%s/menu.p' % cachedir):
- print 'using existing cache'
- import cPickle
- self.pickle = cPickle.load(open('%s/menu.p' % cachedir))
- else:
- print 'no cache found'
- self.desktopEntriesSeen.clear()
- menu = xdg.Menu.parse(os.path.join(self.menudir, 'applications.menu'))
- self._populateFromEntry(menu)
- self.refresh(progress)
- self.store = self.real_categories_store
- self.treeview_categories.set_model(self.real_categories_store)
- self.treeview_packages.set_model(None)
-
-
- def _refilter(self):
- model = self.treeview_packages.get_model()
- name = None
- (path, colum) = self.treeview_packages.get_cursor()
- if path:
- name = model.get_value(model.get_iter(path), COL_NAME)
-
- self.treeview_packages.set_model(None)
- if model != None:
- model.get_model().refilter()
-
- self.treeview_packages.set_model(model)
- self.treeview_packages.columns_autosize()
- if name != None:
- for it in iterate_list_store(model, model.get_iter_first()):
- aname = model.get_value(it, COL_NAME)
- if name == aname:
- self.treeview_packages.set_cursor(model.get_path(it))
- return None
- continue
-
- elif len(model) > 0:
- self.treeview_packages.set_cursor(0)
-
-
-
- def _ranking_sort_func(self, model, iter1, iter2):
- '''
- Sort by the search result rank
- '''
- item1 = model.get_value(iter1, COL_ITEM)
- item2 = model.get_value(iter2, COL_ITEM)
- if item1 == None or item2 == None:
- return 0
-
- if item1.rank < item2.rank:
- return 1
- elif item1.rank > item2.rank:
- return -1
- else:
- return 0
-
-
- def _visible_filter(self, model, iter):
- item = model.get_value(iter, COL_ITEM)
- if item:
- if self.mimeSearch and not self.mimeSearch.approved(item.component, item.pkgname):
- return False
-
- if self.filter == SHOW_ONLY_MAIN and item.component != 'main':
- return False
-
- if self.filter == SHOW_ALL_SUPPORTED and item.supported != True:
- return False
-
- if self.filter == SHOW_ALL_FREE and item.free == False:
- return False
-
- if self.filter == SHOW_ONLY_PROPRIETARY and item.free == True:
- return False
-
- if self.filter == SHOW_ONLY_THIRD_PARTY and item.thirdparty != True:
- return False
-
- if len(self.searchTerms) > 0:
- rank = self._filterAndRank(item)
- if rank == None:
- return False
- else:
- item.rank = rank
-
-
- return True
-
-
- def _filterAndRank(self, item):
- '''
- Watch out, Google!
- '''
- trigger = ''
- rank = item.popcon
- if self.mimeSearch:
- if self._mimeFilter(item):
- return rank
- else:
- return None
-
- for term in self.searchTerms:
- hit = False
- if term == item.name.lower() or term == item.pkgname.lower():
- rank += 1500
- hit = True
-
- if term in item.name.lower():
- rank += item.popcon * 3
- trigger += ' name'
- hit = True
-
- if term in item.pkgname.lower():
- rank += item.popcon * 3
- trigger += ' pkg_name'
- hit = True
-
- if self._mimeMatch(item, term, fuzzy = True):
- rank += item.popcon * 2
- trigger += ' mime'
- hit = True
-
- if term in item.description.lower():
- rank += item.popcon
- trigger += ' desc'
- hit = True
-
- if self.cache.has_key(item.pkgname) and term in self.cache[item.pkgname].description.lower():
- rank += item.popcon
- trigger += ' pkg_desc'
- hit = True
-
- if hit == False:
- return None
- continue
-
- return rank
-
-
- def _mimeMatch(self, item, term, fuzzy = False):
- for re_pattern in item.mime:
- pattern = re_pattern.pattern
- if fuzzy and term in pattern:
- return True
- continue
- if not fuzzy and re_pattern.match(term):
- return True
- continue
-
- return False
-
-
- def _mimeFilter(self, item):
- for mime_type in self.searchTerms:
- if self._mimeMatch(item, mime_type):
- return True
- continue
-
- return False
-
-
- def doMimeSearch(self, mime_type, fuzzy = False):
- res = set()
- model = self.real_categories_store.get_value(self.all_category_iter, COL_ITEM).all_applications
- for it in iterate_list_store(model, model.get_iter_first()):
- item = model.get_value(it, COL_ITEM)
- for re_pattern in item.mime:
- pattern = re_pattern.pattern
- if fuzzy and mime_type in pattern:
- res.add(item)
- continue
- if not fuzzy and re_pattern.match(mime_type):
- res.add(item)
- continue
-
-
- return res
-
-
- def createMenuCache(self, targetdir):
- self.desktopEntriesSeen.clear()
- self.pkg_to_app.clear()
- menu = xdg.Menu.parse(os.path.abspath(os.path.join(self.menudir, 'applications.menu')))
- self._populateFromEntry(menu)
- import pickle
- pickle.dump(self.pickle, open('%s/menu.p' % targetdir, 'w'))
-
-
- def refresh(self, progress):
- self.real_categories_store.clear()
- self.all_category_iter = self.real_categories_store.append()
- item = Category(self, '<b>%s</b>' % _('All'), 'distributor-logo')
- item.initListStores(self)
- self.real_categories_store.set(self.all_category_iter, COL_NAME, '<b>%s</b>' % _('All'), COL_ITEM, item)
- i = 0
- lenx = len(self.pickle.keys())
- keys = self.pickle.keys()
- keys.sort(cmp = (lambda x, y: cmp(x.name.lower(), y.name.lower())))
- for category in keys:
- category.initListStores(self)
- self.real_categories_store.set(self.real_categories_store.append(), COL_NAME, category.name, COL_ITEM, category)
- i += 1
- progress.update((i / float(lenx)) * 100.0)
- for item in self.pickle[category]:
- item.name = xmlescape(item.desktop_entry.getName())
- item.description = xmlescape(item.desktop_entry.getComment())
- if not item.description:
- item.description = xmlescape(item.desktop_entry.get('GenericName'))
-
- item.icontheme = self.icons
- category.all_applications.set(category.all_applications.append(), COL_NAME, item.name, COL_ITEM, item, COL_POPCON, item.popcon)
- store = self.real_categories_store.get_value(self.all_category_iter, COL_ITEM).all_applications
- store.set(store.append(), COL_NAME, item.name, COL_ITEM, item, COL_POPCON, item.popcon)
- if item.popcon > self.popcon_max:
- self.popcon_max = item.popcon
-
- pkgname = item.pkgname
- if self.cache.has_key(pkgname):
- item.isInstalled = self.cache[pkgname].isInstalled
- else:
- item.isInstalled = False
- item.toInstall = item.isInstalled
-
-
-
-
- def getChanges(self, get_paths = False):
- ''' return the selected changes in the tree
- TODO: what is get_paths?
- '''
- to_inst = set()
- to_rm = set()
- for name, item in self.store:
- for name, item, popcon in item.all_applications:
- if item.isInstalled and not (item.toInstall):
- to_rm.add(item)
-
- if not (item.isInstalled) and item.toInstall:
- to_inst.add(item)
- continue
-
-
- return (to_inst, to_rm)
-
-
- def isChanged(self):
- ''' check if there are changes at all '''
- for cat_name, cat in self.store:
- for name, item, popcon in cat.all_applications:
- if item.toInstall != item.isInstalled:
- return True
- continue
-
-
- return False
-
-
- def _populateFromEntry(self, node, parent = None, progress = None):
- for entry in node.getEntries(hidden = True):
- self._dbg(2, 'entry: %s' % entry)
- if isinstance(entry, xdg.Menu.Menu):
- name = xmlescape(entry.getName())
- self._dbg(1, 'we have a sub-menu %s ' % name)
- item = Category(self, name, entry.getIcon())
- self.pickle[item] = []
- self._populateFromEntry(entry, item, progress = progress)
- continue
- if isinstance(entry, xdg.Menu.MenuEntry):
- self._dbg(3, node.getPath() + '/\t' + entry.DesktopFileID + '\t' + entry.DesktopEntry.getFileName())
- name = xmlescape(entry.DesktopEntry.getName())
- self._dbg(1, 'we have a application %s (%s) ' % (name, entry.DesktopFileID))
- None if name and entry.DesktopEntry.hasKey('X-AppInstall-Package') else None<EXCEPTION MATCH>UnicodeEncodeError
- if isinstance(entry, xdg.Menu.Header):
- print 'got header'
- continue
-
-
-
- def _dbg(self, level, msg):
- '''Write debugging output to sys.stderr.
- '''
- if level <= self.debug:
- print >>sys.stderr, msg
-
-
-
- if __name__ == '__main__':
- print 'testing the menu'
- desktopdir = '/usr/share/app-install'
- from Util import MyCache
- cache = MyCache()
- treeview = gtk.TreeView()
- menu = ApplicationMenu(desktopdir, cache, treeview, treeview, apt.progress.OpProgress())
-
-