[nautilus-sendto] Implement setting the title and flesh out test



commit 56a921fbd4fb2ccd13f348b917c219468453a6f2
Author: Bastien Nocera <hadess hadess net>
Date:   Wed Aug 18 15:55:12 2010 +0100

    Implement setting the title and flesh out test
    
    Turns out setting the title also requires the number of folders
    if we're going to differentiate them in the UI.
    
    Implement test case in a way that doesn't crash.

 src/Makefile.am                |    1 +
 src/nautilus-sendto-command.c  |   11 +++---
 src/nautilus-sendto-mimetype.c |   61 ++++++++++++++++++++++++++++++++-
 src/nautilus-sendto-mimetype.h |    3 +-
 src/test-mimetype-data         |   57 ++++++++++++++++++++++++++++++
 src/test-mimetype.c            |   74 ++++++++++++++++++++++++++++++---------
 src/totem-mime-types.h         |   58 +++++++++++++++++++++++++++++++
 7 files changed, 241 insertions(+), 24 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 8a97328..cf482e6 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -10,6 +10,7 @@ INCLUDES =					\
 	-I$(top_srcdir)/src/plugins		\
 	-DUIDIR=\""$(uidir)"\"			\
 	-DLOCALEDIR="\"$(datadir)/locale\""	\
+	-DTEST_SRCDIR=\""$(srcdir)/"\"		\
 	$(NAUTILUS_SENDTO_CFLAGS)		\
 	$(NAUTILUS_EXT_SENDTO_CFLAGS)		\
 	$(DISABLE_DEPRECATED)			\
diff --git a/src/nautilus-sendto-command.c b/src/nautilus-sendto-command.c
index a3ac0ee..f3042b7 100644
--- a/src/nautilus-sendto-command.c
+++ b/src/nautilus-sendto-command.c
@@ -52,7 +52,7 @@ static PeasExtensionSet *exten_set;
 
 GList *file_list = NULL;
 char **mime_types = NULL;
-gboolean has_dirs = FALSE;
+guint num_dirs = 0;
 GList *plugin_list = NULL;
 GHashTable *hash ;
 guint option = 0;
@@ -307,7 +307,9 @@ nautilus_sendto_create_ui (void)
 
 	/* Set a title depending on the number of files to
 	 * share, and their types */
-	title = nst_title_from_mime_types ((const char **) mime_types, g_list_length (file_list));
+	title = nst_title_from_mime_types ((const char **) mime_types,
+					   g_list_length (file_list) - num_dirs,
+					   num_dirs);
 	gtk_window_set_title (GTK_WINDOW (gtk_builder_get_object (app, "nautilus_sendto_dialog")),
 			     title);
 	g_free (title);
@@ -488,12 +490,11 @@ nautilus_sendto_init (void)
 		}
 		mimetype = g_file_info_get_attribute_string (info, G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE);
 		g_hash_table_insert (ht, g_strdup (mimetype), GINT_TO_POINTER (1));
+		if (g_str_equal (mimetype, "inode/directory"))
+			num_dirs++;
 
 		g_object_unref (info);
 
-		if (g_file_test (filename, G_FILE_TEST_IS_DIR) != FALSE)
-			has_dirs = TRUE;
-
 		uri = g_filename_to_uri (filename, NULL, NULL);
 		g_free (filename);
 		escaped = escape_ampersands_and_commas (uri);
diff --git a/src/nautilus-sendto-mimetype.c b/src/nautilus-sendto-mimetype.c
index ac15b1a..3f520dd 100644
--- a/src/nautilus-sendto-mimetype.c
+++ b/src/nautilus-sendto-mimetype.c
@@ -20,12 +20,71 @@
  *          Bastien Nocera <hadess hadess net>
  */
 
+#include <glib/gi18n.h>
+#include <gio/gio.h>
+
 #include "nautilus-sendto-mimetype.h"
+#include "totem-mime-types.h"
+
+static gboolean
+is_video (const char *mimetype)
+{
+	guint i;
+
+	for (i = 0; i < G_N_ELEMENTS (video_mime_types); i++)
+		if (g_str_equal (video_mime_types[i], mimetype))
+			return TRUE;
+
+	return FALSE;
+}
 
 char *
 nst_title_from_mime_types (const char **mimetypes,
-			   guint        num_files)
+			   guint        num_files,
+			   guint        num_dirs)
 {
+	guint i;
+
+	if (num_dirs >= 1) {
+		if (num_files == 0) {
+			return g_strdup_printf (ngettext ("Sharing %d folder", "Sharing %d folders", num_dirs), num_dirs);
+		}
+		return g_strdup_printf (_("Sharing %d folders and files"), num_dirs + num_files);
+	}
+
+	guint num_elems = 0;
+	guint num_videos = 0;
+	guint num_photos = 0;
+	guint num_images = 0;
+	guint num_folders = 0;
+	guint num_text = 0;
+
+	for (i = 0; mimetypes[i] != NULL; i++) {
+		if (is_video (mimetypes[i]))
+			num_videos++;
+		else if (g_content_type_is_a (mimetypes[i], "image/jpeg"))
+			num_photos++;
+		else if (g_str_equal (mimetypes[i], "inode/directory"))
+			num_folders++;
+		else if (g_content_type_is_a (mimetypes[i], "image/*"))
+			num_images++;
+		else if (g_content_type_is_a (mimetypes[i], "text/plain"))
+			num_text++;
+		num_elems++;
+	}
+
+	if (num_videos == num_elems) {
+		return g_strdup_printf (ngettext ("Sharing %d video", "Sharing %d videos", num_files), num_files);
+	} else if (num_photos == num_elems) {
+		return g_strdup_printf (ngettext ("Sharing %d photo", "Sharing %d photos", num_files), num_files);
+	} else if (num_images + num_photos == num_elems) {
+		return g_strdup_printf (ngettext ("Sharing %d image", "Sharing %d images", num_files), num_files);
+	} else if (num_text == num_elems) {
+		return g_strdup_printf (ngettext ("Sharing %d text file", "Sharing %d text files", num_files), num_files);
+	} else {
+		return g_strdup_printf (ngettext ("Sharing %d file", "Sharing %d files", num_files), num_files);
+	}
+
 	return NULL;
 }
 
diff --git a/src/nautilus-sendto-mimetype.h b/src/nautilus-sendto-mimetype.h
index c23875c..577aa02 100644
--- a/src/nautilus-sendto-mimetype.h
+++ b/src/nautilus-sendto-mimetype.h
@@ -23,5 +23,6 @@
 #include <glib.h>
 
 char *nst_title_from_mime_types (const char **mimetypes,
-				 guint        num_files);
+				 guint        num_files,
+				 guint        num_dirs);
 
diff --git a/src/test-mimetype-data b/src/test-mimetype-data
new file mode 100644
index 0000000..a46a4a9
--- /dev/null
+++ b/src/test-mimetype-data
@@ -0,0 +1,57 @@
+# Test data for the nautilus-sendto title setter
+# Records are separated by an empty line
+# First line is the bug number in question, use NULL if none
+# Subsequent lines are mime-types (they all contain a '/')
+# Followed by the number of files
+# and the number of directories
+# and the expected result
+
+NULL
+application/octetstream
+1
+0
+Sharing 1 file
+
+NULL
+image/jpeg
+1
+0
+Sharing 1 photo
+
+NULL
+image/jpeg
+image/foobar
+2
+0
+Sharing 2 images
+
+NULL
+image/fab
+1
+0
+Sharing 1 image
+
+NULL
+video/ogg
+1
+0
+Sharing 1 video
+
+NULL
+text/x-python
+1
+0
+Sharing 1 text file
+
+NULL
+inode/directory
+0
+1
+Sharing 1 folder
+
+NULL
+inode/directory
+video/ogg
+1
+1
+Sharing 2 folders and files
diff --git a/src/test-mimetype.c b/src/test-mimetype.c
index ee1d44a..04e1f35 100644
--- a/src/test-mimetype.c
+++ b/src/test-mimetype.c
@@ -2,33 +2,73 @@
 #include "config.h"
 
 #include <locale.h>
+#include <stdlib.h>
 #include <glib.h>
 #include <glib-object.h>
 #include <glib/gi18n.h>
 
 #include "nautilus-sendto-mimetype.h"
 
-typedef struct {
-	const char **mimetypes;
-	guint num_files;
-	const char *result;
-} TitleTests;
-
-static TitleTests titles[] = {
-	{
-		{ "application/octet-stream", NULL },
-		1,
-		"Sharing one file",
-	},
-};
-
 static void
 test_name (void)
 {
-	guint i;
+	guint i, num_lines;
+	char *contents, **lines;
+
+	char *bug, *result;
+	int num_files, num_dirs;
+	GPtrArray *types;
+
+	if (g_file_get_contents (TEST_SRCDIR "test-mimetype-data", &contents, NULL, NULL) == FALSE)
+		g_error ("Failed to open test-mimetype-data file");
+
+	lines = g_strsplit (contents, "\n", -1);
+	num_lines = g_strv_length (lines);
+	g_free (contents);
+
+	num_files = -1;
+	num_dirs = -1;
+	bug = NULL;
+	result = NULL;
+	types = g_ptr_array_new ();
+
+	for (i = 0; i < num_lines; i++) {
+		char **mimetypes;
+
+		if (*lines[i] == '#')
+			continue;
+		if (*lines[i] == '\0') {
+			continue;
+		}
+		if (bug == NULL) {
+			bug = lines[i];
+			continue;
+		}
+		if (strstr (lines[i], "/") != NULL) {
+			g_ptr_array_add (types, lines[i]);
+			continue;
+		}
+		if (num_files == -1) {
+			num_files = (int) strtod (lines[i], NULL);
+			continue;
+		}
+		if (num_dirs == -1) {
+			num_dirs = (int) strtod (lines[i], NULL);
+			continue;
+		}
+		result = lines[i];
+
+		g_ptr_array_add (types, NULL);
+		mimetypes = (char **) g_ptr_array_free (types, FALSE);
+
+		g_test_bug (bug);
+		g_assert_cmpstr (nst_title_from_mime_types ((const char **) mimetypes, num_files, num_dirs), ==, result);
 
-	for (i = 0; i < G_N_ELEMENTS (titles); i++) {
-		g_assert_cmpstr (nst_title_from_mime_types (titles[i].mimetypes, titles[i].num_files), ==, titles[i].result);
+		num_files = -1;
+		num_dirs = -1;
+		bug = NULL;
+		result = NULL;
+		types = g_ptr_array_new ();
 	}
 }
 
diff --git a/src/totem-mime-types.h b/src/totem-mime-types.h
new file mode 100644
index 0000000..9698ff8
--- /dev/null
+++ b/src/totem-mime-types.h
@@ -0,0 +1,58 @@
+/* generated with mime-types-include.sh, don't edit */
+const char *video_mime_types[] = {
+"application/mxf",
+"application/ogg",
+"application/ram",
+"application/sdp",
+"application/vnd.ms-wpl",
+"application/vnd.rn-realmedia",
+"application/x-extension-m4a",
+"application/x-extension-mp4",
+"application/x-flash-video",
+"application/x-matroska",
+"application/x-netshow-channel",
+"application/x-ogg",
+"application/x-quicktimeplayer",
+"application/x-shorten",
+"image/vnd.rn-realpix",
+"image/x-pict",
+"misc/ultravox",
+"text/x-google-video-pointer",
+"video/3gpp",
+"video/dv",
+"video/fli",
+"video/flv",
+"video/mp2t",
+"video/mp4",
+"video/mp4v-es",
+"video/mpeg",
+"video/msvideo",
+"video/ogg",
+"video/quicktime",
+"video/vivo",
+"video/vnd.divx",
+"video/vnd.rn-realvideo",
+"video/vnd.vivo",
+"video/webm",
+"video/x-anim",
+"video/x-avi",
+"video/x-flc",
+"video/x-fli",
+"video/x-flic",
+"video/x-flv",
+"video/x-m4v",
+"video/x-matroska",
+"video/x-mpeg",
+"video/x-ms-asf",
+"video/x-ms-asx",
+"video/x-msvideo",
+"video/x-ms-wm",
+"video/x-ms-wmv",
+"video/x-ms-wmx",
+"video/x-ms-wvx",
+"video/x-nsv",
+"video/x-ogm+ogg",
+"video/x-theora+ogg",
+"video/x-totem-stream",
+"audio/x-pn-realaudio",
+};



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