pep_tune.c
changeset 31 e404cd1c7077
parent 30 a66ed0454ca8
child 35 a77a8e7ab8ae
equal deleted inserted replaced
30:a66ed0454ca8 31:e404cd1c7077
    28 #include <mcabber/logprint.h>
    28 #include <mcabber/logprint.h>
    29 #include <mcabber/hooks.h>
    29 #include <mcabber/hooks.h>
    30 #include <mcabber/modules.h>
    30 #include <mcabber/modules.h>
    31 
    31 
    32 #include "pep.h"
    32 #include "pep.h"
    33 #include "tune.h"
    33 #include "pep_tune.h"
    34 
    34 
    35 #include "config.h"
    35 #include "config.h"
    36 
    36 
    37 //
    37 //
    38 //  module description
    38 //  module description
    39 //
    39 //
    40 
    40 
    41 void pep_tune_init   (void);
    41 void pep_tune_init   (void);
    42 void pep_tune_uninit (void);
    42 void pep_tune_uninit (void);
    43 
    43 
    44 #define DESCRIPTION ( \
    44 #define DESCRIPTION ( PEP_TUNE_DESCRIPTION )
    45 	"PEP tune event handler\n" \
       
    46 	"Optios: " OPT_TUNE_PAUSE_IS_STOP ", " OPT_TUNE_INTERVAL )
       
    47 
    45 
    48 static const gchar *deps[] = { "pep", NULL };
    46 static const gchar *deps[] = { "pep", NULL };
    49 
    47 
    50 static module_info_t info_tune_dev = {
    48 static module_info_t info_tune_dev = {
    51 	.branch      = "dev",
    49 	.branch      = "dev",
   217 	}
   215 	}
   218 
   216 
   219 	lm_message_unref (request);
   217 	lm_message_unref (request);
   220 }
   218 }
   221 
   219 
   222 void tune_publish (const tune_pair_t *pairs)
   220 gboolean pep_tune_request ( const gchar *to, GError **err )
   223 {
       
   224 	gboolean           publish    = FALSE;
       
   225 	const tune_pair_t *tag;
       
   226 	tune_pair_t        new_info[] = {
       
   227 		{ "artist", NULL },
       
   228 		{ "length", NULL },
       
   229 		{ "rating", NULL },
       
   230 		{ "source", NULL },
       
   231 		{ "title",  NULL },
       
   232 		{ "track",  NULL },
       
   233 		{ "uri",    NULL },
       
   234 		{ NULL,     NULL },
       
   235 	};
       
   236 
       
   237 	// populate new_info with new values
       
   238 	for ( tag = pairs; tag -> name; ++ tag )
       
   239 		if ( ! g_strcmp0 ( tag -> name, "state" ) ) {
       
   240 			if ( ( ! g_strcmp0 ( tag -> value, "stop" ) ) ||
       
   241 				 ( ( ! g_strcmp0 ( tag -> value, "pause" ) ) &&
       
   242 				   settings_opt_get_int ( OPT_TUNE_PAUSE_IS_STOP ) ) ) {
       
   243 				int i;
       
   244 				for ( i = 0; i <= MAX_NO; ++ i )
       
   245 					new_info [ i ] .value = NULL;
       
   246 				break;
       
   247 			}
       
   248 		} else {
       
   249 			int i;
       
   250 			for ( i = 0; i <= MAX_NO; ++ i )
       
   251 				if ( ! g_strcmp0 ( tag -> name, new_info [ i  ] .name ) )
       
   252 					new_info [ i ] .value = tag -> value;
       
   253 		}
       
   254 
       
   255 	{ // check, if it differ from info
       
   256 		int i;
       
   257 		for (i = 0; i <= MAX_NO; ++i)
       
   258 			if (g_strcmp0 (new_info[i].value, info[i].value)) {
       
   259 				publish = TRUE;
       
   260 				break;
       
   261 			}
       
   262 	}
       
   263 
       
   264 	if (publish) {
       
   265 
       
   266 		{ // copy new values to info
       
   267 			int i;
       
   268 			for (i = 0; i <= MAX_NO; ++i) {
       
   269 				if (info[i].value)
       
   270 					g_free (info[i].value);
       
   271 				info[i].value = g_strdup (new_info[i].value);
       
   272 			}
       
   273 		}
       
   274 
       
   275 		tune_publish_info ();
       
   276 	}
       
   277 }
       
   278 
       
   279 gboolean tune_request ( const gchar *to, GError **err )
       
   280 {
   221 {
   281 	LmMessage *request;
   222 	LmMessage *request;
   282 	LmMessageNode *node;
   223 	LmMessageNode *node;
   283 
   224 
   284 	if (!xmpp_is_online ()) {
   225 	if (!xmpp_is_online ()) {
   365 	return HOOK_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
   306 	return HOOK_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
   366 }
   307 }
   367 
   308 
   368 static guint tune_htoh ( const gchar *htype, hk_arg_t *args, gpointer udata )
   309 static guint tune_htoh ( const gchar *htype, hk_arg_t *args, gpointer udata )
   369 {
   310 {
   370 	tune_publish ( (const tune_pair_t *) args );
   311 	gboolean        publish = FALSE;
       
   312 	gboolean        stop    = FALSE;
       
   313 
       
   314 	{ // check state
       
   315 		const hk_arg_t *arg;
       
   316 		for ( arg = args; arg -> name != NULL; ++ arg ) {
       
   317 			if ( ! g_strcmp0 ( arg -> name, "state" ) ) {
       
   318 				stop = ( ! g_strcmp0 ( arg -> value, "stop" ) ) ||
       
   319 					   ( ( ! g_strcmp0 ( arg -> value, "pause" ) ) && settings_opt_get_int ( OPT_TUNE_PAUSE_IS_STOP ) );
       
   320 				break;
       
   321 			}
       
   322 		}
       
   323 	}
       
   324 
       
   325 	if ( stop ) { // ignore info
       
   326 		int i;
       
   327 		for ( i = 0; i <= MAX_NO; ++ i )
       
   328 			if ( info [ i ] .value != NULL ) {
       
   329 				g_free ( info [ i ] .value );
       
   330 				info [ i ] .value = NULL;
       
   331 				publish = TRUE;
       
   332 			}
       
   333 	} else { // populate info with new values
       
   334 		int i;
       
   335 		for ( i = 0; i <= MAX_NO; ++ i ) {
       
   336 			const hk_arg_t *arg;
       
   337 			for ( arg = args; arg -> name != NULL; ++ arg ) {
       
   338 				if ( ! g_strcmp0 ( arg -> name, info [ i ] .name ) ) {
       
   339 					if ( g_strcmp0 ( arg -> value, info [ i ] .value ) ) {
       
   340 						if ( info [ i ] .value )
       
   341 							g_free ( info [ i ] .value );
       
   342 						info [ i ] .value = g_strdup ( arg -> value );
       
   343 						publish = TRUE;
       
   344 					}
       
   345 					break;
       
   346 				}
       
   347 			}
       
   348 		}
       
   349 	}
       
   350 
       
   351 	if ( publish )
       
   352 		tune_publish_info ();
       
   353 
   371 	return HOOK_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
   354 	return HOOK_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
   372 }
   355 }
   373 
   356 
   374 static gchar *tune_guard (const char *key, const char *new_value)
   357 static gchar *tune_guard (const char *key, const char *new_value)
   375 {
   358 {