1 /* logjam - a GTK client for LiveJournal.
2 * Copyright (C) 2000-2004 Evan Martin <evan@livejournal.com>
4 * vim: tabstop=4 shiftwidth=4 noexpandtab :
11 #include "usejournal.h"
13 /* "having a shared prefix" is a pretty vague concept.
14 * we try a few heuristics. */
16 sharedprefix(char *a
, char *b
) {
17 const int minmatch
= 4;
21 /* does this journal have an underscore? go from there. */
22 ulpos
= strchr(b
, '_');
24 if (strncmp(a
, b
, ulpos
-b
) == 0)
25 return (int)(ulpos
- b
);
28 /* or if the shared prefix is at least minmatch chars... */
31 return (i
> minmatch
) ? i
: 0;
35 activate_cb(GtkWidget
*w
, JamDoc
*doc
) {
37 JamAccount
*acc
= jam_doc_get_account(doc
);
39 if (!gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(w
)))
42 user
= gtk_label_get_text(GTK_LABEL(GTK_BIN(w
)->child
));
44 if (g_ascii_strcasecmp(user
, jam_account_get_username(acc
)) == 0)
45 user
= NULL
; /* post as normal user. */
47 jam_doc_set_usejournal(doc
, user
);
52 usejournal_build_menu(const char *defaultjournal
, const char *currentjournal
,
53 GSList
*journals
, gpointer doc
) {
54 GtkWidget
*menu
, *curmenu
, *item
, *label
;
58 char *curmenuprefix
= NULL
;
61 curmenu
= menu
= gtk_menu_new();
63 item
= gtk_radio_menu_item_new_with_label(group
, defaultjournal
);
64 group
= gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(item
));
65 g_signal_connect(G_OBJECT(item
), "activate",
66 G_CALLBACK(activate_cb
), doc
);
68 if (currentjournal
== NULL
)
69 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item
), TRUE
);
71 gtk_widget_show(item
);
72 gtk_menu_shell_append(GTK_MENU_SHELL(menu
), item
);
74 for (l
= journals
; l
!= NULL
; l
= l
->next
) {
75 journal
= (char*)l
->data
;
78 /* try to match this item to the prefix. */
79 if (sharedprefix(curmenuprefix
, journal
)) {
86 if (!curmenuprefix
&& l
->next
) {
87 /* try to see if this begins a new prefix. */
88 char *nextjournal
= (char*)l
->next
->data
;
90 ofs
= sharedprefix(journal
, nextjournal
);
92 /* make a new submenu for these shared-prefix journals. */
93 memcpy(prefix
, journal
, ofs
);
96 item
= gtk_menu_item_new();
97 label
= gtk_label_new(NULL
);
98 gtk_misc_set_alignment(GTK_MISC(label
), 0, 0.5);
99 gtk_label_set_markup(GTK_LABEL(label
), prefix
);
100 gtk_container_add(GTK_CONTAINER(item
), label
);
101 gtk_menu_shell_append(GTK_MENU_SHELL(menu
), item
);
102 curmenu
= gtk_menu_new();
103 curmenuprefix
= prefix
;
104 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item
), curmenu
);
105 gtk_widget_show_all(item
);
108 item
= gtk_radio_menu_item_new_with_label(group
, journal
);
109 if (currentjournal
&& strcmp(currentjournal
, journal
) == 0)
110 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item
), TRUE
);
111 g_signal_connect(G_OBJECT(item
), "activate",
112 G_CALLBACK(activate_cb
), doc
);
113 gtk_widget_show(item
);
114 gtk_menu_shell_append(GTK_MENU_SHELL(curmenu
), item
);
116 group
= gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(item
));