apply FSL license
[apps/core/preloaded/video-player.git] / src / mp-video-subtitle-ctrl.c
1 /*
2  * Copyright 2012  Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *     http://www.tizenopensource.org/license
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17
18
19 #include <vconf.h>
20 #include <vconf-keys.h>
21 #include <mm_sound.h>
22 #include <Ecore_Evas.h>
23 #include <Elementary.h>
24 #include <appcore-efl.h>
25 #include <appcore-common.h>
26
27 #include "mp-util.h"
28 #include "mp-video-log.h"
29 #include "video-player.h"
30 #include "mp-video-subtitle-ctrl.h"
31
32 #define MP_VIDEO_SUBTITLE_MENU_NUM              2
33 #define VCONFKEY_GALLERY_SUBTITLE_STATE         "db/setting/gallery/subtitle_state" //boolean
34
35 static int bSubtitleSettingState = FALSE;
36 static bool bExistSubtitleFile = FALSE;
37 static char szSubtitleFileUri[STR_LEN_MAX] = { 0 };
38
39 static char szSubTitleExtWildkey[MP_SUBTITLE_TYPE_MAX][STR_LEN_MAX] = { ".srt", ".sub", ".smi" };
40
41
42 /*
43  * Internal function
44  */
45 bool MpVideoSubtitleCtrlCheckSubtitleFile(char *szMediaUriPath)
46 {
47         if (!bSubtitleSettingState) {
48                 VideoLogInfo("Subtitle .");
49                 return FALSE;
50         }
51
52         if (!szMediaUriPath) {
53                 VideoLogInfo("No Exist szMediaUri.");
54                 return FALSE;
55         }
56
57         if (strlen(szMediaUriPath) < 1) {
58                 VideoLogInfo("No Exist szMediaUri.");
59                 return FALSE;
60         }
61
62         VideoLogInfo("");
63
64         struct stat buf;
65         int nCount = 0;
66         int nStrLength = 0;
67
68         bExistSubtitleFile = FALSE;
69         memset(szSubtitleFileUri, 0, STR_LEN_MAX);
70
71         for (nCount = 0; nCount < MP_SUBTITLE_TYPE_MAX; nCount++) {
72                 nStrLength = strcspn(szMediaUriPath, ".");
73                 strncpy(szSubtitleFileUri, szMediaUriPath, nStrLength);
74                 strcat(szSubtitleFileUri, szSubTitleExtWildkey[nCount]);
75
76                 if (!stat(szSubtitleFileUri, &buf)) {
77                         VideoLogInfo("Success finding subtitle file. - %d", nCount);
78                         bExistSubtitleFile = TRUE;
79                         break;
80                 } else {
81                         memset(szSubtitleFileUri, 0, STR_LEN_MAX);
82                 }
83         }
84
85         if (!bExistSubtitleFile) {
86                 VideoLogInfo("Fail finding subtitle file.");
87         }
88
89         return bExistSubtitleFile;
90 }
91
92 void MpVideoSubtitleCtrlClear(void)
93 {
94         VideoLogInfo("");
95
96         bExistSubtitleFile = FALSE;
97         bSubtitleSettingState = FALSE;
98
99         memset(szSubtitleFileUri, 0, STR_LEN_MAX);
100 }
101
102 static void MpVideoSubtitleCtrlStateChangeCb(keynode_t *pKeyNode, void *pUserData)
103 {
104         if (!pUserData) {
105                 VideoLogInfo("User data is NULL.");
106                 return;
107         }
108
109         VideoLogInfo("");
110
111         if (!vconf_get_bool(VCONFKEY_GALLERY_SUBTITLE_STATE, &bSubtitleSettingState)) {
112                 if (!bSubtitleSettingState) {
113                         VideoLogInfo("Subtitle OFF.");
114                 } else {
115                         VideoLogInfo("Subtitle ON.");
116                 }
117         } else {
118                 VideoLogInfo("Fail to get vconf - subtitle.");
119         }
120 }
121
122 /*
123  * External function
124  */
125 bool MpVideoSubtitleCtrlInit(void *pUserData)
126 {
127         if (pUserData == NULL) {
128                 VideoLogInfo("[ERR]No have pUserData");
129                 return FALSE;
130         }
131
132         VideoLogInfo("");
133
134         VideoAppData *pAppData = (VideoAppData *)pUserData;
135
136         MpVideoSubtitleCtrlClear();
137
138         if (pAppData->nCurPlayerType != MP_VIDEO_PLAYER) {
139                 VideoLogInfo("Current player type is not video player.");
140                 return FALSE;
141         }
142
143         if (!vconf_get_bool(VCONFKEY_GALLERY_SUBTITLE_STATE, &bSubtitleSettingState)) {
144                 if (!bSubtitleSettingState) {
145                         VideoLogInfo("Subtitle OFF.");
146                 } else {
147                         VideoLogInfo("Subtitle ON.");
148                 }
149         } else {
150                 VideoLogInfo("Fail to get vconf - subtitle.");
151         }
152
153         if (vconf_notify_key_changed(VCONFKEY_GALLERY_SUBTITLE_STATE, MpVideoSubtitleCtrlStateChangeCb, pAppData)) {
154                 VideoLogInfo("[ERR] Fail to resiste subtitle notification callback.");
155         }
156
157         return MpVideoSubtitleCtrlCheckSubtitleFile(pAppData->szMediaUri);
158 }
159
160 void MpVideoSubtitleCtrlDestroy(void)
161 {
162         VideoLogInfo("");
163
164         MpVideoSubtitleCtrlClear();
165 }
166
167 bool MpVideoSubtitleCtrlIsExistSubtitle(void)
168 {
169         VideoLogInfo("");
170
171         return bExistSubtitleFile;
172 }
173
174 bool MpVideoSubtitleCtrlGetSubtitleUri(char *szSubtitleUri)
175 {
176         VideoLogInfo("");
177
178         if (!MpVideoSubtitleCtrlIsExistSubtitle()) {
179                 return FALSE;
180         }
181
182         if (strlen(szSubtitleFileUri) < 1) {
183                 VideoLogInfo("[ERR]No have szSubtitleFileUri");
184                 return FALSE;
185         }
186
187         strncpy(szSubtitleUri, szSubtitleFileUri, STR_LEN_MAX -1);
188
189         return TRUE;
190 }