summaryrefslogtreecommitdiff
path: root/src/capplet/mate-notification-applet.c
blob: ec10acccaeb8e3efaa266f02561e363a13589d47 (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
/* mate-notification-applet.c - MATE Notification Applet
 *
 * Copyright (c) 2020 Robert Buj <robert.buj@gmail.com>
 *
 * 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, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301  USA
 */

#include "config.h"

#include <glib.h>
#include <glib/gi18n.h>
#include <gtk/gtk.h>
#include <mate-panel-applet.h>

#define MATE_DESKTOP_USE_UNSTABLE_API
#include <libmate-desktop/mate-desktop-utils.h>

#include "constants.h"

typedef struct
{
  MatePanelApplet *applet;

  GtkWidget       *image_on;
  GtkWidget       *image_off;
  GtkActionGroup  *action_group;
  GSettings       *settings;
} MateNotificationApplet;

static void
show_about      (GtkAction              *action,
                 MateNotificationApplet *applet);
static void
call_properties (GtkAction              *action,
                 MateNotificationApplet *applet);

static const GtkActionEntry applet_menu_actions [] = {
  { "Preferences", "document-properties", N_("_Preferences"),
    NULL, NULL, G_CALLBACK (call_properties) },
  { "About", "help-about", N_("_About"),
    NULL, NULL, G_CALLBACK (show_about) }
};

static const char* notification_properties[] = {
  "mate-notification-properties",
};

static void
applet_destroy (MatePanelApplet *applet_widget,
                MateNotificationApplet *applet)
{
  g_assert (applet);

  g_object_unref (applet->settings);
  g_object_unref (applet->action_group);
  g_free (applet);
}

static void
call_properties (GtkAction              *action,
                 MateNotificationApplet *applet)
{
  gsize i;
  (void) action;

  for (i = 0; i < G_N_ELEMENTS (notification_properties); i++) {
    char *programpath = g_find_program_in_path (notification_properties[i]);

    if (programpath != NULL) {
      g_free (programpath);
      mate_gdk_spawn_command_line_on_screen (gtk_widget_get_screen (GTK_WIDGET (applet->applet)),
                                             notification_properties[i],
                                             NULL);
      return;
    }
  }
}

static void
show_about (GtkAction              *action,
            MateNotificationApplet *applet)
{
  static const char *authors[] = {
    "Robert Buj <robert.buj@gmail.com>",
    NULL
  };
  (void) action;
  (void) applet;

  gtk_show_about_dialog (NULL,
                         "title", _("About Do Not Disturb"),
                         "version", VERSION,
                         "copyright", _("Copyright \xc2\xa9 2021 MATE developers"),
                         "comments", _("Activate the do not disturb mode quickly."),
                         "authors", authors,
                         "translator-credits", _("translator-credits"),
                         "logo_icon_name", "mate-notification-properties",
                         NULL);
}

static void
set_status_image (MateNotificationApplet *applet,
                  gboolean                active)
{
  gtk_widget_show_all (GTK_WIDGET (applet->applet));
  if (active)
    gtk_widget_hide (applet->image_on);
  else
    gtk_widget_hide (applet->image_off);
}

static void
settings_changed (GSettings              *settings,
                  gchar                  *key,
                  MateNotificationApplet *applet)
{
  if (g_strcmp0 (GSETTINGS_KEY_DO_NOT_DISTURB, key) == 0)
    set_status_image (applet,
                      g_settings_get_boolean (settings, key));
}

static void
applet_draw_icon (MatePanelApplet *applet_widget,
                  int arg1,
                  MateNotificationApplet *applet)
{
  gint size, scale;

  g_assert (applet);

  size = (gint) mate_panel_applet_get_size (applet_widget);
  scale = gtk_widget_get_scale_factor (GTK_WIDGET (applet_widget));

  cairo_surface_t *image_on = gtk_icon_theme_load_surface (gtk_icon_theme_get_default (),
                                                           "user-available",
                                                           size, scale,
                                                           NULL, 0, NULL);
  cairo_surface_t *image_off = gtk_icon_theme_load_surface (gtk_icon_theme_get_default (),
                                                            "user-invisible",
                                                            size, scale,
                                                            NULL, 0, NULL);

  gtk_image_set_from_surface (GTK_IMAGE (applet->image_on), image_on);
  gtk_image_set_from_surface (GTK_IMAGE (applet->image_off), image_off);

  cairo_surface_destroy (image_on);
  cairo_surface_destroy (image_off);
}

static MateNotificationApplet*
applet_main (MatePanelApplet *applet_widget)
{
  MateNotificationApplet *applet;
  GtkWidget *box;

#ifdef ENABLE_NLS
  bindtextdomain (GETTEXT_PACKAGE, MATELOCALEDIR);
  bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
  textdomain (GETTEXT_PACKAGE);
#endif /* ENABLE_NLS */

  g_set_application_name (_("Do Not Disturb"));
  gtk_window_set_default_icon_name ("mate-notification-properties");

  applet = g_new (MateNotificationApplet, 1);
  applet->applet = applet_widget;
  applet->settings = g_settings_new (GSETTINGS_SCHEMA);

  /* needed to clamp ourselves to the panel size */
  mate_panel_applet_set_flags (MATE_PANEL_APPLET (applet), MATE_PANEL_APPLET_EXPAND_MINOR);

  box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);

  applet->image_on  = gtk_image_new ();
  applet->image_off = gtk_image_new ();

  applet_draw_icon (applet_widget, 0, applet);

  gtk_widget_set_tooltip_text (applet->image_off, N_("Do Not Disturb"));
  gtk_widget_set_tooltip_text (applet->image_on, N_("Notifications Enabled"));

  gtk_box_pack_start (GTK_BOX (box), applet->image_on,
                      TRUE, TRUE, 0);
  gtk_box_pack_start (GTK_BOX (box), applet->image_off,
                      TRUE, TRUE, 0);
  gtk_container_add (GTK_CONTAINER (applet_widget), box);

  set_status_image (applet,
                    g_settings_get_boolean (applet->settings,
                                            GSETTINGS_KEY_DO_NOT_DISTURB));

  /* set up context menu */
  applet->action_group = gtk_action_group_new ("Do Not Disturb Actions");
#ifdef ENABLE_NLS
  gtk_action_group_set_translation_domain (applet->action_group, GETTEXT_PACKAGE);
#endif /* ENABLE_NLS */
  gtk_action_group_add_actions (applet->action_group, applet_menu_actions,
                                G_N_ELEMENTS (applet_menu_actions), applet);

  GtkToggleAction *do_not_disturb_toggle_action =
    gtk_toggle_action_new ("DoNotDisturb", N_("_Do not disturb"),
                           N_("Enable/Disable do-not-disturb mode."), NULL);

  gtk_action_group_add_action (applet->action_group,
                               GTK_ACTION (do_not_disturb_toggle_action));

  mate_panel_applet_setup_menu_from_resource  (applet->applet,
                                               RESOURCE_PATH "menu.xml",
                                               applet->action_group);

  g_settings_bind (applet->settings, "do-not-disturb",
                   do_not_disturb_toggle_action, "active",
                   G_SETTINGS_BIND_DEFAULT);

  g_signal_connect (G_OBJECT (applet->applet), "destroy",
                    G_CALLBACK (applet_destroy), applet);

  /* GSettings callback */
  g_signal_connect (G_OBJECT (applet->settings), "changed::" GSETTINGS_KEY_DO_NOT_DISTURB,
                    G_CALLBACK (settings_changed), applet);

  return applet;
}

static gboolean
applet_factory (MatePanelApplet *applet_widget,
                const gchar     *iid,
                gpointer         data)
{
  MateNotificationApplet *applet;
  (void) data;

  if (!strcmp (iid, "MateNotificationApplet")) {
    applet = applet_main (applet_widget);

    g_signal_connect (G_OBJECT (applet_widget), "change_size",
                      G_CALLBACK (applet_draw_icon),
                      (gpointer) applet);

    return TRUE;
  }

  return FALSE;
}

MATE_PANEL_APPLET_OUT_PROCESS_FACTORY ("MateNotificationAppletFactory",
                                       PANEL_TYPE_APPLET,
                                       "Do Not Disturb Applet",
                                       applet_factory,
                                       NULL)