message service dependency removed
[platform/core/messaging/email-service.git] / test / testapp-rule.c
1 /*
2  *  email-service
3  *
4  * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22
23
24 /* common header */
25 #include <stdio.h>
26 #include <string.h>
27 #include <stdlib.h>
28
29
30 /* open header */
31 #include <glib.h>
32
33 #include "email-api-rule.h"
34
35 /* internal header */
36 #include "testapp-utility.h"
37 #include "testapp-mailbox.h"
38
39 static gboolean testapp_test_add_rule()
40 {
41         email_rule_t*  rule = NULL;
42         int account_id = 0;
43         int target_mailbox_id = 0;
44         int action = 0;
45         int type = 0;
46         int flag = 0;
47         char arg[500];
48
49         rule = malloc(sizeof(email_rule_t));
50         testapp_print("> Enter account id: ");
51         if (0 >= scanf("%d", &account_id))
52                 testapp_print("Invalid input. ");
53         rule->account_id = account_id;
54
55         testapp_print("> Enter Type(FROM - 1 / SUBJECT - 2): ");
56         if (0 >= scanf("%d", &type))
57                 testapp_print("Invalid input. ");
58         rule->type = type;
59
60         memset(arg, 0x00, 500);
61         testapp_print("\n> Enter Filtering Value:");
62         if (0 >= scanf("%s", arg))
63                 testapp_print("Invalid input. ");
64         rule->value = strdup(arg);
65
66         testapp_print("> Enter Action(MOVE - 1, BLOCK - 2, DELETE - 3): ");
67         if (0 >= scanf("%d", &action))
68                 testapp_print("Invalid input. ");
69         rule->faction = action;
70
71         if (action == 1) {
72                 testapp_print("\n> Enter target mailbox id:");
73                 if (0 >= scanf("%d", &target_mailbox_id))
74                         testapp_print("Invalid input. ");
75                 rule->target_mailbox_id = target_mailbox_id;
76         }
77
78         testapp_print("> Enter Flag1 value [On/Off]: ");
79         if (0 >= scanf("%d", &flag))
80                 testapp_print("Invalid input. ");
81         rule->flag1 = flag;
82
83         testapp_print("> Enter Flag2 value [0:Exactly same as, 1:Include, 2:Compare Domain] : ");
84         if (0 >= scanf("%d", &flag))
85                 testapp_print("Invalid input. ");
86         rule->flag2 = flag;
87
88         if (email_add_rule(rule) < 0)
89                 testapp_print("\n email_add_rule failed");
90
91         testapp_print("ID of rule : [%d]\n", rule->filter_id);
92
93         if (email_apply_rule(rule->filter_id) != EMAIL_ERROR_NONE)
94                 testapp_print("email_apply_rule failed\n");
95
96         email_free_rule(&rule, 1);
97
98         return FALSE;
99 }
100
101 static gboolean testapp_test_delete_rule()
102 {
103         int filter_id = 0;
104
105         testapp_print("> Enter filter id: ");
106         if (0 >= scanf("%d", &filter_id))
107                 testapp_print("Invalid input. ");
108
109         if (email_delete_rule(filter_id) < 0)
110                 testapp_print("email_delete_rule failed..! ");
111
112         return FALSE;
113 }
114
115
116 static gboolean testapp_test_update_rule()
117 {
118         email_rule_t*  rule = NULL;
119         int account_id = 0;
120         int target_mailbox_id = 0;
121         int action = 0;
122         int type = 0;
123         int flag = 0;
124         char arg[500];
125         int filter_id = 0;
126
127         rule = malloc(sizeof(email_rule_t));
128         memset(rule, 0X00, sizeof(email_rule_t));
129         testapp_print("> Enter filter id: ");
130         if (0 >= scanf("%d", &filter_id))
131                 testapp_print("Invalid input. ");
132
133         testapp_print("> Enter account id: ");
134         if (0 >= scanf("%d", &account_id))
135                 testapp_print("Invalid input. ");
136         rule->account_id = account_id;
137
138         testapp_print("> Enter Type(FROM - 1 / SUBJECT - 2): ");
139         if (0 >= scanf("%d", &type))
140                 testapp_print("Invalid input. ");
141         rule->type = type;
142
143         memset(arg, 0x00, 500);
144         testapp_print("\n> Enter Filtering Value:");
145         if (0 >= scanf("%s", arg))
146                 testapp_print("Invalid input. ");
147         rule->value = strdup(arg);
148
149         testapp_print("> Enter Action(MOVE - 1, BLOCK - 2, DELETE - 3): ");
150         if (0 >= scanf("%d", &action))
151                 testapp_print("Invalid input. ");
152         rule->faction = action;
153
154         if (action == 1) {
155                 testapp_print("\n> Enter target mailbox id:");
156                 if (0 >= scanf("%d", &target_mailbox_id))
157                         testapp_print("Invalid input. ");
158                 rule->target_mailbox_id = target_mailbox_id;
159         }
160
161         testapp_print("> Enter Flag1 value: ");
162         if (0 >= scanf("%d", &flag))
163                 testapp_print("Invalid input. ");
164         rule->flag1 = flag;
165
166         testapp_print("> Enter Flag2 value: ");
167         if (0 >= scanf("%d", &flag))
168                 testapp_print("Invalid input. ");
169         rule->flag2 = flag;
170
171         if (email_update_rule(filter_id, rule) < 0)
172                 testapp_print("email_update_rule failed..! ");
173
174         email_free_rule(&rule, 1);
175
176         return FALSE;
177 }
178
179
180 static gboolean testapp_test_get_rule(void)
181 {
182         email_rule_t*  rule = NULL;
183         int filter_id = 0;
184
185         testapp_print("> Enter filter id: ");
186         if (0 >= scanf("%d", &filter_id))
187                 testapp_print("Invalid input. ");
188
189         if (email_get_rule(filter_id, &rule) >= 0)
190                 testapp_print("\n Got rule of account_id = %d and type = %d\n", rule->account_id, rule->type);
191
192         email_free_rule(&rule, 1);
193
194         return FALSE;
195
196 }
197
198 static gboolean testapp_test_get_rule_list(void)
199 {
200         int count, i;
201         email_rule_t* rule_list = NULL;
202
203         if (email_get_rule_list(&rule_list, &count) < 0) {
204                 testapp_print("   email_get_rule_list error\n");
205                 return false ;
206         }
207
208         for (i = 0; i < count; i++) {
209                 testapp_print("   %2d) Fileter_Id: %d | Account_id: %d  | Type: %d | Value %s \n", i + 1,
210                                 rule_list[i].filter_id,
211                                 rule_list[i].account_id,
212                                 rule_list[i].type,
213                                 rule_list[i].value);
214         }
215
216         email_free_rule(&rule_list, count);
217         return FALSE;
218
219 }
220
221
222
223 static gboolean testapp_test_interpret_command(int menu_number)
224 {
225         gboolean go_to_loop = TRUE;
226
227         switch (menu_number) {
228         case 1:
229                 testapp_test_add_rule();
230                 break;
231         case 2:
232                 testapp_test_delete_rule();
233                 break;
234         case 3:
235                 testapp_test_update_rule();
236                 break;
237         case 5:
238                 testapp_test_get_rule();
239                 break;
240         case 6:
241                 testapp_test_get_rule_list();
242                 break;
243
244         case 0:
245                 go_to_loop = FALSE;
246                 break;
247         default:
248                 break;
249         }
250
251         return go_to_loop;
252 }
253
254 void email_test_rule_main()
255 {
256         gboolean go_to_loop = TRUE;
257         int menu_number = 0;
258
259         while (go_to_loop) {
260                 testapp_show_menu(EMAIL_RULE_MENU);
261                 testapp_show_prompt(EMAIL_RULE_MENU);
262
263                 if (0 >= scanf("%d", &menu_number))
264                         testapp_print("Invalid input. ");
265
266                 go_to_loop = testapp_test_interpret_command(menu_number);
267         }
268 }
269