3
* This is a simple action for posting to twitter using GNOME DO.
5
* GNOME Do is the legal property of its developers. Please refer to the
6
* COPYRIGHT file distributed with this source distribution.
8
* This program is free software: you can redistribute it and/or modify
9
* it under the terms of the GNU General Public License as published by
10
* the Free Software Foundation, either version 3 of the License, or
11
* (at your option) any later version.
13
* This program is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
* GNU General Public License for more details.
18
* You should have received a copy of the GNU General Public License
19
* along with this program. If not, see <http://www.gnu.org/licenses/>.
23
using System.Collections.Generic;
33
using org.freedesktop;
34
using org.freedesktop.DBus;
41
public class Twitter : IAction
50
public string Description {
52
return "Update Twitter Status";
58
return "twitter-icon.png@Twitter";
62
public Type[] SupportedItemTypes {
70
public bool SupportsItem (IItem item)
80
public IItem[] Perform (IItem[] items, IItem[] modItems)
83
String tweet = EscapeTweet ((items [0] as ITextItem).Text);
85
String url = "http://twitter.com/statuses/update.json?status=" + tweet;
86
HttpWebRequest request = WebRequest.Create (url) as HttpWebRequest;
88
request.Method = "POST";
90
GConf.Client gconf = new GConf.Client ();
92
if (!SetRequestCredentials (request, gconf)) return null;
93
if (!SetRequestProxy (request, gconf)) return null;
97
request.GetResponse ();
98
SendNotification ("Tweet Successful", "Successfully posted tweet '"
99
+ (items [0] as ITextItem).Text
103
} catch (Exception e) {
105
Console.WriteLine (e.ToString ());
106
SendNotification ("Tweet Failed", "Unable to post tweet. Check your login "
107
+ "settings (/apps/gnome-do/plugins/twitter). If you are "
108
+ "behind a proxy, also make sure that the settings in "
109
+ "/system/http_proxy are correct.\n\nDetails:\n"
116
public Type[] SupportedModifierItemTypes {
117
get { return new Type[] {}; }
120
public bool ModifierItemsOptional {
124
public bool SupportsModifierItemForItems (IItem[] items, IItem modItem)
129
public IItem[] DynamicModifierItemsForItem (IItem item)
139
private string EscapeTweet (string tweet) {
141
String retstring = tweet.Replace ("%", "%25")
142
.Replace ("#", "%23")
143
.Replace ("{", "%7B")
144
.Replace ("}", "%7D")
145
.Replace ("|", "%7C")
146
.Replace ("\\", "%5C")
147
.Replace ("^", "%5E")
148
.Replace ("~", "%7E")
149
.Replace ("[", "%5B")
150
.Replace ("]", "%5D")
151
.Replace ("`", "%60")
152
.Replace (";", "%3B")
153
.Replace (")", "%29")
154
.Replace ("/", "%2F");
156
retstring = tweet.Replace ("(", "%28")
157
.Replace ("?", "%3F")
158
.Replace (":", "%3A")
159
.Replace ("@", "%40")
160
.Replace ("=", "%3D")
161
.Replace ("&", "%26")
162
.Replace ("$", "%24")
163
.Replace ("\"", "%22")
164
.Replace ("'", "%27")
165
.Replace ("*", "%2A")
166
.Replace ("+", "%2B")
167
.Replace ("!", "%21")
174
private bool SetRequestCredentials (HttpWebRequest request, GConf.Client gconf)
181
username = gconf.Get ("/apps/gnome-do/plugins/twitter/username") as String;
182
password = gconf.Get ("/apps/gnome-do/plugins/twitter/password") as String;
184
catch (GConf.NoSuchKeyException) {
185
gconf.Set ("/apps/gnome-do/plugins/twitter/username", "");
186
gconf.Set ("/apps/gnome-do/plugins/twitter/password", "");
187
SendNotification ("GConf keys created", "GConf keys for storing your Twitter "
188
+ "login information have been created "
189
+ "in /apps/gnome-do/plugins/twitter/\n"
190
+ "Please set your username and password "
191
+ "in order to post tweets");
195
request.Credentials = new NetworkCredential (username, password);
201
private bool SetRequestProxy (HttpWebRequest request, GConf.Client gconf)
207
use_proxy = (bool) gconf.Get ("/system/http_proxy/use_http_proxy");
209
catch (GConf.NoSuchKeyException) {
222
phost = gconf.Get ("/system/http_proxy/host") as String;
223
pport = (int) gconf.Get ("/system/http_proxy/port");
224
pignore = gconf.Get ("/system/http_proxy/ignore_hosts") as String[];
225
pauth = (bool) gconf.Get ("/system/http_proxy/use_authentication");
228
catch (GConf.NoSuchKeyException) {
230
SendNotification ("Unable to load proxy settings",
231
"You have specified in GConf that "
232
+ "you are using a proxy server, but no proxy "
233
+ "settings could be found. Please check "
234
+ "/system/http_proxy/ to make sure the "
235
+ "appropriate values are set.");
241
WebProxy proxy = new WebProxy (phost, pport);
243
proxy.BypassList = pignore;
247
String pauser, papass;
251
pauser = gconf.Get ("/system/http_proxy/authentication_user") as String;
252
papass = gconf.Get ("/system/http_proxy/authentication_password") as String;
255
catch (GConf.NoSuchKeyException) {
257
SendNotification ("Unable to load proxy settings",
258
"You have specified in GConf that "
259
+ "you are using a proxy server, but no proxy "
260
+ "settings could be found. Please check "
261
+ "/system/http_proxy/ to make sure the "
262
+ "appropriate values are set.");
267
proxy.Credentials = new NetworkCredential (pauser, papass);
271
request.Proxy = proxy;
277
private void SendNotification (string title, string message)
280
Bus bus = Bus.Session;
282
Notifications nf = bus.GetObject<Notifications> ("org.freedesktop.Notifications", new ObjectPath ("/org/freedesktop/Notifications"));
284
Dictionary <string,object> hints = new Dictionary <string,object> ();
286
nf.Notify (title, 0, "", title, message, new string[0], hints, -1);