diff --git a/src/calls-account.c b/src/calls-account.c index 414b714..5a61522 100644 --- a/src/calls-account.c +++ b/src/calls-account.c @@ -23,8 +23,13 @@ */ #include "calls-account.h" +#include "calls-message-source.h" +#include "calls-log.h" + #include "enum-types.h" +#include + /** * SECTION:account * @short_description: An interface for online accounts @@ -42,6 +47,48 @@ enum { static guint signals [SIGNAL_LAST_SIGNAL]; +static gboolean +calls_account_state_change_is_important (CallsAccountState new_state) +{ + if (calls_log_get_verbosity () >= 3) + return TRUE; + + switch (new_state) { + case CALLS_ACCOUNT_STATE_UNKNOWN: + case CALLS_ACCOUNT_STATE_INITIALIZING: + case CALLS_ACCOUNT_STATE_DEINITIALIZING: + case CALLS_ACCOUNT_STATE_CONNECTING: + case CALLS_ACCOUNT_STATE_DISCONNECTING: + return FALSE; + + case CALLS_ACCOUNT_STATE_ERROR: + case CALLS_ACCOUNT_STATE_OFFLINE: + case CALLS_ACCOUNT_STATE_ONLINE: + return TRUE; + + default: + return FALSE; + } +} + + +static gboolean +calls_account_state_reason_is_error (CallsAccountStateReason reason) +{ + switch (reason) { + case CALLS_ACCOUNT_STATE_REASON_NO_CREDENTIALS: + case CALLS_ACCOUNT_STATE_REASON_CONNECTION_TIMEOUT: + case CALLS_ACCOUNT_STATE_REASON_CONNECTION_DNS_ERROR: + case CALLS_ACCOUNT_STATE_REASON_AUTHENTICATION_FAILURE: + case CALLS_ACCOUNT_STATE_REASON_INTERNAL_ERROR: + return TRUE; + + default: + return FALSE; + } +} + + static void calls_account_default_init (CallsAccountInterface *iface) { @@ -141,3 +188,136 @@ calls_account_get_address (CallsAccount *self) return iface->get_address (self); } + +/** + * calls_account_state_to_string: + * @state: A #CallsAccountState + * + * Returns: (transfer none): A human readable description of the account state + */ +const char * +calls_account_state_to_string (CallsAccountState state) +{ + switch (state) { + case CALLS_ACCOUNT_STATE_UNKNOWN: + return _("Default (uninitialized) state"); + + case CALLS_ACCOUNT_STATE_INITIALIZING: + return _("Initializing account…"); + + case CALLS_ACCOUNT_STATE_DEINITIALIZING: + return _("Uninitializing account…"); + + case CALLS_ACCOUNT_STATE_CONNECTING: + return _("Connecting to server…"); + + case CALLS_ACCOUNT_STATE_ONLINE: + return _("Account is online"); + + case CALLS_ACCOUNT_STATE_DISCONNECTING: + return _("Disconnecting from server…"); + + case CALLS_ACCOUNT_STATE_OFFLINE: + return _("Account is offline"); + + case CALLS_ACCOUNT_STATE_ERROR: + return _("Account encountered an error"); + + default: + return NULL; + } +} + +/** + * calls_account_state_reason_to_string: + * @reason: A #CallsAccountStateReason + * + * Returns: (transfer none): A human readable description for why an account state changed + */ +const char * +calls_account_state_reason_to_string (CallsAccountStateReason reason) +{ + switch (reason) { + case CALLS_ACCOUNT_STATE_REASON_UNKNOWN: + return _("No reason given"); + + case CALLS_ACCOUNT_STATE_REASON_INITIALIZATION_STARTED: + return _("Initialization started"); + + case CALLS_ACCOUNT_STATE_REASON_INITIALIZED: + return _("Initialization complete"); + + case CALLS_ACCOUNT_STATE_REASON_DEINITIALIZATION_STARTED: + return _("Uninitialization started"); + + case CALLS_ACCOUNT_STATE_REASON_DEINITIALIZED: + return _("Uninitialization complete"); + + case CALLS_ACCOUNT_STATE_REASON_NO_CREDENTIALS: + return _("No credentials set"); + + case CALLS_ACCOUNT_STATE_REASON_CONNECT_STARTED: + return _("Starting to connect"); + + case CALLS_ACCOUNT_STATE_REASON_CONNECTION_TIMEOUT: + return _("Connection timed out"); + + case CALLS_ACCOUNT_STATE_REASON_CONNECTION_DNS_ERROR: + return _("Domain name could not be resolved"); + + case CALLS_ACCOUNT_STATE_REASON_AUTHENTICATION_FAILURE: + return _("Server did not accept username or password"); + + case CALLS_ACCOUNT_STATE_REASON_CONNECTED: + return _("Connecting complete"); + + case CALLS_ACCOUNT_STATE_REASON_DISCONNECT_STARTED: + return _("Starting to disconnect"); + + case CALLS_ACCOUNT_STATE_REASON_DISCONNECTED: + return _("Disconnecting complete"); + + case CALLS_ACCOUNT_STATE_REASON_INTERNAL_ERROR: + return _("Internal error occurred"); + + default: + return NULL; + } +} + +/** + * calls_account_emit_message_for_state_change: + * @account: A #CallsAccount + * @new_state: The new #CallsAccountState + * @reason: The #CallsAccountStateReason for the state change + * + * Returns: Emits a human readable message for a state change + */ +void +calls_account_emit_message_for_state_change (CallsAccount *account, + CallsAccountState new_state, + CallsAccountStateReason reason) +{ + g_autofree char *message = NULL; + gboolean state_is_important = FALSE; + gboolean reason_is_error = FALSE; + + g_return_if_fail (CALLS_IS_ACCOUNT (account)); + + state_is_important = calls_account_state_change_is_important (new_state); + reason_is_error = calls_account_state_reason_is_error (reason); + + if (!state_is_important && !reason_is_error) + return; + + if (reason_is_error || calls_log_get_verbosity () >= 3) + message = g_strdup_printf ("%s: %s", + calls_account_state_to_string (new_state), + calls_account_state_reason_to_string (reason)); + else + message = g_strdup (calls_account_state_to_string (new_state)); + + calls_message_source_emit_message (CALLS_MESSAGE_SOURCE (account), + message, + reason_is_error ? GTK_MESSAGE_ERROR : GTK_MESSAGE_INFO); +} diff --git a/src/calls-account.h b/src/calls-account.h index 8f56660..f405604 100644 --- a/src/calls-account.h +++ b/src/calls-account.h @@ -101,11 +101,14 @@ struct _CallsAccountInterface }; -void calls_account_go_online (CallsAccount *self, - gboolean online); -const char *calls_account_get_address (CallsAccount *self); -CallsAccountState calls_account_get_state (CallsAccount *self); -const char *calls_account_state_to_string (CallsAccountState *state); -const char *calls_account_state_reason_to_string (CallsAccountStateReason *reason); +void calls_account_go_online (CallsAccount *self, + gboolean online); +const char *calls_account_get_address (CallsAccount *self); +CallsAccountState calls_account_get_state (CallsAccount *self); +const char *calls_account_state_to_string (CallsAccountState state); +const char *calls_account_state_reason_to_string (CallsAccountStateReason reason); +void calls_account_emit_message_for_state_change (CallsAccount *account, + CallsAccountState new_state, + CallsAccountStateReason reason); G_END_DECLS