diff --git a/url_image_preview/config_dialog.py b/url_image_preview/config_dialog.py new file mode 100644 index 0000000..0bf433f --- /dev/null +++ b/url_image_preview/config_dialog.py @@ -0,0 +1,114 @@ +# -*- coding: utf-8 -*- +# +# Copyright (C) 2017 Philipp Hörist +# +# This file is part of Gajim. +# +# Gajim is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Gajim is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Gajim. If not, see . + +from gi.repository import GObject +from gi.repository import Gtk + +from gajim.options_dialog import OptionsDialog, GenericOption, SpinOption +from gajim.common.const import Option, OptionType + + +class UrlImagePreviewConfigDialog(OptionsDialog): + def __init__(self, plugin, parent): + + sizes = [('256 KiB', '262144'), + ('512 KiB', '524288'), + ('1 MiB', '1048576'), + ('5 MiB', '5242880'), + ('10 MiB', '10485760')] + actions = [ + (_('Open'), 'open_menuitem'), + (_('Save as'), 'save_as_menuitem'), + (_('Copy Link Location'), 'copy_link_location_menuitem'), + (_('Open Link in Browser'), 'open_link_in_browser_menuitem'), + (_('Open File in Browser'), 'open_file_in_browser_menuitem')] + + self.plugin = plugin + options = [ + Option('PreviewSizeSpinOption', _('Preview size'), + OptionType.VALUE, self.plugin.config['PREVIEW_SIZE'], + callback=self.on_option, data='PREVIEW_SIZE', + props={'range_': (100, 1000)}), + + Option('PreviewComboOption', _('Accepted filesize'), + OptionType.VALUE, self.plugin.config['MAX_FILE_SIZE'], + callback=self.on_option, data='MAX_FILE_SIZE', + props={'items': sizes, + 'plugin': self.plugin}), + + Option('PreviewComboOption', _('Left click action'), + OptionType.VALUE, self.plugin.config['LEFTCLICK_ACTION'], + callback=self.on_option, data='LEFTCLICK_ACTION', + props={'items': actions, + 'plugin': self.plugin}), + ] + + OptionsDialog.__init__(self, parent, _('UrlImagePreview Options'), + Gtk.DialogFlags.MODAL, options, None, + extend=[ + ('PreviewComboOption', ComboOption), + ('PreviewSizeSpinOption', SizeSpinOption)]) + + def on_option(self, value, data): + self.plugin.config[data] = value + + +class SizeSpinOption(SpinOption): + + __gproperties__ = { + "option-value": (int, 'Size', '', 100, 1000, 300, + GObject.ParamFlags.READWRITE), } + + def __init__(self, *args, **kwargs): + SpinOption.__init__(self, *args, **kwargs) + + +class ComboOption(GenericOption): + + __gproperties__ = { + "option-value": (str, 'Value', '', '', + GObject.ParamFlags.READWRITE), } + + def __init__(self, *args, items, plugin): + GenericOption.__init__(self, *args) + self.plugin = plugin + self.combo = Gtk.ComboBox() + text_renderer = Gtk.CellRendererText() + self.combo.pack_start(text_renderer, True) + self.combo.add_attribute(text_renderer, 'text', 0) + + self.store = Gtk.ListStore(str, str) + for item in items: + self.store.append(item) + + self.combo.set_model(self.store) + self.combo.set_id_column(1) + self.combo.set_active_id(str(self.option_value)) + + self.combo.connect('changed', self.on_value_change) + self.combo.set_valign(Gtk.Align.CENTER) + + self.option_box.pack_start(self.combo, True, True, 0) + self.show_all() + + def on_value_change(self, combo): + self.set_value(combo.get_active_id()) + + def on_row_activated(self): + pass diff --git a/url_image_preview/config_dialog.ui b/url_image_preview/config_dialog.ui deleted file mode 100644 index c43e685..0000000 --- a/url_image_preview/config_dialog.ui +++ /dev/null @@ -1,188 +0,0 @@ - - - - - - - - - - - 256 KiB - - - 512 KiB - - - 1 MiB - - - 5 MiB - - - 10 MiB - - - - - - - - - - - Open - - - Save as - - - Copy Link Location - - - Open Link in Browser - - - Open Downloaded File in Browser - - - - - False - - - True - False - 9 - - - True - False - 0 - none - - - True - False - 3 - 2 - - - True - True - - 6 - False - False - True - True - True - True - - - - 1 - 2 - - - - - - True - False - liststore1 - - - - - 0 - - - - - 1 - 2 - 1 - 2 - GTK_EXPAND - - - - - True - False - liststore2 - - - - - 0 - - - - - 1 - 2 - 2 - 3 - GTK_EXPAND - - - - - True - False - 0 - 13 - Accept files smaller then - False - - - 1 - 2 - GTK_EXPAND - - - - - True - False - 0 - 12 - Preview size - False - - - GTK_EXPAND - - - - - True - False - 0 - 12 - Left click action - False - - - GTK_EXPAND - 2 - 3 - GTK_EXPAND - - - - - - - True - True - 6 - 0 - - - - - - diff --git a/url_image_preview/url_image_preview.py b/url_image_preview/url_image_preview.py index 1ff5c9a..1d73898 100644 --- a/url_image_preview/url_image_preview.py +++ b/url_image_preview/url_image_preview.py @@ -22,6 +22,7 @@ import binascii from urllib.parse import urlparse from io import BytesIO import shutil +from functools import partial import logging import nbxmpp @@ -35,6 +36,7 @@ from gajim.plugins.helpers import log_calls from gajim.plugins.gui import GajimPluginConfigDialog from gajim.conversation_textview import TextViewImage from .http_functions import get_http_head, get_http_file +from .config_dialog import UrlImagePreviewConfigDialog log = logging.getLogger('gajim.plugin_system.url_image_preview') @@ -68,7 +70,7 @@ class UrlImagePreviewPlugin(GajimPlugin): def init(self): if not decryption_available: self.available_text = DEP_MSG - self.config_dialog = UrlImagePreviewPluginConfigDialog(self) + self.config_dialog = partial(UrlImagePreviewConfigDialog, self) self.events_handlers = {} self.events_handlers['message-received'] = ( ged.PRECORE, self.handle_message_received) @@ -626,64 +628,3 @@ class Base(object): def disconnect_from_chat_control(self): pass - - -class UrlImagePreviewPluginConfigDialog(GajimPluginConfigDialog): - max_file_size = [262144, 524288, 1048576, 5242880, 10485760] - leftclick_action = ['open_menuitem', 'save_as_menuitem', 'copy_link_location_menuitem', - 'open_link_in_browser_menuitem', 'open_file_in_browser_menuitem'] - - def init(self): - self.GTK_BUILDER_FILE_PATH = self.plugin.local_file_path( - 'config_dialog.ui') - self.xml = Gtk.Builder() - self.xml.set_translation_domain('gajim_plugins') - self.xml.add_objects_from_file(self.GTK_BUILDER_FILE_PATH, [ - 'vbox1', 'liststore1', 'liststore2']) - self.preview_size_spinbutton = self.xml.get_object('preview_size') - self.preview_size_spinbutton.get_adjustment().configure(20, 10, 512, 1, - 10, 0) - self.max_size_combobox = self.xml.get_object('max_size_combobox') - self.leftclick_action_combobox = self.xml.get_object('leftclick_action_combobox') - vbox = self.xml.get_object('vbox1') - self.get_child().pack_start(vbox, True, True, 0) - - self.xml.connect_signals(self) - - def on_run(self): - self.preview_size_spinbutton.set_value(self.plugin.config[ - 'PREVIEW_SIZE']) - value = self.plugin.config['MAX_FILE_SIZE'] - if value: - # this fails if we upgrade from an old version - # which has other file size values than we have now - try: - self.max_size_combobox.set_active( - self.max_file_size.index(value)) - except: - pass - else: - self.max_size_combobox.set_active(-1) - - value = self.plugin.config['LEFTCLICK_ACTION'] - if value: - # this fails if we upgrade from an old version - # which has other file size values than we have now - try: - self.leftclick_action_combobox.set_active( - self.leftclick_action.index(value)) - except: - pass - else: - self.leftclick_action_combobox.set_active(0) - - def preview_size_value_changed(self, spinbutton): - self.plugin.config['PREVIEW_SIZE'] = spinbutton.get_value() - - def max_size_value_changed(self, widget): - self.plugin.config['MAX_FILE_SIZE'] = self.max_file_size[ - self.max_size_combobox.get_active()] - - def leftclick_action_changed(self, widget): - self.plugin.config['LEFTCLICK_ACTION'] = self.leftclick_action[ - self.leftclick_action_combobox.get_active()]