• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 #ifndef FILE_FILTER_H
16 #define FILE_FILTER_H
17 
18 #include <string>
19 #include <vector>
20 
21 #include "parcel.h"
22 
23 namespace OHOS {
24 namespace FileManagement {
25 namespace ModuleFileIO {
26 class FileFilter : public Parcelable {
27 public:
28     FileFilter() = default;
29     ~FileFilter() = default;
30 
FileFilter(std::vector<std::string> suffix)31     explicit FileFilter(std::vector<std::string> suffix): suffix_(suffix) {}
32     FileFilter(const FileFilter &filter) = default;
33     FileFilter &operator=(const FileFilter& filter) = default;
34 
SetSuffix(const std::vector<std::string> & suffix)35     void SetSuffix(const std::vector<std::string> &suffix)
36     {
37         suffix_ = suffix;
38     }
39 
GetSuffix()40     std::vector<std::string> GetSuffix() const
41     {
42         return suffix_;
43     }
44 
SetDisplayName(const std::vector<std::string> & displayName)45     void SetDisplayName(const std::vector<std::string> &displayName)
46     {
47         displayName_ = displayName;
48     }
49 
GetDisplayName()50     std::vector<std::string> GetDisplayName() const
51     {
52         return displayName_;
53     }
54 
SetMimeType(const std::vector<std::string> & mimeType)55     void SetMimeType(const std::vector<std::string> &mimeType)
56     {
57         mimeType_ = mimeType;
58     }
59 
GetMimeType()60     std::vector<std::string> GetMimeType() const
61     {
62         return mimeType_;
63     }
64 
SetFileSizeOver(const int64_t & fileSizeOver)65     void SetFileSizeOver(const int64_t &fileSizeOver)
66     {
67         fileSizeOver_ = fileSizeOver;
68     }
69 
GetFileSizeOver()70     int64_t GetFileSizeOver() const
71     {
72         return fileSizeOver_;
73     }
74 
SetLastModifiedAfter(const double & lastModifiedAfter)75     void SetLastModifiedAfter(const double &lastModifiedAfter)
76     {
77         lastModifiedAfter_ = lastModifiedAfter;
78     }
79 
GetLastModifiedAfter()80     double GetLastModifiedAfter() const
81     {
82         return lastModifiedAfter_;
83     }
84 
SetExcludeMedia(const bool & excludeMedia)85     void SetExcludeMedia(const bool &excludeMedia)
86     {
87         excludeMedia_ = excludeMedia;
88     }
89 
GetExcludeMedia()90     bool GetExcludeMedia() const
91     {
92         return excludeMedia_;
93     }
94 
SetHasFilter(const bool & hasFilter)95     void SetHasFilter(const bool &hasFilter)
96     {
97         hasFilter_ = hasFilter;
98     }
99 
GetHasFilter()100     bool GetHasFilter() const
101     {
102         return hasFilter_;
103     }
104 
Marshalling(Parcel & parcel)105     bool Marshalling(Parcel &parcel) const
106     {
107         if (!parcel.WriteStringVector(suffix_)) {
108             return false;
109         }
110         if (!parcel.WriteStringVector(displayName_)) {
111             return false;
112         }
113         if (!parcel.WriteStringVector(mimeType_)) {
114             return false;
115         }
116         if (!parcel.WriteInt64(fileSizeOver_)) {
117             return false;
118         }
119         if (!parcel.WriteDouble(lastModifiedAfter_)) {
120             return false;
121         }
122         if (!parcel.WriteBool(excludeMedia_)) {
123             return false;
124         }
125         if (!parcel.WriteBool(hasFilter_)) {
126             return false;
127         }
128         return true;
129     }
130 
Unmarshalling(Parcel & parcel)131     static FileFilter* Unmarshalling(Parcel &parcel)
132     {
133         auto obj = new (std::nothrow) FileFilter();
134         if (obj != nullptr && !obj->ReadFromParcel(parcel)) {
135             delete obj;
136             obj = nullptr;
137         }
138         return obj;
139     }
140 
ReadFromParcel(Parcel & parcel)141     bool ReadFromParcel(Parcel &parcel)
142     {
143         if (!parcel.ReadStringVector(&suffix_)) {
144             return false;
145         }
146         if (!parcel.ReadStringVector(&displayName_)) {
147             return false;
148         }
149         if (!parcel.ReadStringVector(&mimeType_)) {
150             return false;
151         }
152         fileSizeOver_ = parcel.ReadInt64();
153         lastModifiedAfter_  = parcel.ReadDouble();
154         excludeMedia_ = parcel.ReadBool();
155         hasFilter_ = parcel.ReadBool();
156         return true;
157     }
158 
FilterClear()159     void FilterClear()
160     {
161         this->suffix_.clear();
162         this->displayName_.clear();
163         this->mimeType_.clear();
164         this->fileSizeOver_ = 0;
165         this->lastModifiedAfter_ = 0;
166         this->excludeMedia_ = false;
167         this->hasFilter_ = false;
168     }
169 
170 private:
171     std::vector<std::string> suffix_ = std::vector<std::string>();
172     std::vector<std::string> displayName_ = std::vector<std::string>();
173     std::vector<std::string> mimeType_ = std::vector<std::string>();
174     int64_t fileSizeOver_ = 0;
175     double lastModifiedAfter_ = 0;
176     bool excludeMedia_ = false;
177     bool hasFilter_ = false;
178 };
179 
180 class FileFilterBuilder {
181 public:
182     FileFilterBuilder() = default;
183     ~FileFilterBuilder() = default;
184 
SetSuffix(const std::vector<std::string> & suffix)185     FileFilterBuilder& SetSuffix(const std::vector<std::string> &suffix)
186     {
187         fileFilter_.SetSuffix(suffix);
188         return *this;
189     }
190 
SetDisplayName(const std::vector<std::string> & displayName)191     FileFilterBuilder& SetDisplayName(const std::vector<std::string> &displayName)
192     {
193         fileFilter_.SetDisplayName(displayName);
194         return *this;
195     }
196 
SetMimeType(const std::vector<std::string> & mimeType)197     FileFilterBuilder& SetMimeType(const std::vector<std::string> &mimeType)
198     {
199         fileFilter_.SetMimeType(mimeType);
200         return *this;
201     }
202 
SetFileSizeOver(const int64_t & fileSizeOver)203     FileFilterBuilder& SetFileSizeOver(const int64_t &fileSizeOver)
204     {
205         fileFilter_.SetFileSizeOver(fileSizeOver);
206         return *this;
207     }
208 
SetLastModifiedAfter(const double & lastModifiedAfter)209     FileFilterBuilder& SetLastModifiedAfter(const double &lastModifiedAfter)
210     {
211         fileFilter_.SetLastModifiedAfter(lastModifiedAfter);
212         return *this;
213     }
214 
SetExcludeMedia(const bool & excludeMedia)215     FileFilterBuilder& SetExcludeMedia(const bool &excludeMedia)
216     {
217         fileFilter_.SetExcludeMedia(excludeMedia);
218         return *this;
219     }
220 
SetHasFilter(const bool & hasFilter)221     FileFilterBuilder& SetHasFilter(const bool &hasFilter)
222     {
223         fileFilter_.SetHasFilter(hasFilter);
224         return *this;
225     }
226 
Build()227     FileFilter Build()
228     {
229         return fileFilter_;
230     }
231 
232 private:
233     FileFilter fileFilter_;
234 };
235 } // namespace ModuleFileIO
236 } // namespace FileManagement
237 } // namespace OHOS
238 #endif // FILE_FILTER_H