Audacious  $Id:Doxyfile42802007-03-2104:39:00Znenolod$
general.c
Go to the documentation of this file.
1 /*
2  * general.c
3  * Copyright 2011 John Lindgren
4  *
5  * This file is part of Audacious.
6  *
7  * Audacious is free software: you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License as published by the Free Software
9  * Foundation, version 2 or version 3 of the License.
10  *
11  * Audacious is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13  * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * Audacious. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * The Audacious team does not consider modular code linking to Audacious or
19  * using our public API to be a derived work.
20  */
21 
22 #include <gtk/gtk.h>
23 
24 #include "debug.h"
25 #include "general.h"
26 #include "interface.h"
27 #include "plugin.h"
28 #include "plugins.h"
29 #include "ui_preferences.h"
30 
31 typedef struct {
33  GeneralPlugin * gp;
34  GtkWidget * widget;
36 
37 static int running = FALSE;
38 static GList * loaded_general_plugins = NULL;
39 
41 {
42  return (general->plugin == plugin) ? 0 : -1;
43 }
44 
46 {
47  GList * node = g_list_find_custom (loaded_general_plugins, plugin,
48  (GCompareFunc) general_find_cb);
49  if (node != NULL)
50  return;
51 
52  AUDDBG ("Loading %s.\n", plugin_get_name (plugin));
53  GeneralPlugin * gp = plugin_get_header (plugin);
54  g_return_if_fail (gp != NULL);
55 
56  LoadedGeneral * general = g_slice_new (LoadedGeneral);
57  general->plugin = plugin;
58  general->gp = gp;
59  general->widget = NULL;
60 
61  if (gp->get_widget != NULL)
62  general->widget = gp->get_widget ();
63 
64  if (general->widget != NULL)
65  {
66  AUDDBG ("Adding %s to interface.\n", plugin_get_name (plugin));
67  g_signal_connect (general->widget, "destroy", (GCallback)
68  gtk_widget_destroyed, & general->widget);
69  interface_add_plugin_widget (plugin, general->widget);
70  }
71 
72  loaded_general_plugins = g_list_prepend (loaded_general_plugins, general);
73 }
74 
76 {
77  GList * node = g_list_find_custom (loaded_general_plugins, plugin,
78  (GCompareFunc) general_find_cb);
79  if (node == NULL)
80  return;
81 
82  AUDDBG ("Unloading %s.\n", plugin_get_name (plugin));
83  LoadedGeneral * general = node->data;
84  loaded_general_plugins = g_list_delete_link (loaded_general_plugins, node);
85 
86  if (general->widget != NULL)
87  {
88  AUDDBG ("Removing %s from interface.\n", plugin_get_name (plugin));
89  interface_remove_plugin_widget (plugin, general->widget);
90  g_return_if_fail (general->widget == NULL); /* not destroyed? */
91  }
92 
93  g_slice_free (LoadedGeneral, general);
94 }
95 
97 {
98  general_load (plugin);
99  return TRUE;
100 }
101 
102 void general_init (void)
103 {
104  g_return_if_fail (! running);
105  running = TRUE;
106 
109 }
110 
111 static void general_cleanup_cb (LoadedGeneral * general)
112 {
113  general_unload (general->plugin);
114 }
115 
116 void general_cleanup (void)
117 {
118  g_return_if_fail (running);
119  running = FALSE;
120 
121  g_list_foreach (loaded_general_plugins, (GFunc) general_cleanup_cb, NULL);
122 }
123 
125 {
126  GeneralPlugin * gp = plugin_get_header (plugin);
127  g_return_val_if_fail (gp != NULL, FALSE);
128 
129  if (gp->init != NULL && ! gp->init ())
130  return FALSE;
131 
132  if (running)
133  general_load (plugin);
134 
135  return TRUE;
136 }
137 
139 {
140  GeneralPlugin * gp = plugin_get_header (plugin);
141  g_return_if_fail (gp != NULL);
142 
143  if (running)
144  general_unload (plugin);
145 
146  if (gp->settings != NULL)
147  plugin_preferences_cleanup (gp->settings);
148  if (gp->cleanup != NULL)
149  gp->cleanup ();
150 }
151 
152 PluginHandle * general_plugin_by_widget (/* GtkWidget * */ void * widget)
153 {
154  g_return_val_if_fail (widget, NULL);
155 
156  for (GList * node = loaded_general_plugins; node; node = node->next)
157  {
158  LoadedGeneral * general = node->data;
159  if (general->widget == widget)
160  return general->plugin;
161  }
162 
163  return NULL;
164 }