1
1

wm-properties: Cosmetic cleanup

mime-type: more work.  Syncing with home 'puter
Этот коммит содержится в:
Jonathan Blandford 1999-04-11 17:46:45 +00:00
родитель 9f68cc2df5
Коммит 10e3bd7aae
6 изменённых файлов: 140 добавлений и 6 удалений

Просмотреть файл

@ -11,6 +11,5 @@ void launch_edit_window (MimeInfo *mi);
void initialize_main_win_vals (void);
void hide_edit_window (void);
void show_edit_window (void);
void edit_clicked (void);
#endif

Просмотреть файл

@ -14,6 +14,11 @@
#include "mime-data.h"
#include "mime-info.h"
#include "new-mime-window.h"
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
/* Prototypes */
static void mime_fill_from_file (const char *filename, gboolean init_user);
static void mime_load_from_dir (const char *mime_info_dir, gboolean system_dir);
@ -57,7 +62,7 @@ add_to_key (char *mime_type, char *def, GHashTable *table)
char *s, *p, *ext;
int used;
MimeInfo *info;
info = g_hash_table_lookup (table, (const void *) mime_type);
if (info == NULL) {
info = g_malloc (sizeof (MimeInfo));
@ -341,3 +346,121 @@ init_mime_type (void)
g_free (mime_info_dir);
init_mime_info ();
}
void
add_new_mime_type (gchar *mime_type, gchar *ext, gchar *regexp1, gchar *regexp2)
{
gchar *temp;
MimeInfo *mi;
/* first we make sure that the information is good */
/* passed check, now we add it. */
if (ext) {
temp = g_strconcat ("ext: ", ext, NULL);
add_to_key (mime_type, temp, user_mime_types);
mi = (MimeInfo *) g_hash_table_lookup (user_mime_types, mime_type);
mi->ext_readable[0] = g_strdup (ext);
g_free (temp);
}
if (regexp1) {
temp = g_strconcat ("regex: ", regexp1, NULL);
add_to_key (mime_type, temp, user_mime_types);
g_free (temp);
}
if (regexp2) {
temp = g_strconcat ("regex,2: ", regexp2, NULL);
add_to_key (mime_type, temp, user_mime_types);
g_free (temp);
}
}
static void
write_mime_foreach (gpointer mime_type, gpointer info, gpointer data)
{
gchar *buf;
MimeInfo *mi = (MimeInfo *) info;
g_print ("in write_mime_foreach:%s:\n", (gchar *) mime_type);
fwrite ((char *) mi->mime_type, 1, strlen ((char *) mi->mime_type), (FILE *) data);
fwrite ("\n", 1, 1, (FILE *) data);
if (mi->ext_readable[0]) {
fwrite ("\text: ", 1, strlen ("\text: "), (FILE *) data);
fwrite (mi->ext_readable[0], 1,
strlen (mi->ext_readable[0]),
(FILE *) data);
fwrite ("\n", 1, 1, (FILE *) data);
}
if (mi->regex_readable[0]) {
fwrite ("\tregex: ", 1, strlen ("\tregex: "), (FILE *) data);
fwrite (mi->regex_readable[0], 1,
strlen (mi->regex_readable[0]),
(FILE *) data);
fwrite ("\n", 1, 1, (FILE *) data);
}
if (mi->regex_readable[1]) {
fwrite ("\tregex,2: ", 1, strlen ("\tregex,2: "), (FILE *) data);
fwrite (mi->regex_readable[1], 1,
strlen (mi->regex_readable[1]),
(FILE *) data);
fwrite ("\n", 1, 1, (FILE *) data);
}
fwrite ("\n", 1, 1, (FILE *) data);
}
static void
run_error (gchar *message)
{
GtkWidget *error_box;
error_box = gnome_message_box_new (
message,
GNOME_MESSAGE_BOX_ERROR,
GNOME_STOCK_BUTTON_OK,
NULL);
gnome_dialog_run_and_close (GNOME_DIALOG (error_box));
}
static void
write_mime (GHashTable *hash)
{
struct stat s;
gchar *dirname, *filename;
FILE *file;
GtkWidget *error_box;
dirname = g_concat_dir_and_file (gnome_util_user_home (), ".gnome/mime-info");
if ((stat (dirname, &s) < 0) || !(S_ISDIR (s.st_mode))){
if (errno == ENOENT) {
if (mkdir (dirname, S_IRWXU) < 0) {
run_error ("We are unable to create the directory\n"
"~/.gnome/mime-info\n\n"
"We will not be able to save the state.");
return;
}
} else {
run_error ("We are unable to access the directory\n"
"~/.gnome/mime-info\n\n"
"We will not be able to save the state.");
return;
}
}
filename = g_concat_dir_and_file (dirname, "user.mime");
remove (filename);
file = fopen (filename, "w");
if (file == NULL) {
run_error (_("Cannot create the file\n~/.gnome/mime-info/user.mime\n\n"
"We will not be able to save the state"));
return;
}
g_hash_table_foreach (hash, write_mime_foreach, file);
g_hash_table_foreach (hash, write_mime_foreach, file);
fclose (file);
}
void write_user_mime (void)
{
write_mime (user_mime_types);
}
void write_initial_mime (void)
{
write_mime (initial_user_mime_types);
}

Просмотреть файл

@ -20,5 +20,9 @@ typedef struct {
GtkWidget *get_mime_clist (void);
void init_mime_type (void);
void add_clicked (GtkWidget *widget, gpointer data);
void edit_clicked ();
void add_new_mime_type (gchar *mime_type, gchar *ext, gchar *regexp1, gchar *regexp2);
void write_user_mime (void);
void write_initial_mime (void);
#endif

Просмотреть файл

@ -13,5 +13,4 @@ void set_mime_key_value (gchar *mime_type, gchar *key, gchar *value);
const char * local_mime_get_value (const char *mime_type, char *key);
void write_user_keys (void);
void write_initial_keys (void);
#endif

Просмотреть файл

@ -38,11 +38,13 @@ static void
try_callback ()
{
write_user_keys ();
write_user_mime ();
}
static void
revert_callback ()
{
write_initial_keys ();
write_initial_mime ();
discard_mime_info ();
initialize_main_win_vals ();
}

Просмотреть файл

@ -1,6 +1,7 @@
#include "new-mime-window.h"
#include "capplet-widget.h"
static GtkWidget *add_dialog = NULL;
extern GtkWidget *capplet;
/*Public functions */
void
@ -17,7 +18,7 @@ launch_new_mime_window (void)
GtkWidget *table;
add_dialog = gnome_dialog_new (_("Add Mime Type"), GNOME_STOCK_BUTTON_OK, GNOME_STOCK_BUTTON_CANCEL, NULL);
label = gtk_label_new (_("Add a new Mime Type\n\nFor example: image/png"));
label = gtk_label_new (_("Add a new Mime Type\nFor example: image/tiff; text/x-scheme"));
gtk_label_set_justify (GTK_LABEL (label), GTK_JUSTIFY_LEFT);
hbox = gtk_hbox_new (FALSE, GNOME_PAD_SMALL);
gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
@ -88,7 +89,13 @@ launch_new_mime_window (void)
gtk_widget_show_all (GNOME_DIALOG (add_dialog)->vbox);
switch (gnome_dialog_run (GNOME_DIALOG (add_dialog))) {
case 0:
capplet_widget_state_changed (CAPPLET_WIDGET (capplet),
TRUE);
add_new_mime_type (gtk_entry_get_text (GTK_ENTRY (mime_entry)),
gtk_entry_get_text (GTK_ENTRY (ext_entry)),
gtk_entry_get_text (GTK_ENTRY (regex1_entry)),
gtk_entry_get_text (GTK_ENTRY (regex2_entry)));
case 1:
gtk_widget_destroy (add_dialog);
default:;