chatty-purple-init.c 8.69 KB
Newer Older
Andrea Schaefer's avatar
Andrea Schaefer committed
1
2
3
/*
 * Copyright (C) 2018 Purism SPC
 *
4
 * SPDX-License-Identifier: GPL-3.0-or-later
Andrea Schaefer's avatar
Andrea Schaefer committed
5
6
 */

Guido Gunther's avatar
Guido Gunther committed
7
#define G_LOG_DOMAIN "chatty-purple"
Andrea Schaefer's avatar
Andrea Schaefer committed
8
9
10
11
12
13
14
15
16
17
18
19

#include "purple.h"
#include "chatty-purple-init.h"
#include <glib.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include "chatty-window.h"
#include "chatty-config.h"
#include "chatty-icons.h"
#include "chatty-account.h"
20
#include "./xeps/chatty-xep-0184.h"
Andrea Schaefer's avatar
Andrea Schaefer committed
21
#include "chatty-message-list.h"
22
23
#include "chatty-purple-request.h"
#include "chatty-purple-notify.h"
Andrea Schaefer's avatar
Andrea Schaefer committed
24
25
26
27
#include "chatty-buddy-list.h"
#include "chatty-connection.h"
#include "chatty-conversation.h"

28
static GHashTable *ui_info = NULL;
Andrea Schaefer's avatar
Andrea Schaefer committed
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54

static chatty_purple_data_t chatty_purple_data;

chatty_purple_data_t *chatty_get_purple_data (void)
{
  return &chatty_purple_data;
}


typedef struct _PurpleGLibIOClosure
{
  PurpleInputFunction function;
  guint               result;
  gpointer            data;
} PurpleGLibIOClosure;


static void
purple_glib_io_destroy (gpointer data)
{
  g_free (data);
}


static gboolean
purple_glib_io_invoke (GIOChannel   *source,
55
56
                       GIOCondition  condition,
                       gpointer      data)
Andrea Schaefer's avatar
Andrea Schaefer committed
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
{
  PurpleGLibIOClosure *closure = data;
  PurpleInputCondition purple_cond = 0;

  if (condition & PURPLE_GLIB_READ_COND) {
    purple_cond |= PURPLE_INPUT_READ;
  }

  if (condition & PURPLE_GLIB_WRITE_COND) {
    purple_cond |= PURPLE_INPUT_WRITE;
  }

  closure->function (closure->data, g_io_channel_unix_get_fd (source),
                     purple_cond);

  return TRUE;
}


static guint
glib_input_add (gint                 fd,
                PurpleInputCondition condition,
                PurpleInputFunction  function,
                gpointer             data)
{

  PurpleGLibIOClosure *closure;
  GIOChannel          *channel;
85
  GIOCondition         cond = 0;
Andrea Schaefer's avatar
Andrea Schaefer committed
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101

  closure = g_new0 (PurpleGLibIOClosure, 1);

  closure->function = function;
  closure->data = data;

  if (condition & PURPLE_INPUT_READ) {
    cond |= PURPLE_GLIB_READ_COND;
  }

  if (condition & PURPLE_INPUT_WRITE) {
    cond |= PURPLE_GLIB_WRITE_COND;
  }

  channel = g_io_channel_unix_new (fd);

102
103
104
105
106
  closure->result = g_io_add_watch_full (channel,
                                         G_PRIORITY_DEFAULT,
                                         cond,
                                         purple_glib_io_invoke,
                                         closure,
Andrea Schaefer's avatar
Andrea Schaefer committed
107
108
109
110
111
112
113
114
                                         purple_glib_io_destroy);

  g_io_channel_unref (channel);
  return closure->result;
}


static
115
PurpleEventLoopUiOps eventloop_ops =
Andrea Schaefer's avatar
Andrea Schaefer committed
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
{
  g_timeout_add,
  g_source_remove,
  glib_input_add,
  g_source_remove,
  NULL,
#if GLIB_CHECK_VERSION(2,14,0)
  g_timeout_add_seconds,
#else
  NULL,
#endif
  NULL,
  NULL,
  NULL
};


133
134
135
136
137
138
139
static PurpleEventLoopUiOps *
chatty_eventloop_get_ui_ops (void)
{
  return &eventloop_ops;
}


Andrea Schaefer's avatar
Andrea Schaefer committed
140
static void
141
chatty_purple_quit (void)
Andrea Schaefer's avatar
Andrea Schaefer committed
142
143
144
145
146
{
  chatty_conversations_uninit ();
  chatty_blist_uninit ();
  chatty_connection_uninit();
  chatty_account_uninit();
Andrea Schaefer's avatar
Andrea Schaefer committed
147
148
149
150

  purple_conversations_set_ui_ops (NULL);
  purple_connections_set_ui_ops (NULL);
  purple_blist_set_ui_ops (NULL);
Andrea Schaefer's avatar
Andrea Schaefer committed
151
152
  purple_accounts_set_ui_ops (NULL);

153
154
155
156
  if (NULL != ui_info) {
    g_hash_table_destroy (ui_info);
  }

157
158
  chatty_xeps_close ();

159
  g_application_quit (g_application_get_default ());
Andrea Schaefer's avatar
Andrea Schaefer committed
160
161
162
163
}


static void
164
chatty_purple_ui_init (void)
Andrea Schaefer's avatar
Andrea Schaefer committed
165
166
167
168
169
{
  chatty_account_init ();
  chatty_connection_init ();
  chatty_blist_init ();
  chatty_conversations_init ();
Andrea Schaefer's avatar
Andrea Schaefer committed
170
171

  purple_accounts_set_ui_ops (chatty_accounts_get_ui_ops ());
172
173
  purple_request_set_ui_ops (chatty_request_get_ui_ops ());
  purple_notify_set_ui_ops (chatty_notify_get_ui_ops ());
Andrea Schaefer's avatar
Andrea Schaefer committed
174
175
  purple_connections_set_ui_ops (chatty_connection_get_ui_ops ());
  purple_blist_set_ui_ops (chatty_blist_get_ui_ops ());
Andrea Schaefer's avatar
Andrea Schaefer committed
176
177
178
179
  purple_conversations_set_ui_ops (chatty_conversations_get_conv_ui_ops ());
}


180
181
182
183
184
185
186
static void
chatty_purple_prefs_init (void)
{
  purple_prefs_add_none (CHATTY_PREFS_ROOT "");
  purple_prefs_add_none ("/plugins/chatty");

  purple_prefs_add_none (CHATTY_PREFS_ROOT "/plugins");
187
  purple_prefs_add_bool (CHATTY_PREFS_ROOT "/plugins/message_carbons", TRUE);
188
189
  purple_prefs_add_path_list (CHATTY_PREFS_ROOT "/plugins/loaded", NULL);

190
191
192
  purple_prefs_add_none (CHATTY_PREFS_ROOT "/status");
  purple_prefs_add_bool (CHATTY_PREFS_ROOT "/status/first_start", TRUE);

193
194
195
196
197
198
199
  purple_prefs_add_none (CHATTY_PREFS_ROOT "/filelocations");
  purple_prefs_add_path (CHATTY_PREFS_ROOT "/filelocations/last_save_folder", "");
  purple_prefs_add_path (CHATTY_PREFS_ROOT "/filelocations/last_open_folder", "");
  purple_prefs_add_path (CHATTY_PREFS_ROOT "/filelocations/last_icon_folder", "");
}


200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
static GHashTable *
chatty_purple_ui_get_info (void)
{
  if (NULL == ui_info) {
    ui_info = g_hash_table_new (g_str_hash, g_str_equal);

    g_hash_table_insert (ui_info, "name", CHATTY_APP_NAME);
    g_hash_table_insert (ui_info, "version", CHATTY_VERSION);
    g_hash_table_insert (ui_info, "dev_website", "https://source.puri.sm/Librem5/chatty");
    g_hash_table_insert (ui_info, "client_type", "phone");
  }

  return ui_info;
}


Andrea Schaefer's avatar
Andrea Schaefer committed
216
static
217
PurpleCoreUiOps core_ui_ops =
Andrea Schaefer's avatar
Andrea Schaefer committed
218
{
219
  chatty_purple_prefs_init,
Andrea Schaefer's avatar
Andrea Schaefer committed
220
  NULL,
221
  chatty_purple_ui_init,
222
223
  chatty_purple_quit,
  chatty_purple_ui_get_info,
Andrea Schaefer's avatar
Andrea Schaefer committed
224
225
226
227
228
229
  NULL,
  NULL,
  NULL
};


230
231
232
233
234
235
236
static PurpleCoreUiOps *
chatty_core_get_ui_ops (void)
{
  return &core_ui_ops;
}


237
gboolean
238
239
chatty_purple_load_plugin (const char *name)
{
240
  GList    *iter;
241
  gboolean  result = FALSE;
242
243
244
245
246
247
248
249

  iter = purple_plugins_get_all ();

  for (; iter; iter = iter->next) {
    PurplePlugin      *plugin = iter->data;
    PurplePluginInfo  *info = plugin->info;

    if (g_strcmp0 (info->id, name) == 0) {
250
251
252
      result = TRUE;
      g_debug ("Found plugin %s", info->name);

253
      if (!purple_plugin_is_loaded (plugin)) {
254
        result = purple_plugin_load (plugin);
255
256
257
258
259
        purple_plugins_save_loaded (CHATTY_PREFS_ROOT "/plugins/loaded");
        g_debug ("Loaded plugin %s", info->name);
      }
    }
  }
260
261

  return result;
262
263
264
}


265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
gboolean
chatty_purple_unload_plugin (const char *name)
{
  PurplePlugin  *plugin;
  gboolean       result = FALSE;

  plugin = purple_plugins_find_with_id (name);

  if (plugin != NULL) {
    result = purple_plugin_unload (plugin);

    purple_plugin_disable (plugin);

    purple_plugins_save_loaded (CHATTY_PREFS_ROOT "/plugins/loaded");
  } else {
    g_debug ("Plugin %s couldn't be unloaded, it wasn't found", name);
    return FALSE;
  }

  if (result) {
    g_debug ("Unloaded plugin %s", name);
  } else {
    g_debug ("Plugin %s couldn't be unloaded now, "
             "it will be unloaded after a restart", name);
  }

  return result;
}


295

296
void
297
chatty_purple_check_sms_plugin (void)
Andrea Schaefer's avatar
Andrea Schaefer committed
298
{
299
  PurpleAccount *account;
300
301
302
303
304
305
306
307
308
309
310
311

  account = purple_accounts_find ("SMS", "prpl-mm-sms");

  if (account == NULL) {
    chatty_account_add_sms_account ();
  }
}


void
libpurple_init (void)
{
312
  gchar         *search_path;
313
314

  chatty_data_t *chatty = chatty_get_data ();
315

316
317
  chatty_purple_data_t *chatty_purple = chatty_get_purple_data ();

318
319
  signal (SIGCHLD, SIG_IGN);

Andrea Schaefer's avatar
Andrea Schaefer committed
320
321
  purple_debug_set_enabled (!!(chatty->cml_options & CHATTY_CML_OPT_DEBUG));
  purple_debug_set_verbose (!!(chatty->cml_options & CHATTY_CML_OPT_VERBOSE));
Andrea Schaefer's avatar
Andrea Schaefer committed
322

323
324
  purple_core_set_ui_ops (chatty_core_get_ui_ops ());
  purple_eventloop_set_ui_ops (chatty_eventloop_get_ui_ops ());
Andrea Schaefer's avatar
Andrea Schaefer committed
325
326
327
328
329

  search_path = g_build_filename (purple_user_dir (), "plugins", NULL);
  purple_plugins_add_search_path (search_path);
  g_free (search_path);

Andrea Schaefer's avatar
Andrea Schaefer committed
330
  if (!purple_core_init (CHATTY_UI)) {
331
    g_printerr ("libpurple initialization failed\n");
332

333
    g_application_quit (g_application_get_default ());
Andrea Schaefer's avatar
Andrea Schaefer committed
334
335
  }

336
  if (!purple_core_ensure_single_instance ()) {
337
    g_printerr ("Another libpurple client is already running\n");
338

339
    g_application_quit (g_application_get_default ());
Andrea Schaefer's avatar
Andrea Schaefer committed
340
  }
341

Andrea Schaefer's avatar
Andrea Schaefer committed
342
343
344
  purple_set_blist (purple_blist_new ());
  purple_prefs_load ();
  purple_blist_load ();
345
346
347
348
  purple_plugins_load_saved (CHATTY_PREFS_ROOT "/plugins/loaded");

  purple_plugins_probe (G_MODULE_SUFFIX);

Andrea Schaefer's avatar
Andrea Schaefer committed
349
350
351
352
353
  if (purple_plugins_find_with_id ("core-riba-carbons") != NULL) {
    chatty_purple->plugin_carbons_available = TRUE;
  } else {
    chatty_purple->plugin_carbons_available = FALSE;
  }
354
355
356
357
358
359

  if (purple_prefs_get_bool (CHATTY_PREFS_ROOT "/plugins/message_carbons")) {
    chatty_purple->plugin_carbons_loaded = chatty_purple_load_plugin ("core-riba-carbons");
  }

  chatty_purple->plugin_lurch_loaded = chatty_purple_load_plugin ("core-riba-lurch");
360

Andrea Schaefer's avatar
Andrea Schaefer committed
361
  purple_plugins_init ();
362
  purple_network_force_online();
Andrea Schaefer's avatar
Andrea Schaefer committed
363
  purple_pounces_load ();
Andrea Schaefer's avatar
Andrea Schaefer committed
364

365
366
  chatty_xeps_init ();

367
  chatty_purple->plugin_mm_sms_loaded = chatty_purple_load_plugin ("prpl-mm-sms");
368

Andrea Schaefer's avatar
Andrea Schaefer committed
369
  purple_savedstatus_activate (purple_savedstatus_get_startup());
Andrea Schaefer's avatar
Andrea Schaefer committed
370
  purple_accounts_restore_current_statuses ();
371

372
373
  purple_blist_show ();

Guido Gunther's avatar
Guido Gunther committed
374
375
  g_debug ("libpurple initialized. Running version %s.",
           purple_core_get_version ());
Andrea Schaefer's avatar
Andrea Schaefer committed
376
}