Tizen 2.0 Release
[platform/core/messaging/email-service.git] / utilities / test-application / 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         int result_from_scanf = 0;
49
50         rule = malloc(sizeof(email_rule_t));
51         testapp_print("> Enter account id: ");
52         result_from_scanf = scanf("%d", &account_id);
53         rule->account_id = account_id;
54
55         testapp_print("> Enter Type(FROM - 1 / SUBJECT - 2): ");
56         result_from_scanf = scanf("%d", &type);
57         rule->type= type;               
58
59         memset(arg, 0x00, 500);
60         testapp_print("\n> Enter Filtering Value:");
61         result_from_scanf = scanf("%s",arg);
62         rule->value= strdup(arg);       
63
64         testapp_print("> Enter Action(MOVE - 1, BLOCK - 2, DELETE - 3): ");
65         result_from_scanf = scanf("%d", &action);
66         rule->faction= action;  
67
68         if (action == 1) {
69                 testapp_print("\n> Enter target mailbox id:");
70                 result_from_scanf = scanf("%d", &target_mailbox_id);
71                 rule->target_mailbox_id = target_mailbox_id;
72         }
73
74         testapp_print("> Enter Flag1 value: ");
75         result_from_scanf = scanf("%d", &flag);
76         rule->flag1= flag;
77
78         testapp_print("> Enter Flag2 value: ");
79         result_from_scanf = scanf("%d", &flag);
80         rule->flag2= flag;
81
82         if ( email_add_rule(rule) < 0)
83                 testapp_print("\n email_add_rule failed");
84
85         
86         email_free_rule(&rule, 1);
87         
88         return FALSE;
89 }
90
91 static gboolean testapp_test_delete_rule()
92 {
93         int result_from_scanf = 0;
94         int filter_id = 0;
95
96         testapp_print("> Enter filter id: ");
97         result_from_scanf = scanf("%d", &filter_id);
98
99         if(email_delete_rule(filter_id) < 0)
100                 testapp_print("email_delete_rule failed..! ");
101                 
102         return FALSE;
103 }
104
105
106 static gboolean testapp_test_update_rule()
107 {
108         int result_from_scanf = 0;
109         email_rule_t*  rule = NULL;
110         int account_id = 0;
111         int target_mailbox_id = 0;
112         int action = 0;
113         int type = 0;
114         int flag = 0;
115         char arg[500];
116         int filter_id = 0;
117
118         rule = malloc(sizeof(email_rule_t));
119         memset(rule,0X00,sizeof(email_rule_t));
120         testapp_print("> Enter filter id: ");
121         result_from_scanf = scanf("%d", &filter_id);
122         
123         testapp_print("> Enter account id: ");
124         result_from_scanf = scanf("%d", &account_id);
125         rule->account_id = account_id;
126
127         testapp_print("> Enter Type(FROM - 1 / SUBJECT - 2): ");
128         result_from_scanf = scanf("%d", &type);
129         rule->type= type;               
130
131         memset(arg, 0x00, 500);
132         testapp_print("\n> Enter Filtering Value:");
133         result_from_scanf = scanf("%s",arg);
134         rule->value= strdup(arg);       
135
136         testapp_print("> Enter Action(MOVE - 1, BLOCK - 2, DELETE - 3): ");
137         result_from_scanf = scanf("%d", &action);
138         rule->faction= action;  
139
140         if (action == 1) {
141                 testapp_print("\n> Enter target mailbox id:");
142                 result_from_scanf = scanf("%d", &target_mailbox_id);
143                 rule->target_mailbox_id = target_mailbox_id;
144         }
145
146         testapp_print("> Enter Flag1 value: ");
147         result_from_scanf = scanf("%d", &flag);
148         rule->flag1= flag;
149
150         testapp_print("> Enter Flag2 value: ");
151         result_from_scanf = scanf("%d", &flag);
152         rule->flag2= flag;
153         
154         if( !email_update_rule(filter_id, rule) < 0)
155                 testapp_print("email_update_rule failed..! ");
156                 
157         email_free_rule(&rule, 1);
158                 
159         return FALSE;
160 }
161
162
163 static gboolean testapp_test_get_rule(void)
164 {
165         email_rule_t*  rule = NULL;
166         int filter_id = 0;
167         int result_from_scanf = 0;
168
169         testapp_print("> Enter filter id: ");
170         result_from_scanf = scanf("%d", &filter_id);
171
172         if(email_get_rule(filter_id, &rule) >= 0)       
173                 testapp_print("\n Got rule of account_id = %d and type = %d\n", rule->account_id, rule->type);
174
175         email_free_rule(&rule, 1);
176         
177         return FALSE;
178         
179 }
180
181 static gboolean testapp_test_get_rule_list      (void)
182 {
183         int count, i;
184         email_rule_t* rule_list=NULL;
185
186         if(email_get_rule_list(&rule_list, &count) < 0) {
187                 testapp_print("   email_get_rule_list error\n");
188                 return false ;
189         }
190         
191         for(i=0;i<count;i++){
192                 testapp_print("   %2d) Fileter_Id: %d | Account_id: %d  | Type: %d | Value %s \n", i+1, 
193                         rule_list[i].filter_id,
194                         rule_list[i].account_id, 
195                         rule_list[i].type,
196                         rule_list[i].value);
197         }
198
199         email_free_rule(&rule_list, count);
200         return FALSE;
201
202 }
203
204
205
206 static gboolean testapp_test_interpret_command (int menu_number)
207 {
208         gboolean go_to_loop = TRUE;
209         
210         switch (menu_number) {
211                 case 1:
212                         testapp_test_add_rule();
213                         break;
214                 case 2:
215                         testapp_test_delete_rule ();
216                         break;
217                 case 3:
218                         testapp_test_update_rule();
219                         break;
220                 case 5:
221                         testapp_test_get_rule ();
222                         break;
223                 case 6:
224                         testapp_test_get_rule_list();
225                         break;
226
227                 case 0:
228                         go_to_loop = FALSE;
229                         break;
230                 default:
231                         break;
232         }
233
234         return go_to_loop;
235 }
236
237 void email_test_rule_main()
238 {
239         gboolean go_to_loop = TRUE;
240         int menu_number = 0;
241         int result_from_scanf = 0;
242         
243         while (go_to_loop) {
244                 testapp_show_menu (EMAIL_RULE_MENU);
245                 testapp_show_prompt (EMAIL_RULE_MENU);
246                         
247                 result_from_scanf = scanf("%d", &menu_number);
248
249                 go_to_loop = testapp_test_interpret_command (menu_number);
250         }
251 }
252