Update for modification of db utilities.
[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 #define MEDIA_ROOT_PATH_SDCARD  tzplatform_mkpath(TZ_SYS_STORAGE,"sdcard")
32 void *funcHandle = NULL;
33
34 static void msg_print(int line, char *msg);
35
36 int (*svc_connect)                              (void ** handle, char ** err_msg);
37 int (*svc_disconnect)                   (void * handle, char ** err_msg);
38 int (*svc_check_item_exist)             (void* handle, const char *file_path, bool *modified, char ** err_msg);
39 int (*svc_insert_item_immediately)      (void* handle, const char *file_path, int storage_type, const char * mime_type, char ** err_msg);
40 int (*svc_set_folder_item_validity) (void * handle, const char * folder_path, int validity, int recursive, char ** err_msg);
41 int (*svc_delete_all_invalid_items_in_folder) (void * handle, const char *folder_path, char ** err_msg);
42
43 int __load_functions()
44 {
45         msg_print(__LINE__, "__load_functions");
46
47         funcHandle = dlopen (PLUGIN_SO_FILE_NAME, RTLD_LAZY);
48         if (!funcHandle) {
49                 fprintf (stderr,"error: %s\n", dlerror());
50         }
51
52         svc_connect                     = dlsym (funcHandle, "connect");
53         svc_disconnect          = dlsym (funcHandle, "disconnect");
54         svc_check_item_exist    = dlsym (funcHandle, "check_item_exist");
55         svc_insert_item_immediately     = dlsym (funcHandle, "insert_item_immediately");
56         svc_set_folder_item_validity    = dlsym (funcHandle, "set_folder_item_validity");
57         svc_delete_all_invalid_items_in_folder  = dlsym (funcHandle, "delete_all_invalid_items_in_folder");
58
59         if ( !svc_connect ||
60                  !svc_disconnect ||
61                  !svc_insert_item_immediately ||
62                 !svc_set_folder_item_validity ||
63                 !svc_delete_all_invalid_items_in_folder ||
64                  !svc_check_item_exist) {
65                 fprintf(stderr,"error: %s\n", dlerror());
66                 return -1;
67         }
68
69         return 0;
70 }
71
72 int __unload_functions(void)
73 {
74         msg_print(__LINE__, "__unload_functions");
75
76         if (funcHandle)
77         {
78                 dlclose (funcHandle);
79         }
80
81         return 0;
82 }
83
84 int main()
85 {
86         int ret = 0;
87         MediaSvcHandle * db_handle = NULL;
88         char * err_msg = NULL;
89         char path[1024] = {0,};
90         char type[1024] = {0,};
91
92         ret = __load_functions();
93         if(ret < 0) {
94                 msg_print(__LINE__, "__load_functions error");
95                 return -1;
96         } else {
97                 msg_print(__LINE__, "__load_functions success");
98         }
99
100         //db open ==================================================
101         ret = svc_connect(&db_handle, &err_msg);
102         if(ret < 0) {
103                 msg_print(__LINE__, "svc_connect error");
104                 if(err_msg != NULL) {
105                         printf("err_msg[%s]\n", err_msg);
106                         free(err_msg);
107                         err_msg = NULL;
108                 }
109                 __unload_functions();
110                 return -1;
111         } else {
112                 msg_print(__LINE__, "svc_connect success");
113         }
114
115 #if 1
116         ret = media_svc_create_table(db_handle);
117         if (ret < 0) {
118                 msg_print(__LINE__, "table already exists");
119         } else {
120                 msg_print(__LINE__, "table create success");
121         }
122 #endif
123
124 #if 1
125         while (1) {
126
127         printf("Enter path and mimetype ( ex. %s image ) : ", tzplatform_mkpath(TZ_USER_CONTENT, "a.jpg"));
128         scanf("%s %s", path, type);
129         bool modified = false;
130         //check_item_exist ============================================
131         ret = svc_check_item_exist(db_handle, path, &modified, &err_msg);
132         if(ret < 0) {
133                 msg_print(__LINE__, "svc_check_item_exist error");
134                 if(err_msg != NULL) {
135                         printf("err_msg[%s]\n", err_msg);
136                         free(err_msg);
137                         err_msg = NULL;
138                 }
139                 //__unload_functions();
140                 //return -1;
141         } else {
142                 if(modified)
143                         msg_print(__LINE__, "svc_check_item_exist success. Modified");
144                 else
145                         msg_print(__LINE__, "svc_check_item_exist success. Not modified");
146         }
147
148         // svc_check_item_exist ============================================
149         ret = svc_insert_item_immediately(db_handle, path, 0, type, &err_msg);
150         if(ret < 0) {
151                 msg_print(__LINE__, "svc_insert_item_immediately error");
152                 if(err_msg != NULL) {
153                         printf("err_msg[%s]\n", err_msg);
154                         free(err_msg);
155                         err_msg = NULL;
156                 }
157                 //__unload_functions();
158                 //return -1;
159         } else {
160                 msg_print(__LINE__, "svc_insert_item_immediately success");
161         }
162         } // End of While
163
164         ret = media_svc_insert_folder(db_handle, 0,  path);
165         if(ret < 0) {
166                 msg_print(__LINE__, "media_svc_insert_folder error ");
167         } else {
168                 msg_print(__LINE__, "media_svc_insert_folder success");
169         }
170 #endif
171
172         //folder test ==================================================
173         char *folder_path = tzplatform_mkpath(TZ_USER_CONTENT,"Sounds"));
174         ret = svc_set_folder_item_validity(db_handle, folder_path, 0, 1, &err_msg);
175         if(ret < 0) {
176                 msg_print(__LINE__, "svc_set_folder_item_validity error");
177                 if(err_msg != NULL) {
178                         printf("err_msg[%s]\n", err_msg);
179                         free(err_msg);
180                         err_msg = NULL;
181                 }
182         } else {
183                 msg_print(__LINE__, "svc_insert_item_immediately success");
184         }
185
186         ret = svc_delete_all_invalid_items_in_folder(db_handle, folder_path, &err_msg);
187         if(ret < 0) {
188                 msg_print(__LINE__, "svc_delete_all_invalid_items_in_folder error");
189                 if(err_msg != NULL) {
190                         printf("err_msg[%s]\n", err_msg);
191                         free(err_msg);
192                         err_msg = NULL;
193                 }
194         } else {
195                 msg_print(__LINE__, "svc_insert_item_immediately success");
196         }
197
198         //db close ==================================================
199         ret = svc_disconnect(db_handle, &err_msg);
200         if(ret < 0) {
201                 msg_print(__LINE__, "svc_disconnect error");
202                 if(err_msg != NULL) {
203                         printf("err_msg[%s]\n", err_msg);
204                         free(err_msg);
205                         err_msg = NULL;
206                 }
207                 __unload_functions();
208                 return -1;
209         } else {
210                 msg_print(__LINE__, "svc_disconnect success");
211         }
212
213         __unload_functions();
214
215         return 0;
216 }
217
218
219 static void msg_print(int line, char *msg)
220 {
221         fprintf(stderr, "[%d]%s\n", line, msg);
222 }
223