Add new functions derived from xdg_mime 07/142207/5 accepted/tizen/4.0/unified/20171017.212218 accepted/tizen/unified/20171017.071002 submit/tizen/20171016.143258 submit/tizen/20171016.143345 submit/tizen_4.0/20171016.143540
authorDaehyeon Jung <darrenh.jung@samsung.com>
Mon, 31 Jul 2017 11:03:58 +0000 (20:03 +0900)
committerDaehyeon Jung <darrenh.jung@samsung.com>
Thu, 28 Sep 2017 08:58:40 +0000 (17:58 +0900)
Change-Id: I5766fbfd1679e350eb33d6b119ceee72b3d53a04
Signed-off-by: Daehyeon Jung <darrenh.jung@samsung.com>
include/mime_type.h
src/mime_type.c

index 5e8c787..350eabd 100755 (executable)
@@ -31,13 +31,14 @@ extern "C"
 
 
 /**
- * @brief Enumeration of error code
+ * @brief Enumeration of error code.
  */
 typedef enum {
        MIME_TYPE_ERROR_NONE = TIZEN_ERROR_NONE, /**< Successful */
        MIME_TYPE_ERROR_INVALID_PARAMETER = TIZEN_ERROR_INVALID_PARAMETER, /**< Invalid parameter */
        MIME_TYPE_ERROR_OUT_OF_MEMORY = TIZEN_ERROR_OUT_OF_MEMORY, /**< Out of memory */
-       MIME_TYPE_ERROR_IO_ERROR = TIZEN_ERROR_IO_ERROR , /**< Internal I/O error */
+       MIME_TYPE_ERROR_IO_ERROR = TIZEN_ERROR_IO_ERROR, /**< Internal I/O error */
+       MIME_TYPE_ERROR_PERMISSION_DENIED = TIZEN_ERROR_PERMISSION_DENIED, /**< No access for the file (Since 4.0) */
 } mime_type_error_e;
 
 
@@ -45,8 +46,8 @@ typedef enum {
  * @brief Gets the MIME type for the given file extension.
  *
  * @remarks The @a mime_type must be released with free() by you.
- * @param [in] file_extension The file extension without the leading dot ('.')
- * @param [out] mime_type The MIME type for the given file extension \n
+ * @param[in] file_extension The file extension without the leading dot ('.')
+ * @param[out] mime_type The MIME type for the given file extension \n
  * The MIME type is 'application/octet-stream' if the given file extension is not associated with specific file formats
  * @return 0 on success, otherwise a negative error value.
  * @retval #MIME_TYPE_ERROR_NONE Successful
@@ -62,9 +63,9 @@ int mime_type_get_mime_type(const char *file_extension, char **mime_type);
  * @brief Gets the file extensions for the given MIME type.
  *
  * @remarks The @a file_extension must be released with free() by you.
- * @param [in] mime_type The MIME type
- * @param [out] file_extension The array of file extension without the leading dot ('.')
- * @param [out] length The length of the array of file extension or zero if there is none
+ * @param[in] mime_type The MIME type
+ * @param[out] file_extension The array of file extension without the leading dot ('.')
+ * @param[out] length The length of the array of file extension or zero if there is none
  * @return 0 on success, otherwise a negative error value.
  * @retval #MIME_TYPE_ERROR_NONE Successful
  * @retval #MIME_TYPE_ERROR_INVALID_PARAMETER Invalid parameter
@@ -74,6 +75,75 @@ int mime_type_get_mime_type(const char *file_extension, char **mime_type);
  */
 int mime_type_get_file_extension(const char *mime_type, char *** file_extension, int *length);
 
+/**
+ * @brief Gets the MIME type for the given data.
+ *
+ * @since_tizen 4.0
+ * @remarks The @a mime_type must be released with free() by you.
+ * @param[in] data The stream of file data
+ * @param[in] len The length of data
+ * @param[out] mime_type The MIME type for the given file extension \n
+ * The MIME type is 'application/octet-stream' if the given file extension is not associated with specific file formats
+ * @return 0 on success, otherwise a negative error value.
+ * @retval #MIME_TYPE_ERROR_NONE Successful
+ * @retval #MIME_TYPE_ERROR_INVALID_PARAMETER Invalid parameter
+ * @retval #MIME_TYPE_ERROR_OUT_OF_MEMORY Out of memory
+ * @retval #MIME_TYPE_ERROR_IO_ERROR Internal I/O error
+ */
+int mime_type_get_mime_type_for_data(const void *data, size_t len, char **mime_type);
+
+/**
+ * @brief Gets the MIME type for the given file path.
+ *
+ * @since_tizen 4.0
+ * @remarks The @a mime_type must be released with free() by you.
+ * @remarks http://tizen.org/privilege/mediastorage is needed if input or output path are relevant to media storage.
+ * @remarks http://tizen.org/privilege/externalstorage is needed if input or output path are relevant to external storage.
+ * @param[in] file_path The path of target file
+ * @param[out] mime_type The MIME type for the given file extension \n
+ * The MIME type is 'application/octet-stream' if the given file extension is not associated with specific file formats
+ * @return 0 on success, otherwise a negative error value.
+ * @retval #MIME_TYPE_ERROR_NONE Successful
+ * @retval #MIME_TYPE_ERROR_INVALID_PARAMETER Invalid parameter
+ * @retval #MIME_TYPE_ERROR_OUT_OF_MEMORY Out of memory
+ * @retval #MIME_TYPE_ERROR_IO_ERROR Internal I/O error
+ * @retval #MIME_TYPE_ERROR_PERMISSION_DENIED Permission denined
+ */
+int mime_type_get_mime_type_for_file(const char *file_path, char **mime_type);
+
+/**
+ * @brief Gets the icon name for the given MIME type.
+ *
+ * @since_tizen 4.0
+ * @remarks The @a icon must be released with free() by you.
+ * @param[in] mime_type The MIME type
+ * @param[out] icon The icon name for the given MIME type
+ * @return 0 on success, otherwise a negative error value.
+ * @retval #MIME_TYPE_ERROR_NONE Successful
+ * @retval #MIME_TYPE_ERROR_INVALID_PARAMETER Invalid parameter
+ * @retval #MIME_TYPE_ERROR_OUT_OF_MEMORY Out of memory
+ * @retval #MIME_TYPE_ERROR_IO_ERROR Internal I/O error
+ * @see mime_type_get_mime_type()
+ */
+int mime_type_get_icon(const char  *mime_type, char **icon);
+
+/**
+ * @brief Gets the generic icon name for the given MIME type.
+ *
+ * @since_tizen 4.0
+ * @remarks The @a generic_icon must be released with free() by you.
+ * @param[in] mime_type The MIME type
+ * @param[out] generic_icon The generic icon name for the given MIME type
+ * @return 0 on success, otherwise a negative error value.
+ * @retval #MIME_TYPE_ERROR_NONE Successful
+ * @retval #MIME_TYPE_ERROR_INVALID_PARAMETER Invalid parameter
+ * @retval #MIME_TYPE_ERROR_OUT_OF_MEMORY Out of memory
+ * @retval #MIME_TYPE_ERROR_IO_ERROR Internal I/O error
+ * @see mime_type_get_mime_type()
+ */
+int mime_type_get_generic_icon(const char *mime_type, char **generic_icon);
+
+
 
 /**
  * @}
index 7ab5716..fca73bb 100755 (executable)
@@ -17,6 +17,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <unistd.h>
 
 #include <dlog.h>
 #include <xdgmime.h>
@@ -137,3 +138,111 @@ int mime_type_get_file_extension(const char *mime_type, char *** file_extension,
        return MIME_TYPE_ERROR_NONE;
 }
 
+int mime_type_get_mime_type_for_file(const char *file_path, char **mime_type)
+{
+       const char *xdg_mime_type = NULL;
+       struct stat statbuf;
+
+       if (file_path == NULL || mime_type == NULL)
+               return mime_type_error(MIME_TYPE_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
+
+       if (access(file_path, R_OK) < 0) {
+               if (errno == EACCES)
+                       return mime_type_error(MIME_TYPE_ERROR_PERMISSION_DENIED, __FUNCTION__, NULL);
+
+               return mime_type_error(MIME_TYPE_ERROR_IO_ERROR, __FUNCTION__, NULL);
+       }
+
+       xdg_mime_type = xdg_mime_get_mime_type_for_file(file_path, &statbuf);
+
+       if (xdg_mime_type == NULL || *xdg_mime_type == '\0') {
+               *mime_type = NULL;
+               return mime_type_error(MIME_TYPE_ERROR_IO_ERROR, __FUNCTION__, "failed to get the mime type from the shared MIME database");
+       }
+
+       char * mime_type_dup = NULL;
+
+       mime_type_dup = strdup(xdg_mime_type);
+
+       if (mime_type_dup == NULL)
+               return mime_type_error(MIME_TYPE_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
+
+       *mime_type = mime_type_dup;
+
+       return MIME_TYPE_ERROR_NONE;
+}
+
+int mime_type_get_mime_type_for_data(const void *data, size_t len, char **mime_type)
+{
+       const char *xdg_mime_type = NULL;
+       int result_prio;
+
+       if (data == NULL || mime_type == NULL)
+               return mime_type_error(MIME_TYPE_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
+
+       xdg_mime_type = xdg_mime_get_mime_type_for_data(data, len, &result_prio);
+
+       if (xdg_mime_type == NULL || *xdg_mime_type == '\0') {
+               *mime_type = NULL;
+               return mime_type_error(MIME_TYPE_ERROR_IO_ERROR, __FUNCTION__, "failed to get the mime type from the sahred MIME database");
+       }
+
+       char * mime_type_dup = NULL;
+
+       mime_type_dup = strdup(xdg_mime_type);
+
+       if (mime_type_dup == NULL)
+               return mime_type_error(MIME_TYPE_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
+
+       *mime_type = mime_type_dup;
+
+       return MIME_TYPE_ERROR_NONE;
+}
+
+int mime_type_get_icon(const char *mime_type, char **icon)
+{
+       const char *xdg_icon = NULL;
+
+       if (mime_type == NULL || icon == NULL)
+               return mime_type_error(MIME_TYPE_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
+
+       xdg_icon = xdg_mime_get_icon(mime_type);
+
+       if (xdg_icon == NULL)
+               return mime_type_error(MIME_TYPE_ERROR_IO_ERROR, __FUNCTION__, "failed to get the icon from the shared MIME database");
+
+       char * icon_dup = NULL;
+
+       icon_dup = strdup(xdg_icon);
+
+       if (icon_dup == NULL)
+               return mime_type_error(MIME_TYPE_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
+
+       *icon = icon_dup;
+
+       return MIME_TYPE_ERROR_NONE;
+}
+
+int mime_type_get_generic_icon(const char *mime_type, char **generic_icon)
+{
+       const char *xdg_icon = NULL;
+
+       if (mime_type == NULL || generic_icon == NULL)
+               return mime_type_error(MIME_TYPE_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
+
+       xdg_icon = xdg_mime_get_generic_icon(mime_type);
+
+       if (xdg_icon == NULL)
+               return mime_type_error(MIME_TYPE_ERROR_IO_ERROR, __FUNCTION__, "failed to get the generic icon from the shared MIME database");
+
+       char * icon_dup = NULL;
+
+       icon_dup = strdup(xdg_icon);
+
+       if (icon_dup == NULL)
+               return mime_type_error(MIME_TYPE_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
+
+       *generic_icon = icon_dup;
+
+       return MIME_TYPE_ERROR_NONE;
+}