Merge "binding: media: free sqlite3 prepared query statement" into dab
[apps/mediaplayer.git] / binding / mediaplayer-manager.c
1 /*
2  *  Copyright 2017 Konsulko Group
3  *
4  *  Based on bluetooth-manager.c
5  *   Copyright 2016 ALPS ELECTRIC CO., LTD.
6  *
7  *   Licensed under the Apache License, Version 2.0 (the "License");
8  *   you may not use this file except in compliance with the License.
9  *   You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  *   Unless required by applicable law or agreed to in writing, software
14  *   distributed under the License is distributed on an "AS IS" BASIS,
15  *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *   See the License for the specific language governing permissions and
17  *   limitations under the License.
18  */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26
27 #include <pthread.h>
28 #include <glib.h>
29 #include <gio/gio.h>
30 #include <glib-object.h>
31 #include <sqlite3.h>
32
33 #include "mediaplayer-manager.h"
34
35 static Binding_RegisterCallback_t g_RegisterCallback = { 0 };
36 static stMediaPlayerManage MediaPlayerManage = { 0 };
37
38 /* ------ LOCAL  FUNCTIONS --------- */
39 void ListLock() {
40     g_mutex_lock(&(MediaPlayerManage.m));
41 }
42
43 void ListUnlock() {
44     g_mutex_unlock(&(MediaPlayerManage.m));
45 }
46
47 void DebugTraceSendMsg(int level, gchar* message)
48 {
49 #ifdef LOCAL_PRINT_DEBUG
50     switch (level)
51     {
52             case DT_LEVEL_ERROR:
53                 g_print("[E]");
54                 break;
55
56             case DT_LEVEL_WARNING:
57                 g_print("[W]");
58                 break;
59
60             case DT_LEVEL_NOTICE:
61                 g_print("[N]");
62                 break;
63
64             case DT_LEVEL_INFO:
65                 g_print("[I]");
66                 break;
67
68             case DT_LEVEL_DEBUG:
69                 g_print("[D]");
70                 break;
71
72             default:
73                 g_print("[-]");
74                 break;
75     }
76
77     g_print("%s",message);
78 #endif
79
80     if (message) {
81         g_free(message);
82     }
83
84 }
85
86 GList* media_lightmediascanner_scan(void)
87 {
88     sqlite3 *conn;
89     sqlite3_stmt *res;
90     GList *list = NULL;
91     const char *tail;
92     const gchar *db_path;
93     int ret = 0;
94
95     db_path = scanner1_get_data_base_path(MediaPlayerManage.lms_proxy);
96
97     ret = sqlite3_open(db_path, &conn);
98     if (ret) {
99         LOGE("Cannot open SQLITE database: '%s'\n", db_path);
100         return NULL;
101     }
102
103     ret = sqlite3_prepare_v2(conn, SQL_QUERY, strlen(SQL_QUERY) + 1, &res, &tail);
104     if (ret) {
105         LOGE("Cannot execute query '%s'\n", SQL_QUERY);
106         return NULL;
107     }
108
109     while (sqlite3_step(res) == SQLITE_ROW) {
110         struct stat buf;
111         const char *path = (const char *) sqlite3_column_text(res, 0);
112
113         ret = stat(path, &buf);
114         if (ret)
115             continue;
116
117         list = g_list_append(list, g_strdup_printf("file://%s", path));
118     }
119
120     sqlite3_finalize(res);
121
122     return list;
123 }
124
125
126 static void
127 on_interface_proxy_properties_changed (GDBusProxy *proxy,
128                                     GVariant *changed_properties,
129                                     const gchar* const  *invalidated_properties)
130 {
131     GVariantIter iter;
132     const gchar *key;
133     GVariant *subValue;
134     const gchar *pInterface;
135     GList *list;
136
137     pInterface = g_dbus_proxy_get_interface_name (proxy);
138
139     if (0 != g_strcmp0(pInterface, LIGHTMEDIASCANNER_INTERFACE))
140         return;
141
142     g_variant_iter_init (&iter, changed_properties);
143     while (g_variant_iter_next (&iter, "{&sv}", &key, &subValue))
144     {
145         gboolean val;
146         if (0 == g_strcmp0(key,"IsScanning")) {
147             g_variant_get(subValue, "b", &val);
148             if (val == TRUE)
149                 return;
150         } else if (0 == g_strcmp0(key, "WriteLocked")) {
151             g_variant_get(subValue, "b", &val);
152             if (val == TRUE)
153                 return;
154         }
155     }
156
157     ListLock();
158
159     list = media_lightmediascanner_scan();
160
161     if (list != NULL && g_RegisterCallback.binding_device_added)
162         g_RegisterCallback.binding_device_added(list);
163
164     g_list_free_full(list, g_free);
165
166     ListUnlock();
167 }
168
169 static int MediaPlayerDBusInit(void)
170 {
171     GError *error = NULL;
172
173     MediaPlayerManage.lms_proxy = scanner1_proxy_new_for_bus_sync(
174         G_BUS_TYPE_SESSION, G_DBUS_PROXY_FLAGS_NONE, LIGHTMEDIASCANNER_SERVICE,
175         LIGHTMEDIASCANNER_PATH, NULL, &error);
176
177     if (MediaPlayerManage.lms_proxy == NULL) {
178         LOGE("Create LightMediaScanner Proxy failed\n");
179         return -1;
180     }
181
182     g_signal_connect (MediaPlayerManage.lms_proxy,
183                       "g-properties-changed",
184                       G_CALLBACK (on_interface_proxy_properties_changed),
185                       NULL);
186
187     return 0;
188 }
189
190 static void *media_event_loop_thread(void *unused)
191 {
192     GMainLoop *loop = g_main_loop_new(NULL, FALSE);
193     int ret;
194
195     ret = MediaPlayerDBusInit();
196     if (ret == 0) {
197         LOGD("g_main_loop_run\n");
198         g_main_loop_run(loop);
199     }
200
201     g_main_loop_unref(loop);
202
203     return NULL;
204 }
205
206 void
207 unmount_cb (GFileMonitor      *mon,
208             GFile             *file,
209             GFile             *other_file,
210             GFileMonitorEvent  event,
211             gpointer           udata)
212 {
213     gchar *path = g_file_get_path(file);
214     gchar *uri = g_strconcat("file://", path, NULL);
215     g_free(path);
216
217     ListLock();
218
219     if (g_RegisterCallback.binding_device_removed &&
220         event == G_FILE_MONITOR_EVENT_DELETED) {
221             g_RegisterCallback.binding_device_removed(uri);
222     }
223
224     ListUnlock();
225     g_free(uri);
226 }
227
228 /*
229  * Create MediaPlayer Manager Thread
230  * Note: mediaplayer-api should do MediaPlayerManagerInit() before any other 
231  *       API calls
232  * Returns: 0 - success or error conditions
233  */
234 int MediaPlayerManagerInit() {
235     pthread_t thread_id;
236     GFile *file;
237     GFileMonitor *mon;
238
239     g_mutex_init(&(MediaPlayerManage.m));
240
241     file = g_file_new_for_path("/media");
242     g_assert(file != NULL);
243
244     mon = g_file_monitor (file, G_FILE_MONITOR_NONE, NULL, NULL);
245     g_assert(mon != NULL);
246     g_signal_connect (mon, "changed", G_CALLBACK(unmount_cb), NULL);
247
248     pthread_create(&thread_id, NULL, media_event_loop_thread, NULL);
249
250     return 0;
251 }
252
253 /*
254  * Register MediaPlayer Manager Callback functions
255  */
256 void BindingAPIRegister(const Binding_RegisterCallback_t* pstRegisterCallback)
257 {
258     if (NULL != pstRegisterCallback)
259     {
260         if (NULL != pstRegisterCallback->binding_device_added)
261         {
262             g_RegisterCallback.binding_device_added =
263                 pstRegisterCallback->binding_device_added;
264         }
265
266         if (NULL != pstRegisterCallback->binding_device_removed)
267         {
268             g_RegisterCallback.binding_device_removed =
269                 pstRegisterCallback->binding_device_removed;
270         }
271     }
272 }