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
/*
 * Copyright (C) 2002 Anders Carlsson <andersca@gnu.org>
 * Copyright (C) 2003-2006 Vincent Untz
 * Copyright (C) 2006, 2007 Christian Persch
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "config.h"

#include <string.h>
#include <stdio.h>
#include <signal.h>
#include <glib-unix.h>
#include <ctk/ctk.h>
#include "system-tray/na-tray-manager.h"
#ifdef PROVIDE_WATCHER_SERVICE
# include "libstatus-notifier-watcher/gf-status-notifier-watcher.h"
#endif
#include "na-grid.h"

#define NOTIFICATION_AREA_ICON "cafe-panel-notification-area"

static guint n_windows = 0;

typedef struct
{
  CdkScreen *screen;
  guint screen_num;
  CtkWidget *window;
  CtkWidget *traybox;
  CtkLabel *count_label;
} TrayData;

static void
do_add (CtkWidget *child, guint *n_children)
{
  *n_children += 1;
}

static void
update_child_count (TrayData *data)
{
  guint n_children = 0;
  char text[64];

  if (!ctk_widget_get_realized (data->window))
    return;

  ctk_container_foreach (CTK_CONTAINER (data->traybox), (CtkCallback) do_add, &n_children);

  g_snprintf (text, sizeof (text), "%u icons", n_children);
  ctk_label_set_text (data->count_label, text);
}

static void
tray_added_cb (CtkContainer *box, CtkWidget *icon, TrayData *data)
{
  g_print ("[Screen %u tray %p] Child %p added to tray: \"%s\"\n",
	   data->screen_num, data->traybox, icon, "XXX");//na_tray_child_get_title (icon));

  update_child_count (data);
}

static void
tray_removed_cb (CtkContainer *box, CtkWidget *icon, TrayData *data)
{
  g_print ("[Screen %u tray %p] Child %p removed from tray\n",
	   data->screen_num, data->traybox, icon);

  update_child_count (data);
}

static void orientation_changed_cb (CtkComboBox *combo, TrayData *data)
{
  CtkOrientation orientation = (CtkOrientation) ctk_combo_box_get_active (combo);

  g_print ("[Screen %u tray %p] Setting orientation to \"%s\"\n",
	   data->screen_num, data->traybox, orientation == 0 ? "horizontal" : "vertical");

  ctk_orientable_set_orientation (CTK_ORIENTABLE (data->traybox), orientation);
}

static void
maybe_quit (gpointer data,
	    GObject *zombie)<--- Parameter 'zombie' can be declared as pointer to const
{
  if (--n_windows == 0) {
    ctk_main_quit ();
  }
}

static TrayData *create_tray_on_screen (CdkScreen *screen, gboolean force);

static void
warning_dialog_response_cb (CtkWidget *dialog,
			    gint response,
			    CdkScreen *screen)
{
  if (response == CTK_RESPONSE_YES) {
    create_tray_on_screen (screen, TRUE);
  }

  ctk_widget_destroy (dialog);
}

static void
add_tray_cb (CtkWidget *button, TrayData *data)
{
  create_tray_on_screen (data->screen, TRUE);
}

static TrayData *
create_tray_on_screen (CdkScreen *screen,
		       gboolean force)
{
  CtkWidget *window, *hbox, *vbox, *button, *combo, *label;
  TrayData *data;

  n_windows++;

  if (!force && na_tray_manager_check_running (screen)) {
    CtkWidget *dialog;

    dialog = ctk_message_dialog_new (NULL, 0, CTK_MESSAGE_WARNING, CTK_BUTTONS_YES_NO,
				     "Override tray manager?");
    ctk_message_dialog_format_secondary_text (CTK_MESSAGE_DIALOG (dialog),
					     "There is already a tray manager running on screen %d.",
					     cdk_x11_screen_get_screen_number (screen));
    ctk_window_set_screen (CTK_WINDOW (dialog), screen);
    g_signal_connect (dialog, "response", G_CALLBACK (warning_dialog_response_cb), screen);
    ctk_window_present (CTK_WINDOW (dialog));
    g_object_weak_ref (G_OBJECT (dialog), (GWeakNotify) maybe_quit, NULL);
    return NULL;
  }

  data = g_new0 (TrayData, 1);
  data->screen = screen;
  data->screen_num = cdk_x11_screen_get_screen_number (screen);

  data->window = window = ctk_window_new (CTK_WINDOW_TOPLEVEL);
  g_object_weak_ref (G_OBJECT (window), (GWeakNotify) maybe_quit, NULL);<--- You might need to cast the function pointer here

  vbox = ctk_box_new (CTK_ORIENTATION_VERTICAL, 6);
  ctk_container_add (CTK_CONTAINER (window), vbox);

  button = ctk_button_new_with_mnemonic ("_Add another tray");
  g_signal_connect (button, "clicked", G_CALLBACK (add_tray_cb), data);
  ctk_box_pack_start (CTK_BOX (vbox), button, FALSE, FALSE, 0);

  hbox = ctk_box_new (CTK_ORIENTATION_HORIZONTAL, 12);
  ctk_box_pack_start (CTK_BOX (vbox), hbox, FALSE, FALSE, 0);
  label = ctk_label_new_with_mnemonic ("_Orientation:");
  ctk_label_set_xalign (CTK_LABEL (label), 0.0);
  ctk_label_set_yalign (CTK_LABEL (label), 0.5);
  ctk_box_pack_start (CTK_BOX (hbox), label, FALSE, FALSE, 0);

  combo = ctk_combo_box_text_new ();
  ctk_combo_box_text_append_text (CTK_COMBO_BOX_TEXT (combo), "Horizontal");
  ctk_combo_box_text_append_text (CTK_COMBO_BOX_TEXT (combo), "Vertical");
  g_signal_connect (combo, "changed",
		    G_CALLBACK (orientation_changed_cb), data);
  ctk_box_pack_start (CTK_BOX (hbox), combo, FALSE, FALSE, 0);

  label = ctk_label_new (NULL);
  data->count_label = CTK_LABEL (label);
  ctk_label_set_xalign (CTK_LABEL (label), 0.0);
  ctk_label_set_yalign (CTK_LABEL (label), 0.5);
  ctk_box_pack_start (CTK_BOX (vbox), label, FALSE, FALSE, 0);

  data->traybox = na_grid_new (CTK_ORIENTATION_HORIZONTAL);
  ctk_box_pack_start (CTK_BOX (vbox), CTK_WIDGET (data->traybox), TRUE, TRUE, 0);

  g_signal_connect_after (data->traybox, "add", G_CALLBACK (tray_added_cb), data);
  g_signal_connect_after (data->traybox, "remove", G_CALLBACK (tray_removed_cb), data);

  ctk_combo_box_set_active (CTK_COMBO_BOX (combo), 0);

  ctk_window_set_screen (CTK_WINDOW (window), screen);
  ctk_window_set_default_size (CTK_WINDOW (window), -1, 200);

  /* ctk_window_set_resizable (CTK_WINDOW (window), FALSE); */

  ctk_widget_show_all (window);

  update_child_count (data);

  return data;
}

static gboolean
signal_handler (gpointer data G_GNUC_UNUSED)
{
  ctk_main_quit ();

  return FALSE;
}

#ifdef PROVIDE_WATCHER_SERVICE
static GfStatusNotifierWatcher *
status_notifier_watcher_maybe_new (void)
{
  GDBusProxy *proxy;
  GError *error = NULL;
  GfStatusNotifierWatcher *service = NULL;

  /* check if the service already exists
   * FIXME: is that a not-too-stupid way of doing it?  bah, so long as it works.
   *        it's for testing purposes only anyway. */
  proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
                                         G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS |
                                         G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES,
                                         NULL,
                                         "org.kde.StatusNotifierWatcher",
                                         "/StatusNotifierWatcher",
                                         "org.kde.StatusNotifierWatcher",
                                         NULL, &error);
  if (proxy)
    g_object_unref (proxy);
  else
    {
      g_warning ("Failed to connect to org.kde.StatusNotifierWatcher (%s), starting our own.",
                 error->message);
      g_clear_error (&error);

      service = gf_status_notifier_watcher_new ();
    }

  return service;
}
#endif

int
main (int argc, char *argv[])
{
  CdkDisplay *display;
  CdkScreen *screen;
#ifdef PROVIDE_WATCHER_SERVICE
  GfStatusNotifierWatcher *service;
#endif

  ctk_init (&argc, &argv);

  g_unix_signal_add (SIGTERM, signal_handler, NULL);
  g_unix_signal_add (SIGINT, signal_handler, NULL);

#ifdef PROVIDE_WATCHER_SERVICE
  service = status_notifier_watcher_maybe_new ();
#endif

  ctk_window_set_default_icon_name (NOTIFICATION_AREA_ICON);

  display = cdk_display_get_default ();
  screen = cdk_display_get_default_screen (display);

  create_tray_on_screen (screen, FALSE);

  ctk_main ();

#ifdef PROVIDE_WATCHER_SERVICE
  if (service)
    g_object_unref (service);
#endif

  return 0;
}