~luismmontielg/do-plugins/Facebook

« back to all changes in this revision

Viewing changes to Facebook/src/Facebook.cs

  • Committer: Luis Montiel
  • Date: 2009-03-04 23:34:47 UTC
  • Revision ID: luis@luis-laptop-20090304233447-lj63jdu3nj0akmn0
no more indexing walls and inbox

Show diffs side-by-side

added added

removed removed

Lines of Context:
94
94
                        username = Preferences.Username;
95
95
                        password = Preferences.Password;
96
96
                        statusText = "";
 
97
                        Facebook.facebook = new GoogleCode.FacebookCSharp.Facebook();
 
98
                        facebook.AutoFetchPictures=false;
 
99
                        facebook.AutoFetchProfiles= false;
 
100
                        facebook.GotChatMessage += new EventHandler<ChatMessageEventArgs>(Facebook.OnGotChatMessage);
 
101
                        facebook.OwnerUpdated += new EventHandler<OwnerEventArgs>(Facebook.OnOwnerUpdated);
 
102
                        facebook.FriendUpdated+= new EventHandler<FriendUpdatedEventArgs>(Facebook.OnFriendUpdated);
 
103
                        facebook.ExceptionOccured += new EventHandler<ExceptionEventArgs>(Facebook.OnException);
97
104
                        Thread thread = new Thread ((ThreadStart) (new FacebookConnectionThread(username, password).Connect));
98
105
                        thread.IsBackground = true;
99
106
                        thread.Start ();
122
129
                    Facebook.friends.Add(new FacebookContactItem(friend.UserID, friend.Name, friend.StatusText, user.UserID));          
123
130
                                }
124
131
                        }
125
 
                }
126
 
                
127
 
                public static void updateInbox()
128
 
                {
129
 
                        if(Facebook.facebook == null)
130
 
                                return;
131
 
                        if(!(Facebook.facebook.ConnectedToAPI || Facebook.facebook.ConnectedToWebsite)){
132
 
                                Log<Facebook>.Debug("Not connected to facebook network yet");
133
 
                                return;
134
 
                        }
135
 
                        if(Facebook.wallArray == null)
136
 
                                return;
137
 
                        lock(Facebook.inboxArray){
138
 
                                Facebook.inboxMessages.Clear();
139
 
                                foreach (InboxMessage msg in Facebook.inboxArray)
140
 
                    {
141
 
                       Facebook.inboxMessages.Add(new FacebookInboxMessageItem(msg.SenderID, msg.SenderName, msg.Subject, msg.Body));
142
 
                    }
143
 
                        }
144
 
                        Log<Facebook>.Debug("Found {0} mails...", Facebook.inboxMessages.Count);
145
 
                }
146
 
                
147
 
                public static void updateWall()
148
 
                {
149
 
                        if(Facebook.facebook == null)
150
 
                                return;
151
 
                        if(!(Facebook.facebook.ConnectedToAPI || Facebook.facebook.ConnectedToWebsite)){
152
 
                                Log<Facebook>.Debug("Not connected to facebook network yet");
153
 
                                return;
154
 
                        }
155
 
                        if(Facebook.wallArray == null)
156
 
                                return;
157
 
                        lock(Facebook.wallArray){
158
 
                                Facebook.wallPosts.Clear();
159
 
                                foreach (WallPost wall in Facebook.wallArray)
160
 
                    {
161
 
                       Facebook.wallPosts.Add(new FacebookWallPostItem(wall.Sender, wall.Contents));
162
 
                    }
163
 
                        }
164
 
                        Log<Facebook>.Debug("Found {0} wall posts...", Facebook.Wall.Count);
165
 
                }
166
 
                
 
132
                }               
167
133
                
168
134
                public static void updatePages()
169
135
                {
187
153
                        Facebook.Pages.Add(new FacebookPageItem("inbox"));
188
154
                        Facebook.Pages.Add(new FacebookPageItem("notifications"));
189
155
                        Facebook.Pages.Add(new FacebookPageItem("photo news"));
 
156
                        Facebook.Pages.Add(new FacebookPageItem("wall"));
190
157
                }
191
158
                
192
159
                public static void WriteOnWall(long id, string msg)
210
177
                
211
178
                public static bool TryConnect (string username, string password)
212
179
                {
213
 
                        try {
 
180
                        try 
 
181
                        {
214
182
                                GoogleCode.FacebookCSharp.Facebook fb = new GoogleCode.FacebookCSharp.Facebook();
215
183
                                fb.Login(username, password);
216
184
                                fb.Logout();
217
 
                        } catch (Exception e) {
 
185
                        } 
 
186
                        catch (Exception e) 
 
187
                        {
218
188
                                Log<Facebook>.Error (ConnectionErrorMessage);
219
189
                                return false;
220
190
                        }
 
191
                        Thread thread = new Thread ((ThreadStart) (new FacebookConnectionThread(username, password).Connect));
 
192
                        thread.IsBackground = true;
 
193
                        thread.Start ();
221
194
                        return true;
222
195
                }
223
196
                
224
197
                public static void Connect (string username, string password) 
225
198
                {
226
 
                        Log<Facebook>.Debug("Connecting to facebook network...");
227
 
                        if (string.IsNullOrEmpty (username) || string.IsNullOrEmpty (password)) {
228
 
                                Log<Facebook>.Error (MissingCredentialsMessage);
229
 
                                return;
230
 
                        }                       
231
 
                        try {
 
199
                        lock(facebook)
 
200
                        {
 
201
                                if((Facebook.facebook.ConnectedToAPI || Facebook.facebook.ConnectedToWebsite))
 
202
                                        return;
232
203
                                
233
 
                                Facebook.facebook = new GoogleCode.FacebookCSharp.Facebook();
234
 
                                facebook.AutoFetchPictures=false;
235
 
                                facebook.AutoFetchProfiles= false;
236
 
                                facebook.GotChatMessage += new EventHandler<ChatMessageEventArgs>(Facebook.OnGotChatMessage);
237
 
                                facebook.OwnerUpdated += new EventHandler<OwnerEventArgs>(Facebook.OnOwnerUpdated);
238
 
                                facebook.FriendUpdated+= new EventHandler<FriendUpdatedEventArgs>(Facebook.OnFriendUpdated);
239
 
                                facebook.ExceptionOccured += new EventHandler<ExceptionEventArgs>(Facebook.OnException);
240
 
                                Facebook.user = Facebook.facebook.Login(username, password);
241
 
                                Log<Facebook>.Debug("Connected");
242
 
                                Facebook.notifications.Notify(new LoginNotification(user.Name));
243
 
                                Facebook.notificationThread = new Thread(NotificationsThread);
244
 
                                Facebook.notificationThread.IsBackground = true;
245
 
                Facebook.notificationThread.Start();
246
 
                        } catch (Exception e) {
247
 
                                Log<Facebook>.Error (ConnectionErrorMessage);
 
204
                                Log<Facebook>.Debug("Connecting to facebook network...");
 
205
                                if (string.IsNullOrEmpty (username) || string.IsNullOrEmpty (password)) {
 
206
                                        Log<Facebook>.Error (MissingCredentialsMessage);
 
207
                                        return;
 
208
                                }
 
209
                                try 
 
210
                                {                               
 
211
                                        Facebook.user = Facebook.facebook.Login(username, password);
 
212
                                        Log<Facebook>.Debug("Connected");
 
213
                                        Facebook.notifications.Notify(new LoginNotification(user.Name));
 
214
                                        Facebook.notificationThread = new Thread(NotificationsThread);
 
215
                                        Facebook.notificationThread.IsBackground = true;
 
216
                        Facebook.notificationThread.Start();
 
217
                                } 
 
218
                                catch (Exception e) 
 
219
                                {
 
220
                                        Log<Facebook>.Error (ConnectionErrorMessage);
 
221
                                }
248
222
                        }
249
223
                }
250
224
                
269
243
                        if(e.FieldUpdated(UserField.Poked))
270
244
                                notifications.Notify(new PokeNotification());
271
245
                        if(e.FieldUpdated(UserField.StatusText)){
 
246
                                        Log<Facebook>.Debug("got an status update..");
272
247
                                        if(e.Owner.StatusText != null && !(user.StatusText.Equals(e.Owner.StatusText))){
273
248
                                                Log<Facebook>.Debug("New status text: "+e.Owner.StatusText);
274
 
                                                if(e.Owner.StatusText == "")
 
249
                                                if(e.Owner.StatusText == "" || e.Owner.StatusText.Length == 0)
275
250
                                                        return;                                         
276
251
                                                notifications.Notify(new StatusChangedNotification(e.Owner.Name, e.Owner.StatusText));
277
252
                                        }
293
268
                //Thread that checks for new walls and inbox messages
294
269
                private static void NotificationsThread()
295
270
                {
296
 
                        try{
 
271
                        try
 
272
                        {
 
273
                                if(!(Facebook.facebook.ConnectedToAPI || Facebook.facebook.ConnectedToWebsite))
 
274
                                        Log<Facebook>.Debug("Not connected to facebook network yet");
297
275
                                Log<Facebook>.Debug("Started notifications Thread");
298
276
                                Facebook.wallArray = facebook.GetWall(user.UserID);
299
 
                                Facebook.inboxArray = facebook.GetInbox(); 
300
 
                        }catch(Exception e){
301
 
                                Log<Facebook>.Error("Exception while fetching inbox or wall");
 
277
                                foreach (WallPost wall in Facebook.wallArray)
 
278
                       Facebook.wallPosts.Add(new FacebookWallPostItem(wall.Sender, wall.Contents));                                    
 
279
                                Facebook.inboxArray = facebook.GetInbox();
 
280
                                foreach (InboxMessage msg in Facebook.inboxArray)
 
281
                       Facebook.inboxMessages.Add(new FacebookInboxMessageItem(msg.SenderID, msg.SenderName, msg.Subject, msg.Body));
 
282
                        }
 
283
                        catch(Exception e)
 
284
                        {
 
285
                                Log<Facebook>.Error("Exception while trying to fetch inbox or wall");
302
286
                        }
303
287
                
304
288
                        while(true)
305
289
                        {
306
290
                                try
307
291
                                {
 
292
                                        if(!(Facebook.facebook.ConnectedToAPI || Facebook.facebook.ConnectedToWebsite))
 
293
                                                Log<Facebook>.Debug("Not connected to facebook network yet");
 
294
                                        if(wallArray == null)
 
295
                                        {
 
296
                                                Facebook.wallArray = facebook.GetWall(user.UserID);
 
297
                                                foreach (WallPost wall in Facebook.wallArray)
 
298
                                        Facebook.wallPosts.Add(new FacebookWallPostItem(wall.Sender, wall.Contents));                                   
 
299
                                        }
 
300
                                        if(inboxArray == null)
 
301
                                        {
 
302
                                                Facebook.inboxArray = facebook.GetInbox();
 
303
                                                foreach (InboxMessage msg in Facebook.inboxArray)
 
304
                                        Facebook.inboxMessages.Add(new FacebookInboxMessageItem(msg.SenderID, msg.SenderName, msg.Subject, msg.Body));
 
305
                                        }
 
306
                                                
308
307
                                        Log<Facebook>.Debug("NotificationsThread -> updating...");
 
308
                                        
 
309
                                        //Check for new walls
309
310
                                        WallPost[] newWalls = null;
310
311
                                        try{
311
312
                                                newWalls = facebook.GetWall(user.UserID);
325
326
                                        
326
327
                                        if(! (Facebook.wallArray[0].Contents.Equals(latestWall.Contents) &&  Facebook.wallArray[0].Sender.Name.Equals(latestWall.Sender.Name)) )
327
328
                                        {
328
 
                                                lock(Facebook.wallArray){
329
 
                                                        Facebook.wallArray = newWalls;
330
 
                                                }
 
329
                                                Facebook.wallArray = newWalls;
 
330
                                                wallPosts.Add(new FacebookWallPostItem(latestWall.Sender, latestWall.Contents));
331
331
                                                if(!(Facebook.wallArray[1].Contents.Equals(latestWall.Contents)))
332
332
                                                        Facebook.notifications.Notify(new WallNotification(latestWall.Sender.Name, latestWall.Contents));                               
333
333
                                        }
352
352
                                        
353
353
                                        if(! (Facebook.inboxArray[0].Body.Equals(latestMessage.Body) &&  Facebook.inboxArray[0].SenderName.Equals(latestMessage.SenderName)) )
354
354
                                        {
355
 
                                                lock(Facebook.inboxArray){
356
 
                                                        Facebook.inboxArray = newMessages;
357
 
                                                }
 
355
                                                Facebook.inboxArray = newMessages;
 
356
                                                Facebook.inboxMessages.Add(new FacebookInboxMessageItem(latestMessage.SenderID, latestMessage.SenderName, latestMessage.Subject, latestMessage.Body));
358
357
                                                if(!(Facebook.wallArray[1].Contents.Equals(latestWall.Contents)))
359
358
                                                        Facebook.notifications.Notify(new NewInboxNotification(latestMessage.SenderName, latestMessage.Subject, latestMessage.Body));                           
360
 
                                        }
361
 
                                        
362
 
                                        newMessages = null;
363
 
                                        latestMessage = null;
364
 
                                        
365
 
                                        newWalls = null;
366
 
                                        latestWall = null;
367
 
                                        
 
359
                                        }                                       
368
360
                                        Log<Facebook>.Debug("NotificationsThread -> sleeping...");
369
361
                                        //Will refresh in 35 seconds
370
362
                                        Thread.Sleep(35000);