1
0
Fork 0
mirror of https://github.com/deltachat/deltachat-core.git synced 2025-10-06 03:50:08 +02:00
This commit is contained in:
B. Petersen 2017-11-20 16:19:56 +01:00
parent 65b4d2d515
commit c278145081
5 changed files with 355 additions and 275 deletions

View file

@ -110,7 +110,6 @@ void mrmsg_empty(mrmsg_t* msg)
/**
* Set the type of a message.
* Possible types are MR_MSG_TEXT, MR_MSG_IMAGE, MR_MSG_GIF,MR_MSG_AUDIO,, MR_MSG_VOICE, MR_MSG_VIDEO or MR_MSG_FILE.
*
* See mrmailbox_send_msg() for some examples.
*
@ -119,6 +118,8 @@ void mrmsg_empty(mrmsg_t* msg)
* @param msg The message object to modify.
*
* @param type Type to set for the message.
* Possible types are MR_MSG_TEXT (10), MR_MSG_IMAGE (20), MR_MSG_GIF (21),
* MR_MSG_AUDIO (40), MR_MSG_VOICE (41), MR_MSG_VIDEO (50) or MR_MSG_FILE (60).
*
* @return None.
*/
@ -132,28 +133,6 @@ void mrmsg_set_type(mrmsg_t* msg, int type)
}
/**
* Set the file belonging to a message.
* The file may be an image, a video, an audio file, an PDF and so on.
* This function is a shortcut for mrparam_set(msg->m_param, MRP_FILE, file)
*
* @memberof mrmsg_t
*
* @param msg The message object to modify.
*
* @param file Path, filename and extension to set for the given message.
*
* @return None.
*/
void mrmsg_set_file(mrmsg_t* msg, const char* file)
{
if( msg == NULL ) {
return;
}
mrparam_set(msg->m_param, MRP_FILE, file);
}
/**
* Set the text of a message object.
*
@ -183,58 +162,315 @@ void mrmsg_set_text(mrmsg_t* msg, const char* text)
/**
* Guess message type from suffix.
* Set the file belonging to a message.
* The file may be an image, a video, an audio file, an PDF and so on.
* This function is a shortcut for mrparam_set(msg->m_param, MRP_FILE, file)
*
* @private @memberof mrmsg_t
* @memberof mrmsg_t
*
* @param pathNfilename Path and filename of the file to guess the type for.
* @param msg The message object to modify.
*
* @param[out] ret_msgtype Guessed message type is copied here as one of the MR_MSG_* constants.
* @param file Path, filename and extension to set for the given message.
*
* @param[out] ret_mime The pointer to a string buffer is set to the guessed MIME-type. May be NULL. Must be free()'d by the caller.
*
* @return None. But there are output parameters.
* @return None.
*/
void mrmsg_guess_msgtype_from_suffix(const char* pathNfilename, int* ret_msgtype, char** ret_mime)
void mrmsg_set_file(mrmsg_t* msg, const char* file)
{
if( pathNfilename == NULL || ret_msgtype == NULL || ret_mime == NULL) {
if( msg == NULL ) {
return;
}
mrparam_set(msg->m_param, MRP_FILE, file);
}
*ret_msgtype = MR_MSG_UNDEFINED;
*ret_mime = NULL;
char* s = mr_get_filesuffix_lc(pathNfilename);
if( s == NULL ) {
/*******************************************************************************
* Getters
******************************************************************************/
/**
* Get the type of the message.
*
* @memberof mrmsg_t
*
* @param msg The message object.
*
* @return One of MR_MSG_TEXT (10), MR_MSG_IMAGE (20), MR_MSG_GIF (21),
* MR_MSG_AUDIO (40), MR_MSG_VOICE (41), MR_MSG_VIDEO (50), MR_MSG_FILE (60)
* or MR_MSG_UNDEFINED (0) if the type is undefined.
*/
int mrmsg_get_type(mrmsg_t* msg)
{
if( msg == NULL ) {
return MR_MSG_UNDEFINED;
}
return msg->m_type;
}
/**
* Get the state of a message.
*
* Incoming message states:
* - MR_STATE_IN_FRESH (10) - Incoming _fresh_ message. Fresh messages are not noticed nor seen and are typically shown in notifications. Use mrmailbox_get_fresh_msgs() to get all fresh messages.
* - MR_STATE_IN_NOTICED (13) - Incoming _noticed_ message. Eg. chat opened but message not yet read - noticed messages are not counted as unread but did not marked as read nor resulted in MDNs. Use mrmailbox_marknoticed_chat() or mrmailbox_marknoticed_contact() to mark messages as being noticed.
* - MR_STATE_IN_SEEN (16) - Incoming message, really _seen_ by the user. Marked as read on IMAP and MDN may be send. Use mrmailbox_markseen_msgs() to mark messages as being seen.
*
* Outgoing message states:
* - MR_STATE_OUT_PENDING (20) - The user has send the "send" button but the
* message is not yet sent and is pending in some way. Maybe we're offline (no checkmark).
* - MR_STATE_OUT_ERROR (24) - _Unrecoverable_ error (_recoverable_ errors result in pending messages)
* - MR_STATE_OUT_DELIVERED (26) - Outgoing message successfully delivered to server (one checkmark). Note, that already delivered messages may get into the state MR_STATE_OUT_ERROR if we get such a hint from the server.
* If a sent message changes to this state, you'll receive the event #MR_EVENT_MSG_DELIVERED.
* - MR_STATE_OUT_MDN_RCVD (28) - Outgoing message read by the recipient (two checkmarks; this requires goodwill on the receiver's side)
* If a sent message changes to this state, you'll receive the event #MR_EVENT_MSG_READ.
*
* The state of just created message objects is MR_STATE_UNDEFINED (0).
* The state is always set by the core-library, users of the library cannot set the state directly, but it is changed implicitly eg.
* when calling mrmailbox_marknoticed_chat() or mrmailbox_markseen_msgs().
*
* @memberof mrmsg_t
*
* @param msg The message object.
*
* @return The state of the message.
*/
int mrmsg_get_state(mrmsg_t* msg)
{
if( msg == NULL ) {
return MR_STATE_UNDEFINED;
}
return msg->m_state;
}
/**
* Get the text of the message.
*
* @memberof mrmsg_t
*
* @param msg The message object.
*
* @return Message text. The result must be free()'d.
*/
char* mrmsg_get_text(mrmsg_t* msg)
{
return safe_strdup(msg? msg->m_text : NULL);
}
/**
* Find out full path, file name and extension of the file associated with a
* message.
*
* @memberof mrmsg_t
*
* @param msg The message object.
*
* @return Full path, file name and extension of the file associated with the
* message. If there is no file associated with the message, an emtpy
* string is returned. NULL is never returned and the returned value must be free()'d.
*/
char* mrmsg_get_file(mrmsg_t* msg)
{
char* ret = NULL;
if( msg == NULL ) {
goto cleanup;
}
if( strcmp(s, "mp3")==0 ) {
*ret_msgtype = MR_MSG_AUDIO;
*ret_mime = safe_strdup("audio/mpeg");
ret = mrparam_get(msg->m_param, MRP_FILE, NULL);
cleanup:
return ret? ret : safe_strdup(NULL);
}
/**
* Get base file name without path. The base file name includes the extension; the path
* is not returned. To get the full path, use mrmsg_get_file().
*
* @param msg the message object
*
* @return base file name plus extension without part. If there is no file
* associated with the message, an empty string is returned. The returned
* value must be free()'d.
*/
char* mrmsg_get_filename(mrmsg_t* msg)
{
char* ret = NULL, *pathNfilename = NULL;
if( msg == NULL ) {
goto cleanup;
}
else if( strcmp(s, "mp4")==0 ) {
*ret_msgtype = MR_MSG_VIDEO;
*ret_mime = safe_strdup("video/mp4");
pathNfilename = mrparam_get(msg->m_param, MRP_FILE, NULL);
if( pathNfilename == NULL ) {
goto cleanup;
}
else if( strcmp(s, "jpg")==0 || strcmp(s, "jpeg")==0 ) {
*ret_msgtype = MR_MSG_IMAGE;
*ret_mime = safe_strdup("image/jpeg");
ret = mr_get_filename(pathNfilename);
cleanup:
free(pathNfilename);
return ret? ret : safe_strdup(NULL);
}
/**
* Get real author and title.
*
* - For voice messages, the author is the sender and the trackname is the sending time.
* - For music messages and videos, we read the information from the filename
* (we do not read ID3 and such at this stage, the needed libraries are too complicated and oversized.
* However, this is no big problem, as the sender usually sets the filename in a way we expect it)
*
* @memberof mrmsg_t
*
* @param msg the message object
*
* @return mrpoortext_t object that contains the author as mrpoortext_t::m_text1 and the title as mrpoortext_t::m_text2.
* Both may be NULL if unknown. The returned object must be freed using mrpoortext_unref() when no longer used.
*/
mrpoortext_t* mrmsg_get_mediainfo(mrmsg_t* msg)
{
mrpoortext_t* ret = mrpoortext_new();
char *pathNfilename = NULL;
mrcontact_t* contact = NULL;
if( msg == NULL || msg->m_mailbox == NULL ) {
goto cleanup;
}
else if( strcmp(s, "png")==0 ) {
*ret_msgtype = MR_MSG_IMAGE;
*ret_mime = safe_strdup("image/png");
if( msg->m_type == MR_MSG_VOICE )
{
if( (contact = mrmailbox_get_contact(msg->m_mailbox, msg->m_from_id))==NULL ) {
goto cleanup;
}
ret->m_text1 = safe_strdup((contact->m_name&&contact->m_name[0])? contact->m_name : contact->m_addr);
ret->m_text2 = mrstock_str(MR_STR_VOICEMESSAGE);
}
else if( strcmp(s, "gif")==0 ) {
*ret_msgtype = MR_MSG_GIF;
*ret_mime = safe_strdup("image/gif");
else
{
ret->m_text1 = mrparam_get(msg->m_param, MRP_AUTHORNAME, NULL);
ret->m_text2 = mrparam_get(msg->m_param, MRP_TRACKNAME, NULL);
if( ret->m_text1 && ret->m_text1[0] && ret->m_text2 && ret->m_text2[0] ) {
goto cleanup;
}
free(ret->m_text1); ret->m_text1 = NULL;
free(ret->m_text2); ret->m_text2 = NULL;
pathNfilename = mrparam_get(msg->m_param, MRP_FILE, NULL);
if( pathNfilename == NULL ) {
goto cleanup;
}
mrmsg_get_authorNtitle_from_filename(pathNfilename, &ret->m_text1, &ret->m_text2);
if( ret->m_text1 == NULL && ret->m_text2 != NULL ) {
ret->m_text1 = mrstock_str(MR_STR_AUDIO);
}
}
cleanup:
free(s);
free(pathNfilename);
mrcontact_unref(contact);
return ret;
}
/**
* Check if a padlock should be shown beside the message.
*
* @memberof mrmsg_t
*
* @param msg The message object.
*
* @return 1=padlock should be shown beside message, 0=do not show a padlock beside the message.
*/
int mrmsg_get_showpadlock(mrmsg_t* msg)
{
/* a padlock guarantees that the message is e2ee _and_ answers will be as well */
if( msg != NULL ) {
if( msg->m_mailbox && msg->m_mailbox->m_e2ee_enabled ) {
if( mrparam_get_int(msg->m_param, MRP_GUARANTEE_E2EE, 0) != 0 ) {
return 1;
}
}
}
return 0;
}
/**
* Get a summary for a message.
* Typically used to display a search result.
*
* @memberof mrmsg_t
*
* @param msg The message object.
*
* @param chat To speed up things, pass an already available chat object here.
* If the chat object is not yet available, it is faster to pass NULL.
*
* @return The returned summary is similar to mrchatlist_get_summary(), however, without
* "draft", "no messages" and so on. The result must be freed using mrpoortext_unref().
*/
mrpoortext_t* mrmsg_get_summary(mrmsg_t* msg, mrchat_t* chat)
{
mrpoortext_t* ret = mrpoortext_new();
mrcontact_t* contact = NULL;
mrchat_t* chat_to_delete = NULL;
if( msg==NULL ) {
goto cleanup;
}
if( chat == NULL ) {
if( (chat=mrmailbox_get_chat(msg->m_mailbox, msg->m_chat_id)) == NULL ) {
goto cleanup;
}
chat_to_delete = chat;
}
if( msg->m_from_id != MR_CONTACT_ID_SELF && chat->m_type == MR_CHAT_TYPE_GROUP ) {
contact = mrmailbox_get_contact(chat->m_mailbox, msg->m_from_id);
}
mrpoortext_fill(ret, msg, chat, contact);
cleanup:
mrcontact_unref(contact);
mrchat_unref(chat_to_delete);
return ret;
}
/**
* Get a message summary as a single line of text. Typically used for
* notifications.
*
* @memberof mrmsg_t
*
* @param msg The message object.
*
* @param approx_characters Rough length of the expected string.
*
* @return A summary for the given messages. The returned string must be free()'d.
*/
char* mrmsg_get_summarytext(mrmsg_t* msg, int approx_characters)
{
if( msg==NULL ) {
return safe_strdup(NULL);
}
return mrmsg_get_summarytext_by_raw(msg->m_type, msg->m_text, msg->m_param, approx_characters);
}
/*******************************************************************************
* Misc.
******************************************************************************/
int mrmsg_set_from_stmt__(mrmsg_t* ths, sqlite3_stmt* row, int row_offset) /* field order must be MR_MSG_FIELDS */
{
mrmsg_empty(ths);
@ -299,66 +535,55 @@ int mrmsg_load_from_db__(mrmsg_t* ths, mrmailbox_t* mailbox, uint32_t id)
/**
* Get a summary for a message. The last parameter can be set to speed up
* things if the chat object is already available; if not, it is faster to pass
* NULL here. The result must be freed using mrpoortext_unref().
* Typically used to display a search result.
* Guess message type from suffix.
*
* @memberof mrmsg_t
* @private @memberof mrmsg_t
*
* @return The returned summary is similar to mrchatlist_get_summary(), however, without
* "draft", "no messages" and so on.
* @param pathNfilename Path and filename of the file to guess the type for.
*
* @param[out] ret_msgtype Guessed message type is copied here as one of the MR_MSG_* constants.
*
* @param[out] ret_mime The pointer to a string buffer is set to the guessed MIME-type. May be NULL. Must be free()'d by the caller.
*
* @return None. But there are output parameters.
*/
mrpoortext_t* mrmsg_get_summary(mrmsg_t* msg, mrchat_t* chat)
void mrmsg_guess_msgtype_from_suffix(const char* pathNfilename, int* ret_msgtype, char** ret_mime)
{
mrpoortext_t* ret = mrpoortext_new();
mrcontact_t* contact = NULL;
mrchat_t* chat_to_delete = NULL;
if( pathNfilename == NULL || ret_msgtype == NULL || ret_mime == NULL) {
return;
}
if( msg==NULL ) {
*ret_msgtype = MR_MSG_UNDEFINED;
*ret_mime = NULL;
char* s = mr_get_filesuffix_lc(pathNfilename);
if( s == NULL ) {
goto cleanup;
}
if( chat == NULL ) {
if( (chat=mrmailbox_get_chat(msg->m_mailbox, msg->m_chat_id)) == NULL ) {
goto cleanup;
}
chat_to_delete = chat;
if( strcmp(s, "mp3")==0 ) {
*ret_msgtype = MR_MSG_AUDIO;
*ret_mime = safe_strdup("audio/mpeg");
}
if( msg->m_from_id != MR_CONTACT_ID_SELF && chat->m_type == MR_CHAT_TYPE_GROUP ) {
contact = mrmailbox_get_contact(chat->m_mailbox, msg->m_from_id);
else if( strcmp(s, "mp4")==0 ) {
*ret_msgtype = MR_MSG_VIDEO;
*ret_mime = safe_strdup("video/mp4");
}
else if( strcmp(s, "jpg")==0 || strcmp(s, "jpeg")==0 ) {
*ret_msgtype = MR_MSG_IMAGE;
*ret_mime = safe_strdup("image/jpeg");
}
else if( strcmp(s, "png")==0 ) {
*ret_msgtype = MR_MSG_IMAGE;
*ret_mime = safe_strdup("image/png");
}
else if( strcmp(s, "gif")==0 ) {
*ret_msgtype = MR_MSG_GIF;
*ret_mime = safe_strdup("image/gif");
}
mrpoortext_fill(ret, msg, chat, contact);
cleanup:
mrcontact_unref(contact);
mrchat_unref(chat_to_delete);
return ret;
}
/**
* Check if a padlock should be shown beside the message.
*
* @memberof mrmsg_t
*
* @param msg The message object.
*
* @return 1=padlock should be shown beside message, 0=do not show a padlock beside the message.
*/
int mrmsg_show_padlock(mrmsg_t* msg)
{
/* a padlock guarantees that the message is e2ee _and_ answers will be as well */
if( msg != NULL ) {
if( msg->m_mailbox && msg->m_mailbox->m_e2ee_enabled ) {
if( mrparam_get_int(msg->m_param, MRP_GUARANTEE_E2EE, 0) != 0 ) {
return 1;
}
}
}
return 0;
free(s);
}
@ -380,22 +605,6 @@ void mrmsg_get_authorNtitle_from_filename(const char* pathNfilename, char** ret_
}
/**
* Get a message summary as a single line of text. Typically used for
* notifications. The returned value must be free()'d.
*
* @memberof mrmsg_t
*/
char* mrmsg_get_summarytext(mrmsg_t* msg, int approx_characters)
{
if( msg==NULL ) {
return safe_strdup(NULL);
}
return mrmsg_get_summarytext_by_raw(msg->m_type, msg->m_text, msg->m_param, approx_characters);
}
char* mrmsg_get_summarytext_by_raw(int type, const char* text, mrparam_t* param, int approx_characters)
{
char* ret = NULL;
@ -453,124 +662,6 @@ char* mrmsg_get_summarytext_by_raw(int type, const char* text, mrparam_t* param,
}
/**
* Find out full path, file name and extension of the file associated with a
* message.
*
* @param msg the message object
*
* @return full path, file name and extension of the file associated with the
* message. If there is no file associated with the message, an emtpy
* string is returned. The returned value must be free()'d.
*/
char* mrmsg_get_file(mrmsg_t* msg)
{
char* ret = NULL;
if( msg == NULL ) {
goto cleanup;
}
ret = mrparam_get(msg->m_param, MRP_FILE, NULL);
cleanup:
return ret? ret : safe_strdup(NULL);
}
/**
* Get base file name without path. The base file name includes the extension; the path
* is not returned. To get the full path, use mrmsg_get_file().
*
* @param msg the message object
*
* @return base file name plus extension without part. If there is no file
* associated with the message, an empty string is returned. The returned
* value must be free()'d.
*/
char* mrmsg_get_filename(mrmsg_t* msg)
{
char* ret = NULL, *pathNfilename = NULL;
if( msg == NULL ) {
goto cleanup;
}
pathNfilename = mrparam_get(msg->m_param, MRP_FILE, NULL);
if( pathNfilename == NULL ) {
goto cleanup;
}
ret = mr_get_filename(pathNfilename);
cleanup:
free(pathNfilename);
return ret? ret : safe_strdup(NULL);
}
/**
* Get real author and title. (as return.text1, this is not always the sender, NULL if
* unknown) and title (return.text2, NULL if unknown) of a message.
*
* For voice messages, the author the sender and the trackname is the sending time
* For music messages, we read the information from the filename
* We do not read ID3 and such at this stage, the needed libraries may be buggy
* and the whole stuff is way to complicated.
* However, this is not a great disadvantage, as the sender usually sets the filename in a way we expect it -
* if not, we simply print the whole filename as we do it for documents. All fine in any case :-)
*
* @memberof mrmsg_t
*
* @param msg the message object
*
* @return poortext object that must be unref'd using mrpoortext_unref() when no longer used.
*/
mrpoortext_t* mrmsg_get_mediainfo(mrmsg_t* msg)
{
mrpoortext_t* ret = mrpoortext_new();
char *pathNfilename = NULL;
mrcontact_t* contact = NULL;
if( msg == NULL || msg->m_mailbox == NULL ) {
goto cleanup;
}
if( msg->m_type == MR_MSG_VOICE )
{
if( (contact = mrmailbox_get_contact(msg->m_mailbox, msg->m_from_id))==NULL ) {
goto cleanup;
}
ret->m_text1 = safe_strdup((contact->m_name&&contact->m_name[0])? contact->m_name : contact->m_addr);
ret->m_text2 = mrstock_str(MR_STR_VOICEMESSAGE);
}
else
{
ret->m_text1 = mrparam_get(msg->m_param, MRP_AUTHORNAME, NULL);
ret->m_text2 = mrparam_get(msg->m_param, MRP_TRACKNAME, NULL);
if( ret->m_text1 && ret->m_text1[0] && ret->m_text2 && ret->m_text2[0] ) {
goto cleanup;
}
free(ret->m_text1); ret->m_text1 = NULL;
free(ret->m_text2); ret->m_text2 = NULL;
pathNfilename = mrparam_get(msg->m_param, MRP_FILE, NULL);
if( pathNfilename == NULL ) {
goto cleanup;
}
mrmsg_get_authorNtitle_from_filename(pathNfilename, &ret->m_text1, &ret->m_text2);
if( ret->m_text1 == NULL && ret->m_text2 != NULL ) {
ret->m_text1 = mrstock_str(MR_STR_AUDIO);
}
}
cleanup:
free(pathNfilename);
mrcontact_unref(contact);
return ret;
}
int mrmsg_is_increation__(const mrmsg_t* msg)
{
int is_increation = 0;
@ -596,6 +687,8 @@ int mrmsg_is_increation__(const mrmsg_t* msg)
* `<filename>` is created then, the user should just delete
* `<filename>.increation`
*
* @memberof mrmsg_t
*
* @param msg the message object
*
* @return 1=message is still in creation (`<filename>.increation` exists),