text
stringlengths
0
357
/**
* Get shared locations from the database.
* The locations can be filtered by the chat-id, the contact-id
* and by a timespan.
*
* The number of returned locations can be retrieved using dc_array_get_cnt().
* To get information for each location,
* use dc_array_get_latitude(), dc_array_get_longitude(),
* dc_array_get_accuracy(), dc_array_get_timestamp(), dc_array_get_contact_id()
* and dc_array_get_msg_id().
* The latter returns 0 if there is no message bound to the location.
*
* Note that only if dc_array_is_independent() returns 0,
* the location is the current or a past position of the user.
* If dc_array_is_independent() returns 1,
* the location is any location on earth that is marked by the user.
*
* @memberof dc_context_t
* @param context The context object.
* @param chat_id Chat-id to get location information for.
* 0 to get locations independently of the chat.
* @param contact_id Contact-id to get location information for.
* If also a chat-id is given, this should be a member of the given chat.
* 0 to get locations independently of the contact.
* @param timestamp_from Start of timespan to return.
* Must be given in number of seconds since 00:00 hours, Jan 1, 1970 UTC.
* 0 for "start from the beginning".
* @param timestamp_to End of timespan to return.
* Must be given in number of seconds since 00:00 hours, Jan 1, 1970 UTC.
* 0 for "all up to now".
* @return Array of locations, NULL is never returned.
* The array is sorted decending;
* the first entry in the array is the location with the newest timestamp.
* Note that this is only realated to the recent postion of the user
* if dc_array_is_independent() returns 0.
* The returned array must be freed using dc_array_unref().
*
* Examples:
* ~~~
* // get locations from the last hour for a global map
* dc_array_t* loc = dc_get_locations(context, 0, 0, time(NULL)-60*60, 0);
* for (int i=0; i<dc_array_get_cnt(); i++) {
* double lat = dc_array_get_latitude(loc, i);
* ...
* }
* dc_array_unref(loc);
*
* // get locations from a contact for a global map
* dc_array_t* loc = dc_get_locations(context, 0, contact_id, 0, 0);
* ...
*
* // get all locations known for a given chat
* dc_array_t* loc = dc_get_locations(context, chat_id, 0, 0, 0);
* ...
*
* // get locations from a single contact for a given chat
* dc_array_t* loc = dc_get_locations(context, chat_id, contact_id, 0, 0);
* ...
* ~~~
*/
dc_array_t* dc_get_locations(dc_context_t* context,
uint32_t chat_id, uint32_t contact_id,
time_t timestamp_from, time_t timestamp_to)
{
dc_array_t* ret = dc_array_new_typed(context, DC_ARRAY_LOCATIONS, 500);
sqlite3_stmt* stmt = NULL;
if (context==NULL || context->magic!=DC_CONTEXT_MAGIC) {
goto cleanup;
}
if (timestamp_to==0) {
timestamp_to = time(NULL) + 10/*messages may be inserted by another thread just now*/;
}
stmt = dc_sqlite3_prepare(context->sql,
"SELECT l.id, l.latitude, l.longitude, l.accuracy, l.timestamp, l.independent, "
" m.id, l.from_id, l.chat_id, m.txt "
" FROM locations l "
" LEFT JOIN msgs m ON l.id=m.location_id "
" WHERE (? OR l.chat_id=?) "
" AND (? OR l.from_id=?) "
" AND (l.independent=1 OR (l.timestamp>=? AND l.timestamp<=?)) "
" ORDER BY l.timestamp DESC, l.id DESC, m.id DESC;");
sqlite3_bind_int(stmt, 1, chat_id==0? 1 : 0);
sqlite3_bind_int(stmt, 2, chat_id);
sqlite3_bind_int(stmt, 3, contact_id==0? 1 : 0);
sqlite3_bind_int(stmt, 4, contact_id);
sqlite3_bind_int(stmt, 5, timestamp_from);
sqlite3_bind_int(stmt, 6, timestamp_to);
while (sqlite3_step(stmt)==SQLITE_ROW) {
struct _dc_location* loc = calloc(1, sizeof(struct _dc_location));
if (loc==NULL) {
goto cleanup;
}
loc->location_id = sqlite3_column_double(stmt, 0);