Replace functions which called once with their bodies
[pidgin-git.git] / libpurple / core.c
bloba59ac139dcd60c5e76e72b38713e088fe4342f81
1 /* purple
3 * Purple is the legal property of its developers, whose names are too numerous
4 * to list here. Please refer to the COPYRIGHT file distributed with this
5 * source distribution.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
21 #include "internal.h"
22 #include "cmds.h"
23 #include "connection.h"
24 #include "conversation.h"
25 #include "core.h"
26 #include "debug.h"
27 #include "xfer.h"
28 #include "glibcompat.h"
29 #include "idle.h"
30 #include "image-store.h"
31 #include "keyring.h"
32 #include "message.h"
33 #include "network.h"
34 #include "notify.h"
35 #include "plugins.h"
36 #include "pounce.h"
37 #include "prefs.h"
38 #include "proxy.h"
39 #include "savedstatuses.h"
40 #include "signals.h"
41 #include "smiley-custom.h"
42 #include "smiley-parser.h"
43 #include "smiley-theme.h"
44 #include "sound.h"
45 #include "sound-theme-loader.h"
46 #include "sslconn.h"
47 #include "status.h"
48 #include "stun.h"
49 #include "theme-manager.h"
50 #include "util.h"
52 struct PurpleCore
54 char *ui;
56 void *reserved;
59 static PurpleCoreUiOps *_ops = NULL;
60 static PurpleCore *_core = NULL;
62 static void
63 purple_core_print_version(void)
65 GHashTable *ui_info = purple_core_get_ui_info();
66 const gchar *ui_name;
67 const gchar *ui_version;
68 gchar *ui_full_name = NULL;
70 ui_name = ui_info ? g_hash_table_lookup(ui_info, "name") : NULL;
71 ui_version = ui_info ? g_hash_table_lookup(ui_info, "version") : NULL;
73 if (ui_name) {
74 if (ui_version) {
75 ui_full_name = g_strdup_printf("%s %s", ui_name, ui_version);
76 } else {
77 ui_full_name = g_strdup(ui_name);
81 purple_debug_info("main", "Launching %s%slibpurple %s",
82 ui_full_name ? ui_full_name : "",
83 ui_full_name ? " with " : "",
84 purple_core_get_version());
86 g_free(ui_full_name);
89 gboolean
90 purple_core_init(const char *ui)
92 PurpleCoreUiOps *ops;
93 PurpleCore *core;
95 g_return_val_if_fail(ui != NULL, FALSE);
96 g_return_val_if_fail(purple_get_core() == NULL, FALSE);
98 bindtextdomain(PACKAGE, PURPLE_LOCALEDIR);
100 #ifdef _WIN32
101 wpurple_init();
102 #endif
104 _core = core = g_new0(PurpleCore, 1);
105 core->ui = g_strdup(ui);
106 core->reserved = NULL;
108 ops = purple_core_get_ui_ops();
110 /* The signals subsystem is important and should be first. */
111 purple_signals_init();
113 purple_util_init();
115 purple_signal_register(core, "uri-handler",
116 purple_marshal_BOOLEAN__POINTER_POINTER_POINTER,
117 G_TYPE_BOOLEAN, 3,
118 G_TYPE_STRING, /* Protocol */
119 G_TYPE_STRING, /* Command */
120 G_TYPE_POINTER); /* Parameters (GHashTable *) */
122 purple_signal_register(core, "quitting", purple_marshal_VOID, G_TYPE_NONE,
124 purple_signal_register(core, "core-initialized", purple_marshal_VOID,
125 G_TYPE_NONE, 0);
127 purple_core_print_version();
129 /* The prefs subsystem needs to be initialized before static protocols
130 * for protocol prefs to work. */
131 purple_prefs_init();
133 purple_debug_init();
135 if (ops != NULL)
137 if (ops->ui_prefs_init != NULL)
138 ops->ui_prefs_init();
140 if (ops->debug_ui_init != NULL)
141 ops->debug_ui_init();
144 purple_cmds_init();
145 purple_protocols_init();
147 /* Since plugins get probed so early we should probably initialize their
148 * subsystem right away too.
150 purple_plugins_init();
152 purple_keyring_init(); /* before accounts */
153 purple_theme_manager_init();
155 /* The buddy icon code uses the image store, so init it early. */
156 _purple_image_store_init();
158 /* Accounts use status, buddy icons and connection signals, so
159 * initialize these before accounts
161 purple_statuses_init();
162 purple_buddy_icons_init();
163 purple_connections_init();
165 purple_accounts_init();
166 purple_savedstatuses_init();
167 purple_notify_init();
168 _purple_message_init();
169 purple_conversations_init();
170 purple_blist_init();
171 purple_log_init();
172 purple_network_init();
173 purple_pounces_init();
174 purple_proxy_init();
175 purple_sound_init();
176 purple_stun_init();
177 purple_xfers_init();
178 purple_idle_init();
179 _purple_smiley_custom_init();
180 _purple_smiley_parser_init();
183 * Call this early on to try to auto-detect our IP address and
184 * hopefully save some time later.
186 purple_network_get_my_ip(-1);
188 if (ops != NULL && ops->ui_init != NULL)
189 ops->ui_init();
191 /* The UI may have registered some theme types, so refresh them */
192 purple_theme_manager_refresh();
194 /* Load the buddy list after UI init */
195 purple_blist_boot();
197 purple_signal_emit(purple_get_core(), "core-initialized");
199 return TRUE;
202 void
203 purple_core_quit(void)
205 PurpleCoreUiOps *ops;
206 PurpleCore *core = purple_get_core();
208 g_return_if_fail(core != NULL);
210 /* The self destruct sequence has been initiated */
211 purple_signal_emit(purple_get_core(), "quitting");
213 /* Transmission ends */
214 purple_connections_disconnect_all();
216 /* Save .xml files, remove signals, etc. */
217 _purple_smiley_theme_uninit();
218 _purple_smiley_custom_uninit();
219 _purple_smiley_parser_uninit();
220 purple_idle_uninit();
221 purple_pounces_uninit();
222 purple_conversations_uninit();
223 purple_blist_uninit();
224 purple_notify_uninit();
225 purple_connections_uninit();
226 purple_buddy_icons_uninit();
227 purple_savedstatuses_uninit();
228 purple_statuses_uninit();
229 purple_accounts_uninit();
230 purple_keyring_uninit(); /* after accounts */
231 purple_sound_uninit();
232 purple_theme_manager_uninit();
233 purple_xfers_uninit();
234 purple_proxy_uninit();
235 _purple_image_store_uninit();
236 purple_network_uninit();
238 ops = purple_core_get_ui_ops();
239 if (ops != NULL && ops->quit != NULL)
240 ops->quit();
242 /* Everything after prefs_uninit must not try to read any prefs */
243 purple_prefs_uninit();
244 purple_plugins_uninit();
246 purple_protocols_uninit();
248 purple_cmds_uninit();
249 purple_log_uninit();
250 _purple_message_uninit();
251 /* Everything after util_uninit cannot try to write things to the
252 * confdir nor use purple_escape_js
254 purple_util_uninit();
256 purple_signals_uninit();
258 g_free(core->ui);
259 g_free(core);
261 #ifdef _WIN32
262 wpurple_cleanup();
263 #endif
265 _core = NULL;
268 gboolean
269 purple_core_quit_cb(gpointer unused)
271 purple_core_quit();
273 return FALSE;
276 const char *
277 purple_core_get_version(void)
279 return VERSION;
282 const char *
283 purple_core_get_ui(void)
285 PurpleCore *core = purple_get_core();
287 g_return_val_if_fail(core != NULL, NULL);
289 return core->ui;
292 PurpleCore *
293 purple_get_core(void)
295 return _core;
298 static PurpleCoreUiOps *
299 purple_core_ui_ops_copy(PurpleCoreUiOps *ops)
301 PurpleCoreUiOps *ops_new;
303 g_return_val_if_fail(ops != NULL, NULL);
305 ops_new = g_new(PurpleCoreUiOps, 1);
306 *ops_new = *ops;
308 return ops_new;
311 GType
312 purple_core_ui_ops_get_type(void)
314 static GType type = 0;
316 if (type == 0) {
317 type = g_boxed_type_register_static("PurpleCoreUiOps",
318 (GBoxedCopyFunc)purple_core_ui_ops_copy,
319 (GBoxedFreeFunc)g_free);
322 return type;
325 void
326 purple_core_set_ui_ops(PurpleCoreUiOps *ops)
328 _ops = ops;
331 PurpleCoreUiOps *
332 purple_core_get_ui_ops(void)
334 return _ops;
337 GHashTable* purple_core_get_ui_info() {
338 PurpleCoreUiOps *ops = purple_core_get_ui_ops();
340 if(NULL == ops || NULL == ops->get_ui_info)
341 return NULL;
343 return ops->get_ui_info();
346 #define MIGRATE_TO_XDG_DIR(xdg_base_dir, legacy_path) \
347 G_STMT_START { \
348 gboolean migrate_res; \
350 migrate_res = purple_move_to_xdg_base_dir(xdg_base_dir, legacy_path); \
351 if (!migrate_res) { \
352 purple_debug_error("core", "Error migrating %s to %s\n", \
353 legacy_path, xdg_base_dir); \
354 return FALSE; \
356 } G_STMT_END
358 gboolean
359 purple_core_migrate_to_xdg_base_dirs(void)
361 gboolean xdg_dir_exists;
363 xdg_dir_exists = g_file_test(purple_data_dir(), G_FILE_TEST_EXISTS);
364 if (!xdg_dir_exists) {
365 MIGRATE_TO_XDG_DIR(purple_data_dir(), "certificates");
366 MIGRATE_TO_XDG_DIR(purple_data_dir(), "custom_smiley");
367 MIGRATE_TO_XDG_DIR(purple_data_dir(), "logs");
368 MIGRATE_TO_XDG_DIR(purple_config_dir(), "accounts.xml");
369 MIGRATE_TO_XDG_DIR(purple_config_dir(), "blist.xml");
370 MIGRATE_TO_XDG_DIR(purple_config_dir(), "fs-codec.conf");
371 MIGRATE_TO_XDG_DIR(purple_config_dir(), "fs-element.conf");
372 MIGRATE_TO_XDG_DIR(purple_config_dir(), "pounces.xml");
373 MIGRATE_TO_XDG_DIR(purple_config_dir(), "prefs.xml");
374 MIGRATE_TO_XDG_DIR(purple_config_dir(), "smileys.xml");
375 MIGRATE_TO_XDG_DIR(purple_config_dir(), "status.xml");
378 return TRUE;