Re: [gnome-db] Connection Widget / Other stuff



So, here it's a sample (a.k.a. lame & imcomplete) implementation
of a GnomeDbConnection as I tought of it.

There are a couple of things lacking (menu to control the connection,
a "close" signal on GdaConnection, so that we can update the 
GnomeDbConnection automatically, plus some other stuff).

Please tell me wheter to continue this, and also if I should
change GnomeDbConnectionSelector to use this.

Cheers.
/* GNOME DB library
 * Copyright (C) 2002 The GNOME Foundation.
 *
 * AUTHORS:
 *      Cleber Rodrigues <cleber gnome-db org>
 *
 * This Library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This Library 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 Library General Public
 * License along with this Library; see the file COPYING.LIB.  If not,
 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <libgnomedb/gnome-db-connection.h>
#include <gtk/gtklabel.h>
#include <gtk/gtkimage.h>
#include <gtk/gtkstock.h>

#define PARENT_TYPE GTK_TYPE_HBOX

struct _GnomeDbConnectionPrivate {
  GdaConnection *connection;

  /* widgets */
  GtkWidget *label;
  GtkWidget *image;
};

static void gnome_db_connection_init       (GnomeDbConnection      *connection);
static void gnome_db_connection_class_init (GnomeDbConnectionClass *klass);
static void gnome_db_connection_finalize   (GObject *object);

static gchar *gnome_db_connection_label_text_construct   (GnomeDbConnection *cnc);
static void   gnome_db_connection_label_construct_update (GnomeDbConnection *cnc);
static void   gnome_db_connection_image_construct_update (GnomeDbConnection *cnc);

static GObjectClass *parent_class = NULL;

GType
gnome_db_connection_get_type (void)
{
  static GType type = 0;

  if (!type) {
    static const GTypeInfo info = {
      sizeof (GnomeDbConnectionClass),
      (GBaseInitFunc) NULL,
      (GBaseFinalizeFunc) NULL,
      (GClassInitFunc) gnome_db_connection_class_init,
      NULL,
      NULL,
      sizeof (GnomeDbConnection),
      0,
      (GInstanceInitFunc) gnome_db_connection_init
    };
    type = g_type_register_static (PARENT_TYPE, "GnomeDbConnection", &info, 0);
  }
  return type;
}

static void
gnome_db_connection_init (GnomeDbConnection *connection)
{
  g_return_if_fail (GNOME_DB_IS_CONNECTION (connection));

  connection->priv = g_new0 (GnomeDbConnectionPrivate, 1);
  connection->priv->label = NULL;
  connection->priv->image = NULL;
}

static void
gnome_db_connection_class_init (GnomeDbConnectionClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  parent_class = g_type_class_peek_parent (klass);
  
  object_class->finalize = gnome_db_connection_finalize;
}

static void
gnome_db_connection_finalize (GObject *object)
{
  GnomeDbConnection *connection = (GnomeDbConnection *) object;

  g_return_if_fail (GNOME_DB_IS_CONNECTION (connection));

  gtk_widget_destroy (connection->priv->label);
  gtk_widget_destroy (connection->priv->image);

  g_free (connection->priv);
  connection->priv = NULL;

  parent_class->finalize (object);
}

/* This handles creating AND updating of the widget.
 * I know you know that, I just said so I look a bit less lame */
static void
gnome_db_connection_label_construct_update (GnomeDbConnection *cnc)
{
  g_return_if_fail (GNOME_DB_IS_CONNECTION (cnc));

  if (gda_connection_is_open (cnc->priv->connection)) {
    gchar *buff;
    buff = gnome_db_connection_label_text_construct (cnc);
    if (cnc->priv->label == NULL) {
      cnc->priv->label = gtk_label_new (buff);
    } else {
      gtk_label_set_text (GTK_LABEL (cnc->priv->label),
			  buff);
    }
    g_free (buff);
  } else {
    if (cnc->priv->label == NULL) {
      cnc->priv->label = gtk_label_new ("This connection is closed");
    } else {
      gtk_label_set_text (GTK_LABEL (cnc->priv->label),
			  "This connection is closed");
    }
  }
}


static void
gnome_db_connection_image_construct_update (GnomeDbConnection *cnc)
{
  g_return_if_fail (GNOME_DB_IS_CONNECTION (cnc));

  if (gda_connection_is_open (cnc->priv->connection)) {
    if (cnc->priv->image == NULL) {
      cnc->priv->image = gtk_image_new_from_stock (GTK_STOCK_YES,
						   GTK_ICON_SIZE_BUTTON);
    } else {
      gtk_image_set_from_stock (GTK_IMAGE (cnc->priv->image),
				GTK_STOCK_YES, 
				GTK_ICON_SIZE_BUTTON);
    }
  } else {

    if (cnc->priv->image == NULL) {
      cnc->priv->image = gtk_image_new_from_stock (GTK_STOCK_NO,
						   GTK_ICON_SIZE_BUTTON);
    } else {
      gtk_image_set_from_stock (GTK_IMAGE (cnc->priv->image),
				GTK_STOCK_NO, 
				GTK_ICON_SIZE_BUTTON);
    }
  }
}

static gchar *
gnome_db_connection_label_text_construct (GnomeDbConnection *cnc)
{
  gchar *buff;

  g_return_val_if_fail (GDA_IS_CONNECTION (cnc->priv->connection), NULL);

  buff = g_strdup_printf ("Datasource %s,\nUser %s, on %s provider",
			  gda_connection_get_dsn (cnc->priv->connection),
			  gda_connection_get_username (cnc->priv->connection),
			  gda_connection_get_provider (cnc->priv->connection));

  return buff;
}

/* Public functions */

GtkWidget *
gnome_db_connection_new (GdaConnection *cnc)
{
  GtkWidget *connection_box;
  GnomeDbConnection *connection;

  g_return_val_if_fail (GDA_IS_CONNECTION (cnc), NULL);

  connection = g_object_new (GNOME_DB_TYPE_CONNECTION, NULL);

  connection_box = (GtkWidget *) connection;
  connection_box = gtk_hbox_new (FALSE, 5);

  gnome_db_connection_label_construct_update (connection);
  gnome_db_connection_image_construct_update (connection);

  gtk_box_pack_start (GTK_BOX (connection_box), connection->priv->label, TRUE, TRUE, 5);
  gtk_box_pack_start (GTK_BOX (connection_box), connection->priv->image, TRUE, TRUE, 5);

  gtk_widget_show_all (connection_box);

  return (GtkWidget *) connection;
}

void
gnome_db_connection_update (GnomeDbConnection *cnc)
{
  g_return_if_fail (GNOME_DB_IS_CONNECTION (cnc));

  gnome_db_connection_label_construct_update (cnc);
  gnome_db_connection_image_construct_update (cnc);
}

GdaConnection *
gnome_db_connection_get_connection (GnomeDbConnection *cnc) 
{
  g_return_val_if_fail (GNOME_DB_IS_CONNECTION (cnc), NULL);

  return cnc->priv->connection;
}

gboolean
gnome_db_connection_close (GnomeDbConnection *cnc)
{
  g_return_val_if_fail (GNOME_DB_IS_CONNECTION (cnc), FALSE);

  return gda_connection_close (cnc->priv->connection);
}
/* GNOME DB library
 * Copyright (C) 2002 The GNOME Foundation.
 *
 * AUTHORS:
 *      Cleber Rodrigues <cleber gnome-db org>
 *
 * This Library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This Library 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 Library General Public
 * License along with this Library; see the file COPYING.LIB.  If not,
 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#if !defined(__gnome_db_connection_h__)
#  define __gnome_db_connection_h__

#include <gtk/gtkhbox.h>
#include <libgda/gda-connection.h>

G_BEGIN_DECLS

#define GNOME_DB_TYPE_CONNECTION            (gnome_db_connection_get_type())
#define GNOME_DB_CONNECTION(obj)            (G_TYPE_CHECK_INSTANCE_CAST (obj, GNOME_DB_TYPE_CONNECTION, GnomeDbConnection))
#define GNOME_DB_CONNECTION_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST (klass, GNOME_DB_TYPE_CONNECTION, GnomeDbConnectionClass))
#define GNOME_DB_IS_CONNECTION(obj)         (G_TYPE_CHECK_INSTANCE_TYPE (obj, GNOME_DB_TYPE_CONNECTION))
#define GNOME_DB_IS_CONNECTION_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GNOME_DB_TYPE_CONNECTION))

typedef struct _GnomeDbConnection        GnomeDbConnection;
typedef struct _GnomeDbConnectionClass   GnomeDbConnectionClass;
typedef struct _GnomeDbConnectionPrivate GnomeDbConnectionPrivate;

struct _GnomeDbConnection {
  GtkHBox box;
  GnomeDbConnectionPrivate *priv;
};

struct _GnomeDbConnectionClass {
  GtkHBoxClass parent_class;
};

GType          gnome_db_connection_get_type (void);
GtkWidget     *gnome_db_connection_new (GdaConnection *cnc);
void           gnome_db_connection_update (GnomeDbConnection *cnc)
GdaConnection *gnome_db_connection_get_connection (GnomeDbConnection *cnc);
gboolean       gnome_db_connection_close (GnomeDbConnection *cnc);

G_END_DECLS

#endif


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