# HG changeset patch
# Node ID cccb7f669e47
# Parent  c8bf9a5663b1
diff --git a/finch/gntblist.c b/finch/gntblist.c
--- a/finch/gntblist.c
+++ b/finch/gntblist.c
@@ -306,9 +306,7 @@ static GList *managers;
 
 static void
 finch_blist_node_free(FinchBlistNode *node) {
-	if(node->signed_timer) {
-		g_source_remove(node->signed_timer);
-	}
+	g_clear_handle_id(&node->signed_timer, g_source_remove);
 
 	g_free(node);
 }
@@ -741,8 +739,7 @@ add_group_cb(FinchBuddyList *ggblist, co
 	 * make things easier to add buddies to empty groups (new or old) without having
 	 * to turn on 'show empty groups' setting */
 	ggblist->new_group = g_list_prepend(ggblist->new_group, grp);
-	if (ggblist->new_group_timeout)
-		g_source_remove(ggblist->new_group_timeout);
+	g_clear_handle_id(&ggblist->new_group_timeout, g_source_remove);
 	ggblist->new_group_timeout = g_timeout_add_seconds(SHOW_EMPTY_GROUP_TIMEOUT,
 			remove_new_empty_group, NULL);
 
@@ -1841,13 +1838,11 @@ reset_blist_window(G_GNUC_UNUSED GntWidg
 {
 	purple_signals_disconnect_by_handle(finch_blist_get_handle());
 
-	if (ggblist->typing)
-		g_source_remove(ggblist->typing);
+	g_clear_handle_id(&ggblist->typing, g_source_remove);
 	remove_peripherals(ggblist);
 	g_clear_list(&ggblist->tagged, NULL);
 
-	if (ggblist->new_group_timeout)
-		g_source_remove(ggblist->new_group_timeout);
+	g_clear_handle_id(&ggblist->new_group_timeout, g_source_remove);
 	g_clear_list(&ggblist->new_group, NULL);
 
 	ggblist = NULL;
@@ -2070,9 +2065,7 @@ remove_typing_cb(G_GNUC_UNUSED gpointer 
 	gnt_box_give_focus_to_child(GNT_BOX(ggblist->window), ggblist->tree);
 end:
 	g_free(escnewmessage);
-	if (ggblist->typing)
-		g_source_remove(ggblist->typing);
-	ggblist->typing = 0;
+	g_clear_handle_id(&ggblist->typing, g_source_remove);
 	return FALSE;
 }
 
@@ -2119,9 +2112,7 @@ status_text_changed(G_GNUC_UNUSED GntEnt
 	if ((text[0] == 27 || (text[0] == '\t' && text[1] == '\0')) && ggblist->typing == 0)
 		return FALSE;
 
-	if (ggblist->typing)
-		g_source_remove(ggblist->typing);
-	ggblist->typing = 0;
+	g_clear_handle_id(&ggblist->typing, g_source_remove);
 
 	if (text[0] == '\r' && text[1] == 0)
 	{
@@ -2340,8 +2331,7 @@ buddy_recent_signed_on_off(gpointer data
 	PurpleBlistNode *node = data;
 	FinchBlistNode *fnode = g_object_get_data(G_OBJECT(node), UI_DATA);
 
-	g_source_remove(fnode->signed_timer);
-	fnode->signed_timer = 0;
+	g_clear_handle_id(&fnode->signed_timer, g_source_remove);
 
 	if (!ggblist->manager->can_add_node(node)) {
 		node_remove(purple_blist_get_default(), node);
@@ -2364,9 +2354,7 @@ buddy_signed_on_off_cb(gpointer data)
 		return FALSE;
 	}
 
-	if(fnode->signed_timer) {
-		g_source_remove(fnode->signed_timer);
-	}
+	g_clear_handle_id(&fnode->signed_timer, g_source_remove);
 
 	g_object_ref(node);
 	fnode->signed_timer = g_timeout_add_seconds(6, (GSourceFunc)buddy_recent_signed_on_off, data);
diff --git a/finch/gntconn.c b/finch/gntconn.c
--- a/finch/gntconn.c
+++ b/finch/gntconn.c
@@ -50,8 +50,7 @@ free_auto_recon(gpointer data)
 {
 	FinchAutoRecon *info = data;
 
-	if (info->timeout != 0)
-		g_source_remove(info->timeout);
+	g_clear_handle_id(&info->timeout, g_source_remove);
 
 	g_free(info);
 }
@@ -99,8 +98,7 @@ finch_connection_report_disconnect(Purpl
 			info->delay = g_random_int_range(INITIAL_RECON_DELAY_MIN, INITIAL_RECON_DELAY_MAX);
 		} else {
 			info->delay = MIN(2 * info->delay, MAX_RECON_DELAY);
-			if (info->timeout != 0)
-				g_source_remove(info->timeout);
+			g_clear_handle_id(&info->timeout, g_source_remove);
 		}
 		info->timeout = g_timeout_add_seconds(info->delay, do_signon, account);
 	} else {
diff --git a/libpurple/accounts.c b/libpurple/accounts.c
--- a/libpurple/accounts.c
+++ b/libpurple/accounts.c
@@ -679,10 +679,8 @@ void
 purple_accounts_uninit(void)
 {
 	gpointer handle = purple_accounts_get_handle();
-	if (save_timer != 0)
-	{
-		g_source_remove(save_timer);
-		save_timer = 0;
+	if(save_timer != 0) {
+		g_clear_handle_id(&save_timer, g_source_remove);
 		sync_accounts();
 	}
 
diff --git a/libpurple/buddylist.c b/libpurple/buddylist.c
--- a/libpurple/buddylist.c
+++ b/libpurple/buddylist.c
@@ -2171,9 +2171,8 @@ purple_blist_uninit(void)
 	if (purplebuddylist == NULL)
 		return;
 
-	if (save_timer != 0) {
-		g_source_remove(save_timer);
-		save_timer = 0;
+	if(save_timer != 0) {
+		g_clear_handle_id(&save_timer, g_source_remove);
 		purple_blist_sync();
 	}
 
diff --git a/libpurple/connection.c b/libpurple/connection.c
--- a/libpurple/connection.c
+++ b/libpurple/connection.c
@@ -837,10 +837,7 @@ purple_connection_dispose(GObject *obj) 
 
 	g_clear_object(&priv->account);
 
-	if(priv->disconnect_timeout > 0) {
-		g_source_remove(priv->disconnect_timeout);
-		priv->disconnect_timeout = 0;
-	}
+	g_clear_handle_id(&priv->disconnect_timeout, g_source_remove);
 
 	G_OBJECT_CLASS(purple_connection_parent_class)->dispose(obj);
 }
diff --git a/libpurple/idle.c b/libpurple/idle.c
--- a/libpurple/idle.c
+++ b/libpurple/idle.c
@@ -282,9 +282,7 @@ idle_reporting_cb(G_GNUC_UNUSED const ch
                   G_GNUC_UNUSED PurplePrefType type,
                   G_GNUC_UNUSED gconstpointer val, G_GNUC_UNUSED gpointer data)
 {
-	if (idle_timer)
-		g_source_remove(idle_timer);
-	idle_timer = 0;
+	g_clear_handle_id(&idle_timer, g_source_remove);
 	check_idleness_timer(NULL);
 }
 
@@ -294,9 +292,7 @@ purple_idle_touch(void)
 	time(&last_active_time);
 	if (!no_away)
 	{
-		if (idle_timer)
-			g_source_remove(idle_timer);
-		idle_timer = 0;
+		g_clear_handle_id(&idle_timer, g_source_remove);
 		check_idleness_timer(NULL);
 	}
 }
@@ -373,9 +369,7 @@ purple_idle_uninit(void)
 	purple_prefs_disconnect_by_handle(purple_idle_get_handle());
 
 	/* Remove the idle timer */
-	if (idle_timer > 0)
-		g_source_remove(idle_timer);
-	idle_timer = 0;
+	g_clear_handle_id(&idle_timer, g_source_remove);
 
 	g_clear_object(&idle_ui);
 }
diff --git a/libpurple/mediamanager.c b/libpurple/mediamanager.c
--- a/libpurple/mediamanager.c
+++ b/libpurple/mediamanager.c
@@ -479,15 +479,8 @@ free_appdata_info_locked (PurpleMediaApp
 	info->readable_cb_token = 0;
 	info->writable_cb_token = 0;
 
-	if (info->readable_timer_id) {
-		g_source_remove (info->readable_timer_id);
-		info->readable_timer_id = 0;
-	}
-
-	if (info->writable_timer_id) {
-		g_source_remove (info->writable_timer_id);
-		info->writable_timer_id = 0;
-	}
+	g_clear_handle_id(&info->readable_timer_id, g_source_remove);
+	g_clear_handle_id(&info->writable_timer_id, g_source_remove);
 
 	g_clear_pointer(&info->current_sample, gst_sample_unref);
 
@@ -1582,15 +1575,8 @@ purple_media_manager_set_application_dat
 		info->notify (info->user_data);
 	}
 
-	if (info->readable_cb_token) {
-		g_source_remove (info->readable_timer_id);
-		info->readable_cb_token = 0;
-	}
-
-	if (info->writable_cb_token) {
-		g_source_remove (info->writable_timer_id);
-		info->writable_cb_token = 0;
-	}
+	g_clear_handle_id(&info->readable_cb_token, g_source_remove);
+	g_clear_handle_id(&info->writable_cb_token, g_source_remove);
 
 	if (callbacks) {
 		info->callbacks = *callbacks;
diff --git a/libpurple/prefs.c b/libpurple/prefs.c
--- a/libpurple/prefs.c
+++ b/libpurple/prefs.c
@@ -1478,9 +1478,8 @@ purple_prefs_init(void)
 void
 purple_prefs_uninit(void)
 {
-	if (save_timer != 0)
-	{
-		g_source_remove(save_timer);
+	if(save_timer != 0) {
+		g_clear_handle_id(&save_timer, g_source_remove);
 		save_cb(NULL);
 	}
 
diff --git a/libpurple/protocols/bonjour/xmpp.c b/libpurple/protocols/bonjour/xmpp.c
--- a/libpurple/protocols/bonjour/xmpp.c
+++ b/libpurple/protocols/bonjour/xmpp.c
@@ -254,8 +254,7 @@ static void
 	writelen = purple_circular_buffer_get_max_read(bconv->tx_buf);
 
 	if (writelen == 0) {
-		g_source_remove(bconv->tx_handler);
-		bconv->tx_handler = 0;
+		g_clear_handle_id(&bconv->tx_handler, g_source_remove);
 		return;
 	}
 
@@ -525,8 +524,7 @@ static void
 	bconv->stream_data = NULL;
 
 	/* Stream started; process the send buffer if there is one */
-	g_source_remove(bconv->tx_handler);
-	bconv->tx_handler = 0;
+	g_clear_handle_id(&bconv->tx_handler, g_source_remove);
 	bconv->sent_stream_start = FULLY_SENT;
 
 	bonjour_xmpp_stream_started(bconv);
@@ -1187,14 +1185,8 @@ bonjour_xmpp_close_conversation(BonjourX
 		                          G_INPUT_STREAM(bconv->input),
 		                          G_OUTPUT_STREAM(bconv->output));
 	}
-	if (bconv->rx_handler != 0) {
-		g_source_remove(bconv->rx_handler);
-		bconv->rx_handler = 0;
-	}
-	if (bconv->tx_handler != 0) {
-		g_source_remove(bconv->tx_handler);
-		bconv->tx_handler = 0;
-	}
+	g_clear_handle_id(&bconv->rx_handler, g_source_remove);
+	g_clear_handle_id(&bconv->tx_handler, g_source_remove);
 
 	/* Cancel any pending operations. */
 	if (bconv->cancellable != NULL) {
@@ -1218,9 +1210,7 @@ bonjour_xmpp_close_conversation(BonjourX
 		bonjour_parser_setup(bconv);
 	}
 
-	if (bconv->close_timeout != 0) {
-		g_source_remove(bconv->close_timeout);
-	}
+	g_clear_handle_id(&bconv->close_timeout, g_source_remove);
 
 	g_free(bconv->buddy_name);
 	g_free(bconv->ip);
diff --git a/libpurple/protocols/facebook/data.c b/libpurple/protocols/facebook/data.c
--- a/libpurple/protocols/facebook/data.c
+++ b/libpurple/protocols/facebook/data.c
@@ -292,8 +292,8 @@ fb_data_clear_timeout(FbData *fata, cons
 	ptr = g_hash_table_lookup(fata->evs, name);
 	id = GPOINTER_TO_UINT(ptr);
 
-	if ((id > 0) && remove) {
-		g_source_remove(id);
+	if(remove) {
+		g_clear_handle_id(&id, g_source_remove);
 	}
 
 	g_hash_table_remove(fata->evs, name);
diff --git a/libpurple/protocols/facebook/mqtt.c b/libpurple/protocols/facebook/mqtt.c
--- a/libpurple/protocols/facebook/mqtt.c
+++ b/libpurple/protocols/facebook/mqtt.c
@@ -217,10 +217,7 @@ fb_mqtt_close(FbMqtt *mqtt)
 {
 	g_return_if_fail(FB_IS_MQTT(mqtt));
 
-	if(mqtt->tev > 0) {
-		g_source_remove(mqtt->tev);
-		mqtt->tev = 0;
-	}
+	g_clear_handle_id(&mqtt->tev, g_source_remove);
 
 	if(mqtt->cancellable != NULL) {
 		g_cancellable_cancel(mqtt->cancellable);
@@ -303,10 +300,7 @@ fb_mqtt_cb_timeout(gpointer data)
 static void
 fb_mqtt_timeout_clear(FbMqtt *mqtt)
 {
-	if(mqtt->tev > 0) {
-		g_source_remove(mqtt->tev);
-		mqtt->tev = 0;
-	}
+	g_clear_handle_id(&mqtt->tev, g_source_remove);
 }
 
 static void
diff --git a/libpurple/protocols/gg/edisc.c b/libpurple/protocols/gg/edisc.c
--- a/libpurple/protocols/gg/edisc.c
+++ b/libpurple/protocols/gg/edisc.c
@@ -1467,10 +1467,7 @@ ggp_xfer_finalize(GObject *obj) {
 #endif
 	g_clear_object(&edisc_xfer->msg);
 
-	if(edisc_xfer->handler) {
-		g_source_remove(edisc_xfer->handler);
-		edisc_xfer->handler = 0;
-	}
+	g_clear_handle_id(&edisc_xfer->handler, g_source_remove);
 
 	if (edisc_xfer->ticket_id != NULL) {
 		g_hash_table_remove(sdata->xfers_initialized,
diff --git a/libpurple/protocols/gg/gg.c b/libpurple/protocols/gg/gg.c
--- a/libpurple/protocols/gg/gg.c
+++ b/libpurple/protocols/gg/gg.c
@@ -516,8 +516,7 @@ ggp_async_login_handler(gpointer _gc, G_
 	purple_debug_info("gg", "login_handler: session: check = %d; state = %d;\n",
 			info->session->check, info->session->state);
 
-	g_source_remove(info->inpa);
-	info->inpa = 0;
+	g_clear_handle_id(&info->inpa, g_source_remove);
 
 	/** XXX I think that this shouldn't be done if ev->type is GG_EVENT_CONN_FAILED or GG_EVENT_CONN_SUCCESS -datallah */
 	if (info->session->fd >= 0)
@@ -550,10 +549,7 @@ ggp_async_login_handler(gpointer _gc, G_
 			}
 			break;
 		case GG_EVENT_CONN_FAILED:
-			if (info->inpa > 0) {
-				g_source_remove(info->inpa);
-				info->inpa = 0;
-			}
+			g_clear_handle_id(&info->inpa, g_source_remove);
 			purple_debug_info("gg", "Connection failure: %d\n",
 				ev->event.failure);
 			switch (ev->event.failure) {
@@ -909,8 +905,7 @@ ggp_close(G_GNUC_UNUSED PurpleProtocol *
 		ggp_message_cleanup(gc);
 		ggp_edisc_cleanup(gc);
 
-		if (info->inpa > 0)
-			g_source_remove(info->inpa);
+		g_clear_handle_id(&info->inpa, g_source_remove);
 		g_free(info->imtoken);
 
 		if (info->http) {
diff --git a/libpurple/protocols/gg/roster.c b/libpurple/protocols/gg/roster.c
--- a/libpurple/protocols/gg/roster.c
+++ b/libpurple/protocols/gg/roster.c
@@ -239,8 +239,7 @@ void ggp_roster_cleanup(PurpleConnection
 {
 	ggp_roster_session_data *rdata = ggp_roster_get_rdata(gc);
 
-	if (rdata->timer)
-		g_source_remove(rdata->timer);
+	g_clear_handle_id(&rdata->timer, g_source_remove);
 	ggp_roster_content_free(rdata->content);
 	g_list_free_full(rdata->sent_updates, ggp_roster_change_free);
 	g_list_free_full(rdata->pending_updates, ggp_roster_change_free);
diff --git a/libpurple/protocols/gg/tcpsocket.c b/libpurple/protocols/gg/tcpsocket.c
--- a/libpurple/protocols/gg/tcpsocket.c
+++ b/libpurple/protocols/gg/tcpsocket.c
@@ -115,10 +115,7 @@ ggp_tcpsocket_connected(GObject *source,
 
 	info = purple_connection_get_protocol_data(data->gc);
 
-	if (info->inpa > 0) {
-		g_source_remove(info->inpa);
-		info->inpa = 0;
-	}
+	g_clear_handle_id(&info->inpa, g_source_remove);
 
 	if (info->session->fd < 0)
 		return;
diff --git a/libpurple/protocols/irc/dcc_send.c b/libpurple/protocols/irc/dcc_send.c
--- a/libpurple/protocols/irc/dcc_send.c
+++ b/libpurple/protocols/irc/dcc_send.c
@@ -173,8 +173,7 @@ irc_dccsend_send_read(gpointer data, int
 		return;
 	else if (len <= 0) {
 		/* XXX: Shouldn't this be canceling the transfer? */
-		g_source_remove(xd->inpa);
-		xd->inpa = 0;
+		g_clear_handle_id(&xd->inpa, g_source_remove);
 		return;
 	}
 
@@ -203,8 +202,7 @@ irc_dccsend_send_read(gpointer data, int
 		}
 
 		if ((goffset)acked >= purple_xfer_get_size(xfer)) {
-			g_source_remove(xd->inpa);
-			xd->inpa = 0;
+			g_clear_handle_id(&xd->inpa, g_source_remove);
 			purple_xfer_set_completed(xfer, TRUE);
 			purple_xfer_end(xfer);
 			return;
@@ -387,9 +385,7 @@ irc_xfer_finalize(GObject *obj) {
 		g_socket_service_stop(xfer->service);
 	}
 	g_clear_object(&xfer->service);
-	if(xfer->inpa > 0) {
-		g_source_remove(xfer->inpa);
-	}
+	g_clear_handle_id(&xfer->inpa, g_source_remove);
 	g_clear_object(&xfer->conn);
 
 	G_OBJECT_CLASS(irc_xfer_parent_class)->finalize(obj);
diff --git a/libpurple/protocols/irc/irc.c b/libpurple/protocols/irc/irc.c
--- a/libpurple/protocols/irc/irc.c
+++ b/libpurple/protocols/irc/irc.c
@@ -708,8 +708,7 @@ irc_close(G_GNUC_UNUSED PurpleProtocol *
 	g_clear_object(&irc->output);
 	g_clear_object(&irc->conn);
 
-	if (irc->timer)
-		g_source_remove(irc->timer);
+	g_clear_handle_id(&irc->timer, g_source_remove);
 	g_hash_table_destroy(irc->cmds);
 	g_hash_table_destroy(irc->msgs);
 	g_hash_table_destroy(irc->buddies);
diff --git a/libpurple/protocols/jabber/bosh.c b/libpurple/protocols/jabber/bosh.c
--- a/libpurple/protocols/jabber/bosh.c
+++ b/libpurple/protocols/jabber/bosh.c
@@ -155,8 +155,7 @@ jabber_bosh_connection_destroy(PurpleJab
 		jabber_bosh_connection_send_now(conn);
 	}
 
-	if (conn->send_timer)
-		g_source_remove(conn->send_timer);
+	g_clear_handle_id(&conn->send_timer, g_source_remove);
 
 	soup_session_abort(conn->payload_reqs);
 
@@ -295,10 +294,7 @@ jabber_bosh_connection_send_now(PurpleJa
 
 	g_return_if_fail(conn != NULL);
 
-	if (conn->send_timer != 0) {
-		g_source_remove(conn->send_timer);
-		conn->send_timer = 0;
-	}
+	g_clear_handle_id(&conn->send_timer, g_source_remove);
 
 	if (conn->sid == NULL)
 		return;
diff --git a/libpurple/protocols/jabber/buddy.c b/libpurple/protocols/jabber/buddy.c
--- a/libpurple/protocols/jabber/buddy.c
+++ b/libpurple/protocols/jabber/buddy.c
@@ -705,8 +705,7 @@ jabber_setup_set_info(G_GNUC_UNUSED GSim
 static void jabber_buddy_info_destroy(JabberBuddyInfo *jbi)
 {
 	/* Remove the timeout, which would otherwise trigger jabber_buddy_get_info_timeout() */
-	if (jbi->timeout_handle > 0)
-		g_source_remove(jbi->timeout_handle);
+	g_clear_handle_id(&jbi->timeout_handle, g_source_remove);
 
 	g_slist_free(jbi->ids);
 	g_free(jbi->jid);
diff --git a/libpurple/protocols/jabber/caps.c b/libpurple/protocols/jabber/caps.c
--- a/libpurple/protocols/jabber/caps.c
+++ b/libpurple/protocols/jabber/caps.c
@@ -202,9 +202,8 @@ void jabber_caps_init(void)
 
 void jabber_caps_uninit(void)
 {
-	if (save_timer != 0) {
-		g_source_remove(save_timer);
-		save_timer = 0;
+	if(save_timer != 0) {
+		g_clear_handle_id(&save_timer, g_source_remove);
 		do_jabber_caps_store(NULL);
 	}
 	g_clear_pointer(&capstable, g_hash_table_destroy);
diff --git a/libpurple/protocols/jabber/jabber.c b/libpurple/protocols/jabber/jabber.c
--- a/libpurple/protocols/jabber/jabber.c
+++ b/libpurple/protocols/jabber/jabber.c
@@ -701,8 +701,7 @@ static void tls_init(JabberStream *js)
 	GIOStream *tls_conn;
 	GError *error = NULL;
 
-	g_source_remove(js->inpa);
-	js->inpa = 0;
+	g_clear_handle_id(&js->inpa, g_source_remove);
 	js->input = NULL;
 	g_filter_output_stream_set_close_base_stream(
 	        G_FILTER_OUTPUT_STREAM(js->output), FALSE);
@@ -1009,10 +1008,7 @@ jabber_close(G_GNUC_UNUSED PurpleProtoco
 		 *
 		 * jabber_send_raw(js, "</stream:stream>", -1);
 		 */
-		if(js->inpa) {
-			g_source_remove(js->inpa);
-			js->inpa = 0;
-		}
+		g_clear_handle_id(&js->inpa, g_source_remove);
 		purple_gio_graceful_close(js->stream, js->input,
 		                          G_OUTPUT_STREAM(js->output));
 	}
@@ -1053,12 +1049,9 @@ jabber_close(G_GNUC_UNUSED PurpleProtoco
 	g_free(js->old_msg);
 	g_free(js->old_avatarhash);
 
-	if (js->keepalive_timeout != 0)
-		g_source_remove(js->keepalive_timeout);
-	if (js->inactivity_timer != 0)
-		g_source_remove(js->inactivity_timer);
-	if (js->conn_close_timeout != 0)
-		g_source_remove(js->conn_close_timeout);
+	g_clear_handle_id(&js->keepalive_timeout, g_source_remove);
+	g_clear_handle_id(&js->inactivity_timer, g_source_remove);
+	g_clear_handle_id(&js->conn_close_timeout, g_source_remove);
 
 	g_cancellable_cancel(js->cancellable);
 	g_object_unref(G_OBJECT(js->cancellable));
@@ -1447,10 +1440,7 @@ inactivity_cb(gpointer data)
 
 void jabber_stream_restart_inactivity_timer(JabberStream *js)
 {
-	if (js->inactivity_timer != 0) {
-		g_source_remove(js->inactivity_timer);
-		js->inactivity_timer = 0;
-	}
+	g_clear_handle_id(&js->inactivity_timer, g_source_remove);
 
 	g_return_if_fail(js->max_inactivity > 0);
 
diff --git a/libpurple/protocols/jabber/ping.c b/libpurple/protocols/jabber/ping.c
--- a/libpurple/protocols/jabber/ping.c
+++ b/libpurple/protocols/jabber/ping.c
@@ -35,10 +35,7 @@ jabber_keepalive_pong_cb(JabberStream *j
                          G_GNUC_UNUSED PurpleXmlNode *packet,
                          G_GNUC_UNUSED gpointer data)
 {
-	if (js->keepalive_timeout != 0) {
-		g_source_remove(js->keepalive_timeout);
-		js->keepalive_timeout = 0;
-	}
+	g_clear_handle_id(&js->keepalive_timeout, g_source_remove);
 }
 
 void
diff --git a/libpurple/protocols/jabber/si.c b/libpurple/protocols/jabber/si.c
--- a/libpurple/protocols/jabber/si.c
+++ b/libpurple/protocols/jabber/si.c
@@ -185,10 +185,7 @@ jabber_si_bytestreams_connect_cb(GObject
 static void
 jabber_si_bytestreams_ibb_timeout_remove(JabberSIXfer *jsx)
 {
-	if (jsx->ibb_timeout_handle) {
-		g_source_remove(jsx->ibb_timeout_handle);
-		jsx->ibb_timeout_handle = 0;
-	}
+	g_clear_handle_id(&jsx->ibb_timeout_handle, g_source_remove);
 }
 
 static gboolean
@@ -1847,13 +1844,8 @@ jabber_si_xfer_finalize(GObject *obj) {
 		jabber_iq_remove_callback_by_id(js, jsx->iq_id);
 	}
 
-	if (jsx->connect_timeout > 0) {
-		g_source_remove(jsx->connect_timeout);
-	}
-
-	if (jsx->ibb_timeout_handle > 0) {
-		g_source_remove(jsx->ibb_timeout_handle);
-	}
+	g_clear_handle_id(&jsx->connect_timeout, g_source_remove);
+	g_clear_handle_id(&jsx->ibb_timeout_handle, g_source_remove);
 
 	g_list_free_full(jsx->streamhosts, (GDestroyNotify)jabber_bytestreams_streamhost_free);
 
diff --git a/libpurple/purpleconversationmember.c b/libpurple/purpleconversationmember.c
--- a/libpurple/purpleconversationmember.c
+++ b/libpurple/purpleconversationmember.c
@@ -128,10 +128,7 @@ purple_conversation_member_dispose(GObje
 
 	g_clear_object(&member->contact_info);
 
-	if(member->typing_timeout != 0) {
-		g_source_remove(member->typing_timeout);
-		member->typing_timeout = 0;
-	}
+	g_clear_handle_id(&member->typing_timeout, g_source_remove);
 
 	G_OBJECT_CLASS(purple_conversation_member_parent_class)->dispose(obj);
 }
@@ -246,10 +243,7 @@ purple_conversation_member_set_typing_st
 	g_return_if_fail(PURPLE_IS_CONVERSATION_MEMBER(member));
 
 	/* Remove an existing timeout if necessary. */
-	if(member->typing_timeout != 0) {
-		g_source_remove(member->typing_timeout);
-		member->typing_timeout = 0;
-	}
+	g_clear_handle_id(&member->typing_timeout, g_source_remove);
 
 	/* If the state has changed, notify. */
 	if(state != member->typing_state) {
diff --git a/libpurple/purpleimconversation.c b/libpurple/purpleimconversation.c
--- a/libpurple/purpleimconversation.c
+++ b/libpurple/purpleimconversation.c
@@ -274,10 +274,7 @@ purple_im_conversation_start_typing_time
 {
 	g_return_if_fail(PURPLE_IS_IM_CONVERSATION(im));
 
-	if(im->typing_timeout > 0) {
-		purple_im_conversation_stop_typing_timeout(im);
-	}
-
+	purple_im_conversation_stop_typing_timeout(im);
 	im->typing_timeout =
 		g_timeout_add_seconds(timeout, purple_im_conversation_reset_typing_cb,
 		                      im);
@@ -287,12 +284,7 @@ void
 purple_im_conversation_stop_typing_timeout(PurpleIMConversation *im) {
 	g_return_if_fail(PURPLE_IS_IM_CONVERSATION(im));
 
-	if(im->typing_timeout == 0) {
-		return;
-	}
-
-	g_source_remove(im->typing_timeout);
-	im->typing_timeout = 0;
+	g_clear_handle_id(&im->typing_timeout, g_source_remove);
 }
 
 guint
@@ -334,12 +326,7 @@ void
 purple_im_conversation_stop_send_typed_timeout(PurpleIMConversation *im) {
 	g_return_if_fail(PURPLE_IS_IM_CONVERSATION(im));
 
-	if(im->send_typed_timeout == 0) {
-		return;
-	}
-
-	g_source_remove(im->send_typed_timeout);
-	im->send_typed_timeout = 0;
+	g_clear_handle_id(&im->typing_timeout, g_source_remove);
 }
 
 guint
diff --git a/libpurple/savedstatuses.c b/libpurple/savedstatuses.c
--- a/libpurple/savedstatuses.c
+++ b/libpurple/savedstatuses.c
@@ -1261,10 +1261,8 @@ purple_savedstatuses_uninit(void)
 
 	remove_old_transient_statuses();
 
-	if (save_timer != 0)
-	{
-		g_source_remove(save_timer);
-		save_timer = 0;
+	if(save_timer != 0) {
+		g_clear_handle_id(&save_timer, g_source_remove);
 		sync_statuses();
 	}
 
diff --git a/pidgin/gtkmedia.c b/pidgin/gtkmedia.c
--- a/pidgin/gtkmedia.c
+++ b/pidgin/gtkmedia.c
@@ -388,9 +388,7 @@ pidgin_media_dispose(GObject *media)
 
 	g_clear_object(&gtkmedia->priv->ui);
 
-	if (gtkmedia->priv->timeout_id != 0) {
-		g_source_remove(gtkmedia->priv->timeout_id);
-	}
+	g_clear_handle_id(&gtkmedia->priv->timeout_id, g_source_remove);
 
 	g_clear_pointer(&gtkmedia->priv->recv_progressbars, g_hash_table_destroy);
 	g_clear_pointer(&gtkmedia->priv->remote_videos, g_hash_table_destroy);
@@ -768,8 +766,7 @@ pidgin_media_ready_cb(PurpleMedia *media
 	}
 
 	if (purple_media_is_initiator(media, sid, NULL) == FALSE) {
-		if (gtkmedia->priv->timeout_id != 0)
-			g_source_remove(gtkmedia->priv->timeout_id);
+		g_clear_handle_id(&gtkmedia->priv->timeout_id, g_source_remove);
 		gtkmedia->priv->request_type |= type;
 		gtkmedia->priv->timeout_id = g_timeout_add(500,
 				(GSourceFunc)pidgin_request_timeout_cb,
diff --git a/pidgin/gtkroomlist.c b/pidgin/gtkroomlist.c
--- a/pidgin/gtkroomlist.c
+++ b/pidgin/gtkroomlist.c
@@ -80,9 +80,6 @@ pidgin_roomlist_close(PidginRoomlistDial
 	if (dialog->roomlist && purple_roomlist_get_in_progress(dialog->roomlist))
 		purple_roomlist_cancel_get_list(dialog->roomlist);
 
-	if (dialog->pg_update_to > 0)
-		g_source_remove(dialog->pg_update_to);
-
 	if (dialog->roomlist) {
 		PidginRoomlist *rl = NULL;
 
@@ -98,6 +95,8 @@ pidgin_roomlist_close(PidginRoomlistDial
 		g_object_unref(dialog->roomlist);
 	}
 
+	g_clear_handle_id(&dialog->pg_update_to, g_source_remove);
+
 	dialog->progress = NULL;
 }
 
diff --git a/pidgin/pidgindebug.c b/pidgin/pidgindebug.c
--- a/pidgin/pidgindebug.c
+++ b/pidgin/pidgindebug.c
@@ -925,14 +925,8 @@ pidgin_debug_init(void)
 void
 pidgin_debug_uninit(void)
 {
-	if(pref_callback_id != 0) {
-		purple_prefs_disconnect_callback(pref_callback_id);
-	}
-	pref_callback_id = 0;
-	if(debug_enabled_timer != 0) {
-		g_source_remove(debug_enabled_timer);
-	}
-	debug_enabled_timer = 0;
+	g_clear_handle_id(&pref_callback_id, purple_prefs_disconnect_callback);
+	g_clear_handle_id(&debug_enabled_timer, g_source_remove);
 }
 
 void *
diff --git a/pidgin/plugins/gestures/stroke-draw.c b/pidgin/plugins/gestures/stroke-draw.c
--- a/pidgin/plugins/gestures/stroke-draw.c
+++ b/pidgin/plugins/gestures/stroke-draw.c
@@ -96,10 +96,7 @@ gstroke_cancel(GtkWidget *widget, GdkEve
 {
 	last_mouse_position.invalid = TRUE;
 
-	if (timer_id > 0)
-	    g_source_remove (timer_id);
-
-	timer_id = 0;
+	g_clear_handle_id(&timer_id, g_source_remove);
 
 	if (event != NULL) {
 		gdk_seat_ungrab(gdk_event_get_seat(event));
@@ -158,9 +155,8 @@ process_event(GtkWidget *widget, GdkEven
 
       last_mouse_position.invalid = TRUE;
       original_widget = NULL;
-      g_source_remove (timer_id);
+      g_clear_handle_id(&timer_id, g_source_remove);
 	  gdk_seat_ungrab(gdk_event_get_seat(event));
-      timer_id = 0;
 
 			{
 				GtkWidget *history = data;
