Ejemplo n.º 1
0
def _find_plugin(name):
	plugins = PluginManager()
	for plugin_name in plugins.list_installed_plugins():
		try:
			klass = plugins.get_plugin_class(plugin_name)
			for objtype in klass.discover_classes(InsertedObjectTypeExtension):
				if objtype.name == name:
					activatable = klass.check_dependencies_ok()
					return (plugin_name, klass.plugin_info['name'], activatable, klass)
		except:
			continue
	return None
    def testSelectPlugins(self):
        window = MyWindow()

        pref_dialog = PreferencesDialog(window)
        treeview = pref_dialog.plugins_tab.treeview
        for name in PluginManager.list_installed_plugins():
            pref_dialog.plugins_tab.select_plugin(name)
            model, iter = treeview.get_selection().get_selected()
            self.assertEqual(model[iter][0], name)

            path = model.get_path(iter)
            wasactive = model[iter][1]
            model.do_toggle_path(path)
            if wasactive:
                self.assertEqual(model[iter][1], False)
            else:
                self.assertEqual(model[iter][1],
                                 model[iter][2])  # active matched activatable

        pref_dialog.do_response_cancel()
class PreferencesDialog(Dialog):

	def __init__(self, widget, default_tab=None, select_plugin=None):
		Dialog.__init__(self, widget, _('Preferences')) # T: Dialog title
		self.preferences = ConfigManager.preferences

		# warning for locale
		_pref_enc = locale.getpreferredencoding()
		if _pref_enc in ('ascii', 'us-ascii', 'ANSI_X3.4-1968'):
			self.vbox.pack_start(localeWarningBar(_pref_enc), True, True, 0)

		# saves a list of loaded plugins to be used later
		self.plugins = PluginManager()
		self.p_save_loaded = list(self.plugins)

		# Dynamic tabs
		gtknotebook = Gtk.Notebook()
		self.vbox.pack_start(gtknotebook, True, True, 0)
		self.forms = {}

		############################### needs rewrite to make defintion more robust
		for category in ('Interface', 'Editing'):
			vbox = Gtk.VBox()
			index = gtknotebook.append_page(vbox, Gtk.Label(label=_(category)))
			# From GTK Doc: Note that due to historical reasons, GtkNotebook refuses
			# to switch to a page unless the child widget is visible.
			vbox.show()
			if category == default_tab:
				gtknotebook.set_current_page(index)

			fields = []
			values = {}
			sections = {}

			for section, preferences in (
				('GtkInterface', interface_preferences),
				('PageView', pageview_preferences)
			):
				for p in [p for p in preferences if p[2] == category]:
					# key, type, category, label, default, (check)
					if len(p) == 5:
						key, type, cat, label, default = p
						self.preferences[section].setdefault(key, default)
						fields.append((key, type, label))
					else:
						key, type, cat, label, default, check = p
						self.preferences[section].setdefault(key, default, check)
						fields.append((key, type, label, check))

					values[key] = self.preferences[section][key]
					sections[key] = section

			form = InputForm(fields, values)
			form.preferences_sections = sections
			vbox.pack_start(form, False, True, 0)
			self.forms[category] = form

			if category == 'Interface':
				self._add_font_selection(form)

		# Styles tab
		#~ gtknotebook.append_page(StylesTab(self), Gtk.Label(label=_('Styles')))

		# Keybindings tab
		gtknotebook.append_page(KeyBindingsTab(self), Gtk.Label(label=_('Key bindings')))
				# T: Heading in preferences dialog

		# Plugins tab
		self.plugins_tab = PluginsTab(self, self.plugins)
		plugins_tab_index = gtknotebook.append_page(self.plugins_tab, Gtk.Label(label=_('Plugins')))
				# T: Heading in preferences dialog
		self.plugins_tab.show()
		#~ print default_tab, index
		if default_tab == "Plugins":
			gtknotebook.set_current_page(plugins_tab_index)
			if not select_plugin is None:
					self.plugins_tab.select_plugin(select_plugin)

		# Applications tab
		gtknotebook.append_page(ApplicationsTab(self), Gtk.Label(label=_('Applications')))
			# T: Heading in preferences dialog


	def _add_font_selection(self, table):
		# need to hardcode this, cannot register it as a preference
		table.add_inputs((
			('use_custom_font', 'bool', _('Use a custom font')),
			# T: option in preferences dialog
		))
		table.preferences_sections['use_custom_font'] = 'GtkInterface'

		self.fontbutton = Gtk.FontButton()
		self.fontbutton.set_use_font(True) # preview in button
		self.fontbutton.set_sensitive(False)
		text_style = ConfigManager.get_config_dict('style.conf')
		try:
			font = text_style['TextView']['font']
			if font:
				self.fontbutton.set_font_name(font)
				self.fontbutton.set_sensitive(True)
				table['use_custom_font'] = True
		except KeyError:
			pass

		table.widgets['use_custom_font'].connect('toggled',
			lambda o: self.fontbutton.set_sensitive(o.get_active()))

		self.fontbutton.set_size_request(100, -1)
		input_table_factory(((None, self.fontbutton),), table)

	def do_response_ok(self):
		# Get dynamic tabs
		newpreferences = {}
		for form in list(self.forms.values()):
			for key, value in list(form.items()):
				section = form.preferences_sections[key]
				if not section in newpreferences:
					newpreferences[section] = {}
				newpreferences[section][key] = value

		# Set font - special case, consider it a HACK
		customfont = newpreferences['GtkInterface'].pop('use_custom_font')
		if customfont:
			font = self.fontbutton.get_font_name()
		else:
			font = None

		text_style = ConfigManager.get_config_dict('style.conf')
		text_style['TextView'].define(font=String(None))
		text_style['TextView']['font'] = font
		#

		with self.preferences.block_signals('changed'):
			# note we do not block signal on section dicts
			for section in newpreferences:
				self.preferences[section].update(newpreferences[section])

		self.preferences.emit('changed') # delayed emission

		return True

	def do_response_cancel(self):
		# Obtain an updated list of loaded plugins
		now_loaded = list(self.plugins)

		# Restore previous situation if the user changed something
		# in this dialog session
		with self.preferences.block_signals('changed'):
			for name in self.plugins.list_installed_plugins():
				if name in self.p_save_loaded and name not in now_loaded:
					try:
						self.plugins.load_plugin(name)
					except:
						logger.exception('Could not restore plugin: %s', name)
				elif name not in self.p_save_loaded and name in now_loaded:
					self.plugins.remove_plugin(name)

		self.preferences.emit('changed') # delayed emission

		return True