File: gobj-example.c.html

package info (click to toggle)
qof 0.7.5-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 4,436 kB
  • ctags: 3,587
  • sloc: ansic: 33,810; sh: 9,356; xml: 487; makefile: 405; yacc: 184; lex: 123; sed: 16
file content (111 lines) | stat: -rw-r--r-- 3,511 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en_gb">
<head>
<title>QOF GObject Usage Example</title>
<link rel="shortcut icon" href="favicon.png">
<link rel="stylesheet" type="text/css" href="qof.css">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<h1>Query Over a Collection of GObjects</h1>
<div id="resource">
<ul>
<li><a href="index.html">Home</a></li>
<li><a href="http://sourceforge.net/projects/qof/">SourceForge</a></li>
<li><a href="index.html#docs">Documentation</a></li>
<li><a href="http://qof-gen.sourceforge.net/">QOF-Generator</a></li>
<li><a href="my-obj-example.c.html">Object queries</a></li>
<li><a href="why-qof.html">Why QOF?</a></li>
<li><a href="http://www.data-freedom.org/">Data Freedom</a></li>
<li><a href="index.html#support">Support</a></li>
</ul>
</div>

<div><p>The example below shows how to perform an SQL query over 
a collection of Glib GObjects.  Three 
<b><tt>GtkButton</tt></b> objects are created, each with
a different label.  Then the SQL statement
<b><tt>SELECT * FROM GtkButton WHERE (label='asdfasdf');</tt></b>
is performed on this collection.  The one button with the
matching label is returned.</p>
</div>
<div class="clear">&nbsp;</div>
<pre>
/** @file gobj-example.c
 *  @brief Example usage of QOF to query a set of GObjects.
 *  @author Copyright (c) 2004 Linas Vepstas &lt;linas@linas.org&gt;
 *
 * This example program shows how to use QOF to query over a set
 * of GObjects.  In this example, the query is performed over
 * three GtkButtons, each having a different label.  The result
 * of the search is the one GtkButton with the desired label.
 *
 * The use of GtkButton in this example is artificial.  GLib
 * GObjects do not have to be GUI objects, and can be created 
 * for any use desired.  I didn't want to turn this into an 
 * example of how to create a GObject, and so used a pre-existing
 * GObject, the GtkButton.
 */

#include &lt;qof/qof.h&gt;
#include &lt;gtk/gtk.h&gt;

int
main(int argc, char *argv[])
{
	g_type_init ();
	gtk_init (&argc, &argv);
	qof_gobject_init ();

	/* Register a new searchable type */
	GtkWidget *w = gtk_button_new_with_label ("Howdy Doody");
	GtkButtonClass * wc = GTK_BUTTON_GET_CLASS(w);
	GObjectClass *goc = G_OBJECT_CLASS(wc);

	qof_gobject_register ("MyGtkButton", goc);

	/* Add a number of instances to the collection */
	QofBook *book =  qof_book_new();
	qof_gobject_register_instance (book, "MyGtkButton", G_OBJECT(w));

	w = gtk_button_new_with_label ("dorf");
	qof_gobject_register_instance (book, "MyGtkButton", G_OBJECT(w));

	w = gtk_button_new_with_label ("zinger");
	qof_gobject_register_instance (book, "MyGtkButton", G_OBJECT(w));

	printf ("\nCreated three objects and registered them\n\n");

	/* Create a new query, run that query */
	QofSqlQuery *q;
	q =  qof_sql_query_new ();
 
	/* Set the book to be searched */
	qof_sql_query_set_book(q, book);

	char * qstr = "SELECT * FROM MyGtkButton WHERE (label = 'dorf')";
	printf ("Going to perform the query %s\n", qstr); 

	GList *results = qof_sql_query_run (q, qstr);
	printf ("\nQuery returned %d results\n", g_list_length (results));

	GList *n;
	for (n=results; n; n=n-&gt;next)
	{
		GtkWidget *qw = n-&gt;data;
		if (GTK_IS_BUTTON(qw))
		{
			GtkButton *bw = GTK_BUTTON (qw);
			printf ("Found a button whose label is %s\n", gtk_button_get_label(bw));
		}
	}

	qof_gobject_shutdown ();
	return 0;
}

/* =================== END OF FILE ===================== */
</pre>

</body>
</html>