[balsa/wip/gtk4: 114/351] Use g_{clear,set}_object



commit 9fb060a91c4bb06d57bef096e1fe7feb2d1f4694
Author: Peter Bloomfield <PeterBloomfield bellsouth net>
Date:   Fri Jan 5 23:06:37 2018 -0500

    Use g_{clear,set}_object

 libbalsa/address-book-osmo.c        |    5 +--
 libbalsa/body.c                     |   16 +++------
 libbalsa/identity.c                 |   21 +++---------
 libbalsa/libbalsa-vfs.c             |    6 +--
 libbalsa/mailbox.c                  |   60 +++++++---------------------------
 libbalsa/mailbox_imap.c             |    4 +--
 libbalsa/mailbox_local.c            |    9 +----
 libbalsa/mailbox_remote.c           |    4 +--
 libbalsa/message.c                  |   55 ++++++--------------------------
 libbalsa/rfc3156.c                  |   22 +++++--------
 libbalsa/rfc6350.c                  |    9 ++---
 libinit_balsa/assistant_page_user.c |    1 +
 libnetclient/net-client-pop.c       |    3 +-
 libnetclient/net-client.c           |   43 +++++++-----------------
 src/balsa-icons.c                   |    1 +
 src/balsa-index.c                   |    5 +--
 src/balsa-message.c                 |   36 +++++---------------
 src/balsa-mime-widget.c             |   12 +++----
 src/balsa-mime-widget.h             |    1 -
 src/balsa-print-object-default.c    |    3 +-
 src/balsa-print-object-image.c      |    3 +-
 src/mailbox-conf.c                  |    3 +-
 src/print-gtk.c                     |   11 ++-----
 src/toolbar-factory.c               |    6 +--
 24 files changed, 91 insertions(+), 248 deletions(-)
---
diff --git a/libbalsa/address-book-osmo.c b/libbalsa/address-book-osmo.c
index e5028cf..bee55da 100644
--- a/libbalsa/address-book-osmo.c
+++ b/libbalsa/address-book-osmo.c
@@ -99,10 +99,7 @@ libbalsa_address_book_osmo_finalize(GObject *object)
        LibBalsaAddressBookOsmo *osmo;
 
        osmo = LIBBALSA_ADDRESS_BOOK_OSMO(object);
-       if (osmo->proxy != NULL) {
-               g_object_unref(osmo->proxy);
-               osmo->proxy = NULL;
-       }
+        g_clear_object(&osmo->proxy);
 
        G_OBJECT_CLASS(libbalsa_address_book_osmo_parent_class)->finalize(object);
 }
diff --git a/libbalsa/body.c b/libbalsa/body.c
index caf68a2..6b18270 100644
--- a/libbalsa/body.c
+++ b/libbalsa/body.c
@@ -76,8 +76,7 @@ libbalsa_message_body_free(LibBalsaMessageBody * body)
     libbalsa_message_headers_destroy(body->embhdrs);
     g_free(body->content_type);
     g_free(body->filename);
-    if (body->file_uri)
-        g_object_unref(body->file_uri);
+    g_clear_object(&body->file_uri);
     if (body->temp_filename) {
        unlink(body->temp_filename);
         g_free(body->temp_filename);
@@ -86,16 +85,14 @@ libbalsa_message_body_free(LibBalsaMessageBody * body)
     g_free(body->charset);
 
 #ifdef HAVE_GPGME
-    if (body->sig_info)
-       g_object_unref(G_OBJECT(body->sig_info));
+    g_clear_object(&body->sig_info);
 #endif
 
     libbalsa_message_body_free(body->next);
     libbalsa_message_body_free(body->parts);
 
-    if (body->mime_part)
-       g_object_unref(body->mime_part);        
-    
+    g_clear_object(&body->mime_part);
+
     g_free(body);
 }
 
@@ -273,10 +270,7 @@ libbalsa_message_body_set_mime_body(LibBalsaMessageBody * body,
     g_return_if_fail(body != NULL);
     g_return_if_fail(GMIME_IS_OBJECT(mime_part));
 
-    g_object_ref(mime_part);
-    if (body->mime_part)
-       g_object_unref(body->mime_part);
-    body->mime_part = mime_part;
+    g_set_object(&body->mime_part, mime_part);
 
     libbalsa_message_body_set_filename(body);
     libbalsa_message_body_set_types(body);
diff --git a/libbalsa/identity.c b/libbalsa/identity.c
index 3d8d2ab..479913a 100644
--- a/libbalsa/identity.c
+++ b/libbalsa/identity.c
@@ -133,8 +133,7 @@ libbalsa_identity_finalize(GObject * object)
 {
     LibBalsaIdentity *ident = LIBBALSA_IDENTITY(object);
 
-    if (ident->ia)
-       g_object_unref(ident->ia);
+    g_clear_object(&ident->ia);
     g_free(ident->identity_name);
     g_free(ident->replyto);
     g_free(ident->domain);
@@ -142,8 +141,7 @@ libbalsa_identity_finalize(GObject * object)
     g_free(ident->reply_string);
     g_free(ident->forward_string);
     g_free(ident->signature_path);
-    if (ident->smtp_server)
-        g_object_unref(ident->smtp_server);
+    g_clear_object(&ident->smtp_server);
     g_free(ident->face);
     g_free(ident->x_face);
     g_free(ident->force_gpg_key_id);
@@ -199,9 +197,7 @@ libbalsa_identity_set_address(LibBalsaIdentity * ident,
 {
     g_return_if_fail(ident != NULL);
 
-    if (ident->ia)
-       g_object_unref(ident->ia);
-    ident->ia = ia;
+    g_set_object(&ident->ia, ia);
 }
 
 
@@ -383,11 +379,7 @@ libbalsa_identity_set_smtp_server(LibBalsaIdentity * ident,
 {
     g_return_if_fail(ident != NULL);
 
-    if (ident->smtp_server)
-       g_object_unref(ident->smtp_server);
-    ident->smtp_server = smtp_server;
-    if (smtp_server)
-       g_object_ref(smtp_server);
+    g_set_object(&ident->smtp_server, smtp_server);
 }
 
 
@@ -1503,6 +1495,7 @@ ident_dialog_update(GObject * dlg)
     internet_address_set_name(ia, text);
     libbalsa_identity_set_address(id, ia);
     g_free(text);
+    g_object_unref(ia);
 
     g_free(id->replyto);
     id->replyto         = ident_dialog_get_text(dlg, "identity-replyto");
@@ -1515,9 +1508,7 @@ ident_dialog_update(GObject * dlg)
     g_free(id->forward_string);
     id->forward_string  = ident_dialog_get_text(dlg, "identity-forwardstring");
     id->send_mp_alternative = ident_dialog_get_bool(dlg, "identity-sendmpalternative");
-    if(id->smtp_server) g_object_unref(id->smtp_server);
-    id->smtp_server = ident_dialog_get_value(dlg, "identity-smtp-server");
-    g_object_ref(id->smtp_server);
+    g_set_object(&id->smtp_server, ident_dialog_get_value(dlg, "identity-smtp-server"));
 
     g_free(id->signature_path);
     id->signature_path  = ident_dialog_get_text(dlg, "identity-sigpath");
diff --git a/libbalsa/libbalsa-vfs.c b/libbalsa/libbalsa-vfs.c
index 66adb0c..bdd6344 100644
--- a/libbalsa/libbalsa-vfs.c
+++ b/libbalsa/libbalsa-vfs.c
@@ -136,10 +136,8 @@ libbalsa_vfs_finalize(LibbalsaVfs * self)
         g_free(priv->folder_uri);
         g_free(priv->mime_type);
         g_free(priv->charset);
-        if (priv->gio_gfile)
-            g_object_unref(priv->gio_gfile);
-        if (priv->info)
-            g_object_unref(priv->info);
+        g_clear_object(&priv->gio_gfile);
+        g_clear_object(&priv->info);
         g_free(priv);
     }
 
diff --git a/libbalsa/mailbox.c b/libbalsa/mailbox.c
index c2a881e..778c148 100644
--- a/libbalsa/mailbox.c
+++ b/libbalsa/mailbox.c
@@ -945,10 +945,7 @@ static void
 libbalsa_mailbox_real_release_message(LibBalsaMailbox * mailbox,
                                       LibBalsaMessage * message)
 {
-    if (message->mime_msg) {
-       g_object_unref(message->mime_msg);
-       message->mime_msg = NULL;
-    }
+    g_clear_object(&message->mime_msg);
 }
 
 struct MsgCopyData {
@@ -972,10 +969,7 @@ copy_iterator(LibBalsaMessageFlag *flags, GMimeStream **stream, void * arg)
     if(mcd->current_idx >= mcd->msgnos->len)
        return FALSE; /* no more messages */
        
-    if(mcd->stream) {
-       g_object_unref(mcd->stream);
-       mcd->stream = NULL;
-    }
+    g_clear_object(&mcd->stream);
     msgno_has_flags = LIBBALSA_MAILBOX_GET_CLASS(mailbox)->msgno_has_flags;
     msgno = g_array_index(mcd->msgnos, guint, mcd->current_idx);
 
@@ -1042,8 +1036,7 @@ libbalsa_mailbox_real_messages_copy(LibBalsaMailbox * mailbox,
                                                       copy_iterator,
                                                       &mcd,
                                                       err);
-    if(mcd.stream)
-       g_object_unref(mcd.stream);
+    g_clear_object(&mcd.stream);
 
     libbalsa_progress_set_text(&mcd.progress, NULL, 0);
 
@@ -3150,86 +3143,59 @@ mbox_model_iter_parent(GtkTreeModel     * tree_model,
 }
 
 /* Set icons used in tree view. */
-static void
-libbalsa_mailbox_set_icon(GdkPixbuf * pixbuf, GdkPixbuf ** pixbuf_store)
-{
-    if (*pixbuf_store)
-        g_object_unref(*pixbuf_store);
-    *pixbuf_store = pixbuf;
-}
 
 /* Icons for status column. */
 void
 libbalsa_mailbox_set_unread_icon(GdkPixbuf * pixbuf)
 {
-    libbalsa_mailbox_set_icon(pixbuf,
-                              &status_icons
-                              [LIBBALSA_MESSAGE_STATUS_UNREAD]);
+    g_set_object(&status_icons[LIBBALSA_MESSAGE_STATUS_UNREAD], pixbuf);
 }
 
 void libbalsa_mailbox_set_trash_icon(GdkPixbuf * pixbuf)
 {
-    libbalsa_mailbox_set_icon(pixbuf,
-                              &status_icons
-                              [LIBBALSA_MESSAGE_STATUS_DELETED]);
+    g_set_object(&status_icons[LIBBALSA_MESSAGE_STATUS_DELETED], pixbuf);
 }
 
 void libbalsa_mailbox_set_flagged_icon(GdkPixbuf * pixbuf)
 {
-    libbalsa_mailbox_set_icon(pixbuf,
-                              &status_icons
-                              [LIBBALSA_MESSAGE_STATUS_FLAGGED]);
+    g_set_object(&status_icons[LIBBALSA_MESSAGE_STATUS_FLAGGED], pixbuf);
 }
 
 void libbalsa_mailbox_set_replied_icon(GdkPixbuf * pixbuf)
 {
-    libbalsa_mailbox_set_icon(pixbuf,
-                              &status_icons
-                              [LIBBALSA_MESSAGE_STATUS_REPLIED]);
+    g_set_object(&status_icons[LIBBALSA_MESSAGE_STATUS_REPLIED], pixbuf);
 }
 
 /* Icons for attachment column. */
 void libbalsa_mailbox_set_attach_icon(GdkPixbuf * pixbuf)
 {
-    libbalsa_mailbox_set_icon(pixbuf,
-                              &attach_icons
-                              [LIBBALSA_MESSAGE_ATTACH_ATTACH]);
+    g_set_object(&status_icons[LIBBALSA_MESSAGE_ATTACH_ATTACH], pixbuf);
 }
 
 #ifdef HAVE_GPGME
 void libbalsa_mailbox_set_good_icon(GdkPixbuf * pixbuf)
 {
-    libbalsa_mailbox_set_icon(pixbuf,
-                              &attach_icons
-                              [LIBBALSA_MESSAGE_ATTACH_GOOD]);
+    g_set_object(&status_icons[LIBBALSA_MESSAGE_ATTACH_GOOD], pixbuf);
 }
 
 void libbalsa_mailbox_set_notrust_icon(GdkPixbuf * pixbuf)
 {
-    libbalsa_mailbox_set_icon(pixbuf,
-                              &attach_icons
-                              [LIBBALSA_MESSAGE_ATTACH_NOTRUST]);
+    g_set_object(&status_icons[LIBBALSA_MESSAGE_ATTACH_NOTRUST], pixbuf);
 }
 
 void libbalsa_mailbox_set_bad_icon(GdkPixbuf * pixbuf)
 {
-    libbalsa_mailbox_set_icon(pixbuf,
-                              &attach_icons
-                              [LIBBALSA_MESSAGE_ATTACH_BAD]);
+    g_set_object(&status_icons[LIBBALSA_MESSAGE_ATTACH_BAD], pixbuf);
 }
 
 void libbalsa_mailbox_set_sign_icon(GdkPixbuf * pixbuf)
 {
-    libbalsa_mailbox_set_icon(pixbuf,
-                              &attach_icons
-                              [LIBBALSA_MESSAGE_ATTACH_SIGN]);
+    g_set_object(&status_icons[LIBBALSA_MESSAGE_ATTACH_SIGN], pixbuf);
 }
 
 void libbalsa_mailbox_set_encr_icon(GdkPixbuf * pixbuf)
 {
-    libbalsa_mailbox_set_icon(pixbuf,
-                              &attach_icons
-                              [LIBBALSA_MESSAGE_ATTACH_ENCR]);
+    g_set_object(&status_icons[LIBBALSA_MESSAGE_ATTACH_ENCR], pixbuf);
 }
 #endif /* HAVE_GPGME */
 
diff --git a/libbalsa/mailbox_imap.c b/libbalsa/mailbox_imap.c
index f2b2736..910498a 100644
--- a/libbalsa/mailbox_imap.c
+++ b/libbalsa/mailbox_imap.c
@@ -849,9 +849,7 @@ imap_exists_idle(gpointer data)
                     message_info_from_msgno(mimap, i+1);
                 if(!msg_info)
                     continue;
-                if(msg_info->message)
-                    g_object_unref(msg_info->message);
-                msg_info->message = NULL;
+                g_clear_object(&msg_info->message);
                 msgid = g_ptr_array_index(mimap->msgids, i);
                 if(msgid) { 
                     g_free(msgid);
diff --git a/libbalsa/mailbox_local.c b/libbalsa/mailbox_local.c
index 142be75..82abbd1 100644
--- a/libbalsa/mailbox_local.c
+++ b/libbalsa/mailbox_local.c
@@ -295,9 +295,7 @@ lbml_add_message_to_pool(LibBalsaMailboxLocal * local,
             oldest = item;
     }
 
-    if (oldest->message)
-        g_object_unref(oldest->message);
-    oldest->message = g_object_ref(message);
+    g_set_object(&oldest->message, message);
     oldest->pool_seqno = local->pool_seqno;
 }
 
@@ -784,10 +782,7 @@ libbalsa_mailbox_local_close_mailbox(LibBalsaMailbox * mailbox,
 
     for (item = &local->message_pool[0];
          item < &local->message_pool[LBML_POOL_SIZE]; item++) {
-        if (item->message) {
-            g_object_unref(item->message);
-            item->message = NULL;
-        }
+        g_clear_object(&item->message);
         item->pool_seqno = 0;
     }
     local->pool_seqno = 0;
diff --git a/libbalsa/mailbox_remote.c b/libbalsa/mailbox_remote.c
index c9be8e2..815c003 100644
--- a/libbalsa/mailbox_remote.c
+++ b/libbalsa/mailbox_remote.c
@@ -92,7 +92,5 @@ libbalsa_mailbox_remote_test_can_reach(LibBalsaMailbox          * mailbox,
 void 
 libbalsa_mailbox_remote_set_server(LibBalsaMailboxRemote *m, LibBalsaServer *s)
 {
-    if(m->server) g_object_unref(G_OBJECT(m->server));
-    m->server = s;
-    if(s) g_object_ref(G_OBJECT(s));
+    g_set_object(&m->server, s);
 }
diff --git a/libbalsa/message.c b/libbalsa/message.c
index d9ad40d..4f068f1 100644
--- a/libbalsa/message.c
+++ b/libbalsa/message.c
@@ -143,10 +143,7 @@ libbalsa_message_finalize(GObject * object)
     libbalsa_message_headers_destroy(message->headers);
     message->headers = NULL;
 
-    if (message->sender) {
-       g_object_unref(message->sender);
-       message->sender = NULL;
-    }
+    g_clear_object(&message->sender);
 
 #if MESSAGE_COPY_CONTENT
     g_free(message->subj);
@@ -174,15 +171,10 @@ libbalsa_message_finalize(GObject * object)
     libbalsa_message_body_free(message->body_list);
     message->body_list = NULL;
 
-    if (message->mime_msg) {
-       g_object_unref(message->mime_msg);
-       message->mime_msg = NULL;
-    }
+    g_clear_object(&message->mime_msg);
 
 #ifdef HAVE_GPGME
-    if (message->ident != NULL) {
-       g_object_unref(message->ident);
-    }
+    g_clear_object(&message->ident);
 #endif
 
     if (message->tempdir) {
@@ -217,40 +209,13 @@ libbalsa_message_headers_destroy(LibBalsaMessageHeaders * headers)
     g_free(headers->subject);
     headers->subject = NULL;
 
-    if (headers->from) {
-       g_object_unref(headers->from);
-       headers->from = NULL;
-    }
-
-    if (headers->to_list) {
-       g_object_unref(headers->to_list);
-       headers->to_list = NULL;
-    }
-
-    if (headers->content_type) {
-       g_object_unref(headers->content_type);
-       headers->content_type = NULL;
-    }
-
-    if (headers->cc_list) {
-       g_object_unref(headers->cc_list);
-       headers->cc_list = NULL;
-    }
-
-    if (headers->bcc_list) {
-       g_object_unref(headers->bcc_list);
-       headers->bcc_list = NULL;
-    }
-
-    if (headers->reply_to) {
-       g_object_unref(headers->reply_to);
-       headers->reply_to = NULL;
-    }
-
-    if(headers->dispnotify_to) {
-       g_object_unref(headers->dispnotify_to);
-       headers->dispnotify_to = NULL;
-    }
+    g_clear_object(&headers->from);
+    g_clear_object(&headers->to_list);
+    g_clear_object(&headers->content_type);
+    g_clear_object(&headers->cc_list);
+    g_clear_object(&headers->bcc_list);
+    g_clear_object(&headers->reply_to);
+    g_clear_object(&headers->dispnotify_to);
 
     lb_message_headers_extra_destroy(headers);
 
diff --git a/libbalsa/rfc3156.c b/libbalsa/rfc3156.c
index 8f901b3..eff9c9d 100644
--- a/libbalsa/rfc3156.c
+++ b/libbalsa/rfc3156.c
@@ -516,12 +516,6 @@ libbalsa_rfc2440_verify(GMimePart * part, GMimeGpgmeSigstat ** sig_info)
     /* paranoia checks */
     g_return_val_if_fail(part != NULL, FALSE);
 
-    /* free any old signature */
-    if (sig_info && *sig_info) {
-       g_object_unref(*sig_info);
-       *sig_info = NULL;
-    }
-
     /* check if gpg is currently available */
     if (gpg_updates_trustdb())
        return GPG_ERR_TRY_AGAIN;
@@ -544,12 +538,13 @@ libbalsa_rfc2440_verify(GMimePart * part, GMimeGpgmeSigstat ** sig_info)
        retval = result->status;
 
     /* return the signature info if requested */
-    if (result) {
-       if (sig_info)
-           *sig_info = result;
-       else
-           g_object_unref(G_OBJECT(result));
+    if (sig_info != NULL) {
+        g_set_object(sig_info, result);
     }
+    if (result != NULL) {
+        g_object_unref(result);
+    }
+
     return retval;
 }
 
@@ -571,9 +566,8 @@ libbalsa_rfc2440_decrypt(GMimePart * part, GMimeGpgmeSigstat ** sig_info,
     g_return_val_if_fail(part != NULL, FALSE);
 
     /* free any old signature */
-    if (sig_info && *sig_info) {
-       g_object_unref(*sig_info);
-       *sig_info = NULL;
+    if (sig_info != NULL) {
+        g_clear_object(sig_info);
     }
 
     /* check if gpg is currently available */
diff --git a/libbalsa/rfc6350.c b/libbalsa/rfc6350.c
index ae26ac4..81574d9 100644
--- a/libbalsa/rfc6350.c
+++ b/libbalsa/rfc6350.c
@@ -86,8 +86,7 @@ rfc6350_parse_from_stream(GDataInputStream *stream,
 
                                /* evaluate unfolded line, drop address on error */
                                if (!rfc6350_eval_line(line, result, error)) {
-                                       g_object_unref(result);
-                                       result = NULL;
+                                       g_clear_object(&result);
                                }
 
                                /* process next line */
@@ -98,16 +97,14 @@ rfc6350_parse_from_stream(GDataInputStream *stream,
 
                if (!parse_done) {
                        g_set_error(error, RFC6350_ERROR_QUARK, RFC6350_ERROR_END, _("malformed card, 
END:VCARD missing"));
-                       g_object_unref(result);
-                       result = NULL;
+                        g_clear_object(&result);
                }
        }
 
        /* ignore items without an Email address, fill empty full name if necessary */
        if (result != NULL) {
                if (result->address_list == NULL) {
-                       g_object_unref(result);
-                       result = NULL;
+                        g_clear_object(&result);
                } else if (result->full_name == NULL) {
                        result->full_name = g_strdup(_("No-Name"));
                }
diff --git a/libinit_balsa/assistant_page_user.c b/libinit_balsa/assistant_page_user.c
index 7c0a1ca..e363330 100644
--- a/libinit_balsa/assistant_page_user.c
+++ b/libinit_balsa/assistant_page_user.c
@@ -302,6 +302,7 @@ balsa_druid_page_user_next(GtkAssistant * druid, GtkWidget * page,
     
     ia = internet_address_mailbox_new (mailbox, gtk_entry_get_text(GTK_ENTRY(user->email)));
     libbalsa_identity_set_address (ident, ia);
+    g_object_unref(ia);
 
     /* outgoing mail */
     if (balsa_app.smtp_servers == NULL) {
diff --git a/libnetclient/net-client-pop.c b/libnetclient/net-client-pop.c
index 6e032ff..9d0a512 100644
--- a/libnetclient/net-client-pop.c
+++ b/libnetclient/net-client-pop.c
@@ -79,8 +79,7 @@ net_client_pop_new(const gchar *host, guint16 port, NetClientCryptMode crypt_mod
        client = NET_CLIENT_POP(g_object_new(NET_CLIENT_POP_TYPE, NULL));
        if (client != NULL) {
                if (!net_client_configure(NET_CLIENT(client), host, port, MAX_POP_LINE_LEN, NULL)) {
-                       g_object_unref(G_OBJECT(client));
-                       client = NULL;
+                       g_clear_object(&client);
                } else {
                        client->priv->crypt_mode = crypt_mode;
                        client->priv->use_pipelining = use_pipelining;
diff --git a/libnetclient/net-client.c b/libnetclient/net-client.c
index 4dab4ec..78f2455 100644
--- a/libnetclient/net-client.c
+++ b/libnetclient/net-client.c
@@ -53,14 +53,14 @@ net_client_new(const gchar *host_and_port, guint16 default_port, gsize max_line_
        client = NET_CLIENT(g_object_new(NET_CLIENT_TYPE, NULL));
 
        if (client->priv->sock == NULL) {
-               g_object_unref(G_OBJECT(client));
-               client = NULL;
-       } else {
-               client->priv->host_and_port = g_strdup(host_and_port);
-               client->priv->default_port = default_port;
-               client->priv->max_line_len = max_line_len;
+               g_object_unref(client);
+               return NULL;
        }
 
+       client->priv->host_and_port = g_strdup(host_and_port);
+       client->priv->default_port = default_port;
+       client->priv->max_line_len = max_line_len;
+
        return client;
 }
 
@@ -288,10 +288,7 @@ net_client_set_cert_from_pem(NetClient *client, const gchar *pem_data, GError **
        g_return_val_if_fail(NET_IS_CLIENT(client) && (pem_data != NULL), FALSE);
 
        /* always free any existing certificate */
-       if (client->priv->certificate != NULL) {
-               g_object_unref(G_OBJECT(client->priv->certificate));
-               client->priv->certificate = NULL;
-       }
+       g_clear_object(&client->priv->certificate);
 
        /* load the certificate */
        res = gnutls_x509_crt_init(&cert);
@@ -483,26 +480,12 @@ net_client_finalise(GObject *object)
        const GObjectClass *parent_class = G_OBJECT_CLASS(net_client_parent_class);
 
        /* note: we must unref the GDataInputStream, but *not* the GOutputStream! */
-       if (client->priv->istream != NULL) {
-               g_object_unref(G_OBJECT(client->priv->istream));
-               client->priv->istream = NULL;
-       }
-       if (client->priv->tls_conn != NULL) {
-               g_object_unref(G_OBJECT(client->priv->tls_conn));
-               client->priv->tls_conn = NULL;
-       }
-       if (client->priv->plain_conn != NULL) {
-               g_object_unref(G_OBJECT(client->priv->plain_conn));
-               client->priv->plain_conn = NULL;
-       }
-       if (client->priv->sock != NULL) {
-               g_object_unref(G_OBJECT(client->priv->sock));
-               client->priv->sock = NULL;
-       }
-       if (client->priv->certificate != NULL) {
-               g_object_unref(G_OBJECT(client->priv->certificate));
-               client->priv->certificate = NULL;
-       }
+        g_clear_object(&client->priv->istream);
+
+        g_clear_object(&client->priv->tls_conn);
+        g_clear_object(&client->priv->plain_conn);
+        g_clear_object(&client->priv->sock);
+        g_clear_object(&client->priv->certificate);
        g_debug("finalised connection to %s", client->priv->host_and_port);
        g_free(client->priv->host_and_port);
        (*parent_class->finalize)(object);
diff --git a/src/balsa-icons.c b/src/balsa-icons.c
index 4dbeab1..018a290 100644
--- a/src/balsa-icons.c
+++ b/src/balsa-icons.c
@@ -238,6 +238,7 @@ balsa_register_pixbufs(GtkWidget * widget)
             g_clear_error(&err);
         } else {
             icons[i].set_icon(pixbuf);
+            g_object_unref(pixbuf);
         }
     }
 }
diff --git a/src/balsa-index.c b/src/balsa-index.c
index 393fd56..394effc 100644
--- a/src/balsa-index.c
+++ b/src/balsa-index.c
@@ -244,10 +244,7 @@ bndx_destroy(GObject * obj)
         index->search_iter = NULL;
     }
 
-    if (index->popup_menu) {
-        g_object_unref(index->popup_menu);
-        index->popup_menu = NULL;
-    }
+    g_clear_object(&index->popup_menu);
 
     g_free(index->filter_string); index->filter_string = NULL;
 
diff --git a/src/balsa-message.c b/src/balsa-message.c
index 5ad70a2..e833ba8 100644
--- a/src/balsa-message.c
+++ b/src/balsa-message.c
@@ -153,7 +153,7 @@ static void mdn_dialog_response(GtkWidget * dialog, gint response,
 
 static void balsa_part_info_init(GObject *object, gpointer data);
 static BalsaPartInfo* balsa_part_info_new(LibBalsaMessageBody* body);
-static void balsa_part_info_free(GObject * object);
+static void balsa_part_info_finalize(GObject * object);
 
 
 #ifdef HAVE_GPGME
@@ -171,7 +171,7 @@ balsa_part_info_class_init(BalsaPartInfoClass *klass)
 {
     GObjectClass *object_class = G_OBJECT_CLASS(klass);
 
-    object_class->finalize = balsa_part_info_free;
+    object_class->finalize = balsa_part_info_finalize;
 }
 
 static GType
@@ -812,21 +812,11 @@ balsa_message_destroy(GObject * object)
     g_list_free(bm->save_all_list);
     bm->save_all_list = NULL;
 
-    if (bm->save_all_popup) {
-        g_object_unref(bm->save_all_popup);
-       bm->save_all_popup = NULL;
-    }
-
-    if (bm->parts_popup) {
-       g_object_unref(bm->parts_popup);
-       bm->parts_popup = NULL;
-    }
+    g_clear_object(&bm->save_all_popup);
+    g_clear_object(&bm->parts_popup);
 
 #ifdef HAVE_HTML_WIDGET
-    if (bm->html_find_info) {
-        g_free(bm->html_find_info);
-        bm->html_find_info = NULL;
-    }
+    g_clear_object(&bm->html_find_info);
 #endif                          /* HAVE_HTML_WIDGET */
 
     if (G_OBJECT_CLASS(parent_class)->dispose)
@@ -1675,7 +1665,7 @@ balsa_part_info_new(LibBalsaMessageBody* body)
 }
 
 static void
-balsa_part_info_free(GObject * object)
+balsa_part_info_finalize(GObject * object)
 {
     BalsaPartInfo * info;
     GObjectClass *part_info_parent_class;
@@ -1684,13 +1674,8 @@ balsa_part_info_free(GObject * object)
     g_return_if_fail(IS_BALSA_PART_INFO(object));
     info = BALSA_PART_INFO(object);
 
-    if (info->mime_widget) {
-       g_object_unref(G_OBJECT(info->mime_widget));
-       info->mime_widget = NULL;
-    }
-    if (info->popup_menu)
-        g_object_unref(info->popup_menu);
-
+    g_clear_object(&info->mime_widget);
+    g_clear_object(&info->popup_menu);
     gtk_tree_path_free(info->path);
 
     part_info_parent_class = g_type_class_peek_parent(G_OBJECT_GET_CLASS(object));
@@ -2909,10 +2894,7 @@ libbalsa_msg_try_mp_signed(LibBalsaMessage * message, LibBalsaMessageBody *body,
     }
 
     /* force creating the protection info */
-    if (body->parts->next->sig_info) {
-       g_object_unref(body->parts->next->sig_info);
-       body->parts->next->sig_info = NULL;
-    }
+    g_clear_object(&body->parts->next->sig_info);
     if (!libbalsa_body_check_signature(body,
                                       signres & LIBBALSA_PROTECT_RFC3156 ?
                                       GPGME_PROTOCOL_OpenPGP : GPGME_PROTOCOL_CMS))
diff --git a/src/balsa-mime-widget.c b/src/balsa-mime-widget.c
index 2fcfc28..7ca7172 100644
--- a/src/balsa-mime-widget.c
+++ b/src/balsa-mime-widget.c
@@ -39,6 +39,7 @@
 /* object related functions */
 static void balsa_mime_widget_init (GTypeInstance *instance, gpointer g_class);
 static void balsa_mime_widget_class_init(BalsaMimeWidgetClass * klass);
+static void balsa_mime_widget_finalize(GObject * object);
 
 
 /* fall-back widget (unknown/unsupported mime type) */
@@ -97,7 +98,7 @@ balsa_mime_widget_class_init(BalsaMimeWidgetClass * klass)
     GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
     parent_class = g_type_class_ref(G_TYPE_OBJECT);
-    object_class->finalize = balsa_mime_widget_destroy;
+    object_class->finalize = balsa_mime_widget_finalize;
 }
 
 
@@ -200,18 +201,15 @@ balsa_mime_widget_new(BalsaMessage * bm, LibBalsaMessageBody * mime_body, gpoint
 }
 
 
-void
-balsa_mime_widget_destroy(GObject * object)
+static void
+balsa_mime_widget_finalize(GObject * object)
 {
     BalsaMimeWidget * mime_widget = BALSA_MIME_WIDGET(object);
 
     if (mime_widget->container && mime_widget->container != mime_widget->widget)
        gtk_widget_destroy(mime_widget->container);
     mime_widget->container = NULL;
-    if (mime_widget->widget) {
-        g_object_unref(mime_widget->widget);
-        mime_widget->widget = NULL;
-    }
+    g_clear_object(&mime_widget->widget);
 
     G_OBJECT_CLASS(parent_class)->finalize(object);
 }
diff --git a/src/balsa-mime-widget.h b/src/balsa-mime-widget.h
index ac070dd..e08b151 100644
--- a/src/balsa-mime-widget.h
+++ b/src/balsa-mime-widget.h
@@ -68,7 +68,6 @@ GType balsa_mime_widget_get_type (void);
 BalsaMimeWidget *balsa_mime_widget_new(BalsaMessage * bm,
                                       LibBalsaMessageBody * mime_body,
                                       gpointer data);
-void balsa_mime_widget_destroy(GObject * object);
 void balsa_mime_widget_schedule_resize(GtkWidget * widget);
 
 
diff --git a/src/balsa-print-object-default.c b/src/balsa-print-object-default.c
index 4facc5f..d3d9f50 100644
--- a/src/balsa-print-object-default.c
+++ b/src/balsa-print-object-default.c
@@ -97,8 +97,7 @@ balsa_print_object_default_destroy(GObject * self)
 {
     BalsaPrintObjectDefault *po = BALSA_PRINT_OBJECT_DEFAULT(self);
 
-    if (po->pixbuf)
-       g_object_unref(po->pixbuf);
+    g_clear_object(&po->pixbuf);
     g_free(po->description);
 
     G_OBJECT_CLASS(parent_class)->finalize(self);
diff --git a/src/balsa-print-object-image.c b/src/balsa-print-object-image.c
index 090bbf3..b4f75b8 100644
--- a/src/balsa-print-object-image.c
+++ b/src/balsa-print-object-image.c
@@ -93,8 +93,7 @@ balsa_print_object_image_destroy(GObject * self)
 {
     BalsaPrintObjectImage *po = BALSA_PRINT_OBJECT_IMAGE(self);
 
-    if (po->pixbuf)
-       g_object_unref(po->pixbuf);
+    g_clear_object(&po->pixbuf);
 
     G_OBJECT_CLASS(parent_class)->finalize(self);
 }
diff --git a/src/mailbox-conf.c b/src/mailbox-conf.c
index 999b6b1..74362f2 100644
--- a/src/mailbox-conf.c
+++ b/src/mailbox-conf.c
@@ -1089,8 +1089,7 @@ mailbox_conf_add(MailboxConfWindow * mcw)
 
         if (libbalsa_mailbox_local_set_path(ml, path, TRUE) != 0) {
             g_free(path);
-           g_object_unref(G_OBJECT(mcw->mailbox));
-           mcw->mailbox = NULL;
+            g_clear_object(&mcw->mailbox);
            return;
        }
 
diff --git a/src/print-gtk.c b/src/print-gtk.c
index 05ee794..400e933 100644
--- a/src/print-gtk.c
+++ b/src/print-gtk.c
@@ -690,10 +690,8 @@ message_print_page_setup(GtkWindow * parent)
        gtk_print_run_page_setup_dialog(parent, balsa_app.page_setup,
                                        balsa_app.print_settings);
 
-    if (balsa_app.page_setup)
-       g_object_unref(G_OBJECT(balsa_app.page_setup));
-
-    balsa_app.page_setup = new_page_setup;
+    g_set_object(&balsa_app.page_setup, new_page_setup);
+    g_object_unref(new_page_setup);
 }
 
 
@@ -739,10 +737,7 @@ message_print(LibBalsaMessage * msg, GtkWindow * parent)
                                parent, &err);
 
     if (res == GTK_PRINT_OPERATION_RESULT_APPLY) {
-       if (balsa_app.print_settings != NULL)
-           g_object_unref(balsa_app.print_settings);
-       balsa_app.print_settings =
-           g_object_ref(gtk_print_operation_get_print_settings(print));
+        g_set_object(&balsa_app.print_settings, gtk_print_operation_get_print_settings(print));
     } else if (res == GTK_PRINT_OPERATION_RESULT_ERROR)
        balsa_information(LIBBALSA_INFORMATION_ERROR,
                          _("Error printing message: %s"), err->message);
diff --git a/src/toolbar-factory.c b/src/toolbar-factory.c
index a61fe36..4c815b9 100644
--- a/src/toolbar-factory.c
+++ b/src/toolbar-factory.c
@@ -70,10 +70,8 @@ balsa_toolbar_model_finalize(GObject * object)
         g_hash_table_destroy(model->legal);
         model->legal = NULL;
     }
-    if (model->settings) {
-        g_object_unref(model->settings);
-        model->settings = NULL;
-    }
+    g_clear_object(&model->settings);
+
     G_OBJECT_CLASS(parent_class)->finalize(object);
 }
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]