respect https prefix
[k8lowj.git] / src / moodpicker.c
blob4995157022553d1ab5a08da1c524734305e71503
1 /* logjam - a GTK client for LiveJournal.
2 * Copyright (C) 2000-2003 Evan Martin <evan@livejournal.com>
3 */
4 #include "gtk-all.h"
5 #include "util-gtk.h"
7 #include <stdlib.h>
9 #include "liblj/livejournal.h"
11 #include "account.h"
14 typedef struct {
15 LJMood *mood;
16 GSList *children;
17 } MoodTree;
20 static gint mood_tree_compare (gconstpointer a, gconstpointer b) {
21 const MoodTree *mta = a, *mtb = b;
22 return g_ascii_strcasecmp(mta->mood->name, mtb->mood->name);
26 static void mood_tree_free (MoodTree *mt) {
27 g_slist_foreach(mt->children, (GFunc) mood_tree_free, NULL);
28 g_slist_free(mt->children);
29 g_free(mt);
33 static MoodTree *build_mood_tree (LJServer *server) {
34 GSList *moodsleft, *cur;
35 GHashTable *moodtrees;
36 LJMood nullmood = { 0 };
37 MoodTree *mtbase;
39 moodtrees = g_hash_table_new(g_int_hash, g_int_equal);
40 moodsleft = g_slist_copy(server->moods);
42 mtbase = g_new0(MoodTree, 1);
43 mtbase->mood = &nullmood;
44 g_hash_table_insert(moodtrees, &mtbase->mood->id, mtbase);
46 while (moodsleft) {
47 LJMood *mood = NULL;
48 MoodTree *mtparent = NULL, *mtnew;
49 GSList *last = NULL;
50 /* find mood that has a parent in the tree. */
51 for (cur = moodsleft; cur; last = cur, cur = cur->next) {
52 mood = cur->data;
53 mtparent = g_hash_table_lookup(moodtrees, &mood->parentid);
54 if (mtparent)
55 break;
57 if (!cur) {
58 if (moodsleft) {
59 mood = moodsleft->data;
60 g_warning("nowhere to attach mood %d (%s) to %d?\n", mood->id, mood->name, mood->parentid);
62 break;
64 if (last) last->next = cur->next; else moodsleft = cur->next;
65 g_slist_free_1(cur);
67 mtnew = g_new0(MoodTree, 1);
68 mtnew->mood = mood;
69 mtparent->children = g_slist_insert_sorted(mtparent->children, mtnew, mood_tree_compare);
70 g_hash_table_insert(moodtrees, &mtnew->mood->id, mtnew);
73 g_hash_table_destroy(moodtrees);
74 mtbase->mood = NULL;
75 return mtbase;
79 static void populate_mood_list (GtkTreeStore *store, MoodTree *mtbase, GtkTreeIter *parent) {
80 GSList *l;
81 GtkTreeIter iter;
82 MoodTree *mt;
83 for (l = mtbase->children; l; l = l->next) {
84 mt = l->data;
85 gtk_tree_store_insert(store, &iter, parent, -1);
86 gtk_tree_store_set(store, &iter, 0, mt->mood->name, 1, mt->mood->id, -1);
87 populate_mood_list(store, mt, &iter);
92 static GtkWidget *make_mood_picker (MoodTree *mt) {
93 GtkTreeStore *store;
94 GtkWidget *tree;
95 GtkTreeViewColumn *column;
96 GtkCellRenderer *renderer;
98 store = gtk_tree_store_new(2, G_TYPE_STRING, G_TYPE_INT);
99 populate_mood_list(store, mt, NULL);
101 tree = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
102 g_object_unref(G_OBJECT(store));
104 renderer = gtk_cell_renderer_text_new();
105 column = gtk_tree_view_column_new_with_attributes(_("Mood"), renderer, "text", 0, NULL);
106 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column);
108 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(tree), FALSE);
109 gtk_tree_view_expand_all(GTK_TREE_VIEW(tree));
111 return tree;
115 gboolean mood_picker_run (GtkWindow *parent, JamHostLJ *host, int *moodid, char *moodtext) {
116 GtkWidget *dlg, *vbox, *hbox;
117 GtkWidget *textentry, *textpicker, *scroll;
118 MoodTree *mt;
120 mt = NULL; // XXX blogger build_mood_tree(host->server);
121 build_mood_tree(NULL);
123 dlg = gtk_dialog_new_with_buttons(_("Current Mood"), parent, GTK_DIALOG_MODAL, GTK_STOCK_OK, GTK_RESPONSE_OK, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, NULL);
125 vbox = gtk_vbox_new(FALSE, 6);
126 gtk_container_set_border_width(GTK_CONTAINER(vbox), 6);
128 hbox = gtk_hbox_new(FALSE, 6);
129 gtk_box_pack_start(GTK_BOX(hbox), gtk_label_new_with_mnemonic(_("Mood _Text:")), FALSE, FALSE, 0);
131 GtkWidget *vbox;
132 vbox = gtk_vbox_new(FALSE, 6);
133 textentry = gtk_entry_new();
134 gtk_box_pack_start(GTK_BOX(vbox), textentry, FALSE, FALSE, 0);
136 textpicker = make_mood_picker(mt);
137 scroll = scroll_wrap(textpicker);
138 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
139 gtk_box_pack_start(GTK_BOX(vbox), scroll, TRUE, TRUE, 0);
141 gtk_box_pack_start(GTK_BOX(hbox), vbox, TRUE, TRUE, 0);
143 gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, TRUE, 0);
145 hbox = gtk_hbox_new(FALSE, 6);
146 gtk_box_pack_start(GTK_BOX(hbox), gtk_label_new_with_mnemonic(_("Mood _Icon:")), FALSE, FALSE, 0);
148 GtkWidget *vbox, *check;
149 vbox = gtk_vbox_new(FALSE, 6);
150 check = gtk_check_button_new_with_mnemonic(_("_Match Text"));
151 gtk_box_pack_start(GTK_BOX(vbox), check, FALSE, FALSE, 0);
152 /*iconpicker = make_mood_picker(mt);
153 scroll = scroll_wrap(iconpicker);
154 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll),
155 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
156 gtk_box_pack_start(GTK_BOX(vbox), scroll, TRUE, TRUE, 0); */
158 gtk_box_pack_start(GTK_BOX(hbox), vbox, TRUE, TRUE, 0);
160 gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, TRUE, 0);
162 gtk_widget_show_all(vbox);
163 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dlg)->vbox), vbox, TRUE, TRUE, 0);
165 gtk_dialog_run(GTK_DIALOG(dlg));
166 gtk_widget_destroy(dlg);
168 mood_tree_free(mt);
170 return FALSE;