Wednesday, 4 February 2009

Things you shouldn't do

Things you shouldn't ever be doing.
  • Transform a filename into a URI like that:
uri = g_strdup_printf ("file:///%s", filename);

Use g_filename_to_uri() instead. Same goes for the opposite direction, where stripping "file://" isn't good enough to transform a URI into a local filename. Use g_filename_from_uri(), or g_file_get_path() if you're actually going to be opening the files.

  • Use GStreamer to play a single small pop sound:
Use libcanberra instead. It's portable, might use GStreamer in some cases, and very certainly requires less code for a simple use like this.

Bruno, you should get a Bugzilla component for Garfunkel.

6 comments:

  1. Is there a python binding for this

    ReplyDelete
  2. Heh - I was just going to ask the same Python-binding question :-)

    Also: in one case I want to check if a file name is on a local directory (ie. not on gvfs mount) and then get the directory of the file... Currently I use this:

    if theUri.startswith("file:///"):
    theDir = os.path.dirname(theUri[7:])

    Not elegant, and uses a hardcoded file:/ prefix... Any suggestion how to improve this?

    ReplyDelete
  3. oliver:
    use g_file_is_native()

    / Alex

    ReplyDelete
  4. You can also use g_file_get_path() if it returns Non-NULL its a local path. If you do it this way it can return local paths for gvfs mounts too, via fuse. (but is_native() will return FALSE even if fuse is availible).

    ReplyDelete
  5. Thank you, Bastien!

    ReplyDelete
  6. > Use libcanberra instead.

    Or Phonon. :-)

    ReplyDelete