summaryrefslogtreecommitdiff
path: root/src/doc-local-data.c
blob: e4dfac35e143d5b73776fb71192d2ba0f48d4b00 (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
/*
* Copyright (c) 2000-2015 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 <stdio.h>
#include <media_content.h>
#include <media_info.h>
#include <glib.h>
#include <string.h>
#include "doc-data-util.h"
#include "doc-local-data.h"
#include "doc-debug.h"


static bool _doc_local_data_clone_folder(media_folder_h folder,
										 bool b_path,
										 doc_folder_s ** pfolder)
{
	DOC_CHECK_FALSE(folder);
	DOC_CHECK_FALSE(pfolder);
	doc_folder_s *tmp_folder = NULL;

	tmp_folder = (doc_folder_s *) calloc(1, sizeof(doc_folder_s));
	if (tmp_folder == NULL) {
		doc_dbgE("Failed to calloc!");
		return false;
	}
	tmp_folder->gtype = DOC_TYPE_FOLDER;

	if (b_path) {
		if (media_folder_get_path(folder, &(tmp_folder->path)) !=
			MEDIA_CONTENT_ERROR_NONE) {
			doc_dbgE("Get folder path failed!");
			goto DOC_LOCAL_FAILED;
		}
	}
	if (media_folder_get_folder_id(folder, &(tmp_folder->uuid)) !=
		MEDIA_CONTENT_ERROR_NONE) {
		doc_dbgE("Get folder id failed!");
		goto DOC_LOCAL_FAILED;
	}
	if (media_folder_get_modified_time(folder, &(tmp_folder->mtime)) !=
		MEDIA_CONTENT_ERROR_NONE) {
		doc_dbgE("Get modified time failed!");
		goto DOC_LOCAL_FAILED;
	}

	media_content_storage_e storage_type;
	if (media_folder_get_storage_type(folder, &storage_type) !=
		MEDIA_CONTENT_ERROR_NONE) {
		doc_dbgE("Get storage type failed!");
		goto DOC_LOCAL_FAILED;
	}

	if (storage_type == MEDIA_CONTENT_STORAGE_INTERNAL) {	/* The device's internal storage */
		tmp_folder->type = DOC_PHONE;
	} else if (storage_type == MEDIA_CONTENT_STORAGE_EXTERNAL) {	/* The device's external storage */
		tmp_folder->type = DOC_MMC;
	} else {
		doc_dbgE("Undefined mode[%d]!", storage_type);
	}

	if (media_folder_get_name(folder, &(tmp_folder->display_name)) !=
		MEDIA_CONTENT_ERROR_NONE) {
		doc_dbgE("Get folder name failed!");
		goto DOC_LOCAL_FAILED;
	}

	*pfolder = tmp_folder;
	return true;

  DOC_LOCAL_FAILED:

	doc_data_type_free_item((void **) (&tmp_folder));
	*pfolder = NULL;
	return false;
}

static bool _doc_local_data_get_media_list_cb(media_info_h media,
											  void *data)
{
	DOC_CHECK_FALSE(data);
	doc_transfer_data_s *td = (doc_transfer_data_s *) data;
	DOC_CHECK_FALSE(td->userdata);
	DOC_CHECK_FALSE(media);
	doc_media_s *item = NULL;

	Eina_List **elist = (Eina_List **) (td->userdata);

	if (doc_data_util_clone_media(media, &item, td->with_meta)) {
		*elist = eina_list_append(*elist, item);
		return true;
	} else {
		return false;
	}
}

int doc_local_data_connect(void)
{
	int ret = -1;

	ret = media_content_connect();
	if (ret == MEDIA_CONTENT_ERROR_NONE) {
		doc_dbg("DB connection is success");
		return 0;
	} else {
		doc_dbgE("DB connection is failed!");
		return -1;
	}
}

int doc_local_data_disconnect(void)
{
	int ret = -1;

	ret = media_content_disconnect();
	if (ret == MEDIA_CONTENT_ERROR_NONE) {
		doc_dbg("DB disconnection is success");
		return 0;
	} else {
		doc_dbgE("DB disconnection is failed!");
		return -1;
	}
}

static bool _doc_local_data_get_folder_list_cb(media_folder_h folder,
											   void *data)
{
	DOC_CHECK_FALSE(data);
	doc_transfer_data_s *tmp_data = (doc_transfer_data_s *) data;
	DOC_CHECK_FALSE(tmp_data->userdata);
	DOC_CHECK_FALSE(folder);
	Eina_List **elist = (Eina_List **) (tmp_data->userdata);
	doc_folder_s *tmp_folder = NULL;

	char *path = NULL;
	if (media_folder_get_path(folder, &path) != MEDIA_CONTENT_ERROR_NONE) {
		doc_dbgE("Get folder path failed!");
		return false;
	}
	DOC_CHECK_FALSE(path);

	if (tmp_data->list_type != DOC_FOLDER_LIST_PATH) {
		if (!g_strcmp0(DOC_CAMERA_PATH_PHONE, path) ||
			!g_strcmp0(DOC_CAMERA_PATH_MMC, path) ||
			!g_strcmp0(DOC_DOWNLOADS_PATH, path)) {
			doc_dbgW("Camera or Downloads!");
			DOC_FREE(path);
			return true;
		}
	}

	_doc_local_data_clone_folder(folder, false, &tmp_folder);
	if (tmp_folder == NULL) {
		DOC_FREE(path);
		doc_dbgE("Failed clone folder!");
		return false;
	}
	tmp_folder->path = path;

	*elist = eina_list_append(*elist, tmp_folder);
	return true;
}

static int _doc_local_data_get_folder_list(doc_filter_s * condition,
										   Eina_List ** elilst)
{
	DOC_CHECK_VAL(elilst, -1);
	DOC_CHECK_VAL(condition, -1);
	int ret = -1;
	filter_h filter = NULL;

	ret = doc_data_util_create_filter(condition, &filter);
	if (ret != 0) {
		doc_dbgE("Create filter failed[%d]!", ret);
		return -1;
	}

	doc_transfer_data_s tran_data;
	memset(&tran_data, 0x00, sizeof(doc_transfer_data_s));
	tran_data.userdata = (void **) elilst;
	tran_data.folder_id = NULL;
	tran_data.with_meta = false;
	tran_data.list_type = condition->list_type;

	doc_dbg("Get folders--start");
	ret = media_folder_foreach_folder_from_db(filter,
											  _doc_local_data_get_folder_list_cb,
											  &tran_data);
	doc_dbg("Get folders--over");

	doc_data_util_destroy_filter(filter);

	if (ret != MEDIA_CONTENT_ERROR_NONE) {
		doc_dbgE("Get folders failed[%d]!", ret);
		return -1;
	}

	return 0;
}

int doc_local_data_get_folder_by_path(char *path, doc_folder_s ** folder)
{
	DOC_CHECK_VAL(path, -1);
	DOC_CHECK_VAL(folder, -1);
	int ret = -1;
	Eina_List *list = NULL;
	doc_filter_s condition;
	doc_folder_s *_item = NULL;
	int i = 0;

	if (strlen(path) <= 0) {
		doc_dbgE("Invalid path!");
		return -1;
	}
	doc_dbg("path: %s", path);

	memset(&condition, 0x00, sizeof(doc_filter_s));
	condition.collate_type = MEDIA_CONTENT_COLLATE_NOCASE;
	condition.sort_type = MEDIA_CONTENT_ORDER_DESC;
	condition.offset = -1;
	condition.count = -1;
	condition.with_meta = false;
	condition.list_type = DOC_FOLDER_LIST_PATH;

	snprintf(condition.cond, CONDITION_LENGTH, "%s AND %s=\'%s\'",
			 DOC_CONDITION_LOCAL_DOCUMENT, FOLDER_PATH, path);

	ret = _doc_local_data_get_folder_list(&condition, &list);
	if (ret != 0 || NULL == list) {
		doc_dbgE("Failed to get folder list[%d]!", ret);
		ret = -1;
	} else if (NULL != list) {
		*folder = eina_list_nth(list, 0);
		i = 1;
		ret = 0;
	}

	/* Free other items */
	if (list) {
		int len = eina_list_count(list);
		doc_dbg("len: %d", len);

		for (; i < len; i++) {
			_item = eina_list_nth(list, i);
			doc_data_type_free_item((void **) (&_item));
		}

		eina_list_free(list);
	}

	return ret;
}

int doc_local_data_get_all_media_count(doc_filter_s * condtion,
									   int *item_cnt)
{
	DOC_CHECK_VAL(condtion, -1);
	DOC_CHECK_VAL(item_cnt, -1);
	int ret = -1;
	filter_h filter = NULL;

	ret = doc_data_util_create_filter(condtion, &filter);
	if (ret != 0) {
		doc_dbgE("Create filter failed[%d]!", ret);
		return -1;
	}

	doc_dbg("Get media count--start");
	ret = media_info_get_media_count_from_db(filter, item_cnt);
	doc_dbg("Get media count--over");

	doc_data_util_destroy_filter(filter);

	if (ret != MEDIA_CONTENT_ERROR_NONE) {
		doc_dbgE("Get media count failed[d]!", ret);
		return -1;
	}

	return 0;
}

int doc_local_data_get_all_folders_media_list(doc_filter_s * condition,
											  Eina_List ** elist)
{
	DOC_CHECK_VAL(elist, -1);
	DOC_CHECK_VAL(condition, -1);
	int ret = -1;
	filter_h filter = NULL;

	ret = doc_data_util_create_filter(condition, &filter);
	if (ret != 0) {
		doc_dbgE("Create filter failed!");
		return -1;
	}

	doc_transfer_data_s tran_data;
	memset(&tran_data, 0x00, sizeof(doc_transfer_data_s));
	tran_data.userdata = (void **) elist;
	tran_data.folder_id = NULL;
	tran_data.with_meta = condition->with_meta;

	doc_dbg("Get all medias--start");
	ret = media_info_foreach_media_from_db(filter,
										   _doc_local_data_get_media_list_cb,
										   &tran_data);
	doc_dbg("Get all medias--over");

	doc_data_util_destroy_filter(filter);

	if (ret != MEDIA_CONTENT_ERROR_NONE) {
		doc_dbgE("Get all medias failed[d]!", ret);
		return -1;
	}

	return 0;
}