Audacious  $Id:Doxyfile42802007-03-2104:39:00Znenolod$
general.c
Go to the documentation of this file.
00001 /*
00002  * general.c
00003  * Copyright 2011 John Lindgren
00004  *
00005  * This file is part of Audacious.
00006  *
00007  * Audacious is free software: you can redistribute it and/or modify it under
00008  * the terms of the GNU General Public License as published by the Free Software
00009  * Foundation, version 2 or version 3 of the License.
00010  *
00011  * Audacious is distributed in the hope that it will be useful, but WITHOUT ANY
00012  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
00013  * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License along with
00016  * Audacious. If not, see <http://www.gnu.org/licenses/>.
00017  *
00018  * The Audacious team does not consider modular code linking to Audacious or
00019  * using our public API to be a derived work.
00020  */
00021 
00022 #include <gtk/gtk.h>
00023 
00024 #include "debug.h"
00025 #include "general.h"
00026 #include "interface.h"
00027 #include "plugin.h"
00028 #include "plugins.h"
00029 #include "ui_preferences.h"
00030 
00031 typedef struct {
00032     PluginHandle * plugin;
00033     GeneralPlugin * gp;
00034     GtkWidget * widget;
00035 } LoadedGeneral;
00036 
00037 static int running = FALSE;
00038 static GList * loaded_general_plugins = NULL;
00039 
00040 static int general_find_cb (LoadedGeneral * general, PluginHandle * plugin)
00041 {
00042     return (general->plugin == plugin) ? 0 : -1;
00043 }
00044 
00045 static void general_load (PluginHandle * plugin)
00046 {
00047     GList * node = g_list_find_custom (loaded_general_plugins, plugin,
00048      (GCompareFunc) general_find_cb);
00049     if (node != NULL)
00050         return;
00051 
00052     AUDDBG ("Loading %s.\n", plugin_get_name (plugin));
00053     GeneralPlugin * gp = plugin_get_header (plugin);
00054     g_return_if_fail (gp != NULL);
00055 
00056     LoadedGeneral * general = g_slice_new (LoadedGeneral);
00057     general->plugin = plugin;
00058     general->gp = gp;
00059     general->widget = NULL;
00060 
00061     if (gp->get_widget != NULL)
00062         general->widget = gp->get_widget ();
00063 
00064     if (general->widget != NULL)
00065     {
00066         AUDDBG ("Adding %s to interface.\n", plugin_get_name (plugin));
00067         g_signal_connect (general->widget, "destroy", (GCallback)
00068          gtk_widget_destroyed, & general->widget);
00069         interface_add_plugin_widget (plugin, general->widget);
00070     }
00071 
00072     loaded_general_plugins = g_list_prepend (loaded_general_plugins, general);
00073 }
00074 
00075 static void general_unload (PluginHandle * plugin)
00076 {
00077     GList * node = g_list_find_custom (loaded_general_plugins, plugin,
00078      (GCompareFunc) general_find_cb);
00079     if (node == NULL)
00080         return;
00081 
00082     AUDDBG ("Unloading %s.\n", plugin_get_name (plugin));
00083     LoadedGeneral * general = node->data;
00084     loaded_general_plugins = g_list_delete_link (loaded_general_plugins, node);
00085 
00086     if (general->widget != NULL)
00087     {
00088         AUDDBG ("Removing %s from interface.\n", plugin_get_name (plugin));
00089         interface_remove_plugin_widget (plugin, general->widget);
00090         g_return_if_fail (general->widget == NULL); /* not destroyed? */
00091     }
00092 
00093     g_slice_free (LoadedGeneral, general);
00094 }
00095 
00096 static bool_t general_init_cb (PluginHandle * plugin)
00097 {
00098     general_load (plugin);
00099     return TRUE;
00100 }
00101 
00102 void general_init (void)
00103 {
00104     g_return_if_fail (! running);
00105     running = TRUE;
00106 
00107     plugin_for_enabled (PLUGIN_TYPE_GENERAL, (PluginForEachFunc)
00108      general_init_cb, NULL);
00109 }
00110 
00111 static void general_cleanup_cb (LoadedGeneral * general)
00112 {
00113     general_unload (general->plugin);
00114 }
00115 
00116 void general_cleanup (void)
00117 {
00118     g_return_if_fail (running);
00119     running = FALSE;
00120 
00121     g_list_foreach (loaded_general_plugins, (GFunc) general_cleanup_cb, NULL);
00122 }
00123 
00124 bool_t general_plugin_start (PluginHandle * plugin)
00125 {
00126     GeneralPlugin * gp = plugin_get_header (plugin);
00127     g_return_val_if_fail (gp != NULL, FALSE);
00128 
00129     if (gp->init != NULL && ! gp->init ())
00130         return FALSE;
00131 
00132     if (running)
00133         general_load (plugin);
00134 
00135     return TRUE;
00136 }
00137 
00138 void general_plugin_stop (PluginHandle * plugin)
00139 {
00140     GeneralPlugin * gp = plugin_get_header (plugin);
00141     g_return_if_fail (gp != NULL);
00142 
00143     if (running)
00144         general_unload (plugin);
00145 
00146     if (gp->settings != NULL)
00147         plugin_preferences_cleanup (gp->settings);
00148     if (gp->cleanup != NULL)
00149         gp->cleanup ();
00150 }
00151 
00152 PluginHandle * general_plugin_by_widget (/* GtkWidget * */ void * widget)
00153 {
00154     g_return_val_if_fail (widget, NULL);
00155 
00156     for (GList * node = loaded_general_plugins; node; node = node->next)
00157     {
00158         LoadedGeneral * general = node->data;
00159         if (general->widget == widget)
00160             return general->plugin;
00161     }
00162 
00163     return NULL;
00164 }