home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.6)
-
- import gtk
- import gtk.gdk as gtk
- import gobject
- import xdg.Menu as xdg
- import sys
- import os
- import gettext
- import gst
- from warnings import warn
- from gettext import gettext as _
- from Util import *
- from CoreMenu import *
- (SHOW_ALL, SHOW_ONLY_FREE, UNUSED_1, SHOW_ONLY_SUPPORTED, SHOW_ONLY_THIRD_PARTY, UNUSED_2, SHOW_ONLY_INSTALLED, SHOW_ONLY_MAIN, SHOW_ONLY_PROPRIETARY) = range(9)
- PIMP_APPS = [
- 'gstreamer0.10-plugins-ugly',
- 'gstreamer0.10-ffmpeg',
- 'sun-java5-plugin',
- 'flashplugin-nonfree',
- 'ubuntu-restricted-extras']
-
- class NullActivationStyleForMenu:
-
- def __init__(self):
- self.selectFilter = None
- self.menuFilter = None
-
-
- def isApproved(self, component, pkgname):
- return True
-
-
- def menuCacheName(self):
- return 'menu.p'
-
-
-
- class ApplicationMenu(CoreApplicationMenu):
- ''' 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_packages, progress, filter = SHOW_ONLY_SUPPORTED, dontPopulate = False, activation_style = NullActivationStyleForMenu()):
- CoreApplicationMenu.__init__(self, datadir)
- self.cache = cache
- self.treeview_packages = treeview_packages
- self.activationStyle = activation_style
- self.icons = gtk.icon_theme_get_default()
- self.icons.append_search_path(os.path.join(datadir, 'icons'))
- self.searchTerms = []
- if self.activationStyle.menuFilter is not None:
- self.filter = self.activationStyle.menuFilter
- else:
- self.filter = filter
- self.real_categories_store = gtk.ListStore(gobject.TYPE_STRING, gobject.TYPE_PYOBJECT)
- if dontPopulate:
- return None
- cacheLoaded = False
- cname = activation_style.menuCacheName()
- if not cacheLoaded:
- progress.label_action.set_label(_('Collecting application data...'))
- 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
-
-
- def get_categories_store(self):
- return self.real_categories_store
-
-
- def _refilter(self, model = None):
- if not model:
- model = self.treeview_packages.get_model()
-
- name = None
- (path, colum) = self.treeview_packages.get_cursor()
- if path:
-
- try:
- name = model.get_value(model.get_iter(path), COL_NAME)
- except ValueError:
- e = None
- except:
- None<EXCEPTION MATCH>ValueError
-
-
- None<EXCEPTION MATCH>ValueError
- self.treeview_packages.set_model(None)
- if model != None:
- model.get_model().refilter()
-
- self.treeview_packages.set_model(model)
- 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
-
- elif len(model) > 0:
- self.treeview_packages.set_cursor(0)
-
-
-
- def _name_sort_func(self, model, iter1, iter2):
- '''
- Sort by name, honor special craziness
- '''
- item1 = model.get_value(iter1, COL_ITEM)
- item2 = model.get_value(iter2, COL_ITEM)
- if item1 == None or item2 == None:
- return 0
- cat = model.get_data('category')
- if cat and cat.name != self.all_category_name and hasattr(item1, 'onTop') and hasattr(item2, 'onTop'):
- if not item1.onTop and item2.onTop:
- if item1.onTop:
- return -1
- if item2.onTop:
- return 1
-
-
- name1 = model.get_value(iter1, COL_NAME)
- name2 = model.get_value(iter2, COL_NAME)
- if name1 < name2:
- return -1
- if name1 > name2:
- return 1
- return 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
- if item1.rank > item2.rank:
- return -1
- return 0
-
-
- def _visible_filter(self, model, iter):
- item = model.get_value(iter, COL_ITEM)
- return True
-
-
- def _filterAndRank(self, item):
- '''
- Watch out, Google!
- '''
- trigger = ''
- rank = 100 * item.popcon / self.popcon_max
- for term in self.searchTerms:
- hit = False
- if term == item.name.lower() or term == item.pkgname.lower():
- rank += 100
- hit = True
-
- if term in item.name.lower():
- rank += 30
- trigger += ' name'
- hit = True
-
- if term in item.pkgname.lower():
- rank += 30
- trigger += ' pkg_name'
- hit = True
-
- if self._mimeMatch(item, term, fuzzy = True):
- rank += 25
- trigger += ' mime'
- hit = True
-
- if self._codecMatch(item, term, fuzzy = True):
- rank += 15
- trigger += ' codec'
- hit = True
-
- if self.cache.has_key(item.pkgname) and term in self.cache[item.pkgname].description.lower():
- rank += 10
- trigger += ' pkg_desc'
- hit = True
-
- if hit == False:
- return None
- if item.pkgname.lower() in PIMP_APPS:
- rank += 75
- continue
- hit == False
-
- return rank
-
-
- def _mimeMatch(self, item, term, fuzzy = False):
- for pattern in item.mime:
- if fuzzy and term in pattern:
- return True
- if not fuzzy and pattern == term:
- return True
-
- return False
-
-
- def _codecMatch(self, item, term, fuzzy = False):
- if ':' in term:
- term = term.split(':')[1]
-
- search_cap = gst.caps_from_string(term)
- for codec in item.codecs:
- if ':' in codec:
- codec = codec.split(':')[1]
-
- if fuzzy and term in codec:
- return True
- cap = gst.caps_from_string(codec)
- if cap and search_cap and search_cap & cap:
- return True
-
- return False
-
-
- def _activationStyleFilter(self, item):
- filter = self.activationStyle.selectFilter(self)
- if self.activationStyle.isInstallerOnly and self.itemIsInstalled(item):
- return False
- for term in self.activationStyle.searchTerms():
- if filter(item, term):
- return True
-
- return False
-
-
- def doMimeSearch(self, mime_type, fuzzy = False):
- res = set()
- model = self.real_categories_store.get_value(self.all_category_iter, COL_CAT_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 refreshAfterCacheChange(self, progress):
- for cat in self.pickle:
- for item in self.pickle[cat]:
- if self.cache.has_key(item.pkgname):
- pass
- item.toInstall = self.cache[item.pkgname].isInstalled
-
-
-
-
- def refresh(self, progress):
- self.real_categories_store.clear()
- progress.subOp = _('Loading applications...')
- progress.update(0)
- self.all_category_iter = self.real_categories_store.append()
- self.all_category_name = '<b>%s</b>' % _('All')
- item = Category(self, self.all_category_name, 'distributor-logo')
- self.initListStores(item, self)
- self.real_categories_store.set(self.all_category_iter, COL_CAT_NAME, '<b>%s</b>' % _('All'), COL_CAT_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:
- progress.subOp = _('Loading %s...') % category.name
- self.initListStores(category, self)
- self.real_categories_store.set(self.real_categories_store.append(), COL_CAT_NAME, category.name, COL_CAT_ITEM, category)
- i += 1
- progress.update((i / float(lenx)) * 100)
- for item in self.pickle[category]:
- category.all_applications.append([
- item.name,
- item,
- item.popcon])
- store = self.real_categories_store.get_value(self.all_category_iter, COL_ITEM).all_applications
- store.append([
- item.name,
- item,
- item.popcon])
- if item.popcon > self.popcon_max:
- self.popcon_max = item.popcon
-
- pkgname = item.pkgname
- if not self.pkg_to_app.has_key(pkgname):
- self.pkg_to_app[pkgname] = set()
-
- self.pkg_to_app[pkgname].add(item)
-
-
- self.refreshAfterCacheChange(progress)
-
-
- def itemAvailable(self, item):
- ''' returns True if the item is available in the current
- apt cache '''
- return self.cache.has_key(item.pkgname)
-
-
- def itemIsInstalled(self, item):
- ''' returns True if the item is currently installed '''
- if self.cache.has_key(item.pkgname):
- pass
- return self.cache[item.pkgname].isInstalled
-
-
- def initListStores(self, category, parent):
- category.all_applications = gtk.ListStore(gobject.TYPE_STRING, gobject.TYPE_PYOBJECT, gobject.TYPE_INT)
- category.filtered_applications = category.all_applications.filter_new()
- category.filtered_applications.set_visible_func(parent._visible_filter)
- category.filtered_applications.set_data('category', category)
- category.applications = gtk.TreeModelSort(category.filtered_applications)
- category.applications.set_data('category', category)
-
-
- 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 self.itemIsInstalled(item) and not (item.toInstall):
- to_rm.add(item)
-
- if not self.itemIsInstalled(item) 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 != self.itemIsInstalled(item):
- return True
-
-
- return False
-
-
- 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())
-
-