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;
40
public class Twitter : IAction
48
public string Description {
50
return "Update Twitter Status";
56
return "twitter-icon.png@Twitter";
60
public Type[] SupportedItemTypes {
68
public bool SupportsItem (IItem item)
73
public IItem[] Perform (IItem[] items, IItem[] modItems)
76
if (modItems.Length > 0)
77
reply = (modItems[0] as TwitterFriendItem).Description + " ";
78
string tweet = EscapeTweet ((items [0] as ITextItem).Text);
79
string url = "http://twitter.com/statuses/update.json?status=" + reply + tweet;
80
HttpWebRequest request = WebRequest.Create (url) as HttpWebRequest;
81
request.Method = "POST";
83
GConf.Client gconf = new GConf.Client ();
85
if (!SetRequestCredentials (request, gconf)) return null;
86
if (!SetRequestProxy (request, gconf)) return null;
88
request.GetResponse ();
89
SendNotification ("Tweet Successful", "Successfully posted tweet '"
90
+ (items [0] as ITextItem).Text
92
} catch (Exception e) {
93
Console.WriteLine (e.ToString ());
94
SendNotification ("Tweet Failed", "Unable to post tweet. Check your login "
95
+ "settings (/apps/gnome-do/plugins/twitter). If you are "
96
+ "behind a proxy, also make sure that the settings in "
97
+ "/system/http_proxy are correct.\n\nDetails:\n"
103
public Type[] SupportedModifierItemTypes {
104
get { return new Type[] { typeof (TwitterFriendItem) }; }
107
public bool ModifierItemsOptional {
111
public bool SupportsModifierItemForItems (IItem[] items, IItem modItem)
116
public IItem[] DynamicModifierItemsForItem (IItem item)
123
private bool SetRequestCredentials (HttpWebRequest request, GConf.Client gconf)
129
username = gconf.Get ("/apps/gnome-do/plugins/twitter/username") as String;
130
password = gconf.Get ("/apps/gnome-do/plugins/twitter/password") as String;
132
catch (GConf.NoSuchKeyException) {
133
gconf.Set ("/apps/gnome-do/plugins/twitter/username", "");
134
gconf.Set ("/apps/gnome-do/plugins/twitter/password", "");
135
SendNotification ("GConf keys created", "GConf keys for storing your Twitter "
136
+ "login information have been created "
137
+ "in /apps/gnome-do/plugins/twitter/\n"
138
+ "Please set your username and password "
139
+ "in order to post tweets");
142
request.Credentials = new NetworkCredential (username, password);
146
private bool SetRequestProxy (HttpWebRequest request, GConf.Client gconf)
151
use_proxy = (bool) gconf.Get ("/system/http_proxy/use_http_proxy");
153
catch (GConf.NoSuchKeyException) {
163
phost = gconf.Get ("/system/http_proxy/host") as String;
164
pport = (int) gconf.Get ("/system/http_proxy/port");
165
pignore = gconf.Get ("/system/http_proxy/ignore_hosts") as String[];
166
pauth = (bool) gconf.Get ("/system/http_proxy/use_authentication");
168
catch (GConf.NoSuchKeyException) {
169
SendNotification ("Unable to load proxy settings",
170
"You have specified in GConf that "
171
+ "you are using a proxy server, but no proxy "
172
+ "settings could be found. Please check "
173
+ "/system/http_proxy/ to make sure the "
174
+ "appropriate values are set.");
177
WebProxy proxy = new WebProxy (phost, pport);
178
proxy.BypassList = pignore;
180
String pauser, papass;
182
pauser = gconf.Get ("/system/http_proxy/authentication_user") as String;
183
papass = gconf.Get ("/system/http_proxy/authentication_password") as String;
185
catch (GConf.NoSuchKeyException) {
186
SendNotification ("Unable to load proxy settings",
187
"You have specified in GConf that "
188
+ "you are using a proxy server, but no proxy "
189
+ "settings could be found. Please check "
190
+ "/system/http_proxy/ to make sure the "
191
+ "appropriate values are set.");
194
proxy.Credentials = new NetworkCredential (pauser, papass);
196
request.Proxy = proxy;
201
private void SendNotification (string title, string message)
203
Bus bus = Bus.Session;
204
Notifications nf = bus.GetObject<Notifications> ("org.freedesktop.Notifications", new ObjectPath ("/org/freedesktop/Notifications"));
205
Dictionary <string,object> hints = new Dictionary <string,object> ();
206
nf.Notify (title, 0, "", title, message, new string[0], hints, -1);