summaryrefslogtreecommitdiff
path: root/src/view/base.c
blob: 163c66c33dd9ad8205670e6d534fa4befa6839b3 (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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
/*
 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the License);
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an AS IS BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <app.h>
#include <Elementary.h>
#include <viewmgr.h>
#include <inputmgr.h>
#include <layoutmgr.h>
#include <app_contents.h>
#include <app_define.h>
#include <app_debug.h>

#include "define.h"
#include "view.h"
#include "layout.h"
#include "data.h"
#include "util/utils.h"
#include "util/dialog_popup.h"
#include "util/file-util.h"
#include "util/fs-util.h"
#include "util/fs-error.h"

#define STYLE_MENU_BTN "menu_btn"

struct _menu_item {
	const char *name;
	const char *layout_id;
	layout_class *(*func_get_lclass)(void);
};

static struct _menu_item g_menu_item[E_LAYOUT_MAX] = {
	[E_LAYOUT_DEFAULT] = {
		"Default",
		LAYOUT_DEFAULT,
		layout_default_get_lclass,
	}
};

static void
gengrid_it_cb(void *data, Evas_Object *obj, void *event_info)
{
	if (event_info == NULL) {
		_ERR("event_info is null. failed gengrid_it_cb");
		return;
	}
	if (obj == NULL) {
		_ERR("obj is null. failed gengrid_it_cb");
		return;
	}
	if (data == NULL) {
		_ERR("data is null. failed gengrid_it_cb");
		return;
	}

	struct _priv *priv = (struct _priv *)data;

	int index, count, i;
	Elm_Object_Item *next_it = NULL, *deleted_it = NULL;
	Elm_Object_Item *selected_it = (Elm_Object_Item *)event_info;
	path_item_data_s *pid = (path_item_data_s *)elm_object_item_data_get(selected_it);

	priv->list_index = pid->list_index;
	count = elm_gengrid_items_count(obj);
	if (count == 1) {
		layoutmgr_update_layout(priv->lmgr, g_menu_item[E_LAYOUT_DEFAULT].layout_id,
				UPDATE_FILE_LIST, priv);
		return;
	}

	index = elm_gengrid_item_index_get(selected_it);
	next_it = elm_gengrid_item_next_get(selected_it);
	deleted_it = next_it;

	if (deleted_it == NULL) {
		return;
	}

	for (i = 0; i < count-index; i++) {
		next_it = elm_gengrid_item_next_get(deleted_it);
		elm_object_item_del(deleted_it);
		deleted_it = next_it;
	}

	layoutmgr_update_layout(priv->lmgr, g_menu_item[E_LAYOUT_DEFAULT].layout_id,
			UPDATE_FILE_LIST, priv);

	return;
}

static char*
_gengrid_text_get_cb(void *data, Evas_Object *obj, const char *part)
{
	if (data == NULL) {
		_ERR("data is null. failed get text");
		return NULL;
	}
	char buf[1024];
	path_item_data_s *pid = (path_item_data_s *)data;
	int index = pid->index;

	if (!strcmp(part, "elm.text")) {
		if (index == 0)
			snprintf(buf, 1023, "%s", "Home");
		else {
			if (pid->selected_folder == NULL) {
				_ERR("pid->selected_folder is null.");
				return NULL;
			}
			else
				snprintf(buf, 1023, "%s", pid->selected_folder);
		}

		return strdup(buf);
	}

	return NULL;
}

static Evas_Object*
create_gengrid(struct _priv *priv)
{
	if (priv == NULL) {
		_ERR("priv is null. failed to create_gengrid");
		return NULL;
	}

	path_item_data_s *pid = NULL;
	int w, h;
	Elm_Gengrid_Item_Class *gic = NULL;
	Evas_Object *gengrid = NULL;
	Elm_Object_Item *item = NULL;

	gengrid = elm_gengrid_add(priv->base);
	evas_object_size_hint_weight_set(gengrid, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
	evas_object_size_hint_align_set(gengrid, EVAS_HINT_FILL, EVAS_HINT_FILL);

	w = (int)(ELM_SCALE_SIZE(150));
	h = (int)(ELM_SCALE_SIZE(100));
	elm_gengrid_item_size_set(gengrid, w, h);
	elm_gengrid_align_set(gengrid, 0.0, 0.5);
	elm_gengrid_horizontal_set(gengrid, EINA_TRUE);
	elm_gengrid_multi_select_set(gengrid, EINA_FALSE);
	elm_scroller_policy_set(gengrid, ELM_SCROLLER_POLICY_OFF, ELM_SCROLLER_POLICY_OFF);

	priv->selected_parent_path = Get_Root_Path(STORAGE_TYPE_INTERNAL);

	if (priv->selected_parent_path == NULL) {
		_ERR("priv->selected_parent_path is null. failed to create_gengrid");
		return NULL;
	}

	gic = elm_gengrid_item_class_new();
	gic->item_style = "custom_1text";

	gic->func.text_get = _gengrid_text_get_cb;

	pid = calloc(1, sizeof(path_item_data_s));
	pid->index = 0;

	pid->parent_path = strdup(priv->selected_parent_path);
	pid->selected_folder = "HOME";
	pid->full_path = strdup(priv->selected_parent_path);
	pid->list_index = 0;

	item = elm_gengrid_item_append(gengrid, gic, pid, gengrid_it_cb, priv);

	if (item == NULL) {
		_ERR("item is null. Failed elm_gengrid_item_append");
		util_free_path_item_data_s(pid);
		free(pid);
		pid = NULL;
		return NULL;
	}

	pid->item = item;
	mf_file_attr_get_path_level(pid->parent_path, &pid->level);

	elm_object_item_data_set(item, pid);

	evas_object_show(gengrid);
	elm_gengrid_item_class_free(gic);

	return gengrid;
}

static void
update_gengrid(void *data)
{
	if (data == NULL) {
		_ERR("data is null, failed to update gengrid");
		return;
	}

	struct _priv *priv = (struct _priv *)data;

	path_item_data_s *pid = NULL;
	path_item_data_s *pid_del = NULL;
	Elm_Gengrid_Item_Class *gic = NULL;
	Elm_Object_Item *item = NULL, *delete = NULL;

	int buffer_size;
	char *new_path;
	Evas_Object *file_path = NULL;

	if (priv->base == NULL) {
		_ERR("priv->base is null. failed to update gengrid");
		return;
	}

	file_path = elm_object_part_content_get(priv->base, PART_FILE_PATH);

	if (file_path == NULL) {
		_ERR("file_path is null. failed to update gengrid");
		return;
	}

	if (priv->selected_parent_path == NULL || priv->selected_folder == NULL) {
		_ERR("selected_parent_path or selected_folder is null. failed to update gengrid");
		return;
	}

	buffer_size = strlen(priv->selected_parent_path)+strlen("/")+strlen(priv->selected_folder);
	new_path = (char *)calloc(buffer_size+1, sizeof(char));
	snprintf(new_path, buffer_size+1, "%s/%s", priv->selected_parent_path, priv->selected_folder);

	if(new_path == NULL) {
		_ERR("new_path is null. failed to update gengrid");
		SAFE_FREE_CHAR(new_path);
		return;
	}

	if (priv->same == 1) {
		delete = elm_gengrid_last_item_get(file_path);
		if (delete != NULL) {
			pid_del = (path_item_data_s *)elm_object_item_data_get(delete);
			if (pid_del != NULL) {
				util_free_path_item_data_s(pid_del);
				free(pid_del);
				pid_del = NULL;
			}

			elm_object_item_del(delete);
		}
	} else if (priv->same < 0) {
		int i;
		int num = priv->same * (-1);
		for (i = 0; i <= num; i++) {
			delete = elm_gengrid_last_item_get(file_path);
			if (delete != NULL) {
				pid_del = (path_item_data_s *)elm_object_item_data_get(delete);
				if (pid_del != NULL) {
					util_free_path_item_data_s(pid_del);
					free(pid_del);
					pid_del = NULL;
				}
				elm_object_item_del(delete);
			}
		}
	}

	gic = elm_gengrid_item_class_new();
	gic->item_style = "custom_1text";

	gic->func.text_get = _gengrid_text_get_cb;

	pid = calloc(1, sizeof(path_item_data_s));
	pid->selected_folder = priv->selected_folder;
	pid->parent_path = strdup(priv->selected_parent_path);
	pid->index = elm_gengrid_items_count(file_path);
	pid->full_path = strdup(new_path);
	pid->list_index = priv->list_index;

	if (pid->full_path == NULL) {
		_ERR("pid->full_path is null. failed to update gengrid");
		util_free_path_item_data_s(pid);
		free(pid);
		pid = NULL;
		SAFE_FREE_CHAR(new_path);
		return;
	}

	mf_file_attr_get_path_level(pid->full_path, &pid->level);
	item = elm_gengrid_item_append(file_path, gic, pid, gengrid_it_cb, priv);
	if (item == NULL) {
		_ERR("item is null. Failed elm_gengrid_item_append");
		util_free_path_item_data_s(pid);
		free(pid);
		pid = NULL;
		SAFE_FREE_CHAR(new_path);
		return;
	}
	pid->item = item;

	elm_object_item_data_set(item, pid);
	evas_object_show(file_path);
	elm_gengrid_item_class_free(gic);

	if (pid_del != NULL) {
		free(pid_del);
		pid_del = NULL;
	}
	SAFE_FREE_CHAR(new_path);
	return;
}


static void _menu_cb(void *data, Evas_Object *obj, void *event_info)
{
	if (data == NULL) {
		_ERR("data is null. failed menu_cb");
		return;
	}

	struct _priv *priv = (struct _priv *)data;

	Elm_Object_Item *item = NULL;

	if (priv->base == NULL) {
		_ERR("priv->base is null. failed _menu_cb");
		return;
	}

	Evas_Object *file_path = elm_object_part_content_get(priv->base, PART_FILE_PATH);

	if (file_path == NULL) {
		_ERR("file_path is null. failed _menu_cb");
		return;
	}

	item = elm_gengrid_last_item_get(file_path);

	if (item == NULL) {
		_ERR("item is null. failed _menu_cb");
		return;
	}

	path_item_data_s *pid = (path_item_data_s *)elm_object_item_data_get(item);

	if (pid->selected_folder == NULL)
		priv->selected_folder = NULL;
	else
		priv->selected_folder = pid->selected_folder;

	if (pid->parent_path == NULL)
		priv->selected_parent_path = NULL;
	else
		priv->selected_parent_path = pid->parent_path;

	utils_show_submenu_popup(priv);
}

static void _menu_cb2(void *data, Evas_Object *obj, void *event_info)
{
	if (data == NULL) {
		_ERR("data is null. failed menu_cb2");
		return;
	}

	struct _priv *priv = (struct _priv *)data;
	priv->popup_base = obj;

	Elm_Object_Item *item;

	Evas_Object *file_path = elm_object_part_content_get(priv->base, PART_FILE_PATH);
	item = elm_gengrid_last_item_get(file_path);

	path_item_data_s *pid = (path_item_data_s *)elm_object_item_data_get(item);

	if (pid->selected_folder == NULL)
		priv->selected_folder = NULL;
	else
		priv->selected_folder = pid->selected_folder;

	if (pid->parent_path == NULL)
		priv->selected_parent_path = NULL;
	else
		priv->selected_parent_path = pid->parent_path;

	utils_show_select_folder_popup(priv);
}

static void _key_up_cb(int id, void *data, Evas *e, Evas_Object *obj,
		Evas_Event_Key_Up *ev)
{
	if (!strcmp(ev->keyname, KEY_BACK) ||
			!strcmp(ev->keyname, KEY_ESC))
		ui_app_exit();

	return;
}

static input_handler _menu_input_handler = {
	.key_up = _key_up_cb
};

static bool _draw_menu(struct _priv *priv)
{
	if (priv == NULL) {
		_ERR("priv is null. failed draw menu");
		return false;
	}

	Evas_Object *icon = elm_button_add(priv->base);

	elm_object_style_set(icon, STYLE_MENU_BTN);
	elm_object_part_content_set(priv->base, PART_MENU, icon);
	evas_object_resize(icon, 46, 46);
	evas_object_show(icon);
	elm_object_focus_allow_set(icon, EINA_TRUE);

	Evas_Object *icon2 = elm_button_add(priv->base);

	elm_object_style_set(icon2, STYLE_MENU_BTN);
	elm_object_part_content_set(priv->base, PART_CONFIRM, icon2);
	evas_object_resize(icon2, 46, 46);
	elm_object_focus_allow_set(icon2, EINA_TRUE);

	priv->icon1 = icon;
	priv->icon2 = icon2;

	evas_object_smart_callback_add(icon, "clicked", _menu_cb, priv);
	evas_object_smart_callback_add(icon2, "clicked", _menu_cb2, priv);

	Evas_Object *path = elm_object_part_content_get(priv->base, PART_FILE_PATH);
	elm_object_focus_next_object_set(path, icon, ELM_FOCUS_UP);
	elm_object_focus_next_object_set(icon, path, ELM_FOCUS_DOWN);
	elm_object_focus_next_object_set(icon, icon2, ELM_FOCUS_LEFT);
	elm_object_focus_next_object_set(icon2, icon, ELM_FOCUS_RIGHT);

	return true;
}

static bool _draw_file_path(struct _priv *priv)
{
	Evas_Object *gengrid = NULL;

	if (priv == NULL) {
		_ERR("priv is null. failed draw file path");
		return false;
	}

	gengrid = create_gengrid(priv);

	if (gengrid == NULL) {
		_ERR("gengrid is null. failed draw file path");
		return false;
	}

	evas_object_show(gengrid);
	elm_object_part_content_set(priv->base, PART_FILE_PATH, gengrid);

	return true;
}

static bool _draw_view_content(struct _priv *priv)
{
	if (!priv)
		return false;

	if (!_draw_file_path(priv)) {
		_ERR("Draw file path failed.");
		return false;
	}
	if (!_draw_menu(priv)) {
		_ERR("Draw menu failed.");
		return false;
	}

	return true;
}

static Evas_Object *_create(Evas_Object *win, void *data)
{
	struct _priv *priv;
	Evas_Object *base;
	layoutmgr *lmgr;
	int i;

	if (!win) {
		_ERR("Get window object failed.");
		return NULL;
	}

	priv = calloc(1, sizeof(*priv));
	if (!priv) {
		_ERR("Calloc failed.");
		return NULL;
	}

	base = utils_add_layout(win, GRP_LAYOUT_BASE, EINA_TRUE);

	if (!base) {
		_ERR("Add layout failed.");
		free(priv);
		return NULL;
	}

	priv->win = win;
	priv->base = base;

	if (!_draw_view_content(priv)) {
		_ERR("failed to draw contents");
		free(priv);
		return NULL;
	}

	lmgr = layoutmgr_create(base);
	priv->lmgr = lmgr;

	for (i = 0; i < 1; i++)
		layoutmgr_add_layout(lmgr, g_menu_item[i].func_get_lclass(), priv);

	if (!viewmgr_set_view_data(VIEW_BASE, priv)) {
		_ERR("Set view data failed.");
		goto end;
	}

	inputmgr_add_callback(priv->base, 0, &_menu_input_handler, priv);

	return base;

end:
	evas_object_del(base);
	free(priv);

	return NULL;
}

static void _show(void *data)
{
	struct _priv *priv;

	if (!data) {
		_ERR("Get data failed.");
		return;
	}
	priv = data;

	if (priv->base) {
		evas_object_show(priv->base);
		layoutmgr_show_layout(priv->lmgr, g_menu_item[E_LAYOUT_DEFAULT].layout_id);
	}
}

static void _hide(void *data)
{
	struct _priv *priv;

	if (!data) {
		_ERR("Get data failed.");
		return;
	}
	priv = data;

	if (priv->base)
		evas_object_hide(priv->base);
}

static void _update(void *view_data, int update_type, void *data)
{
	struct _priv *priv;

	if (!view_data) {
		_ERR("failed to get view data");
		return;
	}

	priv = data;

	switch (update_type) {
	case UPDATE_FILE:
		elm_object_signal_emit(priv->base, SIG_INVISIBLE, SOURCE_ELM);
		layoutmgr_update_layout(priv->lmgr, g_menu_item[E_LAYOUT_DEFAULT].layout_id,
						update_type, priv);
		break;

	case UPDATE_FILE_TO_SELECT:
		elm_object_signal_emit(priv->base, SIG_VISIBLE, SOURCE_ELM);
		layoutmgr_update_layout(priv->lmgr, g_menu_item[E_LAYOUT_DEFAULT].layout_id,
						update_type, priv);
		break;

	case UPDATE_PATH:
		update_gengrid(priv);
		break;

	default:
		break;
	}

	return;
}

static void _destroy(void *data)
{
	struct _priv *priv;

	if (!data) {
		_ERR("Get data failed.");
		return;
	}
	priv = data;

	if (priv->base)
		evas_object_del(priv->base);

	free(priv);
}

static view_class _vclass = {
	.view_id = VIEW_BASE,
	.create = _create,
	.show = _show,
	.hide = _hide,
	.update = _update,
	.destroy = _destroy,
};

view_class *view_base_get_vclass(void)
{
	return &_vclass;
}