[acronyms] Add config dialog
This commit is contained in:
@@ -18,6 +18,7 @@
|
|||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
from functools import partial
|
||||||
|
|
||||||
from gi.repository import GLib
|
from gi.repository import GLib
|
||||||
|
|
||||||
@@ -27,6 +28,7 @@ from gajim.plugins import GajimPlugin
|
|||||||
from gajim.plugins.plugins_i18n import _
|
from gajim.plugins.plugins_i18n import _
|
||||||
|
|
||||||
from acronyms_expander.acronyms import DEFAULT_DATA
|
from acronyms_expander.acronyms import DEFAULT_DATA
|
||||||
|
from acronyms_expander.gtk.config import ConfigDialog
|
||||||
|
|
||||||
log = logging.getLogger('gajim.plugin_system.acronyms')
|
log = logging.getLogger('gajim.plugin_system.acronyms')
|
||||||
|
|
||||||
@@ -35,36 +37,51 @@ class AcronymsExpanderPlugin(GajimPlugin):
|
|||||||
def init(self):
|
def init(self):
|
||||||
self.description = _('Replaces acronyms (or other strings) '
|
self.description = _('Replaces acronyms (or other strings) '
|
||||||
'with given expansions/substitutes.')
|
'with given expansions/substitutes.')
|
||||||
self.config_dialog = None
|
self.config_dialog = partial(ConfigDialog, self)
|
||||||
self.gui_extension_points = {
|
self.gui_extension_points = {
|
||||||
'chat_control_base': (self._connect, self._disconnect)
|
'chat_control_base': (self._connect, self._disconnect)
|
||||||
}
|
}
|
||||||
self._invoker = ' '
|
self._invoker = ' '
|
||||||
self._acronyms = self._load_acronyms()
|
|
||||||
self._replace_in_progress = False
|
self._replace_in_progress = False
|
||||||
self._handler_ids = {}
|
self._handler_ids = {}
|
||||||
|
|
||||||
|
self.acronyms = self._load_acronyms()
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _load_acronyms():
|
def _load_acronyms():
|
||||||
try:
|
try:
|
||||||
path = Path(configpaths.get('PLUGINS_DATA')) / 'acronyms'
|
data_path = Path(configpaths.get('PLUGINS_DATA'))
|
||||||
except KeyError:
|
except KeyError:
|
||||||
# PLUGINS_DATA was added in 1.0.99.1
|
# PLUGINS_DATA was added in 1.0.99.1
|
||||||
return DEFAULT_DATA
|
return DEFAULT_DATA
|
||||||
|
|
||||||
|
path = data_path / 'acronyms' / 'acronyms'
|
||||||
if not path.exists():
|
if not path.exists():
|
||||||
return DEFAULT_DATA
|
return DEFAULT_DATA
|
||||||
|
|
||||||
with open(path / 'acronyms', 'r') as file:
|
with open(path, 'r') as file:
|
||||||
acronyms = json.load(file)
|
acronyms = json.load(file)
|
||||||
return acronyms
|
return acronyms
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _save_acronyms(acronyms):
|
def _save_acronyms(acronyms):
|
||||||
path = Path(configpaths.get('PLUGINS_DATA')) / 'acronyms'
|
try:
|
||||||
|
data_path = Path(configpaths.get('PLUGINS_DATA'))
|
||||||
|
except KeyError:
|
||||||
|
# PLUGINS_DATA was added in 1.0.99.1
|
||||||
|
return
|
||||||
|
|
||||||
|
path = data_path / 'acronyms'
|
||||||
|
if not path.exists():
|
||||||
|
path.mkdir(parents=True)
|
||||||
|
|
||||||
with open(path / 'acronyms', 'w') as file:
|
with open(path / 'acronyms', 'w') as file:
|
||||||
json.dump(acronyms, file)
|
json.dump(acronyms, file)
|
||||||
|
|
||||||
|
def set_acronyms(self, acronyms):
|
||||||
|
self.acronyms = acronyms
|
||||||
|
self._save_acronyms(acronyms)
|
||||||
|
|
||||||
def _on_buffer_changed(self, _textview, buffer_):
|
def _on_buffer_changed(self, _textview, buffer_):
|
||||||
if self._replace_in_progress:
|
if self._replace_in_progress:
|
||||||
return
|
return
|
||||||
@@ -91,7 +108,7 @@ class AcronymsExpanderPlugin(GajimPlugin):
|
|||||||
# Get last word and cut invoker
|
# Get last word and cut invoker
|
||||||
last_word = word_start_iter.get_slice(insert_iter).strip()
|
last_word = word_start_iter.get_slice(insert_iter).strip()
|
||||||
|
|
||||||
substitute = self._acronyms.get(last_word)
|
substitute = self.acronyms.get(last_word)
|
||||||
if substitute is None:
|
if substitute is None:
|
||||||
log.debug('%s not an acronym', last_word)
|
log.debug('%s not an acronym', last_word)
|
||||||
return
|
return
|
||||||
|
|||||||
90
acronyms_expander/gtk/config.py
Normal file
90
acronyms_expander/gtk/config.py
Normal file
@@ -0,0 +1,90 @@
|
|||||||
|
# Copyright (C) 2018 Philipp Hörist <philipp AT hoerist.com>
|
||||||
|
#
|
||||||
|
# This file is part of Acronyms Expander.
|
||||||
|
#
|
||||||
|
# Acronyms Expander 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; version 3 only.
|
||||||
|
#
|
||||||
|
# Acronyms Expander 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 Acronyms Expander. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
from gi.repository import Gtk
|
||||||
|
from gi.repository import Gdk
|
||||||
|
|
||||||
|
from gajim.common import app
|
||||||
|
|
||||||
|
from gajim.plugins.plugins_i18n import _
|
||||||
|
from gajim.plugins.helpers import get_builder
|
||||||
|
|
||||||
|
|
||||||
|
class ConfigDialog(Gtk.ApplicationWindow):
|
||||||
|
def __init__(self, plugin, transient):
|
||||||
|
Gtk.ApplicationWindow.__init__(self)
|
||||||
|
self.set_application(app.app)
|
||||||
|
self.set_show_menubar(False)
|
||||||
|
self.set_title(_('Acronyms Configuration'))
|
||||||
|
self.set_transient_for(transient)
|
||||||
|
self.set_default_size(400, 400)
|
||||||
|
self.set_type_hint(Gdk.WindowTypeHint.DIALOG)
|
||||||
|
self.set_modal(True)
|
||||||
|
self.set_destroy_with_parent(True)
|
||||||
|
|
||||||
|
ui_path = Path(__file__).parent
|
||||||
|
self._ui = get_builder(ui_path / 'config.ui')
|
||||||
|
|
||||||
|
self._plugin = plugin
|
||||||
|
|
||||||
|
self.add(self._ui.grid)
|
||||||
|
|
||||||
|
self._fill_list()
|
||||||
|
self.show_all()
|
||||||
|
|
||||||
|
self._ui.connect_signals(self)
|
||||||
|
self.connect('destroy', self._on_destroy)
|
||||||
|
|
||||||
|
def _fill_list(self):
|
||||||
|
for acronym, substitute in self._plugin.acronyms.items():
|
||||||
|
self._ui.acronyms_store.append([acronym, substitute])
|
||||||
|
|
||||||
|
def _on_acronym_edited(self, _renderer, path, new_text):
|
||||||
|
iter_ = self._ui.acronyms_store.get_iter(path)
|
||||||
|
self._ui.acronyms_store.set_value(iter_, 0, new_text)
|
||||||
|
|
||||||
|
def _on_substitute_edited(self, _renderer, path, new_text):
|
||||||
|
iter_ = self._ui.acronyms_store.get_iter(path)
|
||||||
|
self._ui.acronyms_store.set_value(iter_, 1, new_text)
|
||||||
|
|
||||||
|
def _on_add_clicked(self, _button):
|
||||||
|
self._ui.acronyms_store.append(['', ''])
|
||||||
|
row = self._ui.acronyms_store[-1]
|
||||||
|
self._ui.acronyms_treeview.scroll_to_cell(
|
||||||
|
row.path, None, False, 0, 0)
|
||||||
|
self._ui.selection.unselect_all()
|
||||||
|
self._ui.selection.select_path(row.path)
|
||||||
|
|
||||||
|
def _on_remove_clicked(self, _button):
|
||||||
|
model, paths = self._ui.selection.get_selected_rows()
|
||||||
|
references = []
|
||||||
|
for path in paths:
|
||||||
|
references.append(Gtk.TreeRowReference.new(model, path))
|
||||||
|
|
||||||
|
for ref in references:
|
||||||
|
iter_ = model.get_iter(ref.get_path())
|
||||||
|
self._ui.acronyms_store.remove(iter_)
|
||||||
|
|
||||||
|
def _on_destroy(self, *args):
|
||||||
|
acronyms = {}
|
||||||
|
for row in self._ui.acronyms_store:
|
||||||
|
acronym, substitute = row
|
||||||
|
if not acronym or not substitute:
|
||||||
|
continue
|
||||||
|
acronyms[acronym] = substitute
|
||||||
|
self._plugin.set_acronyms(acronyms)
|
||||||
139
acronyms_expander/gtk/config.ui
Normal file
139
acronyms_expander/gtk/config.ui
Normal file
@@ -0,0 +1,139 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!-- Generated with glade 3.22.1 -->
|
||||||
|
<interface>
|
||||||
|
<requires lib="gtk+" version="3.20"/>
|
||||||
|
<object class="GtkListStore" id="acronyms_store">
|
||||||
|
<columns>
|
||||||
|
<!-- column-name acronym -->
|
||||||
|
<column type="gchararray"/>
|
||||||
|
<!-- column-name substitute -->
|
||||||
|
<column type="gchararray"/>
|
||||||
|
</columns>
|
||||||
|
</object>
|
||||||
|
<object class="GtkGrid" id="grid">
|
||||||
|
<property name="visible">True</property>
|
||||||
|
<property name="can_focus">False</property>
|
||||||
|
<property name="margin_left">12</property>
|
||||||
|
<property name="margin_right">12</property>
|
||||||
|
<property name="margin_top">12</property>
|
||||||
|
<property name="margin_bottom">12</property>
|
||||||
|
<property name="row_spacing">6</property>
|
||||||
|
<child>
|
||||||
|
<object class="GtkScrolledWindow">
|
||||||
|
<property name="visible">True</property>
|
||||||
|
<property name="can_focus">True</property>
|
||||||
|
<property name="hexpand">True</property>
|
||||||
|
<property name="vexpand">True</property>
|
||||||
|
<property name="shadow_type">in</property>
|
||||||
|
<child>
|
||||||
|
<object class="GtkTreeView" id="acronyms_treeview">
|
||||||
|
<property name="visible">True</property>
|
||||||
|
<property name="can_focus">True</property>
|
||||||
|
<property name="hexpand">True</property>
|
||||||
|
<property name="vexpand">True</property>
|
||||||
|
<property name="model">acronyms_store</property>
|
||||||
|
<property name="search_column">1</property>
|
||||||
|
<child internal-child="selection">
|
||||||
|
<object class="GtkTreeSelection" id="selection">
|
||||||
|
<property name="mode">multiple</property>
|
||||||
|
</object>
|
||||||
|
</child>
|
||||||
|
<child>
|
||||||
|
<object class="GtkTreeViewColumn">
|
||||||
|
<property name="resizable">True</property>
|
||||||
|
<property name="title" translatable="yes">Acronym</property>
|
||||||
|
<property name="clickable">True</property>
|
||||||
|
<property name="sort_indicator">True</property>
|
||||||
|
<property name="sort_column_id">0</property>
|
||||||
|
<child>
|
||||||
|
<object class="GtkCellRendererText">
|
||||||
|
<property name="editable">True</property>
|
||||||
|
<signal name="edited" handler="_on_acronym_edited" swapped="no"/>
|
||||||
|
</object>
|
||||||
|
<attributes>
|
||||||
|
<attribute name="text">0</attribute>
|
||||||
|
</attributes>
|
||||||
|
</child>
|
||||||
|
</object>
|
||||||
|
</child>
|
||||||
|
<child>
|
||||||
|
<object class="GtkTreeViewColumn">
|
||||||
|
<property name="resizable">True</property>
|
||||||
|
<property name="title" translatable="yes">Substitute</property>
|
||||||
|
<property name="clickable">True</property>
|
||||||
|
<property name="sort_indicator">True</property>
|
||||||
|
<property name="sort_column_id">0</property>
|
||||||
|
<child>
|
||||||
|
<object class="GtkCellRendererText">
|
||||||
|
<property name="editable">True</property>
|
||||||
|
<signal name="edited" handler="_on_substitute_edited" swapped="no"/>
|
||||||
|
</object>
|
||||||
|
<attributes>
|
||||||
|
<attribute name="text">1</attribute>
|
||||||
|
</attributes>
|
||||||
|
</child>
|
||||||
|
</object>
|
||||||
|
</child>
|
||||||
|
</object>
|
||||||
|
</child>
|
||||||
|
</object>
|
||||||
|
<packing>
|
||||||
|
<property name="left_attach">0</property>
|
||||||
|
<property name="top_attach">0</property>
|
||||||
|
</packing>
|
||||||
|
</child>
|
||||||
|
<child>
|
||||||
|
<object class="GtkButtonBox">
|
||||||
|
<property name="visible">True</property>
|
||||||
|
<property name="can_focus">False</property>
|
||||||
|
<child>
|
||||||
|
<object class="GtkButton">
|
||||||
|
<property name="visible">True</property>
|
||||||
|
<property name="can_focus">True</property>
|
||||||
|
<property name="receives_default">True</property>
|
||||||
|
<property name="halign">start</property>
|
||||||
|
<signal name="clicked" handler="_on_add_clicked" swapped="no"/>
|
||||||
|
<child>
|
||||||
|
<object class="GtkImage">
|
||||||
|
<property name="visible">True</property>
|
||||||
|
<property name="can_focus">False</property>
|
||||||
|
<property name="icon_name">list-add-symbolic</property>
|
||||||
|
</object>
|
||||||
|
</child>
|
||||||
|
<style>
|
||||||
|
<class name="suggested-action"/>
|
||||||
|
</style>
|
||||||
|
</object>
|
||||||
|
<packing>
|
||||||
|
<property name="expand">False</property>
|
||||||
|
<property name="fill">True</property>
|
||||||
|
<property name="position">0</property>
|
||||||
|
</packing>
|
||||||
|
</child>
|
||||||
|
<child>
|
||||||
|
<object class="GtkButton">
|
||||||
|
<property name="label" translatable="yes">Remove</property>
|
||||||
|
<property name="visible">True</property>
|
||||||
|
<property name="can_focus">True</property>
|
||||||
|
<property name="receives_default">True</property>
|
||||||
|
<property name="halign">end</property>
|
||||||
|
<signal name="clicked" handler="_on_remove_clicked" swapped="no"/>
|
||||||
|
<style>
|
||||||
|
<class name="destructive-action"/>
|
||||||
|
</style>
|
||||||
|
</object>
|
||||||
|
<packing>
|
||||||
|
<property name="expand">True</property>
|
||||||
|
<property name="fill">True</property>
|
||||||
|
<property name="pack_type">end</property>
|
||||||
|
<property name="position">1</property>
|
||||||
|
</packing>
|
||||||
|
</child>
|
||||||
|
</object>
|
||||||
|
<packing>
|
||||||
|
<property name="left_attach">0</property>
|
||||||
|
<property name="top_attach">1</property>
|
||||||
|
</packing>
|
||||||
|
</child>
|
||||||
|
</object>
|
||||||
|
</interface>
|
||||||
Reference in New Issue
Block a user