[tracker] tracker-sparql: Added --notifies to list classes which notify changes



commit e44a9cc692cd97e956c93ee5b317fda1c216c88d
Author: Martyn Russell <martyn lanedo com>
Date:   Wed Mar 10 11:35:35 2010 +0000

    tracker-sparql: Added --notifies to list classes which notify changes

 docs/manpages/tracker-sparql.1     |   42 +++---
 src/tracker-utils/tracker-sparql.c |  260 ++++++++++++++++++++++++------------
 2 files changed, 194 insertions(+), 108 deletions(-)
---
diff --git a/docs/manpages/tracker-sparql.1 b/docs/manpages/tracker-sparql.1
index 2c1ae5b..e65c421 100644
--- a/docs/manpages/tracker-sparql.1
+++ b/docs/manpages/tracker-sparql.1
@@ -21,13 +21,15 @@ does not have to be an absolute path.
 Show summary of options.
 .TP
 .B \-f, \-\-file=FILE
-Use a file with SPARQL content to query.
+Use a \fIFILE\fR with SPARQL content to query or update.
 .TP
 .B \-q, \-\-query=SPARQL
-Use a SPARQL string to query the database with.
+Use a \fISPARQL\fR string to query the database with.
 .TP
 .B \-u, \-\-update
-This has to be used with \-\-query. This tells
+This has to be used with 
+.B \-\-query.
+This tells
 .B tracker-sparql
 to use the SPARQL update extensions so it knows it isn't a regular
 data lookup request. So if your query is intended to change data in
@@ -36,17 +38,14 @@ the database, this option is needed.
 .B \-c, \-\-list-classes
 Returns a list of classes which describe the ontology used for storing
 data. These classes are also used in queries. For example,
-.B http://www.w3.org/2000/01/rdf-schema#Resource
-is one of many classes which should be returned here.
+\fIhttp://www.w3.org/2000/01/rdf-schema#Resource\fR is one of many
+classes which should be returned here.
 .TP
 .B \-x, \-\-list-class-prefixes
 Returns a list of classes and their related prefixes. Prefixes are
 used to make querying a lot simpler and are much like an alias. For
-example,
-.B http://www.w3.org/2000/01/rdf-schema#Resource
-has the prefix
-.B rdfs
-so queries can be cut down to:
+example, \fIhttp://www.w3.org/2000/01/rdf-schema#Resource\fR has the
+prefix \fIrdfs\fR so queries can be cut down to:
 
 "SELECT ?u WHERE { ?u a rdfs:Resource }"
 
@@ -54,9 +53,8 @@ so queries can be cut down to:
 .B \-p, \-\-list-properties=CLASS
 Returns a list of properties which pertain to a class. You can use
 both formats here for the class, either the full name
-.B http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#Video
-or the shortened prefix name
-.B nfo:Video.
+\fIhttp://www.semanticdesktop.org/ontologies/2007/03/22/nfo#Video\fR
+or the shortened prefix name \fInfo:Video\fR.
 
 This gives the following result:
 
@@ -68,17 +66,21 @@ Properties: 2
   http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#frameCount
 .FI
 
-These properties
-.B nfo:frameRate
-and
-.B nfo:frameCount
-can be used in more complex queries (see --query).
+These properties \fInfo:frameRate\fR and \fInfo:frameCount\fR can be
+used in more complex queries (see --query).
+
+.TP
+.B \-n, \-\-list-notifies=CLASS
+Returns a list of classes which are notified over D-Bus about any
+changes that occur in the database. \fICLASS\fR does not have to be
+supplied here. This is optional and filters the results according to
+any argument supplied. With no \fICLASS\fR, all classes are listed.
 
 .TP
 .B \-s, \-\-search=TERM
 Returns a list of classes and properties which partially match
-.B TERM
-in the ontology. This is a case insensitive match, for example:
+\fITERM\fR in the ontology. This is a case insensitive match, for
+example:
 
 .NF
 $ tracker-sparql -s text
diff --git a/src/tracker-utils/tracker-sparql.c b/src/tracker-utils/tracker-sparql.c
index 7b55d45..89a45d1 100644
--- a/src/tracker-utils/tracker-sparql.c
+++ b/src/tracker-utils/tracker-sparql.c
@@ -41,14 +41,20 @@
 	"\n" \
 	"  http://www.gnu.org/licenses/gpl.txt\n";
 
-static gchar    *file;
-static gchar    *query;
-static gboolean  update;
-static gboolean  list_classes;
-static gboolean  list_class_prefixes;
-static gchar    *list_properties;
-static gboolean  print_version;
-static gchar    *search;
+static gboolean parse_list_notifies (const gchar  *option_name,
+                                     const gchar  *value,
+                                     gpointer      data,
+                                     GError      **error);
+
+static gchar *file;
+static gchar *query;
+static gboolean update;
+static gboolean list_classes;
+static gboolean list_class_prefixes;
+static gchar *list_properties;
+static gchar *list_notifies;
+static gboolean print_version;
+static gchar *search;
 
 static GOptionEntry   entries[] = {
 	{ "file", 'f', 0, G_OPTION_ARG_FILENAME, &file,
@@ -75,6 +81,10 @@ static GOptionEntry   entries[] = {
 	  N_("Retrieve properties for a class, prefixes can be used too (e.g. rdfs:Resource)"),
 	  N_("CLASS"),
 	},
+	{ "list-notifies", 'n', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK, parse_list_notifies,
+	  N_("Retrieve classes which notify changes in the database (CLASS is optional)"),
+	  N_("CLASS"),
+	},
 	{ "search", 's', 0, G_OPTION_ARG_STRING, &search,
 	  N_("Search for a class or property and display more information (e.g. Document)"),
 	  N_("CLASS/PROPERTY"),
@@ -161,6 +171,21 @@ results_foreach (gpointer value,
 	g_print ("\n");
 }
 
+static gboolean
+parse_list_notifies (const gchar  *option_name,
+                     const gchar  *value,
+                     gpointer      data,
+                     GError      **error)
+{
+        if (!value) {
+	        list_notifies = g_strdup ("");
+        } else {
+	        list_notifies = g_strdup (value);
+        }
+
+        return TRUE;
+}
+
 int
 main (int argc, char **argv)
 {
@@ -187,12 +212,21 @@ main (int argc, char **argv)
 		return EXIT_SUCCESS;
 	}
 
-	if (!list_classes && !list_class_prefixes && !list_properties && !search &&
-	    ((!file && !query) || (file && query))) {
+	const gchar *error_message;
+
+	if (!list_classes && !list_class_prefixes && !list_properties && 
+	    !list_notifies && !search && !file && !query) {
+		error_message = _("An argument must be supplied");
+	} else if (file && query) {
+		error_message = _("File and query can not be used together");
+	} else {
+		error_message = NULL;
+	}
+
+	if (error_message) {
 		gchar *help;
 
-		g_printerr ("%s\n\n",
-		            _("Either a file or query needs to be specified"));
+		g_printerr ("%s\n\n", error_message);
 
 		help = g_option_context_get_help (context, TRUE, NULL);
 		g_option_context_free (context);
@@ -283,79 +317,6 @@ main (int argc, char **argv)
 		}
 	}
 
-	if (search) {
-		gchar *query;
-
-		/* First list classes */
-		query = g_strdup_printf ("SELECT ?c "
-		                         "WHERE {"
-		                         "  ?c a rdfs:Class"
-		                         "  FILTER regex (?c, \"%s\", \"i\") "
-		                         "}",
-		                         search);
-		results = tracker_resources_sparql_query (client, query, &error);
-		g_free (query);
-
-		if (error) {
-			g_printerr ("%s, %s\n",
-			            _("Could not search classes"),
-			            error->message);
-			g_clear_error (&error);
-		} else {
-			if (!results) {
-				g_print ("%s\n",
-				         _("No classes were found to match search term"));
-			} else {
-				g_print (g_dngettext (NULL,
-				                      "Class: %d",
-				                      "Classes: %d",
-				                      results->len),
-				         results->len);
-				g_print ("\n");
-
-				g_ptr_array_foreach (results, results_foreach, NULL);
-				g_ptr_array_foreach (results, (GFunc) g_strfreev, NULL);
-				g_ptr_array_free (results, TRUE);
-			}
-		}
-
-		/* Second list properties */
-		query = g_strdup_printf ("SELECT ?p "
-		                         "WHERE {"
-		                         "  ?p a rdf:Property"
-		                         "  FILTER regex (?p, \"%s\", \"i\") "
-		                         "}",
-		                         search);
-
-		results = tracker_resources_sparql_query (client, query, &error);
-		g_free (query);
-
-		if (error) {
-			g_printerr ("  %s, %s\n",
-			            _("Could not search properties"),
-			            error->message);
-			g_clear_error (&error);
-		} else {
-			if (!results) {
-				g_print ("%s\n",
-				         _("No properties were found to match search term"));
-			} else {
-				g_print (g_dngettext (NULL,
-				                      "Property: %d",
-				                      "Properties: %d",
-				                      results->len),
-				         results->len);
-				g_print ("\n");
-
-				g_ptr_array_foreach (results, results_foreach, NULL);
-				g_ptr_array_foreach (results, (GFunc) g_strfreev, NULL);
-				g_ptr_array_free (results, TRUE);
-			}
-		}
-
-		return EXIT_SUCCESS;
-	}
-
 	if (list_properties) {
 		gchar *query;
 		gchar *class_name;
@@ -377,6 +338,7 @@ main (int argc, char **argv)
 				              "e.g. :Resource in 'rdfs:Resource'"));
 				g_free (prefix);
 				g_object_unref (client);
+
 				return EXIT_FAILURE;
 			}
 
@@ -389,6 +351,7 @@ main (int argc, char **argv)
 			if (!class_name_no_property) {
 				g_free (property);
 				g_object_unref (client);
+
 				return EXIT_FAILURE;
 			}
 
@@ -435,6 +398,124 @@ main (int argc, char **argv)
 		}
 	}
 
+	if (list_notifies) {
+		gchar *query;
+
+		/* First list classes */
+		if (*list_notifies == '\0') {
+			query = g_strdup_printf ("SELECT ?c "
+			                         "WHERE {"
+			                         "  ?c a rdfs:Class ."
+			                         "  ?c tracker:notify true ."
+			                         "}");
+		} else {
+			query = g_strdup_printf ("SELECT ?c "
+			                         "WHERE {"
+			                         "  ?c a rdfs:Class ."
+			                         "  ?c tracker:notify true "
+			                         "  FILTER regex (?c, \"%s\", \"i\") "
+			                         "}",
+			                         list_notifies);
+		}
+
+		results = tracker_resources_sparql_query (client, query, &error);
+		g_free (query);
+
+		if (error) {
+			g_printerr ("%s, %s\n",
+			            _("Could not find notify classes"),
+			            error->message);
+			g_clear_error (&error);
+		} else {
+			if (!results) {
+				g_print ("%s\n",
+				         _("No notifies were found"));
+			} else {
+				g_print (g_dngettext (NULL,
+				                      "Notify: %d",
+				                      "Notifies: %d",
+				                      results->len),
+				         results->len);
+				g_print ("\n");
+
+				g_ptr_array_foreach (results, results_foreach, NULL);
+				g_ptr_array_foreach (results, (GFunc) g_strfreev, NULL);
+				g_ptr_array_free (results, TRUE);
+			}
+		}
+	}
+
+	if (search) {
+		gchar *query;
+
+		/* First list classes */
+		query = g_strdup_printf ("SELECT ?c "
+		                         "WHERE {"
+		                         "  ?c a rdfs:Class"
+		                         "  FILTER regex (?c, \"%s\", \"i\") "
+		                         "}",
+		                         search);
+		results = tracker_resources_sparql_query (client, query, &error);
+		g_free (query);
+
+		if (error) {
+			g_printerr ("%s, %s\n",
+			            _("Could not search classes"),
+			            error->message);
+			g_clear_error (&error);
+		} else {
+			if (!results) {
+				g_print ("%s\n",
+				         _("No classes were found to match search term"));
+			} else {
+				g_print (g_dngettext (NULL,
+				                      "Class: %d",
+				                      "Classes: %d",
+				                      results->len),
+				         results->len);
+				g_print ("\n");
+
+				g_ptr_array_foreach (results, results_foreach, NULL);
+				g_ptr_array_foreach (results, (GFunc) g_strfreev, NULL);
+				g_ptr_array_free (results, TRUE);
+			}
+		}
+
+		/* Second list properties */
+		query = g_strdup_printf ("SELECT ?p "
+		                         "WHERE {"
+		                         "  ?p a rdf:Property"
+		                         "  FILTER regex (?p, \"%s\", \"i\") "
+		                         "}",
+		                         search);
+
+		results = tracker_resources_sparql_query (client, query, &error);
+		g_free (query);
+
+		if (error) {
+			g_printerr ("  %s, %s\n",
+			            _("Could not search properties"),
+			            error->message);
+			g_clear_error (&error);
+		} else {
+			if (!results) {
+				g_print ("%s\n",
+				         _("No properties were found to match search term"));
+			} else {
+				g_print (g_dngettext (NULL,
+				                      "Property: %d",
+				                      "Properties: %d",
+				                      results->len),
+				         results->len);
+				g_print ("\n");
+
+				g_ptr_array_foreach (results, results_foreach, NULL);
+				g_ptr_array_foreach (results, (GFunc) g_strfreev, NULL);
+				g_ptr_array_free (results, TRUE);
+			}
+		}
+	}
+
 	if (file) {
 		gchar *path_in_utf8;
 		gsize size;
@@ -477,7 +558,7 @@ main (int argc, char **argv)
 				            error->message);
 				g_error_free (error);
 
-				return FALSE;
+				return EXIT_FAILURE;
 			}
 
 			if (results) {
@@ -496,7 +577,10 @@ main (int argc, char **argv)
 						g_hash_table_iter_init (&iter, solution);
 						n = 0;
 						while (g_hash_table_iter_next (&iter, &key, &value)) {
-							g_print ("%s%s: %s", n > 0 ? ", " : "", (const gchar *) key, (const gchar *) value);
+							g_print ("%s%s: %s", 
+							         n > 0 ? ", " : "", 
+							         (const gchar *) key, 
+							         (const gchar *) value);
 							n++;
 						}
 						g_print ("\n");
@@ -512,7 +596,7 @@ main (int argc, char **argv)
 				            error->message);
 				g_error_free (error);
 
-				return FALSE;
+				return EXIT_FAILURE;
 			}
 
 			if (!results) {



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