summaryrefslogtreecommitdiff
path: root/src/modules/shot/e_mod_share.c
blob: caa6be6a0e920cfc1e3ccd045a216f6012e94b3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#include "e_mod_main.h"

static E_Confirm_Dialog *cd = NULL;
static Ecore_Exe        *img_write_exe = NULL;
static Evas_Object      *o_label = NULL;
static Evas_Object      *o_entry = NULL;
static Eina_List        *handlers = NULL;
static char             *url_ret = NULL;
static const char       *cnp_file = NULL;
static Eina_Bool         cnp = EINA_FALSE;

// clean up and be done
static void
_share_done(void)
{
   E_FREE_LIST(handlers, ecore_event_handler_del);
   free(url_ret);
   o_label = NULL;
   img_write_exe = NULL;
   url_ret = NULL;
   preview_abort();
}

static void
_cnp_thread_io(void *data, Ecore_Thread *eth EINA_UNUSED)
{
   char *file = data;
   unsigned char *fdata = NULL;
   ssize_t fsize = 0;
   FILE *f = fopen(file, "r");

   if (!f) goto err;
   fseek(f, 0, SEEK_END);
   fsize = ftell(f);
   fseek(f, 0, SEEK_SET);
   if (fsize > 0)
     {
        fdata = malloc(fsize);
        if (fdata)
          {
             if (fread(fdata, fsize, 1, f) == 1)
               {
                  ecore_thread_main_loop_begin();
                  elm_cnp_selection_set(e_comp->elm,
                                        ELM_SEL_TYPE_CLIPBOARD,
                                        ELM_SEL_FORMAT_IMAGE,
                                        fdata, fsize);
                  ecore_thread_main_loop_end();
               }
             free(fdata);
          }
     }
   fclose(f);
   eina_file_unlink(file);
err:
   free(file);
}

static void
_cnp_file(const char *file)
{
   ecore_thread_run(_cnp_thread_io, NULL, NULL, strdup(file));
}

// the upload dialog
static void
_upload_ok_cb(void *data EINA_UNUSED, E_Dialog *dia)
{
   // ok just hides dialog and does background upload
   o_label = NULL;
   if (dia) e_util_defer_object_del(E_OBJECT(dia));
   if (!win) return;
   E_FREE_FUNC(win, evas_object_del);
}

static void
_upload_cancel_cb(void *data EINA_UNUSED, E_Dialog *dia)
{
   o_label = NULL;
   if (dia) e_util_defer_object_del(E_OBJECT(dia));
   E_FREE_FUNC(win, evas_object_del);
   _share_done();
}

static Eina_Bool
_img_write_end_cb(void *data EINA_UNUSED, int ev_type EINA_UNUSED, void *event)
{
   Ecore_Exe_Event_Del *ev = event;

   if (ev->exe != img_write_exe) return EINA_TRUE;
   _share_done();
   if ((cnp) && (cnp_file))
     {
        _cnp_file(cnp_file);
        eina_stringshare_replace(&cnp_file, NULL);
        cnp = EINA_FALSE;
     }
   return EINA_FALSE;
}

static Eina_Bool
_img_write_out_cb(void *data, int ev_type EINA_UNUSED, void *event)
{
   Ecore_Exe_Event_Data *ev = event;
   int i;

   if (ev->exe != img_write_exe) return EINA_TRUE;
   if (!((ev->lines) && (ev->lines[0].line))) goto done;
   for (i = 0; ev->lines[i].line; i++)
     {
        const char *l = ev->lines[i].line;

        if ((l[0] == 'U') && (l[1] == ' '))
          {
             int v = atoi(l + 2);
             if ((v >= 0) && (v <= 1000))
               {
                  char buf[128];
                  // update gui...
                  snprintf(buf, sizeof(buf), _("Uploaded %i%%"), (v * 100) / 1000);
                  e_widget_label_text_set(o_label, buf);
               }
          }
        else if ((l[0] == 'R') && (l[1] == ' '))
          {
             const char *r = l + 2;
             // finished - got final url
             if (!url_ret) url_ret = strdup(r);
          }
        else if ((l[0] == 'E') && (l[1] == ' '))
          {
             int err = atoi(l + 2);
             if (data) e_widget_disabled_set(data, 1);
             e_util_dialog_show(_("Error - Upload Failed"),
                                _("Upload failed with status code:<ps/>%i"),
                                err);
             _share_done();
             break;
          }
        else if ((l[0] == 'O'))
          {
             if (data) e_widget_disabled_set(data, 1);
             if ((o_entry) && (url_ret))
             e_widget_entry_text_set(o_entry, url_ret);
             _share_done();
             break;
          }
     }
done:
   return EINA_FALSE;
}

void
share_save(const char *cmd, const char *file, Eina_Bool copy)
{
   if (copy)
     {
        eina_stringshare_replace(&cnp_file, file);
        cnp = copy;
     }
   share_write_end_watch(NULL);
   img_write_exe = ecore_exe_pipe_run
     (cmd, ECORE_EXE_PIPE_READ | ECORE_EXE_PIPE_READ_LINE_BUFFERED |
      ECORE_EXE_NOT_LEADER | ECORE_EXE_TERM_WITH_PARENT, NULL);
}

void
share_write_end_watch(void *data)
{
   E_LIST_HANDLER_APPEND(handlers, ECORE_EXE_EVENT_DEL,
                         _img_write_end_cb, data);
}

void
share_write_status_watch(void *data)
{
   E_LIST_HANDLER_APPEND(handlers, ECORE_EXE_EVENT_DATA,
                         _img_write_out_cb, data);
}

static void
_win_share_del(void *data EINA_UNUSED)
{
   if (handlers)
     ecore_event_handler_data_set(eina_list_last_data_get(handlers), NULL);
   _upload_cancel_cb(NULL, NULL);
   if (cd) e_object_del(E_OBJECT(cd));
}

void
share_dialog_show(void)
{
   E_Dialog *dia;
   Evas_Object *o, *ol;
   Evas_Coord mw, mh;

   E_FREE_LIST(handlers, ecore_event_handler_del);

   save_to(NULL, EINA_FALSE);

   E_FREE_FUNC(win, evas_object_del);

   dia = e_dialog_new(NULL, "E", "_e_shot_share");
   e_dialog_resizable_set(dia, EINA_TRUE);
   e_dialog_title_set(dia, _("Uploading screenshot"));

   o = e_widget_list_add(evas_object_evas_get(dia->win), 0, 0);
   ol = o;

   o = e_widget_label_add(evas_object_evas_get(dia->win), _("Uploading ..."));
   o_label = o;
   e_widget_list_object_append(ol, o, 0, 0, 0.5);

   o = e_widget_label_add(evas_object_evas_get(dia->win),
                          _("Screenshot is available at this location:"));
   e_widget_list_object_append(ol, o, 0, 0, 0.5);

   o = e_widget_entry_add(dia->win, NULL, NULL, NULL, NULL);
   o_entry = o;
   e_widget_list_object_append(ol, o, 1, 0, 0.5);

   e_widget_size_min_get(ol, &mw, &mh);
   e_dialog_content_set(dia, ol, mw, mh);
   e_dialog_button_add(dia, _("Hide"), NULL, _upload_ok_cb, NULL);
   e_dialog_button_add(dia, _("Cancel"), NULL, _upload_cancel_cb, NULL);
   e_object_del_attach_func_set(E_OBJECT(dia), _win_share_del);
   share_write_status_watch(eina_list_last_data_get(dia->buttons));
   elm_win_center(dia->win, 1, 1);
   e_dialog_show(dia);
}

// confirm dialog that it's ok to share
static void
_win_share_confirm_del(void *d EINA_UNUSED)
{
   cd = NULL;
}

static void
_win_share_confirm_yes(void *d EINA_UNUSED)
{
   share_dialog_show();
}

void
share_confirm(void)
{
   if (cd) return;
   cd = e_confirm_dialog_show
     (_("Confirm Share"), NULL,
      _("This image will be uploaded<ps/>"
        "to enlightenment.org. It will be publicly visible."),
      _("Confirm"), _("Cancel"),
      _win_share_confirm_yes, NULL,
      NULL, NULL, _win_share_confirm_del, NULL);
}

Eina_Bool
share_have(void)
{
   if (img_write_exe) return EINA_TRUE;
   return EINA_FALSE;
}

void
share_abort(void)
{
   E_FREE_FUNC(cd, e_object_del);
   E_FREE_FUNC(win, evas_object_del);
}