tizen beta release
[framework/web/wrt-plugins-common.git] / src / modules / 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  * @file       Recipient.cpp
20  * @author     Pawel Misiak (p.misiak@samsung.com)
21  * @version    0.1
22  * @brief
23  */
24 #include <algorithm>
25 #include <sstream>
26 #include <dpl/log/log.h>
27 #include <Commons/Exception.h>
28 #include "Recipient.h"
29
30 using namespace std;
31 using namespace WrtDeviceApis::Commons;
32
33
34 namespace {
35 const static string emptyString;
36 }
37
38 namespace WrtDeviceApis {
39 namespace Messaging {
40 namespace Api {
41 Recipients::Recipients() :
42     m_validRecipients(false)
43 {
44 }
45
46 Recipients::Recipients(const vector<string>& value) :
47     m_validRecipients(false)
48 {
49     m_recipients = value;
50 }
51
52 Recipients::~Recipients()
53 {
54 }
55
56 bool Recipients::isValid() const
57 {
58     return m_validRecipients;
59 }
60
61 void Recipients::setValid(bool state)
62 {
63     m_validRecipients = state;
64 }
65
66 void Recipients::setRecipients(const vector<string>& value)
67 {
68     m_recipients = value;
69     m_validRecipients = false;
70 }
71
72 void Recipients::setRecipients(const std::string& value)
73 {
74     vector<string> tmp = split(value, ';');
75     setRecipients(tmp);
76 }
77
78 void Recipients::setRecipient(const size_t index,
79         const string& value)
80 {
81     if (index >= m_recipients.size()) {
82         Throw(OutOfRangeException);
83         return;
84     }
85     m_recipients[index] = value;
86     m_validRecipients = false;
87 }
88
89 void Recipients::setRecipientWithExpand(const size_t index,
90         const std::string& value)
91 {
92     if (index >= m_recipients.size()) {
93         m_recipients.resize(index + 1, emptyString);
94     }
95     m_recipients[index] = value;
96     m_validRecipients = false;
97 }
98
99 vector<string> Recipients::getRecipients() const
100 {
101     return m_recipients;
102 }
103
104 const vector<string>& Recipients::getRecipientsRef() const
105 {
106     return m_recipients;
107 }
108
109 string Recipients::getRecipient(const size_t index) const
110 {
111     if (index >= m_recipients.size()) {
112         Throw(OutOfRangeException);
113         return "";
114     }
115     return m_recipients[index];
116 }
117
118 void Recipients::appendRecipient(const string & value)
119 {
120     vector<string> tmp = split(value, ';');
121     m_recipients.insert(m_recipients.end(), tmp.begin(), tmp.end());
122     m_validRecipients = false;
123 }
124
125 void Recipients::removeRecipient(const size_t position)
126 {
127     if (position >= m_recipients.size()) {
128         Throw(Commons::OutOfRangeException);
129     } else {
130         m_recipients.erase(m_recipients.begin() + position);
131         m_validRecipients = false;
132     }
133 }
134
135 void Recipients::removeRecipient(const string & value)
136 {
137     m_recipients.erase(
138         remove(m_recipients.begin(), m_recipients.end(), value),
139         m_recipients.end());
140     m_validRecipients = false;
141 }
142
143 string Recipients::join(char delimiter) const
144 {
145     string retVal;
146     for (size_t i = 0; i < m_recipients.size(); i++) {
147         retVal += m_recipients[i] + delimiter;
148     }
149     return retVal;
150 }
151
152 vector<string> Recipients::split(const string& input,
153         char delimiter)
154 {
155     vector<string> ret;
156     stringstream stream(input);
157     string item;
158     while (getline(stream, item, delimiter)) {
159         ret.push_back(item);
160     }
161     return ret;
162 }
163
164 size_t Recipients::getRecipientSize() const
165 {
166     return m_recipients.size();
167 }
168
169 bool Recipients::operator !=(const Recipients& arg) const
170 {
171     if (m_recipients == arg.m_recipients) {
172         return true;
173     }
174     return false;
175 }
176
177 Recipients & Recipients::operator +=(const Recipients& val)
178 {
179     const vector<string>& tmp = val.getRecipientsRef();
180     m_recipients.insert(m_recipients.end(), tmp.begin(), tmp.end());
181     m_validRecipients = false;
182     return *this;
183 }
184
185 Recipients & Recipients::operator -=(const Recipients& val)
186 {
187     //check if recipients are inside list
188     vector<string> l_recipients = m_recipients;
189     vector<string> l_toDelete = val.getRecipients();
190     sort(l_recipients.begin(), l_recipients.end());
191     sort(l_toDelete.begin(), l_toDelete.end());
192     if (!includes(l_recipients.begin(), l_recipients.end(), l_toDelete.begin(),
193                   l_toDelete.end())) {
194         LogError("lack of recipient to delete from main variable");
195         Throw(Commons::InvalidArgumentException);
196     }
197     //delete recipients
198     vector<string>::const_iterator it = l_toDelete.begin();
199     const vector<string>::const_iterator endIt = l_toDelete.end();
200     while (it != endIt) {
201         m_recipients.erase(
202             remove(m_recipients.begin(),
203                    m_recipients.end(), *it), m_recipients.end());
204         ++it;
205     }
206     m_validRecipients = false;
207     return *this;
208 }
209 }}
210 }