forked from zevlg/telega.el
-
Notifications
You must be signed in to change notification settings - Fork 0
/
telega-appindicator.c
156 lines (139 loc) · 4.95 KB
/
telega-appindicator.c
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
/*
* telega-appindicator.c --- Bridge between Emacs and TDLib.
*
* Copyright (C) 2016-2020 by Zajcev Evgeny
*
* Author: Zajcev Evgeny <zevlg@yandex.ru>
*
* telega 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 3 of the License, or
* (at your option) any later version.
*
* telega 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 telega. If not, see <http://www.gnu.org/licenses/>.
*
** Commentary:
*
* Support for telega icon in System Tray using libappindicator.
*
* Adds "appindicator" command:
* - appindicator setup <svg-icon:str>"
* - appindicator status <active|passive>"
* - appindicator label <label:str>"
*
* Can send "appindicator-event" when menu buttons are clicked.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
#ifdef WITH_AYATANA_APPINDICATOR
#include <libayatana-appindicator/app-indicator.h>
#else
#include <libappindicator/app-indicator.h>
#endif
extern void telega_output_json(const char* otype, const char* json);
static GMainLoop* loop = NULL;
static GtkWidget* menu = NULL;
static AppIndicator* appind = NULL;
static void
appindicator_click_cb(GtkWidget *widget, gpointer data)
{
const gchar* text = (const gchar*)data;
if (text)
telega_output_json("appindicator-event", text);
}
static void
appindicator_setup(char* icon_path)
{
if (!appind) {
appind = app_indicator_new("telega", icon_path,
APP_INDICATOR_CATEGORY_COMMUNICATIONS);
g_assert(IS_APP_INDICATOR(appind));
app_indicator_set_status(appind, APP_INDICATOR_STATUS_ACTIVE);
menu = gtk_menu_new();
GtkWidget* item = gtk_menu_item_new_with_label("Open telega");
g_signal_connect(item, "activate",
G_CALLBACK(appindicator_click_cb), "\"open\"");
gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
gtk_widget_show(item);
item = gtk_separator_menu_item_new();
gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
gtk_widget_show(item);
item = gtk_menu_item_new_with_label("Quit telega");
g_signal_connect(item, "activate",
G_CALLBACK(appindicator_click_cb), "\"quit\"");
gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
gtk_widget_show(item);
app_indicator_set_menu(appind, GTK_MENU(menu));
} else {
app_indicator_set_status(appind, APP_INDICATOR_STATUS_ACTIVE);
app_indicator_set_icon_full(appind, icon_path, "telega icon");
app_indicator_set_label(appind, "", "");
}
}
static int
appindicator_cmd(void* data)
{
char* cmd = (char*)data;
char* cmd_args = strchr(cmd, ' ');
if (cmd_args)
cmd_args += 1;
if (cmd_args == NULL) {
/* All commands requires args */
fprintf(stderr, "[telega-appindicator]: Invalid cmd -> %s\n",
cmd);
} else if (!strncmp(cmd, "setup ", 6)) {
/* setup <icon-path:str> */
appindicator_setup(cmd_args);
} else if (!strncmp(cmd, "status ", 7)) {
/* status <active|passive> */
if (appind) {
AppIndicatorStatus status = APP_INDICATOR_STATUS_PASSIVE;
if (!strcmp(cmd_args, "active"))
status = APP_INDICATOR_STATUS_ACTIVE;
app_indicator_set_status(appind, status);
}
} else if (!strncmp(cmd, "label ", 6)) {
/* label <label:str> */
if (appind)
app_indicator_set_label(appind, cmd_args, cmd_args);
} else if (!strncmp(cmd, "icon ", 5)) {
app_indicator_set_icon_full(appind, cmd_args, "telega icon");
} else {
fprintf(stderr, "[telega-appindicator]: unknown cmd -> %s\n",
cmd);
}
free(data);
return 0;
}
void
telega_appindicator_send(const char* json)
{
char* json_copy = strdup(json);
g_idle_add(appindicator_cmd, json_copy);
}
bool
telega_appindicator_init(void)
{
return gtk_init_check(NULL, NULL);
}
void*
telega_appindicator_loop(void* data)
{
loop = g_main_loop_new(NULL, FALSE);
g_main_loop_run(loop);
return NULL;
}
void
telega_appindicator_stop(void)
{
if (loop)
g_main_loop_quit(loop);
}