1 #include "e_mod_main.h"
2 
3 static E_Confirm_Dialog *cd = NULL;
4 static Ecore_Exe        *img_write_exe = NULL;
5 static Evas_Object      *o_label = NULL;
6 static Evas_Object      *o_entry = NULL;
7 static Eina_List        *handlers = NULL;
8 static char             *url_ret = NULL;
9 
10 // clean up and be done
11 static void
_share_done(void)12 _share_done(void)
13 {
14    E_FREE_LIST(handlers, ecore_event_handler_del);
15    free(url_ret);
16    o_label = NULL;
17    img_write_exe = NULL;
18    url_ret = NULL;
19    preview_abort();
20 }
21 
22 // the upload dialog
23 static void
_upload_ok_cb(void * data EINA_UNUSED,E_Dialog * dia)24 _upload_ok_cb(void *data EINA_UNUSED, E_Dialog *dia)
25 {
26    // ok just hides dialog and does background upload
27    o_label = NULL;
28    if (dia) e_util_defer_object_del(E_OBJECT(dia));
29    if (!win) return;
30    E_FREE_FUNC(win, evas_object_del);
31 }
32 
33 static void
_upload_cancel_cb(void * data EINA_UNUSED,E_Dialog * dia)34 _upload_cancel_cb(void *data EINA_UNUSED, E_Dialog *dia)
35 {
36    o_label = NULL;
37    if (dia) e_util_defer_object_del(E_OBJECT(dia));
38    E_FREE_FUNC(win, evas_object_del);
39    _share_done();
40 }
41 
42 static Eina_Bool
_img_write_end_cb(void * data EINA_UNUSED,int ev_type EINA_UNUSED,void * event)43 _img_write_end_cb(void *data EINA_UNUSED, int ev_type EINA_UNUSED, void *event)
44 {
45    Ecore_Exe_Event_Del *ev = event;
46 
47    if (ev->exe != img_write_exe) return EINA_TRUE;
48    _share_done();
49    return EINA_FALSE;
50 }
51 
52 static Eina_Bool
_img_write_out_cb(void * data,int ev_type EINA_UNUSED,void * event)53 _img_write_out_cb(void *data, int ev_type EINA_UNUSED, void *event)
54 {
55    Ecore_Exe_Event_Data *ev = event;
56    int i;
57 
58    if (ev->exe != img_write_exe) return EINA_TRUE;
59    if (!((ev->lines) && (ev->lines[0].line))) goto done;
60    for (i = 0; ev->lines[i].line; i++)
61      {
62         const char *l = ev->lines[i].line;
63 
64         if ((l[0] == 'U') && (l[1] == ' '))
65           {
66              int v = atoi(l + 2);
67              if ((v >= 0) && (v <= 1000))
68                {
69                   char buf[128];
70                   // update gui...
71                   snprintf(buf, sizeof(buf), _("Uploaded %i%%"), (v * 100) / 1000);
72                   e_widget_label_text_set(o_label, buf);
73                }
74           }
75         else if ((l[0] == 'R') && (l[1] == ' '))
76           {
77              const char *r = l + 2;
78              // finished - got final url
79              if (!url_ret) url_ret = strdup(r);
80           }
81         else if ((l[0] == 'E') && (l[1] == ' '))
82           {
83              int err = atoi(l + 2);
84              if (data) e_widget_disabled_set(data, 1);
85              e_util_dialog_show(_("Error - Upload Failed"),
86                                 _("Upload failed with status code:<ps/>%i"),
87                                 err);
88              _share_done();
89              break;
90           }
91         else if ((l[0] == 'O'))
92           {
93              if (data) e_widget_disabled_set(data, 1);
94              if ((o_entry) && (url_ret))
95              e_widget_entry_text_set(o_entry, url_ret);
96              _share_done();
97              break;
98           }
99      }
100 done:
101    return EINA_FALSE;
102 }
103 
104 void
share_save(const char * cmd)105 share_save(const char *cmd)
106 {
107    share_write_end_watch(NULL);
108    img_write_exe = ecore_exe_pipe_run
109      (cmd, ECORE_EXE_PIPE_READ | ECORE_EXE_PIPE_READ_LINE_BUFFERED |
110       ECORE_EXE_NOT_LEADER | ECORE_EXE_TERM_WITH_PARENT, NULL);
111 }
112 
113 void
share_write_end_watch(void * data)114 share_write_end_watch(void *data)
115 {
116    E_LIST_HANDLER_APPEND(handlers, ECORE_EXE_EVENT_DEL,
117                          _img_write_end_cb, data);
118 }
119 
120 void
share_write_status_watch(void * data)121 share_write_status_watch(void *data)
122 {
123    E_LIST_HANDLER_APPEND(handlers, ECORE_EXE_EVENT_DATA,
124                          _img_write_out_cb, data);
125 }
126 
127 static void
_win_share_del(void * data EINA_UNUSED)128 _win_share_del(void *data EINA_UNUSED)
129 {
130    if (handlers)
131      ecore_event_handler_data_set(eina_list_last_data_get(handlers), NULL);
132    _upload_cancel_cb(NULL, NULL);
133    if (cd) e_object_del(E_OBJECT(cd));
134 }
135 
136 void
share_dialog_show(void)137 share_dialog_show(void)
138 {
139    E_Dialog *dia;
140    Evas_Object *o, *ol;
141    Evas_Coord mw, mh;
142 
143    E_FREE_LIST(handlers, ecore_event_handler_del);
144 
145    save_to(NULL);
146 
147    E_FREE_FUNC(win, evas_object_del);
148 
149    dia = e_dialog_new(NULL, "E", "_e_shot_share");
150    e_dialog_resizable_set(dia, EINA_TRUE);
151    e_dialog_title_set(dia, _("Uploading screenshot"));
152 
153    o = e_widget_list_add(evas_object_evas_get(dia->win), 0, 0);
154    ol = o;
155 
156    o = e_widget_label_add(evas_object_evas_get(dia->win), _("Uploading ..."));
157    o_label = o;
158    e_widget_list_object_append(ol, o, 0, 0, 0.5);
159 
160    o = e_widget_label_add(evas_object_evas_get(dia->win),
161                           _("Screenshot is available at this location:"));
162    e_widget_list_object_append(ol, o, 0, 0, 0.5);
163 
164    o = e_widget_entry_add(dia->win, NULL, NULL, NULL, NULL);
165    o_entry = o;
166    e_widget_list_object_append(ol, o, 1, 0, 0.5);
167 
168    e_widget_size_min_get(ol, &mw, &mh);
169    e_dialog_content_set(dia, ol, mw, mh);
170    e_dialog_button_add(dia, _("Hide"), NULL, _upload_ok_cb, NULL);
171    e_dialog_button_add(dia, _("Cancel"), NULL, _upload_cancel_cb, NULL);
172    e_object_del_attach_func_set(E_OBJECT(dia), _win_share_del);
173    share_write_status_watch(eina_list_last_data_get(dia->buttons));
174    elm_win_center(dia->win, 1, 1);
175    e_dialog_show(dia);
176 }
177 
178 // confirm dialog that it's ok to share
179 static void
_win_share_confirm_del(void * d EINA_UNUSED)180 _win_share_confirm_del(void *d EINA_UNUSED)
181 {
182    cd = NULL;
183 }
184 
185 static void
_win_share_confirm_yes(void * d EINA_UNUSED)186 _win_share_confirm_yes(void *d EINA_UNUSED)
187 {
188    share_dialog_show();
189 }
190 
191 void
share_confirm(void)192 share_confirm(void)
193 {
194    if (cd) return;
195    cd = e_confirm_dialog_show
196      (_("Confirm Share"), NULL,
197       _("This image will be uploaded<ps/>"
198         "to enlightenment.org. It will be publicly visible."),
199       _("Confirm"), _("Cancel"),
200       _win_share_confirm_yes, NULL,
201       NULL, NULL, _win_share_confirm_del, NULL);
202 }
203 
204 Eina_Bool
share_have(void)205 share_have(void)
206 {
207    if (img_write_exe) return EINA_TRUE;
208    return EINA_FALSE;
209 }
210 
211 void
share_abort(void)212 share_abort(void)
213 {
214    E_FREE_FUNC(cd, e_object_del);
215    E_FREE_FUNC(win, evas_object_del);
216 }
217