summaryrefslogtreecommitdiff
path: root/main/src/data/ivug-data.c
blob: 4e05214e04da4804690779d9eb8a94ae5194b3bf (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
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
/*
 * Copyright 2012  Samsung Electronics Co., Ltd
 *
 * Licensed under the Flora License, Version 1.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.tizenopensource.org/license
 *
 * 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 "ivug-main.h"
#include "ivug-common.h"

#include "ivug-data.h"
#include "ivug-util.h"
#include "ivug-data-priv.h"
#include "ivug-data-list.h"
#include "ivug-data-filter.h"


#include <media-svc.h>

static bool
_ivug_data_set_window_section(Media_List *mList, int start_index, int total_count, int *stp, int *endp)
{
	IV_ASSERT(mList != NULL);

	_Media_List *_mList = (_Media_List *)mList;

	//if(total_count > _mList->window_size && start_index > 0)	// TODO : IV_ASSERT(start_index > 0); after fix gallery
	if(start_index > 0 && total_count > 0 && start_index <= total_count)	// TODO : IV_ASSERT(start_index > 0); after fix gallery
	{
		*stp = (start_index-1) - (_mList->window_size/2);
		if(*stp < 0)
		{
			*stp = 0;
		}
		*endp = (start_index-1) + (_mList->window_size/2);
		if(*endp > total_count-1)
		{
			*endp = total_count-1;
		}
		if(_mList->window_count > 0)
		{
			if(*stp < _mList->left_end)	/* load left window */
			{
				if(*endp > _mList->left_end-1)
				{
					*endp = _mList->left_end -1;
				}
				_mList->left_end = *stp;
			}
			if(*endp > _mList->right_end)	/* load right window */
			{
				if(*stp < _mList->right_end+1)
				{
					*stp = _mList->right_end +1;
				}
				_mList->right_end = *endp;
			}
		}
		else		// first window loding
		{
			MSG_SDATA_HIGH("First window loading");
			_mList->left_end = *stp;
			_mList->right_end = *endp;
			_mList->window_count++;
		}
		MSG_SDATA_HIGH("Window loading [%d~%d]", *stp, *endp);

		MSG_SDATA_HIGH("true : _mlist [%d~%d]", _mList->left_end, _mList->right_end);
		return true;
	}
	else
	{
		if(_mList->window_count == 0)
		{
			MSG_SDATA_HIGH("Entire loading");
			_mList->left_end = 0;
			_mList->right_end = total_count-1;
		}
		MSG_SDATA_HIGH("false : _mlist [%d~%d]", _mList->left_end, _mList->right_end);
		return false;
	}
}


static bool
_ivug_data_add_media_item(Eina_List **list, const char *filepath)
{
	IV_ASSERT(list != NULL);

	int ret = 0;
	Mitem* item = NULL;

	if (ivug_is_web_uri(filepath) == false)
	{	//local file.
		ret = minfo_get_item(gGetMediaSVCHandle(), filepath, &item); //get cluster id
		if (ret != MB_SVC_ERROR_NONE)
		{
			MSG_SDATA_ERROR("Cannot find file in media db for %s", filepath);
		}
	}

	Media_Data* mdata = NULL;

	if (item != NULL)
	{
		mdata = ivug_alloc_mediadata_from_mitem(item);
		minfo_destroy_mtype_item(item);
	}
	else
	{
		mdata = ivug_alloc_mediadata_from_filepath(filepath);
	}

	if ( mdata == NULL )
	{
		MSG_SDATA_ERROR("Cannot create slide data for %s", filepath);
		return false;
	}

	*list = eina_list_append(*list, mdata);

	return true;

}

Eina_List *_ivug_data_create_window_size_list(Media_List *mList, int start_index, int total_count)
{
	IV_ASSERT(mList != NULL);

	_Media_List *_mList = (_Media_List *)mList;
	Eina_List* slide_list = NULL;

	MSG_SDATA_HIGH("start index is %d", start_index);

	if(start_index < 1 || start_index > total_count)
	{
		MSG_SDATA_ERROR("start index is invalid %d", start_index);
		return NULL;
	}

//	Eina_List *ivug_list_load_items(List_Filter *filter, int stp, int endp);

	int stp = 0, endp = 0;

	_ivug_data_set_window_section(mList, start_index, total_count, &stp, &endp);

	slide_list = ivug_list_load_items(_mList->filter, stp, endp);

	if(slide_list == NULL)
	{
		MSG_SDATA_ERROR("Create list ERROR");
		return NULL;
	}

	return slide_list;
}

static Media_Item *
_ivug_data_create_list(Media_List *mList, Eina_List **list, const char *filepath, int *start_index, int *total_count)
{
	IV_ASSERT(mList != NULL);
	IV_ASSERT(filepath != NULL);

	MSG_SDATA_HIGH("Creating slide data list");

	_Media_List *_mList = (_Media_List *)mList;
	Media_Item *item = NULL;

	*total_count = -1;

	if(_mList->filter->view_by == IVUG_VIEW_BY_FILE)
	{
		if (filepath == NULL)
		{
			MSG_SDATA_ERROR("file path is NULL");
			return NULL;
		}

		// No need to create list. just one file only.
		if ( _ivug_data_add_media_item(list, filepath) == false)
		{
			MSG_SDATA_ERROR("Cannot add file : %s", filepath);
				return NULL;
		}
		*total_count = 1;
		item = *list;
		return item;
	}
	else
	{
		*total_count = ivug_list_get_item_cnt(_mList->filter);

		*list = _ivug_data_create_window_size_list(mList, *start_index, *total_count);
	}

	if(*list == NULL)
	{
		MSG_SDATA_ERROR("list is NULL");
		return NULL;
	}

	if(*total_count == 1)
	{
		item = *list;
		*start_index = 1;
		return item;
	}
	else if(*start_index > 0 && *start_index <= *total_count)
	{
		item = eina_list_nth_list(*list, (*start_index-1)-_mList->left_end);
	}

	Media_Data *mdata = NULL;
	bool find = false;
	int index = 0;

	if(item != NULL)
	{
		MSG_SDATA_HIGH("start index exist");
		mdata = ivug_data_get_data(item);

		if(mdata != NULL)
		{
			if ((mdata)->filepath)
			{
				if(strncmp(filepath, mdata->filepath, strlen(filepath)) == 0)
				{
					MSG_SLIDER_HIGH("Founded Filepath=%s Index=%d", ivug_get_filename(mdata->filepath), *start_index);
					index = *start_index;	// recieve from bundle
					find = true;
				}
			}
			if (find == false && mdata->fileurl)
			{
				if (strncmp(filepath, mdata->fileurl, strlen(filepath)) == 0)		//match
				{
					MSG_SLIDER_HIGH("Founded Fileurl=%s Index=%d", ivug_get_filename(mdata->fileurl), *start_index);
					index = *start_index;	// recieve from bundle
					find = true;
				}
			}
			if(find == false)
			{
				MSG_SLIDER_WARN("param index is not match to real index");
			}
		}
	}
	//check indexed item
	if(item == NULL || find == false)		// start index is not exist || can't find filename at window list
	{
		item = ivug_data_find_item(*list, filepath, &index);

		if (item == NULL)
		{
			// TODO : find file at whole file list
			MSG_SLIDER_ERROR("Item is not found for file %s", filepath);
			return NULL;
		}
	}
	*start_index = index;
	MSG_SDATA_HIGH("Create slide data list END");
	return item;
}

Media_List *ivug_data_init_media_list(const ivug_parameter *param)
{
	_Media_List *_mList = calloc(1, sizeof(_Media_List));
	IV_ASSERT(_mList != NULL);

	_mList->window_size = IV_WINDOW_SIZE;
	_mList->window_reload_offset = IV_RELOAD_OFFSET;
	_mList->window_count = 0;
	_mList->left_end = 0;

	_mList->filter = ivug_data_filter_create(param);

	return (Media_List *)_mList;
}

Media_Item *
ivug_data_create_media_list_from_param
	(Media_List *mList, Eina_List **list, const char *filepath, int *start_index, int *total_count)
{
	IV_ASSERT(mList != NULL);

	MSG_SDATA_HIGH("Creating slide data list");

	_Media_List *_mList = (_Media_List *)mList;

	if(list == NULL)
	{
		return _ivug_data_create_list(mList, &(_mList->header), filepath, start_index, total_count);
	}
	else
	{
		return _ivug_data_create_list(mList, list, filepath, start_index, total_count);
	}
}

bool ivug_data_set_window_size(Media_List *mList, int size)
{
	_Media_List *_mList = (_Media_List *)mList;

	if(size < 1)
	{
		MSG_SDATA_ERROR("Set window size error, size = %d", size);
		_mList->window_size = 0;
		return false;
	}

	_mList->window_size = size;
	return true;
}

void ivug_data_reset_window_load_count(Media_List *mList)
{
	_Media_List *_mList = (_Media_List *)mList;

	_mList->window_count = 0;
}

void
ivug_data_delete_media_list(Media_List *mList)
{
	IV_ASSERT(mList != NULL);
	_Media_List *_mList = (_Media_List *)mList;

	MSG_SDATA_HIGH("Removing all media data");

	ivug_list_delete_items(_mList->header);

	ivug_data_filter_delete(_mList->filter);

	free(_mList);
}



#ifdef TRACE_CALLER
Media_Data *
dbg_ivug_data_get_data(Media_Item *media_item, const char *func, unsigned int line)
#else
Media_Data *
ivug_data_get_data(Media_Item *media_item)
#endif	// TRACE_CALLER
{
#ifdef TRACE_CALLER
	MSG_SDATA_HIGH("Called by %s(%d) Item=0x%08x", func, line, media_item);
#endif
	IV_ASSERT(media_item != NULL);

	Eina_List *item = media_item;

	return eina_list_data_get(item);

}

Media_Item *
ivug_data_get_first(Media_List *mList)
{
	IV_ASSERT(mList != NULL);
	_Media_List *_mList = (_Media_List *)mList;

	return (Media_Item *)_mList->header;
}


Media_Item *
ivug_data_get_next(Media_Item *media_item)
{
	IV_ASSERT(media_item != NULL);

	Eina_List *item = media_item;

	Eina_List *list;

	list = eina_list_next(item);

	if ( list == NULL )
	{
		MSG_SDATA_ERROR("Next item is NULL");
		return NULL;
	}

	return list;
}

Media_Item *
ivug_data_get_prev(Media_Item *media_item)
{
	IV_ASSERT(media_item != NULL);

	Eina_List *item = media_item;

	Eina_List *list;

	list = eina_list_prev(item);

	if ( list == NULL )
	{
		MSG_SDATA_ERROR("Prev item is NULL");
		return NULL;
	}

	return list;
}

Media_Item *
ivug_data_find_item(Eina_List *list, const char* fileuri, int /*OUT*/ *index)
{
	IV_ASSERT(list != NULL);
	IV_ASSERT(fileuri != NULL);

	Eina_List *item;
	Media_Data* mdata;

	unsigned int i = 1;

	EINA_LIST_FOREACH(list, item, mdata)
	{
		if(mdata == NULL)
		{
			MSG_SDATA_FATAL("sdata cannot be NULL");
			return NULL;
		}

		if (mdata->filepath)
		{
			if (strncmp(mdata->filepath, fileuri, strlen(mdata->filepath)) == 0)	//match
			{
				MSG_SDATA_HIGH("Founded Filepath=%s Index=%d", ivug_get_filename(mdata->filepath), i);
				*index = i;
				return item;
			}
		}

		if (mdata->fileurl)
		{
			if (strncmp(mdata->fileurl, fileuri, strlen(mdata->fileurl)) == 0)		//match
			{
				MSG_SDATA_HIGH("Founded Fileuri=%s Index=%d", mdata->fileurl, i);
				*index = i;
				return item;
			}
		}
		i++;
	}

	*index = 0;
	return NULL;
}


bool
ivug_data_delete_media_item(Media_List *mList, Media_Item *media_item)
{
	IV_ASSERT(mList != NULL);
	IV_ASSERT(media_item != NULL);

	_Media_List *_mList = (_Media_List *)mList;

	int ret;

	Media_Data* mdata;

	mdata = (Media_Data*)eina_list_data_get(media_item);

	IV_ASSERT(mdata != NULL);

	if (uuid_is_valid(mdata->mediaID) == true)
	{
		ret = minfo_delete_media_id(gGetMediaSVCHandle(), mdata->mediaID);

		if (ret == MB_SVC_ERROR_NONE)
		{
			if (mdata->filepath)
			{
				if(ivug_remove_file(mdata->filepath) == false)
				{
					MSG_SDATA_ERROR("unlink error , file path =%s", mdata->filepath);
				}
			}
		}
		else
		{
			MSG_SDATA_ERROR("minfo_delete_media_id faild=%d uuid=%s file=%s", ret, uuid_getchar(mdata->mediaID), mdata->filepath);
			goto ERROR;
		}

	}
	else
	{
		if (mdata->filepath)
		{
			if(ivug_remove_file(mdata->filepath) == false)
			{
				MSG_SDATA_ERROR("file remove error , file path =%s", mdata->filepath);
			}
		}
		else
		{
			MSG_SDATA_ERROR("delete faild file path is NULL");
			goto ERROR;
		}

	}

// TODO : Handle when current is deleted
	ivug_free_mediadata(mdata);		// Free data

	_mList->header = eina_list_remove_list(_mList->header, media_item);
	_mList->right_end--;

	return true;

ERROR:
	ivug_free_mediadata(mdata);		// Free data
	_mList->header = eina_list_remove_list(_mList->header, media_item);
	_mList->right_end--;

	MSG_SDATA_ERROR("ivug_data_delete_slide faild.");
	return false;

}

/* it delete start ~ end item */
/* if end is null, it delete until last */
bool
ivug_data_delete_window_list(Media_List *mList, Media_Item *start_item, Media_Item *end_item)
{
	IV_ASSERT(mList != NULL);
	IV_ASSERT(start_item != NULL);

	_Media_List *_mList = (_Media_List *)mList;

	Media_Item *current = start_item;
	Media_Item *next = NULL;

	Media_Data* mdata;

	do
	{
		MSG_SDATA_HIGH("delete window list");
		next = eina_list_next(current);

		mdata = (Media_Data*)eina_list_data_get(current);
		ivug_free_mediadata(mdata);		// Free data
		_mList->header = eina_list_remove_list(_mList->header, current);
		if(end_item == NULL)	/* delete right window */
		{
			/* TODO : _mList->right_end--; */
		}
		else	/* delete left window */
		{
			/* TODO : _mList->left_end++; */
		}
		current = next;
	}while(current && current != end_item);

	if(current) /* delete end item */
	{
		mdata = (Media_Data*)eina_list_data_get(current);
		ivug_free_mediadata(mdata);		// Free data
		_mList->header = eina_list_remove_list(_mList->header, current);
		_mList->left_end++;	/* this case is delete left window */
	}

	_mList->window_count--;

	return true;
}


void
ivug_data_delete_all_window_list(Media_List *mList)
{
	_Media_List *_mList = (_Media_List *)mList;

	ivug_data_delete_window_list(mList, _mList->header, NULL);
}


void ivug_data_append_media_item(Media_List *mList, Eina_List* list)
{
	IV_ASSERT(mList != NULL);

	_Media_List *_mList = (_Media_List *)mList;

	MSG_SDATA_HIGH("original : slide_list_count is %d", eina_list_count(_mList->header));
	if(_mList->window_count > 3)
	{
		/* TODO : delete window item */
		//ivug_data_delete_window_list(mList, _mList->header, _mList->contact_point);
		MSG_SDATA_HIGH("after delete : slide_list_count is %d", eina_list_count(_mList->header));
	}
	//_mList->contact_point = eina_list_last(_mList->header);

	_mList->header = eina_list_merge(_mList->header, list);
	MSG_SDATA_HIGH("merge next : slide_list_count is %d", eina_list_count(_mList->header));

	_mList->window_count++;
}

void ivug_data_prepend_media_item(Media_List *mList, Eina_List* list)
{
	IV_ASSERT(mList != NULL);

	_Media_List *_mList = (_Media_List *)mList;

	MSG_SDATA_HIGH("original : slide_list_count is %d", eina_list_count(_mList->header));
	if(_mList->window_count > 3)
	{
		/* TODO : delete window item */
		//ivug_data_delete_window_list(mList, _mList->contact_point, NULL);
		MSG_SDATA_HIGH("after delete : slide_list_count is %d", eina_list_count(_mList->header));
	}
	//_mList->contact_point = _mList->header;

	_mList->header = eina_list_merge(list, _mList->header);
	MSG_SDATA_HIGH("merge prev : slide_list_count is %d", eina_list_count(_mList->header));

	_mList->window_count++;
}


Load_State
ivug_data_set_window_loading(Media_List *mList, int cur_index, int total_count)
{
	Eina_List* slide_list_prev = NULL;
	Eina_List* slide_list_next = NULL;
	int start = -1;

	IV_ASSERT(mList != NULL);

	_Media_List *_mList = (_Media_List *)mList;

	/* load right widow */
	if(_mList->right_end < total_count-1 && cur_index-1 > _mList->right_end - _mList->window_reload_offset)
	{
		/* start index is basis for view's index(1~total) because +1 needed */
		start = (_mList->right_end+1) + (_mList->window_size/2+1);
		if(start > total_count)	// index is 1~total
		{
			start = total_count;
		}
		MSG_SDATA_HIGH("start = %d, _mList->right_end = %d", start, _mList->right_end);
		slide_list_next = _ivug_data_create_window_size_list(mList, start, total_count);
		if(slide_list_next != NULL)
		{
			ivug_data_append_media_item(mList, slide_list_next);
			return IVUG_SLIDER_LOADED_RIGHT;
		}
	}
	/* load left widow */
	else if(_mList->left_end > 0 && cur_index-1 < _mList->left_end + _mList->window_reload_offset)
	{
		/* start index is basis for view's index(1~total) because +1 needed */
		start = (_mList->left_end+1) - (_mList->window_size/2+1);
		if(start < 1)	// index is 1~total
		{
			start = 1;
		}
		MSG_SDATA_HIGH("start = %d, _mList->left_end = %d", start, _mList->left_end);
		slide_list_prev = _ivug_data_create_window_size_list(mList, start, total_count);
		if(slide_list_prev != NULL)
		{
			ivug_data_prepend_media_item(mList, slide_list_prev);
			return IVUG_SLIDER_LOADED_LEFT;
		}
	}
	return IVUG_SLIDER_LOADED_NONE;
}