[tuxdroid-svn] r5764 - in software_suite_v3/software/plugin/plugin-facebook/branches/october_releas
Status: Beta
Brought to you by:
ks156
From: jerome <c2m...@c2...> - 2009-10-23 12:06:40
|
Author: jerome Date: 2009-10-23 14:06:17 +0200 (Fri, 23 Oct 2009) New Revision: 5764 Modified: software_suite_v3/software/plugin/plugin-facebook/branches/october_release/src/com/kysoh/plugins/facebook/FacebookConnection.java software_suite_v3/software/plugin/plugin-facebook/branches/october_release/src/com/kysoh/plugins/facebook/FacebookFunctions.java software_suite_v3/software/plugin/plugin-facebook/branches/october_release/src/com/kysoh/plugins/facebook/FacebookPlugin.java software_suite_v3/software/plugin/plugin-facebook/branches/october_release/src/com/kysoh/plugins/facebook/configuration/FacebookDatasStruct.java software_suite_v3/software/plugin/plugin-facebook/branches/october_release/src/com/kysoh/plugins/facebook/connection/FacebookSessionUtils.java Log: * Improved history system. * Fixed a bug with history id's. * Fixed a bug when the plugin was requesting too many logins. FB plugin now works with login / sessions. * Implemented 'Store session' function to store the last known session. Modified: software_suite_v3/software/plugin/plugin-facebook/branches/october_release/src/com/kysoh/plugins/facebook/FacebookConnection.java =================================================================== --- software_suite_v3/software/plugin/plugin-facebook/branches/october_release/src/com/kysoh/plugins/facebook/FacebookConnection.java 2009-10-22 06:28:16 UTC (rev 5763) +++ software_suite_v3/software/plugin/plugin-facebook/branches/october_release/src/com/kysoh/plugins/facebook/FacebookConnection.java 2009-10-23 12:06:17 UTC (rev 5764) @@ -39,6 +39,35 @@ public static final byte AUTH_READ_MAILBOX = 1; + /** + * Try to get connected to fb with the old registered session. + */ + private boolean connectOldSession() + { + try + { + FacebookSessionUtils.loadSession(); + String sessionKey = FacebookSessionUtils.getSessionKey(); + String sessionSecret = FacebookSessionUtils.getSessionSecret(); + + if(sessionKey.equals("0") || sessionSecret.equals("0")) + { + return false; + } + + client = new FacebookJsonRestClient(FacebookSessionUtils.api_key, sessionSecret, sessionKey); + client.users_getLoggedInUser(); + return true; + } + catch (FacebookException e) + { + ; + } + return false; + } + + + public boolean Connect(String username, String password) { FacebookSessionUtils.setMail(username); @@ -48,11 +77,19 @@ try { - + //Returning if old session was still valid. + if(this.connectOldSession()) + { + return true; + } + + FacebookSessionUtils.loadSession(); String sessionKey = FacebookSessionUtils.getValidSessionID( true ); + FacebookSessionUtils.registerSessionKey(sessionKey); //Generate session secret, ready to pass to desktop app client String sessionSecret = FacebookSessionUtils.lastClientUsed.getCacheSessionSecret(); + FacebookSessionUtils.registerSessionSecret(sessionSecret); client = new FacebookJsonRestClient(FacebookSessionUtils.api_key, sessionSecret, sessionKey); @@ -61,15 +98,17 @@ return false; } + FacebookSessionUtils.registerSession(); + return true; } catch (FacebookException e) { - e.printStackTrace(); + //e.printStackTrace(); } catch (IOException e) { - e.printStackTrace(); + //e.printStackTrace(); } return false; @@ -88,7 +127,6 @@ } catch (FacebookException e) { - // TODO Auto-generated catch block e.printStackTrace(); } return null; Modified: software_suite_v3/software/plugin/plugin-facebook/branches/october_release/src/com/kysoh/plugins/facebook/FacebookFunctions.java =================================================================== --- software_suite_v3/software/plugin/plugin-facebook/branches/october_release/src/com/kysoh/plugins/facebook/FacebookFunctions.java 2009-10-22 06:28:16 UTC (rev 5763) +++ software_suite_v3/software/plugin/plugin-facebook/branches/october_release/src/com/kysoh/plugins/facebook/FacebookFunctions.java 2009-10-23 12:06:17 UTC (rev 5764) @@ -34,7 +34,6 @@ import com.google.code.facebookapi.FacebookException; import com.google.code.facebookapi.FacebookJsonRestClient; -import com.kysoh.plugins.facebook.connection.FacebookSessionUtils; public class FacebookFunctions { @@ -42,6 +41,10 @@ private FacebookJsonRestClient client; private JSONObject notifications = null; + private Hashtable<String, String> events_id = new Hashtable<String, String>(); + private Hashtable<String, String> groups_id = new Hashtable<String, String>(); + private Hashtable<String, String> friends_id = new Hashtable<String, String>(); + /** * Class constructor. * @param connection @@ -165,6 +168,7 @@ name = ((JSONArray) client.fql_query(query_name + user_id)).getJSONObject( 0 ).getString("name"); result.add(name); + this.friends_id.put(name, user_id); } } @@ -209,7 +213,8 @@ group_id = String.valueOf(groups.get( x )); name = ((JSONArray) client.fql_query(query_name + group_id)).getJSONObject( 0 ).getString("name"); - result.add(name); + result.add(name); + this.groups_id.put(name, group_id); } } @@ -255,7 +260,9 @@ event_id = String.valueOf(events.get( x )); name = ((JSONArray) client.fql_query(query_name + event_id)).getJSONObject( 0 ).getString("name"); - result.add(name); + result.add(name); + //Registering event datas into hashtable. + this.events_id.put(name, event_id); } } @@ -275,8 +282,54 @@ } + /** + * Return the event id from its name. + * @param event_names + * @return the event id of an event name. + */ + public String getEventID(String event_name) + { + if(this.events_id.containsKey(event_name)) + { + return this.events_id.get(event_name); + } + return String.valueOf(0); + } + /** + * Return the group invite id from its name. + * @param group_name + * @return + */ + public String getGroupID(String group_name) + { + if(this.groups_id.containsKey(group_name)) + { + return this.groups_id.get(group_name); + } + return String.valueOf(0); + } + + + + /** + * Return a friend id based on his name. + * @param friend_name + * @return + */ + public String getFriendRequestID(String friend_name) + { + if(this.friends_id.containsKey(friend_name)) + { + return this.friends_id.get(friend_name); + } + return String.valueOf(0); + } + + + + /** * Return a Facebook name for the given uid. * @param uid * @return @@ -349,7 +402,7 @@ Iterator<String> it = baseHash.keySet().iterator(); - //Gettin all senders ( subject and message id are only for history stuff ). + //Getting all senders ( subject and message id are only for history stuff ). while(it.hasNext()) { senders.add(baseHash.get(it.next()).get(1)); @@ -372,25 +425,4 @@ return result; } - - - /** - * Return the current fb rights for the plugin. - * @return - */ - public Vector<String> getCurrentFBAuth() - { - Vector<String> rights = new Vector<String>(); - - try - { - System.out.println(client.permissions_checkAvailableApiAccess(FacebookSessionUtils.api_key)); - } - catch (FacebookException e) - { - ; - } - - return rights; - } } Modified: software_suite_v3/software/plugin/plugin-facebook/branches/october_release/src/com/kysoh/plugins/facebook/FacebookPlugin.java =================================================================== --- software_suite_v3/software/plugin/plugin-facebook/branches/october_release/src/com/kysoh/plugins/facebook/FacebookPlugin.java 2009-10-22 06:28:16 UTC (rev 5763) +++ software_suite_v3/software/plugin/plugin-facebook/branches/october_release/src/com/kysoh/plugins/facebook/FacebookPlugin.java 2009-10-23 12:06:17 UTC (rev 5764) @@ -29,6 +29,7 @@ import java.util.Vector; import com.kysoh.tuxdroid.plugin.framework.plugin.SimplePlugin; +import com.kysoh.plugins.facebook.configuration.FacebookDatasStruct; import com.kysoh.plugins.facebook.configuration.PluginFacebookConfiguration; import com.kysoh.plugins.facebook.FacebookConnection; @@ -37,7 +38,9 @@ private boolean check = false; private boolean throwed = false; + private FacebookFunctions functions; + private FacebookDatasStruct facebookHistory; /** * Throws the result in case of Check mode. @@ -64,6 +67,7 @@ else { this.RunOrCheck(); + System.exit(0); } } @@ -89,7 +93,6 @@ //Prompt web browser for facebook permissions. FacebookConnection.promptAuthorizations(options); - //TODO check actual permissions. } @@ -102,19 +105,11 @@ if ( facebook.Connect(this.configuration().getLogin(), this.configuration().getPassword()) ) { + //Facebook global functions object. functions = new FacebookFunctions(facebook.getClient()); - //Checking first if user has fb rights. - if(!this.getCommand().equalsIgnoreCase("check")) - { - //like gmail plugin, only in run mode. - functions.getCurrentFBAuth(); - - if(false) - { - this.doConfigure(); - } - } + //History object. + facebookHistory = new FacebookDatasStruct(this.configuration().getLogin()); //Updating user status. if(this.configuration().getUpdateStatus()) @@ -150,10 +145,19 @@ { this.throwResult(false); } + + + //Registering facebook history structure. + if(this.facebookHistory != null) + { + this.facebookHistory.registerStruct(); + } + } else { - this.throwMessage("I cannot get connected to your facebook account."); + if (!this.getCommand().equalsIgnoreCase("check")) + this.throwMessage("I cannot get connected to your facebook account."); } } @@ -184,19 +188,25 @@ Hashtable<String, Vector<String>> inbox = new Hashtable<String, Vector<String>>(); inbox = functions.getEmailNotifications(); - if(functions.getEmailNotifications().size() == 1) + Vector<String> keys = new Vector<String>(inbox.keySet()); + //Will contain all messages id's that must be added to the history. + Vector<String> historyCache = new Vector<String>(); + + //Removing email already triggered. + for(int x=0; x < keys.size(); x ++) { - throwRes(); - throwMessage("You have one new message"); + if(this.facebookHistory.isRegistered(FacebookDatasStruct.MESSAGES, keys.get( x ))) + { + inbox.remove(keys.get( x )); + } + else + { + historyCache.add( keys.get( x ) ); + } } - else if(inbox.size() > 1) + if (inbox.size() == 0) { - throwRes(); - this.throwMessage("You have {0} messages", inbox.size()); - } - else - { if (!this.getCommand().equalsIgnoreCase("check")) { this.throwMessage("You don't have any messages"); @@ -213,15 +223,25 @@ for(String sender : senders) { + + throwRes(); if (emailsBySender.get(sender).intValue() == 1) { - this.throwMessage("One from {0}", sender); + this.throwMessage("One message from {0}", sender); } else { this.throwMessage("{0} messages from {1}", sender, emailsBySender.get(sender)); } } + + //Registering history. + for(String message_id : historyCache) + { + this.facebookHistory.addMessage(message_id); + } + + this.facebookHistory.registerStruct(); } } @@ -231,24 +251,30 @@ */ private void throwFriendRequests() { + boolean said = false; Vector<String> f_requests = new Vector<String>(); f_requests = functions.getFriendRequests(); - if (f_requests.size() > 1) + if (f_requests.size() > 0) { - throwRes(); - this.throwMessage("You have {0} friend requests", f_requests.size()); for(String request : f_requests) { - this.throwMessage("From {0}", request); + String fid = functions.getFriendRequestID(request); + if(! facebookHistory.isRegistered(FacebookDatasStruct.FRIENDS_QUERIES, fid)) + { + throwRes(); + this.facebookHistory.addFriendRequest(fid); + this.throwMessage("{0} request to be your friend", request); + said = true; + } } + this.facebookHistory.registerStruct(); + + if((! this.getCommand().equalsIgnoreCase("check")) && (!said)) + { + this.throwMessage("You don't have any friend requests"); + } } - else if(f_requests.size() == 1) - { - throwRes(); - throwMessage("You have one friend request"); - this.throwMessage("From {0}", f_requests.get(0)); - } else { if (! this.getCommand().equalsIgnoreCase("check")) @@ -265,25 +291,31 @@ */ private void throwGroupInvites() { + boolean said = false; //Fetching group invites. Vector<String> g_invites = new Vector<String>(); g_invites = functions.getGroupInvites(); - if (g_invites.size() > 1) + if (g_invites.size() > 0) { - throwRes(); - this.throwMessage("You have {0} group invites", g_invites.size()); for(String group : g_invites) { - this.throwMessage("From {0}", group); + String gid = functions.getGroupID(group); + if(! facebookHistory.isRegistered(FacebookDatasStruct.GROUP_INVITES, gid)) + { + throwRes(); + this.facebookHistory.addGroupInvite(gid); + this.throwMessage("You are invited to the following group: {0}", group); + said = true; + } } + + this.facebookHistory.registerStruct(); + if ((! this.getCommand().equalsIgnoreCase("check")) && (!said)) + { + this.throwMessage("You don't have any group invites"); + } } - else if(g_invites.size() == 1) - { - throwRes(); - throwMessage("You have one group invite"); - this.throwMessage("From {0}", g_invites.get(0)); - } else { if (! this.getCommand().equalsIgnoreCase("check")) @@ -299,25 +331,31 @@ */ private void throwEventInvites() { + boolean said = false; //Fetching event invites. Vector<String> e_invites = new Vector<String>(); e_invites = functions.getEventInvites(); - if (e_invites.size() > 1) + if (e_invites.size() > 0) { - throwRes(); - this.throwMessage("You have {0} event invites", e_invites.size()); - for(String group : e_invites) + for(String event : e_invites) { - this.throwMessage("To {0}", group); + String eid = functions.getEventID( event ); + if(! facebookHistory.isRegistered(FacebookDatasStruct.EVENT_INVITE, eid)) + { + throwRes(); + this.facebookHistory.addEventInvite(functions.getEventID(event)); + this.throwMessage("You are invited to the following event: {0}", event); + said = true; + } } + + this.facebookHistory.registerStruct(); + if ((! this.getCommand().equalsIgnoreCase("check")) && (!said)) + { + this.throwMessage("You don't have any event invites"); + } } - else if(e_invites.size() == 1) - { - throwRes(); - throwMessage("You have one event invite"); - this.throwMessage("To {0}", e_invites.get(0)); - } else { if (! this.getCommand().equalsIgnoreCase("check")) @@ -349,6 +387,10 @@ @Override protected void onPluginStop() throws Throwable { - ; + //Registering facebook history structure. + if(this.facebookHistory != null) + { + this.facebookHistory.registerStruct(); + } } } Modified: software_suite_v3/software/plugin/plugin-facebook/branches/october_release/src/com/kysoh/plugins/facebook/configuration/FacebookDatasStruct.java =================================================================== --- software_suite_v3/software/plugin/plugin-facebook/branches/october_release/src/com/kysoh/plugins/facebook/configuration/FacebookDatasStruct.java 2009-10-22 06:28:16 UTC (rev 5763) +++ software_suite_v3/software/plugin/plugin-facebook/branches/october_release/src/com/kysoh/plugins/facebook/configuration/FacebookDatasStruct.java 2009-10-23 12:06:17 UTC (rev 5764) @@ -80,6 +80,7 @@ { if(this.vktStruct.size() <= 0) { + //Noting to be written. return; } @@ -107,7 +108,7 @@ try { FileInputStream fichier = new FileInputStream(this.structFile); - ObjectInputStream ois = new ObjectInputStream(fichier); + ObjectInputStream ois = new ObjectInputStream(fichier); return (SerializedVector) ois.readObject(); } catch (java.io.IOException e) @@ -125,44 +126,36 @@ /** * Add a message to the message hashtable. */ - public void addMessage(String uuid) + public void addMessage(String message_id) { - this.vktStruct.addMessage(uuid); + this.vktStruct.add(new String("mes:" + message_id)); } /** * Add a reply to the reply hashtable. */ - public void addEventInvite(String uuid) + public void addEventInvite(String event_id) { - this.vktStruct.addEventInvite(uuid); + this.vktStruct.add(new String("eid:" + event_id)); } /** * Add a status for selected friend. */ - public void addFriendRequest(String uuid) + public void addFriendRequest(String request_id) { - this.vktStruct.addFriendQuery(uuid); + this.vktStruct.add(new String("fid:" + request_id)); } - /** - * Add a reply to the reply hashtable. - */ - public void addPoke(String uuid) - { - this.vktStruct.addPoke(uuid); - } - /** * Add a status for selected friend. */ - public void addGroupInvite(String uuid) + public void addGroupInvite(String group_id) { - this.vktStruct.addGroupInvite(uuid); + this.vktStruct.add(new String("gid:" + group_id)); } @@ -181,26 +174,23 @@ * @param uuid. * @return */ - public boolean isRegistered(byte list, String uuid) + public boolean isRegistered(byte list, String id) { switch(list) { case FacebookDatasStruct.FRIENDS_QUERIES: - return vktStruct.FID.contains(uuid); + return vktStruct.contains("fid:" + id); case FacebookDatasStruct.MESSAGES: - return vktStruct.MES.contains(uuid); + return vktStruct.contains("mes:" + id); case FacebookDatasStruct.GROUP_INVITES: - return vktStruct.GID.contains(uuid); + return vktStruct.contains("gid:" + id); case FacebookDatasStruct.EVENT_INVITE: - return vktStruct.EID.contains(uuid); + return vktStruct.contains("eid:" + id); - case FacebookDatasStruct.POKES: - return vktStruct.POK.contains(uuid); - default: return false; } @@ -212,76 +202,15 @@ /** * Handle a serialized vector of vector<String> */ - class SerializedVector extends Vector<Object> implements Serializable{ + class SerializedVector extends Vector<String> implements Serializable{ - private static final long serialVersionUID = 7018170438526763010L; + private static final long serialVersionUID = 7018170438526763045L; - private Vector<String> GID = new Vector<String>(); - private Vector<String> FID = new Vector<String>(); - private Vector<String> POK = new Vector<String>(); - private Vector<String> EID = new Vector<String>(); - private Vector<String> MES = new Vector<String>(); public SerializedVector() { super(); } - /** - * Add a new message in the object. - */ - public void addMessage(String uuid) - { - if (! MES.contains(uuid)) - { - MES.add(uuid); - } - } - - - /** - * Add a new event invite in the object. - */ - public void addEventInvite(String uuid) - { - if (! EID.contains(uuid)) - { - EID.add(uuid); - } - } - - - /** - * Add a new user request in the object. - */ - public void addFriendQuery(String uuid) - { - if (! FID.contains(uuid)) - { - FID.add(uuid); - } - } - - /** - * Add a new user status in the object. - */ - public void addGroupInvite(String uuid) - { - if (! GID.contains(uuid)) - { - GID.add(uuid); - } - } - - /** - * Add a new poke notification in the object. - */ - public void addPoke(String uuid) - { - if (! POK.contains(uuid)) - { - POK.add(uuid); - } - } } } Modified: software_suite_v3/software/plugin/plugin-facebook/branches/october_release/src/com/kysoh/plugins/facebook/connection/FacebookSessionUtils.java =================================================================== --- software_suite_v3/software/plugin/plugin-facebook/branches/october_release/src/com/kysoh/plugins/facebook/connection/FacebookSessionUtils.java 2009-10-22 06:28:16 UTC (rev 5763) +++ software_suite_v3/software/plugin/plugin-facebook/branches/october_release/src/com/kysoh/plugins/facebook/connection/FacebookSessionUtils.java 2009-10-23 12:06:17 UTC (rev 5764) @@ -23,7 +23,14 @@ package com.kysoh.plugins.facebook.connection; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.io.Serializable; +import java.util.Vector; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpState; @@ -44,8 +51,17 @@ public static String lastTokenUsed; public static FacebookXmlRestClient lastClientUsed; + + + private static SerializedVector sessionUtils = new SerializedVector(); + static + { + sessionUtils.add("0"); + sessionUtils.add("1"); + } + public static void setMail(String eMail) { mail = eMail; @@ -71,7 +87,7 @@ final String LOGIN = "http://www.facebook.com/login.php"; GetMethod get = new GetMethod(LOGIN + "?api_key=" + api_key + "&v=1.0&auth_token=" + token ); - + http.executeMethod(get); PostMethod post = new PostMethod(LOGIN); @@ -88,4 +104,122 @@ lastClientUsed = client; return sessionID; } + + + /** + * Register the last known session secret. + * @param aSessionSecret + */ + public static void registerSessionSecret(String aSessionSecret) + { + sessionUtils.set(0, new String(aSessionSecret)); + } + + + /** + * Return the last used session secret. + * @return + */ + public static String getSessionSecret() + { + return (String) sessionUtils.get( 0 ); + } + + + /** + * Register the last known session key. + * @param aSessionKey + */ + public static void registerSessionKey(String aSessionKey) + { + sessionUtils.set(1, new String(aSessionKey)); + } + + + /** + * Return the last used session key. + * @return + */ + public static String getSessionKey() + { + return (String) sessionUtils.get( 1 ); + } + + + + /** + * Load the serialized vector containing old session datas. + */ + public static void loadSession() + { + try + { + File path = new File(""); + File session = new File( path.getAbsolutePath() + File.separator + "session"); + + if(session.isFile()) + { + FileInputStream fichier = new FileInputStream("session"); + ObjectInputStream ois = new ObjectInputStream(fichier); + sessionUtils = (SerializedVector) ois.readObject(); + } + else + { + sessionUtils = new SerializedVector(); + sessionUtils.add("0"); + sessionUtils.add("0"); + } + } + catch (java.io.IOException e) + { + e.printStackTrace(); + } + catch (ClassNotFoundException e) + { + e.printStackTrace(); + } + } + + + /** + * Register the current session as the last known. + */ + public static void registerSession() + { + if(sessionUtils.size() <= 0) + { + //Noting to be written. + return; + } + + try + { + File path = new File(""); + File session = new File( path.getAbsolutePath() + File.separator + "session"); + FileOutputStream file = new FileOutputStream(session.getAbsolutePath()); + ObjectOutputStream oos = new ObjectOutputStream(file); + oos.writeObject(sessionUtils); + oos.flush(); + oos.close(); + } + catch (java.io.IOException e) + { + e.printStackTrace(); + } + } + + /** + * Handle a serialized vector of vector<String> + */ + static class SerializedVector extends Vector<String> implements Serializable{ + + private static final long serialVersionUID = 7018170438526763036L; + + + public SerializedVector() + { + super(); + } + + } } |