Commit ba220ebd authored by Gene Z. Ragan's avatar Gene Z. Ragan Committed by Gene Ragan
Browse files

reviewed by: Pavel Cisler <pavel@eazel.com>

2001-02-26  Gene Z. Ragan  <gzr@eazel.com>

	reviewed by: Pavel Cisler <pavel@eazel.com>

	Fixed bug 7039, User-entered application that matches
	built-in application can't be deleted

	Fixed bug 7040, User-entered application that matches built-in
	application doesn't go away when "all user changes" reverted

	Fixed bug 7064, New mime types added by user using the capplet
	do not appear when capplet is launched again.

	* mime-type-capplet/Makefile.am:
	Add libuuid to build.

	* mime-type-capplet/libuuid/Makefile.am:
	* mime-type-capplet/libuuid/clear.c: (uuid_clear):
	* mime-type-capplet/libuuid/compare.c: (uuid_compare):
	* mime-type-capplet/libuuid/copy.c: (uuid_copy):
	* mime-type-capplet/libuuid/gen_uuid.c: (get_random_bytes),
	(get_node_id), (get_clock), (uuid_generate_time),
	(uuid_generate_random), (uuid_generate):
	* mime-type-capplet/libuuid/gen_uuid_nt.c: (Nt5), (uuid_generate):
	* mime-type-capplet/libuuid/isnull.c: (uuid_is_null):
	* mime-type-capplet/libuuid/pack.c: (uuid_pack):
	* mime-type-capplet/libuuid/parse.c: (uuid_parse):
	* mime-type-capplet/libuuid/tst_uuid.c: (main):
	* mime-type-capplet/libuuid/unpack.c: (uuid_unpack):
	* mime-type-capplet/libuuid/unparse.c: (uuid_unparse):
	* mime-type-capplet/libuuid/uuid.h:
	* mime-type-capplet/libuuid/uuidP.h:
	* mime-type-capplet/libuuid/uuid_time.c: (uuid_time), (uuid_type),
	(uuid_variant), (variant_string), (main):
	Add libuuid to capplet and link statically. This is sort of sad. It would
	be nice if gnome had a uuid library, but at this time it does not.

	* mime-type-capplet/nautilus-mime-type-capplet-dialogs.c:
	(nautilus_mime_type_capplet_show_new_mime_window):
	Call proper API to ensure that new mime type is added to the database.

	(add_or_update_application), (add_item_to_application_list),
	(run_edit_or_new_application_dialog):
	Create and assign a uuid to user defined application so that we never
	conflict with system defined types that use the application name
	as the application id.
parent 730e1685
NULL =
SUBDIRS = libuuid
INCLUDES = -I. \
-I$(top_srcdir) \
-I$(srcdir) \
......@@ -9,7 +13,9 @@ INCLUDES = -I. \
$(GTK_CFLAGS) \
-DGNOMELOCALEDIR=\""$(datadir)/locale"\" \
-I$(includedir) \
$(VFS_CFLAGS) $(WERROR)
$(VFS_CFLAGS) $(WERROR) \
$(NULL)
bin_PROGRAMS = file-types-capplet
......@@ -19,7 +25,9 @@ file_types_capplet_SOURCES = \
nautilus-mime-type-icon-entry.h \
nautilus-mime-type-capplet.c \
nautilus-mime-type-capplet-dialogs.c \
nautilus-mime-type-icon-entry.c
nautilus-mime-type-icon-entry.c \
$(NULL)
file_types_capplet_LDADD = \
$(CAPPLET_LIBDIR) \
......@@ -28,7 +36,9 @@ file_types_capplet_LDADD = \
$(OAF_LIBS) \
$(INTLLIBS) \
$(top_builddir)/libgnomevfs/libgnomevfs.la \
-lgdk_pixbuf
-lgdk_pixbuf \
$/libuuid/libuuid.a \
$(NULL)
sysdir = $(datadir)/control-center
......
......@@ -37,6 +37,7 @@
#include <libgnomevfs/gnome-vfs-mime-info.h>
#include <libgnomevfs/gnome-vfs-utils.h>
#include "libuuid/uuid.h"
#include "nautilus-mime-type-capplet.h"
#include "nautilus-mime-type-capplet-dialogs.h"
......@@ -68,7 +69,7 @@ static edit_dialog_details *edit_component_details = NULL;
static void show_new_application_window (GtkWidget *button, GtkWidget *list);
static void show_edit_application_window (GtkWidget *button, GtkWidget *list);
static void delete_selected_application (GtkWidget *button, GtkWidget *list);
static void add_item_to_application_list (GtkWidget *list, const char *name, const char *mime_type,
static void add_item_to_application_list (GtkWidget *list, const char *id, const char *name, const char *mime_type,
gboolean user_owned, int position);
static void find_message_label_callback (GtkWidget *widget, gpointer callback_data);
static void find_message_label (GtkWidget *widget, const char *message);
......@@ -811,6 +812,12 @@ nautilus_mime_type_capplet_show_new_mime_window (void)
/* Add new mime type here */
if (strlen (mime_type) > 3) {
/* This call creates the key */
gnome_vfs_mime_set_registered_type_key (mime_type,
"description",
description);
/* Ths call sets the user information */
gnome_vfs_mime_set_value (mime_type,
"description",
description);
......@@ -1071,17 +1078,19 @@ nautilus_mime_type_capplet_show_new_extension_window (void)
* Create or update a GnomeVFSMimeApplication and register
* it with the mime database.
*/
static void
static char *
add_or_update_application (GtkWidget *list, const char *name, const char *command,
gboolean multiple, gboolean expects_uris,
gboolean update)
{
GnomeVFSMimeApplication app, *original;
const char *mime_type;
uuid_t app_uuid;
char app_uuid_string[100];
/* Check for empty strings. Command can be empty. */
if (name[0] == '\0') {
return;
return NULL;
}
mime_type = nautilus_mime_type_capplet_get_selected_item_mime_type ();
......@@ -1090,7 +1099,11 @@ add_or_update_application (GtkWidget *list, const char *name, const char *comman
/* It's ok to cast, we don't modify the application
* structure and thus the name/command, this should really
* use the application registry explicitly */
app.id = (char *)name;
/* Generate unique application id */
uuid_generate(app_uuid);
uuid_unparse(app_uuid, app_uuid_string);
app.id = app_uuid_string;
app.name = (char *)name;
app.command = (char *)command;
app.can_open_multiple_files = multiple;
......@@ -1100,7 +1113,7 @@ add_or_update_application (GtkWidget *list, const char *name, const char *comman
app.requires_terminal = FALSE;
if (update) {
original = gnome_vfs_mime_application_new_from_id (name);
original = gnome_vfs_mime_application_new_from_id (app.id);
if (original == NULL) {
const char *original_id;
GList *selection;
......@@ -1110,18 +1123,18 @@ add_or_update_application (GtkWidget *list, const char *name, const char *comman
/* If there isn't a selection we cannot allow an edit */
selection = GTK_LIST (list)->selection;
if (selection == NULL || g_list_length (selection) <= 0) {
return;
return NULL;
}
/* Get application id and info */
item = GTK_LIST_ITEM (selection->data);
if (item == NULL) {
return;
return NULL;
}
original_id = gtk_object_get_data (GTK_OBJECT (item), "application_id");
if (original_id == NULL) {
return;
return NULL;
}
/* Remove original application data */
......@@ -1134,31 +1147,30 @@ add_or_update_application (GtkWidget *list, const char *name, const char *comman
gtk_container_remove (GTK_CONTAINER (list), GTK_WIDGET (item));
/* Add new widget and restore position */
add_item_to_application_list (list, name, mime_type,
add_item_to_application_list (list, original_id, name, mime_type,
gnome_vfs_application_is_user_owned_application (original),
position);
}
}
gnome_vfs_application_registry_save_mime_application (&app);
gnome_vfs_application_registry_add_mime_type (name, mime_type);
gnome_vfs_application_registry_add_mime_type (app.id, mime_type);
gnome_vfs_application_registry_sync ();
gnome_vfs_mime_add_application_to_short_list (mime_type, app.id);
return g_strdup (app.id);
}
static void
add_item_to_application_list (GtkWidget *list, const char *name, const char *mime_type,
add_item_to_application_list (GtkWidget *list, const char *id, const char *name, const char *mime_type,
gboolean user_owned, int position)
{
GtkListItem *list_item;
GList *short_list;
short_list = gnome_vfs_mime_get_short_list_applications (mime_type);
/* FIXME: Is it safe to use the name as the ID? Won't this allow naming conflicts? */
list_item = create_application_list_item (name, name, mime_type, user_owned, short_list);
list_item = create_application_list_item (id, name, mime_type, user_owned, short_list);
gnome_vfs_mime_application_list_free (short_list);
insert_item (GTK_LIST (list), list_item, position);
......@@ -1236,7 +1248,7 @@ run_edit_or_new_application_dialog (const char *mime_type, GtkWidget *list, Gnom
const char *command;
int dialog_result;
gboolean entry_validated;
char *invalid_entry_message;
char *invalid_entry_message, *app_id;
g_assert (mime_type != NULL || application != NULL);
g_assert (GTK_IS_WIDGET (list));
......@@ -1317,15 +1329,16 @@ run_edit_or_new_application_dialog (const char *mime_type, GtkWidget *list, Gnom
if (!handle_invalid_application_input (GTK_WINDOW (dialog), name, command)) {
entry_validated = TRUE;
add_or_update_application (list,
name,
command,
gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (multiple_check_box)),
gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (uri_check_box)),
application != NULL);
if (application == NULL) {
add_item_to_application_list (list, name, mime_type, TRUE, -1);
}
app_id = add_or_update_application (list,
name,
command,
gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (multiple_check_box)),
gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (uri_check_box)),
application != NULL);
if (application == NULL && app_id != NULL) {
add_item_to_application_list (list, app_id, name, mime_type, TRUE, -1);
}
g_free (app_id);
}
}
} while (dialog_result == GNOME_OK && !entry_validated);
......
SUBDIRS =
NULL =
noinst_LIBRARIES = libuuid.a
noinst_HEADERS = \
uuid.h \
uuidP.h \
$(NULL)
libuuid_a_SOURCES = \
clear.c \
compare.c \
copy.c \
gen_uuid.c \
isnull.c \
pack.c \
parse.c \
unpack.c \
unparse.c \
uuid_time.c \
$(NULL)
INCLUDES = \
$(GLIB_CFLAGS) \
$(WERROR) \
$(NULL)
/*
* clear.c -- Clear a UUID
*
* Copyright (C) 1996, 1997 Theodore Ts'o.
*
* %Begin-Header%
* This file may be redistributed under the terms of the GNU Public
* License.
* %End-Header%
*/
#include "string.h"
#include "uuidP.h"
void uuid_clear(uuid_t uu)
{
memset(uu, 0, 16);
}
/*
* compare.c --- compare whether or not two UUID's are the same
*
* Returns 0 if the two UUID's are different, and 1 if they are the same.
*
* Copyright (C) 1996, 1997 Theodore Ts'o.
*
* %Begin-Header%
* This file may be redistributed under the terms of the GNU Public
* License.
* %End-Header%
*/
#include "uuidP.h"
#include <string.h>
#define UUCMP(u1,u2) if (u1 != u2) return((u1 < u2) ? -1 : 1);
int uuid_compare(uuid_t uu1, uuid_t uu2)
{
struct uuid uuid1, uuid2;
uuid_unpack(uu1, &uuid1);
uuid_unpack(uu2, &uuid2);
UUCMP(uuid1.time_low, uuid2.time_low);
UUCMP(uuid1.time_mid, uuid2.time_mid);
UUCMP(uuid1.time_hi_and_version, uuid2.time_hi_and_version);
UUCMP(uuid1.clock_seq, uuid2.clock_seq);
return memcmp(uuid1.node, uuid2.node, 6);
}
/*
* copy.c --- copy UUIDs
*
* Copyright (C) 1996, 1997 Theodore Ts'o.
*
* %Begin-Header%
* This file may be redistributed under the terms of the GNU Public
* License.
* %End-Header%
*/
#include "uuidP.h"
void uuid_copy(uuid_t uu1, uuid_t uu2)
{
unsigned char *cp1, *cp2;
int i;
for (i=0, cp1 = uu1, cp2 = uu2; i < 16; i++)
*cp1++ = *cp2++;
}
/*
* gen_uuid.c --- generate a DCE-compatible uuid
*
* Copyright (C) 1996, 1997 Theodore Ts'o.
*
* %Begin-Header%
* This file may be redistributed under the terms of the GNU Public
* License.
* %End-Header%
*/
#include <config.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#ifdef HAVE_SYS_SOCKIO_H
#include <sys/sockio.h>
#endif
#ifdef HAVE_NET_IF_H
#include <net/if.h>
#endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#include "uuidP.h"
#ifdef HAVE_SRANDOM
#define srand(x) srandom(x)
#define rand() random()
#endif
/*
* Generate a series of random bytes. Use /dev/urandom if possible,
* and if not, use srandom/random.
*/
static void get_random_bytes(void *buf, int nbytes)
{
static int fd = -2;
int i;
char *cp = (char *) buf;
if (fd == -2) {
fd = open("/dev/urandom", O_RDONLY);
srand((getpid() << 16) ^ getuid() ^ time(0));
}
if (fd >= 0) {
while (nbytes > 0) {
i = read(fd, cp, nbytes);
if (i < 0) {
if ((errno == EINTR) || (errno == EAGAIN))
continue;
break;
}
nbytes -= i;
cp += i;
}
}
if (nbytes == 0)
return;
/* XXX put something better here if no /dev/random! */
for (i=0; i < nbytes; i++)
*cp++ = rand() & 0xFF;
return;
}
/*
* Get the ethernet hardware address, if we can find it...
*/
static int get_node_id(unsigned char *node_id)
{
#ifdef HAVE_NET_IF_H
int sd;
struct ifreq ifr, *ifrp;
struct ifconf ifc;
char buf[1024];
int n, i;
unsigned char *a;
/*
* BSD 4.4 defines the size of an ifreq to be
* max(sizeof(ifreq), sizeof(ifreq.ifr_name)+ifreq.ifr_addr.sa_len
* However, under earlier systems, sa_len isn't present, so the size is
* just sizeof(struct ifreq)
*/
#ifdef HAVE_SA_LEN
#ifndef max
#define max(a,b) ((a) > (b) ? (a) : (b))
#endif
#define ifreq_size(i) max(sizeof(struct ifreq),\
sizeof((i).ifr_name)+(i).ifr_addr.sa_len)
#else
#define ifreq_size(i) sizeof(struct ifreq)
#endif /* HAVE_SA_LEN*/
sd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
if (sd < 0) {
return -1;
}
memset(buf, 0, sizeof(buf));
ifc.ifc_len = sizeof(buf);
ifc.ifc_buf = buf;
if (ioctl (sd, SIOCGIFCONF, (char *)&ifc) < 0) {
close(sd);
return -1;
}
n = ifc.ifc_len;
for (i = 0; i < n; i+= ifreq_size(*ifr) ) {
ifrp = (struct ifreq *)((char *) ifc.ifc_buf+i);
strncpy(ifr.ifr_name, ifrp->ifr_name, IFNAMSIZ);
#ifdef SIOCGIFHWADDR
if (ioctl(sd, SIOCGIFHWADDR, &ifr) < 0)
continue;
a = (unsigned char *) &ifr.ifr_hwaddr.sa_data;
#else
#ifdef SIOCGENADDR
if (ioctl(sd, SIOCGENADDR, &ifr) < 0)
continue;
a = (unsigned char *) ifr.ifr_enaddr;
#else
/*
* XXX we don't have a way of getting the hardware
* address
*/
close(sd);
return 0;
#endif /* SIOCGENADDR */
#endif /* SIOCGIFHWADDR */
if (!a[0] && !a[1] && !a[2] && !a[3] && !a[4] && !a[5])
continue;
if (node_id) {
memcpy(node_id, a, 6);
close(sd);
return 1;
}
}
close(sd);
#endif
return 0;
}
/* Assume that the gettimeofday() has microsecond granularity */
#define MAX_ADJUSTMENT 10
static int get_clock(guint32 *clock_high, guint32 *clock_low, guint16 *ret_clock_seq)
{
static int adjustment = 0;
static struct timeval last = {0, 0};
static guint16 clock_seq;
struct timeval tv;
unsigned long long clock_reg;
try_again:
gettimeofday(&tv, 0);
if ((last.tv_sec == 0) && (last.tv_usec == 0)) {
get_random_bytes(&clock_seq, sizeof(clock_seq));
clock_seq &= 0x1FFF;
last = tv;
last.tv_sec--;
}
if ((tv.tv_sec < last.tv_sec) ||
((tv.tv_sec == last.tv_sec) &&
(tv.tv_usec < last.tv_usec))) {
clock_seq = (clock_seq+1) & 0x1FFF;
adjustment = 0;
} else if ((tv.tv_sec == last.tv_sec) &&
(tv.tv_usec == last.tv_usec)) {
if (adjustment >= MAX_ADJUSTMENT)
goto try_again;
adjustment++;
} else
adjustment = 0;
clock_reg = tv.tv_usec*10 + adjustment;
clock_reg += ((unsigned long long) tv.tv_sec)*10000000;
clock_reg += (((unsigned long long) 0x01B21DD2) << 32) + 0x13814000;
*clock_high = clock_reg >> 32;
*clock_low = clock_reg;
*ret_clock_seq = clock_seq;
return 0;
}
void uuid_generate_time(uuid_t out)
{
static unsigned char node_id[6];
static int has_init = 0;
struct uuid uu;
guint32 clock_mid;
if (!has_init) {
if (get_node_id(node_id) <= 0) {
get_random_bytes(node_id, 6);
/*
* Set multicast bit, to prevent conflicts
* with IEEE 802 addresses obtained from
* network cards
*/
node_id[0] |= 0x80;
}
has_init = 1;
}
get_clock(&clock_mid, &uu.time_low, &uu.clock_seq);
uu.clock_seq |= 0x8000;
uu.time_mid = (guint16) clock_mid;
uu.time_hi_and_version = (clock_mid >> 16) | 0x1000;
memcpy(uu.node, node_id, 6);
uuid_pack(&uu, out);
}
void uuid_generate_random(uuid_t out)
{
uuid_t buf;
struct uuid uu;
get_random_bytes(buf, sizeof(buf));
uuid_unpack(buf, &uu);
uu.clock_seq = (uu.clock_seq & 0x3FFF) | 0x8000;
uu.time_hi_and_version = (uu.time_hi_and_version & 0x0FFF) | 0x4000;
uuid_pack(&uu, out);
}
/*
* This is the generic front-end to uuid_generate_random and
* uuid_generate_time. It uses uuid_generate_random only if
* /dev/urandom is available, since otherwise we won't have
* high-quality randomness.
*/
void uuid_generate(uuid_t out)
{
static int has_random = -1;
if (has_random < 0) {
if (access("/dev/urandom", R_OK) == 0)
has_random = 1;
else
has_random = 0;
}
if (has_random)
uuid_generate_random(out);
else
uuid_generate_time(out);
}
/*
* gen_uuid_nt.c -- Use NT api to generate uuid
*
* Written by Andrey Shedel (andreys@ns.cr.cyco.com)
*/
#include "uuidP.h"
#pragma warning(push,4)
#pragma comment(lib, "ntdll.lib")
//
// Here is a nice example why it's not a good idea
// to use native API in ordinary applications.
// Number of parameters in function below was changed from 3 to 4
// for NT5.
//
//
// NTSYSAPI
// NTSTATUS
// NTAPI
// NtAllocateUuids(
// OUT PULONG p1,
// OUT PULONG p2,
// OUT PULONG p3,
// OUT PUCHAR Seed // 6 bytes
// );
//
//