Git Init
[profile/ivi/wrt-plugins-tizen.git] / src / platform / API / Messaging / Recipient.cpp
1 /*
2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License. 
15 */
16
17
18 /**
19  *
20  *
21  * @file       Recipient.cpp
22  * @author     Pawel Misiak (p.misiak@samsung.com)
23  * @version    0.1
24  * @brief
25  */
26 #include <algorithm>
27 #include <sstream>
28 #include <dpl/log.h>
29 #include <Commons/Exception.h>
30 #include "Recipient.h"
31
32 using namespace std;
33 using namespace WrtDeviceApis::Commons;
34
35 namespace {
36 const static string emptyString;
37 }
38
39 namespace TizenApis {
40 namespace Api {
41 namespace Messaging {
42
43 Recipients::Recipients() :
44     m_validRecipients(false)
45 {
46 }
47
48 Recipients::Recipients(const vector<string>& value) :
49     m_validRecipients(false)
50 {
51     m_recipients = value;
52 }
53
54 Recipients::~Recipients()
55 {
56 }
57
58 bool Recipients::isValid() const
59 {
60     return m_validRecipients;
61 }
62
63 void Recipients::setValid(bool state)
64 {
65     m_validRecipients = state;
66 }
67
68 void Recipients::setRecipients(const vector<string>& value)
69 {
70     m_recipients = value;
71     m_validRecipients = false;
72 }
73
74 void Recipients::setRecipients(const std::string& value)
75 {
76     vector<string> tmp = split(value, ';');
77     setRecipients(tmp);
78 }
79
80 void Recipients::setRecipient(const size_t index,
81         const string& value)
82 {
83     if (index >= m_recipients.size()) {
84         Throw(OutOfRangeException);
85         return;
86     }
87     m_recipients[index] = value;
88     m_validRecipients = false;
89 }
90
91 void Recipients::setRecipientWithExpand(const size_t index,
92         const std::string& value)
93 {
94     if (index >= m_recipients.size()) {
95         m_recipients.resize(index + 1, emptyString);
96     }
97     m_recipients[index] = value;
98     m_validRecipients = false;
99 }
100
101 vector<string> Recipients::getRecipients() const
102 {
103     return m_recipients;
104 }
105
106 const vector<string>& Recipients::getRecipientsRef() const
107 {
108     return m_recipients;
109 }
110
111 string Recipients::getRecipient(const size_t index) const
112 {
113     if (index >= m_recipients.size()) {
114         Throw(OutOfRangeException);
115         return "";
116     }
117     return m_recipients[index];
118 }
119
120 void Recipients::appendRecipient(const string & value)
121 {
122     vector<string> tmp = split(value, ';');
123     m_recipients.insert(m_recipients.end(), tmp.begin(), tmp.end());
124     m_validRecipients = false;
125 }
126
127 void Recipients::removeRecipient(const size_t position)
128 {
129     if (position >= m_recipients.size()) {
130         Throw(WrtDeviceApis::Commons::OutOfRangeException);
131     } else {
132         m_recipients.erase(m_recipients.begin() + position);
133         m_validRecipients = false;
134     }
135 }
136
137 void Recipients::removeRecipient(const string & value)
138 {
139     m_recipients.erase(
140         remove(m_recipients.begin(), m_recipients.end(), value),
141         m_recipients.end());
142     m_validRecipients = false;
143 }
144
145 string Recipients::join(char delimiter) const
146 {
147     string retVal;
148     for (size_t i = 0; i < m_recipients.size(); i++) {
149         retVal += m_recipients[i] + delimiter;
150     }
151     return retVal;
152 }
153
154 vector<string> Recipients::split(const string& input,
155         char delimiter)
156 {
157     vector<string> ret;
158     stringstream stream(input);
159     string item;
160     while (getline(stream, item, delimiter)) {
161         ret.push_back(item);
162     }
163     return ret;
164 }
165
166 size_t Recipients::getRecipientSize() const
167 {
168     return m_recipients.size();
169 }
170
171 bool Recipients::operator !=(const Recipients& arg) const
172 {
173     if (m_recipients == arg.m_recipients) {
174         return true;
175     }
176     return false;
177 }
178
179 Recipients & Recipients::operator +=(const Recipients& val)
180 {
181     const vector<string>& tmp = val.getRecipientsRef();
182     m_recipients.insert(m_recipients.end(), tmp.begin(), tmp.end());
183     m_validRecipients = false;
184     return *this;
185 }
186
187 Recipients & Recipients::operator -=(const Recipients& val)
188 {
189     //check if recipients are inside list
190     vector<string> l_recipients = m_recipients;
191     vector<string> l_toDelete = val.getRecipients();
192     sort(l_recipients.begin(), l_recipients.end());
193     sort(l_toDelete.begin(), l_toDelete.end());
194     if (!includes(l_recipients.begin(), l_recipients.end(), l_toDelete.begin(),
195                   l_toDelete.end())) {
196         LogError("lack of recipient to delete from main variable");
197         Throw(WrtDeviceApis::Commons::InvalidArgumentException);
198     }
199     //delete recipients
200     vector<string>::const_iterator it = l_toDelete.begin();
201     const vector<string>::const_iterator endIt = l_toDelete.end();
202     while (it != endIt) {
203         m_recipients.erase(
204             remove(m_recipients.begin(),
205                    m_recipients.end(), *it), m_recipients.end());
206         ++it;
207     }
208     m_validRecipients = false;
209     return *this;
210 }
211 }
212 }
213 }