Thread list window loading from opimd (due to opimd has limitations, all messages...
[mokosuite2:appbunch.git] / mokomessages / threadwin.c
1 #include <libmokosuite/mokosuite.h>
2 #include <libmokosuite/gui.h>
3 #include <libmokosuite/misc.h>
4 #include <libmokosuite/settings-service.h>
5
6 #include "mokomessages.h"
7 #include "messagesdb.h"
8
9 #include <glib/gi18n-lib.h>
10
11 /* finestra principale */
12 static MokoWin* win = NULL;
13
14 /* lista conversazioni */
15 static Evas_Object* th_list;
16 static Elm_Genlist_Item_Class th_itc = {0};
17
18 static void _delete(void* mokowin, Evas_Object* obj, void* event_info)
19 {
20     mokowin_hide((MokoWin *)mokowin);
21 }
22
23 static void _list_selected(void *data, Evas_Object *obj, void *event_info) {
24
25     elm_genlist_item_selected_set((Elm_Genlist_Item*)event_info, FALSE);
26     // TODO
27
28     //thread_t* t = (thread_t *) elm_genlist_item_data_get((Elm_Genlist_Item*)event_info);
29     //msg_list_init(t);
30     //msg_list_activate();
31 }
32
33 void thread_win_activate(void)
34 {
35     g_return_if_fail(win != NULL);
36
37     mokowin_activate(win);
38 }
39
40 void thread_win_hide(void)
41 {
42     g_return_if_fail(win != NULL);
43
44     mokowin_hide(win);
45 }
46
47 static char* _newmsg_genlist_label_get(void *data, Evas_Object * obj, const char *part)
48 {
49     if (!strcmp(part, "elm.text"))
50         return g_strdup(_("New message"));
51
52     else if (!strcmp(part, "elm.text.sub"))
53         return g_strdup(_("Compose new message"));
54
55     return NULL;
56 }
57
58 static char* _th_genlist_label_get(void *data, Evas_Object * obj, const char *part)
59 {
60     MessageThread* t = data;
61
62     if (!strcmp(part, "elm.text"))
63         return g_strdup_printf("%s (%d)", t->peer, t->total_count);
64
65     else if (!strcmp(part, "elm.text.sub"))
66         return g_strdup(t->content);
67
68     else if (!strcmp(part, "elm.text.right"))
69         return get_time_repr(t->timestamp);
70
71     return NULL;
72 }
73
74 static void _list_realized(void *data, Evas_Object *obj, void *event_info)
75 {
76     MessageThread* t = (MessageThread*) elm_genlist_item_data_get((Elm_Genlist_Item*)event_info);
77     if (t && t->unread_count > 0) {
78         Evas_Object* e = (Evas_Object *) elm_genlist_item_object_get((Elm_Genlist_Item*)event_info);
79         edje_object_signal_emit(e, "elm,marker,enable", "elm");
80     }
81 }
82
83 static Evas_Object* thread_list(void)
84 {
85     // overlay per gli elementi della lista del log
86     elm_theme_overlay_add(NULL, "elm/genlist/item/thread/default");
87     elm_theme_overlay_add(NULL, "elm/genlist/item_odd/thread/default");
88
89     Evas_Object* list = elm_genlist_add(win->win);
90     elm_genlist_bounce_set(list, FALSE, FALSE);
91     elm_genlist_horizontal_mode_set(list, ELM_LIST_LIMIT);
92     elm_genlist_homogeneous_set(list, FALSE);
93
94     evas_object_size_hint_weight_set(list, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
95     evas_object_size_hint_align_set(list, EVAS_HINT_FILL, EVAS_HINT_FILL);
96     evas_object_smart_callback_add(list, "selected", _list_selected, NULL);
97     evas_object_smart_callback_add(list, "realized", _list_realized, NULL);
98
99     // prepara l'itc
100     th_itc.item_style = "thread";
101     th_itc.func.label_get = _th_genlist_label_get;
102
103     // aggiungi il primo elemento "Nuovo messaggio"
104     Elm_Genlist_Item_Class *itc = g_new0(Elm_Genlist_Item_Class, 1);
105     itc->item_style = "generic_sub";
106     itc->func.label_get = _newmsg_genlist_label_get;
107
108     elm_genlist_item_append(list, itc, NULL, NULL, ELM_GENLIST_ITEM_NONE, NULL, NULL);
109
110     evas_object_show(list);
111     return list;
112 }
113
114 static Evas_Object* make_menu(void)
115 {
116     Evas_Object *m = elm_table_add(win->win);
117     elm_table_homogenous_set(m, TRUE);
118     evas_object_size_hint_weight_set(m, 1.0, 0.0);
119     evas_object_size_hint_align_set(m, -1.0, 1.0);
120
121     /* pulsante nuovo messaggio */
122     Evas_Object *bt_compose = mokowin_menu_hover_button(win, m, _("Compose"), 0, 0, 1, 1);
123
124     /* pulsante cancella tutto */
125     Evas_Object *bt_del_all = mokowin_menu_hover_button(win, m, _("Delete all"), 1, 0, 1, 1);
126
127     /* pulsante impostazioni */
128     Evas_Object *bt_settings = mokowin_menu_hover_button(win, m, _("Settings"), 2, 0, 1, 1);
129
130     return m;
131 }
132
133 void _thread(MessageThread* th, gpointer userdata)
134 {
135     g_debug("THREAD %p, userdata=%p", th, userdata);
136
137     elm_genlist_item_append(th_list, &th_itc, th, NULL, ELM_GENLIST_ITEM_NONE, NULL, NULL);
138
139 }
140
141 void thread_win_init(MokoSettingsService *settings)
142 {
143     win = mokowin_new("mokomessages");
144     if (win == NULL) {
145         g_error("[ThreadWin] Cannot create main window. Exiting");
146         return;
147     }
148
149     win->delete_callback = _delete;
150
151     elm_win_title_set(win->win, _("Messaging"));
152     //elm_win_borderless_set(win->win, TRUE);
153
154     mokowin_create_vbox(win, FALSE);
155     mokowin_menu_enable(win);
156
157     mokowin_menu_set(win, make_menu());
158
159     th_list = thread_list();
160     elm_box_pack_start(win->vbox, th_list);
161
162     // TODO carica le conversazioni :)
163     // TODO g_idle_add(...);
164     messagesdb_foreach_thread(_thread, NULL);
165 }