[easytag/wip/discid: 2/2] WIP Lookup discID of currently-inserted CD



commit 8a4805aa2a718edcc576bb94aa1f247079d9e567
Author: David King <amigadave amigadave com>
Date:   Wed Jun 12 17:51:53 2013 +0100

    WIP Lookup discID of currently-inserted CD

 Makefile.am      |    2 +
 configure.ac     |    5 +-
 po/POTFILES.in   |    1 +
 src/bar.c        |   13 +++
 src/bar.h        |    1 +
 src/discid.c     |  256 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/discid.h     |   49 ++++++++++
 src/ui_manager.h |    1 +
 8 files changed, 327 insertions(+), 1 deletions(-)
---
diff --git a/Makefile.am b/Makefile.am
index a4fec44..cd35426 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -41,6 +41,7 @@ easytag_SOURCES = \
        src/cddb.c \
        src/charset.c \
        src/crc32.c \
+       src/discid.c \
        src/dlm.c \
        src/easytag.c \
        src/et_core.c \
@@ -80,6 +81,7 @@ easytag_headers = \
        src/cddb.h \
        src/charset.h \
        src/crc32.h \
+       src/discid.h \
        src/dlm.h \
        src/easytag.h \
        src/et_core.h \
diff --git a/configure.ac b/configure.ac
index fb7d8b5..a432d8e 100644
--- a/configure.ac
+++ b/configure.ac
@@ -266,9 +266,12 @@ AS_IF([test "x$have_wavpack" != "xno"],
        AS_IF([test "x$enable_wavpack" = "xyes"],
              [AC_MSG_ERROR([Wavpack support requested but required dependencies ($WAVPACK_DEPS) not 
found])])])
 
+dnl Check for libdiscid
+LIBDISCID_DEPS="libdiscid >= 0.3.0"
+
 dnl Check the pkg-config dependencies
 GIO_DEPS="gio-2.0 >= 2.32.0" dnl For g_file_new_tmp()
-PKG_CHECK_MODULES([EASYTAG], [$GIO_DEPS $GTK_DEPS $OGG_DEPS $SPEEX_DEPS $FLAC_DEPS $ID3TAG_DEPS $TAGLIB_DEPS 
$WAVPACK_DEPS])
+PKG_CHECK_MODULES([EASYTAG], [$GIO_DEPS $GTK_DEPS $OGG_DEPS $SPEEX_DEPS $FLAC_DEPS $ID3TAG_DEPS $TAGLIB_DEPS 
$WAVPACK_DEPS $LIBDISCID_DEPS])
 
 dnl Check for winsock
 AC_SEARCH_LIBS([gethostbyname], [nsl socket], [],
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 9ec12e7..51c761b 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -6,6 +6,7 @@ src/bar.c
 src/browser.c
 src/cddb.c
 src/charset.c
+src/discid.c
 src/easytag.c
 src/et_core.c
 src/flac_header.c
diff --git a/src/bar.c b/src/bar.c
index b2a178f..572422e 100644
--- a/src/bar.c
+++ b/src/bar.c
@@ -24,6 +24,7 @@
 #include <glib/gi18n-lib.h>
 
 #include "bar.h"
+#include "discid.h"
 #include "easytag.h"
 #include "about.h"
 #include "prefs.h"
@@ -68,6 +69,8 @@ static void et_ui_manager_on_disconnect_proxy (GtkUIManager *manager,
 static void on_menu_item_select (GtkMenuItem *item, gpointer user_data);
 static void on_menu_item_deselect (GtkMenuItem *item, gpointer user_data);
 
+static void open_cd_info_dialog (void);
+
 /*************
  * Functions o
  *************/
@@ -279,6 +282,8 @@ void Create_UI (GtkWidget **ppmenubar, GtkWidget **pptoolbar)
           G_CALLBACK (Open_Search_File_Window) },
         { AM_CDDB_SEARCH, GTK_STOCK_CDROM, _("CDD_B Search…"), "<Control>B",
           _("CDDB search"), G_CALLBACK (Open_Cddb_Window) },
+        { AM_READ_CD_INFO, GTK_STOCK_CDROM, _("Read CD Information…"), NULL,
+          _("Read information from a CD"), G_CALLBACK (open_cd_info_dialog) },
         { AM_FILENAME_FROM_TXT, GTK_STOCK_OPEN,
           _("Load Filenames From a Text File…"), "<Control>T",
           _("Load filenames from a text file"),
@@ -716,3 +721,11 @@ on_menu_item_deselect (GtkMenuItem *item, gpointer user_data)
 {
     et_statusbar_pop_tooltip ();
 }
+
+static void
+open_cd_info_dialog (void)
+{
+    EtCDInfoDialog *dialog = et_cd_info_dialog_new (GTK_WINDOW (MainWindow));
+
+    gtk_widget_show (GTK_WIDGET (dialog));
+}
diff --git a/src/bar.h b/src/bar.h
index d189a4e..9e6e65d 100644
--- a/src/bar.h
+++ b/src/bar.h
@@ -96,6 +96,7 @@ GtkWidget *CheckMenuItemBrowseHiddenDirMainMenu;
 #define AM_SEARCH_FILE              "SearchFile"
 #define AM_CDDB_SEARCH_FILE         "CDDBSearchFile"
 #define AM_CDDB_SEARCH              "CDDBSearch"
+#define AM_READ_CD_INFO "ReadCDInfo"
 #define AM_FILENAME_FROM_TXT        "LoadFilenames"
 #define AM_WRITE_PLAYLIST           "WritePlaylist"
 #define AM_RUN_AUDIO_PLAYER         "RunAudio"
diff --git a/src/discid.c b/src/discid.c
new file mode 100644
index 0000000..d6a83d0
--- /dev/null
+++ b/src/discid.c
@@ -0,0 +1,256 @@
+/* EasyTAG - tag editor for audio files
+ * Copyright (C) 2013  David King <amigadave amigadave com>
+ *
+ * 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 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., 51
+ * Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+#include "config.h"
+
+#include "discid.h"
+#include "gtk2_compat.h"
+
+#include <discid/discid.h>
+
+#include <glib/gi18n.h>
+
+G_DEFINE_TYPE (EtCDInfoDialog, et_cd_info_dialog, GTK_TYPE_DIALOG)
+
+static const guint BOX_SPACING = 6;
+
+struct _EtCDInfoDialogPrivate
+{
+    /*< private >*/
+    DiscId *id;
+    GtkWidget *mcn;
+    GtkWidget *length;
+    GtkWidget *n_tracks;
+    GtkListStore *store;
+};
+
+typedef enum
+{
+    COLUMN_NUMBER,
+    COLUMN_OFFSET,
+    COLUMN_LENGTH,
+    COLUMN_ISRC,
+    N_COLUMNS
+} EtCDInfoDialogColumns;
+
+static void et_cd_info_dialog_on_response (EtCDInfoDialog *dialog,
+                                           gint response_id,
+                                           gpointer user_data);
+
+static void
+et_cd_info_dialog_finalize (GObject *object)
+{
+    EtCDInfoDialogPrivate *priv = ET_CD_INFO_DIALOG (object)->priv;
+
+    discid_free (priv->id);
+    priv->id = NULL;
+
+    G_OBJECT_CLASS (et_cd_info_dialog_parent_class)->finalize (object);
+}
+
+static void
+et_cd_info_dialog_init (EtCDInfoDialog *dialog)
+{
+    EtCDInfoDialogPrivate *priv;
+    GtkWidget *content_area;
+    GtkWidget *grid;
+    GtkWidget *treeview;
+    GtkWidget *scrolled;
+    GtkWidget *label;
+    EtCDInfoDialogColumns i;
+    const gchar *column_titles[] = { N_("Track number"), N_("Offset"),
+                                     N_("Length"), N_("ISRC") };
+
+    priv = dialog->priv = G_TYPE_INSTANCE_GET_PRIVATE (dialog,
+                                                       ET_TYPE_CD_INFO_DIALOG,
+                                                       EtCDInfoDialogPrivate);
+
+    priv->id = discid_new ();
+
+    content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
+
+    grid = et_grid_new (4, 2);
+    gtk_grid_set_row_spacing (GTK_GRID (grid), BOX_SPACING);
+    gtk_grid_set_column_spacing (GTK_GRID (grid), BOX_SPACING);
+
+    label = gtk_label_new (_("MCN:"));
+    gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
+    gtk_grid_attach (GTK_GRID (grid), label, 0, 0, 1, 1);
+    label = gtk_label_new (_("Length:"));
+    gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
+    gtk_grid_attach (GTK_GRID (grid), label, 0, 1, 1, 1);
+    label = gtk_label_new (_("Number of tracks:"));
+    gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
+    gtk_grid_attach (GTK_GRID (grid), label, 0, 2, 1, 1);
+
+    priv->mcn = gtk_label_new (NULL);
+    gtk_misc_set_alignment (GTK_MISC (priv->mcn), 0.0, 0.5);
+    gtk_grid_attach (GTK_GRID (grid), priv->mcn, 1, 0, 1, 1);
+    priv->length = gtk_label_new (NULL);
+    gtk_misc_set_alignment (GTK_MISC (priv->length), 0.0, 0.5);
+    gtk_grid_attach (GTK_GRID (grid), priv->length, 1, 1, 1, 1);
+    priv->n_tracks = gtk_label_new (NULL);
+    gtk_misc_set_alignment (GTK_MISC (priv->n_tracks), 0.0, 0.5);
+    gtk_grid_attach (GTK_GRID (grid), priv->n_tracks, 1, 2, 1, 1);
+
+    priv->store = gtk_list_store_new (N_COLUMNS, G_TYPE_INT, G_TYPE_INT,
+                                      G_TYPE_INT, G_TYPE_STRING);
+    treeview = gtk_tree_view_new_with_model (GTK_TREE_MODEL (priv->store));
+    /* GtkListStore is a GObject, so starts with a reference count of 1. */
+    g_object_unref (priv->store);
+    gtk_widget_set_size_request (treeview, -1, 300);
+
+    scrolled = gtk_scrolled_window_new (NULL, NULL);
+    gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled),
+                                    GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
+    gtk_container_add (GTK_CONTAINER (scrolled), treeview);
+    et_grid_attach_full (GTK_GRID (grid), scrolled, 0, 3, 2, 1, FALSE, TRUE, 0,
+                         0);
+
+    for (i = 0; i < N_COLUMNS; i++)
+    {
+        GtkCellRenderer *renderer;
+        GtkTreeViewColumn *column;
+
+        renderer = gtk_cell_renderer_text_new ();
+        column = gtk_tree_view_column_new_with_attributes (_(column_titles[i]),
+                                                           renderer, "text",
+                                                           i,
+                                                           NULL);
+        gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);
+    }
+
+    gtk_container_add (GTK_CONTAINER (content_area), grid);
+    gtk_widget_show_all (grid);
+
+    gtk_dialog_add_buttons (GTK_DIALOG (dialog), GTK_STOCK_CLOSE,
+                            GTK_RESPONSE_CLOSE, _("Read CD"),
+                            GTK_RESPONSE_APPLY, NULL);
+
+    g_signal_connect (dialog, "response",
+                      G_CALLBACK (et_cd_info_dialog_on_response), NULL);
+}
+
+static void
+et_cd_info_dialog_class_init (EtCDInfoDialogClass *klass)
+{
+    G_OBJECT_CLASS (klass)->finalize = et_cd_info_dialog_finalize;
+
+    g_type_class_add_private (klass, sizeof (EtCDInfoDialogPrivate));
+}
+
+/*
+ * et_cd_info_dialog_new:
+ *
+ * Create a new EtCDInfoDialog instance.
+ *
+ * Returns: a new #EtCDInfoDialog
+ */
+EtCDInfoDialog *
+et_cd_info_dialog_new (GtkWindow *parent)
+{
+    return ET_CD_INFO_DIALOG (g_object_new (ET_TYPE_CD_INFO_DIALOG,
+                                            "transient-for", parent, "title",
+                                            _("Read CD Information"), NULL));
+}
+
+/*
+ * et_cd_info_dialog_read_cd:
+ * @dialog: the dialog
+ *
+ * Read information from the inserted CD and populate the CD and track
+ * information structures.
+ *
+ * Returns: %TRUE on success, %FALSE otherwise
+ */
+static gboolean
+et_cd_info_dialog_read_cd (EtCDInfoDialog *dialog)
+{
+    gint first_track;
+    gint last_track;
+    gchar *str;
+    gint i;
+    EtCDInfoDialogPrivate *priv = dialog->priv;
+    DiscId *id = priv->id;
+
+    /* TODO: Run asynchronously in a thread. */
+    if (!discid_read (id, NULL))
+    {
+        /* FIXME: set GError. */
+        g_warning ("Error reading CD: %s", discid_get_error_msg (id));
+        return FALSE;
+    }
+
+    gtk_label_set_text (GTK_LABEL (priv->mcn), discid_get_mcn (id));
+
+    first_track = discid_get_first_track_num (id);
+    last_track = discid_get_last_track_num (id);
+    str = g_strdup_printf ("%i", last_track - first_track);
+    gtk_label_set_text (GTK_LABEL (priv->n_tracks), str);
+    g_free (str);
+
+    str = g_strdup_printf ("%i", discid_get_sectors (id));
+    gtk_label_set_text (GTK_LABEL (priv->length), str);
+    g_free (str);
+
+    gtk_list_store_clear (priv->store);
+
+    for (i = first_track; i < last_track; i++)
+    {
+        gtk_list_store_insert_with_values (priv->store, NULL, G_MAXINT,
+                                           COLUMN_NUMBER, i, COLUMN_OFFSET,
+                                           discid_get_track_offset (id, i),
+                                           COLUMN_LENGTH,
+                                           discid_get_track_length (id, i),
+                                           COLUMN_ISRC,
+                                           discid_get_track_isrc (id, i), -1);
+    }
+
+    return TRUE;
+}
+
+/*
+ * et_cd_info_dialog_on_response:
+ * @dialog: the dialog
+ * @response_id: the #GtkResponseType
+ * @user_data: user data set when the signal was connected
+ *
+ * Handle a response from the dialog, and respond by either closing the dialog
+ * or reading information from a CD.
+ */
+static void
+et_cd_info_dialog_on_response (EtCDInfoDialog *dialog, gint response_id,
+                               gpointer user_data)
+{
+    switch (response_id)
+    {
+        case GTK_RESPONSE_APPLY:
+            if (!et_cd_info_dialog_read_cd (dialog))
+            {
+                g_warning ("Failed to read CD information");
+            }
+            break;
+        case GTK_RESPONSE_CLOSE:
+        case GTK_RESPONSE_DELETE_EVENT:
+            /* TODO: destroy here? */
+            gtk_widget_destroy (GTK_WIDGET (dialog));
+            break;
+        default:
+            g_assert_not_reached ();
+    }
+}
diff --git a/src/discid.h b/src/discid.h
new file mode 100644
index 0000000..68e3bdd
--- /dev/null
+++ b/src/discid.h
@@ -0,0 +1,49 @@
+/* EasyTAG - tag editor for audio files
+ * Copyright (C) 2013  David King <amigadave amigadave com>
+ *
+ * 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 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., 51
+ * Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+#ifndef ET_CD_INFO_DIALOG_H_
+#define ET_CD_INFO_DIALOG_H_
+
+#include <gtk/gtk.h>
+
+G_BEGIN_DECLS
+
+typedef struct _EtCDInfoDialogPrivate EtCDInfoDialogPrivate;
+
+typedef struct
+{
+    /*< private >*/
+    GtkDialog parent_instance;
+    EtCDInfoDialogPrivate *priv;
+} EtCDInfoDialog;
+
+typedef struct
+{
+    /*< private >*/
+    GtkDialogClass parent_class;
+} EtCDInfoDialogClass;
+
+#define ET_TYPE_CD_INFO_DIALOG (et_cd_info_dialog_get_type ())
+#define ET_CD_INFO_DIALOG(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), ET_TYPE_CD_INFO_DIALOG, 
EtCDInfoDialog))
+
+GType et_cd_info_dialog_get_type (void);
+EtCDInfoDialog *et_cd_info_dialog_new (GtkWindow *parent);
+
+G_END_DECLS
+
+#endif /* !ET_CD_INFO_DIALOG_H_ */
diff --git a/src/ui_manager.h b/src/ui_manager.h
index 71411a5..8fcd27b 100644
--- a/src/ui_manager.h
+++ b/src/ui_manager.h
@@ -154,6 +154,7 @@ static const gchar *ui_xml =
 
 "    <menu action='MiscMenu'>"
 "      <menuitem action='CDDBSearch' />"
+"      <menuitem action='ReadCDInfo' />"
 "      <separator />"
 
 "      <menuitem action='LoadFilenames' />"


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]