sugar-toolkit-gtk3/services/presence/buddyiconcache.py

107 lines
3.3 KiB
Python
Raw Normal View History

2007-02-21 13:20:59 +01:00
# Copyright (C) 2007, Red Hat, Inc.
2007-02-26 17:18:52 +01:00
# Copyright (C) 2007, Collabora Ltd.
2007-02-21 13:20:59 +01:00
#
# This program 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 2 of the License, or
# (at your option) any later version.
#
# This program 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 this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
from sugar import env
from sugar import util
2007-02-26 17:18:52 +01:00
import os.path
import cPickle
2007-02-21 13:20:59 +01:00
class BuddyIconCache(object):
2007-02-26 17:18:52 +01:00
"""Caches icons on disk and finds them based on the jid of their owners."""
2007-02-21 13:20:59 +01:00
def __init__(self):
ppath = env.get_profile_path()
2007-02-26 17:18:52 +01:00
self._cachepath = os.path.join(ppath, "cache", "buddy-icons", "cache")
# Ensure cache directory exists
if not os.path.exists(os.path.dirname(self._cachepath)):
os.makedirs(os.path.dirname(self._cachepath))
2007-04-10 21:15:21 +02:00
if not os.path.exists(self._cachepath):
self._cache = {}
# md5 and server token of the last avatar uploaded
self._md5 = ''
self._token = ''
else:
self._load_cache()
2007-02-26 17:18:52 +01:00
def _load_cache(self):
2007-02-21 13:20:59 +01:00
try:
2007-03-05 12:52:29 +01:00
self._cache, self._md5, self._token = cPickle.load(open(self._cachepath, "r"))
2007-02-26 17:18:52 +01:00
except:
2007-03-05 12:52:29 +01:00
self._cache, self._md5, self._token = {}, '', ''
2007-02-26 17:18:52 +01:00
def _save_cache(self):
out = open(self._cachepath, "w")
2007-03-05 12:52:29 +01:00
cPickle.dump((self._cache, self._md5, self._token), out, protocol=2)
2007-02-26 17:18:52 +01:00
def get_icon(self, jid, token):
hit = self._cache.get(jid)
if hit:
t, icon = hit[0], hit[1]
if t == token:
return icon
2007-02-21 13:20:59 +01:00
return None
2007-02-26 17:18:52 +01:00
def store_icon(self, jid, token, data):
self._cache[jid] = (token, data)
self._save_cache()
2007-03-05 12:52:29 +01:00
def check_avatar(self, md5, token):
return self._md5 == md5 and self._token == token
def set_avatar(self, md5, token):
self._md5 = md5
self._token = token
self._save_cache()
2007-02-26 17:18:52 +01:00
if __name__ == "__main__":
my_cache = BuddyIconCache()
# look for the icon in the cache
icon = my_cache.get_icon("test@olpc.collabora.co.uk", "aaaa")
print icon
my_cache.store_icon("test@olpc.collabora.co.uk", "aaaa", "icon1")
# now we're sure that the icon is in the cache
icon = my_cache.get_icon("test@olpc.collabora.co.uk", "aaaa")
print icon
# new icon
my_cache.store_icon("test@olpc.collabora.co.uk", "bbbb", "icon2")
# the icon in the cache is not valid now
icon = my_cache.get_icon("test@olpc.collabora.co.uk", "aaaa")
print icon
2007-03-05 12:52:29 +01:00
my_avatar_md5 = "111"
my_avatar_token = "222"
if not my_cache.check_avatar(my_avatar_md5, my_avatar_token):
# upload of the new avatar
print "upload of the new avatar"
my_cache.set_avatar(my_avatar_md5, my_avatar_token)
else:
print "No need to upload the new avatar"
if my_cache.check_avatar(my_avatar_md5, my_avatar_token):
print "No need to upload the new avatar"