[birthday] Rewrite Plugin
The plugin did not work with Gajim 1.0.3, but will now with Gajim 1.1.0
This commit is contained in:
Binary file not shown.
|
Before Width: | Height: | Size: 6.8 KiB |
@@ -1,8 +1,9 @@
|
|||||||
[info]
|
[info]
|
||||||
name: Birthday reminder
|
name: Birthday reminder
|
||||||
short_name: birthday_reminder
|
short_name: birthday_reminder
|
||||||
version: 0.0.5
|
version: 1.0.0
|
||||||
description: Birthday reminder plugin. Reminder before 5,3,1 and birthday days.
|
description: Reminds you if a contact of yours has birthday
|
||||||
authors: Evgeniy Popov <evgeniypopov@gmail.com>
|
authors: Evgeniy Popov <evgeniypopov@gmail.com>
|
||||||
homepage: http://trac-plugins.gajim.org/wiki/BirthdayReminderPlugin
|
Philipp Hörist <philipp@hoerist.com>
|
||||||
min_gajim_version: 0.16.11
|
homepage: https://dev.gajim.org/gajim/gajim-plugins/wikis/BirthdayReminderPlugin
|
||||||
|
min_gajim_version: 1.0.99
|
||||||
|
|||||||
@@ -1,119 +1,115 @@
|
|||||||
import os
|
import os
|
||||||
import glob
|
import json
|
||||||
import datetime
|
import datetime
|
||||||
from xml.dom.minidom import *
|
import logging
|
||||||
from gi.repository import GObject
|
|
||||||
|
from gi.repository import GLib
|
||||||
|
|
||||||
from gajim.plugins import GajimPlugin
|
from gajim.plugins import GajimPlugin
|
||||||
from gajim.plugins.helpers import log_calls
|
|
||||||
from gajim.notify import popup
|
|
||||||
|
|
||||||
from gajim.common import configpaths
|
from gajim.common import configpaths
|
||||||
from gajim.common import app
|
from gajim.common import app
|
||||||
from gajim.common import ged
|
from gajim.common import ged
|
||||||
|
|
||||||
|
log = logging.getLogger('gajim.plugin_system.birthday')
|
||||||
|
|
||||||
|
TITLE = _('%s has birthday today')
|
||||||
|
TEXT = _('Send him a message')
|
||||||
|
|
||||||
|
|
||||||
class BirthDayPlugin(GajimPlugin):
|
class BirthDayPlugin(GajimPlugin):
|
||||||
|
|
||||||
@log_calls('BirthDayPlugin')
|
|
||||||
def init(self):
|
def init(self):
|
||||||
|
|
||||||
self.config_dialog = None
|
self.config_dialog = None
|
||||||
self.description = ('Birthday reminder plugin')
|
self.description = ('Birthday reminder plugin')
|
||||||
self.events_handlers = {
|
self.events_handlers = {
|
||||||
'roster-received': (ged.GUI2, self.roster_received)}
|
'vcard-received': (ged.GUI2, self._vcard_received)}
|
||||||
configpath = configpaths.ConfigPaths()
|
|
||||||
cache_path = configpath.cache_root
|
self.timeout_id = None
|
||||||
self.vcard_path = os.path.join(cache_path, 'vcards') + os.sep
|
self._timeout_id_start = None
|
||||||
self.timeout_id = 0
|
|
||||||
self.showed_accounts = []
|
self.showed_accounts = []
|
||||||
|
|
||||||
def check_birthdays(self, account=None):
|
self._birthdays = {}
|
||||||
def show_popup(account, jid):
|
self._load_birthdays()
|
||||||
contact_instances = app.contacts.get_contacts(account, jid)
|
|
||||||
contact = app.contacts.get_highest_prio_contact_from_contacts(
|
|
||||||
contact_instances)
|
|
||||||
if contact:
|
|
||||||
nick = GObject.markup_escape_text(contact.get_shown_name())
|
|
||||||
try:
|
|
||||||
image = os.path.dirname(__file__) + os.sep + \
|
|
||||||
'birthday_reminder_large.png'
|
|
||||||
except:
|
|
||||||
image = None
|
|
||||||
|
|
||||||
popup('Send message', contact.jid, account, type_='',
|
def activate(self):
|
||||||
path_to_image=image, title=title, text=text + ' ' + nick)
|
self._timeout_id_start = GLib.timeout_add_seconds(
|
||||||
|
5, self._check_birthdays_at_start)
|
||||||
|
self._timeout_id = GLib.timeout_add_seconds(
|
||||||
|
86400, self._check_birthdays)
|
||||||
|
|
||||||
accounts = app.contacts.get_accounts()
|
def deactivate(self):
|
||||||
vcards = []
|
if self._timeout_id is not None:
|
||||||
date_dict = {}
|
GLib.source_remove(self.timeout_id)
|
||||||
for jid in glob.glob(self.vcard_path + '*@*'):
|
if self._timeout_id_start is not None:
|
||||||
if os.path.isfile(jid):
|
GLib.source_remove(self._timeout_id_start)
|
||||||
vcards.append(jid)
|
|
||||||
|
|
||||||
for xmldoc in vcards:
|
def _load_birthdays(self):
|
||||||
|
path = os.path.join(configpaths.get('MY_DATA'), 'birthdays.json')
|
||||||
|
if os.path.exists(path):
|
||||||
|
with open(path, 'r', encoding='utf-8') as file:
|
||||||
|
content = file.read()
|
||||||
|
if content:
|
||||||
|
self._birthdays = json.loads(content)
|
||||||
|
|
||||||
|
def _store_birthdays(self):
|
||||||
|
path = os.path.join(configpaths.get('MY_DATA'), 'birthdays.json')
|
||||||
|
with open(path, 'w', encoding='utf-8') as file:
|
||||||
|
json.dump(self._birthdays, file)
|
||||||
|
|
||||||
|
def _vcard_received(self, event):
|
||||||
|
birthday = event.vcard_dict.get('BDAY')
|
||||||
|
if not birthday:
|
||||||
|
if event.jid in self._birthdays:
|
||||||
|
del self._birthdays[event.jid]
|
||||||
|
log.info('Received empty birthday: %s', event.jid)
|
||||||
|
else:
|
||||||
try:
|
try:
|
||||||
xml = parse(xmldoc)
|
year, month, day = birthday.split('-')
|
||||||
except:
|
year = int(year)
|
||||||
pass
|
month = int(month)
|
||||||
|
day = int(day)
|
||||||
|
except Exception:
|
||||||
|
log.warning('Invalid date: %s', birthday)
|
||||||
|
if event.jid in self._birthdays:
|
||||||
|
del self._birthdays[event.jid]
|
||||||
else:
|
else:
|
||||||
name = xml.getElementsByTagName('BDAY')
|
self._birthdays[event.jid] = (year, month, day)
|
||||||
for node in name:
|
log.info('Received birthday: %s %s',
|
||||||
try:
|
event.jid, (year, month, day))
|
||||||
data = node.childNodes[0].nodeValue
|
self._store_birthdays()
|
||||||
date_dict[xmldoc[len(self.vcard_path):][:-1]] = data
|
|
||||||
except:
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
def _check_birthdays_at_start(self):
|
||||||
|
self._check_birthdays()
|
||||||
|
|
||||||
|
def _check_birthdays(self):
|
||||||
|
log.info('Check birthdays...')
|
||||||
today = datetime.date.today()
|
today = datetime.date.today()
|
||||||
|
for jid, birthdate in self._birthdays.items():
|
||||||
|
year, month, day = birthdate
|
||||||
|
if today.month == month and today.day == day:
|
||||||
|
account, contact = self._find_contact(jid)
|
||||||
|
if contact is None:
|
||||||
|
if jid in self._birthdays:
|
||||||
|
del self._birthdays[jid]
|
||||||
|
self._store_birthdays()
|
||||||
|
continue
|
||||||
|
else:
|
||||||
|
log.info('Issue notification for %s', jid)
|
||||||
|
nick = contact.get_shown_name() or jid
|
||||||
|
app.notification.popup(
|
||||||
|
'reminder',
|
||||||
|
jid,
|
||||||
|
account,
|
||||||
|
icon_name='trophy-gold',
|
||||||
|
title=TITLE % GLib.markup_escape_text(nick),
|
||||||
|
text=TEXT)
|
||||||
|
|
||||||
for key, value in date_dict.items():
|
|
||||||
try:
|
|
||||||
convert_date = datetime.datetime.strptime(value, "%Y-%m-%d")
|
|
||||||
user_bday = datetime.date(today.year, convert_date.month,
|
|
||||||
convert_date.day)
|
|
||||||
except:
|
|
||||||
continue
|
|
||||||
|
|
||||||
if user_bday < today:
|
|
||||||
user_bday = user_bday.replace(year=today.year+1)
|
|
||||||
|
|
||||||
time_to_bday = abs(user_bday - today)
|
|
||||||
title = "BirthDay Reminder"
|
|
||||||
text = None
|
|
||||||
|
|
||||||
if time_to_bday.days > 5:
|
|
||||||
continue
|
|
||||||
if time_to_bday.days == 5:
|
|
||||||
text = "5 days before BDay"
|
|
||||||
elif time_to_bday.days == 3:
|
|
||||||
text = "3 days before BDay"
|
|
||||||
elif time_to_bday.days == 1:
|
|
||||||
text = "Tomorrow BDay"
|
|
||||||
elif time_to_bday.days == 0:
|
|
||||||
text = "Today BDay"
|
|
||||||
if not text:
|
|
||||||
continue
|
|
||||||
if account:
|
|
||||||
show_popup(account,key)
|
|
||||||
else:
|
|
||||||
for acct in accounts:
|
|
||||||
show_popup(account, key)
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
@log_calls('BirthDayPlugin')
|
def _find_contact(self, jid):
|
||||||
def activate(self):
|
accounts = app.contacts.get_accounts()
|
||||||
self.timeout_id = GObject.timeout_add_seconds(24*3600,
|
for account in accounts:
|
||||||
self.check_birthdays)
|
contact = app.contacts.get_contacts(account, jid)
|
||||||
|
if contact is not None:
|
||||||
@log_calls('BirthDayPlugin')
|
return account, contact[0]
|
||||||
def deactivate(self):
|
|
||||||
if self.timeout_id > 0:
|
|
||||||
GObject.source_remove(self.timeout_id)
|
|
||||||
|
|
||||||
|
|
||||||
@log_calls('BirthDayPlugin')
|
|
||||||
def roster_received(self, obj):
|
|
||||||
if obj.conn.name not in self.showed_accounts:
|
|
||||||
self.check_birthdays(obj.conn.name)
|
|
||||||
self.showed_accounts.append(obj.conn.name)
|
|
||||||
|
|||||||
Reference in New Issue
Block a user