Imported Upstream version 0.2.1
[debian/pino.git] / src / rest_api_re.vala
1 /* rest_api_re.vala
2  *
3  * Copyright (C) 2009-2010  troorl
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  *
18  * Author:
19  *      troorl <troorl@gmail.com>
20  */
21
22 using Gee;
23 using Soup;
24 using Auth;
25 using Xml;
26 using TimeUtils;
27
28 namespace RestAPI {
29
30 public class RestAPIRe : RestAPIAbstract {
31         
32         public RestAPIRe(Account? _account) {
33                 base(_account);
34         }
35         
36         public RestUrls get_urls() {
37                 return urls;
38         }
39         
40         /* send new dm */
41         public void send_dm(string user, string text) throws RestError {
42                 if(account == null)
43                         no_account();
44                 
45                 string req_url = urls.direct_new();
46                 
47                 var map = new HashTable<string, string>(null, null);
48                 map.insert("screen_name", user);
49                 map.insert("text", text);
50                 
51                 if(account != null && account.service == "identi.ca") //client name for identi.ca
52                         map.insert("source", Config.APPNAME);
53                 
54                 make_request(req_url, "POST", map);
55         }
56         
57         /* post new status */
58         public Status update_status(string text,
59                 string reply_id = "") throws RestError, ParseError {
60                 
61                 if(account == null)
62                         no_account();
63                 
64                 string req_url = urls.status_update();
65                 
66                 var map = new HashTable<string, string>(null, null);
67                 map.insert("status", text);
68                 
69                 if(account != null && account.service == "identi.ca") //client name for identi.ca
70                         map.insert("source", Config.APPNAME);
71                 
72                 if(reply_id != "")
73                         map.insert("in_reply_to_status_id", reply_id);
74                 
75                 string data = make_request(req_url, "POST", map);
76                 
77                 return parse_status(data);
78         }
79         
80         private Status parse_status(string data) {
81                 Status status = new Status();
82                 Xml.Doc* xmlDoc = Parser.parse_memory(data, (int)data.size());
83                 Xml.Node* rootNode = xmlDoc->get_root_element();
84                 string result = "";
85                 
86                 //changing locale to C
87                 string currentLocale = GLib.Intl.setlocale(GLib.LocaleCategory.TIME, null);
88                 GLib.Intl.setlocale(GLib.LocaleCategory.TIME, "C");
89                 
90                 Xml.Node* iter;
91                 for(iter = rootNode->children; iter != null; iter = iter->next) {
92                         if (iter->type != ElementType.ELEMENT_NODE)
93                                 continue;
94                         
95                         switch(iter->name) {
96                                 case "id":
97                                         status.id = iter->get_content();
98                                         break;
99                                 
100                                 case "created_at":
101                                         status.created_at = str_to_time(iter->get_content());
102                                         break;
103                                 
104                                 case "text":
105                                         status.text = iter->get_content();
106                                         break;
107                                                         
108                                 case "in_reply_to_screen_name":
109                                         status.to_user = iter->get_content();
110                                         break;
111                                 
112                                 case "in_reply_to_status_id":
113                                         status.to_status_id = iter->get_content();
114                                         break;
115                                 
116                                 case "retweeted_status":
117                                         status.is_retweet = true;
118                                         break;
119                                 
120                                 case "user":
121                                     Xml.Node *iter_user;
122                                     
123                                         for(iter_user = iter->children->next; iter_user != null; iter_user = iter_user->next) {
124                                                 switch(iter_user->name) {
125                                                         case "id":
126                                                         break;
127                                                 
128                                                 case "name":
129                                                         status.user_name = iter_user->get_content();
130                                                         break;
131                                                 
132                                                 case "screen_name":
133                                                         status.user_screen_name = iter_user->get_content();
134                                                         break;
135                                                 
136                                                 case "profile_image_url":
137                                                         status.user_avatar = iter_user->get_content();
138                                                         break;
139                                         }
140                                     } delete iter_user;                 
141                                         break;
142                         } delete iter;
143                 }
144                 
145                 //back to the normal locale
146                 GLib.Intl.setlocale(GLib.LocaleCategory.TIME, currentLocale);
147                 
148                 return status;
149         }
150 }
151
152 }