summaryrefslogtreecommitdiff
path: root/main/src/util/ivug-util.c
blob: 496f027301e7a55f3aecf89812a3b762035ee581 (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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
 * Copyright 2012  Samsung Electronics Co., Ltd
 *
 * Licensed under the Flora License, Version 1.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.tizenopensource.org/license
 *
 * 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 "ivug-common.h"

#include <unistd.h>
#include <stdlib.h>
#include <aul.h>

#include <vconf.h>
#include <vconf-keys.h>
#include <pmapi.h>

#include <media-svc.h>
#include <visual-svc.h>

/*
	If filepath is web url, return TRUE.

*/
bool ivug_is_web_uri(const char* uri)
{
//check file url type. local , http, ftp.
	IV_ASSERT(uri != NULL);

	static const char* web_protocal_name[] =
	{
		"http://",
		"ftp://",
		NULL,
	};

	int i = 0;
	while (web_protocal_name[i] != NULL)
	{
		if ( strlen(uri) > strlen(web_protocal_name[i]) )
		{
			if (strncmp(uri, web_protocal_name[i], strlen(web_protocal_name[i])) == 0)
			{
				return true;
			}
		}

		i++;
	}

	MSG_UTIL_MED("Not web uri. %s", uri);

	return false;
}


unsigned int get_distance(int prevX, int prevY, int X, int Y)
{
#include <math.h>
	int dx = prevX - X;
	int dy = prevY - Y;

	return sqrt(dx*dx + dy*dy);
}



#define USE_ECORE_FILE

#include <Ecore_File.h>

/*
	Remove fname file.
	Returns true fname is not exist or removed sucessfully
*/
bool ivug_remove_file(const char *filepath)
{
	char error_msg[256];
	if (ecore_file_exists(filepath) == EINA_FALSE)
	{
		MSG_UTIL_ERROR("Already removed.%s", filepath);
		return true;
	}

#ifdef USE_ECORE_FILE
	if ( ecore_file_unlink(filepath) == EINA_FALSE)
	{
		MSG_UTIL_ERROR("Cannot remove file : %s %s", filepath, strerror_r(errno, error_msg, sizeof(error_msg)) );
		return false;
	}

	return true;
#else
	if ( unlink(filepath) != 0 )
	{
		MSG_UTIL_ERROR("Cannot remove file : %s %s", filepath, strerror_r(errno, error_msg, sizeof(error_msg)) );
		return false;
	}

	return true;
#endif
}

bool ivug_rename_file(const char *src, const char *dst)
{
	if (ecore_file_exists(src) == EINA_FALSE)
	{
		MSG_UTIL_ERROR("Source file is not exist : %s", src);
		return false;
	}

	if (dst == NULL)
	{
		MSG_UTIL_ERROR("Destination file is NULL");
		return false;
	}


	char error_msg[256];

	if ( rename(src, dst) < 0)
	{
		MSG_UTIL_ERROR("Cannot rename from %s to %s : %s", src, dst, strerror_r(errno, error_msg, sizeof(error_msg)) );
		return false;
	}

	return true;
}

char *
ivug_mktemp(char* filepath, char*ext)
{
	ivug_retv_if(!filepath || !ext, NULL);

	MSG_IVUG_HIGH("filepath %s, ext %s", filepath, ext);

	char tempname[IVUG_MAX_FILE_PATH_LEN+1] = {0};
	snprintf(tempname, sizeof(tempname), "%s_0.%s",filepath, ext);
	int i = 1;

// TODO : Will implement with another algorithm
	while(ecore_file_exists(tempname) == EINA_TRUE )
	{
		snprintf(tempname, sizeof(tempname),"%s_%d.%s", filepath, i, ext);
		i++;
	}

	MSG_IVUG_HIGH( " tempname %s, i %d", tempname, i);

	return strdup(tempname);
}


/*
 	Check whether given filepath file exists

 	CAUTION : filepath cannot be NULL.
*/
bool ivug_is_file_exist(const char* filepath)
{
	IV_ASSERT(filepath != NULL);

	if ( ecore_file_exists(filepath) == EINA_TRUE)
	{
		return true;
	}

	return false;
}


/*
	Returns start pointer of filename within filepath.
	No memory allocated in this function. so user do not free returned pointer.

	CAUTION : filepath cannot be NULL.
*/
const char * ivug_get_filename(const char *filepath)
{
	if ( filepath == NULL )
	{
		MSG_UTIL_WARN("File path is NULL");
		return "NULL";
	}

#define DIRECORY_SPLITTER '/'
	const char*pFileName = NULL;

	pFileName  =  strrchr(filepath, DIRECORY_SPLITTER);
	pFileName = (NULL == pFileName)? "": (pFileName+1);

	return pFileName;
}


/*

	Media service utility

*/

#include <media-svc.h>

UUID ivug_get_album_id_from_filepath(const char *filepath)
{
	int ret = 0;
	Mitem* item = NULL;

	MediaSvcHandle *dbhandle = NULL;

	int err = media_svc_connect(&dbhandle);
	if (err != MB_SVC_ERROR_NONE)
	{
		MSG_UTIL_ERROR("DB open error. %d", err);
		return NULL;
	}

	ret = minfo_get_item(dbhandle, filepath, &item); //get cluster id
	if (ret != MB_SVC_ERROR_NONE)
	{
		MSG_UTIL_ERROR("Cannot find file in media db for %s", filepath);
		return NULL;		// Invalid id
	}

	UUID cluster_uuid = INVALID_UUID;

	cluster_uuid = uuid_assign(item->cluster_uuid);

	minfo_destroy_mtype_item(item);

	err = media_svc_disconnect(dbhandle);
	if (err != MB_SVC_ERROR_NONE)
	{
		MSG_SDATA_ERROR("media service finalize error=%d", err);
	}

	return cluster_uuid;

}

int ivug_prohibit_lcd_off(void)
{
	MSG_UTIL_MED("START : Sleep disabled");
	return pm_lock_state(LCD_NORMAL, GOTO_STATE_NOW, 0);
}

int ivug_allow_lcd_off(void)
{
	MSG_UTIL_MED("END : Sleep disabled");
	return pm_unlock_state(LCD_NORMAL, PM_KEEP_TIMER);
}

#define MIME_TYPE_LEN			(255)
#define MIME_TYPE_3GPP			"video/3gpp"
#define PATH_CAMERA_LOCAL		"/opt/media/Camera shots"
#define PATH_CAMERA_SDCARD		"/opt/storage/sdcard/Camera shots/"

/* Video editor can start when video exist in camera folder and 3gp file format */

bool ivug_is_editable_video_file(char *filepath)
{
	MSG_UTIL_MED("path = %s", filepath);
	if(strncmp(filepath, PATH_CAMERA_LOCAL, strlen(PATH_CAMERA_LOCAL)) == 0
		|| strncmp(filepath, PATH_CAMERA_SDCARD, strlen(PATH_CAMERA_SDCARD)) == 0)
	{
		char mime_type[MIME_TYPE_LEN] = {0,};
		aul_get_mime_from_file(filepath, mime_type, sizeof(mime_type));
		MSG_UTIL_MED("mime type = %s", mime_type);
		if(strcmp(mime_type, MIME_TYPE_3GPP) == 0)
		{
			return true;
		}
	}
	return false;
}