From 161ffc50cd3fc833038ab8ef589c02003bc2982f Mon Sep 17 00:00:00 2001 From: Robert Karolak Date: Wed, 17 Dec 2014 15:28:18 +0100 Subject: [PATCH] [Messaging] Adding attachments to email [Verification] Code compiles without errors, with disabled smack security attachments was added properly to draft. Change-Id: I253bb983c5e547adc440ce0b0c53b0584f98a9b1 Signed-off-by: Robert Karolak --- packaging/webapi-plugins.spec | 1 + src/messaging/email_manager.cc | 7 ++- src/messaging/message.cc | 76 ++++++++++++++--------------- src/messaging/message_email.cc | 11 ++--- src/messaging/messaging.gyp | 1 + src/messaging/messaging_api.js | 39 +++++++++++++++ src/messaging/messaging_instance.cc | 4 +- src/messaging/messaging_util.cc | 37 +++++++++++++- src/messaging/messaging_util.h | 7 +++ 9 files changed, 132 insertions(+), 51 deletions(-) diff --git a/packaging/webapi-plugins.spec b/packaging/webapi-plugins.spec index cf35526d..862fa160 100644 --- a/packaging/webapi-plugins.spec +++ b/packaging/webapi-plugins.spec @@ -160,6 +160,7 @@ BuildRequires: pkgconfig(tapi) BuildRequires: pkgconfig(vconf) BuildRequires: pkgconfig(x11) BuildRequires: pkgconfig(xrandr) +BuildRequires: pkgconfig(ecore) BuildRequires: python BuildRequires: pkgconfig(capi-system-info) BuildRequires: pkgconfig(capi-system-runtime-info) diff --git a/src/messaging/email_manager.cc b/src/messaging/email_manager.cc index 973dc3b6..7462bbdc 100644 --- a/src/messaging/email_manager.cc +++ b/src/messaging/email_manager.cc @@ -240,10 +240,9 @@ void EmailManager::addMessagePlatform(int account_id, message->setId(mail_data->mail_id); message->setMessageStatus(MessageStatus::STATUS_DRAFT); -// TODO Adding attachments -// if (message->getHasAttachment()){ -// Message::addEmailAttachments(message); -// } + if (message->getHasAttachment()){ + Message::addEmailAttachments(message); + } err = email_get_mail_data(message->getId(), &mail_data_final); if(EMAIL_ERROR_NONE != err) { diff --git a/src/messaging/message.cc b/src/messaging/message.cc index f9e65354..ffd703c4 100644 --- a/src/messaging/message.cc +++ b/src/messaging/message.cc @@ -4,7 +4,6 @@ #include "message.h" -//#include "Ecore_File.h" #include #include #include @@ -12,6 +11,7 @@ #include "common/logger.h" #include "common/platform_exception.h" +#include "Ecore_File.h" #include "message_email.h" #include "messaging_util.h" @@ -336,41 +336,41 @@ std::string copyFileToTemp(const std::string& sourcePath) umask(mask); dirPath = "/tmp/" + std::string(buf); -// FIXME filesystem is unavalaible -// if ( sourcePath[0] != '/' ) { -// attPath = Filesystem::External::fromVirtualPath(sourcePath); -// } else { // Assuming that the path is a real path -// attPath = sourcePath; -// } -// -// // Looking for the last occurrence of slash in source path -// std::size_t slashPos; -// if ((slashPos = attPath.find_last_of('/')) == std::string::npos) { -// throw common::UnknownException( -// "Error while copying file to temp: the source path is invalid."); -// } -// -// fileName = attPath.substr(slashPos + 1); -// tmpPath = dirPath + "/" + fileName; -// -// LoggerD("attPath: %s, tmpPath: %s", attPath.c_str(), tmpPath.c_str()); -// if(EINA_TRUE != ecore_file_mkdir(dirPath.c_str())) { -// throw common::UnknownException("Unknown error while creating temp directory."); -// } -// -// if(EINA_TRUE != ecore_file_cp(attPath.c_str(), tmpPath.c_str())) { -// throw common::UnknownException("Unknown error while copying file to temp."); -// } + if ( sourcePath[0] != '/' ) { +// FIXME When filesystem will be available +// attPath = sourcePath; change to attPath = Filesystem::External::fromVirtualPath(sourcePath); + attPath = sourcePath; + } else { // Assuming that the path is a real path + attPath = sourcePath; + } + + // Looking for the last occurrence of slash in source path + std::size_t slashPos; + if ((slashPos = attPath.find_last_of('/')) == std::string::npos) { + throw common::UnknownException( + "Error while copying file to temp: the source path is invalid."); + } + + fileName = attPath.substr(slashPos + 1); + tmpPath = dirPath + "/" + fileName; + + LoggerD("attPath: %s, tmpPath: %s", attPath.c_str(), tmpPath.c_str()); + if(EINA_TRUE != ecore_file_mkdir(dirPath.c_str())) { + throw common::UnknownException("Unknown error while creating temp directory."); + } + + if(EINA_TRUE != ecore_file_cp(attPath.c_str(), tmpPath.c_str())) { + throw common::UnknownException("Unknown error while copying file to temp."); + } return dirPath; } void removeDirFromTemp(const std::string& dirPath) { -// FIXME Eina_File is unavalaible -// if(EINA_TRUE != ecore_file_rmdir(dirPath.c_str())) { -// throw common::UnknownException("Unknown error while deleting temp directory."); -// } + if(EINA_TRUE != ecore_file_rmdir(dirPath.c_str())) { + throw common::UnknownException("Unknown error while deleting temp directory."); + } } email_mail_data_t* Message::convertPlatformEmail(std::shared_ptr message) @@ -752,12 +752,12 @@ msg_struct_t Message::convertPlatformShortMessageToStruct(Message* message, if (type == MSG_TYPE_SMS) { // Set SMS message body text -// std::shared_ptr body; -// body = message->getBody(); -// if (!body->getPlainBody().empty()) { -// msg_set_str_value(msg, MSG_MESSAGE_SMS_DATA_STR, const_cast -// (body->getPlainBody().c_str()), body->getPlainBody().size()); -// } + std::shared_ptr body; + body = message->getBody(); + if (!body->getPlainBody().empty()) { + msg_set_str_value(msg, MSG_MESSAGE_SMS_DATA_STR, const_cast + (body->getPlainBody().c_str()), body->getPlainBody().size()); + } // Reset SMS recipients int error = msg_list_clear(msg, MSG_MESSAGE_ADDR_LIST_HND); @@ -1207,8 +1207,8 @@ void Message::setMMSBodyAndAttachmentsFromStruct(Message* message, throw common::UnknownException("failed to add attachment"); } -// LoggerD("after MSG_MMS_ATTACH_LIST attachments count is:%d", -// message->m_attachments.size()); + LoggerD("after MSG_MMS_ATTACH_LIST attachments count is:%d", + message->m_attachments.size()); msg_release_struct(&mms_struct); } diff --git a/src/messaging/message_email.cc b/src/messaging/message_email.cc index 9f4bcc2a..955d7471 100644 --- a/src/messaging/message_email.cc +++ b/src/messaging/message_email.cc @@ -109,8 +109,7 @@ void MessageEmail::updateEmailMessage(email_mail_data_t& mail) setSubject(mail.subject); } -// TODO fix when MesageBody will be available -// getBody()->updateBody(mail); + getBody()->updateBody(mail); if (mail.mail_id != mail.thread_id) { setInResponseTo(mail.thread_id); @@ -134,10 +133,10 @@ void MessageEmail::updateEmailMessage(email_mail_data_t& mail) setMessageStatus(MessageStatus::STATUS_UNDEFINED); break; } -// TODO fix when Attachment will be available -// AttachmentPtrVector att = convertEmailToMessageAttachment(mail); -// -// setMessageAttachments(att); + + AttachmentPtrVector att = convertEmailToMessageAttachment(mail); + + setMessageAttachments(att); } } // messaging diff --git a/src/messaging/messaging.gyp b/src/messaging/messaging.gyp index e4afa9dc..8de579d7 100644 --- a/src/messaging/messaging.gyp +++ b/src/messaging/messaging.gyp @@ -14,6 +14,7 @@ 'dbus-1', 'dbus-glib-1', 'capi-system-info', + 'ecore', 'tapi', 'vconf' ], diff --git a/src/messaging/messaging_api.js b/src/messaging/messaging_api.js index 21c974c4..1ab8b9c3 100644 --- a/src/messaging/messaging_api.js +++ b/src/messaging/messaging_api.js @@ -17,6 +17,42 @@ var Property = { E: 1 << 1, // ENUMERABLE C: 1 << 2 // CONFIGURABLE } +//TODO remove CommonFS when C++ filesystem will be available +function CommonFS(){}; +CommonFS.cacheVirtualToReal = { + 'downloads' : { path: '/opt/usr/media/Downloads'}, + 'documents' : { path: '/opt/usr/media/Documents'}, + 'music' : { path: '/opt/usr/media/Sounds'}, + 'images' : { path: '/opt/usr/media/Images'}, + 'videos' : { path: '/opt/usr/media/Videos'}, + 'ringtones' : { path: '/opt/usr/share/settings/Ringtones'} +}; + +CommonFS.toRealPath = function (aPath) { + var _fileRealPath = '', + _uriPrefix = 'file://', + i; + if (aPath.indexOf(_uriPrefix) === 0) { + _fileRealPath = aPath.substr(_uriPrefix.length); + } else if (aPath[0] != '/') { + //virtual path$ + var _pathTokens = aPath.split('/'); + if (this.cacheVirtualToReal[_pathTokens[0]] && ( + this.cacheVirtualToReal[_pathTokens[0]].state === undefined || + this.cacheVirtualToReal[_pathTokens[0]].state === 'MOUNTED')) { + _fileRealPath = this.cacheVirtualToReal[_pathTokens[0]].path; + for (i = 1; i < _pathTokens.length; ++i) { + _fileRealPath += '/' + _pathTokens[i]; + } + } else { + _fileRealPath = aPath; + } + } else { + _fileRealPath = aPath; + } + console.log("REAL PATH:"+_fileRealPath); + return _fileRealPath; +}; /** * Example usage: @@ -234,6 +270,9 @@ function MessageAttachment_(data) { function MessageAttachment(filePath, mimeType) { console.dir(this); + + //TODO remove CommonFS.toRealPath function when C++ filesystem will be available + filePath = CommonFS.toRealPath(filePath); if (!this.id) { propertyFactory_(this, 'id', null, Property.E); } diff --git a/src/messaging/messaging_instance.cc b/src/messaging/messaging_instance.cc index e1dd03b4..b7e41ad3 100644 --- a/src/messaging/messaging_instance.cc +++ b/src/messaging/messaging_instance.cc @@ -236,8 +236,8 @@ void MessagingInstance::MessageServiceStopSync(const picojson::value& args, htmlBody: "simle html body", isHightPriority: false } - - var msg = new tizen.Message("messaging.email", initDictionary); + msg = new tizen.Message("messaging.email", initDictionary); + msg.attachments = [new tizen.MessageAttachment("images/myimage.png", "image/png")]; services[0].messageStorage.addDraftMessage(msg, function(){ console.log("Add draft success"); }, function(){ diff --git a/src/messaging/messaging_util.cc b/src/messaging/messaging_util.cc index cb598320..59a7c5b3 100644 --- a/src/messaging/messaging_util.cc +++ b/src/messaging/messaging_util.cc @@ -52,6 +52,12 @@ const char* MESSAGE_BODY_ATTRIBUTE_LOADED = "loaded"; const char* MESSAGE_BODY_ATTRIBUTE_PLAIN_BODY = "plainBody"; const char* MESSAGE_BODY_ATTRIBUTE_HTML_BODY = "htmlBody"; +const char* MESSAGE_ATTRIBUTE_MESSAGE_ATTACHMENTS = "attachments"; +const char* MESSAGE_ATTACHMENT_ATTRIBUTE_ID = "id"; +const char* MESSAGE_ATTACHMENT_ATTRIBUTE_MESSAGE_ID = "messageId"; +const char* MESSAGE_ATTACHMENT_ATTRIBUTE_MIME_TYPE = "mimeType"; +const char* MESSAGE_ATTACHMENT_ATTRIBUTE_FILE_PATH = "filePath"; + namespace { const std::string TYPE_SMS = "messaging.sms"; const std::string TYPE_MMS = "messaging.mms"; @@ -272,7 +278,36 @@ std::shared_ptr MessagingUtil::jsonToMessage(const picojson::value& jso message->setBody(body); - // TODO MessageAttachments + AttachmentPtrVector attachments; + auto ma = data.at(MESSAGE_ATTRIBUTE_MESSAGE_ATTACHMENTS).get>(); + + auto arrayVectorAttachmentConverter = [&attachments] (picojson::value& v)->void + { + std::shared_ptr attachment = + std::shared_ptr(new MessageAttachment()); + + auto obj = v.get(); + int messageAttachmentId = std::atoi(MessagingUtil::getValueFromJSONObject(obj, + MESSAGE_ATTACHMENT_ATTRIBUTE_ID).c_str()); + attachment->setId(messageAttachmentId); + + int messageId = std::atoi(MessagingUtil::getValueFromJSONObject(obj, + MESSAGE_ATTACHMENT_ATTRIBUTE_MESSAGE_ID).c_str()); + attachment->setMessageId(messageId); + + std::string mimeType = MessagingUtil::getValueFromJSONObject(obj, + MESSAGE_ATTACHMENT_ATTRIBUTE_MIME_TYPE); + attachment->setMimeType(mimeType); + + std::string filePath = MessagingUtil::getValueFromJSONObject(obj, + MESSAGE_ATTACHMENT_ATTRIBUTE_FILE_PATH); + attachment->setFilePath(filePath); + + attachments.push_back(attachment); + }; + + for_each(ma.begin(), ma.end(), arrayVectorAttachmentConverter); + message->setMessageAttachments(attachments); return message; diff --git a/src/messaging/messaging_util.h b/src/messaging/messaging_util.h index e83c8a69..df73645a 100644 --- a/src/messaging/messaging_util.h +++ b/src/messaging/messaging_util.h @@ -47,6 +47,13 @@ extern const char* MESSAGE_BODY_ATTRIBUTE_LOADED; extern const char* MESSAGE_BODY_ATTRIBUTE_PLAIN_BODY; extern const char* MESSAGE_BODY_ATTRIBUTE_HTML_BODY; +extern const char* MESSAGE_ATTRIBUTE_MESSAGE_ATTACHMENT; +extern const char* MESSAGE_ATTACHMENT_ATTRIBUTE_ID; +extern const char* MESSAGE_ATTACHMENT_ATTRIBUTE_MESSAGE_ID; +extern const char* MESSAGE_ATTACHMENT_ATTRIBUTE_MIME_TYPE; +extern const char* MESSAGE_ATTACHMENT_ATTRIBUTE_FILE_PATH; + + enum MessageType { UNDEFINED = 0, SMS, -- 2.34.1