Apply tizen coding rule
[platform/core/multimedia/libmedia-service.git] / test / plugin / media_svc_plugin_test.c
1 /*
2  * libmedia-service
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Hyunjun Ko <zzoon.ko@samsung.com>, Haejeong Kim <backto.kim@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <dlfcn.h>
25 #include <stdbool.h>
26 #include <media-svc.h>
27
28 #include <tzplatform_config.h>
29
30 #define PLUGIN_SO_FILE_NAME     "/usr/lib/libmedia-content-plugin.so"
31 void *funcHandle = NULL;
32
33 static void msg_print(int line, char *msg);
34
35 int (*svc_connect)(void **handle, char **err_msg);
36 int (*svc_disconnect)(void *handle, char **err_msg);
37 int (*svc_check_item_exist)(void *handle, const char *file_path, bool *modified, char **err_msg);
38 int (*svc_insert_item_immediately)(void *handle, const char *file_path, int storage_type, const char *mime_type, char **err_msg);
39 int (*svc_set_folder_item_validity)(void *handle, const char *folder_path, int validity, int recursive, char **err_msg);
40 int (*svc_delete_all_invalid_items_in_folder)(void *handle, const char *folder_path, char **err_msg);
41
42 int __load_functions()
43 {
44         msg_print(__LINE__, "__load_functions");
45
46         funcHandle = dlopen(PLUGIN_SO_FILE_NAME, RTLD_LAZY);
47         if (!funcHandle)
48                 fprintf(stderr, "error: %s\n", dlerror());
49
50         svc_connect                     = dlsym(funcHandle, "connect");
51         svc_disconnect          = dlsym(funcHandle, "disconnect");
52         svc_check_item_exist    = dlsym(funcHandle, "check_item_exist");
53         svc_insert_item_immediately     = dlsym(funcHandle, "insert_item_immediately");
54         svc_set_folder_item_validity    = dlsym(funcHandle, "set_folder_item_validity");
55         svc_delete_all_invalid_items_in_folder  = dlsym(funcHandle, "delete_all_invalid_items_in_folder");
56
57         if (!svc_connect || !svc_disconnect || !svc_insert_item_immediately || !svc_set_folder_item_validity || !svc_delete_all_invalid_items_in_folder || !svc_check_item_exist) {
58                 fprintf(stderr, "error: %s\n", dlerror());
59                 return -1;
60         }
61
62         return 0;
63 }
64
65 int __unload_functions(void)
66 {
67         msg_print(__LINE__, "__unload_functions");
68
69         if (funcHandle)
70                 dlclose(funcHandle);
71
72         return 0;
73 }
74
75 int main()
76 {
77         int ret = 0;
78         MediaSvcHandle *db_handle = NULL;
79         char *err_msg = NULL;
80         char path[1024] = {0, };
81         char type[1024] = {0, };
82
83         ret = __load_functions();
84         if (ret < 0) {
85                 msg_print(__LINE__, "__load_functions error");
86                 return -1;
87         } else {
88                 msg_print(__LINE__, "__load_functions success");
89         }
90
91         /*db open ================================================== */
92         ret = svc_connect(&db_handle, &err_msg);
93         if (ret < 0) {
94                 msg_print(__LINE__, "svc_connect error");
95                 if (err_msg != NULL) {
96                         printf("err_msg[%s]\n", err_msg);
97                         free(err_msg);
98                         err_msg = NULL;
99                 }
100                 __unload_functions();
101                 return -1;
102         } else {
103                 msg_print(__LINE__, "svc_connect success");
104         }
105
106 #if 1
107         ret = media_svc_create_table(tzplatform_getuid(TZ_USER_NAME));
108         if (ret < 0)
109                 msg_print(__LINE__, "table already exists");
110         else
111                 msg_print(__LINE__, "table create success");
112 #endif
113
114 #if 1
115         while (1) {
116
117                 printf("Enter path and mimetype ( ex. %s image ) : ", tzplatform_mkpath(TZ_USER_CONTENT, "a.jpg"));
118                 scanf("%s %s", path, type);
119                 bool modified = false;
120                 /*check_item_exist ============================================ */
121                 ret = svc_check_item_exist(db_handle, path, &modified, &err_msg);
122                 if (ret < 0) {
123                         msg_print(__LINE__, "svc_check_item_exist error");
124                         if (err_msg != NULL) {
125                                 printf("err_msg[%s]\n", err_msg);
126                                 free(err_msg);
127                                 err_msg = NULL;
128                         }
129                         /*__unload_functions(); */
130                         /*return -1; */
131                 } else {
132                         if (modified)
133                                 msg_print(__LINE__, "svc_check_item_exist success. Modified");
134                         else
135                                 msg_print(__LINE__, "svc_check_item_exist success. Not modified");
136                 }
137
138                 /* svc_check_item_exist ============================================ */
139                 ret = svc_insert_item_immediately(db_handle, path, 0, type, &err_msg);
140                 if (ret < 0) {
141                         msg_print(__LINE__, "svc_insert_item_immediately error");
142                         if (err_msg != NULL) {
143                                 printf("err_msg[%s]\n", err_msg);
144                                 free(err_msg);
145                                 err_msg = NULL;
146                         }
147                         /*__unload_functions(); */
148                         /*return -1; */
149                 } else {
150                         msg_print(__LINE__, "svc_insert_item_immediately success");
151                 }
152         } /* End of While */
153
154         ret = media_svc_insert_folder(db_handle, 0, path);
155         if (ret < 0)
156                 msg_print(__LINE__, "media_svc_insert_folder error ");
157         else
158                 msg_print(__LINE__, "media_svc_insert_folder success");
159 #endif
160
161         /*folder test ================================================== */
162         char *folder_path = tzplatform_mkpath(TZ_USER_CONTENT, "Sounds");
163         ret = svc_set_folder_item_validity(db_handle, folder_path, 0, 1, &err_msg);
164         if (ret < 0) {
165                 msg_print(__LINE__, "svc_set_folder_item_validity error");
166                 if (err_msg != NULL) {
167                         printf("err_msg[%s]\n", err_msg);
168                         free(err_msg);
169                         err_msg = NULL;
170                 }
171         } else {
172                 msg_print(__LINE__, "svc_insert_item_immediately success");
173         }
174
175         ret = svc_delete_all_invalid_items_in_folder(db_handle, folder_path, &err_msg);
176         if (ret < 0) {
177                 msg_print(__LINE__, "svc_delete_all_invalid_items_in_folder error");
178                 if (err_msg != NULL) {
179                         printf("err_msg[%s]\n", err_msg);
180                         free(err_msg);
181                         err_msg = NULL;
182                 }
183         } else {
184                 msg_print(__LINE__, "svc_insert_item_immediately success");
185         }
186
187         /*db close ================================================== */
188         ret = svc_disconnect(db_handle, &err_msg);
189         if (ret < 0) {
190                 msg_print(__LINE__, "svc_disconnect error");
191                 if (err_msg != NULL) {
192                         printf("err_msg[%s]\n", err_msg);
193                         free(err_msg);
194                         err_msg = NULL;
195                 }
196                 __unload_functions();
197                 return -1;
198         } else {
199                 msg_print(__LINE__, "svc_disconnect success");
200         }
201
202         __unload_functions();
203
204         return 0;
205 }
206
207
208 static void msg_print(int line, char *msg)
209 {
210         fprintf(stderr, "[%d]%s\n", line, msg);
211 }
212