Commit 3f3e93b0 authored by Bob Schlärmann's avatar Bob Schlärmann

Split up all functions into different files for easier maintenance. Changed...

Split up all functions into different files for easier maintenance. Changed from non-blocking sockets to blocking sockets that are less difficult to use. Rewrote the functions that deal with the xml data, it should handle errors in the xml file better. Added a location search dialog. Remodelled the summary and forecast tabs.


(Old svn revision: 569)
parent f82e2c6a
......@@ -171,7 +171,7 @@ am__CONFIG_DISTCLEAN_FILES = config.status config.cache config.log \
configure.lineno
$(srcdir)/Makefile.in: @MAINTAINER_MODE_TRUE@ Makefile.am $(top_srcdir)/configure.ac $(ACLOCAL_M4)
cd $(top_srcdir) && \
$(AUTOMAKE) --foreign Makefile
$(AUTOMAKE) --gnu Makefile
Makefile: @MAINTAINER_MODE_TRUE@ $(srcdir)/Makefile.in $(top_builddir)/config.status
cd $(top_builddir) && $(SHELL) ./config.status $@ $(am__depfiles_maybe)
......
......@@ -425,7 +425,7 @@ PACKAGE_VERSION=
PACKAGE_STRING=
PACKAGE_BUGREPORT=
ac_unique_file="panel-plugin/weather.c"
ac_unique_file="configure"
# Factoring default headers for most tests.
ac_includes_default="\
#include <stdio.h>
......@@ -1782,7 +1782,7 @@ fi
# Define the identity of the package.
PACKAGE=xfce4-weather-plugin
VERSION=0.3.0
VERSION=0.3.9
cat >>confdefs.h <<_ACEOF
......
......@@ -5,11 +5,11 @@ dnl
dnl 2003 Benedikt Meurer <benedikt.meurer@unix-ag.uni-siegen.de>
dnl
AC_INIT([panel-plugin/weather.c])
AC_INIT([configure])
AM_CONFIG_HEADER([config.h])
AM_INIT_AUTOMAKE([xfce4-weather-plugin], [0.3.0])
AM_INIT_AUTOMAKE([xfce4-weather-plugin], [0.3.9])
AM_MAINTAINER_MODE
......
......@@ -159,7 +159,7 @@ all: all-recursive
.SUFFIXES:
$(srcdir)/Makefile.in: @MAINTAINER_MODE_TRUE@ Makefile.am $(top_srcdir)/configure.ac $(ACLOCAL_M4)
cd $(top_srcdir) && \
$(AUTOMAKE) --foreign icons/Makefile
$(AUTOMAKE) --gnu icons/Makefile
Makefile: @MAINTAINER_MODE_TRUE@ $(srcdir)/Makefile.in $(top_builddir)/config.status
cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@ $(am__depfiles_maybe)
......
......@@ -202,7 +202,7 @@ all: all-am
.SUFFIXES:
$(srcdir)/Makefile.in: @MAINTAINER_MODE_TRUE@ Makefile.am $(top_srcdir)/configure.ac $(ACLOCAL_M4)
cd $(top_srcdir) && \
$(AUTOMAKE) --foreign icons/liquid/Makefile
$(AUTOMAKE) --gnu icons/liquid/Makefile
Makefile: @MAINTAINER_MODE_TRUE@ $(srcdir)/Makefile.in $(top_builddir)/config.status
cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@ $(am__depfiles_maybe)
......
......@@ -10,7 +10,24 @@ libweather_la_LDFLAGS = \
libweather_la_SOURCES = \
weather.c
config_dialog.c \
config_dialog.h \
get_data.c \
get_data.h \
http_client.c \
http_client.h \
parsers.c \
parsers.h \
plugin.c \
plugin.h \
scrollbox.c \
scrollbox.h \
summary_window.c \
summary_window.h \
icon.c \
icon.h \
search_dialog.c \
search_dialog.h
libweather_la_CFLAGS = \
@XFCE4_PANEL_CFLAGS@ \
......
This diff is collapsed.
#include "config_dialog.h"
#include "debug_print.h"
struct labeloption labeloptions[11] = {
{"Windchill (F)", FLIK},
{"Temperature (T)", TEMP},
{"Atmosphere pressure (P)", BAR_R},
{"Atmosphere state (P)", BAR_D},
{"Wind speed (WS)", WIND_SPEED},
{"Wind gust (WG)", WIND_GUST},
{"Wind direction (WD)", WIND_DIRECTION},
{"Humidity (H)", HMID},
{"Visibility (V)", VIS},
{"UV Index (UV)", UV_INDEX},
{"Dewpoint (DP)", DEWP}
};
typedef void(*cb_function)(struct xfceweather_data *);
cb_function cb;
void add_mdl_option(GtkListStore *mdl, int opt)
{
GtkTreeIter iter;
gtk_list_store_append(mdl, &iter);
gtk_list_store_set(mdl, &iter,
0, labeloptions[opt].name,
1, labeloptions[opt].number,
-1);
}
gboolean cb_addoption (GtkWidget *widget, gpointer data)
{
struct xfceweather_dialog *dialog = (struct xfceweather_dialog *)data;
gint history = gtk_option_menu_get_history(GTK_OPTION_MENU(dialog->opt_xmloption));
enum datas opt;
int i;
add_mdl_option(dialog->mdl_xmloption, history);
return FALSE;
}
gboolean cb_deloption (GtkWidget *widget, gpointer data)
{
struct xfceweather_dialog *dialog = (struct xfceweather_dialog *)data;
GtkTreeIter iter;
GtkTreeSelection *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(dialog->lst_xmloption));
if (gtk_tree_selection_get_selected(selection, NULL, &iter))
gtk_list_store_remove(GTK_LIST_STORE(dialog->mdl_xmloption), &iter);
return FALSE;
}
static GtkWidget *make_label(void)
{
int i;
GtkWidget *widget, *menu;
menu = gtk_menu_new();
widget = gtk_option_menu_new();
for (i = 0; i < 11; i++)
{
struct labeloption opt = labeloptions[i];
gtk_menu_shell_append(GTK_MENU_SHELL(menu),
gtk_menu_item_new_with_label(opt.name));
}
gtk_option_menu_set_menu(GTK_OPTION_MENU(widget), menu);
return widget;
}
void apply_options (struct xfceweather_dialog *dialog)
{
int history = 0;
gboolean hasiter = FALSE;
GtkTreeIter iter;
struct xfceweather_data *data = (struct xfceweather_data *)dialog->wd;
history = gtk_option_menu_get_history(GTK_OPTION_MENU(dialog->opt_unit));
if (history == 0)
data->unit = IMPERIAL;
else
data->unit = METRIC;
if (data->location_code)
g_free(data->location_code);
data->location_code = g_strdup(gtk_entry_get_text(GTK_ENTRY(dialog->txt_loc_code)));
/* call labels_clear() here */
if (data->labels && data->labels->len > 0)
g_array_free(data->labels, TRUE);
data->labels = g_array_new(FALSE, TRUE, sizeof(enum datas));
for (hasiter = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(dialog->mdl_xmloption), &iter);
hasiter == TRUE; hasiter = gtk_tree_model_iter_next(
GTK_TREE_MODEL(dialog->mdl_xmloption), &iter))
{
gint option;
GValue value = {0, };
gtk_tree_model_get_value(GTK_TREE_MODEL(dialog->mdl_xmloption), &iter, 1, &value);
option = g_value_get_int(&value);
g_array_append_val(data->labels, option);
g_value_unset(&value);
}
if (cb)
cb(data);
}
int option_i(enum datas opt)
{
int i;
for (i = 0; i < OPTIONS_N; i++)
{
if (labeloptions[i].number == opt)
return i;
}
return -1;
}
gboolean cb_findlocation(GtkButton *button, gpointer user_data)
{
struct xfceweather_dialog *dialog = (struct xfceweather_dialog *)user_data;
struct search_dialog *sdialog = create_search_dialog(NULL);
if (run_search_dialog(sdialog))
gtk_entry_set_text(GTK_ENTRY(dialog->txt_loc_code), sdialog->result);
free_search_dialog(sdialog);
return FALSE;
}
struct xfceweather_dialog *create_config_dialog(struct xfceweather_data *data,
GtkContainer *container, GtkWidget *done)
{
struct xfceweather_dialog *dialog;
GtkWidget *vbox, *vbox2, *hbox, *label,
*menu, *menu_item, *button_add,
*button_del, *image, *button, *scroll;
GtkSizeGroup *sg = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
GtkSizeGroup *sg_buttons = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
GtkTreeViewColumn *column;
GtkCellRenderer *renderer;
gint i;
dialog = g_new0(struct xfceweather_dialog, 1);
dialog->wd = (struct xfceweather_data *)data;
dialog->dialog = gtk_widget_get_toplevel(done);
vbox = gtk_vbox_new(FALSE, BORDER);
label = gtk_label_new("Measurement unit:");
gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
menu = gtk_menu_new();
dialog->opt_unit = gtk_option_menu_new();
gtk_menu_shell_append ((GtkMenuShell *)(GTK_MENU(menu)),
(gtk_menu_item_new_with_label("Imperial")));
gtk_menu_shell_append ((GtkMenuShell *)(GTK_MENU(menu)),
(gtk_menu_item_new_with_label("Metric")));
gtk_option_menu_set_menu(GTK_OPTION_MENU(dialog->opt_unit), menu);
if (dialog->wd->unit == IMPERIAL)
gtk_option_menu_set_history(GTK_OPTION_MENU(dialog->opt_unit), 0);
else
gtk_option_menu_set_history(GTK_OPTION_MENU(dialog->opt_unit), 1);
gtk_size_group_add_widget(sg, label);
hbox = gtk_hbox_new(FALSE, BORDER);
gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(hbox), dialog->opt_unit, TRUE, TRUE, 0);
gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
label = gtk_label_new("Location code:");
dialog->txt_loc_code = gtk_entry_new();
gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
if (dialog->wd->location_code != NULL)
gtk_entry_set_text(GTK_ENTRY(dialog->txt_loc_code),
dialog->wd->location_code);
gtk_size_group_add_widget(sg, label);
button = gtk_button_new();
image = gtk_image_new_from_stock(GTK_STOCK_FIND, GTK_ICON_SIZE_BUTTON);
gtk_container_add(GTK_CONTAINER(button), image);
g_signal_connect(button, "clicked", G_CALLBACK(cb_findlocation), dialog);
hbox = gtk_hbox_new(FALSE, BORDER);
gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(hbox), dialog->txt_loc_code, TRUE, TRUE, 0);
gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
dialog->opt_xmloption = make_label();
dialog->mdl_xmloption = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_INT);
dialog->lst_xmloption = gtk_tree_view_new_with_model(GTK_TREE_MODEL(dialog->mdl_xmloption));
renderer = gtk_cell_renderer_text_new();
column = gtk_tree_view_column_new_with_attributes("Labels to display", renderer,
"text", 0);
gtk_tree_view_append_column(GTK_TREE_VIEW(dialog->lst_xmloption), column);
button_add = gtk_button_new_from_stock(GTK_STOCK_ADD);
gtk_size_group_add_widget(sg_buttons, button_add);
hbox = gtk_hbox_new(FALSE, BORDER);
gtk_box_pack_start(GTK_BOX(hbox), dialog->opt_xmloption, TRUE, TRUE, 0);
gtk_box_pack_start(GTK_BOX(hbox), button_add, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
button_del = gtk_button_new_from_stock(GTK_STOCK_REMOVE);
gtk_size_group_add_widget(sg_buttons, button_del);
hbox = gtk_hbox_new(FALSE, BORDER);
scroll = gtk_scrolled_window_new(NULL, NULL);
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll), GTK_POLICY_NEVER,
GTK_POLICY_AUTOMATIC);
gtk_container_add(GTK_CONTAINER(scroll), dialog->lst_xmloption);
gtk_box_pack_start(GTK_BOX(hbox), scroll, TRUE, TRUE, 0);
vbox2 = gtk_vbox_new(FALSE, 0);
gtk_box_pack_start(GTK_BOX(vbox2), button_del, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(hbox), vbox2, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, TRUE, 0);
gtk_widget_set_size_request(dialog->lst_xmloption, -1, 120);
if (data->labels->len > 0)
{
enum datas opt;
gint i, n;
for (i = 0; i < data->labels->len; i++)
{
opt = g_array_index (data->labels, enum datas, i);
if ((n = option_i(opt)) != -1)
add_mdl_option(dialog->mdl_xmloption, n);
}
}
g_signal_connect(button_add, "clicked", G_CALLBACK(cb_addoption), dialog);
g_signal_connect(button_del, "clicked", G_CALLBACK(cb_deloption), dialog);
g_signal_connect_swapped (done, "clicked",
G_CALLBACK (apply_options), dialog);
// g_signal_connect_swapped (dialog->dialog, "destroy",
// G_CALLBACK (gtk_widget_destroy), dialog);
gtk_widget_show_all(vbox);
gtk_container_add (container, vbox);
return dialog;
}
void set_callback_config_dialog(struct xfceweather_dialog *dialog,
cb_function cb_new)
{
cb = cb_new;
}
#ifndef CONFIG_DIALOG_H
#define CONFIG_DIALOG_H
#include "search_dialog.h"
#include "plugin.h"
#include <gtk/gtk.h>
#define OPTIONS_N 11
struct labeloption {
gchar *name;
enum datas number;
};
struct xfceweather_dialog {
GtkWidget *dialog;
GtkWidget *opt_unit;
GtkWidget *txt_loc_code;
GtkWidget *tooltip_yes;
GtkWidget *tooltip_no;
GtkWidget *opt_xmloption;
GtkWidget *lst_xmloption;
GtkListStore *mdl_xmloption;
struct xfceweather_data *wd;
};
struct xfceweather_dialog *create_config_dialog(struct xfceweather_data *data,
GtkContainer *container,
GtkWidget *done);
void set_callback_config_dialog(struct xfceweather_dialog *dialog,
void(cb)(struct xfceweather_data *));
#endif
#ifndef DEBUG_PRINT_H
#define DEBUG_PRINT_H
#define DEBUG 0
#define DEBUG_PRINT(s,i) if (DEBUG) printf(s,i);
#endif
#include "get_data.h"
#include "debug_print.h"
#define KILL_RING_S 5
#define EMPTY_STRING g_strdup("-");
#define CHK_NULL(str) str ? g_strdup(str) : EMPTY_STRING;
gchar *kill_ring[KILL_RING_S] = {NULL, };
#define debug_print printf
gchar *copy_buffer(gchar *str)
{
static int p = 0;
gchar *s;
if (!str)
{
debug_print("copy_buffer: received NULL pointer\n");
return EMPTY_STRING;
}
if (p >= KILL_RING_S)
p = 0;
if (kill_ring[p])
g_free(kill_ring[p]);
s = g_strdup(str);
kill_ring[p++] = s;
return s;
}
void free_get_data_buffer(void)
{
int i;
for (i = 0; i < KILL_RING_S; i++)
{
if (kill_ring[i])
g_free(kill_ring[i]);
}
}
gchar *get_data_uv(struct xml_uv *data, enum datas type)
{
gchar *str;
if (!data)
{
debug_print("get_data_bar: xml-uv not present\n");
return EMPTY_STRING;
}
switch(type)
{
case _UV_INDEX: str = data->i; break;
case _UV_TRANS: str = data->t; break;
}
return CHK_NULL(str);
}
gchar *get_data_bar(struct xml_bar *data, enum datas_bar type)
{
gchar *str;
if (!data)
{
debug_print("get_data_bar: xml-wind not present\n");
return EMPTY_STRING;
}
switch(type)
{
case _BAR_R: str = data->r; break;
case _BAR_D: str = data->d; break;
}
return CHK_NULL(str);
}
gchar *get_data_wind(struct xml_wind *data, enum datas_wind type)
{
gchar *str;
if (!data)
{
debug_print("get_data_wind: xml-wind not present\n");
return EMPTY_STRING;
}
DEBUG_PRINT("starting\n", NULL);
switch(type)
{
case _WIND_SPEED: str = data->s; break;
case _WIND_GUST: str = data->gust; break;
case _WIND_DIRECTION: str = data->t; break;
case _WIND_TRANS: str = data->d; break;
}
DEBUG_PRINT("print %p\n", data->d);
DEBUG_PRINT("%s\n", str);
return CHK_NULL(str);
}
// -- This is not the same as the previous functions
gchar *get_data_cc(struct xml_cc *data, enum datas type)
{
gchar *str;
if (!data)
{
debug_print("get_data_cc: xml-cc not present\n");
return EMPTY_STRING;
}
switch(type)
{
case LSUP: str = data->lsup; break;
case OBST: str = data->obst; break;
case FLIK: str = data->flik; break;
case TRANS: str = data->t; break;
case TEMP: str = data->tmp; break;
case HMID: str = data->hmid; break;
case VIS: str = data->vis; break;
case UV_INDEX: return get_data_uv(data->uv, _UV_INDEX);
case UV_TRANS: return get_data_uv(data->uv, _UV_TRANS);
case WIND_SPEED: return get_data_wind(data->wind, _WIND_SPEED);
case WIND_GUST: return get_data_wind(data->wind, _WIND_GUST);
case WIND_DIRECTION: return get_data_wind(data->wind, _WIND_DIRECTION);
case WIND_TRANS: return get_data_wind(data->wind, _WIND_TRANS);
case BAR_R: return get_data_bar(data->bar, _BAR_R);
case BAR_D: return get_data_bar(data->bar, _BAR_D);
case DEWP: str = data->dewp; break;
case WICON: str = data->icon; break;
}
return CHK_NULL(str);
}
gchar *get_data_loc(struct xml_loc *data, enum datas type)
{
gchar *str;
if (!data)
{
debug_print("get_data_loc: xml-loc not present\n");
return EMPTY_STRING;
}
switch(type)
{
case DNAM: str = data->dnam; break;
case SUNR: str = data->sunr; break;
case SUNS: str = data->suns; break;
}
return CHK_NULL(str);
}
const gchar *get_data(struct xml_weather *data, enum datas type)
{
gchar *str;
gchar *p;
switch (type & 0xFF00)
{
case DATAS_CC: str = get_data_cc(data->cc, type); break;
case DATAS_LOC: str = get_data_loc(data->loc, type); break;
//case DATAS_DAYF: str = get_data_dayf(data->dayf, type, arg);
default: str = EMPTY_STRING;
}
p = copy_buffer(str);
g_free(str);
return p;
}
gchar *get_data_part(struct xml_part *data, enum forecast type)
{
gchar *str;
DEBUG_PRINT("now here %s\n", data->ppcp);
switch (type & 0x000F)
{
case F_ICON: str = data->icon; break;
case F_TRANS: str = data->t; break;
case F_PPCP: str = data->ppcp; break;
case F_W_SPEED: str = get_data_wind(data->wind, _WIND_SPEED); break;
case F_W_DIRECTION: str = get_data_wind(data->wind, _WIND_DIRECTION); break;
}
return str;
}
const gchar *get_data_f(struct xml_dayf *data, enum forecast type)
{
gchar *p, *str;
switch (type & 0x0F00)
{
case ITEMS:
switch(type)
{
case WDAY: str = data->day; break;
case TEMP_MIN: str = data->low; break;
case TEMP_MAX: str = data->hi; break;
}
break;
case NPART: str = get_data_part(data->part[1], type); break;
case DPART: str = get_data_part(data->part[0], type); break;
}