Пример #1
0
int
main (int argc, char *argv[])
{
  const char *result;
  const char *file_name;
  int i;

  test_glob_type ();
  test_aliasing ();
  test_subclassing ();
  test_matches ();
  test_icons ();

  for (i = 1; i < argc; i++)
    {
      file_name = argv[i];
      result = xdg_mime_get_mime_type_for_file (file_name, NULL);
      printf ("File \"%s\" has a mime-type of %s\n", file_name, result);
    }

#if 0
  xdg_mime_dump ();
#endif
  return 0;
}
Пример #2
0
static PyObject *get_mime_type_for_file (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_for_file(file_name, NULL));
}
static void 
test_by_file (const char *dir,
	      const char *filename,
	      const char *mt_expected,
              int         xfail)
{
  const char *mt;
  char path[1024];

  snprintf (path, 1024, "%s/%s", dir, filename);

  mt = xdg_mime_get_mime_type_for_file (path, NULL);
  
  check_mime_type (mt, mt_expected, xfail, "file", filename);
}
Пример #4
0
/* Returns a pointer to the MIME-type.
 *
 * Tries all enabled methods:
 * - Look for extended attribute
 * - If no attribute, check file name
 * - If no name rule, check contents
 *
 * NULL if we can't think of anything.
 */
MIME_type*
type_from_path(const char *path)
{
	if(_debug_ > 1) printf("type_from_path()...\n");

	/* Check for extended attribute first */
	/*
	MIME_type *mime_type = NULL;
	mime_type = xtype_get(path);
	if (mime_type) return mime_type;
	*/

	/* Try name and contents next */
	const char* type_name = xdg_mime_get_mime_type_for_file(path, NULL);
	if (type_name) return get_mime_type(type_name, TRUE);

	return NULL;
}
Пример #5
0
bool MimeType::set(const char* filename) {
	E_ASSERT(filename != NULL);
	/*
	 * Call on the same file again check.
	 * FIXME: some stamp check would be nice here so if file  with the same name change content, can be inspected.
	 */
	if(mtype == filename)
		return true;

	mcmt.clear(); mtype.clear(); micon.clear();

	const char* res = xdg_mime_get_mime_type_for_file(filename, NULL);

	if(!res) {
		status = 0;
		return false;
	}

	mtype.assign(res);
	status = MIME_LOADED;
	return true;
}
Пример #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;

    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;
}
Пример #7
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;	
}