1 /*
2 * Copyright (C) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "map_mse_folder.h"
17 #include <sstream>
18 #include "libxml/parser.h"
19
20 namespace OHOS {
21 namespace bluetooth {
MapMseFolder(const std::string & name)22 MapMseFolder::MapMseFolder(const std::string &name) : name_(name)
23 {}
24
MapMseFolder(const std::string & name,MapMseFolder & parent)25 MapMseFolder::MapMseFolder(const std::string &name, MapMseFolder &parent) : name_(name), parent_(&parent)
26 {}
27
~MapMseFolder()28 MapMseFolder::~MapMseFolder()
29 {
30 subFolders_.clear();
31 }
32
SetFolder(const std::string & name)33 MapMseFolder *MapMseFolder::SetFolder(const std::string &name)
34 {
35 if (subFolders_.find(name) == subFolders_.end()) {
36 subFolders_[name] = std::make_shared<MapMseFolder>(name, *this);
37 }
38 return subFolders_[name].get();
39 }
40
SetSmsMmsFolder(const std::string & name)41 MapMseFolder *MapMseFolder::SetSmsMmsFolder(const std::string &name)
42 {
43 MapMseFolder *folder = SetFolder(name);
44 return folder;
45 }
46
SetEmailFolder(int folderId,const std::string & name)47 MapMseFolder *MapMseFolder::SetEmailFolder(int folderId, const std::string &name)
48 {
49 MapMseFolder *folder = SetFolder(name);
50 folder->SetFolderId(folderId);
51 return folder;
52 }
53
SetImFolder(int folderId,const std::string & name)54 MapMseFolder *MapMseFolder::SetImFolder(int folderId, const std::string &name)
55 {
56 MapMseFolder *folder = SetFolder(name);
57 folder->SetFolderId(folderId);
58 return folder;
59 }
60
SetFolderId(const int folderId)61 void MapMseFolder::SetFolderId(const int folderId)
62 {
63 folderId_ = folderId;
64 }
65
GetParent() const66 MapMseFolder *MapMseFolder::GetParent() const
67 {
68 return parent_;
69 }
70
GetRoot(void)71 MapMseFolder *MapMseFolder::GetRoot(void)
72 {
73 root_ = this;
74 while (root_->parent_ != nullptr) {
75 root_ = root_->parent_;
76 }
77 return root_;
78 }
79
GetSubFolder(const std::string & folderName)80 MapMseFolder *MapMseFolder::GetSubFolder(const std::string &folderName)
81 {
82 if (subFolders_.find(folderName) != subFolders_.end()) {
83 return subFolders_[folderName].get();
84 }
85 return nullptr;
86 }
87
GetFolderByName(const std::string & name)88 MapMseFolder *MapMseFolder::GetFolderByName(const std::string &name)
89 {
90 MapMseFolder *folderElement = GetRoot();
91 folderElement = folderElement->GetSubFolder("telecom");
92 folderElement = folderElement->GetSubFolder("msg");
93 folderElement = folderElement->GetSubFolder(name);
94 return folderElement;
95 }
96
GetData(uint16_t offset,uint16_t count) const97 std::string MapMseFolder::GetData(uint16_t offset, uint16_t count) const
98 {
99 std::string data;
100 if (static_cast<std::size_t>(offset) > subFolders_.size()) {
101 return data;
102 }
103 auto limit = offset + count;
104 if (static_cast<std::size_t>(limit) > subFolders_.size()) {
105 limit = static_cast<int>(subFolders_.size());
106 }
107
108 xmlChar *xmlbuff;
109 int buffersize;
110 std::ostringstream oss;
111
112 xmlDocPtr doc = xmlNewDoc(BAD_CAST "1.0");
113 xmlNodePtr root_node = xmlNewNode(NULL, BAD_CAST "folder-listing");
114 xmlDocSetRootElement(doc, root_node);
115 xmlNewProp(root_node, BAD_CAST "version", BAD_CAST MAP_V10.c_str());
116
117 std::vector<std::string> folderNames;
118 for (auto &iter : subFolders_) {
119 folderNames.push_back(iter.second->GetName());
120 }
121
122 for (int i = static_cast<int>(offset); i < limit; i++) {
123 xmlNodePtr node = xmlNewNode(NULL, BAD_CAST"folder");
124 xmlAddChild(root_node, node);
125 xmlNewProp(node, BAD_CAST"name", BAD_CAST folderNames[i].c_str());
126 }
127 xmlDocDumpFormatMemory(doc, &xmlbuff, &buffersize, 1);
128 oss << xmlbuff;
129 xmlFree(xmlbuff);
130 xmlFreeDoc(doc);
131
132 return oss.str();
133 }
134
GetName() const135 std::string MapMseFolder::GetName() const
136 {
137 return name_;
138 }
139
GetFullPath(void) const140 std::string MapMseFolder::GetFullPath(void) const
141 {
142 std::string fullPath = name_;
143 auto temp = parent_;
144 while (temp != nullptr) {
145 fullPath.insert(0, temp->name_ + "/");
146 temp = temp->parent_;
147 }
148 return fullPath;
149 }
150
GetSubFolderSize(void) const151 uint16_t MapMseFolder::GetSubFolderSize(void) const
152 {
153 return static_cast<uint16_t>(subFolders_.size());
154 }
155
GetFolderId(void) const156 int MapMseFolder::GetFolderId(void) const
157 {
158 return folderId_;
159 }
160
IsIgnore(void) const161 bool MapMseFolder::IsIgnore(void) const
162 {
163 return ignore_;
164 }
165
SetIgnore(const bool ignore)166 void MapMseFolder::SetIgnore(const bool ignore)
167 {
168 ignore_ = ignore;
169 }
170 } // namespace bluetooth
171 } // namespace OHOS