]> Pileus Git - grits/blobdiff - src/plugin-ridge.c
Replacing GFile/GIO with libsoup
[grits] / src / plugin-ridge.c
index 3e08d00eac0f7ce750698255a6c430de2bfeb60e..388ee7405c73ba6d32b27267611d9858a249fe63 100644 (file)
@@ -1,3 +1,20 @@
+/*
+ * Copyright (C) 2009 Andy Spencer <spenceal@rose-hulman.edu>
+ * 
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ * 
+ * This program 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 General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
 #include <config.h>
 #include <gtk/gtk.h>
 #include <curl/curl.h>
@@ -5,6 +22,58 @@
 
 #include <stdio.h>
 
+#include "aweather-gui.h"
+#include "plugin-ridge.h"
+#include "data.h"
+
+/****************
+ * GObject code *
+ ****************/
+/* Plugin init */
+static void aweather_ridge_plugin_init(AWeatherPluginInterface *iface);
+static void aweather_ridge_expose(AWeatherPlugin *_ridge);
+G_DEFINE_TYPE_WITH_CODE(AWeatherRidge, aweather_ridge, G_TYPE_OBJECT,
+               G_IMPLEMENT_INTERFACE(AWEATHER_TYPE_PLUGIN,
+                       aweather_ridge_plugin_init));
+static void aweather_ridge_plugin_init(AWeatherPluginInterface *iface)
+{
+       g_debug("AWeatherRidge: plugin_init");
+       /* Add methods to the interface */
+       iface->expose = aweather_ridge_expose;
+}
+/* Class/Object init */
+static void aweather_ridge_init(AWeatherRidge *ridge)
+{
+       g_debug("AWeatherRidge: init");
+       /* Set defaults */
+       ridge->gui = NULL;
+}
+static void aweather_ridge_dispose(GObject *gobject)
+{
+       g_debug("AWeatherRidge: dispose");
+       AWeatherRidge *self = AWEATHER_RIDGE(gobject);
+       /* Drop references */
+       G_OBJECT_CLASS(aweather_ridge_parent_class)->dispose(gobject);
+}
+static void aweather_ridge_finalize(GObject *gobject)
+{
+       g_debug("AWeatherRidge: finalize");
+       AWeatherRidge *self = AWEATHER_RIDGE(gobject);
+       /* Free data */
+       G_OBJECT_CLASS(aweather_ridge_parent_class)->finalize(gobject);
+
+}
+static void aweather_ridge_class_init(AWeatherRidgeClass *klass)
+{
+       g_debug("AWeatherRidge: class_init");
+       GObjectClass *gobject_class = (GObjectClass*)klass;
+       gobject_class->dispose  = aweather_ridge_dispose;
+       gobject_class->finalize = aweather_ridge_finalize;
+}
+
+/*********************
+ * Overlay constants *
+ *********************/
 enum {
        LAYER_TOPO,
        LAYER_COUNTY,
@@ -14,72 +83,48 @@ enum {
        LAYER_COUNT
 };
 
-static struct {
-       char *fmt;
+typedef struct {
+       gchar *name;
+       gchar *fmt;
+       gboolean enabled;
        float z;
        guint tex;
-} layers[] = {
-       [LAYER_TOPO]     = { "Overlays/" "Topo/"     "Short/" "%s_Topo_Short.jpg",     1, 0 },
-       [LAYER_COUNTY]   = { "Overlays/" "County/"   "Short/" "%s_County_Short.gif",   3, 0 },
-       [LAYER_RIVERS]   = { "Overlays/" "Rivers/"   "Short/" "%s_Rivers_Short.gif",   4, 0 },
-       [LAYER_HIGHWAYS] = { "Overlays/" "Highways/" "Short/" "%s_Highways_Short.gif", 5, 0 },
-       [LAYER_CITY]     = { "Overlays/" "Cities/"   "Short/" "%s_City_Short.gif",     6, 0 },
+} layer_t;
+
+static layer_t layers[] = {
+       [LAYER_TOPO]     = {"Topo",     "Overlays/Topo/Short/%s_Topo_Short.jpg",         TRUE,  1, 0},
+       [LAYER_COUNTY]   = {"Counties", "Overlays/County/Short/%s_County_Short.gif",     TRUE,  3, 0},
+       [LAYER_RIVERS]   = {"Rivers",   "Overlays/Rivers/Short/%s_Rivers_Short.gif",     FALSE, 4, 0},
+       [LAYER_HIGHWAYS] = {"Highways", "Overlays/Highways/Short/%s_Highways_Short.gif", FALSE, 5, 0},
+       [LAYER_CITY]     = {"Cities",   "Overlays/Cities/Short/%s_City_Short.gif",       TRUE,  6, 0},
 };
 
-static CURL *curl_handle;
 
-
-/**
- * Cache a image from Ridge to the local disk
- * \param  path  Path to the Ridge file, starting after /ridge/
- * \return The local path to the cached image
- */
-char *cache_image(char *path)
-{
-       gchar base[] = "http://radar.weather.gov/ridge/";
-       gchar *url   = g_strconcat(base, path, NULL);
-       gchar *local = g_build_filename(g_get_user_cache_dir(), PACKAGE, path, NULL);
-       if (!g_file_test(local, G_FILE_TEST_EXISTS)) {
-               if (!g_file_test(g_path_get_dirname(local), G_FILE_TEST_IS_DIR)) {
-                       //g_printerr("Making directory %s\n", g_path_get_dirname(local));
-                       g_mkdir_with_parents(g_path_get_dirname(local), 0755);
-               }
-               //g_printerr("Fetching image %s -> %s\n", url, local);
-               long http_code;
-               FILE *cached_image = fopen(local, "w+");
-               curl_easy_setopt(curl_handle, CURLOPT_URL, url);
-               curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, cached_image);
-               curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, NULL);
-               curl_easy_perform(curl_handle);
-               curl_easy_getinfo(curl_handle, CURLINFO_RESPONSE_CODE, &http_code);
-               fflush(cached_image);
-               if (http_code != 200) {
-                       g_message("http %ld while fetching %s", http_code, url);
-                       remove(local);
-                       return NULL;
-               }
-       }
-       return local;
-}
-
-/**
+/***********
+ * Helpers *
+ ***********/
+/*
  * Load an image into an OpenGL texture
  * \param  filename  Path to the image file
  * \return The OpenGL identifier for the texture
  */
-guint load_texture(char *filename)
+void load_texture(AWeatherRidge *self, layer_t *layer, gchar *filename)
 {
+       aweather_gui_gl_begin(self->gui);
+
        /* Load image */
        GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(filename, NULL);
+       if (!pixbuf)
+               g_warning("Failed to load texture: %s", filename);
        guchar    *pixels = gdk_pixbuf_get_pixels(pixbuf);
        int        width  = gdk_pixbuf_get_width(pixbuf);
        int        height = gdk_pixbuf_get_height(pixbuf);
        int        format = gdk_pixbuf_get_has_alpha(pixbuf) ? GL_RGBA : GL_RGB;
 
        /* Create Texture */
-       guint id;
-       glGenTextures(1, &id);
-       glBindTexture(GL_TEXTURE_2D, id);   // 2d texture (x and y size)
+       glDeleteTextures(1, &layer->tex);
+       glGenTextures(1, &layer->tex);
+       glBindTexture(GL_TEXTURE_2D, layer->tex); // 2d texture (x and y size)
 
        glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
@@ -87,56 +132,111 @@ guint load_texture(char *filename)
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
 
-       g_message("loaded image:  w=%-3d  h=%-3d  fmt=%x  px=(%02x,%02x,%02x,%02x)  img=%s",
+       char *base = g_path_get_basename(filename);
+       g_debug("AWeatherRidge: load_texture - "
+               "w=%-3d  h=%-3d  fmt=%x  px=(%02x,%02x,%02x,%02x)  img=%s",
                width, height, format, pixels[0], pixels[1], pixels[2], pixels[3],
-               g_path_get_basename(filename));
-       return id;
+               base);
+       g_free(base);
+
+       aweather_gui_gl_end(self->gui);
+
+       g_object_unref(pixbuf);
+
+       /* Redraw */
+       aweather_gui_gl_redraw(self->gui);
 }
 
-static gboolean expose(GtkWidget *da, GdkEventExpose *event, gpointer user_data)
+
+/*****************
+ * ASync helpers *
+ *****************/
+typedef struct {
+       AWeatherRidge *self;
+       layer_t *layer;
+} cached_t;
+void cached_cb(gchar *filename, gboolean updated, gpointer _udata)
 {
-       g_message("ridge:expose");
-       glPushMatrix();
-       glEnable(GL_TEXTURE_2D);
-       glColor4f(1,1,1,1);
+       cached_t *udata = _udata;
+       load_texture(udata->self, udata->layer, filename);
+       g_free(udata);
+}
 
+/*************
+ * callbacks *
+ *************/
+static void on_site_changed(AWeatherView *view, gchar *site, AWeatherRidge *self)
+{
+       g_debug("AWeatherRidge: on_site_changed - site=%s", site);
        for (int i = 0; i < LAYER_COUNT; i++) {
-               glBindTexture(GL_TEXTURE_2D, layers[i].tex);
-
-               glBegin(GL_POLYGON);
-               glTexCoord2f(0.0, 0.0); glVertex3f(500*1000*-1.0, 500*1000* 1.0, layers[i].z);
-               glTexCoord2f(0.0, 1.0); glVertex3f(500*1000*-1.0, 500*1000*-1.0, layers[i].z);
-               glTexCoord2f(1.0, 1.0); glVertex3f(500*1000* 1.0, 500*1000*-1.0, layers[i].z);
-               glTexCoord2f(1.0, 0.0); glVertex3f(500*1000* 1.0, 500*1000* 1.0, layers[i].z);
-               glEnd();
+               gchar *base = "http://radar.weather.gov/ridge/";
+               gchar *path  = g_strdup_printf(layers[i].fmt, site);
+               cached_t *udata = g_malloc(sizeof(cached_t));
+               udata->self  = self;
+               udata->layer = &layers[i];
+               cache_file(base, path, AWEATHER_ONCE, cached_cb, udata);
+               g_free(path);
        }
+}
 
-       glPopMatrix();
-       return FALSE;
+void toggle_layer(GtkToggleButton *check, AWeatherGui *gui)
+{
+       layer_t *layer = g_object_get_data(G_OBJECT(check), "layer");
+       layer->enabled = gtk_toggle_button_get_active(check);
+       aweather_gui_gl_redraw(gui);
 }
 
-static gboolean configure(GtkWidget *da, GdkEventConfigure *event, gpointer user_data)
+/***********
+ * Methods *
+ ***********/
+AWeatherRidge *aweather_ridge_new(AWeatherGui *gui)
 {
+       AWeatherRidge *ridge = g_object_new(AWEATHER_TYPE_RIDGE, NULL);
+       ridge->gui = gui;
+
+       AWeatherView *view    = aweather_gui_get_view(gui);
+       GtkWidget    *drawing = aweather_gui_get_widget(gui, "drawing");
+       GtkWidget    *config  = aweather_gui_get_widget(gui, "tabs");
+
+       /* Add configuration tab */
+       GtkWidget *tab  = gtk_label_new("Ridge");
+       GtkWidget *body = gtk_alignment_new(0.5, 0, 0, 0);
+       GtkWidget *hbox = gtk_hbox_new(FALSE, 10);
        for (int i = 0; i < LAYER_COUNT; i++) {
-               if (layers[i].tex != 0)
-                       continue;
-               char *path  = g_strdup_printf(layers[i].fmt, "IND");
-               char *local = cache_image(path);
-               layers[i].tex = load_texture(local);
-               g_free(local);
-               g_free(path);
+               GtkWidget *check = gtk_check_button_new_with_label(layers[i].name);
+               gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check), layers[i].enabled);
+               gtk_box_pack_start(GTK_BOX(hbox), check, FALSE, TRUE, 0);
+               g_object_set_data(G_OBJECT(check), "layer", &layers[i]);
+               g_signal_connect(check, "toggled", G_CALLBACK(toggle_layer), gui);
        }
-       return FALSE;
+       gtk_container_add(GTK_CONTAINER(body), hbox);
+       gtk_notebook_append_page(GTK_NOTEBOOK(config), body, tab);
+
+       g_signal_connect(view, "site-changed", G_CALLBACK(on_site_changed),  ridge);
+
+       return ridge;
 }
 
-gboolean ridge_init(GtkDrawingArea *drawing, GtkNotebook *config)
+static void aweather_ridge_expose(AWeatherPlugin *_ridge)
 {
-       /* Set up OpenGL Stuff */
-       g_signal_connect(drawing, "expose-event",    G_CALLBACK(expose),    NULL);
-       g_signal_connect(drawing, "configure-event", G_CALLBACK(configure), NULL);
+       AWeatherRidge *ridge = AWEATHER_RIDGE(_ridge);
 
-       curl_global_init(CURL_GLOBAL_ALL);
-       curl_handle = curl_easy_init();
+       g_debug("AWeatherRidge: expose");
+       glPushMatrix();
+       glEnable(GL_TEXTURE_2D);
+       glColor4f(1,1,1,1);
 
-       return TRUE;
+       for (int i = 0; i < LAYER_COUNT; i++) {
+               if (!layers[i].enabled)
+                       continue;
+               glBindTexture(GL_TEXTURE_2D, layers[i].tex);
+               glBegin(GL_POLYGON);
+               glTexCoord2f(0.0, 0.0); glVertex3f(240*1000*-1.0, 282*1000* 1.0, layers[i].z);
+               glTexCoord2f(0.0, 1.0); glVertex3f(240*1000*-1.0, 282*1000*-1.0, layers[i].z);
+               glTexCoord2f(1.0, 1.0); glVertex3f(240*1000* 1.0, 282*1000*-1.0, layers[i].z);
+               glTexCoord2f(1.0, 0.0); glVertex3f(240*1000* 1.0, 282*1000* 1.0, layers[i].z);
+               glEnd();
+       }
+
+       glPopMatrix();
 }