/soc/2015/igor.gajowiak/chatlog: 8eafde43f892: Moved common code...

Igor Gajowiak igor.gajowiak at gmail.com
Fri Jul 24 02:54:56 EDT 2015


Changeset: 8eafde43f8923b00280fc33dc61168225dadafdc
Author:	 Igor Gajowiak <igor.gajowiak at gmail.com>
Date:	 2015-07-24 08:54 +0200
Branch:	 default
URL: https://hg.pidgin.im/soc/2015/igor.gajowiak/chatlog/rev/8eafde43f892

Description:

Moved common code to a macro.

diffstat:

 libpurple/genericlog.c |  179 +++++++++++++-----------------------------------
 1 files changed, 51 insertions(+), 128 deletions(-)

diffs (210 lines):

diff --git a/libpurple/genericlog.c b/libpurple/genericlog.c
--- a/libpurple/genericlog.c
+++ b/libpurple/genericlog.c
@@ -184,155 +184,78 @@ purple_genericlog_set_active_log(PurpleG
 	return TRUE;
 }
 
+#ifdef DEFINE_GENERICLOG_FUNC_WITH_RETURN
+# error "DEFINE_GENERICLOG_FUNC_WITH_RETURN is already defined"
+#endif
+#define DEFINE_GENERICLOG_FUNC_WITH_RETURN(func,                  \
+	not_supported_err_msg, backend_fail_err_msg, ...)             \
+	if (!purple_genericlog_inuse) {                               \
+		if (!error) return FALSE;                                 \
+																  \
+		*error = g_error_new(purple_genericlog_error_domain(),    \
+			PURPLE_GENERICLOG_ERROR_NOLOG, "No active log");      \
+		return FALSE;                                             \
+	}                                                             \
+	PurpleGenericLogClass *klass =                                \
+																  \
+		PURPLE_GENERICLOG_GET_CLASS(purple_genericlog_inuse);     \
+																  \
+	if (!klass->func) {                                           \
+		if (!error) return FALSE;                                 \
+																  \
+		*error = g_error_new(purple_genericlog_error_domain(),    \
+			PURPLE_GENERICLOG_ERROR_OPERATIONNOTSUPPORTED,        \
+			not_supported_err_msg,                                \
+			purple_genericlog_get_id(purple_genericlog_inuse));   \
+		return FALSE;                                             \
+	}                                                             \
+																  \
+	if (!klass->func(__VA_ARGS__)) {                              \
+		if (!error) return FALSE;                                 \
+																  \
+		*error = g_error_new(purple_genericlog_error_domain(),    \
+			PURPLE_GENERICLOG_ERROR_BACKENDFAIL,                  \
+			backend_fail_err_msg,                                 \
+			purple_genericlog_get_id(purple_genericlog_inuse));   \
+		return FALSE;                                             \
+	}                                                             \
+																  \
+	return TRUE;
+
 gboolean
 purple_genericlog_logim(PurpleAccount *account, PurpleMessage *msg,
 	GError **error)
 {
-	if (!purple_genericlog_inuse) {
-		if (!error) return FALSE;
-
-		*error = g_error_new(purple_genericlog_error_domain(),
-			PURPLE_GENERICLOG_ERROR_NOLOG, "No active log");
-		return FALSE;
-	}
-
-	PurpleGenericLogClass *klass =
-		PURPLE_GENERICLOG_GET_CLASS(purple_genericlog_inuse);
-
-	if (!klass->log_im) {
-		if (!error) return FALSE;
-
-		*error = g_error_new(purple_genericlog_error_domain(),
-			PURPLE_GENERICLOG_ERROR_OPERATIONNOTSUPPORTED,
-			"%s does not support IM logging",
-			purple_genericlog_get_id(purple_genericlog_inuse));
-		return FALSE;
-	}
-
-	if (!klass->log_im(account, msg)) {
-		if (!error) return FALSE;
-
-		*error = g_error_new(purple_genericlog_error_domain(),
-			PURPLE_GENERICLOG_ERROR_BACKENDFAIL,
-			"Failed to log IM in %s",
-			purple_genericlog_get_id(purple_genericlog_inuse));
-		return FALSE;
-	}
-
-	return TRUE;
+	DEFINE_GENERICLOG_FUNC_WITH_RETURN(log_im, "%s does not support IM logging",
+		"Failed to log IM in %s", account, msg);
 }
 
 gboolean
 purple_genericlog_mark_as_seen(PurpleMessage *msg, GError **error)
 {
-	if (!purple_genericlog_inuse) {
-		if (!error) return FALSE;
-
-		*error = g_error_new(purple_genericlog_error_domain(),
-			PURPLE_GENERICLOG_ERROR_NOLOG, "No active log");
-		return FALSE;
-	}
-
-	PurpleGenericLogClass *klass =
-		PURPLE_GENERICLOG_GET_CLASS(purple_genericlog_inuse);
-
-	if (!klass->mark_as_seen) {
-		if (!error) return FALSE;
-
-		*error = g_error_new(purple_genericlog_error_domain(),
-			PURPLE_GENERICLOG_ERROR_OPERATIONNOTSUPPORTED,
-			"%s does not support marking message as seen",
-			purple_genericlog_get_id(purple_genericlog_inuse));
-		return FALSE;
-	}
-
-	if (!klass->mark_as_seen(msg)) {
-		if (!error) return FALSE;
-
-		*error = g_error_new(purple_genericlog_error_domain(),
-			PURPLE_GENERICLOG_ERROR_BACKENDFAIL,
-			"Marking message as seen in %s failed",
-			purple_genericlog_get_id(purple_genericlog_inuse));
-		return FALSE;
-	}
-
-	return TRUE;
+	DEFINE_GENERICLOG_FUNC_WITH_RETURN(mark_as_seen,
+		"%s does not support marking message as seen",
+		"Marking message as seen in %s failed", msg);
 }
 
 gboolean
 purple_genericlog_get_unseen_msgs(PurpleAccount *account, GList **result,
 	GError **error)
 {
-	if (!purple_genericlog_inuse) {
-		if (!error) return FALSE;
-
-		*error = g_error_new(purple_genericlog_error_domain(),
-			PURPLE_GENERICLOG_ERROR_NOLOG, "No active log");
-		return FALSE;
-	}
-
-	PurpleGenericLogClass *klass =
-		PURPLE_GENERICLOG_GET_CLASS(purple_genericlog_inuse);
-
-	if (!klass->get_unseen_msgs) {
-		if (!error) return FALSE;
-
-		*error = g_error_new(purple_genericlog_error_domain(),
-			PURPLE_GENERICLOG_ERROR_OPERATIONNOTSUPPORTED,
-			"%s does not support getting unseen messages",
-			purple_genericlog_get_id(purple_genericlog_inuse));
-		return FALSE;
-	}
-
-	if (!klass->get_unseen_msgs(account, result)) {
-		if (!error) return FALSE;
-
-		*error = g_error_new(purple_genericlog_error_domain(),
-			PURPLE_GENERICLOG_ERROR_BACKENDFAIL,
-			"Getting unseen messages from %s failed",
-			purple_genericlog_get_id(purple_genericlog_inuse));
-		return FALSE;
-	}
-
-	return TRUE;
+	DEFINE_GENERICLOG_FUNC_WITH_RETURN(get_unseen_msgs,
+		"%s does not support getting unseen messages",
+		"Getting unseen messages from %s failed", account, result);
 }
 
 gboolean
 purple_genericlog_get_all_buddies(GList** result, GError** error)
 {
-	if (!purple_genericlog_inuse) {
-		if (!error) return FALSE;
+	DEFINE_GENERICLOG_FUNC_WITH_RETURN(get_all_buddies,
+		"%s does not support getting buddies",
+		"Getting buddies from %s failed", result);
+}
 
-		*error = g_error_new(purple_genericlog_error_domain(),
-			PURPLE_GENERICLOG_ERROR_NOLOG, "No active log");
-		return FALSE;
-	}
-
-	PurpleGenericLogClass *klass =
-		PURPLE_GENERICLOG_GET_CLASS(purple_genericlog_inuse);
-
-	if (!klass->get_all_buddies) {
-		if (!error) return FALSE;
-
-		*error = g_error_new(purple_genericlog_error_domain(),
-			PURPLE_GENERICLOG_ERROR_OPERATIONNOTSUPPORTED,
-			"%s does not support getting buddies from log",
-			purple_genericlog_get_id(purple_genericlog_inuse));
-		return FALSE;
-	}
-
-	if (!klass->get_all_buddies(result)) {
-		if (!error) return FALSE;
-
-		*error = g_error_new(purple_genericlog_error_domain(),
-			PURPLE_GENERICLOG_ERROR_BACKENDFAIL,
-			"Getting buddies from %s failed",
-			purple_genericlog_get_id(purple_genericlog_inuse));
-		return FALSE;
-	}
-
-	return TRUE;
-}
+#undef DEFINE_GENERICLOG_FUNC_WITH_RETURN
 
 /**************************************************************************/
 /* Error Codes                                                            */



More information about the Commits mailing list