| 1 | /* |
|---|
| 2 | * WengoPhone, a voice over Internet phone |
|---|
| 3 | * Copyright (C) 2004-2007 Wengo |
|---|
| 4 | * |
|---|
| 5 | * This program is free software; you can redistribute it and/or modify |
|---|
| 6 | * it under the terms of the GNU General Public License as published by |
|---|
| 7 | * the Free Software Foundation; either version 2 of the License, or |
|---|
| 8 | * (at your option) any later version. |
|---|
| 9 | * |
|---|
| 10 | * This program is distributed in the hope that it will be useful, |
|---|
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 13 | * GNU General Public License for more details. |
|---|
| 14 | * |
|---|
| 15 | * You should have received a copy of the GNU General Public License |
|---|
| 16 | * along with this program; if not, write to the Free Software |
|---|
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 18 | */ |
|---|
| 19 | |
|---|
| 20 | #include <cutil/global.h> |
|---|
| 21 | |
|---|
| 22 | #ifdef OS_WINDOWS |
|---|
| 23 | #include <winsock2.h> |
|---|
| 24 | #include <windows.h> |
|---|
| 25 | #endif |
|---|
| 26 | |
|---|
| 27 | #include <stdlib.h> |
|---|
| 28 | |
|---|
| 29 | //#ifndef CC_MSVC8 |
|---|
| 30 | //extern "C" { |
|---|
| 31 | //#endif |
|---|
| 32 | /*#include "glib.h" |
|---|
| 33 | #include "libpurple/account.h" |
|---|
| 34 | #include "libpurple/blist.h" |
|---|
| 35 | #include "libpurple/connection.h" |
|---|
| 36 | #include "libpurple/conversation.h" |
|---|
| 37 | #include "libpurple/core.h" |
|---|
| 38 | #include "libpurple/eventloop.h" |
|---|
| 39 | #include "libpurple/internal.h" |
|---|
| 40 | #include "libpurple/privacy.h" |
|---|
| 41 | #include "libpurple/util.h"*/ |
|---|
| 42 | #ifdef OS_WINDOWS |
|---|
| 43 | //#include "libpurple/win32/win32dep.h" |
|---|
| 44 | #endif |
|---|
| 45 | #include "libpurple/purple.h" |
|---|
| 46 | //#ifndef CC_MSVC8 |
|---|
| 47 | //} |
|---|
| 48 | //#endif |
|---|
| 49 | |
|---|
| 50 | #include "PurpleIMFactory.h" |
|---|
| 51 | #include "PurpleAccountMngr.h" |
|---|
| 52 | #include "PurpleChatMngr.h" |
|---|
| 53 | #include "PurpleConnectMngr.h" |
|---|
| 54 | #include "PurpleContactListMngr.h" |
|---|
| 55 | #include "PurplePresenceMngr.h" |
|---|
| 56 | |
|---|
| 57 | #include <util/File.h> |
|---|
| 58 | #define LOGGER_COMPONENT "Purple" |
|---|
| 59 | #include <util/Logger.h> |
|---|
| 60 | #include <util/Path.h> |
|---|
| 61 | |
|---|
| 62 | extern PurpleConversationUiOps chat_wg_ops; |
|---|
| 63 | extern PurpleBlistUiOps blist_wg_ops; |
|---|
| 64 | extern PurpleBlistUiOps null_blist_wg_ops; |
|---|
| 65 | extern PurpleConnectionUiOps conn_wg_ops; |
|---|
| 66 | extern PurpleConnectionUiOps null_conn_wg_ops; |
|---|
| 67 | extern PurpleAccountUiOps acc_wg_ops; |
|---|
| 68 | extern PurpleAccountUiOps null_acc_wg_ops; |
|---|
| 69 | extern PurplePrivacyUiOps privacy_wg_ops; |
|---|
| 70 | |
|---|
| 71 | /* Static vars */ |
|---|
| 72 | static GMainLoop * gMainLoop = NULL; |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | /* ********************* PURPLE CALLBACK ********************* */ |
|---|
| 76 | #define PURPLE_WG_READ_COND (G_IO_IN | G_IO_HUP | G_IO_ERR) |
|---|
| 77 | #define PURPLE_WG_WRITE_COND (G_IO_OUT | G_IO_HUP | G_IO_ERR | G_IO_NVAL) |
|---|
| 78 | |
|---|
| 79 | extern "C" GIOChannel *wpurple_g_io_channel_win32_new_socket(int socket); |
|---|
| 80 | |
|---|
| 81 | typedef struct _PurpleWgIOClosure { |
|---|
| 82 | PurpleInputFunction function; |
|---|
| 83 | guint result; |
|---|
| 84 | gpointer data; |
|---|
| 85 | } PurpleWgIOClosure; |
|---|
| 86 | |
|---|
| 87 | static void purple_wg_io_destroy(gpointer data) { |
|---|
| 88 | g_free(data); |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | static gboolean purple_wg_io_invoke(GIOChannel * source, GIOCondition condition, gpointer data) { |
|---|
| 92 | |
|---|
| 93 | PurpleInputCondition purple_cond = (PurpleInputCondition) 0; |
|---|
| 94 | if (condition & PURPLE_WG_READ_COND) { |
|---|
| 95 | purple_cond = (PurpleInputCondition)(purple_cond|PURPLE_INPUT_READ); |
|---|
| 96 | } |
|---|
| 97 | if (condition & PURPLE_WG_WRITE_COND) { |
|---|
| 98 | purple_cond = (PurpleInputCondition)(purple_cond|PURPLE_INPUT_WRITE); |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | #ifdef OS_WINDOWS |
|---|
| 102 | if (!purple_cond) { |
|---|
| 103 | return TRUE; |
|---|
| 104 | } |
|---|
| 105 | #endif /* OS_WINDOWS */ |
|---|
| 106 | |
|---|
| 107 | PurpleWgIOClosure * closure = (PurpleWgIOClosure *) data; |
|---|
| 108 | |
|---|
| 109 | closure->function(closure->data, g_io_channel_unix_get_fd(source), purple_cond); |
|---|
| 110 | |
|---|
| 111 | return TRUE; |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | static guint purple_wg_input_add(gint fd, PurpleInputCondition condition, |
|---|
| 115 | PurpleInputFunction function, gpointer data) { |
|---|
| 116 | |
|---|
| 117 | PurpleWgIOClosure * closure = g_new0(PurpleWgIOClosure, 1); |
|---|
| 118 | |
|---|
| 119 | closure->function = function; |
|---|
| 120 | closure->data = data; |
|---|
| 121 | |
|---|
| 122 | GIOCondition cond = (GIOCondition) 0; |
|---|
| 123 | if (condition & PURPLE_INPUT_READ) { |
|---|
| 124 | cond = (GIOCondition)(cond|PURPLE_WG_READ_COND); |
|---|
| 125 | } |
|---|
| 126 | if (condition & PURPLE_INPUT_WRITE) { |
|---|
| 127 | cond = (GIOCondition)(cond|PURPLE_WG_WRITE_COND); |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | GIOChannel * channel; |
|---|
| 131 | #ifdef OS_WINDOWS |
|---|
| 132 | channel = wpurple_g_io_channel_win32_new_socket(fd); |
|---|
| 133 | #else |
|---|
| 134 | channel = g_io_channel_unix_new(fd); |
|---|
| 135 | #endif |
|---|
| 136 | closure->result = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, cond, |
|---|
| 137 | purple_wg_io_invoke, closure, purple_wg_io_destroy); |
|---|
| 138 | |
|---|
| 139 | g_io_channel_unref(channel); |
|---|
| 140 | return closure->result; |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | static void* purple_wg_request_action(const char *title, const char *primary, |
|---|
| 144 | const char *secondary, int default_action, |
|---|
| 145 | PurpleAccount *account, const char *who, |
|---|
| 146 | PurpleConversation *conv, void *user_data, size_t action_count, |
|---|
| 147 | va_list actions) { |
|---|
| 148 | const char *text = va_arg(actions, const char *); |
|---|
| 149 | PurpleRequestActionCb callback = va_arg(actions, PurpleRequestActionCb); |
|---|
| 150 | |
|---|
| 151 | LOG_WARN("requested action: %s: %s, %s. Default action: %s", title, primary, secondary, text); |
|---|
| 152 | callback(user_data, 0); |
|---|
| 153 | return NULL; |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | gpointer PurpleMainEventLoop(gpointer data) { |
|---|
| 157 | if (gMainLoop) { |
|---|
| 158 | LOG_FATAL("gMainLoop already created"); |
|---|
| 159 | } |
|---|
| 160 | gMainLoop = g_main_loop_new(NULL, FALSE); |
|---|
| 161 | LOG_DEBUG("Starting gMainLoop"); |
|---|
| 162 | |
|---|
| 163 | g_main_loop_run(gMainLoop); |
|---|
| 164 | g_main_loop_unref(gMainLoop); |
|---|
| 165 | LOG_DEBUG("gMainLoop stopped"); |
|---|
| 166 | gMainLoop = NULL; |
|---|
| 167 | g_thread_exit(NULL); |
|---|
| 168 | return NULL; |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | |
|---|
| 172 | /* ******************************************************* */ |
|---|
| 173 | |
|---|
| 174 | static PurpleCoreUiOps core_wg_ops = { |
|---|
| 175 | NULL, |
|---|
| 176 | NULL, |
|---|
| 177 | PurpleIMFactory::PurpleSetCallbacks, |
|---|
| 178 | PurpleIMFactory::PurpleQuitCallback, |
|---|
| 179 | }; |
|---|
| 180 | |
|---|
| 181 | static PurpleEventLoopUiOps eventloop_wg_ops = { |
|---|
| 182 | g_timeout_add, |
|---|
| 183 | g_source_remove, |
|---|
| 184 | purple_wg_input_add, |
|---|
| 185 | g_source_remove |
|---|
| 186 | }; |
|---|
| 187 | |
|---|
| 188 | static PurpleRequestUiOps request_wg_ops = { |
|---|
| 189 | NULL, |
|---|
| 190 | NULL, |
|---|
| 191 | purple_wg_request_action, |
|---|
| 192 | NULL, |
|---|
| 193 | NULL, |
|---|
| 194 | NULL, |
|---|
| 195 | NULL, |
|---|
| 196 | NULL, |
|---|
| 197 | NULL, |
|---|
| 198 | NULL |
|---|
| 199 | }; |
|---|
| 200 | |
|---|
| 201 | void MyPurpleIMInit(const gchar* pathToProfile) { |
|---|
| 202 | |
|---|
| 203 | char * home_dir = g_build_filename(pathToProfile, "purple", NULL); |
|---|
| 204 | printf("pathToProfile %s\nEND\n", home_dir); |
|---|
| 205 | // Remove Purple config directory |
|---|
| 206 | File file(home_dir); |
|---|
| 207 | file.remove(); |
|---|
| 208 | |
|---|
| 209 | std::string ssl_certificates; |
|---|
| 210 | std::string ssl_certificates_env("QUTECOM_SSL_CERTIFICATES_DIR="); |
|---|
| 211 | #if defined(OS_WINDOWS) or defined(OS_LINUX) |
|---|
| 212 | ssl_certificates = Path::getApplicationDirPath() + Path::getPathSeparator() + "ca-certs"; |
|---|
| 213 | #elif defined(OS_MACOSX) |
|---|
| 214 | ssl_certificates = Path::getApplicationResourcesDirPath() + Path::getPathSeparator() + "ca-certs"; |
|---|
| 215 | #endif |
|---|
| 216 | |
|---|
| 217 | ssl_certificates_env += ssl_certificates; |
|---|
| 218 | |
|---|
| 219 | putenv((char*)ssl_certificates_env.c_str()); |
|---|
| 220 | |
|---|
| 221 | File::createPath(home_dir + File::getPathSeparator()); |
|---|
| 222 | purple_util_set_user_dir(home_dir); |
|---|
| 223 | |
|---|
| 224 | if(getenv("OW_PURPLE_DEBUG")) { |
|---|
| 225 | purple_debug_set_enabled(TRUE); |
|---|
| 226 | } |
|---|
| 227 | |
|---|
| 228 | purple_core_set_ui_ops(&core_wg_ops); |
|---|
| 229 | purple_eventloop_set_ui_ops(&eventloop_wg_ops); |
|---|
| 230 | |
|---|
| 231 | char * search_path = g_build_filename(Path::getApplicationDirPath().c_str(), "plugins", NULL); |
|---|
| 232 | purple_plugins_add_search_path(search_path); |
|---|
| 233 | purple_plugins_add_search_path("plugins"); |
|---|
| 234 | g_free(search_path); |
|---|
| 235 | |
|---|
| 236 | purple_request_set_ui_ops(&request_wg_ops); |
|---|
| 237 | |
|---|
| 238 | if (!purple_core_init("QuteCom PURPLE")) { |
|---|
| 239 | LOG_WARN("Initialization of the Purple core failed\n"); |
|---|
| 240 | } |
|---|
| 241 | } |
|---|
| 242 | |
|---|
| 243 | static gboolean purple_wg_init_lib(gpointer data) { |
|---|
| 244 | MyPurpleIMInit((gchar*)data); |
|---|
| 245 | g_free(data); |
|---|
| 246 | return false; |
|---|
| 247 | } |
|---|
| 248 | |
|---|
| 249 | static gboolean purple_wg_destroy_lib(gpointer data) { |
|---|
| 250 | purple_connections_set_ui_ops(&null_conn_wg_ops); |
|---|
| 251 | purple_accounts_set_ui_ops(&null_acc_wg_ops); |
|---|
| 252 | purple_blist_set_ui_ops(&null_blist_wg_ops); |
|---|
| 253 | |
|---|
| 254 | purple_core_quit(); |
|---|
| 255 | return false; |
|---|
| 256 | } |
|---|
| 257 | |
|---|
| 258 | bool PurpleIMFactory::equals(const IMAccount & imAccount, std::string login, EnumIMProtocol::IMProtocol protocol) { |
|---|
| 259 | return ((imAccount.getLogin() == login) && (imAccount.getProtocol() == protocol)); |
|---|
| 260 | } |
|---|
| 261 | |
|---|
| 262 | PurpleIMFactory::PurpleIMFactory() { |
|---|
| 263 | AccountMngr = PurpleAccountMngr::getInstance(); |
|---|
| 264 | ConnectMngr = PurpleConnectMngr::getInstance(); |
|---|
| 265 | PresenceMngr = PurplePresenceMngr::getInstance(); |
|---|
| 266 | ChatMngr = PurpleChatMngr::getInstance(); |
|---|
| 267 | ContactListMngr = PurpleContactListMngr::getInstance(); |
|---|
| 268 | //we are using the Qt glib eventloop on linux if Qt is configued with glib option |
|---|
| 269 | #if !defined(QT_WITH_GLIB) || !defined(OS_LINUX) |
|---|
| 270 | if (!g_thread_supported()) { |
|---|
| 271 | g_thread_init(NULL); |
|---|
| 272 | } |
|---|
| 273 | g_thread_create(PurpleMainEventLoop, NULL, FALSE, NULL); |
|---|
| 274 | #endif /* !defined(QT_WITH_GLIB) || !defined(OS_LINUX) */ |
|---|
| 275 | } |
|---|
| 276 | |
|---|
| 277 | PurpleIMFactory::~PurpleIMFactory() { |
|---|
| 278 | #if !defined(QT_WITH_GLIB) || !defined(OS_LINUX) |
|---|
| 279 | if (gMainLoop) { |
|---|
| 280 | LOG_DEBUG("Stopping gMainLoop"); |
|---|
| 281 | g_main_loop_quit(gMainLoop); |
|---|
| 282 | } else { |
|---|
| 283 | LOG_ERROR("No gMainLoop created"); |
|---|
| 284 | } |
|---|
| 285 | #endif /* !defined(QT_WITH_GLIB) || !defined(OS_LINUX) */ |
|---|
| 286 | } |
|---|
| 287 | |
|---|
| 288 | void PurpleIMFactory::PurpleSetCallbacks() { |
|---|
| 289 | purple_accounts_set_ui_ops(&acc_wg_ops); |
|---|
| 290 | purple_blist_set_ui_ops(&blist_wg_ops); |
|---|
| 291 | purple_privacy_set_ui_ops(&privacy_wg_ops); |
|---|
| 292 | purple_connections_set_ui_ops(&conn_wg_ops); |
|---|
| 293 | } |
|---|
| 294 | |
|---|
| 295 | void PurpleIMFactory::PurpleWrapperInit() { |
|---|
| 296 | AccountMngr->Init(); |
|---|
| 297 | ConnectMngr->Init(); |
|---|
| 298 | ContactListMngr->Init(); |
|---|
| 299 | ChatMngr->Init(); |
|---|
| 300 | PresenceMngr->Init(); |
|---|
| 301 | } |
|---|
| 302 | |
|---|
| 303 | void PurpleIMFactory::init(const std::string & pathToProfile) { |
|---|
| 304 | g_idle_add(purple_wg_init_lib, (gpointer)g_strdup(pathToProfile.c_str())); |
|---|
| 305 | PurpleWrapperInit(); |
|---|
| 306 | } |
|---|
| 307 | |
|---|
| 308 | void PurpleIMFactory::PurpleQuitCallback() { |
|---|
| 309 | LOG_DEBUG(""); |
|---|
| 310 | // Don't stop gMainLoop here: this is called when the user logoff, but we |
|---|
| 311 | // want to keep the loop running for the whole time the application is |
|---|
| 312 | // running. |
|---|
| 313 | } |
|---|
| 314 | |
|---|
| 315 | void PurpleIMFactory::terminate() { |
|---|
| 316 | if (AccountMngr) { |
|---|
| 317 | AccountMngr->reset(); |
|---|
| 318 | } |
|---|
| 319 | g_idle_add(purple_wg_destroy_lib, NULL); |
|---|
| 320 | } |
|---|
| 321 | |
|---|
| 322 | IMConnect * PurpleIMFactory::createIMConnect(IMAccount &account) { |
|---|
| 323 | return ConnectMngr->AddIMConnect(account); |
|---|
| 324 | } |
|---|
| 325 | |
|---|
| 326 | IMChat * PurpleIMFactory::createIMChat(IMAccount &account) { |
|---|
| 327 | return ChatMngr->AddIMChat(account); |
|---|
| 328 | } |
|---|
| 329 | |
|---|
| 330 | IMPresence * PurpleIMFactory::createIMPresence(IMAccount &account) { |
|---|
| 331 | return PresenceMngr->AddIMPresence(account); |
|---|
| 332 | } |
|---|
| 333 | |
|---|
| 334 | IMContactList * PurpleIMFactory::createIMContactList(IMAccount &account) { |
|---|
| 335 | return ContactListMngr->AddIMContactList(account); |
|---|
| 336 | } |
|---|
| 337 | |
|---|
| 338 | void PurpleIMFactory::removeIMAccount(IMAccount imAccount) { |
|---|
| 339 | AccountMngr->RemoveIMAccount(imAccount); |
|---|
| 340 | } |
|---|
| 341 | |
|---|
| 342 | void PurpleIMFactory::imAccountUpdated(IMAccount imAccount) { |
|---|
| 343 | AccountMngr->UpdateIMAccount(imAccount); |
|---|
| 344 | } |
|---|