summaryrefslogtreecommitdiff
path: root/src/doc-db-update.c
blob: c983834aea8e67adb95baf5ea9a48d68198cda8d (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
/*
* 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 <media_content.h>
#include "attach-panel-document.h"
#include "doc-data.h"
#include "doc-local-data.h"
#include "doc-util.h"
#include "doc-debug.h"

#define DOC_MONITOE_TIME_DELAY 1.0f

struct doc_db_noti_t {
	Ecore_Timer *db_timer; /*For update db data*/
	/*Make ture calling db update callback after other operations complete*/
	Ecore_Idler *db_idl;
	//media_content_noti_h cloud_h; /* Notify handle fro cloud content updating in DB */
	media_content_db_update_item_type_e update_item;
	media_content_db_update_type_e update_type;
	int count; /* All media count got from DB */
};

static Eina_Bool _doc_db_update_idler(void *data)
{
	DOC_CHECK_FALSE(data);
	doc_ugdata *ugd = (doc_ugdata *)data;
	DOC_CHECK_FALSE(ugd->db_noti_d);
	doc_db_noti_s *db_noti = ugd->db_noti_d;
	doc_update_view(ugd, DOC_UPDATE_VIEW_INOTIFY);
	DOC_IF_DEL_IDLER(db_noti->db_idl);
	return ECORE_CALLBACK_CANCEL;
}

static Eina_Bool _doc_db_update_timer_cb(void *data)
{
	DOC_CHECK_FALSE(data);
	doc_ugdata *ugd = (doc_ugdata *)data;
	DOC_CHECK_FALSE(ugd->db_noti_d);
	doc_db_noti_s *db_noti = ugd->db_noti_d;

	DOC_IF_DEL_IDLER(db_noti->db_idl);
	db_noti->db_idl = ecore_idler_add(_doc_db_update_idler, data);

	DOC_IF_DEL_TIMER(db_noti->db_timer);
	return ECORE_CALLBACK_CANCEL;
}

static int _doc_db_update_add_timer(doc_ugdata *ugd)
{
	DOC_CHECK_VAL(ugd, -1);
	DOC_CHECK_VAL(ugd->db_noti_d, -1);
	doc_db_noti_s *db_noti = ugd->db_noti_d;

	DOC_IF_DEL_TIMER(db_noti->db_timer);
	db_noti->db_timer = ecore_timer_add(DOC_MONITOE_TIME_DELAY,
	                                    _doc_db_update_timer_cb, ugd);
	doc_dbgW("TIMER[1.0f] added!");
	return 0;
}

static int _doc_db_update_op(media_content_error_e error, int pid,
                             media_content_db_update_item_type_e update_item,
                             media_content_db_update_type_e update_type,
                             media_content_type_e media_type, char *uuid,
                             char *path, char *mime_type, void *data)
{
	DOC_CHECK_VAL(data, -1);
	doc_ugdata *ugd = (doc_ugdata *)data;
	DOC_CHECK_VAL(ugd->db_noti_d, -1);
	doc_db_noti_s *db_noti = ugd->db_noti_d;

	if (MEDIA_CONTENT_ERROR_NONE != error) {
		doc_dbgE("Update db error[%d]!", error);
		return -1;
	}
	if (update_item == MEDIA_ITEM_FILE &&
			MEDIA_CONTENT_TYPE_OTHERS != media_type) {
		doc_dbg("Media type is wrong");
		return -1;
	} else if (update_item == MEDIA_ITEM_DIRECTORY && media_type == -1) {
		/* Batch operation, DB wouldn't return media type  */
		int cnt = 0;
		int ret = -1;
		ret = doc_data_get_item_cnt(ugd, &cnt);
		if (ret != 0 || cnt == 0) {
			doc_dbgE("Empty!");
		}
		doc_dbg("old: %d, new: %d", db_noti->count, cnt);
		if (cnt == db_noti->count) {
			doc_dbg("Nothing changed");
			if (path &&
			        !strcmp(path, DOC_ROOT_PATH_MMC)) {
				doc_dbg("MMC insert or remove!");
			} else if (path) {
				doc_folder_s *album = NULL;
				doc_local_data_get_folder_by_path(path, &album);
				if (!album) {
					return -1;
				} else {
					doc_data_type_free_geitem((void **)&album);
					doc_dbgW("Updated album contains images");
				}
			} else {
				return -1;
			}
		} else {
			db_noti->count = cnt;
		}
	}

	db_noti->update_item = update_item;
	db_noti->update_type = update_type;

	_doc_db_update_add_timer(ugd);
	return 0;
}

static void _doc_db_update_cb(media_content_error_e error, int pid,
                              media_content_db_update_item_type_e update_item,
                              media_content_db_update_type_e update_type,
                              media_content_type_e media_type, char *uuid,
                              char *path, char *mime_type, void *data)
{
	doc_dbg("update_item[%d], update_type[%d], media_type[%d]", update_item,
	       update_type, media_type);
	DOC_CHECK(data);
	doc_dbg("uuid[%s], path[%s]", uuid, path);
	_doc_db_update_op(error, pid, update_item, update_type, media_type,
	                  uuid, path, mime_type, data);
}

bool doc_db_update_reg_cb(doc_ugdata *ugd)
{
	DOC_CHECK_FALSE(ugd);
	/*Init data*/
	doc_db_noti_s *db_noti = (doc_db_noti_s *)calloc(1, sizeof(doc_db_noti_s));
	DOC_CHECK_FALSE(db_noti);
	ugd->db_noti_d = db_noti;

	int cnt = 0;
	doc_data_get_item_cnt(ugd, &cnt);
	db_noti->count = cnt;

	int ret = -1;

	doc_dbg("Set db updated callback");
	ret = media_content_set_db_updated_cb(_doc_db_update_cb, ugd);
	if (ret != MEDIA_CONTENT_ERROR_NONE) {
		doc_dbgE("Set db updated cb failed[%d]!", ret);
	}
	return true;
}

bool doc_db_update_finalize(doc_ugdata *ugd)
{
	int ret = -1;
	doc_dbg("Unset db updated callback");

	ret = media_content_unset_db_updated_cb();
	if (ret != MEDIA_CONTENT_ERROR_NONE) {
		doc_dbgE("UNSet db updated cb failed[%d]!", ret);
	}

	DOC_CHECK_FALSE(ugd);
	DOC_CHECK_FALSE(ugd->db_noti_d);
	doc_db_noti_s *db_noti = ugd->db_noti_d;

	DOC_IF_DEL_TIMER(db_noti->db_timer);
	DOC_IF_DEL_IDLER(db_noti->db_idl);
	DOC_FREE(ugd->db_noti_d);
	return true;
}