/** * g_content_type_get_description: * @type: a content type string * * Gets the human readable description of the content type. * * Returns: a short description of the content type @type. Free the * returned string with g_free() */ gchar * g_content_type_get_description (const gchar *type) { static GHashTable *type_comment_cache = NULL; gchar *comment; g_return_val_if_fail (type != NULL, NULL); G_LOCK (gio_xdgmime); type = xdg_mime_unalias_mime_type (type); if (type_comment_cache == NULL) type_comment_cache = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); comment = g_hash_table_lookup (type_comment_cache, type); comment = g_strdup (comment); G_UNLOCK (gio_xdgmime); if (comment != NULL) return comment; comment = load_comment_for_mime (type); G_LOCK (gio_xdgmime); g_hash_table_insert (type_comment_cache, g_strdup (type), g_strdup (comment)); G_UNLOCK (gio_xdgmime); return comment; }
static PyObject *unalias_mime_type (PyObject *self, PyObject *args) { const char* mime; if (!PyArg_ParseTuple(args, "s", &mime)) return NULL; return PyString_FromString(xdg_mime_unalias_mime_type(mime)); }
gchar * _g_unix_content_type_unalias (const gchar *type) { gchar *res; G_LOCK (gio_xdgmime); res = g_strdup (xdg_mime_unalias_mime_type (type)); G_UNLOCK (gio_xdgmime); return res; }
/** * g_content_type_from_mime_type: * @mime_type: a mime type string * * Tries to find a content type based on the mime type name. * * Returns: (allow-none): Newly allocated string with content type * or %NULL. Free with g_free() * * Since: 2.18 **/ gchar * g_content_type_from_mime_type (const gchar *mime_type) { char *umime; g_return_val_if_fail (mime_type != NULL, NULL); G_LOCK (gio_xdgmime); /* mime type and content type are same on unixes */ umime = g_strdup (xdg_mime_unalias_mime_type (mime_type)); G_UNLOCK (gio_xdgmime); return umime; }
/** Main function. Parses options and calls helper function for any heavy lifting. */ int main (int argc, char *argv[]) { int input_type=FILEDATA; int output_type=MIMETYPE; const char *mimetype; char *output=0; int i; hash_table_t launch_hash; locale_init(); /* Parse options */ while( 1 ) { static struct option long_options[] = { { "input-file-data", no_argument, 0, 't' } , { "input-filename", no_argument, 0, 'f' } , { "input-mime", no_argument, 0, 'i' } , { "output-mime", no_argument, 0, 'm' } , { "output-description", no_argument, 0, 'd' } , { "output-action", no_argument, 0, 'a' } , { "help", no_argument, 0, 'h' } , { "version", no_argument, 0, 'v' } , { "launch", no_argument, 0, 'l' } , { 0, 0, 0, 0 } } ; int opt_index = 0; int opt = getopt_long( argc, argv, GETOPT_STRING, long_options, &opt_index ); if( opt == -1 ) break; switch( opt ) { case 0: break; case 't': input_type=FILEDATA; break; case 'f': input_type=FILENAME; break; case 'i': input_type=MIMETYPE; break; case 'm': output_type=MIMETYPE; break; case 'd': output_type=DESCRIPTION; break; case 'a': output_type=ACTION; break; case 'l': output_type=LAUNCH; break; case 'h': print_help( argv[0], 1 ); exit(0); case 'v': printf( _("%s, version %s\n"), MIMEDB, PACKAGE_VERSION ); exit( 0 ); case '?': return 1; } } if( ( output_type == LAUNCH )&&(input_type==MIMETYPE)) { fprintf( stderr, _("%s: Can not launch a mimetype\n"), MIMEDB ); print_help( argv[0], 2 ); exit(1); } if( output_type == LAUNCH ) hash_init( &launch_hash, &hash_str_func, &hash_str_cmp ); /* Loop over all non option arguments and do the specified lookup */ //fprintf( stderr, "Input %d, output %d\n", input_type, output_type ); for (i = optind; (i < argc)&&(!error); i++) { /* Convert from filename to mimetype, if needed */ if( input_type == FILENAME ) { mimetype = xdg_mime_get_mime_type_from_file_name(argv[i]); } else if( input_type == FILEDATA ) { mimetype = xdg_mime_get_mime_type_for_file(argv[i]); } else mimetype = xdg_mime_is_valid_mime_type(argv[i])?argv[i]:0; mimetype = xdg_mime_unalias_mime_type (mimetype); if( !mimetype ) { fprintf( stderr, _( "%s: Could not parse mimetype from argument '%s'\n"), MIMEDB, argv[i] ); error=1; return 1; } /* Convert from mimetype to whatever, if needed */ switch( output_type ) { case MIMETYPE: { output = (char *)mimetype; break; } case DESCRIPTION: { output = get_description( mimetype ); if( !output ) output = strdup( _("Unknown") ); break; } case ACTION: { output = get_action( mimetype ); break; } case LAUNCH: { /* There may be more files using the same launcher, we add them all up in little array_list_ts and launched them together after all the arguments have been parsed. */ array_list_t *l= (array_list_t *)hash_get( &launch_hash, mimetype ); output = 0; if( !l ) { l = my_malloc( sizeof( array_list_t ) ); if( l == 0 ) { break; } al_init( l ); hash_put( &launch_hash, mimetype, l ); } al_push( l, argv[i] ); } } /* Print the glorious result */ if( output ) { printf( "%s\n", output ); if( output != mimetype ) free( output ); } output = 0; } /* Perform the actual launching */ if( output_type == LAUNCH && !error ) { int i; array_list_t mimes; al_init( &mimes ); hash_get_keys( &launch_hash, &mimes ); for( i=0; i<al_get_count( &mimes ); i++ ) { char *mimetype = (char *)al_get( &mimes, i ); array_list_t *files = (array_list_t *)hash_get( &launch_hash, mimetype ); if( !files ) { fprintf( stderr, _( "%s: Unknown error\n"), MIMEDB ); error=1; break; } char *launcher = get_action( mimetype ); if( launcher ) { launch( launcher, files, 0 ); free( launcher ); } } hash_foreach( &launch_hash, &clear_entry ); hash_destroy( &launch_hash ); al_destroy( &mimes ); } if( launch_buff ) free( launch_buff ); if( start_re ) { regfree( start_re ); regfree( stop_re ); free( start_re ); free( stop_re ); } xdg_mime_shutdown(); return error; }
/** Main function. Parses options and calls helper function for any heavy lifting. */ int main (int argc, char *argv[]) { int input_type=FILEDATA; int output_type=MIMETYPE; const char *mimetype; char *output=0; int i; typedef std::map<std::string, string_list_t> launch_hash_t; launch_hash_t launch_hash; locale_init(); /* Parse options */ while( 1 ) { static struct option long_options[] = { { "input-file-data", no_argument, 0, 't' } , { "input-filename", no_argument, 0, 'f' } , { "input-mime", no_argument, 0, 'i' } , { "output-mime", no_argument, 0, 'm' } , { "output-description", no_argument, 0, 'd' } , { "output-action", no_argument, 0, 'a' } , { "help", no_argument, 0, 'h' } , { "version", no_argument, 0, 'v' } , { "launch", no_argument, 0, 'l' } , { 0, 0, 0, 0 } } ; int opt_index = 0; int opt = getopt_long( argc, argv, GETOPT_STRING, long_options, &opt_index ); if( opt == -1 ) break; switch( opt ) { case 0: break; case 't': input_type=FILEDATA; break; case 'f': input_type=FILENAME; break; case 'i': input_type=MIMETYPE; break; case 'm': output_type=MIMETYPE; break; case 'd': output_type=DESCRIPTION; break; case 'a': output_type=ACTION; break; case 'l': output_type=LAUNCH; break; case 'h': print_help( argv[0], 1 ); exit(0); case 'v': printf( _("%s, version %s\n"), MIMEDB, PACKAGE_VERSION ); exit( 0 ); case '?': return 1; } } if( ( output_type == LAUNCH )&&(input_type==MIMETYPE)) { fprintf( stderr, _("%s: Can not launch a mimetype\n"), MIMEDB ); print_help( argv[0], 2 ); exit(1); } /* Loop over all non option arguments and do the specified lookup */ //fprintf( stderr, "Input %d, output %d\n", input_type, output_type ); for (i = optind; (i < argc)&&(!error); i++) { /* Convert from filename to mimetype, if needed */ if( input_type == FILENAME ) { mimetype = xdg_mime_get_mime_type_from_file_name(argv[i]); } else if( input_type == FILEDATA ) { mimetype = xdg_mime_get_mime_type_for_file(argv[i]); } else mimetype = xdg_mime_is_valid_mime_type(argv[i])?argv[i]:0; mimetype = xdg_mime_unalias_mime_type (mimetype); if( !mimetype ) { fprintf( stderr, _( "%s: Could not parse mimetype from argument '%s'\n"), MIMEDB, argv[i] ); error=1; return 1; } /* Convert from mimetype to whatever, if needed */ switch( output_type ) { case MIMETYPE: { output = (char *)mimetype; break; } case DESCRIPTION: { output = get_description( mimetype ); if( !output ) output = strdup( _("Unknown") ); break; } case ACTION: { output = get_action( mimetype ); break; } case LAUNCH: { /* There may be more files using the same launcher, we add them all up in little array_list_ts and launched them together after all the arguments have been parsed. */ output = 0; string_list_t &l = launch_hash[mimetype]; l.push_back(argv[i]); } } /* Print the glorious result */ if( output ) { printf( "%s\n", output ); if( output != mimetype ) free( output ); } output = 0; } /* Perform the actual launching */ if( output_type == LAUNCH && !error ) { for( launch_hash_t::iterator iter = launch_hash.begin(); iter != launch_hash.end(); ++iter) { const char *mimetype = iter->first.c_str(); string_list_t &files = iter->second; char *launcher = get_action( mimetype ); if( launcher ) { launch( launcher, files, 0 ); free( launcher ); } } } if( launch_buff ) free( launch_buff ); if( start_re ) { regfree( start_re ); regfree( stop_re ); free( start_re ); free( stop_re ); } xdg_mime_shutdown(); return error; }
da_result_t da_mime_get_ext_name(char *mime, char **ext) { da_result_t ret = DA_RESULT_OK; const char **extlist = DA_NULL; const char *unaliased_mimetype = DA_NULL; char ext_temp[DA_MAX_STR_LEN] = {0,}; char *temp = NULL; DA_LOG_FUNC_START(Default); if (DA_NULL == mime || DA_NULL == ext) { ret = DA_ERR_INVALID_ARGUMENT; DA_LOG_ERR(Default,"Invalid mime type"); goto ERR; } DA_LOG_VERBOSE(Default,"mime str[%s]ptr[%p]len[%d]",mime,mime,strlen(mime)); /* unaliased_mimetype means representative mime among similar types */ unaliased_mimetype = xdg_mime_unalias_mime_type(mime); if (unaliased_mimetype == DA_NULL) { ret = DA_ERR_INVALID_MIME_TYPE; DA_LOG_ERR(Default,"Invalid mime type : No unsaliased mime type"); goto ERR; } DA_LOG(Default,"unaliased_mimetype[%s]\n",unaliased_mimetype); /* Get extension name from shared-mime-info */ extlist = xdg_mime_get_file_names_from_mime_type(unaliased_mimetype); if (extlist == DA_NULL || *extlist == DA_NULL) { int i = 0; ret = DA_ERR_INVALID_MIME_TYPE; DA_LOG(Default,"No extension list"); #ifdef _SAMSUNG_MIME_POLICY for (i = 0; i < MAX_SEC_MIME_TABLE_INDEX; i++) { if (strncmp(sec_mime_table[i].mime, mime, strlen(mime)) == 0) { strncpy(ext_temp, sec_mime_table[i].ext, DA_MAX_STR_LEN-1); ret = DA_RESULT_OK; break; } } #endif } else { /* For drm case, this else statement is needed */ DA_LOG(Default,"extlist[%s]\n",*extlist); strncpy(ext_temp, *extlist, DA_MAX_STR_LEN); /* If only one extension name is existed, don't enter here */ while (*extlist != NULL) { int i = 0; /* If there are existed many extension names, * try to search common extension name from table * with first mime type at extension list*/ for (i = 0; i < MAX_EXT_TABLE_INDEX; i++) { if (strncmp(ext_trans_table[i].standard,*extlist, strlen(*extlist)) == 0) { memset(ext_temp, 0x00, DA_MAX_STR_LEN); strncpy(ext_temp,ext_trans_table[i].normal, DA_MAX_STR_LEN-1); break; } } DA_LOG(Default,"index[%d]\n",i); /* If there is a mime at extension transform table */ if (i < MAX_EXT_TABLE_INDEX) { break; } DA_LOG(Default,"extlist[%s]\n",*extlist); extlist++; } DA_LOG(Default,"extension from shared mime info[%s]",ext_temp); } if (strlen(ext_temp) < 1) { /* If there is no mime string for OMD descriptor mime type */ if (strncmp(DD_MIME_STR,mime,strlen(DD_MIME_STR)) == 0) { strncpy(ext_temp, DD_EXT_STR, DA_MAX_STR_LEN-1); ret = DA_RESULT_OK; /* If there is no extension name for "applicaion/vnd.oma.drm.messeages" * at shared-mime-info*/ } else if (strncmp(DRM_MIME_STR,mime,strlen(DD_MIME_STR)) == 0) { strncpy(ext_temp, DRM_EXT_STR, DA_MAX_STR_LEN-1); /* If there is extension name at extlist, the return value can have an error.*/ ret = DA_RESULT_OK; } else { ret = DA_ERR_INVALID_MIME_TYPE; DA_LOG_ERR(Default,"Invalid mime type : no extension name at list"); } } if (ret != DA_RESULT_OK) goto ERR; temp = strchr(ext_temp,'.'); if (temp == NULL) temp = ext_temp; else temp++; DA_LOG(Default,"final extension name:[%s]",temp); *ext = (char*)calloc(1, strlen(temp) + 1); if (*ext != DA_NULL) { strncpy(*ext, temp,strlen(temp)); } else { ret = DA_ERR_FAIL_TO_MEMALLOC ; goto ERR ; } ERR: return ret; }