Add Cristian Greco as uploader
[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>(str_hash, str_equal);
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>(str_hash, str_equal);
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                 Parser.init();
78                 var result = parse_status(data);
79                 Parser.cleanup();
80
81                 return result;
82         }
83         
84         private Status parse_status(string data) throws ParseError {
85                 Status status = new Status();
86                 Xml.Doc* xmlDoc = Parser.parse_memory(data, (int)data.size());
87                 if(xmlDoc == null)
88                         throw new ParseError.CODE("Invalid XML data");
89                 
90                 Xml.Node* rootNode = xmlDoc->get_root_element();
91                 string result = "";
92                 
93                 //changing locale to C
94                 string currentLocale = GLib.Intl.setlocale(GLib.LocaleCategory.TIME, null);
95                 GLib.Intl.setlocale(GLib.LocaleCategory.TIME, "C");
96                 
97                 Xml.Node* iter;
98                 for(iter = rootNode->children; iter != null; iter = iter->next) {
99                         if (iter->type != ElementType.ELEMENT_NODE)
100                                 continue;
101                         
102                         switch(iter->name) {
103                                 case "id":
104                                         status.id = iter->get_content();
105                                         break;
106                                 
107                                 case "created_at":
108                                         status.created_at = str_to_time(iter->get_content());
109                                         status.created_at_s = iter->get_content();
110                                         break;
111                                 
112                                 case "text":
113                                         status.text = iter->get_content();
114                                         break;
115                                                         
116                                 case "in_reply_to_screen_name":
117                                         status.to_user = iter->get_content();
118                                         break;
119                                 
120                                 case "in_reply_to_status_id":
121                                         status.to_status_id = iter->get_content();
122                                         break;
123                                 
124                                 case "retweeted_status":
125                                         status.is_retweet = true;
126                                         break;
127                                 
128                                 case "user":
129                                     Xml.Node *iter_user;
130                                     
131                                         for(iter_user = iter->children->next; iter_user != null; iter_user = iter_user->next) {
132                                                 switch(iter_user->name) {
133                                                         case "id":
134                                                         break;
135                                                 
136                                                 case "name":
137                                                         status.user_name = iter_user->get_content();
138                                                         break;
139                                                 
140                                                 case "screen_name":
141                                                         status.user_screen_name = iter_user->get_content();
142                                                         break;
143                                                 
144                                                 case "profile_image_url":
145                                                         status.user_avatar = iter_user->get_content();
146                                                         break;
147                                         }
148                                     } delete iter_user;                 
149                                         break;
150                         } delete iter;
151                 }
152                 
153                 //back to the normal locale
154                 GLib.Intl.setlocale(GLib.LocaleCategory.TIME, currentLocale);
155                 
156                 return status;
157         }
158 }
159
160 }