wrt-plugins-tizen_0.4.23
[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     for (size_t i = 0; i < recipients.getRecipientSize(); ++i) {
63         result += formatAddress(recipients.getRecipient(i)) + ";";
64     }
65     return result;
66 }
67
68 std::string stripAddress(const std::string& address)
69 {
70     std::string result;
71     pcrecpp::RE re(EMAIL_ADDRESS_REGEX_GROUP);
72     if (!re.PartialMatch(address, &result)) {
73         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Couldn't discover email address");
74     }
75     return result;
76 }
77
78 Recipients stripAddressLine(const std::string& addressLine)
79 {
80     typedef std::vector<std::string> Addresses;
81
82     Recipients result;
83     Addresses addresses = String::split(addressLine, ';');
84     for (Addresses::iterator it = addresses.begin();
85          it != addresses.end();
86          ++it) {
87         result.appendRecipient(stripAddress(*it));
88     }
89     return result;
90 }
91
92 bool isFrameworkEmailData(char* filePath)
93 {
94         std::string tempString1;
95         std::string tempString2;
96
97         if(filePath == NULL)
98         {
99                 LoggerD("filePath is NULL");
100                 return false;
101         }
102
103         LoggerD("filePath : " << filePath);
104
105         tempString1.assign(filePath);
106         tempString2.assign(EMAIL_DATA_FILE_PATH);
107
108         if(tempString1.size() < tempString2.size())
109                 return false;
110
111         if(tempString1.compare(0,tempString2.size(), tempString2) == 0)
112         {
113                 LoggerD("email frame work files");
114                 return true;
115         }
116         else{
117                 LoggerD("tmp files");
118                 return false;
119         }
120
121 }
122
123 }
124 }
125 }