Imported Upstream version 0.8~alpha1
[platform/upstream/syncevolution.git] / src / client-api / src / c++ / windows / examples / testConfigFilter.cpp
1 /*
2  * Funambol is a mobile platform developed by Funambol, Inc. 
3  * Copyright (C) 2003 - 2007 Funambol, Inc.
4  * 
5  * This program is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU Affero General Public License version 3 as published by
7  * the Free Software Foundation with the addition of the following permission 
8  * added to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED
9  * WORK IN WHICH THE COPYRIGHT IS OWNED BY FUNAMBOL, FUNAMBOL DISCLAIMS THE 
10  * WARRANTY OF NON INFRINGEMENT  OF THIRD PARTY RIGHTS.
11  * 
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
15  * details.
16  * 
17  * You should have received a copy of the GNU Affero General Public License 
18  * along with this program; if not, see http://www.gnu.org/licenses or write to
19  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20  * MA 02110-1301 USA.
21  * 
22  * You can contact Funambol, Inc. headquarters at 643 Bair Island Road, Suite 
23  * 305, Redwood City, CA 94063, USA, or at email address info@funambol.com.
24  * 
25  * The interactive user interfaces in modified source and object code versions
26  * of this program must display Appropriate Legal Notices, as required under
27  * Section 5 of the GNU Affero General Public License version 3.
28  * 
29  * In accordance with Section 7(b) of the GNU Affero General Public License
30  * version 3, these Appropriate Legal Notices must retain the display of the
31  * "Powered by Funambol" logo. If the display of the logo is not reasonably 
32  * feasible for technical reasons, the Appropriate Legal Notices must display
33  * the words "Powered by Funambol".
34  */
35
36 #include <stdlib.h>
37
38 #include "base/fscapi.h"
39 #include "base/messages.h"
40 #include "base/Log.h"
41 #include "base/util/ArrayList.h"
42 #include "base/util/StringBuffer.h"
43 #include "client/SyncClient.h"
44 #include "client/DMTClientConfig.h"
45 #include "client/MailSourceManagementNode.h"
46 #include "examples/TestSyncSource.h"
47 #include "examples/TestSyncSource2.h"
48 #include "filter/AllClause.h"
49 #include "filter/ClauseUtil.h"
50 #include "filter/LogicalClause.h"
51 #include "filter/FieldClause.h"
52 #include "filter/sourceFilter.h"
53 #include "filter/WhereClause.h"
54 #include "spdm/constants.h"
55 #include "spds/spdsutils.h"
56 #include "spds/constants.h"
57 #include "spds/MailSyncSourceConfig.h"
58 #include "syncml/core/core.h"
59 #include "syncml/formatter/Formatter.h"
60 #include "base/globalsdef.h"
61
62 USE_NAMESPACE
63
64
65 void testConfigFilter() {
66
67     //
68     // Let's create the DM subtrees first
69     //
70     {
71         MailSyncSourceConfig config;
72
73         config.setDownloadAge(-1);
74         config.setBodySize(-1);
75         config.setAttachSize(-1);
76
77         MailSourceManagementNode node("sources", "mail", config);
78     }
79
80     //
81     // Now we can read the config and than create the needed clause
82     //
83     MailSourceManagementNode node("sources", "mail");
84     MailSyncSourceConfig& config = node.getMailSourceConfig(true);
85
86
87     //
88     // No filters!
89     //
90     MessageBox(0, TEXT("No filter"), TEXT("Config to filter"), MB_OK);
91
92     SourceFilter* filter =
93         ClauseUtil::createSourceFilter(NULL, config.getBodySize(),config.getAttachSize());
94
95     if (filter == NULL) {
96         MessageBox(0, TEXT("No filter is required!"), TEXT("Config to filter"), MB_OK);
97     } else {
98         MessageBox(0, TEXT("There must be an erro..."), TEXT("Config to filter"), MB_OK);
99     }
100
101     //
102     // Subject only
103     //
104     MessageBox(0, TEXT("Subject only"), TEXT("Config to filter"), MB_OK);
105
106     filter = ClauseUtil::createSourceFilter(NULL, 0, -1);
107
108     if (filter == NULL) {
109         MessageBox(0, TEXT("There must be an error..."), TEXT("Config to filter"), MB_OK);
110     } else {
111         Filter* f = ClauseUtil::toFilter(*filter);
112         if (f == NULL) {
113             MessageBox(0, TEXT("There must be an error..."), TEXT("Config to filter"), MB_OK);
114         } else {
115             StringBuffer* sb = Formatter::getFilter(f);
116             WCHAR *msg = toWideChar(sb->c_str());
117
118             MessageBox(0, msg, TEXT("Config to filter"), MB_OK);
119
120             delete sb;
121             delete [] msg;
122         }
123
124         if (filter) {
125             delete filter; filter = NULL;
126         }
127         if (f) {
128             delete f; f = NULL;
129         }
130     }
131
132
133     //
134     // 10Kb body, no attachment
135     //
136     MessageBox(0, TEXT("10Kb body, no attachment"), TEXT("Config to filter"), MB_OK);
137
138     filter = ClauseUtil::createSourceFilter(NULL, 10, 0);
139
140     if (filter == NULL) {
141         MessageBox(0, TEXT("There must be an error..."), TEXT("10Kb body, no attachment"), MB_OK);
142     } else {
143         Filter* f = ClauseUtil::toFilter(*filter);
144         if (f == NULL) {
145             MessageBox(0, TEXT("There must be an error..."), TEXT("10Kb body, no attachment"), MB_OK);
146         } else {
147             StringBuffer* sb = Formatter::getFilter(f);
148             WCHAR *msg = toWideChar(sb->c_str());
149             MessageBox(0, msg, TEXT("10Kb body, no attachment"), MB_OK);
150
151             delete sb;
152             delete [] msg;
153         }
154
155         if (filter) {
156             delete filter; filter = NULL;
157         }
158         if (f) {
159             delete f; f = NULL;
160         }
161     }
162
163     //
164     // 10Kb body, attachment up to 25Kb
165     //
166     MessageBox(0, TEXT("10Kb body, attachment up to 25Kb"), TEXT("Config to filter"), MB_OK);
167
168     filter = ClauseUtil::createSourceFilter(NULL, 10, 25);
169
170     if (filter == NULL) {
171         MessageBox(0, TEXT("There must be an error..."), TEXT("10Kb body, attachment up to 25Kb"), MB_OK);
172     } else {
173         Filter* f = ClauseUtil::toFilter(*filter);
174         if (f == NULL) {
175             MessageBox(0, TEXT("There must be an error..."), TEXT("10Kb body, attachment up to 25Kb"), MB_OK);
176         } else {
177             StringBuffer* sb = Formatter::getFilter(f);
178             WCHAR *msg = toWideChar(sb->c_str());
179
180             MessageBox(0, msg, TEXT("10Kb body, attachment up to 25Kb"), MB_OK);
181
182             delete sb;
183             delete [] msg;
184         }
185
186         if (filter) {
187             delete filter; filter = NULL;
188         }
189         if (f) {
190             delete f; f = NULL;
191         }
192     }
193
194     //
195     // TODAY's emails, no attachments
196     //
197     WCHAR* TODAY = TEXT("20051211T000000Z");
198     MessageBox(0, TEXT("TODAY's emails, no attachments"), TEXT("Config to filter"), MB_OK);
199
200     filter = ClauseUtil::createSourceFilter(TODAY, -1, 0);
201
202     if (filter == NULL) {
203         MessageBox(0, TEXT("There must be an error..."), TEXT("TODAY's emails, no attachments"), MB_OK);
204     } else {
205         Filter* f = ClauseUtil::toFilter(*filter);
206         if (f == NULL) {
207             MessageBox(0, TEXT("There must be an error..."), TEXT("TODAY's emails, no attachments"), MB_OK);
208         } else {
209             StringBuffer* sb = Formatter::getFilter(f);
210             WCHAR *msg = toWideChar(sb->c_str());
211
212             MessageBox(0, msg, TEXT("TODAY's emails, no attachments"), MB_OK);
213
214             if (sb) {
215                 delete sb; sb = NULL;
216             }
217             delete [] msg;
218         }
219
220         if (filter) {
221             delete filter; filter = NULL;
222         }
223         if (f) {
224             delete f; f = NULL;
225         }
226     }
227
228     //
229     // All in one reading from the DM
230     //
231     MessageBox(0, TEXT("All in one"), TEXT("Config to filter"), MB_OK);
232     {
233         MailSyncSourceConfig config;
234
235         config.setDownloadAge(3);
236         config.setBodySize(5);
237         config.setAttachSize(30);
238
239         MailSourceManagementNode node("sources", "mail", config);
240     }
241
242     config = node.getMailSourceConfig(true); // reread from the DM
243
244     filter = ClauseUtil::createSourceFilter(TODAY, config.getBodySize(), config.getAttachSize());
245
246     if (filter == NULL) {
247         MessageBox(0, TEXT("There must be an error..."), TEXT("All in one"), MB_OK);
248     } else {
249         Filter* f = ClauseUtil::toFilter(*filter);
250         if (f == NULL) {
251             MessageBox(0, TEXT("There must be an error..."), TEXT("All in one"), MB_OK);
252         } else {
253             StringBuffer* sb = Formatter::getFilter(f);
254             WCHAR *msg = toWideChar(sb->c_str());
255
256             MessageBox(0, msg, TEXT("All in one"), MB_OK);
257
258             if (sb) {
259                 delete sb; sb = NULL;
260             }
261             delete [] msg;
262         }
263
264         if (filter) {
265             delete filter; filter = NULL;
266         }
267         if (f) {
268             delete f; f = NULL;
269         }
270     }
271 }