Standardize all protocol header guard macros.
[pidgin-git.git] / libpurple / core.c
blobbf22616a9e9e2eb3a92f071ac847ab05e97cb840
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 "http.h"
30 #include "idle.h"
31 #include "image-store.h"
32 #include "keyring.h"
33 #include "message.h"
34 #include "network.h"
35 #include "notify.h"
36 #include "plugins.h"
37 #include "pounce.h"
38 #include "prefs.h"
39 #include "proxy.h"
40 #include "savedstatuses.h"
41 #include "signals.h"
42 #include "smiley-custom.h"
43 #include "smiley-parser.h"
44 #include "smiley-theme.h"
45 #include "sound.h"
46 #include "sound-theme-loader.h"
47 #include "sslconn.h"
48 #include "status.h"
49 #include "stun.h"
50 #include "theme-manager.h"
51 #include "util.h"
53 struct PurpleCore
55 char *ui;
57 void *reserved;
60 static PurpleCoreUiOps *_ops = NULL;
61 static PurpleCore *_core = NULL;
63 static void
64 purple_core_print_version(void)
66 GHashTable *ui_info = purple_core_get_ui_info();
67 const gchar *ui_name;
68 const gchar *ui_version;
69 gchar *ui_full_name = NULL;
71 ui_name = ui_info ? g_hash_table_lookup(ui_info, "name") : NULL;
72 ui_version = ui_info ? g_hash_table_lookup(ui_info, "version") : NULL;
74 if (ui_name) {
75 ui_full_name = g_strdup_printf("%s%s%s", ui_name,
76 ui_version ? " " : "", ui_version);
79 purple_debug_info("main", "Launching %s%slibpurple %s",
80 ui_full_name ? ui_full_name : "",
81 ui_full_name ? " with " : "",
82 purple_core_get_version());
86 gboolean
87 purple_core_init(const char *ui)
89 PurpleCoreUiOps *ops;
90 PurpleCore *core;
92 g_return_val_if_fail(ui != NULL, FALSE);
93 g_return_val_if_fail(purple_get_core() == NULL, FALSE);
95 bindtextdomain(PACKAGE, PURPLE_LOCALEDIR);
97 #ifdef _WIN32
98 wpurple_init();
99 #endif
101 _core = core = g_new0(PurpleCore, 1);
102 core->ui = g_strdup(ui);
103 core->reserved = NULL;
105 ops = purple_core_get_ui_ops();
107 /* The signals subsystem is important and should be first. */
108 purple_signals_init();
110 purple_util_init();
112 purple_signal_register(core, "uri-handler",
113 purple_marshal_BOOLEAN__POINTER_POINTER_POINTER,
114 G_TYPE_BOOLEAN, 3,
115 G_TYPE_STRING, /* Protocol */
116 G_TYPE_STRING, /* Command */
117 G_TYPE_POINTER); /* Parameters (GHashTable *) */
119 purple_signal_register(core, "quitting", purple_marshal_VOID, G_TYPE_NONE,
121 purple_signal_register(core, "core-initialized", purple_marshal_VOID,
122 G_TYPE_NONE, 0);
124 purple_core_print_version();
126 /* The prefs subsystem needs to be initialized before static protocols
127 * for protocol prefs to work. */
128 purple_prefs_init();
130 purple_debug_init();
132 if (ops != NULL)
134 if (ops->ui_prefs_init != NULL)
135 ops->ui_prefs_init();
137 if (ops->debug_ui_init != NULL)
138 ops->debug_ui_init();
141 purple_cmds_init();
142 purple_protocols_init();
144 /* Since plugins get probed so early we should probably initialize their
145 * subsystem right away too.
147 purple_plugins_init();
149 purple_keyring_init(); /* before accounts */
150 purple_theme_manager_init();
152 /* The buddy icon code uses the image store, so init it early. */
153 _purple_image_store_init();
155 /* Accounts use status, buddy icons and connection signals, so
156 * initialize these before accounts
158 purple_statuses_init();
159 purple_buddy_icons_init();
160 purple_connections_init();
162 purple_accounts_init();
163 purple_savedstatuses_init();
164 purple_notify_init();
165 _purple_message_init();
166 purple_conversations_init();
167 purple_blist_init();
168 purple_log_init();
169 purple_network_init();
170 purple_pounces_init();
171 purple_proxy_init();
172 purple_sound_init();
173 purple_stun_init();
174 purple_xfers_init();
175 purple_idle_init();
176 purple_http_init();
177 _purple_smiley_custom_init();
178 _purple_smiley_parser_init();
181 * Call this early on to try to auto-detect our IP address and
182 * hopefully save some time later.
184 purple_network_get_my_ip(-1);
186 if (ops != NULL && ops->ui_init != NULL)
187 ops->ui_init();
189 /* The UI may have registered some theme types, so refresh them */
190 purple_theme_manager_refresh();
192 /* Load the buddy list after UI init */
193 purple_blist_boot();
195 purple_signal_emit(purple_get_core(), "core-initialized");
197 return TRUE;
200 void
201 purple_core_quit(void)
203 PurpleCoreUiOps *ops;
204 PurpleCore *core = purple_get_core();
206 g_return_if_fail(core != NULL);
208 /* The self destruct sequence has been initiated */
209 purple_signal_emit(purple_get_core(), "quitting");
211 /* Transmission ends */
212 purple_connections_disconnect_all();
214 /* Save .xml files, remove signals, etc. */
215 _purple_smiley_theme_uninit();
216 _purple_smiley_custom_uninit();
217 _purple_smiley_parser_uninit();
218 purple_http_uninit();
219 purple_idle_uninit();
220 purple_pounces_uninit();
221 purple_conversations_uninit();
222 purple_blist_uninit();
223 purple_notify_uninit();
224 purple_connections_uninit();
225 purple_buddy_icons_uninit();
226 purple_savedstatuses_uninit();
227 purple_statuses_uninit();
228 purple_accounts_uninit();
229 purple_keyring_uninit(); /* after accounts */
230 purple_sound_uninit();
231 purple_theme_manager_uninit();
232 purple_xfers_uninit();
233 purple_proxy_uninit();
234 _purple_image_store_uninit();
235 purple_network_uninit();
237 ops = purple_core_get_ui_ops();
238 if (ops != NULL && ops->quit != NULL)
239 ops->quit();
241 /* Everything after prefs_uninit must not try to read any prefs */
242 purple_prefs_uninit();
243 purple_plugins_uninit();
245 purple_protocols_uninit();
247 purple_cmds_uninit();
248 purple_log_uninit();
249 _purple_message_uninit();
250 /* Everything after util_uninit cannot try to write things to the
251 * confdir nor use purple_escape_js
253 purple_util_uninit();
255 purple_signals_uninit();
257 g_free(core->ui);
258 g_free(core);
260 #ifdef _WIN32
261 wpurple_cleanup();
262 #endif
264 _core = NULL;
267 gboolean
268 purple_core_quit_cb(gpointer unused)
270 purple_core_quit();
272 return FALSE;
275 const char *
276 purple_core_get_version(void)
278 return VERSION;
281 const char *
282 purple_core_get_ui(void)
284 PurpleCore *core = purple_get_core();
286 g_return_val_if_fail(core != NULL, NULL);
288 return core->ui;
291 PurpleCore *
292 purple_get_core(void)
294 return _core;
297 static PurpleCoreUiOps *
298 purple_core_ui_ops_copy(PurpleCoreUiOps *ops)
300 PurpleCoreUiOps *ops_new;
302 g_return_val_if_fail(ops != NULL, NULL);
304 ops_new = g_new(PurpleCoreUiOps, 1);
305 *ops_new = *ops;
307 return ops_new;
310 GType
311 purple_core_ui_ops_get_type(void)
313 static GType type = 0;
315 if (type == 0) {
316 type = g_boxed_type_register_static("PurpleCoreUiOps",
317 (GBoxedCopyFunc)purple_core_ui_ops_copy,
318 (GBoxedFreeFunc)g_free);
321 return type;
324 void
325 purple_core_set_ui_ops(PurpleCoreUiOps *ops)
327 _ops = ops;
330 PurpleCoreUiOps *
331 purple_core_get_ui_ops(void)
333 return _ops;
336 GHashTable* purple_core_get_ui_info() {
337 PurpleCoreUiOps *ops = purple_core_get_ui_ops();
339 if(NULL == ops || NULL == ops->get_ui_info)
340 return NULL;
342 return ops->get_ui_info();
345 #define MIGRATE_TO_XDG_DIR(xdg_base_dir, legacy_path) \
346 G_STMT_START { \
347 gboolean migrate_res; \
349 migrate_res = purple_move_to_xdg_base_dir(xdg_base_dir, legacy_path); \
350 if (!migrate_res) { \
351 purple_debug_error("core", "Error migrating %s to %s\n", \
352 legacy_path, xdg_base_dir); \
353 return FALSE; \
355 } G_STMT_END
357 gboolean
358 purple_core_migrate_to_xdg_base_dirs(void)
360 gboolean xdg_dir_exists;
362 xdg_dir_exists = g_file_test(purple_data_dir(), G_FILE_TEST_EXISTS);
363 if (!xdg_dir_exists) {
364 MIGRATE_TO_XDG_DIR(purple_data_dir(), "certificates");
365 MIGRATE_TO_XDG_DIR(purple_data_dir(), "custom_smiley");
366 MIGRATE_TO_XDG_DIR(purple_data_dir(), "logs");
367 MIGRATE_TO_XDG_DIR(purple_config_dir(), "accounts.xml");
368 MIGRATE_TO_XDG_DIR(purple_config_dir(), "blist.xml");
369 MIGRATE_TO_XDG_DIR(purple_config_dir(), "fs-codec.conf");
370 MIGRATE_TO_XDG_DIR(purple_config_dir(), "fs-element.conf");
371 MIGRATE_TO_XDG_DIR(purple_config_dir(), "pounces.xml");
372 MIGRATE_TO_XDG_DIR(purple_config_dir(), "prefs.xml");
373 MIGRATE_TO_XDG_DIR(purple_config_dir(), "smileys.xml");
374 MIGRATE_TO_XDG_DIR(purple_config_dir(), "status.xml");
377 return TRUE;