Skip to content
This repository was archived by the owner on Nov 25, 2022. It is now read-only.

Commit c1206b7

Browse files
authored
Merge pull request #659 from deltachat/locations-enabled
add dc_chat_is_sending_locations()
2 parents 82c5ad4 + 0149cf2 commit c1206b7

File tree

8 files changed

+102
-10
lines changed

8 files changed

+102
-10
lines changed

cmdline/cmdline.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ your library */
1111
#include "../src/dc_sqlite3.h"
1212

1313

14+
#define UTF8_LOCK "\xF0\x9F\x94\x92"
15+
#define UTF8_BLACK_STAR "\xE2\x98\x85"
16+
#define UTF8_ROUND_PUSHPIN "\xF0\x9F\x93\x8D"
17+
1418

1519
/*
1620
* Reset database tables. This function is called from Core cmdline.
@@ -263,11 +267,11 @@ static void log_msg(dc_context_t* context, const char* prefix, dc_msg_t* msg)
263267
dc_log_info(context, 0, "%s#%i%s: %s (Contact#%i): %s %s%s%s%s [%s]",
264268
prefix,
265269
(int)dc_msg_get_id(msg),
266-
dc_msg_get_showpadlock(msg)? "\xF0\x9F\x94\x92" : "",
270+
dc_msg_get_showpadlock(msg)? UTF8_LOCK : "",
267271
contact_name,
268272
contact_id,
269273
msgtext,
270-
dc_msg_is_starred(msg)? " \xE2\x98\x85" : "",
274+
dc_msg_is_starred(msg)? " " UTF8_BLACK_STAR : "",
271275
dc_msg_get_from_id(msg)==1? "" : (dc_msg_get_state(msg)==DC_STATE_IN_SEEN? "[SEEN]" : (dc_msg_get_state(msg)==DC_STATE_IN_NOTICED? "[NOTICED]":"[FRESH]")),
272276
dc_msg_is_info(msg)? "[INFO]" : "",
273277
statestr,
@@ -710,11 +714,12 @@ char* dc_cmdline(dc_context_t* context, const char* cmdline)
710714
char* timestr = dc_timestamp_to_str(dc_lot_get_timestamp(lot));
711715
char* text1 = dc_lot_get_text1(lot);
712716
char* text2 = dc_lot_get_text2(lot);
713-
dc_log_info(context, 0, "%s%s%s%s [%s]",
717+
dc_log_info(context, 0, "%s%s%s%s [%s]%s",
714718
text1? text1 : "",
715719
text1? ": " : "",
716720
text2? text2 : "",
717-
statestr, timestr
721+
statestr, timestr,
722+
dc_chat_is_sending_locations(chat)? UTF8_ROUND_PUSHPIN : ""
718723
);
719724
free(text1);
720725
free(text2);
@@ -754,7 +759,10 @@ char* dc_cmdline(dc_context_t* context, const char* cmdline)
754759
dc_array_t* msglist = dc_get_chat_msgs(context, dc_chat_get_id(sel_chat), DC_GCM_ADDDAYMARKER, 0);
755760
char* temp2 = dc_chat_get_subtitle(sel_chat);
756761
char* temp_name = dc_chat_get_name(sel_chat);
757-
dc_log_info(context, 0, "%s#%i: %s [%s]", chat_prefix(sel_chat), dc_chat_get_id(sel_chat), temp_name, temp2);
762+
dc_log_info(context, 0, "%s#%i: %s [%s]%s",
763+
chat_prefix(sel_chat), dc_chat_get_id(sel_chat),
764+
temp_name, temp2,
765+
dc_chat_is_sending_locations(sel_chat)? UTF8_ROUND_PUSHPIN : "");
758766
free(temp_name);
759767
free(temp2);
760768
if (msglist) {

src/dc_chat.c

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,25 @@ int dc_chat_is_self_talk(const dc_chat_t* chat)
388388
}
389389

390390

391+
/**
392+
* Check if locations are sent to the chat
393+
* at the time the object was created using dc_get_chat().
394+
* To check if locations are sent to _any_ chat,
395+
* use dc_is_sending_locations_to_chat().
396+
*
397+
* @memberof dc_chat_t
398+
* @param chat The chat object.
399+
* @return 1=locations are sent to chat, 0=no locations are sent to chat
400+
*/
401+
int dc_chat_is_sending_locations(const dc_chat_t* chat)
402+
{
403+
if (chat==NULL || chat->magic!=DC_CHAT_MAGIC) {
404+
return 0;
405+
}
406+
return chat->is_sending_locations;
407+
}
408+
409+
391410
int dc_chat_update_param(dc_chat_t* chat)
392411
{
393412
int success = 0;
@@ -411,7 +430,7 @@ static int set_from_stmt(dc_chat_t* chat, sqlite3_stmt* row)
411430

412431
dc_chat_empty(chat);
413432

414-
#define CHAT_FIELDS " c.id,c.type,c.name, c.grpid,c.param,c.archived, c.blocked, c.gossiped_timestamp "
433+
#define CHAT_FIELDS " c.id,c.type,c.name, c.grpid,c.param,c.archived, c.blocked, c.gossiped_timestamp, c.locations_send_until "
415434
chat->id = sqlite3_column_int (row, row_offset++); /* the columns are defined in CHAT_FIELDS */
416435
chat->type = sqlite3_column_int (row, row_offset++);
417436
chat->name = dc_strdup((char*)sqlite3_column_text (row, row_offset++));
@@ -420,6 +439,7 @@ static int set_from_stmt(dc_chat_t* chat, sqlite3_stmt* row)
420439
chat->archived = sqlite3_column_int (row, row_offset++);
421440
chat->blocked = sqlite3_column_int (row, row_offset++);
422441
chat->gossiped_timestamp = sqlite3_column_int64(row, row_offset++);
442+
chat->is_sending_locations = (sqlite3_column_int64(row, row_offset++)>time(NULL));
423443

424444
/* correct the title of some special groups */
425445
if (chat->id==DC_CHAT_ID_DEADDROP) {

src/dc_chat.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ struct _dc_chat
2525
int blocked; /**< One of DC_CHAT_*_BLOCKED */
2626
dc_param_t* param; /**< Additional parameters for a chat. Should not be used directly. */
2727
time_t gossiped_timestamp;
28+
int is_sending_locations;
2829
};
2930

3031
int dc_chat_load_from_db (dc_chat_t*, uint32_t id);

src/dc_context.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ uint32_t dc_save_locations (dc_context_t*, uint32_t chat_id, uint
152152
dc_kml_t* dc_kml_parse (dc_context_t*, const char* content, size_t content_bytes);
153153
void dc_kml_unref (dc_kml_t*);
154154
void dc_job_do_DC_JOB_MAYBE_SEND_LOCATIONS (dc_context_t*, dc_job_t*);
155+
void dc_job_do_DC_JOB_MAYBE_SEND_LOC_ENDED (dc_context_t*, dc_job_t*);
155156

156157

157158
// backups

src/dc_job.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,7 @@ static void dc_job_perform(dc_context_t* context, int thread, int probe_network)
702702
case DC_JOB_CONFIGURE_IMAP: dc_job_do_DC_JOB_CONFIGURE_IMAP (context, &job); break;
703703
case DC_JOB_IMEX_IMAP: dc_job_do_DC_JOB_IMEX_IMAP (context, &job); break;
704704
case DC_JOB_MAYBE_SEND_LOCATIONS: dc_job_do_DC_JOB_MAYBE_SEND_LOCATIONS (context, &job); break;
705+
case DC_JOB_MAYBE_SEND_LOC_ENDED: dc_job_do_DC_JOB_MAYBE_SEND_LOC_ENDED (context, &job); break;
705706
case DC_JOB_HOUSEKEEPING: dc_housekeeping (context); break;
706707
}
707708

src/dc_job.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ extern "C" {
2222

2323
// jobs in the SMTP-thread, range from DC_SMTP_THREAD..DC_SMTP_THREAD+999
2424
#define DC_JOB_MAYBE_SEND_LOCATIONS 5005 // low priority ...
25+
#define DC_JOB_MAYBE_SEND_LOC_ENDED 5007
2526
#define DC_JOB_SEND_MDN_OLD 5010
2627
#define DC_JOB_SEND_MDN 5011
2728
#define DC_JOB_SEND_MSG_TO_SMTP_OLD 5900

src/dc_location.c

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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

542599
cleanup:
@@ -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.

src/deltachat.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,7 @@ int dc_chat_get_archived (const dc_chat_t*);
484484
int dc_chat_is_unpromoted (const dc_chat_t*);
485485
int dc_chat_is_self_talk (const dc_chat_t*);
486486
int dc_chat_is_verified (const dc_chat_t*);
487+
int dc_chat_is_sending_locations (const dc_chat_t*);
487488

488489

489490
/**

0 commit comments

Comments
 (0)