[tracker/tracker-0.8] tracker-search: Added --contacts



commit 4ab34e73502e1602fe9663511ef17d0fff290bc6
Author: Martyn Russell <martyn lanedo com>
Date:   Thu May 6 11:22:12 2010 +0100

    tracker-search: Added --contacts

 docs/manpages/tracker-search.1     |   10 ++-
 src/tracker-utils/tracker-search.c |  128 +++++++++++++++++++++++++++++++++++-
 2 files changed, 133 insertions(+), 5 deletions(-)
---
diff --git a/docs/manpages/tracker-search.1 b/docs/manpages/tracker-search.1
index 3bd342a..2c29871 100644
--- a/docs/manpages/tracker-search.1
+++ b/docs/manpages/tracker-search.1
@@ -56,10 +56,10 @@ Search for folders matching \fIEXPRESSION\fR (optional).
 .B \-m, \-\-music=EXPRESSION
 Search for music files matching \fIEXPRESSION\fR (optional).
 .TP
-.B \-u, \-\-music\-albums=ALBUM
+.B      \-\-music\-albums=ALBUM
 Search for music albums matching \fIALBUM\fR (optional).
 .TP
-.B \-c, \-\-music\-artists=ARTIST
+.B      \-\-music\-artists=ARTIST
 Search for music artists matching \fIARTIST\fR (optional).
 .TP
 .B \-l, \-\-images=EXPRESSION
@@ -71,10 +71,14 @@ Search for videos matching \fIEXPRESSION\fR (optional).
 .B \-t, \-\-documents=EXPRESSION
 Search for documents matching \fIEXPRESSION\fR (optional).
 .TP
-.B \-t, \-\-emails=EXPRESSION
+.B \-e, \-\-emails=EXPRESSION
 Search for emails matching \fIEXPRESSION\fR (optional). Returns a list
 of subjects for emails found.
 .TP
+.B \-c, \-\-contacts=EXPRESSION
+Search for contacts matching \fIEXPRESSION\fR (optional). Returns a list
+of names and email addresses found.
+.TP
 .B \-V, \-\-version
 Print version.
 
diff --git a/src/tracker-utils/tracker-search.c b/src/tracker-utils/tracker-search.c
index aa9bf4e..e440256 100644
--- a/src/tracker-utils/tracker-search.c
+++ b/src/tracker-utils/tracker-search.c
@@ -55,6 +55,7 @@ static gboolean image_files;
 static gboolean video_files;
 static gboolean document_files;
 static gboolean emails;
+static gboolean contacts;
 static gboolean print_version;
 
 static GOptionEntry entries[] = {
@@ -90,11 +91,11 @@ static GOptionEntry entries[] = {
 	  N_("Search for music files"),
 	  NULL
 	},
-	{ "music-albums", 'u', 0, G_OPTION_ARG_NONE, &music_albums,
+	{ "music-albums", 0, 0, G_OPTION_ARG_NONE, &music_albums,
 	  N_("Search for music albums (--all has no effect on this)"),
 	  NULL
 	},
-	{ "music-artists", 'c', 0, G_OPTION_ARG_NONE, &music_artists,
+	{ "music-artists", 0, 0, G_OPTION_ARG_NONE, &music_artists,
 	  N_("Search for music artists (--all has no effect on this) "),
 	  NULL
 	},
@@ -114,6 +115,10 @@ static GOptionEntry entries[] = {
 	  N_("Search for emails"),
 	  NULL
 	},
+	{ "contacts", 'c', 0, G_OPTION_ARG_NONE, &contacts,
+	  N_("Search for contacts"),
+	  NULL
+	},
 	{ "version", 'V', 0, G_OPTION_ARG_NONE, &print_version,
 	  N_("Print version"),
 	  NULL
@@ -172,6 +177,115 @@ get_fts_string (GStrv    search_words,
 }
 
 static void
+get_contacts_foreach (gpointer value,
+		    gpointer user_data)
+{
+	gchar **data;
+	gboolean details;
+
+	data = value;
+	details = GPOINTER_TO_INT (user_data);
+
+	if (details && data[1] && *data[1]) {
+		g_print ("  %s, %s (%s)\n", data[0], data[1], data[2]);
+	} else {
+		g_print ("  %s, %s\n", data[0], data[1]);
+	}
+}
+
+static gboolean
+get_contacts_results (TrackerClient *client,
+		      const gchar   *query,
+		      gint           search_limit,
+		      gboolean       details)
+{
+	GError *error = NULL;
+	GPtrArray *results;
+
+	results = tracker_resources_sparql_query (client, query, &error);
+
+	if (error) {
+		g_printerr ("%s, %s\n",
+		            _("Could not get search results"),
+		            error->message);
+		g_error_free (error);
+
+		return FALSE;
+	}
+
+	if (!results) {
+		g_print ("%s\n",
+		         _("No contacts were found"));
+	} else {
+		g_print (g_dngettext (NULL,
+		                      "Contact: %d",
+		                      "Contacts: %d",
+		                      results->len),
+		         results->len);
+		g_print ("\n");
+
+		g_ptr_array_foreach (results,
+		                     get_contacts_foreach,
+		                     GINT_TO_POINTER (details));
+
+		if (results->len >= search_limit) {
+			show_limit_warning ();
+		}
+
+		g_ptr_array_foreach (results, (GFunc) g_strfreev, NULL);
+		g_ptr_array_free (results, TRUE);
+	}
+
+	return TRUE;
+}
+
+static gboolean
+get_contacts (TrackerClient *client,
+	      GStrv          search_terms,
+	      gboolean       show_all,
+	      gint           search_offset,
+	      gint           search_limit,
+	      gboolean       use_or_operator,
+	      gboolean       details)
+{
+	gchar *fts;
+	gchar *query;
+	gboolean success;
+
+	fts = get_fts_string (search_terms, use_or_operator);
+
+	if (fts) {
+		query = g_strdup_printf ("SELECT tracker:coalesce(nco:fullname(?contact), \"Unknown\") nco:hasEmailAddress(?contact) ?contact "
+		                         "WHERE { "
+		                         "  ?contact a nco:Contact ;"
+		                         "  fts:match \"%s\" ."
+		                         "} "
+		                         "ORDER BY ASC(nco:fullname(?contact)) ASC(nco:hasEmailAddress(?contact)) "
+		                         "OFFSET %d "
+		                         "LIMIT %d",
+		                         fts,
+		                         search_offset,
+		                         search_limit);
+	} else {
+		query = g_strdup_printf ("SELECT tracker:coalesce(nco:fullname(?contact), \"Unknown\") nco:hasEmailAddress(?contact) ?contact "
+		                         "WHERE { "
+		                         "  ?contact a nco:Contact ."
+		                         "} "
+		                         "ORDER BY ASC(nco:fullname(?contact)) ASC(nco:hasEmailAddress(?contact)) "
+		                         "OFFSET %d "
+		                         "LIMIT %d",
+		                         search_offset,
+		                         search_limit);
+	}
+
+	success = get_contacts_results (client, query, search_limit, details);
+	g_free (query);
+	g_free (fts);
+
+	return success;
+}
+
+static void
 get_emails_foreach (gpointer value,
 		    gpointer user_data)
 {
@@ -1018,6 +1132,7 @@ main (int argc, char **argv)
 	    !video_files &&
 	    !document_files &&
 	    !emails &&
+	    !contacts &&
 	    !files && !folders &&
 	    !terms) {
 		gchar *help;
@@ -1175,6 +1290,15 @@ main (int argc, char **argv)
 		return success ? EXIT_SUCCESS : EXIT_FAILURE;
 	}
 
+	if (contacts) {
+		gboolean success;
+
+		success = get_contacts (client, terms, all, offset, limit, or_operator, detailed);
+		g_object_unref (client);
+
+		return success ? EXIT_SUCCESS : EXIT_FAILURE;
+	}
+
 	if (terms) {
 		gboolean success;
 



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