~cszikszoy/do-plugins/google-map

« back to all changes in this revision

Viewing changes to Twitter/TwitterAction.cs

  • Committer: Alex Launi
  • Date: 2008-04-03 16:42:36 UTC
  • mfrom: (85 do-plugins)
  • mto: This revision was merged to the branch mainline in revision 87.
  • Revision ID: alex.launi@gmail.com-20080403164236-6ut2l710phmv92o5
clean up

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Twitter.cs
 
2
 *
 
3
 * This is a simple action for posting to twitter using GNOME DO.
 
4
 *
 
5
 * GNOME Do is the legal property of its developers. Please refer to the
 
6
 * COPYRIGHT file distributed with this source distribution.
 
7
 *
 
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.
 
12
 *
 
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.
 
17
 *
 
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/>.
 
20
 */
 
21
 
 
22
using System;
 
23
using System.Collections.Generic;
 
24
using System.IO;
 
25
using System.Net;
 
26
 
 
27
using Mono;
 
28
using Mono.Unix;
 
29
 
 
30
using GConf;
 
31
 
 
32
using NDesk.DBus;
 
33
using org.freedesktop;
 
34
using org.freedesktop.DBus;
 
35
 
 
36
using Do.Universe;
 
37
 
 
38
namespace Do.Twitter
 
39
{
 
40
    public class Twitter : IAction
 
41
    {
 
42
        public string Name {
 
43
           get {
 
44
               return "Tweet";
 
45
           }
 
46
        }
 
47
        
 
48
        public string Description {
 
49
            get {
 
50
                return "Update Twitter Status";
 
51
            }
 
52
        }
 
53
        
 
54
        public string Icon {
 
55
            get {
 
56
                return "twitter-icon.png@Twitter";
 
57
            }
 
58
        }
 
59
        
 
60
        public Type[] SupportedItemTypes {
 
61
            get {
 
62
                return new Type[] {
 
63
                    typeof (ITextItem),
 
64
                };
 
65
            }
 
66
        }
 
67
 
 
68
        public bool SupportsItem (IItem item) 
 
69
        {
 
70
            return true;
 
71
        }
 
72
 
 
73
        public IItem[] Perform (IItem[] items, IItem[] modItems)
 
74
        {
 
75
            string reply = "";
 
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";
 
82
 
 
83
            GConf.Client gconf = new GConf.Client ();
 
84
 
 
85
            if (!SetRequestCredentials (request, gconf)) return null;
 
86
            if (!SetRequestProxy (request, gconf)) return null;
 
87
            try {
 
88
                request.GetResponse ();
 
89
                SendNotification ("Tweet Successful", "Successfully posted tweet '" 
 
90
                                + (items [0] as ITextItem).Text
 
91
                                + "' to Twitter.");
 
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" 
 
98
                                + e.ToString ());
 
99
            }
 
100
            return null;
 
101
        }
 
102
        
 
103
        public Type[] SupportedModifierItemTypes {
 
104
            get { return new Type[] { typeof (TwitterFriendItem) }; }
 
105
        }
 
106
 
 
107
        public bool ModifierItemsOptional {
 
108
            get { return true; }
 
109
        }
 
110
                        
 
111
        public bool SupportsModifierItemForItems (IItem[] items, IItem modItem)
 
112
        {
 
113
            return true;
 
114
        }
 
115
        
 
116
        public IItem[] DynamicModifierItemsForItem (IItem item)
 
117
        {
 
118
            return null;
 
119
        }
 
120
 
 
121
        
 
122
 
 
123
        private bool SetRequestCredentials (HttpWebRequest request, GConf.Client gconf)
 
124
        {
 
125
            String username;
 
126
            String password;
 
127
 
 
128
            try {
 
129
                username = gconf.Get ("/apps/gnome-do/plugins/twitter/username") as String;
 
130
                password = gconf.Get ("/apps/gnome-do/plugins/twitter/password") as String;
 
131
            }
 
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");
 
140
                return false;
 
141
            }
 
142
            request.Credentials = new NetworkCredential (username, password);
 
143
            return true;
 
144
        }
 
145
 
 
146
        private bool SetRequestProxy (HttpWebRequest request, GConf.Client gconf)
 
147
        {
 
148
            bool use_proxy;
 
149
 
 
150
            try {
 
151
                    use_proxy = (bool) gconf.Get ("/system/http_proxy/use_http_proxy");
 
152
            }
 
153
            catch (GConf.NoSuchKeyException) {
 
154
                    use_proxy = false;
 
155
            }
 
156
 
 
157
            if (use_proxy) {
 
158
                String phost;
 
159
                int pport;
 
160
                String[] pignore;
 
161
                bool pauth;                
 
162
                try {
 
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");                
 
167
                }
 
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.");
 
175
                    return false;
 
176
                }
 
177
                WebProxy proxy = new WebProxy (phost, pport);
 
178
                proxy.BypassList = pignore;
 
179
                if (pauth) {
 
180
                    String pauser, papass;
 
181
                    try {
 
182
                        pauser = gconf.Get ("/system/http_proxy/authentication_user") as String;
 
183
                        papass = gconf.Get ("/system/http_proxy/authentication_password") as String;
 
184
                    }
 
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.");
 
192
                        return false;
 
193
                    }
 
194
                    proxy.Credentials = new NetworkCredential (pauser, papass);
 
195
                }
 
196
                request.Proxy = proxy;
 
197
            }
 
198
            return true;
 
199
        }
 
200
        
 
201
        private void SendNotification (string title, string message)
 
202
        {
 
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);
 
207
        }
 
208
    }
 
209
}