Update change log and spec for wrt-plugins-tizen_0.4.70
[framework/web/wrt-plugins-tizen.git] / src / Messaging / EmailUtils.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 #include <sstream>
19 #include <pcrecpp.h>
20 #include <Commons/Exception.h>
21 #include <Commons/StringBuilder.h>
22 #include <Commons/StringUtils.h>
23 #include "EmailUtils.h"
24 #include <Manager.h>
25 #include <Logger.h>
26
27 using namespace WrtDeviceApis::Commons;
28
29 namespace {
30 const char* EMAIL_ADDRESS_REGEX =
31     "[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,6}";
32
33 const std::string EMAIL_ADDRESS_REGEX_GROUP =
34     StringBuilder()
35         .append("(")
36         .append(EMAIL_ADDRESS_REGEX)
37         .append(")")
38         .toString();
39 }
40 namespace DeviceAPI {
41 namespace Messaging {
42 namespace EmailUtils {
43 std::string formatAddress(const std::string& address,
44         const std::string& name)
45 {
46     if (address.empty()) {
47         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Address is empty.");
48     }
49
50     std::stringstream ss;
51     if (!name.empty()) {
52         ss << "\"" << name << "\"";
53     }
54     ss << "<" << address << ">";
55
56     return ss.str();
57 }
58
59 std::string formatAddressLine(const Recipients& recipients)
60 {
61     std::string result;
62     size_t tempSize = recipients.getRecipientSize();
63
64     for (size_t i = 0; i < tempSize; ++i) {
65         if(i != tempSize-1)
66         {
67             result += formatAddress(recipients.getRecipient(i)) + ";";
68         }
69         else
70         {
71             result += formatAddress(recipients.getRecipient(i));
72         }
73     }
74     return result;
75 }
76
77 std::string stripAddress(const std::string& address)
78 {
79     std::string result;
80     pcrecpp::RE re(EMAIL_ADDRESS_REGEX_GROUP);
81     if (!re.PartialMatch(address, &result)) {
82         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Couldn't discover email address");
83     }
84     return result;
85 }
86
87 Recipients stripAddressLine(const std::string& addressLine)
88 {
89     typedef std::vector<std::string> Addresses;
90
91     Recipients result;
92     Addresses addresses = String::split(addressLine, ';');
93     for (Addresses::iterator it = addresses.begin();
94          it != addresses.end();
95          ++it) {
96         result.appendRecipient(stripAddress(*it));
97     }
98     return result;
99 }
100
101 bool isFrameworkEmailData(char* filePath)
102 {
103         std::string tempString1;
104         std::string tempString2;
105
106         if(filePath == NULL)
107         {
108                 LoggerD("filePath is NULL");
109                 return false;
110         }
111
112         LoggerD("filePath : " << filePath);
113
114         tempString1.assign(filePath);
115         tempString2.assign(EMAIL_DATA_FILE_PATH);
116
117         if(tempString1.size() < tempString2.size())
118                 return false;
119
120         if(tempString1.compare(0,tempString2.size(), tempString2) == 0)
121         {
122                 LoggerD("email frame work files");
123                 return true;
124         }
125         else{
126                 LoggerD("tmp files");
127                 return false;
128         }
129
130 }
131
132 }
133 }
134 }