2016-06-11 10:36:52 +00:00
|
|
|
import util
|
|
|
|
import json
|
2016-06-11 12:37:52 +00:00
|
|
|
import os
|
2016-06-11 10:36:52 +00:00
|
|
|
try:
|
|
|
|
from PySide import QtCore
|
|
|
|
except ImportError:
|
|
|
|
from PyQt4 import QtCore
|
|
|
|
|
|
|
|
|
|
|
|
class SmileyLoader(util.Singleton):
|
2016-06-11 12:37:52 +00:00
|
|
|
"""
|
|
|
|
Class which loads smileys packs and insert smileys into messages
|
|
|
|
"""
|
2016-06-11 10:36:52 +00:00
|
|
|
|
|
|
|
def __init__(self, settings):
|
|
|
|
self.settings = settings
|
2016-06-11 12:37:52 +00:00
|
|
|
self.curr_pack = None # current pack name
|
|
|
|
self.smileys = {} # smileys dict. key - smiley (str), value - path to image (str)
|
2016-06-11 10:36:52 +00:00
|
|
|
self.load_pack()
|
|
|
|
|
|
|
|
def load_pack(self):
|
2016-06-11 12:37:52 +00:00
|
|
|
"""
|
|
|
|
Loads smiley pack
|
|
|
|
"""
|
2016-06-11 10:36:52 +00:00
|
|
|
pack_name = self.settings['smiley_pack']
|
|
|
|
if self.settings['smileys'] and self.curr_pack != pack_name:
|
|
|
|
self.curr_pack = pack_name
|
|
|
|
path = self.get_smileys_path() + 'config.json'
|
|
|
|
try:
|
|
|
|
with open(path) as fl:
|
2016-06-11 12:37:52 +00:00
|
|
|
self.smileys = json.loads(fl.read())
|
2016-06-11 10:36:52 +00:00
|
|
|
print 'Smiley pack', pack_name, 'loaded'
|
|
|
|
except:
|
|
|
|
print 'Smiley pack', pack_name, 'was not loaded'
|
|
|
|
|
|
|
|
def get_smileys_path(self):
|
|
|
|
return util.curr_directory() + '/smileys/' + self.curr_pack + '/'
|
|
|
|
|
2016-06-11 12:37:52 +00:00
|
|
|
def get_packs_list(self):
|
|
|
|
d = util.curr_directory() + '/smileys/'
|
|
|
|
return [x[1] for x in os.walk(d)][0]
|
|
|
|
|
2016-06-11 10:36:52 +00:00
|
|
|
def add_smileys_to_text(self, text, edit):
|
2016-06-11 12:37:52 +00:00
|
|
|
"""
|
|
|
|
Adds smileys to text
|
|
|
|
:param text: message
|
|
|
|
:param edit: MessageEdit instance
|
|
|
|
:return text with smileys
|
|
|
|
"""
|
2016-06-11 10:36:52 +00:00
|
|
|
if not self.settings['smileys']:
|
|
|
|
return text
|
|
|
|
arr = text.split(' ')
|
|
|
|
for i in range(len(arr)):
|
2016-06-11 12:37:52 +00:00
|
|
|
if arr[i] in self.smileys:
|
|
|
|
file_name = self.smileys[arr[i]] # image name
|
2016-06-11 10:36:52 +00:00
|
|
|
arr[i] = u'<img title=\"{}\" src=\"{}\" />'.format(arr[i], file_name)
|
2016-06-11 12:37:52 +00:00
|
|
|
if file_name.endswith('.gif'): # animated smiley
|
2016-06-11 10:36:52 +00:00
|
|
|
edit.addAnimation(QtCore.QUrl(file_name), self.get_smileys_path() + file_name)
|
|
|
|
return ' '.join(arr)
|