summaryrefslogtreecommitdiff
path: root/pidgin/pidginapplication.c
blob: 177f67ae843802f71821bb49fbf3b4b0dbea0427 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
55
56
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
/*
 * Pidgin - Internet Messenger
 * Copyright (C) Pidgin Developers <devel@pidgin.im>
 *
 * Pidgin is the legal property of its developers, whose names are too numerous
 * to list here.  Please refer to the COPYRIGHT file distributed with this
 * source distribution.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses/>.
 */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <glib.h>
#include <glib/gi18n-lib.h>
#include <glib/gstdio.h>

#include <gplugin.h>
#include <purple.h>

#include <adwaita.h>

#include "pidginapplication.h"

#include "gtkblist.h"
#include "gtkdialogs.h"
#include "gtkroomlist.h"
#include "gtksavedstatuses.h"
#include "gtkxfer.h"
#include "pidginabout.h"
#include "pidginaccounteditor.h"
#include "pidginaccountmanager.h"
#include "pidginaccountsdisabledmenu.h"
#include "pidginaccountsenabledmenu.h"
#include "pidgincore.h"
#include "pidgindebug.h"
#include "pidgindisplaywindow.h"
#include "pidginmooddialog.h"
#include "pidginpluginsdialog.h"
#include "pidginpluginsmenu.h"
#include "pidginprefs.h"
#include "pidginstatuseditor.h"
#include "pidginstatusmanager.h"
#include "pidginui.h"

struct _PidginApplication {
	GtkApplication parent;

	GHashTable *action_groups;
};

/******************************************************************************
 * Globals
 *****************************************************************************/
static gchar *opt_config_dir_arg = NULL;
static gboolean opt_debug = FALSE;
static gboolean opt_nologin = FALSE;

static GOptionEntry option_entries[] = {
	{
		"config", 'c', 0, G_OPTION_ARG_FILENAME, &opt_config_dir_arg,
		N_("use DIR for config files"), N_("DIR")
	}, {
		"debug", 'd', 0, G_OPTION_ARG_NONE, &opt_debug,
		N_("print debugging messages to stdout"), NULL
	}, {
		"nologin", 'n', 0, G_OPTION_ARG_NONE, &opt_nologin,
		N_("don't automatically login"), NULL
	},
	{
		"version", 'v', 0, G_OPTION_ARG_NONE, NULL,
		N_("display the current version and exit"), NULL
	}, {
		NULL
	}
};

G_DEFINE_TYPE(PidginApplication, pidgin_application, GTK_TYPE_APPLICATION)

/******************************************************************************
 * Helpers
 *****************************************************************************/

/*
 * pidgin_application_get_account_manager:
 *
 * The Pidgin account manager can get opened from multiple actions, so this
 * helper manages the singleton.
 *
 * Since: 3.0.0
 */
static GtkWidget *
pidgin_application_get_account_manager(void) {
	static GtkWidget *manager = NULL;

	if(!PIDGIN_IS_ACCOUNT_MANAGER(manager)) {
		manager = pidgin_account_manager_new();
		g_object_add_weak_pointer(G_OBJECT(manager), (gpointer)&manager);
	}

	return manager;
}

/*
 * pidgin_application_present_transient_window:
 * @application: The application instance.
 * @window: The [class@Gtk.Window] to present.
 *
 * Presents a window and makes sure its transient parent is set to the currently
 * active window of @application.
 *
 * Since: 3.0.0
 */
static void
pidgin_application_present_transient_window(PidginApplication *application,
                                            GtkWindow *window)
{
	GtkWindow *active_window = NULL;

	g_return_if_fail(PIDGIN_IS_APPLICATION(application));
	g_return_if_fail(GTK_IS_WINDOW(window));

	active_window = pidgin_application_get_active_window(application);

	gtk_window_set_transient_for(window, active_window);

	gtk_window_present_with_time(window, GDK_CURRENT_TIME);
}

static void
pidgin_application_plugin_state_changed(G_GNUC_UNUSED GPluginManager *manager,
                                        G_GNUC_UNUSED GPluginPlugin *plugin,
                                        G_GNUC_UNUSED gpointer data)
{
	purple_plugins_save_loaded(PIDGIN_PREFS_ROOT "/plugins/loaded");
}

static void
pidgin_application_init_plugins(void) {
	GPluginManager *manager = gplugin_manager_get_default();

	gplugin_manager_append_paths_from_environment(manager,
	                                              "PIDGIN_PLUGIN_PATH");

	if(g_getenv("PURPLE_PLUGINS_SKIP")) {
		g_message("PURPLE_PLUGINS_SKIP environment variable set, skipping "
		          "normal Pidgin plugin paths");
	} else {
		gchar *path = g_build_filename(purple_data_dir(), "plugins", NULL);

		if(!g_file_test(path, G_FILE_TEST_IS_DIR)) {
			g_mkdir(path, S_IRUSR | S_IWUSR | S_IXUSR);
		}

		gplugin_manager_append_path(manager, path);
		g_free(path);

		gplugin_manager_append_path(manager, PIDGIN_LIBDIR);
	}

	g_signal_connect(manager, "loaded-plugin",
	                 G_CALLBACK(pidgin_application_plugin_state_changed), NULL);
	g_signal_connect(manager, "load-plugin-failed",
	                 G_CALLBACK(pidgin_application_plugin_state_changed), NULL);
	g_signal_connect(manager, "unloaded-plugin",
	                 G_CALLBACK(pidgin_application_plugin_state_changed), NULL);
	g_signal_connect(manager, "unload-plugin-failed",
	                 G_CALLBACK(pidgin_application_plugin_state_changed), NULL);

	purple_plugins_refresh();
}

static void
pidgin_application_populate_dynamic_menus(PidginApplication *application) {
	GMenu *target = NULL;
	GMenuModel *model = NULL;

	/* Link the AccountsDisabledMenu into its proper location. */
	model = pidgin_accounts_disabled_menu_new();
	target = gtk_application_get_menu_by_id(GTK_APPLICATION(application),
	                                        "disabled-accounts");
	g_menu_append_section(target, NULL, model);

	/* Link the AccountsEnabledMenu into its proper location. */
	model = pidgin_accounts_enabled_menu_new();
	target = gtk_application_get_menu_by_id(GTK_APPLICATION(application),
	                                        "enabled-accounts");
	g_menu_append_section(target, NULL, model);

	/* Link the PluginsMenu into its proper location. */
	model = pidgin_plugins_menu_new();
	target = gtk_application_get_menu_by_id(GTK_APPLICATION(application),
	                                        "plugins-menu");
	g_menu_append_section(target, NULL, model);
}

/******************************************************************************
 * Actions
 *****************************************************************************/
/*< private >
 * pidgin_application_online_actions:
 *
 * This list keeps track of which actions should only be enabled while online.
 */
static const gchar *pidgin_application_online_actions[] = {
	"add-buddy",
	"add-group",
	"get-user-info",
	"new-message",
	"set-mood",
};

/*< private >
 * pidgin_application_chat_actions:
 *
 * This list keeps track of which actions should only be enabled if a protocol
 * supporting groups chats is connected.
 */
static const gchar *pidgin_application_chat_actions[] = {
	"add-chat",
	"join-chat",
};

/*< private >
 * pidgin_application_room_list_actions:
 *
 * This list keeps track of which actions should only be enabled if an online
 * account supports room lists.
 */
static const gchar *pidgin_application_room_list_actions[] = {
	"room-list",
};

/*< private >
 * pidgin_action_group_actions_set_enable:
 * @group: The #PidginActionGroup instance.
 * @actions: The action names.
 * @n_actions: The number of @actions.
 * @enabled: Whether or not to enable the actions.
 *
 * Sets the enabled property of the named actions to @enabled.
 */
static void
pidgin_application_actions_set_enabled(PidginApplication *application,
                                       const gchar *const *actions,
                                       gint n_actions,
                                       gboolean enabled)
{
	gint i = 0;

	for(i = 0; i < n_actions; i++) {
		GAction *action = NULL;
		const gchar *name = actions[i];

		action = g_action_map_lookup_action(G_ACTION_MAP(application), name);

		if(action != NULL) {
			g_simple_action_set_enabled(G_SIMPLE_ACTION(action), enabled);
		} else {
			g_warning("Failed to find action named %s", name);
		}
	}
}

static void
pidgin_application_about(G_GNUC_UNUSED GSimpleAction *simple,
                         G_GNUC_UNUSED GVariant *parameter, gpointer data)
{
	PidginApplication *application = data;
	static GtkWidget *about = NULL;

	if(!GTK_IS_WIDGET(about)) {
		about = pidgin_about_dialog_new();
		g_object_add_weak_pointer(G_OBJECT(about), (gpointer)&about);
	}

	pidgin_application_present_transient_window(application, GTK_WINDOW(about));
}

static void
pidgin_application_accounts(G_GNUC_UNUSED GSimpleAction *simple,
                            G_GNUC_UNUSED GVariant *parameter, gpointer data)
{
	PidginApplication *application = data;
	GtkWidget *manager = pidgin_application_get_account_manager();

	pidgin_account_manager_show_overview(PIDGIN_ACCOUNT_MANAGER(manager));

	pidgin_application_present_transient_window(application,
	                                            GTK_WINDOW(manager));
}

static void
pidgin_application_add_buddy(G_GNUC_UNUSED GSimpleAction *simple,
                             G_GNUC_UNUSED GVariant *parameter,
                             G_GNUC_UNUSED gpointer data)
{
	purple_blist_request_add_buddy(NULL, NULL, NULL, NULL);
}

static void
pidgin_application_add_chat(G_GNUC_UNUSED GSimpleAction *simple,
                            G_GNUC_UNUSED GVariant *parameter,
                            G_GNUC_UNUSED gpointer data)
{
	purple_blist_request_add_chat(NULL, NULL, NULL, NULL);
}

static void
pidgin_application_add_group(G_GNUC_UNUSED GSimpleAction *simple,
                             G_GNUC_UNUSED GVariant *parameter,
                             G_GNUC_UNUSED gpointer data)
{
	purple_blist_request_add_group();
}

static void
pidgin_application_connect_account(G_GNUC_UNUSED GSimpleAction *simple,
                                   GVariant *parameter,
                                   G_GNUC_UNUSED gpointer data)
{
	PurpleAccount *account = NULL;
	PurpleAccountManager *manager = NULL;
	const gchar *id = NULL;

	id = g_variant_get_string(parameter, NULL);

	manager = purple_account_manager_get_default();

	account = purple_account_manager_find_by_id(manager, id);
	if(PURPLE_IS_ACCOUNT(account)) {
		purple_account_connect(account);
	}
}

static void
pidgin_application_debug(G_GNUC_UNUSED GSimpleAction *simple,
                         G_GNUC_UNUSED GVariant *parameter,
                         G_GNUC_UNUSED gpointer data)
{
	gboolean old = purple_prefs_get_bool(PIDGIN_PREFS_ROOT "/debug/enabled");
	purple_prefs_set_bool(PIDGIN_PREFS_ROOT "/debug/enabled", !old);
}


static void
pidgin_application_disable_account(G_GNUC_UNUSED GSimpleAction *simple,
                                   GVariant *parameter,
                                   G_GNUC_UNUSED gpointer data)
{
	PurpleAccount *account = NULL;
	PurpleAccountManager *manager = NULL;
	const gchar *id = NULL;

	id = g_variant_get_string(parameter, NULL);

	manager = purple_account_manager_get_default();

	account = purple_account_manager_find_by_id(manager, id);
	if(PURPLE_IS_ACCOUNT(account)) {
		if(purple_account_get_enabled(account)) {
			purple_account_set_enabled(account, FALSE);
		}
	}
}

static void
pidgin_application_donate(G_GNUC_UNUSED GSimpleAction *simple,
                          G_GNUC_UNUSED GVariant *parameter,
                          G_GNUC_UNUSED gpointer data)
{
	purple_notify_uri(NULL, "https://www.imfreedom.org/donate/");
}

static void
pidgin_application_edit_account(G_GNUC_UNUSED GSimpleAction *simple,
                                GVariant *parameter, gpointer data)
{
	PidginApplication *application = data;
	PurpleAccount *account = NULL;
	PurpleAccountManager *manager = NULL;
	const gchar *id = NULL;

	if(!g_variant_is_of_type(parameter, G_VARIANT_TYPE_STRING)) {
		g_warning("parameter is of type %s, expected %s",
		          g_variant_get_type_string(parameter),
		          (char *)G_VARIANT_TYPE_STRING);

		return;
	}

	id = g_variant_get_string(parameter, NULL);

	manager = purple_account_manager_get_default();

	account = purple_account_manager_find_by_id(manager, id);
	if(PURPLE_IS_ACCOUNT(account)) {
		GtkWidget *account_manager = pidgin_application_get_account_manager();

		pidgin_account_manager_edit_account(PIDGIN_ACCOUNT_MANAGER(account_manager),
		                                    account);

		pidgin_application_present_transient_window(application,
		                                            GTK_WINDOW(account_manager));
	}
}

static void
pidgin_application_enable_account(G_GNUC_UNUSED GSimpleAction *simple,
                                  GVariant *parameter,
                                  G_GNUC_UNUSED gpointer data)
{
	PurpleAccount *account = NULL;
	PurpleAccountManager *manager = NULL;
	const gchar *id = NULL;

	id = g_variant_get_string(parameter, NULL);

	manager = purple_account_manager_get_default();

	account = purple_account_manager_find_by_id(manager, id);
	if(PURPLE_IS_ACCOUNT(account)) {
		if(!purple_account_get_enabled(account)) {
			purple_account_set_enabled(account, TRUE);
		}
	}
}

static void
pidgin_application_file_transfers(G_GNUC_UNUSED GSimpleAction *simple,
                                  G_GNUC_UNUSED GVariant *parameter,
                                  G_GNUC_UNUSED gpointer data)
{
	pidgin_xfer_dialog_show(NULL);
}

static void
pidgin_application_get_user_info(G_GNUC_UNUSED GSimpleAction *simple,
                                 G_GNUC_UNUSED GVariant *parameter,
                                 G_GNUC_UNUSED gpointer data)
{
	pidgin_dialogs_info();
}

static void
pidgin_application_join_chat(G_GNUC_UNUSED GSimpleAction *simple,
                             G_GNUC_UNUSED GVariant *parameter,
                             G_GNUC_UNUSED gpointer data)
{
	pidgin_blist_joinchat_show();
}

static void
pidgin_application_new_message(G_GNUC_UNUSED GSimpleAction *simple,
                               G_GNUC_UNUSED GVariant *parameter,
                               G_GNUC_UNUSED gpointer data)
{
	pidgin_dialogs_im();
}

static void
pidgin_application_new_status(G_GNUC_UNUSED GSimpleAction *simple,
                              G_GNUC_UNUSED GVariant *parameter,
                              G_GNUC_UNUSED gpointer data)
{
	GtkWidget *editor = pidgin_status_editor_new(NULL);
	gtk_window_present_with_time(GTK_WINDOW(editor), GDK_CURRENT_TIME);
}

static void
pidgin_application_online_help(G_GNUC_UNUSED GSimpleAction *simple,
                               G_GNUC_UNUSED GVariant *parameter,
                               G_GNUC_UNUSED gpointer data)
{
	purple_notify_uri(NULL, PURPLE_WEBSITE "help");
}

static void
pidgin_application_plugins(G_GNUC_UNUSED GSimpleAction *simple,
                           G_GNUC_UNUSED GVariant *parameter, gpointer data)
{
	PidginApplication *application = data;
	static GtkWidget *dialog = NULL;

	if(!GTK_IS_WIDGET(dialog)) {
		dialog = pidgin_plugins_dialog_new();
		g_object_add_weak_pointer(G_OBJECT(dialog), (gpointer)&dialog);
	}

	pidgin_application_present_transient_window(application,
	                                            GTK_WINDOW(dialog));
}

static void
pidgin_application_preferences(G_GNUC_UNUSED GSimpleAction *simple,
                               G_GNUC_UNUSED GVariant *parameter,
                               gpointer data)
{
	PidginApplication *application = data;
	static GtkWidget *preferences = NULL;

	if(!GTK_IS_WIDGET(preferences)) {
		preferences = g_object_new(PIDGIN_TYPE_PREFS_WINDOW, NULL);
		g_object_add_weak_pointer(G_OBJECT(preferences), (gpointer)&preferences);
	}

	pidgin_application_present_transient_window(application,
	                                            GTK_WINDOW(preferences));

}

static void
pidgin_application_quit(G_GNUC_UNUSED GSimpleAction *simple,
                        G_GNUC_UNUSED GVariant *parameter,
                        G_GNUC_UNUSED gpointer data)
{
	GPluginManager *manager = NULL;

	/* Remove the signal handlers for plugin state changing so we don't try to
	 * update preferences.
	 */
	manager = gplugin_manager_get_default();
	g_signal_handlers_disconnect_by_func(manager,
	                                     pidgin_application_plugin_state_changed,
	                                     NULL);

	purple_core_quit();
}

static void
pidgin_application_room_list(G_GNUC_UNUSED GSimpleAction *simple,
                             G_GNUC_UNUSED GVariant *parameter,
                             G_GNUC_UNUSED gpointer data)
{
	pidgin_roomlist_dialog_show();
}

static void
pidgin_application_set_mood(G_GNUC_UNUSED GSimpleAction *simple,
                            G_GNUC_UNUSED GVariant *parameter,
                            G_GNUC_UNUSED gpointer data)
{
	pidgin_mood_dialog_show(NULL);
}

static void
pidgin_application_show_status_manager(G_GNUC_UNUSED GSimpleAction *simple,
                                       G_GNUC_UNUSED GVariant *parameter,
                                       gpointer data)
{
	PidginApplication *application = data;
	static GtkWidget *manager = NULL;

	if(!GTK_IS_WIDGET(manager)) {
		manager = pidgin_status_manager_new();
		g_object_add_weak_pointer(G_OBJECT(manager), (gpointer)&manager);
	}

	pidgin_application_present_transient_window(application,
	                                            GTK_WINDOW(manager));
}

static GActionEntry app_entries[] = {
	{
		.name = "about",
		.activate = pidgin_application_about,
	}, {
		.name = "add-buddy",
		.activate = pidgin_application_add_buddy,
	}, {
		.name = "add-chat",
		.activate = pidgin_application_add_chat,
	}, {
		.name = "add-group",
		.activate = pidgin_application_add_group,
	}, {
		.name = "connect-account",
		.activate = pidgin_application_connect_account,
		.parameter_type = "s",
	}, {
		.name = "debug",
		.activate = pidgin_application_debug,
	}, {
		.name = "disable-account",
		.activate = pidgin_application_disable_account,
		.parameter_type = "s",
	}, {
		.name = "donate",
		.activate = pidgin_application_donate,
	}, {
		.name = "edit-account",
		.activate = pidgin_application_edit_account,
		.parameter_type = "s",
	}, {
		.name = "enable-account",
		.activate = pidgin_application_enable_account,
		.parameter_type = "s",
	}, {
		.name = "file-transfers",
		.activate = pidgin_application_file_transfers,
	}, {
		.name = "get-user-info",
		.activate = pidgin_application_get_user_info,
	}, {
		.name = "join-chat",
		.activate = pidgin_application_join_chat,
	}, {
		.name = "manage-accounts",
		.activate = pidgin_application_accounts,
	}, {
		.name = "manage-plugins",
		.activate = pidgin_application_plugins,
	}, {
		.name = "new-message",
		.activate = pidgin_application_new_message,
	}, {
		.name = "new-status",
		.activate = pidgin_application_new_status,
	}, {
		.name = "online-help",
		.activate = pidgin_application_online_help,
	}, {
		.name = "preferences",
		.activate = pidgin_application_preferences,
	}, {
		.name = "quit",
		.activate = pidgin_application_quit,
	}, {
		.name = "room-list",
		.activate = pidgin_application_room_list,
	}, {
		.name = "set-mood",
		.activate = pidgin_application_set_mood,
	}, {
		.name = "status-manager",
		.activate = pidgin_application_show_status_manager,
	}
};

/******************************************************************************
 * Purple Signal Callbacks
 *****************************************************************************/
static void
pidgin_application_online_cb(gpointer data) {
	gint n_actions = G_N_ELEMENTS(pidgin_application_online_actions);

	pidgin_application_actions_set_enabled(PIDGIN_APPLICATION(data),
	                                       pidgin_application_online_actions,
	                                       n_actions,
	                                       TRUE);
}

static void
pidgin_application_offline_cb(gpointer data) {
	gint n_actions = G_N_ELEMENTS(pidgin_application_online_actions);

	pidgin_application_actions_set_enabled(PIDGIN_APPLICATION(data),
	                                       pidgin_application_online_actions,
	                                       n_actions,
	                                       FALSE);
}

static void
pidgin_application_signed_on_cb(PurpleAccount *account, gpointer data) {
	PidginApplication *application = PIDGIN_APPLICATION(data);
	PurpleProtocol *protocol = NULL;
	gboolean should_enable_chat = FALSE, should_enable_room_list = FALSE;
	gint n_actions = 0;

	protocol = purple_account_get_protocol(account);

	/* We assume that the current state is correct, so we don't bother changing
	 * state unless the newly connected account implements the chat interface,
	 * which would cause a state change.
	 */
	should_enable_chat = PURPLE_PROTOCOL_IMPLEMENTS(protocol, CHAT, info);
	if(should_enable_chat) {
		n_actions = G_N_ELEMENTS(pidgin_application_chat_actions);
		pidgin_application_actions_set_enabled(application,
		                                       pidgin_application_chat_actions,
		                                       n_actions,
		                                       TRUE);
	}

	/* likewise, for the room list, we only care about enabling in this
	 * handler.
	 */
	should_enable_room_list = PURPLE_PROTOCOL_IMPLEMENTS(protocol, ROOMLIST,
	                                                     get_list);
	if(should_enable_room_list) {
		n_actions = G_N_ELEMENTS(pidgin_application_room_list_actions);
		pidgin_application_actions_set_enabled(application,
		                                       pidgin_application_room_list_actions,
		                                       n_actions,
		                                       TRUE);
	}
}

static void
pidgin_application_signed_off_cb(G_GNUC_UNUSED PurpleAccount *account,
                                 gpointer data)
{
	PidginApplication *application = PIDGIN_APPLICATION(data);
	gboolean should_disable_chat = TRUE, should_disable_room_list = TRUE;
	GList *connections = NULL, *l = NULL;
	gint n_actions = 0;

	/* walk through all the connections, looking for online ones that implement
	 * the chat interface.  We don't bother checking the account that this
	 * signal was emitted for, because it's already offline and will be
	 * filtered out by the online check.
	 */
	connections = purple_connections_get_all();
	for(l = connections; l != NULL; l = l->next) {
		PurpleConnection *connection = PURPLE_CONNECTION(l->data);
		PurpleProtocol *protocol = NULL;

		/* if the connection isn't online, we don't care about it */
		if(!PURPLE_CONNECTION_IS_CONNECTED(connection)) {
			continue;
		}

		protocol = purple_connection_get_protocol(connection);

		/* check if the protocol implements the chat interface */
		if(PURPLE_PROTOCOL_IMPLEMENTS(protocol, CHAT, info)) {
			should_disable_chat = FALSE;
		}

		/* check if the protocol implements the room list interface */
		if(PURPLE_PROTOCOL_IMPLEMENTS(protocol, ROOMLIST, get_list)) {
			should_disable_room_list = FALSE;
		}

		/* if we can't disable both, we can bail out of the loop */
		if(!should_disable_chat && !should_disable_room_list) {
			break;
		}
	}

	if(should_disable_chat) {
		n_actions = G_N_ELEMENTS(pidgin_application_chat_actions);
		pidgin_application_actions_set_enabled(application,
		                                       pidgin_application_chat_actions,
		                                       n_actions,
		                                       FALSE);
	}

	if(should_disable_room_list) {
		n_actions = G_N_ELEMENTS(pidgin_application_room_list_actions);
		pidgin_application_actions_set_enabled(application,
		                                       pidgin_application_room_list_actions,
		                                       n_actions,
		                                       FALSE);
	}
}

static void
pidgin_application_error_reponse_cb(G_GNUC_UNUSED AdwMessageDialog *self,
                                    G_GNUC_UNUSED char *response,
                                    gpointer data)
{
	g_application_quit(data);
}

/******************************************************************************
 * GtkApplication Implementation
 *****************************************************************************/
static void
pidgin_application_window_added(GtkApplication *application,
                                GtkWindow *window)
{
	PidginApplication *pidgin_application = PIDGIN_APPLICATION(application);
	GHashTableIter iter;
	gpointer key, value;

	GTK_APPLICATION_CLASS(pidgin_application_parent_class)->window_added(application,
	                                                                     window);

	if(strstr(VERSION, "-devel")) {
		gtk_widget_add_css_class(GTK_WIDGET(window), "devel");
	}

	g_hash_table_iter_init(&iter, pidgin_application->action_groups);
	while(g_hash_table_iter_next(&iter, &key, &value)) {
		GActionGroup *action_group = value;
		gchar *prefix = key;

		gtk_widget_insert_action_group(GTK_WIDGET(window), prefix,
		                               action_group);
	}
}

/******************************************************************************
 * GApplication Implementation
 *****************************************************************************/
static void
pidgin_application_startup(GApplication *application) {
	PurpleAccountManager *manager = NULL;
	GError *error = NULL;
	GList *active_accounts = NULL;
	gpointer handle = NULL;

	G_APPLICATION_CLASS(pidgin_application_parent_class)->startup(application);

	adw_init();

	/* set a user-specified config directory */
	if (opt_config_dir_arg != NULL) {
		if (g_path_is_absolute(opt_config_dir_arg)) {
			purple_util_set_user_dir(opt_config_dir_arg);
		} else {
			/* Make an absolute (if not canonical) path */
			gchar *cwd = g_get_current_dir();
			gchar *path = g_build_filename(cwd, opt_config_dir_arg, NULL);

			purple_util_set_user_dir(path);

			g_free(cwd);
			g_free(path);
		}
	}

	pidgin_debug_init_handler();
#ifdef DEBUG
	pidgin_debug_set_print_enabled(TRUE);
#else
	pidgin_debug_set_print_enabled(opt_debug);
#endif

#ifdef _WIN32
	winpidgin_init();
#endif

	if(!purple_core_init(pidgin_ui_new(), &error)) {
		GtkWidget *message = NULL;
		const char *error_message = "unknown error";

		if(error != NULL) {
			error_message = error->message;
		}

		message = adw_message_dialog_new(NULL,
		                                 _("Pidgin 3 failed to initialize"),
		                                 error_message);
		g_clear_error(&error);

		adw_message_dialog_add_responses(ADW_MESSAGE_DIALOG(message),
		                                 "close", _("Close"), NULL);
		adw_message_dialog_set_close_response(ADW_MESSAGE_DIALOG(message),
		                                      "close");

		g_signal_connect(message, "response",
		                 G_CALLBACK(pidgin_application_error_reponse_cb),
		                 application);

		gtk_window_present_with_time(GTK_WINDOW(message), GDK_CURRENT_TIME);

		return;
	}

	pidgin_application_init_plugins();

	/* load plugins we had when we quit */
	purple_plugins_load_saved(PIDGIN_PREFS_ROOT "/plugins/loaded");

	gtk_window_set_default_icon_name("pidgin");

	g_free(opt_config_dir_arg);
	opt_config_dir_arg = NULL;

	/*
	 * We want to show the blist early in the init process so the
	 * user feels warm and fuzzy.
	 */
	purple_blist_show();

	if(purple_prefs_get_bool(PIDGIN_PREFS_ROOT "/debug/enabled")) {
		pidgin_debug_window_show();
	}

	if(opt_nologin) {
		/* Set all accounts to "offline" */
		PurpleSavedStatus *saved_status;

		/* If we've used this type+message before, lookup the transient status */
		saved_status = purple_savedstatus_find_transient_by_type_and_message(
							PURPLE_STATUS_OFFLINE, NULL);

		/* If this type+message is unique then create a new transient saved status */
		if(saved_status == NULL) {
			saved_status = purple_savedstatus_new(NULL, PURPLE_STATUS_OFFLINE);
		}

		/* Set the status for each account */
		purple_savedstatus_activate(saved_status);
	} else {
		/* Everything is good to go--sign on already */
		if (!purple_prefs_get_bool("/purple/savedstatus/startup_current_status")) {
			purple_savedstatus_activate(purple_savedstatus_get_startup());
		}

		purple_accounts_restore_current_statuses();
	}

	manager = purple_account_manager_get_default();
	active_accounts = purple_account_manager_get_enabled(manager);
	if(active_accounts == NULL) {
		g_action_group_activate_action(G_ACTION_GROUP(application),
		                               "manage-accounts", NULL);
	} else {
		g_list_free(active_accounts);
	}

	/* Populate our dynamic menus. */
	pidgin_application_populate_dynamic_menus(PIDGIN_APPLICATION(application));

	/* TODO: Use GtkApplicationWindow or add a window instead */
	g_application_hold(application);

	/* connect to the online and offline signals in purple connections.  This
	 * is used to toggle states of actions that require being online.
	 */
	handle = purple_connections_get_handle();
	purple_signal_connect(handle, "online", application,
	                      G_CALLBACK(pidgin_application_online_cb),
	                      application);
	purple_signal_connect(handle, "offline", application,
	                      G_CALLBACK(pidgin_application_offline_cb),
	                      application);

	/* connect to account-signed-on and account-signed-off to toggle actions
	 * that depend on specific interfaces in accounts.
	 */
	handle = purple_accounts_get_handle();
	purple_signal_connect(handle, "account-signed-on", application,
	                      G_CALLBACK(pidgin_application_signed_on_cb),
	                      application);
	purple_signal_connect(handle, "account-signed-off", application,
	                      G_CALLBACK(pidgin_application_signed_off_cb),
	                      application);

}

static void
pidgin_application_activate(G_GNUC_UNUSED GApplication *application) {
	GtkWidget *convwin = pidgin_display_window_get_default();

	if(GTK_IS_WINDOW(convwin)) {
		gtk_window_present(GTK_WINDOW(convwin));
	}
}

static gint
pidgin_application_command_line(GApplication *application,
                                GApplicationCommandLine *cmdline)
{
	gchar **argv = NULL;
	gint argc = 0, i = 0;

	argv = g_application_command_line_get_arguments(cmdline, &argc);

	if(argc == 1) {
		/* No arguments, just activate */
		g_application_activate(application);
	}

	/* Start at 1 to skip the executable name */
	for (i = 1; i < argc; i++) {
		purple_got_protocol_handler_uri(argv[i]);
	}

	g_strfreev(argv);

	return 0;
}

static gint
pidgin_application_handle_local_options(G_GNUC_UNUSED GApplication *application,
                                        GVariantDict *options)
{
	if (g_variant_dict_contains(options, "version")) {
		printf("%s %s (libpurple %s)\n", PIDGIN_NAME, DISPLAY_VERSION,
		       purple_core_get_version());

		return 0;
	}

	return -1;
}

/******************************************************************************
 * GObject Implementation
 *****************************************************************************/
static void
pidgin_application_dispose(GObject *obj) {
	PidginApplication *application = PIDGIN_APPLICATION(obj);

	g_clear_pointer(&application->action_groups, g_hash_table_destroy);

	G_OBJECT_CLASS(pidgin_application_parent_class)->dispose(obj);
}

static void
pidgin_application_init(PidginApplication *application) {
	GApplication *gapp = G_APPLICATION(application);
	gboolean online = FALSE;
	gint n_actions = 0;

	application->action_groups = g_hash_table_new_full(g_str_hash, g_str_equal,
	                                                   g_free, g_object_unref);

	g_application_add_main_option_entries(gapp, option_entries);
	g_application_add_option_group(gapp, purple_get_option_group());
	g_application_add_option_group(gapp, gplugin_get_option_group());

	g_action_map_add_action_entries(G_ACTION_MAP(application), app_entries,
	                                G_N_ELEMENTS(app_entries), application);

	/* Set the default state for our actions to match our online state. */
	online = purple_connections_is_online();

	n_actions = G_N_ELEMENTS(pidgin_application_online_actions);
	pidgin_application_actions_set_enabled(application,
	                                       pidgin_application_online_actions,
	                                       n_actions,
	                                       online);

	n_actions = G_N_ELEMENTS(pidgin_application_chat_actions);
	pidgin_application_actions_set_enabled(application,
	                                       pidgin_application_chat_actions,
	                                       n_actions,
	                                       online);

	n_actions = G_N_ELEMENTS(pidgin_application_room_list_actions);
	pidgin_application_actions_set_enabled(application,
	                                       pidgin_application_room_list_actions,
	                                       n_actions,
	                                       online);
}

static void
pidgin_application_class_init(PidginApplicationClass *klass) {
	GObjectClass *obj_class = G_OBJECT_CLASS(klass);
	GApplicationClass *app_class = G_APPLICATION_CLASS(klass);
	GtkApplicationClass *gtk_app_class = GTK_APPLICATION_CLASS(klass);

	obj_class->dispose = pidgin_application_dispose;

	app_class->startup = pidgin_application_startup;
	app_class->activate = pidgin_application_activate;
	app_class->command_line = pidgin_application_command_line;
	app_class->handle_local_options = pidgin_application_handle_local_options;

	gtk_app_class->window_added = pidgin_application_window_added;
}

/******************************************************************************
 * Public API
 *****************************************************************************/
GApplication *
pidgin_application_new(void) {
	return g_object_new(
		PIDGIN_TYPE_APPLICATION,
		"application-id", "im.pidgin.Pidgin3",
		"flags", G_APPLICATION_CAN_OVERRIDE_APP_ID |
		         G_APPLICATION_HANDLES_COMMAND_LINE,
		"register-session", TRUE,
		NULL);
}

void
pidgin_application_add_action_group(PidginApplication *application,
                                    const gchar *prefix,
                                    GActionGroup *action_group)
{
	GList *windows = NULL;

	g_return_if_fail(prefix != NULL);

	if(G_IS_ACTION_GROUP(action_group)) {
		/* If action_group is valid, we need to create new references to the
		 * prefix and action_group when inserting them into the hash table.
		 */
		g_hash_table_insert(application->action_groups, g_strdup(prefix),
		                    g_object_ref(action_group));
	} else {
		g_hash_table_remove(application->action_groups, prefix);
	}

	/* Now walk through the windows and add/remove the action group. */
	windows = gtk_application_get_windows(GTK_APPLICATION(application));
	while(windows != NULL) {
		GtkWidget *window = GTK_WIDGET(windows->data);

		gtk_widget_insert_action_group(window, prefix, action_group);

		windows = windows->next;
	}
}

GtkWindow *
pidgin_application_get_active_window(PidginApplication *application) {
	GtkApplication *gtk_application = NULL;
	GtkWindow *window = NULL;

	g_return_val_if_fail(PIDGIN_IS_APPLICATION(application), NULL);

	gtk_application = GTK_APPLICATION(application);

	window = gtk_application_get_active_window(gtk_application);
	if(!GTK_IS_WINDOW(window)) {
		GList *windows = NULL;

		windows = gtk_application_get_windows(gtk_application);
		if(windows != NULL) {
			window = windows->data;
		}
	}

	return window;
}