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
|
/*
* Copyright 2015 Javier S. Pedro <dev.git@javispedro.com>
*
* This file is part of MDock.
*
* MDock 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.
*
* MDock 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 General Public License
* along with MDock. If not, see <http://www.gnu.org/licenses/>.
*/
#include <gtk/gtk.h>
#define WNCK_I_KNOW_THIS_IS_UNSTABLE 1
#include <libwnck/libwnck.h>
#include "../config.h"
#include "mdock-widget.h"
#include "mdock-item-window-selector.h"
#include "mdock-item-menu.h"
#include "mdock-item.h"
#include "matcher.h"
#ifdef HAVE_ZEITGEIST
#include <zeitgeist.h>
#endif
#define POPUP_SHOW_TIMEOUT 200
#define POPUP_HIDE_TIMEOUT 500
struct _MDockWidgetPrivate
{
gchar *settings_path;
GSettings *settings;
WnckScreen *wnck_screen;
GSequence *items;
GHashTable *appid_to_item;
GHashTable *desktopid_to_item;
GHashTable *window_to_item;
MDockItem *current_pointed_item;
MDockItem *current_popup_item;
guint popup_timer;
gboolean loading_settings : 1;
gboolean just_dropped : 1;
};
G_DEFINE_TYPE(MDockWidget, mdock_widget, GTK_TYPE_BOX)
#define MDOCK_WIDGET_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), MDOCK_TYPE_WIDGET, MDockWidgetPrivate))
enum {
PROP_0,
PROP_SETTINGS_PATH,
N_PROPERTIES
};
static GParamSpec *obj_properties[N_PROPERTIES] = { NULL };
G_DEFINE_QUARK(mdock-widget-item-iter, mdock_widget_item_iter)
G_DEFINE_QUARK(mdock-widget-item-menu, mdock_widget_item_menu)
G_DEFINE_QUARK(mdock-widget-item-window-selector, mdock_widget_item_window_selector)
enum {
DRAG_TYPE_ITEM,
DRAG_TYPE_URILIST
};
static const GtkTargetEntry drag_types[] = {
"application/x-mdock-item", GTK_TARGET_SAME_APP, DRAG_TYPE_ITEM,
"text/uri-list", 0, DRAG_TYPE_URILIST
};
static inline MDockItemWindowSelector * selector_from_item(MDockItem *item)
{
return g_object_get_qdata((GObject*)item,
mdock_widget_item_window_selector_quark());
}
static void save_items_to_settings(MDockWidget *self)
{
GSettings *settings = self->priv->settings;
GSequenceIter *seq_iter = g_sequence_get_begin_iter(self->priv->items);
GVariantBuilder builder;
g_variant_builder_init(&builder, G_VARIANT_TYPE("as"));
while (!g_sequence_iter_is_end(seq_iter)) {
MDockItem *item = MDOCK_ITEM(g_sequence_get(seq_iter));
if (mdock_item_get_pinned(item)) {
GDesktopAppInfo *info = mdock_item_get_desktop_app_info(item);
if (info) {
g_variant_builder_add(&builder, "s", g_desktop_app_info_get_filename(info));
}
}
seq_iter = g_sequence_iter_next(seq_iter);
}
self->priv->loading_settings = TRUE;
g_settings_set(settings, "items", "as", &builder);
self->priv->loading_settings = FALSE;
}
static void move_item_to_position(MDockWidget *self, GSequenceIter *iter, GSequenceIter *position)
{
g_debug("moving item to position %d", g_sequence_iter_get_position(position));
gtk_box_reorder_child(GTK_BOX(self), GTK_WIDGET(g_sequence_get(iter)),
g_sequence_iter_get_position(position));
g_sequence_move(iter, position);
}
static void hide_item_popup(MDockWidget *self)
{
g_return_if_fail(self->priv->current_popup_item);
g_debug("hiding popup %p", self->priv->current_popup_item);
MDockItemWindowSelector *selector =
MDOCK_ITEM_WINDOW_SELECTOR(g_object_get_qdata(G_OBJECT(self->priv->current_popup_item),
mdock_widget_item_window_selector_quark()));
gtk_widget_hide(GTK_WIDGET(selector));
self->priv->current_popup_item = NULL;
}
static void show_item_popup(MDockWidget *self, MDockItem *item)
{
g_warn_if_fail(!self->priv->current_popup_item);
g_debug("showing popup %p", item);
MDockItemWindowSelector *selector =
MDOCK_ITEM_WINDOW_SELECTOR(g_object_get_qdata(G_OBJECT(item),
mdock_widget_item_window_selector_quark()));
// Position the popup
GtkOrientation orientation = gtk_orientable_get_orientation(GTK_ORIENTABLE(self));
GdkWindow *item_window = gtk_widget_get_window(GTK_WIDGET(item));
GdkScreen *screen = gdk_window_get_screen(item_window);
gint monitor_num = gdk_screen_get_monitor_at_window(screen, item_window);
GdkRectangle monitor;
GtkRequisition requisition;
gint item_x, item_y, item_w, item_h;
gint x, y;
gdk_window_get_origin(item_window, &item_x, &item_y);
gdk_window_get_size(item_window, &item_w, &item_h);
gdk_screen_get_monitor_geometry(screen, monitor_num, &monitor);
gtk_widget_size_request(GTK_WIDGET(selector), &requisition);
if (requisition.width > 0 || requisition.height > 0) {
if (orientation == GTK_ORIENTATION_HORIZONTAL) {
x = item_x + item_w / 2 - requisition.width / 2;
if (item_y > (monitor.y + monitor.height / 2)) {
y = item_y - requisition.height;
} else {
y = item_y + item_h;
}
} else {
y = item_y + item_y / 2 - requisition.height / 2;
if (item_x > (monitor.x + monitor.width / 2)) {
x = item_x - requisition.width;
} else {
x = item_x + item_w;
}
}
if (x + requisition.width > monitor.x + monitor.width) {
x -= x - (monitor.x + monitor.width) + requisition.width;
} else if (x < monitor.x) {
x = monitor.x;
}
if (y + requisition.height > monitor.y + monitor.height) {
y -= y - (monitor.y + monitor.height) + requisition.height;
}
gtk_window_move(GTK_WINDOW(selector), x, y);
gtk_widget_show(GTK_WIDGET(selector));
}
self->priv->current_popup_item = item;
}
static void handle_item_pinned_changed(MDockWidget *self, GParamSpec *spec, MDockItem *item)
{
if (!self->priv->loading_settings) {
save_items_to_settings(self);
}
}
static gboolean handle_item_button_press(MDockWidget *self, GdkEventButton *event, MDockItem *item)
{
switch (event->button) {
case 3:
if (self->priv->popup_timer) {
g_source_remove(self->priv->popup_timer);
self->priv->popup_timer = 0;
}
if (self->priv->current_popup_item) {
hide_item_popup(self);
}
gtk_menu_popup(GTK_MENU(g_object_get_qdata(G_OBJECT(item), mdock_widget_item_menu_quark())),
NULL, NULL, NULL, NULL, event->button, event->time);
break;
}
return TRUE;
}
static gboolean handle_item_button_release(MDockWidget *self, GdkEventButton *event, MDockItem *item)
{
switch (event->button) {
case 1:
if (self->priv->just_dropped) {
self->priv->just_dropped = FALSE;
break;
}
mdock_item_activate(item);
break;
}
return TRUE;
}
static void handle_item_drag_begin(MDockWidget *self, GdkDragContext *dc, MDockItem *item)
{
g_debug("drag begin");
gtk_drag_set_icon_pixbuf(dc, mdock_item_get_icon_pixbuf(item), 0, 0);
gtk_widget_hide(GTK_WIDGET(item));
}
static void handle_item_drag_end(MDockWidget *self, GdkDragContext *dc, MDockItem *item)
{
g_debug("drag end");
gtk_widget_show(GTK_WIDGET(item));
}
static void handle_item_drag_data_get(MDockWidget *self, GdkDragContext *dc,
GtkSelectionData *selection_data, guint target_type, guint timestamp, MDockItem *item)
{
g_debug("data get");
GSequenceIter *iter = g_object_get_qdata(G_OBJECT(item), mdock_widget_item_iter_quark());
GDesktopAppInfo *appinfo = mdock_item_get_desktop_app_info(item);
gint32 position = g_sequence_iter_get_position(iter);
switch (target_type) {
case DRAG_TYPE_ITEM:
g_debug("data get item");
gtk_selection_data_set(selection_data, gtk_selection_data_get_target(selection_data),
sizeof(gint32)*8, (guchar*)&position, sizeof(gint32));
break;
case DRAG_TYPE_URILIST:
g_debug("data get urilist");
if (appinfo) {
const gchar *strv[2] = { g_desktop_app_info_get_filename(appinfo), NULL };
gtk_selection_data_set_uris(selection_data, (gchar**) strv);
}
break;
}
}
static void handle_item_drag_data_delete(MDockWidget *self, GdkDragContext *dc, MDockItem *item)
{
g_debug("data delete");
}
static GSequenceIter * find_drop_position_at_coordinates(MDockWidget *self, gint x, gint y)
{
const guint icon_size = g_settings_get_uint(self->priv->settings, "icon-size");
const guint spacing = gtk_box_get_spacing(GTK_BOX(self));
const guint offset = icon_size / 4;
if (gtk_orientable_get_orientation(GTK_ORIENTABLE(self)) == GTK_ORIENTATION_HORIZONTAL) {
g_debug("dropped in position %d", x / icon_size);
return g_sequence_get_iter_at_pos(self->priv->items, (x + offset) / (icon_size + spacing)) ;
} else {
return g_sequence_get_iter_at_pos(self->priv->items, (y + offset) / (icon_size + spacing));
}
}
static void mdock_widget_drag_data_received(GtkWidget *widget, GdkDragContext *context,
gint x, gint y, GtkSelectionData *selection_data,
guint info, guint timestamp)
{
g_debug("data received");
MDockWidget *self = MDOCK_WIDGET(widget);
const guchar *data = gtk_selection_data_get_data(selection_data);
gchar **uris;
switch (info) {
case DRAG_TYPE_ITEM:
g_debug("Got item");
if (data) {
gint32 position = *(gint32*)data;
g_debug("Got position %u", position);
GSequenceIter *iter = g_sequence_get_iter_at_pos(self->priv->items, position);
g_warn_if_fail(iter);
GSequenceIter *pos_iter = find_drop_position_at_coordinates(self, x, y);
move_item_to_position(self, iter, pos_iter);
if (mdock_item_get_pinned(MDOCK_ITEM(g_sequence_get(iter)))) {
save_items_to_settings(self);
}
}
gtk_drag_finish(context, TRUE, TRUE, timestamp);
break;
case DRAG_TYPE_URILIST:
uris = gtk_selection_data_get_uris(selection_data);
if (uris) {
for (int i = 0; uris[i]; i++) {
gchar *uri = uris[i];
g_debug("Got uri %s", uri);
}
g_strfreev(uris);
}
gtk_drag_finish(context, TRUE, FALSE, timestamp);
break;
default:
g_message("Drag target type not found");
gtk_drag_finish(context, FALSE, FALSE, timestamp);
break;
}
}
static gboolean mdock_widget_drag_drop(GtkWidget *widget, GdkDragContext *context, gint x, gint y, guint timestamp)
{
MDockWidget *self = MDOCK_WIDGET(widget);
GtkTargetList *target_list = gtk_drag_dest_get_target_list(widget);
GdkAtom target = gtk_drag_dest_find_target(widget, context, target_list);
guint info;
gboolean drag_ok = FALSE;
if (target != GDK_NONE && gtk_target_list_find(target_list, target, &info)) {
switch (info) {
case DRAG_TYPE_ITEM:
case DRAG_TYPE_URILIST:
drag_ok = TRUE;
break;
default:
break;
}
} else {
g_message("Drag target type not found");
}
g_debug("drag drop");
self->priv->just_dropped = TRUE;
if (drag_ok) {
gtk_drag_get_data(widget, context, target, timestamp);
} else {
gtk_drag_finish(context, FALSE, FALSE, timestamp);
}
return TRUE;
}
static gboolean mdock_widget_drag_motion(GtkWidget *widget, GdkDragContext *context, gint x, gint y, guint timestamp)
{
GtkTargetList *target_list = gtk_drag_dest_get_target_list(widget);
GdkAtom target = gtk_drag_dest_find_target(widget, context, target_list);
guint info;
gboolean drag_ok = FALSE;
if (target != GDK_NONE && gtk_target_list_find(target_list, target, &info)) {
switch (info) {
case DRAG_TYPE_ITEM:
gdk_drag_status(context, GDK_ACTION_MOVE, timestamp);
drag_ok = TRUE;
break;
case DRAG_TYPE_URILIST:
gdk_drag_status(context, GDK_ACTION_COPY, timestamp);
drag_ok = TRUE;
break;
default:
break;
}
} else {
g_message("Drag target type not found");
}
if (drag_ok) {
// TODO Draw placeholder
} else {
gdk_drag_status(context, 0, timestamp);
}
return TRUE;
}
static gboolean handle_popup_timer(gpointer user_data)
{
MDockWidget *self = MDOCK_WIDGET(user_data);
if (self->priv->current_popup_item) {
hide_item_popup(self);
} else if (self->priv->current_pointed_item) {
show_item_popup(self, self->priv->current_pointed_item);
}
self->priv->popup_timer = 0;
return G_SOURCE_REMOVE;
}
static gboolean handle_item_enter(MDockWidget *self, GdkEventCrossing *event, MDockItem *item)
{
g_debug("Item enter");
self->priv->current_pointed_item = item;
if (self->priv->current_popup_item) {
if (self->priv->popup_timer) {
g_source_remove(self->priv->popup_timer);
self->priv->popup_timer = 0;
}
hide_item_popup(self);
show_item_popup(self, item);
} else if (!self->priv->popup_timer) {
self->priv->popup_timer = g_timeout_add(POPUP_SHOW_TIMEOUT, handle_popup_timer, self);
}
return FALSE;
}
static gboolean handle_item_leave(MDockWidget *self, GdkEventCrossing *event, MDockItem *item)
{
g_debug("Item leave");
g_warn_if_fail(self->priv->current_pointed_item == item);
g_warn_if_fail(!self->priv->current_popup_item || self->priv->current_pointed_item == item);
self->priv->current_pointed_item = NULL;
if (self->priv->current_popup_item && !self->priv->popup_timer) {
self->priv->popup_timer = g_timeout_add(POPUP_HIDE_TIMEOUT, handle_popup_timer, self);
}
return FALSE;
}
static gboolean handle_item_selector_enter(MDockWidget *self, GdkEventCrossing *event, MDockItemWindowSelector *selector)
{
g_debug("Item selector enter %d", event->detail);
if (event->detail != GDK_NOTIFY_NONLINEAR) return FALSE;
g_warn_if_fail(self->priv->current_popup_item);
if (self->priv->popup_timer) {
g_source_remove(self->priv->popup_timer);
self->priv->popup_timer = 0;
}
return FALSE;
}
static gboolean handle_item_selector_leave(MDockWidget *self, GdkEventCrossing *event, MDockItemWindowSelector *selector)
{
g_debug("Item selector leave %d", event->detail);
if (event->detail != GDK_NOTIFY_NONLINEAR) return FALSE;
g_warn_if_fail(self->priv->current_popup_item);
if (!self->priv->popup_timer) {
self->priv->popup_timer = g_timeout_add(POPUP_HIDE_TIMEOUT, handle_popup_timer, self);
}
return FALSE;
}
static void connect_item(MDockWidget *self, MDockItem *item, GSequenceIter *position)
{
g_object_set_qdata(G_OBJECT(item), mdock_widget_item_iter_quark(), position);;
g_signal_connect_object(item, "notify::pinned",
G_CALLBACK(handle_item_pinned_changed), self,
G_CONNECT_SWAPPED);
g_settings_bind(self->priv->settings, "icon-size",
item, "icon-size",
G_SETTINGS_BIND_GET | G_SETTINGS_BIND_NO_SENSITIVITY);
gtk_drag_source_set(GTK_WIDGET(item), GDK_BUTTON1_MASK,
drag_types, G_N_ELEMENTS(drag_types),
GDK_ACTION_MOVE);
g_signal_connect_object(item, "button-press-event",
G_CALLBACK(handle_item_button_press), self,
G_CONNECT_SWAPPED);
g_signal_connect_object(item, "button-release-event",
G_CALLBACK(handle_item_button_release), self,
G_CONNECT_SWAPPED);
g_signal_connect_object(item, "drag-begin",
G_CALLBACK(handle_item_drag_begin), self,
G_CONNECT_SWAPPED);
g_signal_connect_object(item, "drag-end",
G_CALLBACK(handle_item_drag_end), self,
G_CONNECT_SWAPPED);
g_signal_connect_object(item, "drag-data-get",
G_CALLBACK(handle_item_drag_data_get), self,
G_CONNECT_SWAPPED);
g_signal_connect_object(item, "drag-data-delete",
G_CALLBACK(handle_item_drag_data_delete), self,
G_CONNECT_SWAPPED);
g_signal_connect_object(item, "enter-notify-event",
G_CALLBACK(handle_item_enter), self,
G_CONNECT_SWAPPED);
g_signal_connect_object(item, "leave-notify-event",
G_CALLBACK(handle_item_leave), self,
G_CONNECT_SWAPPED);
MDockItemMenu *menu = mdock_item_menu_new(item);
g_object_set_qdata_full(G_OBJECT(item), mdock_widget_item_menu_quark(),
menu, (GDestroyNotify)gtk_widget_destroy);
MDockItemWindowSelector *selector = mdock_item_window_selector_new(item);
g_object_set_qdata_full(G_OBJECT(item), mdock_widget_item_window_selector_quark(),
selector, (GDestroyNotify)gtk_widget_destroy);
g_object_bind_property(self, "orientation",
selector, "orientation",
G_BINDING_DEFAULT);
g_signal_connect_object(selector, "enter-notify-event",
G_CALLBACK(handle_item_selector_enter), self,
G_CONNECT_SWAPPED);
g_signal_connect_object(selector, "leave-notify-event",
G_CALLBACK(handle_item_selector_leave), self,
G_CONNECT_SWAPPED);
}
static void reload_items_from_settings(MDockWidget *self)
{
GSettings *settings = self->priv->settings;
self->priv->loading_settings = TRUE;
GVariantIter *set_iter;
g_settings_get(settings, "items", "as", &set_iter);
GSequenceIter *seq_iter = g_sequence_get_begin_iter(self->priv->items);
gchar *value;
while (g_variant_iter_next(set_iter, "s", &value)) {
GDesktopAppInfo *set_appinfo = g_desktop_app_info_new_from_filename(value);
const gchar *set_desktopid = g_app_info_get_id(G_APP_INFO(set_appinfo));
GSequenceIter *iter = g_hash_table_lookup(self->priv->desktopid_to_item, set_desktopid);
if (iter) {
// This item is already on the dock, let's move it to the front and pin it.
MDockItem *item = MDOCK_ITEM(g_sequence_get(iter));
mdock_item_set_pinned(item, TRUE);
g_sequence_move(iter, seq_iter);
} else {
// Item not already in the dock.
MDockItem *item = mdock_item_new();
mdock_item_set_desktop_app_info(item, set_appinfo);
mdock_item_set_pinned(item, TRUE);
iter = g_sequence_insert_before(seq_iter, item);
g_hash_table_insert(self->priv->desktopid_to_item, g_strdup(set_desktopid), iter);
connect_item(self, item, iter);
gtk_widget_show(GTK_WIDGET(item));
gtk_box_pack_start(GTK_BOX(self), GTK_WIDGET(item), FALSE, FALSE, 0);
if (!g_sequence_iter_is_end(seq_iter)) {
// Fix the order!
gtk_box_reorder_child(GTK_BOX(self), GTK_WIDGET(item),
g_sequence_iter_get_position(iter));
}
}
g_object_unref(set_appinfo);
g_free(value);
}
g_variant_iter_free(set_iter);
self->priv->loading_settings = FALSE;
}
static void handle_active_window_changed(MDockWidget *self, WnckWindow *previous_window, WnckScreen *screen)
{
WnckWindow *window = wnck_screen_get_active_window(screen);
GSequenceIter *iter = g_hash_table_lookup(self->priv->window_to_item, window);
if (iter) {
MDockItem *item = MDOCK_ITEM(g_sequence_get(iter));
mdock_item_set_last_active_window(item, window);
mdock_item_window_selector_set_active_window(selector_from_item(item), window);
}
}
static gboolean filter_deleted_group(gpointer key, gpointer value, gpointer user_data)
{
return value == user_data;
}
static void handle_window_closed(MDockWidget *self, WnckWindow *window, WnckScreen *screen)
{
GSequenceIter *iter = g_hash_table_lookup(self->priv->window_to_item, window);
if (!iter) {
// We never managed this window
return;
}
MDockItem *item = MDOCK_ITEM(g_sequence_get(iter));
mdock_item_remove_window(item, window);
mdock_item_window_selector_remove_window(selector_from_item(item), window);
g_hash_table_remove(self->priv->window_to_item, window);
if (!mdock_item_has_windows(item) && !mdock_item_get_pinned(item)) {
// Removing item from dock
g_hash_table_foreach_remove(self->priv->appid_to_item, filter_deleted_group, iter);
g_hash_table_foreach_remove(self->priv->desktopid_to_item, filter_deleted_group, iter);
g_sequence_remove(iter);
if (self->priv->current_popup_item == item) {
hide_item_popup(self);
}
gtk_widget_destroy(GTK_WIDGET(item));
}
}
static void handle_window_opened(MDockWidget *self, WnckWindow *window, WnckScreen *screen)
{
if (wnck_window_is_skip_tasklist(window)) {
return;
}
AppId *appid = app_id_from_window(window);
GSequenceIter *iter = g_hash_table_lookup(self->priv->appid_to_item, appid);
if (iter) {
MDockItem *item = MDOCK_ITEM(g_sequence_get(iter));
mdock_item_add_window(item, window);
mdock_item_window_selector_add_window(selector_from_item(item), window);
app_id_destroy(appid);
g_hash_table_insert(self->priv->window_to_item, window, iter);
return;
}
const gchar *desktopid = match_appid_to_desktopid(appid);
if (desktopid) {
iter = g_hash_table_lookup(self->priv->desktopid_to_item, desktopid);
if (iter) {
MDockItem *item = MDOCK_ITEM(g_sequence_get(iter));
mdock_item_add_window(item, window);
mdock_item_window_selector_add_window(selector_from_item(item), window);
g_hash_table_insert(self->priv->appid_to_item, appid, iter); // takes ownership of appid
g_hash_table_insert(self->priv->window_to_item, window, iter);
return;
}
}
MDockItem *item = mdock_item_new();
iter = g_sequence_append(self->priv->items, item);
if (desktopid) {
GDesktopAppInfo *app_info = g_desktop_app_info_new(desktopid);
mdock_item_set_desktop_app_info(item, app_info);
g_object_unref(app_info);
}
connect_item(self, item, iter);
mdock_item_add_window(item, window);
mdock_item_window_selector_add_window(selector_from_item(item), window);
g_hash_table_insert(self->priv->appid_to_item, appid, iter); // takes ownership of appid
if (desktopid) {
g_hash_table_insert(self->priv->desktopid_to_item, g_strdup(desktopid), iter);
}
g_hash_table_insert(self->priv->window_to_item, window, iter);
gtk_widget_show(GTK_WIDGET(item));
gtk_box_pack_start(GTK_BOX(self), GTK_WIDGET(item), FALSE, FALSE, 0);
}
static void handle_settings_items_changed(MDockWidget *self, gchar *key, GSettings *settings)
{
if (!self->priv->loading_settings) {
reload_items_from_settings(self);
}
}
static void mdock_widget_constructed(GObject *obj)
{
MDockWidget *self = MDOCK_WIDGET(obj);
G_OBJECT_CLASS(mdock_widget_parent_class)->constructed(obj);
g_debug("Constructing with path: %s", self->priv->settings_path);
self->priv->settings = g_settings_new_with_path("com.javispedro.mdock.widget",
self->priv->settings_path);
g_signal_connect_object(self->priv->settings, "changed::items",
G_CALLBACK(handle_settings_items_changed), self, G_CONNECT_SWAPPED);
self->priv->wnck_screen = wnck_screen_get_default();
g_signal_connect_object(self->priv->wnck_screen, "active-window-changed",
G_CALLBACK(handle_active_window_changed), self, G_CONNECT_SWAPPED);
g_signal_connect_object(self->priv->wnck_screen, "window-closed",
G_CALLBACK(handle_window_closed), self, G_CONNECT_SWAPPED);
g_signal_connect_object(self->priv->wnck_screen, "window-opened",
G_CALLBACK(handle_window_opened), self, G_CONNECT_SWAPPED);
self->priv->items = g_sequence_new(NULL);
self->priv->appid_to_item = g_hash_table_new_full(app_id_hash, app_id_equal,
(GDestroyNotify)app_id_destroy, NULL);
self->priv->desktopid_to_item = g_hash_table_new_full(g_str_hash, g_str_equal,
g_free, NULL);
self->priv->window_to_item = g_hash_table_new_full(g_direct_hash, g_direct_equal,
NULL, NULL);
reload_items_from_settings(self);
GList *windows = wnck_screen_get_windows_stacked(self->priv->wnck_screen);
for (GList *l = windows; l; l = g_list_next(l)) {
WnckWindow *window = l->data;
handle_window_opened(self, window, self->priv->wnck_screen);
}
}
static void mdock_widget_dispose(GObject *obj)
{
MDockWidget *self = MDOCK_WIDGET(obj);
if (self->priv->settings) {
g_clear_object(&self->priv->settings);
}
if (self->priv->wnck_screen) {
g_signal_handlers_disconnect_by_data(self->priv->wnck_screen, self);
self->priv->wnck_screen = NULL;
}
G_OBJECT_CLASS(mdock_widget_parent_class)->dispose(obj);
}
static void mdock_widget_finalize(GObject *obj)
{
MDockWidget *self = MDOCK_WIDGET(obj);
g_free(self->priv->settings_path);
g_sequence_free(self->priv->items);
g_hash_table_destroy(self->priv->appid_to_item);
g_hash_table_destroy(self->priv->desktopid_to_item);
g_hash_table_destroy(self->priv->window_to_item);
G_OBJECT_CLASS(mdock_widget_parent_class)->finalize(obj);
}
static void mdock_widget_set_property(GObject *object,
guint property_id,
const GValue *value,
GParamSpec *pspec)
{
MDockWidget *self = MDOCK_WIDGET(object);
switch (property_id) {
case PROP_SETTINGS_PATH:
self->priv->settings_path = g_value_dup_string(value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
break;
}
}
static void mdock_widget_get_property(GObject *object,
guint property_id,
GValue *value,
GParamSpec *pspec)
{
MDockWidget *self = MDOCK_WIDGET(object);
switch (property_id) {
case PROP_SETTINGS_PATH:
g_value_set_string(value, self->priv->settings_path);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
break;
}
}
static void mdock_widget_init(MDockWidget *self)
{
self->priv = MDOCK_WIDGET_GET_PRIVATE(self);
#ifdef HAVE_ZEITGEIST
// Initialize zeitgeist now,
// since hilarity ensues if you try to do it on a Wnck callback...
zeitgeist_log_get_default();
#endif
gtk_drag_dest_set(GTK_WIDGET(self), 0,
drag_types, G_N_ELEMENTS(drag_types),
GDK_ACTION_MOVE);
}
static void mdock_widget_class_init(MDockWidgetClass *klass)
{
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);
widget_class->drag_data_received = mdock_widget_drag_data_received;
widget_class->drag_drop = mdock_widget_drag_drop;
widget_class->drag_motion = mdock_widget_drag_motion;
GObjectClass *obj_class = G_OBJECT_CLASS(klass);
obj_class->constructed = mdock_widget_constructed;
obj_class->dispose = mdock_widget_dispose;
obj_class->finalize = mdock_widget_finalize;
obj_class->set_property = mdock_widget_set_property;
obj_class->get_property = mdock_widget_get_property;
obj_properties[PROP_SETTINGS_PATH] = g_param_spec_string("settings-path",
"Path to GSettings for this widget",
"Set the path to GSettings for this widget",
"/com/javispedro/mdock/standalone/",
G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
g_object_class_install_properties(obj_class, N_PROPERTIES, obj_properties);
g_type_class_add_private(klass, sizeof(MDockWidgetPrivate));
}
GtkWidget *mdock_widget_new(void)
{
return GTK_WIDGET(g_object_new(MDOCK_TYPE_WIDGET, NULL));
}
|