@@ -473,6 +473,60 @@ void dc_job_do_DC_JOB_MAYBE_SEND_LOCATIONS(dc_context_t* context, dc_job_t* job)
473473}
474474
475475
476+ void dc_job_do_DC_JOB_MAYBE_SEND_LOC_ENDED (dc_context_t * context , dc_job_t * job )
477+ {
478+ // this function is called when location-streaming _might_ have ended for a chat.
479+ // the function checks, if location-streaming is really ended;
480+ // if so, a device-message is added if not yet done.
481+ uint32_t chat_id = job -> foreign_id ;
482+ time_t locations_send_begin = 0 ;
483+ time_t locations_send_until = 0 ;
484+ sqlite3_stmt * stmt = NULL ;
485+ char * stock_str = NULL ;
486+
487+ stmt = dc_sqlite3_prepare (context -> sql ,
488+ "SELECT locations_send_begin, locations_send_until "
489+ " FROM chats "
490+ " WHERE id=?" );
491+ sqlite3_bind_int (stmt , 1 , chat_id );
492+ if (sqlite3_step (stmt )!= SQLITE_ROW ) {
493+ goto cleanup ;
494+ }
495+ locations_send_begin = sqlite3_column_int64 (stmt , 0 );
496+ locations_send_until = sqlite3_column_int64 (stmt , 1 );
497+ sqlite3_finalize (stmt );
498+ stmt = NULL ;
499+
500+ if (locations_send_begin != 0 && time (NULL )<=locations_send_until ) {
501+ // still streaming -
502+ // may happen as several calls to dc_send_locations_to_chat()
503+ // do not un-schedule pending DC_MAYBE_SEND_LOC_ENDED jobs
504+ goto cleanup ;
505+ }
506+
507+ if (locations_send_begin == 0 && locations_send_until == 0 ) {
508+ // not streaming, device-message already sent
509+ goto cleanup ;
510+ }
511+
512+ // inform the ui that location-streaming has ended
513+ stmt = dc_sqlite3_prepare (context -> sql ,
514+ "UPDATE chats "
515+ " SET locations_send_begin=0, locations_send_until=0 "
516+ " WHERE id=?" );
517+ sqlite3_bind_int (stmt , 1 , chat_id );
518+ sqlite3_step (stmt );
519+
520+ stock_str = dc_stock_system_msg (context , DC_STR_MSGLOCATIONDISABLED , NULL , NULL , 0 );
521+ dc_add_device_msg (context , chat_id , stock_str );
522+ context -> cb (context , DC_EVENT_CHAT_MODIFIED , chat_id , 0 );
523+
524+ cleanup :
525+ sqlite3_finalize (stmt );
526+ free (stock_str );
527+ }
528+
529+
476530/*******************************************************************************
477531 * high-level ui-functions
478532 ******************************************************************************/
@@ -516,8 +570,8 @@ void dc_send_locations_to_chat(dc_context_t* context, uint32_t chat_id,
516570 " SET locations_send_begin=?, "
517571 " locations_send_until=? "
518572 " WHERE id=?" );
519- sqlite3_bind_int64 (stmt , 1 , now );
520- sqlite3_bind_int64 (stmt , 2 , now + seconds );
573+ sqlite3_bind_int64 (stmt , 1 , seconds ? now : 0 );
574+ sqlite3_bind_int64 (stmt , 2 , seconds ? now + seconds : 0 );
521575 sqlite3_bind_int (stmt , 3 , chat_id );
522576 sqlite3_step (stmt );
523577
@@ -527,16 +581,19 @@ void dc_send_locations_to_chat(dc_context_t* context, uint32_t chat_id,
527581 msg = dc_msg_new (context , DC_MSG_TEXT );
528582 msg -> text = dc_stock_system_msg (context , DC_STR_MSGLOCATIONENABLED , NULL , NULL , 0 );
529583 dc_param_set_int (msg -> param , DC_PARAM_CMD , DC_CMD_LOCATION_STREAMING_ENABLED );
530- msg -> id = dc_send_msg (context , chat_id , msg );
531- context -> cb (context , DC_EVENT_MSGS_CHANGED , chat_id , msg -> id );
584+ dc_send_msg (context , chat_id , msg );
532585 }
533586 else if (!seconds && is_sending_locations_before ) {
534587 stock_str = dc_stock_system_msg (context , DC_STR_MSGLOCATIONDISABLED , NULL , NULL , 0 );
535588 dc_add_device_msg (context , chat_id , stock_str );
536589 }
537590
591+ // update eg. the "location-sending"-icon
592+ context -> cb (context , DC_EVENT_CHAT_MODIFIED , chat_id , 0 );
593+
538594 if (seconds ) {
539595 schedule_MAYBE_SEND_LOCATIONS (context , 0 );
596+ dc_job_add (context , DC_JOB_MAYBE_SEND_LOC_ENDED , chat_id , NULL , seconds + 1 );
540597 }
541598
542599cleanup :
@@ -549,6 +606,8 @@ void dc_send_locations_to_chat(dc_context_t* context, uint32_t chat_id,
549606/**
550607 * Check if location streaming is enabled.
551608 * Location stream can be enabled or disabled using dc_send_locations_to_chat().
609+ * If you have already a dc_chat_t object,
610+ * dc_chat_is_sending_locations() may be more handy.
552611 *
553612 * @memberof dc_context_t
554613 * @param context The context object.
0 commit comments