Exemplo n.º 1
0
static PyObject *get_mime_type_from_file_name (PyObject *self, PyObject *args)
{
    const char* file_name;

    if (!PyArg_ParseTuple(args, "s", &file_name))
        return NULL;

    return PyString_FromString(xdg_mime_get_mime_type_from_file_name(file_name));
}
static void 
test_by_name (const char *filename,
	      const char *mt_expected,
              int         xfail)
{
  const char *mt;

  mt = xdg_mime_get_mime_type_from_file_name (filename);

  check_mime_type (mt, mt_expected, xfail, "name", filename);
}
Exemplo n.º 3
0
static void
test_one_match (const char *filename, const char *expected)
{
  const char *actual;

  actual = xdg_mime_get_mime_type_from_file_name (filename);

  if (strcmp (actual, expected) != 0) 
    {
      printf ("Test Failed: mime type of %s is %s, expected %s\n", 
	      filename, actual, expected);
    }  
}
Exemplo n.º 4
0
const char* GuessMimeByFilename(const char *what, size_t len)
{
	/* we know some hardcoded on our own, try them... */
	if ((len > 3) && !strncasecmp(&what[len - 4], ".gif", 4))
		return "image/gif";
	else if ((len > 2) && !strncasecmp(&what[len - 3], ".js", 3))
		return  "text/javascript";
	else if ((len > 3) && !strncasecmp(&what[len - 4], ".txt", 4))
		return "text/plain";
	else if ((len > 3) && !strncasecmp(&what[len - 4], ".css", 4))
		return "text/css";
	else if ((len > 3) && !strncasecmp(&what[len - 4], ".htc", 4))
		return "text/x-component";
	else if ((len > 3) && !strncasecmp(&what[len - 4], ".jpg", 4))
		return "image/jpeg";
	else if ((len > 4) && !strncasecmp(&what[len - 5], ".jpeg", 5))
		return "image/jpeg";
	else if ((len > 3) && !strncasecmp(&what[len - 4], ".png", 4))
		return "image/png";
	else if ((len > 3) && !strncasecmp(&what[len - 4], ".ico", 4))
		return "image/x-icon";
	else if ((len > 3) && !strncasecmp(&what[len - 4], ".vcf", 4))
		return "text/x-vcard";
	else if ((len > 4) && !strncasecmp(&what[len - 5], ".html", 5))
		return "text/html";
	else if ((len > 3) && !strncasecmp(&what[len - 4], ".htm", 4))
		return "text/html";
	else if ((len > 3) && !strncasecmp(&what[len - 4], ".wml", 4))
		return "text/vnd.wap.wml";
	else if ((len > 4) && !strncasecmp(&what[len - 5], ".wmls", 5))
		return "text/vnd.wap.wmlscript";
	else if ((len > 4) && !strncasecmp(&what[len - 5], ".wmlc", 5))
		return "application/vnd.wap.wmlc";
	else if ((len > 5) && !strncasecmp(&what[len - 6], ".wmlsc", 6))
		return "application/vnd.wap.wmlscriptc";
	else if ((len > 4) && !strncasecmp(&what[len - 5], ".wbmp", 5))
		return "image/vnd.wap.wbmp";
	else
		/* and let xdgmime do the fallback. */
		return xdg_mime_get_mime_type_from_file_name(what);
}
Exemplo n.º 5
0
/**
   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;
}
Exemplo n.º 6
0
/**
   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;	
}
Exemplo n.º 7
0
Arquivo: mime.c Projeto: ib/xarchiver
const char *xa_get_stock_mime_icon(const char *filename)
{
	const char *mime;
	static char icon_name[80];
	char *p;

	mime = xdg_mime_get_mime_type_from_file_name(filename);

	strncpy(icon_name,mime,sizeof(icon_name));
	icon_name[sizeof(icon_name)-1] = 0;

	p = strchr(icon_name,'/');
	if (p) *p = '-';

	if (strcmp(mime,"text/html") == 0)
		return icon_name;
	else if (strncmp(mime,"text/",5) == 0)
		return "text-x-generic";
	else if (strncmp(mime,"audio/",6) == 0)
		return "audio-x-generic";
	else if (strncmp(mime,"image/",6) == 0)
		return "image-x-generic";
	else if (strncmp(mime,"video/",6) == 0 ||
	         strcmp(mime,"application/vnd.adobe.flash.movie") == 0)
		return "video-x-generic";
	else if (strcmp(mime,"application/msword") == 0 ||
	         strcmp(mime,"application/pdf") == 0 ||
	         strcmp(mime,"application/rtf") == 0 ||
	         strcmp(mime,"application/vnd.oasis.opendocument.text") == 0)
		return "x-office-document";
	else if (strcmp(mime,"application/vnd.ms-excel") == 0 ||
	         strcmp(mime,"application/vnd.oasis.opendocument.spreadsheet") == 0)
		return "x-office-spreadsheet";
	else if (strcmp(mime,"application/vnd.ms-powerpoint") == 0 ||
	         strcmp(mime,"application/vnd.oasis.opendocument.presentation") == 0)
		return "x-office-presentation";
	else if (strcmp(mime,"application/gzip") == 0 ||
	         strcmp(mime,"application/vnd.android.package-archive") == 0 ||
	         strcmp(mime,"application/vnd.debian.binary-package") == 0 ||
	         strcmp(mime,"application/vnd.openofficeorg.extension") == 0 ||
	         strcmp(mime,"application/vnd.rar") == 0 || /* legacy */ strcmp(mime,"application/x-rar") == 0 ||
	         strcmp(mime,"application/x-7z-compressed") == 0 ||
	         strcmp(mime,"application/x-archive") == 0 ||
	         strcmp(mime,"application/x-arj") == 0 ||
	         strcmp(mime,"application/x-bzip") == 0 ||
	         strcmp(mime,"application/x-bzip-compressed-tar") == 0 ||
	         strcmp(mime,"application/x-compress") == 0 ||
	         strcmp(mime,"application/x-compressed-tar") == 0 ||
	         strcmp(mime,"application/x-cpio") == 0 ||
	         strcmp(mime,"application/x-cpio-compressed") == 0 ||
	         strcmp(mime,"application/x-java-archive") == 0 ||
	         strcmp(mime,"application/x-lha") == 0 ||
	         strcmp(mime,"application/x-lrzip") == 0 ||
	         strcmp(mime,"application/x-lrzip-compressed-tar") == 0 ||
	         strcmp(mime,"application/x-lz4") == 0 ||
	         strcmp(mime,"application/x-lz4-compressed-tar") == 0 ||
	         strcmp(mime,"application/x-lzip") == 0 ||
	         strcmp(mime,"application/x-lzip-compressed-tar") == 0 ||
	         strcmp(mime,"application/x-lzma") == 0 ||
	         strcmp(mime,"application/x-lzma-compressed-tar") == 0 ||
	         strcmp(mime,"application/x-lzop") == 0 ||
	         strcmp(mime,"application/x-rpm") == 0 ||
	         strcmp(mime,"application/x-source-rpm") == 0 ||
	         strcmp(mime,"application/x-tar") == 0 ||
	         strcmp(mime,"application/x-tarz") == 0 ||
	         strcmp(mime,"application/x-tzo") == 0 ||
	         strcmp(mime,"application/x-xpinstall") == 0 ||
	         strcmp(mime,"application/x-xz") == 0 ||
	         strcmp(mime,"application/x-xz-compressed-tar") == 0 ||
	         strcmp(mime,"application/x-zstd-compressed-tar") == 0 ||
	         strcmp(mime,"application/zip") == 0 ||
	         strcmp(mime,"application/zstd") == 0)
		return "package-x-generic";
	else if (strcmp(mime,"application/x-cd-image") == 0)
		return "media-optical";
	else if (strcmp(mime,"application/x-csh") == 0 ||
	         strcmp(mime,"application/x-perl") == 0 ||
	         strcmp(mime,"application/x-php") == 0 ||
	         strcmp(mime,"application/x-shellscript") == 0)
		return "text-x-script";
	else if (strncmp(mime,"font/",5) == 0 ||
	         strncmp(mime,"application/x-font",18) == 0)
		return "font-x-generic";

	return icon_name;
}