Workaround vala 0.10 bug LP 1250543
[moonshot-ui.git] / src / moonshot-provisioning-common.vala
1
2 namespace WebProvisioning
3
4   IdCard card;
5   IdCard[] cards;
6
7   bool
8   check_stack (SList<string> stack, string[] reference)
9   {
10     if (stack.length () < reference.length)
11       return false;
12     
13     for (int i = 0; i<reference.length; i++)
14     {
15       if (stack.nth_data(i) != reference[i])
16         return false;
17     }
18
19     return true;
20   }
21
22   bool
23   always_confirm_handler (SList<string> stack)
24   {
25     string[] always_confirm_path = {"always-confirm", "rule", "selection-rules", "identity", "identities"};
26     
27     return check_stack (stack, always_confirm_path);
28   }
29   
30   bool
31   pattern_handler (SList<string> stack)
32   {
33     string[] pattern_path = {"pattern", "rule", "selection-rules", "identity", "identities"};
34     
35     return check_stack (stack, pattern_path);
36   }
37
38   bool
39   server_cert_handler (SList<string> stack)
40   {
41     string[] server_cert_path = {"server-cert", "trust-anchor", "identity", "identities"};
42     
43     return check_stack (stack, server_cert_path);
44   }
45
46   bool
47   subject_alt_handler (SList<string> stack)
48   {
49     string[] subject_alt_path = {"subject-alt", "trust-anchor", "identity", "identities"};
50     
51     return check_stack (stack, subject_alt_path);
52   }
53
54   bool
55   subject_handler (SList<string> stack)
56   {
57     string[] subject_path = {"subject", "trust-anchor", "identity", "identities"};
58     
59     return check_stack (stack, subject_path);
60   }
61   
62   bool
63   ca_cert_handler (SList<string> stack)
64   {
65     string[] ca_path = {"ca-cert", "trust-anchor", "identity", "identities"};
66     
67     return check_stack (stack, ca_path);
68   }
69
70   bool
71   realm_handler (SList<string> stack)
72   {
73     string[] realm_path = {"realm", "identity", "identities"};
74     
75     return check_stack (stack, realm_path);
76   }
77
78   bool
79   password_handler (SList<string> stack)
80   {
81     string[] password_path = {"password", "identity", "identities"};
82     
83     return check_stack (stack, password_path);
84   }
85
86   bool
87   user_handler (SList<string> stack)
88   {
89     string[] user_path = {"user", "identity", "identities"};
90     
91     return check_stack (stack, user_path);
92   }
93
94   bool
95   display_name_handler (SList<string> stack)
96   {
97     string[] display_name_path = {"display-name", "identity", "identities"};
98     
99     return check_stack (stack, display_name_path);
100   }
101   
102   public void
103   start_element_func (MarkupParseContext context,
104                       string element_name,
105                       string[] attribute_names,
106                       string[] attribute_values) throws MarkupError
107   {
108     if (element_name == "identity")
109     {
110       IdCard[] tmp_cards = cards;
111
112       cards = new IdCard[tmp_cards.length + 1];
113       for (int i=0; i<tmp_cards.length; i++)
114       {
115         cards[i] = tmp_cards[i];
116       }
117       card = new IdCard();
118       cards[tmp_cards.length] = card;
119     }
120     else if (element_name == "rule")
121     {
122       Rule[] tmp_rules = card.rules;
123       card.rules = new Rule[tmp_rules.length + 1];
124       for (int i=0; i<tmp_rules.length; i++)
125       {
126         card.rules[i] = tmp_rules[i];
127       }
128       
129       card.rules[tmp_rules.length] = Rule();
130     }
131   }
132
133   public void
134   text_element_func (MarkupParseContext context,
135                      string             text,
136                      size_t             text_len) throws MarkupError
137   {
138     unowned SList<string> stack = context.get_element_stack ();
139     
140     if (text_len < 1)
141       return;
142     
143     if (stack.nth_data(0) == "display-name" && display_name_handler (stack))
144     {
145       card.display_name = text;
146     }
147     else if (stack.nth_data(0) == "user" && user_handler (stack))
148     {
149       card.username = text;
150     }
151     else if (stack.nth_data(0) == "password" && password_handler (stack))
152     {
153       card.password = text;
154     }
155     else if (stack.nth_data(0) == "realm" && realm_handler (stack))
156     {
157       card.issuer = text;
158     }
159     else if (stack.nth_data(0) == "service")
160     {
161       string[] services = card.services;
162       card.services = new string[services.length + 1];
163       for (int i = 0; i<services.length; i++)
164       {
165         card.services[i] = services[i];
166       }
167       card.services[services.length] = text;
168     }
169     /* Rules */
170     else if (stack.nth_data(0) == "pattern" && pattern_handler (stack))
171     {
172       /* use temp array to workaround valac 0.10 bug accessing array property length */ 
173       var temp = card.rules;
174       card.rules[temp.length - 1].pattern = text;
175     }
176     else if (stack.nth_data(0) == "always-confirm" && always_confirm_handler (stack))
177     {
178       if (text == "true" || text == "false") {
179         /* use temp array to workaround valac 0.10 bug accessing array property length*/
180         var temp = card.rules;
181         card.rules[temp.length - 1].always_confirm = text;
182       }
183     }
184     /*Trust anchor*/
185     else if (stack.nth_data(0) == "ca-cert" && ca_cert_handler (stack))
186     {
187       card.trust_anchor.ca_cert = text;
188     }
189     else if (stack.nth_data(0) == "subject" && subject_handler (stack))
190     {
191       card.trust_anchor.subject = text;
192     }
193     else if (stack.nth_data(0) == "subject-alt" && subject_alt_handler (stack))
194     {
195       card.trust_anchor.subject_alt = text;
196     }
197     else if (stack.nth_data(0) == "server-cert" && server_cert_handler (stack))
198     {
199       card.trust_anchor.server_cert = text;
200     }
201   }
202
203   class Parser
204   {
205     private MarkupParser parser;
206     private string       text;
207     private string       path;
208     public Parser (string path)
209     {
210       text = "";
211       this.path = path;
212
213       var file = File.new_for_path (path);
214     
215       try
216       {
217         var dis = new DataInputStream (file.read ());
218         string line;
219         while ((line = dis.read_line (null)) != null)
220           text += line;
221       }
222       catch (GLib.Error e)
223       {
224         error ("Could not retreive file size");
225       }
226       
227       parser = {start_element_func, null, text_element_func, null, null};
228     }
229     
230     public void
231     parse ()
232     {
233       var ctx = new MarkupParseContext(parser, 0, null, null);
234       
235       try
236       {
237         ctx.parse (text, text.length);
238       }
239       catch (GLib.Error e)
240       {
241         error ("Could not parse %s, invalid content", path);
242       } 
243     }
244   }
245 }