From 4b6f600c55f3bbfbd6d7098cb0785b45c4060ab2 Mon Sep 17 00:00:00 2001 From: Tomeu Vizoso Date: Thu, 30 Apr 2009 14:45:39 +0000 Subject: Initialize the DS object on construction and expose metadata and file name as AS members. --- diff --git a/datastore/datastore.cpp b/datastore/datastore.cpp index d543b8b..f029395 100644 --- a/datastore/datastore.cpp +++ b/datastore/datastore.cpp @@ -2,7 +2,6 @@ #include #include // need builtin_function -#include #include #define DS_DBUS_SERVICE "org.laptop.sugar.DataStore" @@ -14,7 +13,7 @@ namespace gnash { -as_value datastore_get_properties(const fn_call& fn); +typedef std::map Properties; static as_object* getInterface() @@ -27,29 +26,22 @@ getInterface() return o.get(); } -DataStore::DataStore(std::string &object_id) +DataStore::DataStore(VM &vm, std::string &object_id) : as_object(getInterface()), _object_id(object_id) { GNASH_REPORT_FUNCTION; -} -DataStore::~DataStore() -{ - GNASH_REPORT_FUNCTION; -} - -Properties * -DataStore::get_properties() -{ - GNASH_REPORT_FUNCTION; - Properties *properties = new Properties; + string_table& st = vm.getStringTable(); + as_object *metadata = new as_object; + this->set_member(st.find("metadata"), as_value(metadata)); DBusGConnection *connection; GError *error; DBusGProxy *proxy; GHashTable *dict; + gchar *file_name; error = NULL; connection = dbus_g_bus_get (DBUS_BUS_SESSION, @@ -82,8 +74,6 @@ DataStore::get_properties() exit (1); } - g_object_unref (proxy); - GHashTableIter iter; gchar *key; GValue *value; @@ -93,41 +83,12 @@ DataStore::get_properties() if( G_VALUE_HOLDS(value, G_TYPE_BOXED) ) { GArray *array = (GArray *) g_value_get_boxed(value); std::string value_s((gchar *) array->data, array->len); - (*properties)[key] = value_s; + metadata->set_member(st.find(key), value_s); } else { - (*properties)[key] = ""; + metadata->set_member(st.find(key), ""); } } - return properties; -} - -std::auto_ptr -DataStore::get_filename() -{ - GNASH_REPORT_FUNCTION; - - DBusGConnection *connection; - GError *error; - DBusGProxy *proxy; - gchar *file_name; - - error = NULL; - connection = dbus_g_bus_get (DBUS_BUS_SESSION, - &error); - if (connection == NULL) - { - g_printerr ("Failed to open connection to bus: %s\n", - error->message); - g_error_free (error); - exit (1); - } - - proxy = dbus_g_proxy_new_for_name (connection, - DS_DBUS_SERVICE, - DS_DBUS_PATH, - DS_DBUS_INTERFACE); - error = NULL; if (!dbus_g_proxy_call (proxy, "get_filename", &error, G_TYPE_STRING, _object_id.c_str(), G_TYPE_INVALID, @@ -143,67 +104,14 @@ DataStore::get_filename() exit (1); } - g_object_unref (proxy); - - log_debug("%s", file_name); - std::auto_ptr file_name_s(new std::string(file_name)); - return file_name_s; -} - -as_value -datastore_get_properties(const fn_call& fn) -{ - GNASH_REPORT_FUNCTION; - boost::intrusive_ptr ptr = ensureType(fn.this_ptr); - assert(ptr); - - if ( fn.nargs > 0 ) - { - IF_VERBOSE_ASCODING_ERRORS( - std::stringstream ss; fn.dump_args(ss); - log_aserror("DataStore.get_properties(%s): all arguments discarded", ss.str().c_str()); - ); - } - - Properties *properties = ptr->get_properties(); - as_object *as_properties = new as_object; - string_table& st = ptr->getVM().getStringTable(); + this->set_member(st.find("file_name"), file_name); - for (Properties::iterator it=properties->begin(), - itE=properties->end(); - it != itE; ++it) - { - as_properties->set_member(st.find(it->first), - as_value(it->second)); - } - - return as_value(as_properties); -} - -as_value -datastore_get_filename(const fn_call& fn) -{ - GNASH_REPORT_FUNCTION; - boost::intrusive_ptr ptr = ensureType(fn.this_ptr); - assert(ptr); - - if ( fn.nargs > 0 ) - { - IF_VERBOSE_ASCODING_ERRORS( - std::stringstream ss; fn.dump_args(ss); - log_aserror("DataStore.get_properties(%s): all arguments discarded", ss.str().c_str()); - ); - } - - return as_value(*ptr->get_filename().get()); + g_object_unref (proxy); } -static void -attachInterface(as_object *obj) +DataStore::~DataStore() { GNASH_REPORT_FUNCTION; - obj->init_member("get_properties", new builtin_function(datastore_get_properties)); - obj->init_member("get_filename", new builtin_function(datastore_get_filename)); } static as_value @@ -221,8 +129,7 @@ datastore_ctor(const fn_call& fn) } std::string object_id = fn.arg(0).to_string(); - DataStore *obj = new DataStore(object_id); - attachInterface(obj); + DataStore *obj = new DataStore(fn.getVM(), object_id); return as_value(obj); // will keep alive } @@ -238,9 +145,6 @@ extern "C" { static boost::intrusive_ptr cl; if (cl == NULL) { cl = new builtin_function(&datastore_ctor, getInterface()); -// // replicate all interface to class, to be able to access -// // all methods as static functions - //attachInterface(*cl); } obj.init_member("DataStore", cl.get()); diff --git a/datastore/datastore.h b/datastore/datastore.h index aaa986f..3949984 100644 --- a/datastore/datastore.h +++ b/datastore/datastore.h @@ -20,18 +20,15 @@ #include // for auto_ptr #include +#include namespace gnash { -typedef std::map Properties; - class DataStore : public as_object { public: - DataStore(std::string &object_id); + DataStore(VM &vm, std::string &object_id); ~DataStore(); - Properties *get_properties(); - std::auto_ptr get_filename(); private: std::string _object_id; }; -- cgit v0.9.1