summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJames Bowes <jbowes@dangerouslyinc.com>2008-04-16 20:24:51 -0400
committerJames Bowes <jbowes@dangerouslyinc.com>2008-04-20 08:13:29 -0400
commite10da96dfa517f3bb5c787742ebd42c5aa945e76 (patch)
treeb123c2135e03c4ac03fe90315e132f97355a612c
parentbfab83692db064c1db480601ffe724c1560151c0 (diff)
Allow for per-host autoplaying of flash files.
Perferences are stored in an ini style file under $XDG_CONFIG_HOME as swfdec-mozilla.conf. The 'global' group stores default options, which are overridden per-host by a group with the name of that host. For now there is only the 'autoplay' boolean option. Playing/stopping a flash file via the ui will alter the config value for that file's host.
-rw-r--r--src/Makefile.am2
-rw-r--r--src/swfmoz_config.c188
-rw-r--r--src/swfmoz_config.h59
-rw-r--r--src/swfmoz_player.c19
-rw-r--r--src/swfmoz_player.h2
5 files changed, 267 insertions, 3 deletions
diff --git a/src/Makefile.am b/src/Makefile.am
index bbfc043..a01ca5a 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -11,6 +11,7 @@ libswfdecmozilla_la_LDFLAGS = \
libswfdecmozilla_la_SOURCES = \
plugin.c \
plugin_x11.c \
+ swfmoz_config.c \
swfmoz_dialog.c \
swfmoz_loader.c \
swfmoz_player.c
@@ -18,6 +19,7 @@ libswfdecmozilla_la_SOURCES = \
noinst_HEADERS = \
plugin.h \
plugin_x11.h \
+ swfmoz_config.h \
swfmoz_dialog.h \
swfmoz_loader.h \
swfmoz_player.h
diff --git a/src/swfmoz_config.c b/src/swfmoz_config.c
new file mode 100644
index 0000000..add8354
--- /dev/null
+++ b/src/swfmoz_config.c
@@ -0,0 +1,188 @@
+/* Swfdec Mozilla Plugin
+ * Copyright (C) 2008 James Bowes <jbowes@dangerouslyinc.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301 USA
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include "swfmoz_config.h"
+
+G_DEFINE_TYPE (SwfmozConfig, swfmoz_config, G_TYPE_OBJECT)
+
+#define SWFMOZ_CONFIG_FILE "swfdec-mozilla.conf"
+
+static SwfmozConfig *singleton_config = NULL;
+
+static gboolean
+swfmoz_config_save_file (SwfmozConfig *config)
+{
+ gchar *data;
+ gsize data_size;
+ GError *error = NULL;
+ gboolean has_global;
+
+ gchar *keyfile_name = g_build_filename (g_get_user_config_dir (),
+ SWFMOZ_CONFIG_FILE, NULL);
+
+ has_global = g_key_file_has_key (config->keyfile, "global", "autoplay",
+ &error);
+ if (error) {
+ g_printerr ("Unable to check for global config: %s\n", error->message);
+ g_error_free (error);
+ error = NULL;
+ } else if (!has_global) {
+ g_key_file_set_boolean (config->keyfile, "global", "autoplay", FALSE);
+ }
+
+ data = g_key_file_to_data (config->keyfile, &data_size, &error);
+ if (error) {
+ goto fail;
+ }
+
+ g_file_set_contents (keyfile_name, data, data_size, &error);
+ if (error) {
+ goto fail;
+ }
+
+ g_free (data);
+ g_free (keyfile_name);
+
+ return TRUE;
+
+fail:
+ g_printerr ("Unable to write new config file: %s\n", error->message);
+ g_error_free (error);
+ error = NULL;
+
+ g_free (data);
+ g_free (keyfile_name);
+
+ return FALSE;
+}
+
+static GKeyFile *
+swfmoz_config_read_file (void)
+{
+ gchar *keyfile_name = g_build_filename (g_get_user_config_dir (),
+ SWFMOZ_CONFIG_FILE, NULL);
+ GKeyFile *keyfile;
+ GError *error = NULL;
+
+ keyfile = g_key_file_new ();
+ if (!g_key_file_load_from_file (keyfile, keyfile_name, G_KEY_FILE_NONE,
+ &error)) {
+ g_printerr ("Unable to load config file '%s': %s\n", keyfile_name,
+ error->message);
+ g_error_free (error);
+ error = NULL;
+ }
+
+ g_free (keyfile_name);
+ return keyfile;
+}
+
+static gboolean
+swfmoz_config_read_autoplay (SwfmozConfig *config, const char *host,
+ gboolean autoplay)
+{
+ GError *error = NULL;
+ gboolean tmp_autoplay;
+
+ tmp_autoplay = g_key_file_get_boolean (config->keyfile, host, "autoplay",
+ &error);
+ if (error) {
+ g_printerr ("Problem reading 'autoplay' for '%s': %s\n", host,
+ error->message);
+ g_error_free (error);
+ } else {
+ autoplay = tmp_autoplay;
+ }
+
+ return autoplay;
+}
+
+gboolean
+swfmoz_config_should_autoplay (SwfmozConfig *config, const SwfdecURL *url)
+{
+ const gchar *host;
+ gboolean autoplay = FALSE;
+
+ g_return_val_if_fail (SWFMOZ_IS_CONFIG (config), FALSE);
+
+ host = swfdec_url_get_host (url);
+
+ autoplay = swfmoz_config_read_autoplay (config, "global", autoplay);
+ autoplay = swfmoz_config_read_autoplay (config, host, autoplay);
+
+ return autoplay;
+}
+
+void
+swfmoz_config_set_autoplay (SwfmozConfig *config, const SwfdecURL *url,
+ gboolean autoplay)
+{
+ g_return_if_fail (SWFMOZ_IS_CONFIG (config));
+
+ g_key_file_set_boolean (config->keyfile, swfdec_url_get_host (url),
+ "autoplay", autoplay);
+
+ swfmoz_config_save_file (config);
+}
+
+static void
+swfmoz_config_dispose (GObject *object)
+{
+ SwfmozConfig *config = SWFMOZ_CONFIG (object);
+
+ /*Just in case this wasn't initialized via swfmoz_config_new */
+ if (config == singleton_config) {
+ singleton_config = NULL;
+ }
+
+ g_key_file_free (config->keyfile);
+ config->keyfile = NULL;
+
+ G_OBJECT_CLASS (swfmoz_config_parent_class)->dispose (object);
+}
+
+static void
+swfmoz_config_class_init (SwfmozConfigClass *klass)
+{
+ GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+ object_class->dispose = swfmoz_config_dispose;
+}
+
+static void
+swfmoz_config_init (SwfmozConfig *config)
+{
+ config->keyfile = swfmoz_config_read_file ();
+}
+
+SwfmozConfig *
+swfmoz_config_new (void)
+{
+ if (!singleton_config) {
+ singleton_config = SWFMOZ_CONFIG (g_object_new (SWFMOZ_TYPE_CONFIG, NULL));
+ } else {
+ g_object_ref (G_OBJECT (singleton_config));
+ }
+
+ return singleton_config;
+}
diff --git a/src/swfmoz_config.h b/src/swfmoz_config.h
new file mode 100644
index 0000000..b14c786
--- /dev/null
+++ b/src/swfmoz_config.h
@@ -0,0 +1,59 @@
+/* Swfdec Mozilla Plugin
+ * Copyright (C) 2008 James Bowes <jbowes@dangerouslyinc.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301 USA
+ */
+
+#ifndef _SWFMOZ_CONFIG_H_
+#define _SWFMOZ_CONFIG_H_
+
+#include <glib.h>
+#include <swfdec/swfdec.h>
+
+G_BEGIN_DECLS
+
+typedef struct _SwfmozConfig SwfmozConfig;
+typedef struct _SwfmozConfigClass SwfmozConfigClass;
+
+#define SWFMOZ_TYPE_CONFIG (swfmoz_config_get_type())
+#define SWFMOZ_IS_CONFIG(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), SWFMOZ_TYPE_CONFIG))
+#define SWFMOZ_IS_CONFIG_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), SWFMOZ_TYPE_CONFIG))
+#define SWFMOZ_CONFIG(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), SWFMOZ_TYPE_CONFIG, SwfmozConfig))
+#define SWFMOZ_CONFIG_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), SWFMOZ_TYPE_CONFIG, SwfmozConfigClass))
+#define SWFMOZ_CONFIG_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), SWFMOZ_TYPE_CONFIG, SwfmozConfigClass))
+
+struct _SwfmozConfig
+{
+ GObject parent;
+ GKeyFile * keyfile; /* Config file contents */
+};
+
+struct _SwfmozConfigClass
+{
+ GObjectClass parent;
+};
+
+GType swfmoz_config_get_type (void);
+
+SwfmozConfig * swfmoz_config_new (void);
+gboolean swfmoz_config_should_autoplay (SwfmozConfig *config,
+ const SwfdecURL *url);
+void swfmoz_config_set_autoplay (SwfmozConfig *config,
+ const SwfdecURL *url,
+ gboolean autoplay);
+
+G_END_DECLS
+#endif
diff --git a/src/swfmoz_player.c b/src/swfmoz_player.c
index 4156fa6..1f7c15b 100644
--- a/src/swfmoz_player.c
+++ b/src/swfmoz_player.c
@@ -31,10 +31,14 @@
/*** menu ***/
static void
-swfmoz_player_menu_playing_toggled (GtkCheckMenuItem *item, SwfdecGtkPlayer* player)
+swfmoz_player_menu_playing_toggled (GtkCheckMenuItem *item, SwfmozPlayer* player)
{
- if (swfdec_gtk_player_get_playing (player) != gtk_check_menu_item_get_active (item))
- swfdec_gtk_player_set_playing (player, gtk_check_menu_item_get_active (item));
+ gboolean menu_item_active = gtk_check_menu_item_get_active (item);
+
+ if (swfdec_gtk_player_get_playing (SWFDEC_GTK_PLAYER (player)) != menu_item_active) {
+ swfdec_gtk_player_set_playing (SWFDEC_GTK_PLAYER (player), menu_item_active);
+ swfmoz_config_set_autoplay (player->config, swfdec_player_get_url (SWFDEC_PLAYER (player)), menu_item_active);
+ }
}
static void
@@ -362,6 +366,9 @@ swfmoz_player_dispose (GObject *object)
player->loaders = NULL;
}
+ g_object_unref (player->config);
+ player->config = NULL;
+
G_OBJECT_CLASS (swfmoz_player_parent_class)->dispose (object);
}
@@ -402,6 +409,7 @@ swfmoz_player_new (NPP instance, gboolean windowless)
NULL);
ret->instance = instance;
ret->windowless = windowless;
+ ret->config = swfmoz_config_new ();
return SWFDEC_PLAYER (ret);
}
@@ -491,7 +499,11 @@ swfmoz_player_set_initial_stream (SwfmozPlayer *player, NPStream *stream)
return FALSE;
}
swfdec_player_set_url (SWFDEC_PLAYER (player), url);
+ if (swfmoz_config_should_autoplay (player->config, url)) {
+ swfdec_gtk_player_set_playing (SWFDEC_GTK_PLAYER (player), TRUE);
+ }
swfdec_url_free (url);
+
return TRUE;
}
@@ -706,6 +718,7 @@ swfmoz_player_mouse_release (SwfmozPlayer *player, int x, int y, guint button)
} else {
if (button == 1) {
swfdec_gtk_player_set_playing (SWFDEC_GTK_PLAYER (player), TRUE);
+ swfmoz_config_set_autoplay (player->config, swfdec_player_get_url (SWFDEC_PLAYER (player)), TRUE);
ret = TRUE;
} else {
ret = FALSE;
diff --git a/src/swfmoz_player.h b/src/swfmoz_player.h
index 4f3b0e1..6a8cf75 100644
--- a/src/swfmoz_player.h
+++ b/src/swfmoz_player.h
@@ -22,6 +22,7 @@
#include <swfdec-gtk/swfdec-gtk.h>
#include <npapi.h>
+#include "swfmoz_config.h"
#include "swfmoz_loader.h"
G_BEGIN_DECLS
@@ -66,6 +67,7 @@ struct _SwfmozPlayer {
guint no_release; /* for disabling release event when closing right-click menu */
GtkMenu * menu; /* right-click menu */
GtkTreeModel * loaders; /* loaders used in this players */
+ SwfmozConfig * config; /* autoplay configuration */
};
struct _SwfmozPlayerClass {