Home timeline and basic status update fully working
[mokosuite2:micromoko.git] / src / timeline.c
1 /*
2  * Micromoko
3  * Generic timeline window
4  * Copyright (C) 2009-2010 Daniele Ricci <daniele.athome@gmail.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20
21 #include <Elementary.h>
22 #include <mokosuite/utils/utils.h>
23 #include <mokosuite/utils/misc.h>
24 #include <mokosuite/ui/gui.h>
25
26 #include "globals.h"
27 #include "twitter/twitter.h"
28 #include "timeline.h"
29
30 #include <glib/gi18n-lib.h>
31
32 static void _delete(void* mokowin, Evas_Object* obj, void* event_info)
33 {
34     // TODO che famo?
35     mokowin_destroy(MOKO_WIN(mokowin));
36     elm_exit();
37 }
38
39 static Evas_Object* status_bubble(MokoWin* win, twitter_status* e)
40 {
41     Evas_Object* msg = elm_bubble_add(win->win);
42     evas_object_size_hint_weight_set(msg, EVAS_HINT_EXPAND, 0.0);
43     evas_object_size_hint_align_set(msg, EVAS_HINT_FILL, 0.0);
44     elm_bubble_label_set(msg, e->user->name);
45     if (e->timestamp >= 0)
46         elm_bubble_info_set(msg, get_time_repr(e->timestamp));
47
48     elm_bubble_corner_set(msg, "top_left");
49     evas_object_show(msg);
50
51     Evas_Object* lbl = elm_anchorblock_add(win->win);
52     evas_object_size_hint_weight_set(lbl, EVAS_HINT_EXPAND, 0.0);
53     evas_object_size_hint_align_set(lbl, 0.0, 0.0);
54     evas_object_data_set(lbl, "twitter_status", e);
55
56     char* fin_text = g_strdup_printf("%s<br><font_size=7>via %s</>", e->text, e->source);
57     elm_anchorblock_text_set(lbl, fin_text);
58     g_free(fin_text);
59
60     evas_object_show(lbl);
61     elm_bubble_content_set(msg, lbl);
62
63     return msg;
64 }
65
66 static void _home_timeline(twitter_session* session, twitter_call* call, Eina_List* timeline, void* mokowin)
67 {
68     EINA_LOG_DBG("got home timeline (timeline=%p)", timeline);
69
70     MokoWin* win = MOKO_WIN(mokowin);
71     Eina_List* iter;
72     twitter_status* e;
73     EINA_LIST_FOREACH(timeline, iter, e) {
74         //EINA_LOG_DBG("Status via %s: id=%s, text=\"%s\"", e->source, e->id, e->text);
75         Evas_Object* msg = status_bubble(win, e);
76         elm_box_pack_end(win->vbox, msg);
77     }
78 }
79
80 static void _status_sent(twitter_session* session, twitter_call* call, twitter_status* e, void* mokowin)
81 {
82     MokoWin* win = MOKO_WIN(mokowin);
83     void** data = (void**) win->data;
84     Evas_Object* entry = data[TL_DATA_STATUS_ENTRY];
85     Evas_Object* button = data[TL_DATA_TWEET_BUTTON];
86
87     if (e) {
88         Evas_Object* msg = status_bubble(win, e);
89         mokowin_pack_start(win, msg, FALSE);
90         elm_entry_entry_set(entry, "");
91     }
92     else {
93         EINA_LOG_WARN("status update failed!");
94         moko_popup_alert_new(win, _("Status update failed."));
95     }
96
97     elm_object_disabled_set(entry, FALSE);
98     elm_object_disabled_set(button, FALSE);
99 }
100
101 static void _update_status(void* mokowin, Evas_Object* obj, void* event_info)
102 {
103     MokoWin* win = MOKO_WIN(mokowin);
104     void** data = (void**) win->data;
105
106     Evas_Object* entry = data[TL_DATA_STATUS_ENTRY];
107     Evas_Object* button = data[TL_DATA_TWEET_BUTTON];
108     elm_object_disabled_set(entry, TRUE);
109     elm_object_disabled_set(button, TRUE);
110
111     const char* text = elm_entry_entry_get(entry);
112     twitter_update_status(global_session, text, NULL, _status_sent, win);
113 }
114
115 static Evas_Object* make_composer(MokoWin* win)
116 {
117     void** data = (void**) win->data;
118
119     Evas_Object* hbox = elm_box_add(win->win);
120     elm_box_horizontal_set(hbox, TRUE);
121     evas_object_size_hint_weight_set(hbox, EVAS_HINT_EXPAND, 0.0);
122     evas_object_size_hint_align_set(hbox, EVAS_HINT_FILL, 0.0);
123
124     Evas_Object* reply = elm_entry_add(win->win);
125     elm_entry_single_line_set(reply, FALSE);
126     evas_object_size_hint_weight_set(reply, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
127     evas_object_size_hint_align_set(reply, EVAS_HINT_FILL, EVAS_HINT_FILL);
128
129     data[TL_DATA_STATUS_ENTRY] = reply;
130     elm_box_pack_start(hbox, reply);
131     evas_object_show(reply);
132
133     Evas_Object* send = elm_button_add(win->win);
134     elm_button_label_set(send, _("Tweet!"));
135     evas_object_size_hint_weight_set(send, 0.0, EVAS_HINT_EXPAND);
136     evas_object_size_hint_align_set(send, 1.0, EVAS_HINT_FILL);
137     evas_object_smart_callback_add(send, "clicked", _update_status, win);
138
139     data[TL_DATA_TWEET_BUTTON] = send;
140     elm_box_pack_end(hbox, send);
141     evas_object_show(send);
142
143     return hbox;
144 }
145
146 MokoWin* timeline_new(int type)
147 {
148     MokoWin* win = mokowin_new("micromoko_timeline", TRUE);
149     if (win == NULL) {
150         EINA_LOG_ERR("cannot create timeline window.");
151         return NULL;
152     }
153
154     win->delete_callback = _delete;
155
156     elm_win_title_set(win->win, _("Micromoko"));
157
158     mokowin_create_vbox(win, TRUE);
159     const char* title;
160     switch (type) {
161         case TIMELINE_HOME:
162             title = "Home timeline";
163             break;
164         case TIMELINE_USER:
165             title = "User timeline";
166             break;
167         case TIMELINE_PUBLIC:
168             title = "Public timeline";
169             break;
170         default:
171             title = "Timeline";
172     }
173     mokowin_set_title(win, _(title));
174
175     // TODO menu hover
176     //mokowin_menu_enable(win);
177     //mokowin_menu_set(win, make_menu());
178
179     // user data
180     void** data = calloc(TL_DATA_MAX, sizeof(void*));
181     win->data = (void*) data;
182
183     Evas_Object* reply = make_composer(win);
184     mokowin_pack_end(win, reply, TRUE);
185     evas_object_show(reply);
186
187     #ifdef DEBUG
188     evas_object_resize(win->win, 480, 640);
189     #endif
190     mokowin_activate(win);
191
192     // TEST
193     twitter_get_home_timeline(global_session, _home_timeline, win);
194
195     return win;
196 }