grim/guifications3

moved guifications-gtk to cmake
cmake
2010-12-13, Gary Kramlich
36e02fafe588
moved guifications-gtk to cmake
/*
* Guifications - The end-all, be-all notification framework
* Copyright (C) 2003-2009 Gary Kramlich <grim@reaperworld.com>
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif /* HAVE_CONFIG_H */
#include <gflib/gf_connection_manager.h>
#include <gflib/gf_preferences.h>
#include <gflib/gf_type.h>
/******************************************************************************
* Globals
*****************************************************************************/
static GHashTable *connections = NULL;
/******************************************************************************
* Helpers
*****************************************************************************/
static GfClientConnection *
gf_connection_manager_find_client_connection(void) {
GfClientConnection *connection = NULL;
GList *keys = NULL, *l = NULL;
keys = g_hash_table_get_keys(connections);
for(l = keys; l; l = l->next) {
gpointer value = g_hash_table_lookup(connections, l->data);
GType type = GPOINTER_TO_SIZE(value);
connection = g_object_new(type, NULL);
if(connection && GF_IS_CLIENT_CONNECTION(connection))
break;
connection = NULL;
}
g_list_free(keys);
return connection;
}
/******************************************************************************
* Private API
*****************************************************************************/
void
gf_connection_manager_init(void) {
connections = g_hash_table_new_full(g_str_hash, g_str_equal,
g_free, NULL);
gf_connection_manager_refresh();
}
void
gf_connection_manager_uninit(void) {
g_hash_table_destroy(connections);
}
/******************************************************************************
* API
*****************************************************************************/
/**
* gf_connection_manager_refresh:
*
* Refreshes all the connections.
*
* Note: This will probably never need to be used externally.
*/
void
gf_connection_manager_refresh(void) {
GType *types = NULL;
guint ntypes = 0, i = 0;
g_hash_table_remove_all(connections);
types = gf_type_concrete_children(GF_TYPE_CLIENT_CONNECTION, &ntypes);
for(i = 0; i < ntypes; i++) {
GObjectClass *obj_class = g_type_class_ref(types[i]);
const gchar *name = NULL;
name = gf_named_object_class_get_name(GF_NAMED_OBJECT_CLASS(obj_class));
g_hash_table_insert(connections, g_strdup(name),
GINT_TO_POINTER(types[i]));
g_type_class_unref(obj_class);
}
g_free(types);
}
/**
* gf_connection_manager_get_default:
*
* Creates a new instance of the default client connection. If no default
* client connection has been set, it returns a new instance of the first
* client connection found, and NULL if no client connection can be found.
*
* Return Value: A new instance of the default GfClientConnection or NULL.
*/
GfClientConnection *
gf_connection_manager_get_default(void) {
GfClientConnection *connection = NULL;
GType type = G_TYPE_INVALID;
const gchar *name;
name = gf_preferences_get_string("/connections/client/default");
/* need logic for handling new stuff */
if(!name)
return gf_connection_manager_find_client_connection();
type = g_type_from_name(name);
/* need a fall over for this */
if(type == G_TYPE_INVALID)
return gf_connection_manager_find_client_connection();
/* we should have a valid type right now, and all connection require no
* arguments passed to them so this should be good!
*/
connection = g_object_new(type, NULL);
if(!connection)
return gf_connection_manager_find_client_connection();
return connection;
}
/**
* gf_connection_manager_set_default:
* @type: The #GType of the new default.
*
* Sets the default client connection.
*/
void
gf_connection_manager_set_default(GType type) {
const gchar *name = NULL;
g_return_if_fail(g_type_is_a(type, GF_TYPE_CLIENT_CONNECTION));
g_return_if_fail(!G_TYPE_IS_ABSTRACT(type));
name = g_type_name(type);
g_return_if_fail(name);
if(!gf_preferences_exist("/connections"))
gf_preferences_add_section("/connections");
if(!gf_preferences_exist("/connections/client"))
gf_preferences_add_section("/connections/client");
if(!gf_preferences_exist("/connections/client/default"))
gf_preferences_add_string("/connections/client/default", name);
else
gf_preferences_set_string("/connections/client/default", name);
}
/**
* gf_connection_manager_get_connection:
* @name : The name of the #GfClientConnection.
*
* Creates an instance of the #GfClientConnection named @name.
*
* Return Value: A new instance of the #GfClientConnection named @name.
*/
GfClientConnection *
gf_connection_manager_get_connection(const gchar *name) {
GfConnection *connection = NULL;
connection = g_hash_table_lookup(connections, name);
if(!connection)
return NULL;
return GF_CLIENT_CONNECTION(connection);
}