diff --git a/configure.ac b/configure.ac
index b50273e1..78b7cacb 100644
--- a/configure.ac
+++ b/configure.ac
@@ -59,6 +59,7 @@ services/Makefile
services/presence/Makefile
services/nm/Makefile
services/clipboard/Makefile
+services/datastore/Makefile
shell/Makefile
shell/data/Makefile
shell/view/Makefile
@@ -81,6 +82,7 @@ sugar/clipboard/Makefile
sugar/graphics/Makefile
sugar/p2p/Makefile
sugar/presence/Makefile
+sugar/datastore/Makefile
po/Makefile.in
tools/Makefile
tools/sugar-setup-activity
diff --git a/services/Makefile.am b/services/Makefile.am
index 2a0fe5ae..9cf9b955 100644
--- a/services/Makefile.am
+++ b/services/Makefile.am
@@ -1 +1 @@
-SUBDIRS = presence nm clipboard
+SUBDIRS = presence nm clipboard datastore
diff --git a/services/datastore/Makefile.am b/services/datastore/Makefile.am
new file mode 100644
index 00000000..e98a34b7
--- /dev/null
+++ b/services/datastore/Makefile.am
@@ -0,0 +1,12 @@
+sugardir = $(pkgdatadir)/services/data-store
+sugar_PYTHON = \
+ __init__.py \
+ datastore.py \
+ dbus_helpers.py
+
+bin_SCRIPTS = sugar-data-store
+
+dbusservicedir = $(sysconfdir)/dbus-1/system.d/
+dbusservice_DATA = sugar-data-store.conf
+
+EXTRA_DIST = $(dbusservice_DATA) $(bin_SCRIPTS)
diff --git a/services/datastore/__init__.py b/services/datastore/__init__.py
new file mode 100644
index 00000000..e69de29b
diff --git a/services/datastore/datastore.py b/services/datastore/datastore.py
new file mode 100644
index 00000000..92270e4a
--- /dev/null
+++ b/services/datastore/datastore.py
@@ -0,0 +1,333 @@
+#!/bin/python
+# Copyright (C) 2006, Red Hat, Inc.
+#
+# 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
+
+import os
+import dbus, dbus.glib, gobject
+import logging
+import sqlite
+import dbus_helpers
+
+have_sugar = False
+try:
+ from sugar import env
+ have_sugar = True
+except ImportError:
+ pass
+
+
+_DS_SERVICE = "org.laptop.sugar.DataStore"
+_DS_DBUS_INTERFACE = "org.laptop.sugar.DataStore"
+_DS_OBJECT_PATH = "/org/laptop/sugar/DataStore"
+
+_DS_OBJECT_DBUS_INTERFACE = "org.laptop.sugar.DataStore.Object"
+_DS_OBJECT_OBJECT_PATH = "/org/laptop/sugar/DataStore/Object"
+
+class NotFoundError(Exception):
+ pass
+
+def _create_op(uid):
+ return "%s/%d" % (_DS_OBJECT_OBJECT_PATH, uid)
+
+def _get_uid_from_op(op):
+ if not op.startswith(_DS_OBJECT_OBJECT_PATH + "/"):
+ raise ValueError("Invalid object path %s." % op)
+ item = op[len(_DS_OBJECT_OBJECT_PATH + "/"):]
+ return int(item)
+
+def _get_data_as_string(data):
+ if isinstance(data, list):
+ data_str = ""
+ for item in data:
+ data_str += chr(item)
+ return data_str
+ elif isinstance(data, int):
+ return str(data)
+ elif isinstance(data, str):
+ return data
+ elif isinstance(data, unicode):
+ return str(data)
+ else:
+ raise ValueError("Unsupported data type")
+
+class DataStoreDBusHelper(dbus.service.Object):
+ def __init__(self, parent, bus_name):
+ self._parent = parent
+ self._bus_name = bus_name
+ dbus.service.Object.__init__(self, bus_name, _DS_OBJECT_PATH)
+
+ @dbus.service.method(_DS_DBUS_INTERFACE,
+ in_signature="x", out_signature="o")
+ def get(self, uid):
+ uid = self._parent.get(uid)
+ return self._create_op(uid)
+
+ @dbus.service.method(_DS_DBUS_INTERFACE,
+ in_signature="aya{sv}", out_signature="o")
+ def create(self, data, prop_dict):
+ uid = self._parent.create(data, prop_dict)
+ return _create_op(uid)
+
+ @dbus.service.method(_DS_DBUS_INTERFACE,
+ in_signature="o", out_signature="i")
+ def delete(self, op):
+ uid = _get_uid_from_op(op)
+ self._parent.delete(uid)
+ return 0
+
+ @dbus.service.method(_DS_DBUS_INTERFACE,
+ in_signature="a{sv}", out_signature="ao")
+ def find(self, prop_dict):
+ uids = self._parent.find(prop_dict)
+ ops = []
+ for uid in uids:
+ ops.append(_create_op(uid))
+ return ops
+
+class ObjectDBusHelper(dbus_helpers.FallbackObject):
+ def __init__(self, parent, bus_name):
+ self._parent = parent
+ self._bus_name = bus_name
+ dbus_helpers.FallbackObject.__init__(self, bus_name, _DS_OBJECT_OBJECT_PATH)
+
+ @dbus_helpers.method(_DS_OBJECT_DBUS_INTERFACE,
+ in_signature="", out_signature="ay", dbus_message_keyword="dbus_message")
+ def get_data(self, dbus_message=None):
+ if not dbus_message:
+ raise RuntimeError("Need the dbus message.")
+ uid = _get_uid_from_op(dbus_message.get_path())
+ return self._parent.get_data(uid)
+
+ @dbus_helpers.method(_DS_OBJECT_DBUS_INTERFACE,
+ in_signature="ay", out_signature="i", dbus_message_keyword="dbus_message")
+ def set_data(self, data, dbus_message=None):
+ if not dbus_message:
+ raise RuntimeError("Need the dbus message.")
+ uid = _get_uid_from_op(dbus_message.get_path())
+ self._parent.set_data(uid, data)
+ return 0
+
+ @dbus_helpers.method(_DS_OBJECT_DBUS_INTERFACE,
+ in_signature="as", out_signature="a{sv}", dbus_message_keyword="dbus_message")
+ def get_properties(self, keys, dbus_message=None):
+ if not dbus_message:
+ raise RuntimeError("Need the dbus message.")
+ uid = _get_uid_from_op(dbus_message.get_path())
+ return self._parent.get_properties(uid, keys)
+
+ @dbus_helpers.method(_DS_OBJECT_DBUS_INTERFACE,
+ in_signature="a{sv}", out_signature="i", dbus_message_keyword="dbus_message")
+ def set_properties(self, prop_dict, dbus_message=None):
+ if not dbus_message:
+ raise RuntimeError("Need the dbus message.")
+ uid = _get_uid_from_op(dbus_message.get_path())
+ self._parent.set_properties(uid, prop_dict)
+ return 0
+
+ @dbus_helpers.fallback_signal(_DS_OBJECT_DBUS_INTERFACE,
+ signature="ba{sv}b", ignore_args=["uid"])
+ def Updated(self, data, prop_dict, deleted, uid=None):
+ # Return the object path so the signal decorator knows what
+ # object this signal should be fore
+ if not uid:
+ raise RuntimeError("Need a UID.")
+ op = _create_op(uid)
+ return op
+
+class DataStore(object):
+ def __init__(self):
+ self._session_bus = dbus.SessionBus()
+ self._bus_name = dbus.service.BusName(_DS_SERVICE, bus=self._session_bus)
+ self._dbus_helper = DataStoreDBusHelper(self, self._bus_name)
+ self._dbus_obj_helper = ObjectDBusHelper(self, self._bus_name)
+
+ ppath = "/tmp"
+ if have_sugar:
+ ppath = env.get_profile_path()
+ self._dbfile = os.path.join(ppath, "ds", "data-store.db")
+ if not os.path.exists(os.path.dirname(self._dbfile)):
+ os.makedirs(os.path.dirname(self._dbfile), 0755)
+
+ self._dbcx = sqlite.connect(self._dbfile, encoding="utf-8", timeout=3)
+ try:
+ self._ensure_table()
+ except StandardError, e:
+ logging.info("Could not access the data store. Reason: '%s'. Exiting..." % e)
+ os._exit(1)
+
+ def __del__(self):
+ self._dbcx.close()
+ del self._dbcx
+
+ def _ensure_table(self):
+ curs = self._dbcx.cursor()
+ try:
+ curs.execute('SELECT * FROM properties LIMIT 4')
+ self._dbcx.commit()
+ except Exception, e:
+ # If table wasn't created, try to create it
+ self._dbcx.commit()
+ curs.execute('CREATE TABLE objects (' \
+ 'uid INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, ' \
+ 'data BLOB' \
+ ');')
+ curs.execute('CREATE TABLE properties (' \
+ 'objid INTEGER NOT NULL, ' \
+ 'key VARCHAR(100),' \
+ 'value VARCHAR(200)' \
+ ');')
+ curs.execute('CREATE INDEX objid_idx ON properties(objid);')
+ self._dbcx.commit()
+ del curs
+
+ def get(self, uid):
+ curs = self._dbcx.cursor()
+ curs.execute('SELECT uid FROM objects WHERE uid=%d;' % uid)
+ res = curs.fetchall()
+ self._dbcx.commit()
+ del curs
+ if len(res) > 0:
+ return uid
+ raise NotFoundError("Object %d was not found." % uid)
+
+ def create(self, data, prop_dict=None):
+ curs = self._dbcx.cursor()
+ curs.execute("INSERT INTO objects (uid) VALUES (NULL);")
+ curs.execute("SELECT last_insert_rowid();")
+ rows = curs.fetchall()
+ self._dbcx.commit()
+ last_row = rows[0]
+ uid = last_row[0]
+ for (key, value) in prop_dict.items():
+ safe_key = key.replace("'", "''")
+ value = str(value)
+ curs.execute("INSERT INTO properties (objid, key, value) VALUES (%d, '%s', '%s');" % (uid, safe_key, sqlite.encode(value)))
+ self._dbcx.commit()
+ del curs
+ return uid
+
+ def delete(self, uid):
+ curs = self._dbcx.cursor()
+ curs.execute("DELETE FROM objects WHERE (uid=%d);" % uid)
+ curs.execute("DELETE FROM properties WHERE (objid=%d);" % uid)
+ self._dbcx.commit()
+ del curs
+ self._dbus_obj_helper.Updated(False, {}, True, uid=uid)
+ return 0
+
+ def find(self, prop_dict):
+ query = "SELECT objid FROM properties WHERE ("
+ subquery = ""
+ for (key, value) in prop_dict.items():
+ safe_key = key.replace("'", "''")
+ value = str(value)
+ substr = "key='%s' AND value='%s'" % (safe_key, sqlite.encode(value))
+ if len(subquery) > 0:
+ subquery += " AND"
+ subquery += substr
+ query += subquery + ")"
+ curs = self._dbcx.cursor()
+ curs.execute(query)
+ rows = curs.fetchall()
+ self._dbcx.commit()
+ # FIXME: ensure that each properties.objid has a match in objects.uid
+ uids = []
+ for row in rows:
+ uids.append(row['objid'])
+ del curs
+ return uids
+
+ def set_data(self, uid, data):
+ curs = self._dbcx.cursor()
+ curs.execute('SELECT uid FROM objects WHERE uid=%d;' % uid)
+ res = curs.fetchall()
+ self._dbcx.commit()
+ if len(res) <= 0:
+ del curs
+ raise NotFoundError("Object %d was not found." % uid)
+ data = _get_data_as_string(data)
+ curs.execute("UPDATE objects SET data='%s' WHERE uid=%d;" % (sqlite.encode(data), uid))
+ self._dbcx.commit()
+ del curs
+ self._dbus_obj_helper.Updated(True, {}, False, uid=uid)
+
+ _reserved_keys = ["uid", "objid", "data", "created", "modified"]
+ def set_properties(self, uid, prop_dict):
+ curs = self._dbcx.cursor()
+ curs.execute('SELECT uid FROM objects WHERE uid=%d;' % uid)
+ res = curs.fetchall()
+ self._dbcx.commit()
+ if len(res) <= 0:
+ del curs
+ raise NotFoundError("Object %d was not found." % uid)
+
+ for key in prop_dict.keys():
+ if key in self._reserved_keys:
+ raise ValueError("key %s is a reserved key." % key)
+
+ for (key, value) in prop_dict.items():
+ safe_key = key.replace("'", "''")
+ enc_value = sqlite.encode(_get_data_as_string(value))
+ curs.execute("SELECT objid FROM properties WHERE (objid=%d AND key='%s');" % (uid, safe_key))
+ if len(curs.fetchall()) > 0:
+ curs.execute("UPDATE properties SET value='%s' WHERE (objid=%d AND key='%s');" % (enc_value, uid, safe_key))
+ else:
+ curs.execute("INSERT INTO properties (objid, key, value) VALUES (%d, '%s', '%s');" % (uid, safe_key, enc_value))
+ self._dbcx.commit()
+ del curs
+ self._dbus_obj_helper.Updated(False, {}, False, uid=uid)
+
+ def get_data(self, uid):
+ curs = self._dbcx.cursor()
+ curs.execute('SELECT uid, data FROM objects WHERE uid=%d;' % uid)
+ res = curs.fetchall()
+ self._dbcx.commit()
+ del curs
+ if len(res) <= 0:
+ raise NotFoundError("Object %d was not found." % uid)
+ return sqlite.decode(res[0]['data'])
+
+ def get_properties(self, uid, keys):
+ query = "SELECT objid, key, value FROM properties WHERE (objid=%d AND (" % uid
+ subquery = ""
+ for key in keys:
+ if len(subquery) > 0:
+ subquery += " OR "
+ safe_key = key.replace("'", "''")
+ subquery += "key='%s'" % safe_key
+ query += subquery + "));"
+ curs = self._dbcx.cursor()
+ curs.execute(query)
+ rows = curs.fetchall()
+ self._dbcx.commit()
+ prop_dict = {}
+ for row in rows:
+ conv_key = row['key'].replace("''", "'")
+ prop_dict[conv_key] = row['value']
+ del curs
+ return prop_dict
+
+
+def main():
+ loop = gobject.MainLoop()
+ ds = DataStore()
+ try:
+ loop.run()
+ except KeyboardInterrupt:
+ print 'Ctrl+C pressed, exiting...'
+
+if __name__ == "__main__":
+ main()
diff --git a/services/datastore/dbus_helpers.py b/services/datastore/dbus_helpers.py
new file mode 100644
index 00000000..391e2017
--- /dev/null
+++ b/services/datastore/dbus_helpers.py
@@ -0,0 +1,222 @@
+# Copyright (C) 2006, Red Hat, Inc.
+#
+# 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
+
+
+# Mostly taken from dbus-python's service.py
+
+import dbus
+from dbus import service
+import inspect
+
+def method(dbus_interface, in_signature=None, out_signature=None, async_callbacks=None, sender_keyword=None, dbus_message_keyword=None):
+ dbus._util._validate_interface_or_name(dbus_interface)
+
+ def decorator(func):
+ args = inspect.getargspec(func)[0]
+ args.pop(0)
+
+ if async_callbacks:
+ if type(async_callbacks) != tuple:
+ raise TypeError('async_callbacks must be a tuple of (keyword for return callback, keyword for error callback)')
+ if len(async_callbacks) != 2:
+ raise ValueError('async_callbacks must be a tuple of (keyword for return callback, keyword for error callback)')
+ args.remove(async_callbacks[0])
+ args.remove(async_callbacks[1])
+
+ if sender_keyword:
+ args.remove(sender_keyword)
+
+ if dbus_message_keyword:
+ args.remove(dbus_message_keyword)
+
+ if in_signature:
+ in_sig = tuple(dbus.dbus_bindings.Signature(in_signature))
+
+ if len(in_sig) > len(args):
+ raise ValueError, 'input signature is longer than the number of arguments taken'
+ elif len(in_sig) < len(args):
+ raise ValueError, 'input signature is shorter than the number of arguments taken'
+
+ func._dbus_message_keyword = dbus_message_keyword
+ func._dbus_is_method = True
+ func._dbus_async_callbacks = async_callbacks
+ func._dbus_interface = dbus_interface
+ func._dbus_in_signature = in_signature
+ func._dbus_out_signature = out_signature
+ func._dbus_sender_keyword = sender_keyword
+ func._dbus_args = args
+ return func
+
+ return decorator
+
+def fallback_signal(dbus_interface, signature=None, ignore_args=None):
+ dbus._util._validate_interface_or_name(dbus_interface)
+ def decorator(func):
+ def emit_signal(self, *args, **keywords):
+ obj_path = func(self, *args, **keywords)
+ message = dbus.dbus_bindings.Signal(obj_path, dbus_interface, func.__name__)
+ iter = message.get_iter(True)
+
+ if emit_signal._dbus_signature:
+ signature = tuple(dbus.dbus_bindings.Signature(emit_signal._dbus_signature))
+ for (arg, sig) in zip(args, signature):
+ iter.append_strict(arg, sig)
+ else:
+ for arg in args:
+ iter.append(arg)
+
+ self._connection.send(message)
+
+ temp_args = inspect.getargspec(func)[0]
+ temp_args.pop(0)
+
+ args = []
+ for arg in temp_args:
+ if arg not in ignore_args:
+ args.append(arg)
+
+ if signature:
+ sig = tuple(dbus.dbus_bindings.Signature(signature))
+
+ if len(sig) > len(args):
+ raise ValueError, 'signal signature is longer than the number of arguments provided'
+ elif len(sig) < len(args):
+ raise ValueError, 'signal signature is shorter than the number of arguments provided'
+
+ emit_signal.__name__ = func.__name__
+ emit_signal.__doc__ = func.__doc__
+ emit_signal._dbus_is_signal = True
+ emit_signal._dbus_interface = dbus_interface
+ emit_signal._dbus_signature = signature
+ emit_signal._dbus_args = args
+ return emit_signal
+
+ return decorator
+
+class FallbackObject(dbus.service.Object):
+ """A base class for exporting your own Objects across the Bus.
+
+ Just inherit from Object and provide a list of methods to share
+ across the Bus
+ """
+ def __init__(self, bus_name, fallback_object_path):
+ self._object_path = fallback_object_path
+ self._name = bus_name
+ self._bus = bus_name.get_bus()
+
+ self._connection = self._bus.get_connection()
+
+ self._connection.register_fallback(fallback_object_path, self._unregister_cb, self._message_cb)
+
+ def _message_cb(self, connection, message):
+ try:
+ # lookup candidate method and parent method
+ method_name = message.get_member()
+ interface_name = message.get_interface()
+ (candidate_method, parent_method) = dbus.service._method_lookup(self, method_name, interface_name)
+
+ # set up method call parameters
+ args = message.get_args_list()
+ keywords = {}
+
+ # iterate signature into list of complete types
+ if parent_method._dbus_out_signature:
+ signature = tuple(dbus.dbus_bindings.Signature(parent_method._dbus_out_signature))
+ else:
+ signature = None
+
+ # set up async callback functions
+ if parent_method._dbus_async_callbacks:
+ (return_callback, error_callback) = parent_method._dbus_async_callbacks
+ keywords[return_callback] = lambda *retval: dbus.service._method_reply_return(connection, message, method_name, signature, *retval)
+ keywords[error_callback] = lambda exception: dbus.service._method_reply_error(connection, message, exception)
+
+ # include the sender if desired
+ if parent_method._dbus_sender_keyword:
+ keywords[parent_method._dbus_sender_keyword] = message.get_sender()
+
+ if parent_method._dbus_message_keyword:
+ keywords[parent_method._dbus_message_keyword] = message
+
+ # call method
+ retval = candidate_method(self, *args, **keywords)
+
+ # we're done - the method has got callback functions to reply with
+ if parent_method._dbus_async_callbacks:
+ return
+
+ # otherwise we send the return values in a reply. if we have a
+ # signature, use it to turn the return value into a tuple as
+ # appropriate
+ if parent_method._dbus_out_signature:
+ # if we have zero or one return values we want make a tuple
+ # for the _method_reply_return function, otherwise we need
+ # to check we're passing it a sequence
+ if len(signature) == 0:
+ if retval == None:
+ retval = ()
+ else:
+ raise TypeError('%s has an empty output signature but did not return None' %
+ method_name)
+ elif len(signature) == 1:
+ retval = (retval,)
+ else:
+ if operator.isSequenceType(retval):
+ # multi-value signature, multi-value return... proceed unchanged
+ pass
+ else:
+ raise TypeError('%s has multiple output values in signature %s but did not return a sequence' %
+ (method_name, signature))
+
+ # no signature, so just turn the return into a tuple and send it as normal
+ else:
+ signature = None
+ if retval == None:
+ retval = ()
+ else:
+ retval = (retval,)
+
+ dbus.service._method_reply_return(connection, message, method_name, signature, *retval)
+ except Exception, exception:
+ # send error reply
+ dbus.service._method_reply_error(connection, message, exception)
+
+ @method('org.freedesktop.DBus.Introspectable', in_signature='', out_signature='s', dbus_message_keyword="dbus_message")
+ def Introspect(self, dbus_message=None):
+ reflection_data = '\n'
+ op = dbus_message.get_path()
+ reflection_data += '\n' % (op)
+
+ interfaces = self._dbus_class_table[self.__class__.__module__ + '.' + self.__class__.__name__]
+ for (name, funcs) in interfaces.iteritems():
+ reflection_data += ' \n' % (name)
+
+ for func in funcs.values():
+ if getattr(func, '_dbus_is_method', False):
+ reflection_data += self.__class__._reflect_on_method(func)
+ elif getattr(func, '_dbus_is_signal', False):
+ reflection_data += self.__class__._reflect_on_signal(func)
+
+ reflection_data += ' \n'
+
+ reflection_data += '\n'
+
+ return reflection_data
+
+ def __repr__(self):
+ return '' % (self._fallback_object_path, self._name, id(self))
+ __str__ = __repr__
+
diff --git a/services/datastore/sugar-data-store b/services/datastore/sugar-data-store
new file mode 100644
index 00000000..7940c749
--- /dev/null
+++ b/services/datastore/sugar-data-store
@@ -0,0 +1,32 @@
+#!/usr/bin/python
+# vi: ts=4 ai noet
+#
+# Copyright (C) 2006, Red Hat, Inc.
+#
+# 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
+
+import sys
+import logging
+
+from sugar import logger
+from sugar import env
+
+sys.path.insert(0, env.get_services_dir())
+
+logger.start('data-store')
+logging.info('Starting the data store...')
+
+from datastore import datastore
+datastore.main()
diff --git a/sugar/Makefile.am b/sugar/Makefile.am
index c186f07e..825b0114 100644
--- a/sugar/Makefile.am
+++ b/sugar/Makefile.am
@@ -1,4 +1,4 @@
-SUBDIRS = activity chat clipboard graphics p2p presence
+SUBDIRS = activity chat clipboard graphics p2p presence datastore
sugardir = $(pythondir)/sugar
sugar_PYTHON = \
diff --git a/sugar/datastore/Makefile.am b/sugar/datastore/Makefile.am
new file mode 100644
index 00000000..1ab7f9f7
--- /dev/null
+++ b/sugar/datastore/Makefile.am
@@ -0,0 +1,4 @@
+sugardir = $(pythondir)/sugar/datastore
+sugar_PYTHON = \
+ __init__.py \
+ datastore.py
diff --git a/sugar/datastore/__init__.py b/sugar/datastore/__init__.py
new file mode 100644
index 00000000..e69de29b
diff --git a/sugar/datastore/datastore.py b/sugar/datastore/datastore.py
new file mode 100644
index 00000000..9f1ab67e
--- /dev/null
+++ b/sugar/datastore/datastore.py
@@ -0,0 +1,174 @@
+# Copyright (C) 2006, Red Hat, Inc.
+#
+# 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
+
+import dbus, dbus.glib, gobject
+
+class ObjectCache(object):
+ def __init__(self):
+ self._cache = {}
+
+ def get(self, object_path):
+ try:
+ return self._cache[object_path]
+ except KeyError:
+ return None
+
+ def add(self, obj):
+ op = obj.object_path()
+ if not self._cache.has_key(op):
+ self._cache[op] = obj
+
+ def remove(self, object_path):
+ if self._cache.has_key(object_path):
+ del self._cache[object_path]
+
+
+DS_DBUS_SERVICE = "org.laptop.sugar.DataStore"
+DS_DBUS_INTERFACE = "org.laptop.sugar.DataStore"
+DS_DBUS_PATH = "/org/laptop/sugar/DataStore"
+
+class DSObject(gobject.GObject):
+
+ __gsignals__ = {
+ 'updated': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
+ ([gobject.TYPE_PYOBJECT, gobject.TYPE_PYOBJECT, gobject.TYPE_PYOBJECT]))
+ }
+
+ _DS_OBJECT_DBUS_INTERFACE = "org.laptop.sugar.DataStore.Object"
+
+ def __init__(self, bus, new_obj_cb, del_obj_cb, object_path):
+ gobject.GObject.__init__(self)
+ self._object_path = object_path
+ self._ps_new_object = new_obj_cb
+ self._ps_del_object = del_obj_cb
+ bobj = bus.get_object(DS_DBUS_SERVICE, object_path)
+ self._dsobj = dbus.Interface(bobj, self.__DS_OBJECT_DBUS_INTERFACE)
+ self._dsobj.connect_to_signal('Updated', self._updated_cb)
+ self._data = None
+ self._data_needs_update = True
+ self._properties = self._dsobj.get_properties()
+ self._deleted = False
+
+ def object_path(self):
+ return self._object_path
+
+ def _emit_updated_signal(self, data, prop_dict, deleted):
+ self.emit('updated', data, prop_dict, deleted)
+ return False
+
+ def _update_internal_properties(self, prop_dict):
+ did_update = False
+ for (key, value) in prop_dict.items():
+ if not len(value):
+ if self._properties.has_key(ley):
+ did_update = True
+ del self._properties[key]
+ else:
+ if self._properties.has_key(key):
+ if self._properties[key] != value:
+ did_update = True
+ self._properties[key] = value
+ else:
+ did_update = True
+ self._properties[key] = value
+ return did_update
+
+ def _updated_cb(self, data=False, prop_dict={}, deleted=False):
+ if self._update_internal_properties(prop_dict):
+ gobject.idle_add(self._emit_updated_signal, data, prop_dict, deleted)
+ self._deleted = deleted
+
+ def get_data(self):
+ if self._data_needs_update:
+ self._data = self._dsobj.get_data()
+ return self._data
+
+ def set_data(self, data):
+ old_data = self._data
+ self._data = data
+ try:
+ self._dsobj.set_data(dbus.ByteArray(data))
+ del old_data
+ except dbus.DBusException, e:
+ self._data = old_data
+ raise e
+
+ def set_properties(self, prop_dict):
+ old_props = self._properties
+ self._update_internal_properties(prop_dict)
+ try:
+ self._dsobj.set_properties(prop_dict)
+ del old_props
+ except dbus.DBusException, e:
+ self._properties = old_props
+ raise e
+
+ def get_properties(self, prop_dict):
+ return self._properties
+
+class DataStore(gobject.GObject):
+
+ _DS_DBUS_OBJECT_PATH = DBUS_PATH + "/Object/"
+
+ def __init__(self):
+ gobject.GObject.__init__(self)
+ self._objcache = ObjectCache()
+ self._bus = dbus.SessionBus()
+ self._ds = dbus.Interface(self._bus.get_object(DS_DBUS_SERVICE,
+ DS_DBUS_PATH), DS_DBUS_INTERFACE)
+
+ def _new_object(self, object_path):
+ obj = self._objcache.get(object_path)
+ if not obj:
+ if object_path.startswith(self._DS_DBUS_OBJECT_PATH):
+ obj = DSObject(self._bus, self._new_object,
+ self._del_object, object_path)
+ else:
+ raise RuntimeError("Unknown object type")
+ self._objcache.add(obj)
+ return obj
+
+ def _del_object(self, object_path):
+ # FIXME
+ pass
+
+ def get(self, uid):
+ return self._new_object(self._ds.get(uid))
+
+ def create(self, data, prop_dict={}):
+ op = self._ds.create(dbus.ByteArray(data), dbus.Dictionary(prop_dict))
+ return self._new_object(op)
+
+ def delete(self, obj):
+ op = obj.object_path()
+ obj = self._objcache.get(op)
+ if not obj:
+ raise RuntimeError("Object not found.")
+ self._ds.delete(op)
+
+ def find(self, prop_dict):
+ ops = self._ds.find(dbus.Dictionary(prop_dict))
+ objs = []
+ for op in ops:
+ objs.append(self._new_object(op))
+ return objs
+
+_ds = None
+def get_instance():
+ global _ds
+ if not _ds:
+ _ds = DataStore()
+ return _ds