[Goodies-commits] r2869 - xfce4-cddrive-plugin/trunk/panel-plugin

Sylvain Reynal syl at xfce.org
Wed Jun 27 10:32:25 CEST 2007


Author: syl
Date: 2007-06-27 08:32:25 +0000 (Wed, 27 Jun 2007)
New Revision: 2869

Added:
   xfce4-cddrive-plugin/trunk/panel-plugin/cddrive-audio.c
   xfce4-cddrive-plugin/trunk/panel-plugin/cddrive-audio.h
Removed:
   xfce4-cddrive-plugin/trunk/panel-plugin/cddrive-cddb.c
   xfce4-cddrive-plugin/trunk/panel-plugin/cddrive-cddb.h
Modified:
   xfce4-cddrive-plugin/trunk/panel-plugin/Makefile.am
   xfce4-cddrive-plugin/trunk/panel-plugin/cddrive-monitor.c
   xfce4-cddrive-plugin/trunk/panel-plugin/cddrive.c
Log:
renamed cddrive-cddb source and header to cddrive-audio, as it defines now not only CDDB, but also CD-TEXT support.

Modified: xfce4-cddrive-plugin/trunk/panel-plugin/Makefile.am
===================================================================
--- xfce4-cddrive-plugin/trunk/panel-plugin/Makefile.am	2007-06-27 08:19:49 UTC (rev 2868)
+++ xfce4-cddrive-plugin/trunk/panel-plugin/Makefile.am	2007-06-27 08:32:25 UTC (rev 2869)
@@ -20,8 +20,8 @@
   os-dependant/cddrive-process.c    \
 	cddrive-error.h                   \
 	cddrive-error.c                   \
-  cddrive-cddb.h                    \
-  cddrive-cddb.c                    \
+  cddrive-audio.h                   \
+  cddrive-audio.c                   \
 	cddrive-monitor.h                 \
   cddrive-monitor-private.h         \
 	cddrive-monitor.c                 \
@@ -31,7 +31,7 @@
 	cddrive.c
 
 xfce4_cddrive_plugin_CFLAGS =	\
-	-Wall						            \
+	-Wall -Werror		            \
 	$(LIBXFCEGUI4_CFLAGS)				\
 	$(LIBXFCE4PANEL_CFLAGS)			\
 	$(EXO_CFLAGS)			          \

Copied: xfce4-cddrive-plugin/trunk/panel-plugin/cddrive-audio.c (from rev 2867, xfce4-cddrive-plugin/trunk/panel-plugin/cddrive-cddb.c)
===================================================================
--- xfce4-cddrive-plugin/trunk/panel-plugin/cddrive-audio.c	                        (rev 0)
+++ xfce4-cddrive-plugin/trunk/panel-plugin/cddrive-audio.c	2007-06-27 08:32:25 UTC (rev 2869)
@@ -0,0 +1,426 @@
+/*  $Id$
+ *
+ *  Copyright (c) 2007 The Xfce development team. All rights reserved.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU Library General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include "cddrive-audio.h"
+#include <cdio/cdio.h>
+
+#ifdef HAVE_LIBCDDB
+
+#include <cddb/cddb.h>
+#include <libxfce4util/libxfce4util.h>
+
+#ifndef PACKAGE
+#define PACKAGE "xfce4-cddrive-plugin"
+#endif
+#ifndef VERSION
+#define VERSION "unknown"
+#endif
+
+#define CDDRIVE_CDDB_CACHE_PATH           "xfce4/panel/cddrive"
+#define CDDRIVE_CDDB_CACHE_GROUP          "Cddb"
+#define CDDRIVE_CDDB_CACHE_SIZE           20
+#define cddrive_audio_get_key_for_id(id)  g_strdup_printf ("%08x", id)
+
+#endif
+
+
+/* Create a read access on the CD */
+static CdIo_t*
+cddrive_audio_new_cdio (const gchar* device)
+{
+  CdIo_t     *res;
+
+  res = cdio_open (device, DRIVER_DEVICE);
+  if (res == NULL)
+    g_warning ("unable to open audio CD in drive '%s'.", device);
+  
+  else
+    {
+      if (cdio_get_discmode (res) == CDIO_DISC_MODE_ERROR ||
+          cdio_get_discmode (res) == CDIO_DISC_MODE_NO_INFO)
+        {
+          cdio_destroy (res);
+          res = NULL;
+        }
+    }
+  
+  return res;
+}
+
+
+
+/* Return the title of the CD from the CD-TEXT info it contains,
+   or NULL if the CD does not contain such CD-TEXT info. */
+static gchar*
+cddrive_audio_get_cdtext_title (CdIo_t *cdio)
+{
+  gchar    *res;
+  cdtext_t *cdtxt;
+
+  cdtxt = cdio_get_cdtext (cdio, 0);
+  res = cdtext_get (CDTEXT_TITLE, cdtxt);
+  cdtext_destroy (cdtxt);
+
+  return res;
+}
+
+
+
+#ifdef HAVE_LIBCDDB /* ---------- CDDB SUPPORT ---------- */
+
+/* Save a pair (CDDB id, CD title) in the cache file only if it is not already
+   stored. Do nothing otherwise.
+   
+   If it does not yet exist, create the cache file before saving.
+   
+   The cache file is a Xfce rc file, containing a group called "Cddb" with
+   at most CDDRIVE_CDDB_CACHE_SIZE entries, each having a CDDB id as the key,
+   and a CD title as the value.
+*/
+static void
+cddrive_audio_cache_save (guint id, const gchar *title)
+{
+  XfceRc *cache;
+  gchar* *keys;
+  gchar  *k;
+  guint   nb;
+
+  cache = xfce_rc_config_open (XFCE_RESOURCE_CACHE,
+                               CDDRIVE_CDDB_CACHE_PATH,
+                               FALSE);
+  if (cache == NULL)
+    {
+      g_warning ("unable to open CDDB cache file.");
+      return;
+    }
+  
+  xfce_rc_set_group (cache, CDDRIVE_CDDB_CACHE_GROUP);
+  
+  k = cddrive_audio_get_key_for_id (id);
+  if (! xfce_rc_has_entry (cache, k))
+    {
+      /* check if the cache limit is reached. If so, remove the first entry. */
+      keys = xfce_rc_get_entries (cache, CDDRIVE_CDDB_CACHE_GROUP);
+      nb = g_strv_length (keys);
+    
+      if (nb >= CDDRIVE_CDDB_CACHE_SIZE)
+        xfce_rc_delete_entry (cache, keys [0], FALSE);
+      
+      g_strfreev (keys);
+      
+      /* write the new entry */
+      xfce_rc_write_entry (cache, k, title);
+    }
+  
+  xfce_rc_close (cache);
+  g_free (k);
+}
+
+
+
+/* Return the title corresponding to the CDDB id 'id' in the cache, or NULL
+   if not found. */
+gchar*
+cddrive_audio_cache_read (guint id)
+{
+  XfceRc *cache;
+  gchar  *k, *res;
+
+  cache = xfce_rc_config_open (XFCE_RESOURCE_CACHE,
+                               CDDRIVE_CDDB_CACHE_PATH,
+                               TRUE);
+  if (cache == NULL)
+    {
+      g_warning ("unable to open CDDB cache file.");
+      return NULL;
+    }
+  
+  xfce_rc_set_group (cache, CDDRIVE_CDDB_CACHE_GROUP);
+  k = cddrive_audio_get_key_for_id (id);
+  res = g_strdup (xfce_rc_read_entry (cache, k, NULL));
+  xfce_rc_close (cache);
+  g_free (k);
+  
+  return res;
+}
+
+
+
+/* Set the cddb id and the length of the audio CD in 'cdda', as well as each
+   track and its frame offset.
+   Return TRUE on success, FALSE otherwise. */
+static gboolean
+cddrive_audio_fill_cdda (CdIo_t *cdio, cddb_disc_t *cdda)
+{
+  track_t       first, last, i; /* first, last and current track numbers */
+  lba_t         lba;
+  cddb_track_t *track;
+  
+  /* set CDDA's length */
+  lba = cdio_get_track_lba (cdio, CDIO_CDROM_LEADOUT_TRACK);
+  if (lba == CDIO_INVALID_LBA)
+    {
+      g_warning ("leadout track has invalid Logical Block Address.");
+      return FALSE;
+    }
+  cddb_disc_set_length (cdda, FRAMES_TO_SECONDS (lba));
+
+  /* get number of the first track (should be 1, but just in case...) */
+  first = cdio_get_first_track_num (cdio);
+  if (first == CDIO_INVALID_TRACK)
+    {
+      g_warning ("unable to get first track number.");
+      return FALSE;
+    }
+
+  /* get number of the last track */
+  last = cdio_get_last_track_num (cdio);
+  if (last == CDIO_INVALID_TRACK)
+    {
+      g_warning ("unable to get last track number.");
+      return FALSE;
+    }
+  
+  /* set frame offset for each track of the CDDA */
+  for (i = first; i <= last; i++)
+    {
+      track = cddb_track_new ();
+      if (track == NULL)
+        {
+          g_warning ("not enough memory to get CDDA title.");
+          return FALSE;
+        }
+      
+      cddb_disc_add_track (cdda, track);
+      
+      lba = cdio_get_track_lba (cdio, i);
+      if (lba == CDIO_INVALID_LBA)
+        {
+          g_warning ("invalid Logical Block Adress for track %d.", i);
+          return FALSE;
+        }
+      
+      cddb_track_set_frame_offset (track, lba);
+    }
+  
+  if (! cddb_disc_calc_discid (cdda))
+    {
+      g_warning ("failed to compute the CDDB disc id.");
+      return FALSE;
+    }
+  
+  return TRUE;
+}
+
+
+
+static cddb_disc_t*
+cddrive_audio_new_cdda (CdIo_t *cdio)
+{
+  cddb_disc_t *res;
+
+  res = cddb_disc_new ();
+  if (res == NULL)
+    g_warning ("not enough memory to get CDDA title.");
+  else
+    {
+      if (! cddrive_audio_fill_cdda (cdio, res))
+        {
+          cddb_disc_destroy (res);
+          res = NULL;
+        }
+    }
+  
+  return res;
+}
+
+
+
+static cddb_conn_t*
+cddrive_audio_new_connection ()
+{
+  cddb_conn_t *res;
+
+  res = cddb_new ();
+  if (res == NULL)
+    {
+      g_warning ("failed to create CDDB connection.");
+      return NULL;
+    }
+  
+  /* Use HTTP rather than CDDB to avoid firewall headaches...
+     as soon as freedb.org accept HTTP requests again. */
+  /*cddb_http_enable (res);*/
+  
+  cddb_set_client (res, PACKAGE, VERSION);
+  
+  /* cache disabled (cache does not work for me, and we only need to cache the title anyway) */
+  cddb_cache_disable (res);
+  
+  return res;
+}
+
+
+
+static gpointer
+cddrive_audio_cache_title_from_server (gpointer data)
+{
+  cddb_disc_t *cdda = (cddb_disc_t*) data;
+  cddb_conn_t *conn;
+  static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
+
+  /* we don't want two connections at the same time, have mercy for freedb.org,
+     nor we want two thread writing in the cache together; hence the mutex. */
+  if (g_static_mutex_trylock (&mutex))
+    {
+      conn = cddrive_audio_new_connection ();
+      if (conn != NULL)
+        {
+          if (cddb_query (conn, cdda) == -1)
+            g_warning ("query on server '%s' failed (%s).",
+                       cddb_get_server_name (conn),
+                       cddb_error_str (cddb_errno (conn)));
+          else
+            {
+              if (cddb_disc_get_title (cdda) != NULL)
+                cddrive_audio_cache_save (cddb_disc_get_discid (cdda),
+                                          cddb_disc_get_title (cdda));
+            }
+      
+          cddb_destroy (conn);
+        }
+      g_static_mutex_unlock (&mutex);
+    }
+  
+  cddb_disc_destroy (cdda);
+  
+  return NULL; /* dummy return value */
+}
+
+
+
+gchar*
+cddrive_audio_get_title (const gchar* device, gboolean connection_allowed)
+{
+  gchar       *res = NULL;
+  cddb_disc_t *cdda;
+  CdIo_t      *cdio;
+
+  g_assert (device != NULL);
+
+  cdio = cddrive_audio_new_cdio (device);
+  if (cdio != NULL)
+    {
+      cdda = cddrive_audio_new_cdda (cdio);
+      if (cdda != NULL)
+        {
+          res = cddrive_audio_cache_read (cddb_disc_get_discid (cdda));
+      
+          if (res == NULL)
+            {
+              res = cddrive_audio_get_cdtext_title (cdio);
+        
+              if (res == NULL && connection_allowed)
+                {
+                  /* the CDDB disc id was not found in cache. Try to fetch it on freedb.org */
+        
+#ifdef HAVE_GTHREAD
+                  /* if possible, fetch the title in a thread, so the plugin do not freeze
+                     while attempting to connect to the server */          
+                  g_thread_create (cddrive_audio_cache_title_from_server,
+                                   cdda,
+                                   FALSE,
+                                   NULL);
+                  /* note: cdda is destroyed in the thread function */
+        
+#else
+                  /* no thread support, plugin freeze will depend on the connection quality */
+                  cddrive_audio_cache_title_from_server (cdda);
+                  res = g_strdup (cddb_disc_get_title (cdda));
+#endif
+                }
+            }
+        }
+      
+      cdio_destroy (cdio);
+    }
+
+  return res;
+}
+
+
+
+void
+cddrive_audio_init_globals ()
+{
+#ifdef HAVE_GTHREAD
+  if (! g_thread_supported ())
+    g_thread_init (NULL);
+#endif
+}
+
+
+
+void
+cddrive_audio_free_globals ()
+{
+  libcddb_shutdown ();
+}
+
+
+
+#else /* ---------- NO CDDB SUPPORT ---------- */
+
+gchar*
+cddrive_audio_get_title (const gchar* device, gboolean connection_allowed)
+{
+  gchar       *res = NULL;
+  CdIo_t      *cdio;
+
+  g_assert (device != NULL);
+
+  cdio = cddrive_audio_new_cdio (device);
+  if (cdio != NULL)
+    {
+      res = cddrive_audio_get_cdtext_title (cdio);
+      cdio_destroy (cdio);
+    }
+  
+  return res;
+}
+
+
+
+void
+cddrive_audio_init_globals ()
+{
+}
+
+
+
+void
+cddrive_audio_free_globals ()
+{
+}
+
+#endif

Copied: xfce4-cddrive-plugin/trunk/panel-plugin/cddrive-audio.h (from rev 2867, xfce4-cddrive-plugin/trunk/panel-plugin/cddrive-cddb.h)
===================================================================
--- xfce4-cddrive-plugin/trunk/panel-plugin/cddrive-audio.h	                        (rev 0)
+++ xfce4-cddrive-plugin/trunk/panel-plugin/cddrive-audio.h	2007-06-27 08:32:25 UTC (rev 2869)
@@ -0,0 +1,59 @@
+/*  $Id$
+ *
+ *  Copyright (c) 2007 The Xfce development team. All rights reserved.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU Library General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#ifndef __CDDRIVE_AUDIO_H__
+#define __CDDRIVE_AUDIO_H__
+
+#include <glib.h>
+
+
+G_BEGIN_DECLS
+
+/* freedb.org support (to get the title of an audio CD) */
+
+
+/* To call at program start */
+void
+cddrive_audio_init_globals ();
+
+
+
+/* Free cddb global resources.
+   To call at program termination. */
+void
+cddrive_audio_free_globals ();
+
+
+
+/* Return the title of the audio CD in the drive of device path 'device',
+   or NULL if the drive have no audio CD, or if the operation failed.
+   
+   If compiled without CDDB support, the function looks for the title using
+   CD-TEXT only.
+   If compiled with CDDB support and 'connection_allowed' is FALSE, the function
+   looks for the title using the cache and CD-TEXT only.
+   Otherwise, the function connects to one of the freedb.org server if cache
+   or CD-TEXT methods failed.
+   
+   The result must be freed after use. */
+gchar*
+cddrive_audio_get_title (const gchar* device, gboolean connection_allowed);
+
+G_END_DECLS
+#endif

Deleted: xfce4-cddrive-plugin/trunk/panel-plugin/cddrive-cddb.c

Deleted: xfce4-cddrive-plugin/trunk/panel-plugin/cddrive-cddb.h

Modified: xfce4-cddrive-plugin/trunk/panel-plugin/cddrive-monitor.c
===================================================================
--- xfce4-cddrive-plugin/trunk/panel-plugin/cddrive-monitor.c	2007-06-27 08:19:49 UTC (rev 2868)
+++ xfce4-cddrive-plugin/trunk/panel-plugin/cddrive-monitor.c	2007-06-27 08:32:25 UTC (rev 2869)
@@ -28,7 +28,7 @@
 #include "cddrive-error.h"
 #include "os-dependant/cddrive-process.h"
 #include "os-dependant/cddrive-cdrom.h"
-#include "cddrive-cddb.h"
+#include "cddrive-audio.h"
 
 
 #define _(message) gettext (message)

Modified: xfce4-cddrive-plugin/trunk/panel-plugin/cddrive.c
===================================================================
--- xfce4-cddrive-plugin/trunk/panel-plugin/cddrive.c	2007-06-27 08:19:49 UTC (rev 2868)
+++ xfce4-cddrive-plugin/trunk/panel-plugin/cddrive.c	2007-06-27 08:32:25 UTC (rev 2869)
@@ -28,7 +28,7 @@
 #include <exo/exo.h> /* for exo_gdk_pixbuf_lucent() */
 #include "cddrive.h"
 #include "cddrive-error.h"
-#include "cddrive-cddb.h"
+#include "cddrive-audio.h"
 #include "cddrive-dialogs.h"
 
 




More information about the Goodies-commits mailing list