[preview] New Config Dialog

This commit is contained in:
Philipp Hörist
2017-11-01 12:33:02 +01:00
parent 377c9fc9b8
commit e52ab4c627
3 changed files with 117 additions and 250 deletions

View File

@@ -0,0 +1,114 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2017 Philipp Hörist <philipp AT hoerist.com>
#
# 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 <http://www.gnu.org/licenses/>.
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

View File

@@ -1,188 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<requires lib="gtk+" version="3.0"/>
<object class="GtkListStore" id="liststore1">
<columns>
<!-- column-name Text -->
<column type="gchararray"/>
</columns>
<data>
<row>
<col id="0" translatable="yes">256 KiB</col>
</row>
<row>
<col id="0" translatable="yes">512 KiB</col>
</row>
<row>
<col id="0" translatable="yes">1 MiB</col>
</row>
<row>
<col id="0" translatable="yes">5 MiB</col>
</row>
<row>
<col id="0" translatable="yes">10 MiB</col>
</row>
</data>
</object>
<object class="GtkListStore" id="liststore2">
<columns>
<!-- column-name Text -->
<column type="gchararray"/>
</columns>
<data>
<row>
<col id="0" translatable="yes">Open</col>
</row>
<row>
<col id="0" translatable="yes">Save as</col>
</row>
<row>
<col id="0" translatable="yes">Copy Link Location</col>
</row>
<row>
<col id="0" translatable="yes">Open Link in Browser</col>
</row>
<row>
<col id="0" translatable="yes">Open Downloaded File in Browser</col>
</row>
</data>
</object>
<object class="GtkWindow" id="window1">
<property name="can_focus">False</property>
<child>
<object class="GtkVBox" id="vbox1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="border_width">9</property>
<child>
<object class="GtkFrame" id="frame1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label_xalign">0</property>
<property name="shadow_type">none</property>
<child>
<object class="GtkTable" id="table1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="n_rows">3</property>
<property name="n_columns">2</property>
<child>
<object class="GtkSpinButton" id="preview_size">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="invisible_char">&#x25CF;</property>
<property name="width_chars">6</property>
<property name="primary_icon_activatable">False</property>
<property name="secondary_icon_activatable">False</property>
<property name="primary_icon_sensitive">True</property>
<property name="secondary_icon_sensitive">True</property>
<property name="snap_to_ticks">True</property>
<property name="numeric">True</property>
<signal name="value-changed" handler="preview_size_value_changed" swapped="no"/>
</object>
<packing>
<property name="left_attach">1</property>
<property name="right_attach">2</property>
<property name="y_options"/>
</packing>
</child>
<child>
<object class="GtkComboBox" id="max_size_combobox">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="model">liststore1</property>
<signal name="changed" handler="max_size_value_changed" swapped="no"/>
<child>
<object class="GtkCellRendererText" id="cellrenderertext1"/>
<attributes>
<attribute name="text">0</attribute>
</attributes>
</child>
</object>
<packing>
<property name="left_attach">1</property>
<property name="right_attach">2</property>
<property name="top_attach">1</property>
<property name="bottom_attach">2</property>
<property name="y_options">GTK_EXPAND</property>
</packing>
</child>
<child>
<object class="GtkComboBox" id="leftclick_action_combobox">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="model">liststore2</property>
<signal name="changed" handler="leftclick_action_changed" swapped="no"/>
<child>
<object class="GtkCellRendererText" id="cellrenderertext2"/>
<attributes>
<attribute name="text">0</attribute>
</attributes>
</child>
</object>
<packing>
<property name="left_attach">1</property>
<property name="right_attach">2</property>
<property name="top_attach">2</property>
<property name="bottom_attach">3</property>
<property name="y_options">GTK_EXPAND</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="max_size_label">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="xalign">0</property>
<property name="xpad">13</property>
<property name="label" translatable="yes">Accept files smaller then</property>
<property name="track_visited_links">False</property>
</object>
<packing>
<property name="top_attach">1</property>
<property name="bottom_attach">2</property>
<property name="y_options">GTK_EXPAND</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="preview_size_label">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="xalign">0</property>
<property name="xpad">12</property>
<property name="label" translatable="yes">Preview size</property>
<property name="track_visited_links">False</property>
</object>
<packing>
<property name="y_options">GTK_EXPAND</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="leftclick_action_label">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="xalign">0</property>
<property name="xpad">12</property>
<property name="label" translatable="yes">Left click action</property>
<property name="track_visited_links">False</property>
</object>
<packing>
<property name="y_options">GTK_EXPAND</property>
<property name="top_attach">2</property>
<property name="bottom_attach">3</property>
<property name="y_options">GTK_EXPAND</property>
</packing>
</child>
</object>
</child>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="padding">6</property>
<property name="position">0</property>
</packing>
</child>
</object>
</child>
</object>
</interface>

View File

@@ -22,6 +22,7 @@ import binascii
from urllib.parse import urlparse from urllib.parse import urlparse
from io import BytesIO from io import BytesIO
import shutil import shutil
from functools import partial
import logging import logging
import nbxmpp import nbxmpp
@@ -35,6 +36,7 @@ from gajim.plugins.helpers import log_calls
from gajim.plugins.gui import GajimPluginConfigDialog from gajim.plugins.gui import GajimPluginConfigDialog
from gajim.conversation_textview import TextViewImage from gajim.conversation_textview import TextViewImage
from .http_functions import get_http_head, get_http_file from .http_functions import get_http_head, get_http_file
from .config_dialog import UrlImagePreviewConfigDialog
log = logging.getLogger('gajim.plugin_system.url_image_preview') log = logging.getLogger('gajim.plugin_system.url_image_preview')
@@ -68,7 +70,7 @@ class UrlImagePreviewPlugin(GajimPlugin):
def init(self): def init(self):
if not decryption_available: if not decryption_available:
self.available_text = DEP_MSG self.available_text = DEP_MSG
self.config_dialog = UrlImagePreviewPluginConfigDialog(self) self.config_dialog = partial(UrlImagePreviewConfigDialog, self)
self.events_handlers = {} self.events_handlers = {}
self.events_handlers['message-received'] = ( self.events_handlers['message-received'] = (
ged.PRECORE, self.handle_message_received) ged.PRECORE, self.handle_message_received)
@@ -626,64 +628,3 @@ class Base(object):
def disconnect_from_chat_control(self): def disconnect_from_chat_control(self):
pass 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()]