-
Notifications
You must be signed in to change notification settings - Fork 15
/
news.c
1456 lines (1312 loc) · 42.4 KB
/
news.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
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
/**
* kalu - Copyright (C) 2012-2014 Olivier Brunel
*
* news.c
* Copyright (C) 2012-2014 Olivier Brunel <jjk@jjacky.com>
*
* This file is part of kalu.
*
* kalu 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.
*
* kalu 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
* kalu. If not, see http://www.gnu.org/licenses/
*/
#include <config.h>
/* C */
#include <string.h>
#ifndef DISABLE_GUI
/* gtk */
#include <gtk/gtk.h>
#else
/* glib */
#include <glib.h>
#endif
/* kalu */
#include "kalu.h"
#include "news.h"
#include "curl.h"
#include "util.h"
#ifndef DISABLE_GUI
#include "util-gtk.h"
#include "gui.h" /* show_notif() */
#endif
enum {
LIST_TITLES_ALL = 0,
LIST_TITLES_SHOWN,
LIST_TITLES_READ,
NB_LISTS
};
typedef struct _parse_updates_data_t {
gboolean is_last_reached;
alpm_list_t *titles;
} parse_updates_data_t;
#ifndef DISABLE_GUI
#define HTML_MAN_PAGE DOCDIR "/html/index.html"
#define HISTORY DOCDIR "/HISTORY"
typedef struct _parse_news_data_t {
gboolean only_updates;
GtkTextView *textview;
GtkTextBuffer *buffer;
PangoAttrList *attr_list;
gboolean is_last_reached;
alpm_list_t **lists;
} parse_news_data_t;
typedef void (*GMP_text_fn) (GMarkupParseContext *context,
const gchar *text,
gsize text_len,
gpointer user_data,
GError **error);
static void
create_tags (GtkTextBuffer *buffer);
static void
xml_parser_news_text (GMarkupParseContext *context,
const gchar *text,
gsize text_len,
parse_news_data_t *parse_news_data,
GError **error _UNUSED_);
/* TRUE when hovering over a link */
static gboolean hovering_link = FALSE;
/* standard & hover-link cursors */
static GdkCursor *cursor_std = NULL;
static GdkCursor *cursor_link = NULL;
/* nb of windows open */
static gint nb_windows = 0;
static void
xml_parser_updates_text (GMarkupParseContext *context,
const gchar *text,
gsize text_len _UNUSED_,
parse_updates_data_t *parse_updates_data,
GError **error _UNUSED_)
{
const GSList *list;
alpm_list_t *i;
/* have we already reached the last unread item */
if (parse_updates_data->is_last_reached)
{
return;
}
/* is this a tag (title, description, ...) inside an item? */
list = g_markup_parse_context_get_element_stack (context);
if (!list->next || !streq ("item", list->next->data))
{
return;
}
if (streq ("title", list->data))
{
/* is this the last item from last check? */
if (NULL != config->news_last && streq (config->news_last, text))
{
parse_updates_data->is_last_reached = TRUE;
return;
}
/* was this item already read? */
FOR_LIST (i, config->news_read)
{
if (streq (i->data, text))
{
return;
}
}
/* add title to the new news */
parse_updates_data->titles = alpm_list_add (parse_updates_data->titles,
strdup (text));
}
}
#endif /* DISABLE_GUI */
static gboolean
parse_xml (gchar *xml, gboolean for_updates, gpointer data_out, GError **error)
{
GMarkupParseContext *context;
GMarkupParser parser;
GError *local_err = NULL;
zero (parser);
if (for_updates)
{
#ifndef DISABLE_GUI
parser.text = (GMP_text_fn) xml_parser_updates_text;
#endif
}
else
{
#ifdef DISABLE_GUI
return FALSE;
#else
parse_news_data_t *data;
GtkTextBuffer *buffer;
parser.text = (GMP_text_fn) xml_parser_news_text;
data = data_out;
buffer = data->buffer;
create_tags (buffer);
if (data->only_updates)
{
/* create a attribute list, for labels of check-titles */
PangoAttribute *attr;
data->attr_list = pango_attr_list_new ();
attr = pango_attr_weight_new (800);
pango_attr_list_insert (data->attr_list, attr);
attr = pango_attr_size_new (10 * PANGO_SCALE);
pango_attr_list_insert (data->attr_list, attr);
attr = pango_attr_foreground_new (0, 30583, 48059);
pango_attr_list_insert (data->attr_list, attr);
}
#endif
}
context = g_markup_parse_context_new (&parser, G_MARKUP_TREAT_CDATA_AS_TEXT,
data_out, NULL);
if (!g_markup_parse_context_parse (context,
xml,
(gssize) strlen (xml),
&local_err))
{
g_markup_parse_context_free (context);
#ifndef DISABLE_GUI
if (!for_updates && ((parse_news_data_t *)data_out)->only_updates)
{
pango_attr_list_unref (((parse_news_data_t *)data_out)->attr_list);
}
#endif
g_propagate_error (error, local_err);
return FALSE;
}
g_markup_parse_context_free (context);
#ifndef DISABLE_GUI
if (!for_updates && ((parse_news_data_t *)data_out)->only_updates)
{
pango_attr_list_unref (((parse_news_data_t *)data_out)->attr_list);
}
#endif
return TRUE;
}
gboolean
news_has_updates (alpm_list_t **titles,
gchar **xml_news,
GError **error)
{
GError *local_err = NULL;
parse_updates_data_t data;
*xml_news = curl_download (NEWS_RSS_URL, &local_err);
if (local_err != NULL)
{
g_propagate_error (error, local_err);
return FALSE;
}
zero (data);
if (!parse_xml (*xml_news, TRUE, (gpointer) &data, &local_err))
{
free (*xml_news);
g_propagate_error (error, local_err);
return FALSE;
}
if (data.titles == NULL)
{
free (*xml_news);
return FALSE;
}
else
{
*titles = data.titles;
return TRUE;
}
}
/******************* EVERYTHING BELOW IS NOT DISABLE_GUI *******************/
#ifndef DISABLE_GUI
static void
title_toggled_cb (GtkToggleButton *button, alpm_list_t **lists)
{
gchar *title = g_object_get_data (G_OBJECT (button), "title");
gboolean is_active;
g_object_get (G_OBJECT (button), "active", &is_active, NULL);
if (is_active)
{
lists[LIST_TITLES_READ] = alpm_list_add (lists[LIST_TITLES_READ], title);
}
else
{
lists[LIST_TITLES_READ] = alpm_list_remove_str (lists[LIST_TITLES_READ],
title, NULL);
}
}
#define insert_text_with_tags() do { \
gtk_text_buffer_move_mark (buffer, mark, &iter); \
gtk_text_buffer_insert (buffer, &iter, ss, -1); \
gtk_text_buffer_get_iter_at_mark (buffer, &iter2, mark); \
FOR_LIST (i, tags) \
{ \
gtk_text_buffer_apply_tag_by_name (buffer, i->data, &iter, &iter2); \
} \
} while (0)
static void
parse_to_buffer (GtkTextBuffer *buffer, const gchar *text, gsize text_len)
{
GtkTextIter iter, iter2;
GtkTextMark *mark;
GtkTextTag *tag;
alpm_list_t *i, *tags = NULL;
gchar *s, *ss, *start, *end;
gchar buf[10];
gint c, margin;
gint in_ordered_list = -1;
GdkRGBA color;
gchar *link = NULL;
s = new (gchar, text_len + 2);
snprintf (s, text_len + 1, "%s", text);
/* color used for links */
gdk_rgba_parse (&color, "rgb(0,119,187)");
/* inside <code> blocs, \n must not be converted to space */
start = s;
while ((start = strstr (start, "<code>")))
{
if (!(end = strstr (start, "</code>")))
{
break;
}
/* for now, we turn them into \r */
while ((ss = strchr (start, '\n')) && ss < end)
{
*ss = '\r';
}
start = end;
}
/* \n replaced by space */
while ((ss = strchr (s, '\n')))
{
*ss = ' ';
}
/* turn the \r from <code> blocks into \n */
while ((ss = strchr (s, '\r')))
{
*ss = '\n';
}
/* now turn <br> into \n */
while ((ss = strstr (s, "<br")))
{
if (!(end = strchr (ss, '>')))
{
break;
}
*ss = '\n';
start = end + 1;
memmove (++ss, start, strlen (start) + 1);
}
/* convert some HTML stuff */
ss = s;
while ((ss = strchr (ss, '&')))
{
end = strchr (++ss, ';');
if (!end)
{
break;
}
*end = '\0';
if (streq (ss, "minus"))
{
*--ss = '-';
++end;
memmove (++ss, end, strlen (end) + 1);
}
else if (streq (ss, "lsquo"))
{
*--ss = '`';
++end;
memmove (++ss, end, strlen (end) + 1);
}
else if (streq (ss, "rsquo"))
{
*--ss = '\'';
++end;
memmove (++ss, end, strlen (end) + 1);
}
else if (streq (ss, "quot"))
{
*--ss = '"';
++end;
memmove (++ss, end, strlen (end) + 1);
}
else if (streq (ss, "amp"))
{
*--ss = '&';
++end;
memmove (++ss, end, strlen (end) + 1);
}
else if (streq (ss, "lt"))
{
*--ss = '<';
*end = '>';
}
else if (streq (ss, "gt"))
{
*--ss = '<';
*end = '>';
}
else
{
*end = ';';
ss = end + 1;
}
}
gtk_text_buffer_get_end_iter (buffer, &iter);
mark = gtk_text_buffer_create_mark (buffer, "mark", &iter, TRUE);
ss = s;
while ((start = strchr (ss, '<')))
{
if (NULL == (end = strchr (start, '>')))
{
break;
}
*end = '\0';
if (*(start + 1) == 'p' && (*(start + 2) == '\0'
|| *(start + 2) == ' '))
{
*start = '\0';
insert_text_with_tags ();
gtk_text_buffer_insert (buffer, &iter, "\n", -1);
/* look for the margin-left style, and create a corresponding tag.
* This is useful when showing the (HTML) man page */
++start;
if ((ss = strstr (start, "margin-left:")))
{
ss += 12; /* 12 = strlen ("margin-left:") */
for (c = 0, margin = 0; *ss >= '0' && *ss <= '9'; ++ss, ++c)
{
margin = margin * 10 + (*ss - '0');
}
snprintf (buf, 10, "margin%d", margin);
if (!gtk_text_tag_table_lookup (
gtk_text_buffer_get_tag_table (buffer),
buf))
{
gtk_text_buffer_create_tag (buffer, buf,
"left-margin", margin,
NULL);
}
tags = alpm_list_add (tags, (void *) buf);
}
}
else if (streq (start + 1, "/p"))
{
*start = '\0';
insert_text_with_tags ();
gtk_text_buffer_insert (buffer, &iter, "\n", -1);
/* when showing the (HTML) man page, <p> tags (might) have a margin,
* that should be closed here. we assume proper HTML, no recursion
* and whatnot, but that should be the case
* Go through tags from last to first (first->prev is last) and
* remove the first margin found */
if (tags)
for (i = tags->prev; i; i = i->prev)
{
if (strncmp (i->data, "margin", 6) == 0)
{
tags = alpm_list_remove_str (tags, i->data, NULL);
break;
}
else if (!i->prev->next)
/* i is the first element */
break;
}
}
else if (streq (start + 1, "strong"))
{
*start = '\0';
insert_text_with_tags ();
tags = alpm_list_add (tags, (void *) "bold");
}
else if (streq (start + 1, "/strong"))
{
*start = '\0';
insert_text_with_tags ();
tags = alpm_list_remove_str (tags, "bold", NULL);
}
else if (streq (start + 1, "b"))
{
*start = '\0';
insert_text_with_tags ();
tags = alpm_list_add (tags, (void *) "bold");
}
else if (streq (start + 1, "/b"))
{
*start = '\0';
insert_text_with_tags ();
tags = alpm_list_remove_str (tags, "bold", NULL);
}
else if (streq (start + 1, "code"))
{
*start = '\0';
insert_text_with_tags ();
tags = alpm_list_add (tags, (void *) "code");
}
else if (streq (start + 1, "/code"))
{
*start = '\0';
insert_text_with_tags ();
tags = alpm_list_remove_str (tags, "code", NULL);
}
else if (streq (start + 1, "pre"))
{
*start = '\0';
insert_text_with_tags ();
tags = alpm_list_add (tags, (void *) "pre");
}
else if (streq (start + 1, "/pre"))
{
*start = '\0';
insert_text_with_tags ();
tags = alpm_list_remove_str (tags, "pre", NULL);
}
else if (streq (start + 1, "h2"))
{
*start = '\0';
insert_text_with_tags ();
gtk_text_buffer_insert (buffer, &iter, "\n", -1);
tags = alpm_list_add (tags, (void *) "title");
}
else if (streq (start + 1, "/h2"))
{
*start = '\0';
insert_text_with_tags ();
gtk_text_buffer_insert (buffer, &iter, "\n", -1);
tags = alpm_list_remove_str (tags, "title", NULL);
}
else if (streq (start + 1, "i"))
{
*start = '\0';
insert_text_with_tags ();
tags = alpm_list_add (tags, (void *) "italic");
}
else if (streq (start + 1, "/i"))
{
*start = '\0';
insert_text_with_tags ();
tags = alpm_list_remove_str (tags, "italic", NULL);
}
else if (streq (start + 1, "ul"))
{
*start = '\0';
insert_text_with_tags ();
gtk_text_buffer_insert (buffer, &iter, "\n", -1);
}
else if (streq (start + 1, "ol"))
{
*start = '\0';
insert_text_with_tags ();
gtk_text_buffer_insert (buffer, &iter, "\n", -1);
in_ordered_list = 0;
}
else if (streq (start + 1, "li"))
{
*start = '\0';
insert_text_with_tags ();
gtk_text_buffer_insert (buffer, &iter, "\n", -1);
tags = alpm_list_add (tags, (void *) "listitem");
if (in_ordered_list == -1)
{
ss = (gchar *) "• ";
}
else
{
++in_ordered_list;
snprintf (buf, 10, "%d. ", in_ordered_list);
ss = buf;
}
insert_text_with_tags ();
}
else if (streq (start + 1, "/li"))
{
*start = '\0';
insert_text_with_tags ();
gtk_text_buffer_insert (buffer, &iter, "\n", -1);
tags = alpm_list_remove_str (tags, "listitem", NULL);
}
else if (streq (start + 1, "/ol"))
{
*start = '\0';
insert_text_with_tags ();
in_ordered_list = -1;
}
else if (streq (start + 1, "lt"))
{
*start = '\0';
insert_text_with_tags ();
ss = (gchar *) "<";
insert_text_with_tags ();
}
else if (streq (start + 1, "gt"))
{
*start = '\0';
insert_text_with_tags ();
ss = (gchar *) ">";
insert_text_with_tags ();
}
else if (strncmp (start + 1, "a ", 2) == 0)
{
/* add the text */
*start = '\0';
insert_text_with_tags ();
/* get URL */
if ((link = strstr (start + 1, "href"))
&& (link = strchr (link, '"')))
{
/* got one, end it properly */
ss = strchr (++link, '"');
*ss = '\0';
}
}
else if (streq (start + 1, "/a"))
{
*start = '\0';
insert_text_with_tags ();
if (link)
{
/* create a new tag, so we can set the link to it */
tag = gtk_text_buffer_create_tag (buffer, NULL,
"foreground-rgba", &color,
"underline", PANGO_UNDERLINE_SINGLE,
NULL);
/* links on Arch's website don't always include the http:// part */
if (link[0] == '/')
{
ss = new (gchar, strlen (link) + 25);
/* TODO: get domain from NEWS_RSS_URL */
sprintf (ss, "http://www.archlinux.org%s", link);
link = ss;
}
else
{
link = strdup (link);
}
/* set link, and apply it */
g_object_set_data_full (G_OBJECT (tag), "link", link, free);
gtk_text_buffer_apply_tag (buffer, tag, &iter, &iter2);
/* done */
link = NULL;
}
}
else
{
/* unknown tag - just skip it */
*start = '\0';
insert_text_with_tags ();
}
ss = end + 1;
}
insert_text_with_tags ();
gtk_text_buffer_delete_mark (buffer, mark);
free (s);
}
#undef insert_text_with_tags
static void
xml_parser_news_text (GMarkupParseContext *context,
const gchar *text,
gsize text_len,
parse_news_data_t *parse_news_data,
GError **error _UNUSED_)
{
GtkTextBuffer *buffer = parse_news_data->buffer;
GtkTextIter iter;
gchar *s = NULL;
const GSList *list;
alpm_list_t *i;
gboolean is_title = FALSE;
static gboolean skip_next_description = FALSE;
alpm_list_t **lists = NULL;
/* have we already reached the last unread item */
if (parse_news_data->only_updates && parse_news_data->is_last_reached)
{
return;
}
/* is this a tag (title, description, ...) inside an item? */
list = g_markup_parse_context_get_element_stack (context);
if (!list->next || !streq ("item", list->next->data))
{
return;
}
/* gather whether this is title or description, don't go any further if it's
* anything else*/
if (streq ("title", list->data))
{
is_title = TRUE;
}
else if (!streq ("description", list->data))
{
return;
}
if (parse_news_data->only_updates)
{
if (is_title)
{
/* make a copy of the title, and store it in list of all titles */
/* it will not be free-d here. this is done on window_destroy_cb */
s = strdup (text);
lists = parse_news_data->lists;
lists[LIST_TITLES_ALL] = alpm_list_add (lists[LIST_TITLES_ALL], s);
/* is this the last item from last check? */
if (NULL != config->news_last && streq (config->news_last, text))
{
parse_news_data->is_last_reached = TRUE;
return;
}
/* was this item already read? */
FOR_LIST (i, config->news_read)
{
if (streq (i->data, text))
{
/* make a note to skip its description as well */
skip_next_description = TRUE;
return;
}
}
}
else if (skip_next_description)
{
skip_next_description = FALSE;
return;
}
}
if (is_title)
{
/* add a LF */
gtk_text_buffer_get_end_iter (buffer, &iter);
gtk_text_buffer_insert (buffer, &iter, "\n", -1);
if (parse_news_data->only_updates && lists)
{
GtkTextChildAnchor *anchor;
GtkWidget *check, *label;
/* store title in list of shown titles */
lists[LIST_TITLES_SHOWN] = alpm_list_add (lists[LIST_TITLES_SHOWN], s);
/* add a widget to check if the news should be marked read */
anchor = gtk_text_buffer_create_child_anchor(buffer, &iter);
check = gtk_check_button_new ();
/* we set as data the title, same as in the lists above. it will be
* used on toggled callback to be added to/removed from
* lists[LIST_TITLES_READ] */
g_object_set_data (G_OBJECT (check), "title", s);
g_signal_connect (G_OBJECT (check), "toggled",
G_CALLBACK (title_toggled_cb), lists);
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (check), TRUE);
gtk_widget_show (check);
label = gtk_label_new (text);
gtk_label_set_attributes (GTK_LABEL (label),
parse_news_data->attr_list);
gtk_container_add (GTK_CONTAINER (check), label);
gtk_widget_show (label);
gtk_text_view_add_child_at_anchor (parse_news_data->textview,
check,
anchor);
}
else
{
gtk_text_buffer_insert_with_tags_by_name (buffer, &iter,
text, -1, "title", NULL);
}
gtk_text_buffer_insert (buffer, &iter, "\n", -1);
}
else
{
parse_to_buffer (buffer, text, text_len);
}
}
static void
create_tags (GtkTextBuffer *buffer)
{
/* create tags */
GdkRGBA color;
gdk_rgba_parse (&color, "rgb(0,119,187)");
gtk_text_buffer_create_tag (buffer, "title",
"size-points", 10.0,
"weight", 800,
"foreground-rgba", &color,
NULL);
gtk_text_buffer_create_tag (buffer, "bold",
"weight", 800,
NULL);
gdk_rgba_parse (&color, "rgb(255,255,221)");
gtk_text_buffer_create_tag (buffer, "code",
"family", "Monospace",
"background-rgba", &color,
NULL);
gdk_rgba_parse (&color, "rgb(221,255,221)");
gtk_text_buffer_create_tag (buffer, "pre",
"family", "Monospace",
"background-rgba", &color,
NULL);
gtk_text_buffer_create_tag (buffer, "italic",
"style", PANGO_STYLE_ITALIC,
NULL);
gtk_text_buffer_create_tag (buffer, "listitem",
"left-margin", 15,
NULL);
}
static void
btn_close_cb (GtkWidget *button _UNUSED_, GtkWidget *window)
{
gtk_widget_destroy (window);
}
static void
btn_mark_cb (GtkWidget *button _UNUSED_, GtkWidget *window)
{
alpm_list_t **lists, *titles_all, *titles_shown, *titles_read, *i;
alpm_list_t *news_read = NULL;
char *news_last = NULL;
gboolean is_last_set = FALSE;
int nb_unread = 0;
gtk_widget_hide (window);
lists = g_object_get_data (G_OBJECT (window), "lists");
/* reverse this one, to start with the oldest news */
titles_all = alpm_list_reverse (lists[LIST_TITLES_ALL]);
titles_shown = lists[LIST_TITLES_SHOWN];
titles_read = lists[LIST_TITLES_READ];
FOR_LIST (i, titles_all)
{
void *shown = alpm_list_find_ptr (titles_shown, i->data);
/* was this news not shown, or shown and mark read? */
if (!shown || (shown && alpm_list_find_ptr (titles_read, i->data)))
{
/* was last already set? */
if (is_last_set)
{
/* then we add it to read */
debug ("read:%s", (char*)i->data);
news_read = alpm_list_add (news_read, strdup (i->data));
}
else
{
/* set the new last */
if (news_last)
{
free (news_last);
}
debug ("last=%s", (char*)i->data);
news_last = strdup (i->data);
}
}
/* was it shown? i.e. stays unread */
else if (shown)
{
++nb_unread;
/* item was not read, so we can simply say that now last is set.
* either it has been set/updated before, or it remains unchanged */
is_last_set = TRUE;
}
}
/* we only free this like so, because everything else (including the data
* in titles) will be free-d when destroying the window */
alpm_list_free (titles_all);
/* save */
FILE *fp;
char file[MAX_PATH];
gboolean saved = FALSE;
snprintf (file, MAX_PATH - 1, "%s/kalu/news.conf", g_get_user_config_dir ());
if (ensure_path (file))
{
fp = fopen (file, "w");
if (fp != NULL)
{
fputs ("Last=", fp);
fputs (news_last, fp);
fputs ("\n", fp);
FOR_LIST (i, news_read)
{
fputs ("Read=", fp);
fputs ((const char *) i->data, fp);
fputs ("\n", fp);
}
fclose (fp);
/* update */
if (config->news_last)
{
free (config->news_last);
}
config->news_last = news_last;
FREELIST (config->news_read);
config->news_read = news_read;
/* we go and change the last_notifs. if nb_unread = 0 we can
* simply remove it, else we change it to ask to run the checks again
* to be up to date */
FOR_LIST (i, config->last_notifs)
{
notif_t *notif = i->data;
if (notif->type & CHECK_NEWS)
{
if (nb_unread == 0)
{
config->last_notifs = alpm_list_remove_item (
config->last_notifs,
i);
free_notif (notif);
}
else
{
free (notif->data);
notif->data = NULL;
free (notif->text);
notif->text = strdup (_("Read news have changed, "
"you need to run the checks again to be up-to-date."));
}
break;
}
}
set_kalpm_nb (CHECK_NEWS, nb_unread, TRUE);
saved = TRUE;
}
}
if (saved)
{
gtk_widget_destroy (window);
}
else
{
gtk_widget_show (window);
show_error (_("Unable to save changes to disk"), file,
GTK_WINDOW (window));
}
}
static void
window_destroy_cb (GtkWidget *window, gpointer data _UNUSED_)
{
alpm_list_t **lists;
int i;
if (--nb_windows == 0)
{
g_object_unref (cursor_link);
g_object_unref (cursor_std);
}
/* will be present if this was a only_updates window */
lists = g_object_get_data (G_OBJECT (window), "lists");
if (lists)
{
/* lists[LIST_TITLES_ALL] is a list of all titles, we free it */
FREELIST (lists[LIST_TITLES_ALL]);
/* the other lists[LIST_TITLES_*] are made of the same pointers, hence
* their data are free-d when calling FREELIST (lists[LIST_TITLES_ALL])
* IOW alpm_list_free is enough */
for (i = 1; i < NB_LISTS; ++i)
{
alpm_list_free (lists[i]);
}
/* this was holding the pointers, free it */
free (lists);
}
/* remove from list of open windows */
remove_open_window (window);
}
static gboolean
motion_notify_event_cb (GtkTextView *textview, GdkEventMotion *event)
{
gint x, y;
GSList *tags = NULL, *t;
GtkTextIter iter;
gboolean hovering = FALSE;
gtk_text_view_window_to_buffer_coords (textview, GTK_TEXT_WINDOW_WIDGET,
(gint) event->x, (gint) event->y,
&x, &y);
gtk_text_view_get_iter_at_location (textview, &iter, x, y);
tags = gtk_text_iter_get_tags (&iter);
for (t = tags; t; t = t->next)