update tizen source
[framework/messaging/msg-service.git] / framework / plugin-manager / MsgPluginConfig.cpp
1 /*
2 *
3 * Copyright (c) 2000-2012 Samsung Electronics Co., Ltd. All Rights Reserved.
4 *
5 * This file is part of msg-service.
6 *
7 * Contact: Jaeyun Jeong <jyjeong@samsung.com>
8 *          Sangkoo Kim <sangkoo.kim@samsung.com>
9 *          Seunghwan Lee <sh.cat.lee@samsung.com>
10 *          SoonMin Jung <sm0415.jung@samsung.com>
11 *          Jae-Young Lee <jy4710.lee@samsung.com>
12 *          KeeBum Kim <keebum.kim@samsung.com>
13 *
14 * PROPRIETARY/CONFIDENTIAL
15 *
16 * This software is the confidential and proprietary information of
17 * SAMSUNG ELECTRONICS ("Confidential Information"). You shall not
18 * disclose such Confidential Information and shall use it only in
19 * accordance with the terms of the license agreement you entered
20 * into with SAMSUNG ELECTRONICS.
21 *
22 * SAMSUNG make no representations or warranties about the suitability
23 * of the software, either express or implied, including but not limited
24 * to the implied warranties of merchantability, fitness for a particular
25 * purpose, or non-infringement. SAMSUNG shall not be liable for any
26 * damages suffered by licensee as a result of using, modifying or
27 * distributing this software or its derivatives.
28 *
29 */
30
31 #include "MsgPluginConfig.h"
32 #include "MsgException.h"
33
34
35 /*==================================================================================================
36                                      IMPLEMENTATION OF MsgPlgToken - Member Functions
37 ==================================================================================================*/
38 int MsgPlgToken::tokenize(char *pStr)
39 {
40         MSG_BEGIN();
41
42         if (pStr == NULL)
43                 THROW(MsgException::INVALID_PARAM, "pstr is NULL");
44
45         char *pFirst = index(pStr, '[');
46
47         if (pFirst) // title is enclosed by [ ]
48         {
49                 pFirst++; // excluding '['
50
51                 char *pEnd = index(pStr, ']');
52
53                 if (pEnd)
54                 {
55                         pEnd--;
56
57                         tokenType = TOK_PLG_TITLE;
58                         int len = pEnd-pFirst+1;
59
60                         memcpy(tokenVal, pFirst, len);
61                         tokenVal[len] = '\0';
62
63                         MSG_DEBUG("%s", tokenVal);
64                 }
65                 else
66                         THROW(MsgException::INVALID_RESULT, "']' not found");
67         }
68         else // (attribute, value) pair
69         {
70                 char tokStr[strlen(pStr)+1];
71                 strncpy(tokStr, pStr, strlen(pStr));
72
73                 char *attr = strtok(tokStr, "=");
74                 char *val = strtok(NULL, "=");
75
76                 if (!attr || !val)
77                 {
78                         tokenType = TOK_UNDEFINED;
79                 }
80                 else
81                 {
82                         // trimming enter key
83                         char *tmp = index(val,'\n');
84
85                         if (tmp)
86                                 *tmp = '\0';
87
88                         MSG_DEBUG("attr \"%s\", val \"%s\" ", attr, val);
89
90                         // classifying the attribute
91                         if (strcmp(attr, "type") == 0)
92                         {
93                                 tokenType = TOK_PLG_TYPE;
94                                 strncpy(tokenVal, val, 255);
95                         }
96                         else if (strcmp(attr, "path") == 0)
97                         {
98                                 tokenType = TOK_PLG_PATH;
99                                 strncpy(tokenVal, val, 255);
100                         }
101                         else
102                         {
103                                 tokenType = TOK_UNDEFINED;
104                         }
105                 }
106         }
107
108         MSG_END();
109
110         return tokenType;
111 }
112
113
114 /*==================================================================================================
115                                      IMPLEMENTATION OF MsgPlgConfig - Member Functions
116 ==================================================================================================*/
117 MsgPlgConfig::MsgPlgConfig(FILE* fp)
118 {
119         MSG_BEGIN();
120
121         if (fp == NULL)
122                 THROW(MsgException::INVALID_PARAM, "fp is NULL");
123
124         MsgPlgToken tokTitle, tokMsgType, tokLibPath;
125
126         char str[256];
127         memset(str, 0x00, sizeof(str));
128
129         MsgPlgToken tok = MsgPlgToken();
130
131         while (fgets(str, 255, fp))
132         {
133                 tok.tokenize(str); // parsing the line into tokens
134
135                 switch ( tok.getType() )
136                 {
137                         case MsgPlgToken::TOK_PLG_TITLE:
138                                 // processing previous items
139                                 if( tokTitle && tokMsgType && tokLibPath )
140                                 {
141                                         insert(tokTitle, tokMsgType, tokLibPath);
142                                 }
143
144                                 tokTitle = tok;
145                                 tokMsgType.reset();
146                                 tokLibPath.reset();
147
148                                 break;
149
150                         case MsgPlgToken::TOK_PLG_TYPE:
151                                 tokMsgType = tok;
152                                 break;
153
154                         case MsgPlgToken::TOK_PLG_PATH:
155                                 tokLibPath = tok;
156                                 break;
157
158                         default:
159                                 MSG_DEBUG("the line \"%s\" is not accecpted", str);
160                                 break;
161                 }
162         }
163
164         if (tokTitle && tokMsgType && tokLibPath)
165         {
166                 insert(tokTitle, tokMsgType, tokLibPath);
167         }
168
169         MSG_END();
170 }
171
172
173 void MsgPlgConfig::insert(const MsgPlgToken& tokTitle, const MsgPlgToken& tokMsgType, const MsgPlgToken& tokLibPath)
174 {
175         MSG_BEGIN();
176
177         MsgPlgTokenVec item2add;
178         item2add.push_back(tokMsgType);
179         item2add.push_back(tokLibPath);
180
181         CharVector titleVec;
182         tokTitle.getVal(titleVec);
183         MsgConfigMap::iterator it=configMap.find(titleVec);
184
185         if (it == configMap.end())
186                 configMap.insert(std::make_pair(titleVec, item2add));
187         else
188                 THROW(MsgException::PLUGIN_ERROR, "duplicated plugin title");
189
190         MSG_DEBUG("item inserted");
191         MSG_DEBUG("token:%d,value:%s", tokTitle.getType(), tokTitle.getVal());
192         MSG_DEBUG("token:%d,value:%s", tokMsgType.getType(), tokMsgType.getVal());
193         MSG_DEBUG("token:%d,value:%s", tokLibPath.getType(), tokLibPath.getVal());
194
195         MSG_END();
196 }
197
198
199 const CharVector& MsgPlgConfig::title(unsigned int pos)
200 {
201         if (pos >= configMap.size() || pos < 0)
202                 THROW(MsgException::OUT_OF_RANGE, "Input Parameter is not valid [%d]", pos);
203
204         MsgConfigMap::iterator it = configMap.begin();
205
206         unsigned int i=0;
207
208         while (it != configMap.end())
209         {
210                 if (i++ == pos) break;
211                 it++;
212         }
213
214         if (it == configMap.end())
215                 THROW(MsgException::INVALID_RESULT, "no title");
216
217         MSG_DEBUG("searched title:%s", &(it->first)[0]);
218
219         return it->first;
220 }
221
222
223 void MsgPlgConfig::token(const CharVector& key, unsigned int pos, MsgPlgToken& retTok)
224 {
225         MsgConfigMap::iterator it = configMap.find(key);
226
227         if (it != configMap.end()) //found
228         {
229                 MSG_DEBUG("searched title:%s", &(it->first)[0]);
230
231                 MsgPlgTokenVec tokVec = it->second;
232                 retTok = tokVec[pos];
233
234                 MSG_DEBUG("searched token:%d,value:%s", retTok.getType(), retTok.getVal());
235         }
236         else
237         {
238                 THROW(MsgException::INVALID_RESULT, "no title");
239         }
240 }
241
242
243 void MsgPlgConfig::token(int i, unsigned int pos, MsgPlgToken& retTok)
244 {
245         const CharVector& key = title(i);
246
247         token(key, pos, retTok);
248
249         MSG_DEBUG("returned token:%d,value:%s", retTok.getType(), retTok.getVal());
250 }
251