Commit fea91a10 authored by Alexander's avatar Alexander

[stickers] Some small code improvements

parent 2d88f372
......@@ -239,6 +239,58 @@ class StickersPlugin(GajimPlugin):
self._load_sticker_packs()
self._load_icon()
def reload_sticker_packs(self):
'''
Searches again for sticker packs and loads them.
'''
log.debug('Reloading sticker packs')
# To prevent duplicate sticker packs, first remove them all
self.sticker_pack_model.remove_all()
self._load_sticker_packs()
def delete_sticker_pack(self, id_):
'''
Starts the process to remove sticker packs from both
the accounts' PubSub nodes and the disk.
id_: The ID of the sticker pack to remove
'''
if id_ in self.sticker_pack_retractions:
return
for account in app.connections:
dict_append_or_set(self.sticker_pack_retractions, id_, account)
for account in app.connections:
app.connections[account].get_module('Stickers').retract_sticker_pack(id_)
def upload_sticker_pack(self, pack_id, on_use=False):
'''
Asks the user if the sticker pack should be uploaded.
pack_id: The ID of the sticker pack in question
on_use: Whether this function is called from the context of the attempt
to send a sticker (True).
'''
def on_confirm():
log.debug('Manually uploading sticker pack %s', pack_id)
pack = self.sticker_packs[pack_id]
for account in app.connections:
app.connections[account].get_module('Stickers').publish_pack(pack, upload=True)
if on_use:
text = _('Sending this sticker requires the upload of the sticker pack. Are you sure you want to upload the sticker pack?')
else:
text = _('Are you sure you want to upload the sticker pack?')
ConfirmationDialog(
_('Upload Sticker Pack'),
text,
_('This will also publish the sticker pack on your account(s).'),
[DialogButton.make('Cancel'),
DialogButton.make('Accept',
callback=on_confirm)]).show()
def _model_append(self, pack):
'''
Appends the StickerPackObject pack to the model.
......@@ -268,12 +320,34 @@ class StickersPlugin(GajimPlugin):
if index >= 0:
self.sticker_pack_model.insert(index, pack)
def __has_sticker_pack(self, id_):
def _load_icon(self):
# pylint: disable=attribute-defined-outside-init
icon_path = self.local_file_path('stickers.png')
pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale(icon_path,
24,
24,
True)
self._button_icon_pixbuf = pixbuf
def _has_sticker_pack(self, id_):
'''
Returns True if we know a sticker pack by this ID
'''
return id_ in self.sticker_packs and id_ not in self.sticker_requests
def _should_download_sticker_pack(self):
'''
Returns True when the user allows requesting new stickers. False
otherwise.
'''
return self.config['DOWNLOAD_NEW']
def _sticker_width(self):
'''
Returns the preferred sticker width
'''
return self.config['STICKER_WIDTH']
def _show_animations(self):
'''
Returns True when animated stickers should be shown as animations.
......@@ -299,30 +373,12 @@ class StickersPlugin(GajimPlugin):
app.connections[event.account].get_module('Stickers').request_sticker_packs()
self.signin_pack_requests.append(event.account)
def _load_icon(self):
# pylint: disable=attribute-defined-outside-init
icon_path = os.path.join(str(Path(__file__).parent), 'stickers.png')
pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale(icon_path,
24,
24,
True)
self._button_icon_pixbuf = pixbuf
def _on_sticker_pack_added(self, pack):
'''
Called when a new sticker pack has been added
'''
self._model_append(StickerPackObject.from_sticker_pack(pack))
def reload_sticker_packs(self):
'''
Searches again for sticker packs and loads them.
'''
log.debug('Reloading sticker packs')
# To prevent duplicate sticker packs, first remove them all
self.sticker_pack_model.remove_all()
self._load_sticker_packs()
def _load_sticker_packs(self):
path = sticker_data_path()
if not os.path.exists(path):
......@@ -396,48 +452,6 @@ class StickersPlugin(GajimPlugin):
mention.end)
del self.sticker_mentions[pack.id_]
def upload_sticker_pack(self, pack_id, on_use=False):
'''
Asks the user if the sticker pack should be uploaded.
pack_id: The ID of the sticker pack in question
on_use: Whether this function is called from the context of the attempt
to send a sticker (True).
'''
def on_confirm():
log.debug('Manually uploading sticker pack %s', pack_id)
pack = self.sticker_packs[pack_id]
for account in app.connections:
app.connections[account].get_module('Stickers').publish_pack(pack, upload=True)
if on_use:
text = _('Sending this sticker requires the upload of the sticker pack. Are you sure you want to upload the sticker pack?')
else:
text = _('Are you sure you want to upload the sticker pack?')
ConfirmationDialog(
_('Upload Sticker Pack'),
text,
_('This will also publish the sticker pack on your account(s).'),
[DialogButton.make('Cancel'),
DialogButton.make('Accept',
callback=on_confirm)]).show()
def delete_sticker_pack(self, id_):
'''
Starts the process to remove sticker packs from both
the accounts' PubSub nodes and the disk.
id_: The ID of the sticker pack to remove
'''
if id_ in self.sticker_pack_retractions:
return
for account in app.connections:
dict_append_or_set(self.sticker_pack_retractions, id_, account)
for account in app.connections:
app.connections[account].get_module('Stickers').retract_sticker_pack(id_)
def _on_sticker_pack_retracted(self, event):
self.sticker_pack_retractions[event.id_].remove(event.account)
if self.sticker_pack_retractions[event.id_]:
......@@ -573,25 +587,11 @@ class StickersPlugin(GajimPlugin):
}
# Download the sticker pack if we don't have it
if (not self.__has_sticker_pack(pack_id) and
if (not self._has_sticker_pack(pack_id) and
self._should_download_sticker_pack()):
self.sticker_requests.append(pack_id)
app.connections[event.account].get_module('Stickers').request_sticker_pack(pack_id,
jid)
def _sticker_width(self):
'''
Returns the preferred sticker width
'''
return self.config['STICKER_WIDTH']
def _should_download_sticker_pack(self):
'''
Returns True when the user allows requesting new stickers. False
otherwise.
'''
return self.config['DOWNLOAD_NEW']
# Taken from url_image_preview
@staticmethod
def _print_text(buffer_, iter_, text):
......@@ -674,7 +674,7 @@ class StickersPlugin(GajimPlugin):
if 'sticker' in additional_data:
# Find the correct sticker pack
pack_id = additional_data['sticker']['pack_id']
if not self.__has_sticker_pack(pack_id):
if not self._has_sticker_pack(pack_id):
if not self._should_download_sticker_pack():
from_ = additional_data['sticker']['from']
account = additional_data['sticker']['account']
......@@ -847,6 +847,8 @@ class StickersButton(Gtk.Button):
'''
if (not self._query or
any([x.startswith(self._query) for x in pack.sticker_strings])):
# NOTE: This function gets called the first time before we have
# set up the "submodels". In that case, we just ignore them.
if pack.id_ in self._submodels:
# Update the search within the sticker pack
self._submodels[pack.id_].invalidate_filter()
......@@ -856,10 +858,17 @@ class StickersButton(Gtk.Button):
def _send_sticker_lambda(self, sticker, pack_id, uploaded):
'''
To prevent weirdness, create a lambda to perform
the actual sending.
In order to react to clicks on stickers, we capture some metadata
in the closure.
sticker: The sticker that will be sent
pack_id: The ID of the sticker pack that the sticker belongs to
uploaded: True if the sticker pack has been uploaded
Returns a function (button -> None) that can be bound to the button's
'clicked' event. It will close the popover and send the sticker.
'''
def actually_send_sticker(argument):
def wrapper(argument):
# pylint: disable=unused-argument
if not uploaded:
self._upload_sticker_pack(pack_id, on_use=True)
......@@ -868,7 +877,7 @@ class StickersButton(Gtk.Button):
self._popover.popdown()
self._send_sticker(sticker, pack_id)
return actually_send_sticker
return wrapper
def _send_sticker(self, sticker, pack_id):
sticker_node = Node(tag='sticker',
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment