Skip to content

Commit

Permalink
Port to libsoup-3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
sgn committed Mar 8, 2024
1 parent acb8c06 commit 97cd9b0
Show file tree
Hide file tree
Showing 11 changed files with 157 additions and 93 deletions.
10 changes: 4 additions & 6 deletions .build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ requires:
- gcc
- git
- gtk3
- libsoup
- libsoup3
- make
- mate-common
- tzdata
Expand All @@ -27,8 +27,7 @@ requires:
- gtk-doc-tools
- libglib2.0-dev
- libgtk-3-dev
- libsoup-gnome2.4-dev
- libsoup2.4-dev
- libsoup-3.0-dev
- libxml2-dev
- libxml2-utils
- make
Expand All @@ -44,7 +43,7 @@ requires:
- gcc
- git
- gtk3-devel
- libsoup-devel
- libsoup3-devel
- libxml2-devel
- make
- mate-common
Expand All @@ -60,8 +59,7 @@ requires:
- gtk-doc-tools
- libglib2.0-dev
- libgtk-3-dev
- libsoup-gnome2.4-dev
- libsoup2.4-dev
- libsoup-3.0-dev
- libxml2-dev
- libxml2-utils
- make
Expand Down
6 changes: 4 additions & 2 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ AC_CONFIG_AUX_DIR([build-aux])
AM_INIT_AUTOMAKE([1.9 no-dist-gzip dist-xz tar-ustar check-news])
m4_ifdef([AM_SILENT_RULES], [AM_SILENT_RULES([yes])])

AC_USE_SYSTEM_EXTENSIONS
# Before making a release, the LT_VERSION string should be modified.
# The string is of the form C:R:A.
# - If interfaces have been changed or added, but binary compatibility has
Expand All @@ -23,7 +24,7 @@ AC_CANONICAL_HOST

GLIB_REQUIRED=2.56.0
GTK_REQUIRED=3.22.0
LIBSOUP_REQUIRED=2.34.0
LIBSOUP_REQUIRED=3.0.0
GIO_REQUIRED=2.25.0
LIBXML_REQUIRED=2.6.0

Expand Down Expand Up @@ -65,7 +66,7 @@ dnl -- Check for libxml (required) ------------------------------------------
PKG_CHECK_MODULES(LIBXML, libxml-2.0 >= $LIBXML_REQUIRED)

dnl -- check for libsoup (required) -----------------------------------------
PKG_CHECK_MODULES(LIBSOUP, [libsoup-2.4 >= $LIBSOUP_REQUIRED])
PKG_CHECK_MODULES(LIBSOUP, [libsoup-3.0 >= $LIBSOUP_REQUIRED])

dnl -- check for gio (required) -----------------------------------------
PKG_CHECK_MODULES(GIO,
Expand Down Expand Up @@ -100,6 +101,7 @@ AC_CHECK_FUNCS(regexec,,[AC_CHECK_LIB(regex,regexec,
[AC_MSG_ERROR([No regex library found])])])
AC_SUBST(REGEX_LIBS)

AC_CHECK_FUNC(memmem,[],[AC_MSG_ERROR([memmem is required])])

dnl ***************************************************************************
dnl *** Check for presence of tm.tm_gmtoff on the system ***
Expand Down
2 changes: 1 addition & 1 deletion libmateweather/mateweather-uninstalled.pc.in
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ Name: MateWeather
Description: MateWeather shared library
Version: @VERSION@
Requires: glib-2.0 gobject-2.0 gdk-pixbuf-2.0 gtk+-3.0 gio-2.0
Requires.private: libxml-2.0 libsoup-2.4
Requires.private: libxml-2.0 libsoup-3.0
Libs: ${pc_top_builddir}/${pcfiledir}/libmateweather.la
Cflags: -I${pc_top_builddir}/${pcfiledir}/..
2 changes: 1 addition & 1 deletion libmateweather/mateweather.pc.in
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Name: MateWeather
Description: MateWeather shared library
Version: @VERSION@
Requires: glib-2.0 gobject-2.0 gdk-pixbuf-2.0 gtk+-3.0 gio-2.0
Requires.private: libxml-2.0 libsoup-2.4
Requires.private: libxml-2.0 libsoup-3.0
Libs: -L${libdir} -lmateweather
Libs.private: -lm
Cflags: -I${includedir}
36 changes: 24 additions & 12 deletions libmateweather/weather-bom.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,34 +27,45 @@
#include "weather-priv.h"

static void
bom_finish (SoupSession *session, SoupMessage *msg, gpointer data)
bom_finish (GObject *source, GAsyncResult *result, gpointer data)
{
char *p, *rp;
WeatherInfo *info = (WeatherInfo *)data;
GError *error = NULL;
GBytes *bytes;
const char *response_body = NULL;
gsize len = 0;

g_return_if_fail (info != NULL);

if (!SOUP_STATUS_IS_SUCCESSFUL (msg->status_code)) {
g_warning ("Failed to get BOM forecast data: %d %s.\n",
msg->status_code, msg->reason_phrase);
request_done (info, FALSE);
return;
bytes = soup_session_send_and_read_finish (SOUP_SESSION(source),
result, &error);

if (error != NULL) {
g_warning ("Failed to get BOM forecast data: %s.\n", error->message);
request_done (info, error);
g_error_free (error);
return;
}

p = strstr (msg->response_body->data, "Forecast for the rest");
response_body = g_bytes_get_data (bytes, &len);

p = xstrnstr (response_body, len, "Forecast for the rest");
if (p != NULL) {
rp = strstr (p, "The next routine forecast will be issued");
rp = xstrnstr (p, len - (p - response_body),
"The next routine forecast will be issued");
if (rp == NULL)
info->forecast = g_strdup (p);
info->forecast = g_strndup (p, len - (p - response_body));
else
info->forecast = g_strndup (p, rp - p);
}

if (info->forecast == NULL)
info->forecast = g_strdup (msg->response_body->data);
info->forecast = g_strndup (response_body, len);

g_bytes_unref (bytes);
g_print ("%s\n", info->forecast);
request_done (info, TRUE);
request_done (info, NULL);
}

void
Expand All @@ -70,7 +81,8 @@ bom_start_open (WeatherInfo *info)
loc->zone + 1);

msg = soup_message_new ("GET", url);
soup_session_queue_message (info->session, msg, bom_finish, info);
soup_session_send_and_read_async (info->session, msg, G_PRIORITY_DEFAULT,
NULL, bom_finish, info);
g_free (url);

info->requests_pending++;
Expand Down
37 changes: 25 additions & 12 deletions libmateweather/weather-iwin.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ hasAttr (xmlNode *node, const char *attr_name, const char *attr_value)
}

static GSList *
parseForecastXml (const char *buff, WeatherInfo *master_info)
parseForecastXml (const char *buff, gsize len, WeatherInfo *master_info)
{
GSList *res = NULL;
xmlDocPtr doc;
Expand All @@ -107,7 +107,7 @@ parseForecastXml (const char *buff, WeatherInfo *master_info)
#define XC (const xmlChar *)
#define isElem(_node,_name) g_str_equal ((const char *)_node->name, _name)

doc = xmlParseMemory (buff, strlen (buff));
doc = xmlParseMemory (buff, len);
if (!doc)
return NULL;

Expand Down Expand Up @@ -380,26 +380,36 @@ parseForecastXml (const char *buff, WeatherInfo *master_info)
}

static void
iwin_finish (SoupSession *session, SoupMessage *msg, gpointer data)
iwin_finish (GObject *source, GAsyncResult *result, gpointer data)
{
WeatherInfo *info = (WeatherInfo *)data;
GError *error = NULL;
GBytes *bytes;
const char *response_body = NULL;
gsize len = 0;

g_return_if_fail (info != NULL);

if (!SOUP_STATUS_IS_SUCCESSFUL (msg->status_code)) {
bytes = soup_session_send_and_read_finish (SOUP_SESSION(source),
result, &error);

if (error != NULL) {
/* forecast data is not really interesting anyway ;) */
g_warning ("Failed to get IWIN forecast data: %d %s\n",
msg->status_code, msg->reason_phrase);
request_done (info, FALSE);
g_warning ("Failed to get IWIN forecast data: %s\n",
error->message);
request_done (info, error);
g_error_free (error);
return;
}

response_body = g_bytes_get_data (bytes, &len);
if (info->forecast_type == FORECAST_LIST)
info->forecast_list = parseForecastXml (msg->response_body->data, info);
info->forecast_list = parseForecastXml (response_body, len, info);
else
info->forecast = formatWeatherMsg (g_strdup (msg->response_body->data));
info->forecast = formatWeatherMsg (g_strndup (response_body, len));

request_done (info, TRUE);
g_bytes_unref (bytes);
request_done (info, NULL);
}

/* Get forecast into newly alloc'ed string */
Expand Down Expand Up @@ -439,7 +449,9 @@ iwin_start_open (WeatherInfo *info)

msg = soup_message_new ("GET", url);
g_free (url);
soup_session_queue_message (info->session, msg, iwin_finish, info);
soup_session_send_and_read_async (info->session, msg,
G_PRIORITY_DEFAULT,
NULL, iwin_finish, info);

info->requests_pending++;
}
Expand Down Expand Up @@ -470,7 +482,8 @@ iwin_start_open (WeatherInfo *info)

msg = soup_message_new ("GET", url);
g_free (url);
soup_session_queue_message (info->session, msg, iwin_finish, info);
soup_session_send_and_read_async (info->session, msg, G_PRIORITY_DEFAULT,
NULL, iwin_finish, info);

info->requests_pending++;
}
34 changes: 23 additions & 11 deletions libmateweather/weather-met.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,19 +119,20 @@ met_reprocess (char *x, int len)
*/

static gchar *
met_parse (const gchar *meto)
met_parse (const gchar *meto, gsize len)
{
gchar *p;
gchar *rp;
gchar *r = g_strdup ("Met Office Forecast\n");
gchar *t;
const gchar *end = meto + len;

g_return_val_if_fail (meto != NULL, r);

p = strstr (meto, "Summary: </b>");
p = xstrnstr (meto, len, "Summary: </b>");
g_return_val_if_fail (p != NULL, r);

rp = strstr (p, "Text issued at:");
rp = xstrnstr (p, end - p, "Text issued at:");
g_return_val_if_fail (rp != NULL, r);

p += 13;
Expand All @@ -143,21 +144,31 @@ met_parse (const gchar *meto)
}

static void
met_finish (SoupSession *session, SoupMessage *msg, gpointer data)
met_finish (GObject *source, GAsyncResult *result, gpointer data)
{
WeatherInfo *info = (WeatherInfo *)data;
GError *error = NULL;
GBytes *bytes;
const char *response_body = NULL;
gsize len = 0;

g_return_if_fail (info != NULL);

if (!SOUP_STATUS_IS_SUCCESSFUL (msg->status_code)) {
g_warning ("Failed to get Met Office forecast data: %d %s.\n",
msg->status_code, msg->reason_phrase);
request_done (info, FALSE);
bytes = soup_session_send_and_read_finish (SOUP_SESSION(source),
result, &error);

if (error != NULL) {
g_warning ("Failed to get Met Office forecast data: %s.\n",
error->message);
request_done (info, error);
g_error_free (error);
return;
}

info->forecast = met_parse (msg->response_body->data);
request_done (info, TRUE);
response_body = g_bytes_get_data (bytes, &len);
info->forecast = met_parse (response_body, len);
g_bytes_unref (bytes);
request_done (info, NULL);
}

void
Expand All @@ -171,7 +182,8 @@ metoffice_start_open (WeatherInfo *info)
url = g_strdup_printf ("http://www.metoffice.gov.uk/weather/europe/uk/%s.html", loc->zone + 1);

msg = soup_message_new ("GET", url);
soup_session_queue_message (info->session, msg, met_finish, info);
soup_session_send_and_read_async (info->session, msg, G_PRIORITY_DEFAULT,
NULL, met_finish, info);
g_free (url);

info->requests_pending++;
Expand Down
Loading

0 comments on commit 97cd9b0

Please sign in to comment.