[Messaging] Adding attachments to email
authorRobert Karolak <r.karolak@samsung.com>
Wed, 17 Dec 2014 14:28:18 +0000 (15:28 +0100)
committerRobert Karolak <r.karolak@samsung.com>
Fri, 19 Dec 2014 14:50:59 +0000 (15:50 +0100)
[Verification] Code compiles without errors,
with disabled smack security attachments was added
properly to draft.

Change-Id: I253bb983c5e547adc440ce0b0c53b0584f98a9b1
Signed-off-by: Robert Karolak <r.karolak@samsung.com>
packaging/webapi-plugins.spec
src/messaging/email_manager.cc
src/messaging/message.cc
src/messaging/message_email.cc
src/messaging/messaging.gyp
src/messaging/messaging_api.js
src/messaging/messaging_instance.cc
src/messaging/messaging_util.cc
src/messaging/messaging_util.h

index cf35526d8b4227b793d5810e7e3f52db73d7bf8f..862fa160eaf2f9668203f2ea8d5e36a6a04fb3df 100644 (file)
@@ -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)
index 973dc3b6c4d4d0e4177b2d72dce0c8ddedfff716..7462bbdce9ad4ffdffc4ba0e524dbf7faa01d65a 100644 (file)
@@ -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) {
index f9e65354921dc5f37da5fd10e595888d05da9f6c..ffd703c46e740f39a8f65af5f5f0a477a7d5ea57 100644 (file)
@@ -4,7 +4,6 @@
 
 #include "message.h"
 
-//#include "Ecore_File.h"
 #include <time.h>
 #include <sys/stat.h>
 #include <sstream>
@@ -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> 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<MessageBody> body;
-//        body = message->getBody();
-//        if (!body->getPlainBody().empty()) {
-//            msg_set_str_value(msg, MSG_MESSAGE_SMS_DATA_STR, const_cast<char*>
-//                    (body->getPlainBody().c_str()), body->getPlainBody().size());
-//        }
+        std::shared_ptr<MessageBody> body;
+        body = message->getBody();
+        if (!body->getPlainBody().empty()) {
+            msg_set_str_value(msg, MSG_MESSAGE_SMS_DATA_STR, const_cast<char*>
+                    (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);
 }
 
index 9f4bcc2a14da290f99e42a958e95df9dc8ee8c4e..955d74714d46dfcbd22493f15a46655e343fab63 100644 (file)
@@ -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
index e4afa9dcbc46ad8972dd8930edbe8f14c676d9ad..8de579d7937b2ff8285cec8d31d6f5ed6a511131 100644 (file)
@@ -14,6 +14,7 @@
             'dbus-1',
             'dbus-glib-1',
             'capi-system-info',
+            'ecore',
             'tapi',
             'vconf'
         ],
index 21c974c497efa112cb14d80da1669b146d52c2cb..1ab8b9c3086293effd17c38f0dd7d855332eb6a4 100644 (file)
@@ -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);
     }
index e1dd03b4c09c023e007e922ec3e0133de6f2dba3..b7e41ad3ff27be632119fbe4c92f71bcf80f73ea 100644 (file)
@@ -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(){
index cb598320de48062d46b3ad11b5f6f3d1e1f456d7..59a7c5b3ec0dd4abdac9d5750b18597f21d9d5dc 100644 (file)
@@ -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<Message> MessagingUtil::jsonToMessage(const picojson::value& jso
 
     message->setBody(body);
 
-    // TODO MessageAttachments
+    AttachmentPtrVector attachments;
+    auto ma = data.at(MESSAGE_ATTRIBUTE_MESSAGE_ATTACHMENTS).get<std::vector<picojson::value>>();
+
+    auto arrayVectorAttachmentConverter = [&attachments] (picojson::value& v)->void
+    {
+        std::shared_ptr<MessageAttachment> attachment =
+                std::shared_ptr<MessageAttachment>(new MessageAttachment());
+
+        auto obj = v.get<picojson::object>();
+        int messageAttachmentId = std::atoi(MessagingUtil::getValueFromJSONObject<std::string>(obj,
+                MESSAGE_ATTACHMENT_ATTRIBUTE_ID).c_str());
+        attachment->setId(messageAttachmentId);
+
+        int messageId = std::atoi(MessagingUtil::getValueFromJSONObject<std::string>(obj,
+                MESSAGE_ATTACHMENT_ATTRIBUTE_MESSAGE_ID).c_str());
+        attachment->setMessageId(messageId);
+
+        std::string mimeType = MessagingUtil::getValueFromJSONObject<std::string>(obj,
+                MESSAGE_ATTACHMENT_ATTRIBUTE_MIME_TYPE);
+        attachment->setMimeType(mimeType);
+
+        std::string filePath = MessagingUtil::getValueFromJSONObject<std::string>(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;
 
index e83c8a695de2f6b1c59382af3133399c96b26521..df73645aa9a601d83b1b69232da52133ccec01ba 100644 (file)
@@ -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,