[tuxdroid-svn] r6086 - in software_suite_v3/software/plugin/plugin-facebook/trunk: . resources src
Status: Beta
Brought to you by:
ks156
Author: jerome Date: 2010-03-08 14:15:31 +0100 (Mon, 08 Mar 2010) New Revision: 6086 Modified: software_suite_v3/software/plugin/plugin-facebook/trunk/ software_suite_v3/software/plugin/plugin-facebook/trunk/resources/plugin.xml software_suite_v3/software/plugin/plugin-facebook/trunk/src/com/kysoh/plugins/facebook/FacebookConnection.java software_suite_v3/software/plugin/plugin-facebook/trunk/src/com/kysoh/plugins/facebook/FacebookFunctions.java software_suite_v3/software/plugin/plugin-facebook/trunk/src/com/kysoh/plugins/facebook/FacebookPlugin.java software_suite_v3/software/plugin/plugin-facebook/trunk/src/com/kysoh/plugins/facebook/configuration/FacebookDatasStruct.java software_suite_v3/software/plugin/plugin-facebook/trunk/src/com/kysoh/plugins/facebook/configuration/PluginFacebookConfiguration.java software_suite_v3/software/plugin/plugin-facebook/trunk/src/com/kysoh/plugins/facebook/connection/FacebookSessionUtils.java Log: * Merged new_login branch. Property changes on: software_suite_v3/software/plugin/plugin-facebook/trunk ___________________________________________________________________ Added: svn:mergeinfo + /software_suite_v3/software/plugin/plugin-facebook/branches/new_login:5889-6085 Modified: software_suite_v3/software/plugin/plugin-facebook/trunk/resources/plugin.xml =================================================================== --- software_suite_v3/software/plugin/plugin-facebook/trunk/resources/plugin.xml 2010-03-08 13:07:36 UTC (rev 6085) +++ software_suite_v3/software/plugin/plugin-facebook/trunk/resources/plugin.xml 2010-03-08 13:15:31 UTC (rev 6086) @@ -21,12 +21,6 @@ description="Login" type="string" defaultValue="your login" /> - <parameter - category="Gadget parameters" - name="password" - description="Password" - type="password" - defaultValue="your password" /> <parameter category="Gadget parameters" name="notifyEmail" @@ -70,7 +64,8 @@ description="Run to check your Facebook account" /> <command name="check" - description="Check your Facebook account" /> + description="Check your Facebook account" + /> <command name="configure" description="Configure Tux Droid Facebook permissions" /> Modified: software_suite_v3/software/plugin/plugin-facebook/trunk/src/com/kysoh/plugins/facebook/FacebookConnection.java =================================================================== --- software_suite_v3/software/plugin/plugin-facebook/trunk/src/com/kysoh/plugins/facebook/FacebookConnection.java 2010-03-08 13:07:36 UTC (rev 6085) +++ software_suite_v3/software/plugin/plugin-facebook/trunk/src/com/kysoh/plugins/facebook/FacebookConnection.java 2010-03-08 13:15:31 UTC (rev 6086) @@ -38,7 +38,7 @@ /** * Try to get connected to fb with the old registered session. */ - private boolean connectOldSession(String username, String password) + private boolean connectOldSession(String username) { try { @@ -47,11 +47,7 @@ //Checking username. if(! username.equalsIgnoreCase(FacebookSessionUtils.getMail())) { - if(! password.equalsIgnoreCase(FacebookSessionUtils.getPass())) - { - //then a new user has logged in. - return false; - } + return false; } String sessionKey = FacebookSessionUtils.getSessionKey(); @@ -75,32 +71,37 @@ - public boolean Connect(String username, String password) throws FacebookLoginException + public boolean Connect(String username) throws FacebookLoginException { + //Checking for unique Facebook login process stuff. + if(! FacebookSessionUtils.isSingletonProgram("FacebookConnectFrame")) + { + return false; + } + FacebookSessionUtils.setMail(username); - FacebookSessionUtils.setPass(password); //Facebook connection stuff. try { //Returning if old session was still valid. - if(this.connectOldSession(username, password)) + if(this.connectOldSession(username)) { return true; } FacebookSessionUtils.loadSession(); String sessionKey = FacebookSessionUtils.getValidSessionID( true ); - FacebookSessionUtils.registerSessionKey(sessionKey); + FacebookSessionUtils.registerSessionKey(); //Generate session secret, ready to pass to desktop app client - String sessionSecret = FacebookSessionUtils.lastClientUsed.getCacheSessionSecret(); - FacebookSessionUtils.registerSessionSecret(sessionSecret); + String sessionSecret = FacebookSessionUtils.getSessionSecret(); + FacebookSessionUtils.registerSessionSecret(); client = new FacebookJsonRestClient(FacebookSessionUtils.api_key, sessionSecret, sessionKey); - if((sessionSecret == null) || (client == null)) + if((sessionSecret.equals("0")) || (client.equals(null))) { return false; } @@ -111,6 +112,7 @@ } catch (FacebookException e) { + e.printStackTrace(); if(e.getMessage().equalsIgnoreCase("invalid parameter")) { throw new FacebookLoginException(); @@ -118,7 +120,7 @@ } catch (IOException e) { - //e.printStackTrace(); + ; } return false; Modified: software_suite_v3/software/plugin/plugin-facebook/trunk/src/com/kysoh/plugins/facebook/FacebookFunctions.java =================================================================== --- software_suite_v3/software/plugin/plugin-facebook/trunk/src/com/kysoh/plugins/facebook/FacebookFunctions.java 2010-03-08 13:07:36 UTC (rev 6085) +++ software_suite_v3/software/plugin/plugin-facebook/trunk/src/com/kysoh/plugins/facebook/FacebookFunctions.java 2010-03-08 13:15:31 UTC (rev 6086) @@ -100,7 +100,7 @@ JSONObject messages = (JSONObject) notifications.get("messages"); return messages.getInt("unread"); } - catch (JSONException e) + catch (Exception e) { ; } Modified: software_suite_v3/software/plugin/plugin-facebook/trunk/src/com/kysoh/plugins/facebook/FacebookPlugin.java =================================================================== --- software_suite_v3/software/plugin/plugin-facebook/trunk/src/com/kysoh/plugins/facebook/FacebookPlugin.java 2010-03-08 13:07:36 UTC (rev 6085) +++ software_suite_v3/software/plugin/plugin-facebook/trunk/src/com/kysoh/plugins/facebook/FacebookPlugin.java 2010-03-08 13:15:31 UTC (rev 6086) @@ -42,6 +42,8 @@ private FacebookFunctions functions; private FacebookDatasStruct facebookHistory; + private String type = null; + /** * Throws the result in case of Check mode. */ @@ -67,7 +69,7 @@ if (this.getCommand().equalsIgnoreCase("configure")) { FacebookConnection facebook = new FacebookConnection(); - if ( facebook.Connect(this.configuration().getLogin(), this.configuration().getPassword()) ) + if ( facebook.Connect(this.configuration().getLogin()) ) { functions = new FacebookFunctions(facebook.getClient()); this.doConfigure(functions); @@ -99,12 +101,12 @@ public void RunOrCheck() { FacebookConnection facebook = new FacebookConnection(); - + type = this.getCommand().toUpperCase(); boolean connected = false; try { - connected = facebook.Connect(this.configuration().getLogin(), this.configuration().getPassword()); + connected = facebook.Connect(this.configuration().getLogin()); } catch (FacebookLoginException e) { @@ -274,10 +276,18 @@ for(String request : f_requests) { String fid = functions.getFriendRequestID(request); - if(! facebookHistory.isRegistered(FacebookDatasStruct.FRIENDS_QUERIES, fid)) + if(! facebookHistory.isRegistered(FacebookDatasStruct.FRIENDS_QUERIES, fid, type)) { throwRes(); - this.facebookHistory.addFriendRequest(fid); + if(this.getCommand().equalsIgnoreCase("check")) + { + this.facebookHistory.addFriendRequest(fid, "CHECK"); + } + else + { + this.facebookHistory.addFriendRequest(fid, "CHECK"); + this.facebookHistory.addFriendRequest(fid, "RUN"); + } //Only throwing in the second cycle of check command or run. if(this.getCommand().equalsIgnoreCase("run") || secondCycle) { @@ -319,10 +329,18 @@ for(String group : g_invites) { String gid = functions.getGroupID(group); - if(! facebookHistory.isRegistered(FacebookDatasStruct.GROUP_INVITES, gid)) + if(! facebookHistory.isRegistered(FacebookDatasStruct.GROUP_INVITES, gid, type)) { throwRes(); - this.facebookHistory.addGroupInvite(gid); + if(this.getCommand().equalsIgnoreCase("check")) + { + this.facebookHistory.addGroupInvite(gid, "CHECK"); + } + else + { + this.facebookHistory.addGroupInvite(gid, "CHECK"); + this.facebookHistory.addGroupInvite(gid, "RUN"); + } //Only throwing in the second cycle of check command or run. if(this.getCommand().equalsIgnoreCase("run") || secondCycle) { @@ -363,10 +381,18 @@ for(String event : e_invites) { String eid = functions.getEventID( event ); - if(! facebookHistory.isRegistered(FacebookDatasStruct.EVENT_INVITE, eid)) + if(! facebookHistory.isRegistered(FacebookDatasStruct.EVENT_INVITE, eid, type)) { throwRes(); - this.facebookHistory.addEventInvite(functions.getEventID(event)); + if(this.getCommand().equalsIgnoreCase("check")) + { + this.facebookHistory.addEventInvite(functions.getEventID(event), "CHECK"); + } + else + { + this.facebookHistory.addEventInvite(functions.getEventID(event), "CHECK"); + this.facebookHistory.addEventInvite(functions.getEventID(event), "RUN"); + } //Only throwing in the second cycle of check command or run. if(this.getCommand().equalsIgnoreCase("run") || secondCycle) { Modified: software_suite_v3/software/plugin/plugin-facebook/trunk/src/com/kysoh/plugins/facebook/configuration/FacebookDatasStruct.java =================================================================== --- software_suite_v3/software/plugin/plugin-facebook/trunk/src/com/kysoh/plugins/facebook/configuration/FacebookDatasStruct.java 2010-03-08 13:07:36 UTC (rev 6085) +++ software_suite_v3/software/plugin/plugin-facebook/trunk/src/com/kysoh/plugins/facebook/configuration/FacebookDatasStruct.java 2010-03-08 13:15:31 UTC (rev 6086) @@ -148,27 +148,27 @@ /** * Add a reply to the reply hash table. */ - public void addEventInvite(String event_id) + public void addEventInvite(String event_id, String type) { - this.vktStruct.add(new String("eid:" + event_id)); + this.vktStruct.add(new String("eid:" + event_id + "_" + type)); } /** * Add a status for selected friend. */ - public void addFriendRequest(String request_id) + public void addFriendRequest(String request_id, String type) { - this.vktStruct.add(new String("fid:" + request_id)); + this.vktStruct.add(new String("fid:" + request_id + "_" + type)); } /** * Add a status for selected friend. */ - public void addGroupInvite(String group_id) + public void addGroupInvite(String group_id, String type) { - this.vktStruct.add(new String("gid:" + group_id)); + this.vktStruct.add(new String("gid:" + group_id + "_" + type)); } @@ -187,22 +187,46 @@ * @param uuid. * @return */ - public boolean isRegistered(byte list, String id) + public boolean isRegistered(byte list, String id, String type) { switch(list) { case FacebookDatasStruct.FRIENDS_QUERIES: - return vktStruct.contains("fid:" + id); + if (type == "CHECK") + { + return ( vktStruct.contains("fid:" + id + "_" + "CHECK") || + vktStruct.contains("fid:" + id + "_" + "RUN")); + } + else + { + return vktStruct.contains("fid:" + id + "_" + type); + } case FacebookDatasStruct.MESSAGES: return vktStruct.contains("mes:" + id); case FacebookDatasStruct.GROUP_INVITES: - return vktStruct.contains("gid:" + id); + if (type == "CHECK") + { + return (vktStruct.contains("gid:" + id+ "_" + "CHECK") || + vktStruct.contains("gid:" + id+ "_" + "RUN")); + } + else + { + return vktStruct.contains("gid:" + id+ "_" + type); + } case FacebookDatasStruct.EVENT_INVITE: - return vktStruct.contains("eid:" + id); + if (type == "CHECK") + { + return (vktStruct.contains("eid:" + id+ "_" + "CHECK") || + vktStruct.contains("eid:" + id+ "_" + "RUN")); + } + else + { + return vktStruct.contains("eid:" + id+ "_" + type); + } default: return false; Modified: software_suite_v3/software/plugin/plugin-facebook/trunk/src/com/kysoh/plugins/facebook/configuration/PluginFacebookConfiguration.java =================================================================== --- software_suite_v3/software/plugin/plugin-facebook/trunk/src/com/kysoh/plugins/facebook/configuration/PluginFacebookConfiguration.java 2010-03-08 13:07:36 UTC (rev 6085) +++ software_suite_v3/software/plugin/plugin-facebook/trunk/src/com/kysoh/plugins/facebook/configuration/PluginFacebookConfiguration.java 2010-03-08 13:15:31 UTC (rev 6086) @@ -27,7 +27,6 @@ public class PluginFacebookConfiguration extends SimplePluginConfiguration{ private String login = ""; - private String password = ""; private boolean showFriendRequests = true; private boolean showGroupsInvites = true; @@ -99,26 +98,6 @@ /** - * Return tthe user password. - * @return - */ - public String getPassword() - { - return this.password; - } - - - /** - * Set the user password. - * @param aPassword - */ - public void setPassword(String aPassword) - { - this.password = aPassword; - } - - - /** * Return true if user want to hear his email notifications. * @return */ Modified: software_suite_v3/software/plugin/plugin-facebook/trunk/src/com/kysoh/plugins/facebook/connection/FacebookSessionUtils.java =================================================================== --- software_suite_v3/software/plugin/plugin-facebook/trunk/src/com/kysoh/plugins/facebook/connection/FacebookSessionUtils.java 2010-03-08 13:07:36 UTC (rev 6085) +++ software_suite_v3/software/plugin/plugin-facebook/trunk/src/com/kysoh/plugins/facebook/connection/FacebookSessionUtils.java 2010-03-08 13:15:31 UTC (rev 6086) @@ -22,7 +22,7 @@ package com.kysoh.plugins.facebook.connection; - +import java.awt.event.WindowEvent; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; @@ -30,10 +30,22 @@ import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; +import java.io.UnsupportedEncodingException; +import java.net.MalformedURLException; import java.util.Vector; +import javax.swing.ImageIcon; +import javax.swing.JFrame; + +import org.lobobrowser.gui.FramePanel; +import org.lobobrowser.main.PlatformInit; +import org.lobobrowser.ua.NavigationEvent; +import org.lobobrowser.ua.NavigationListener; +import org.lobobrowser.ua.NavigationVetoException; + import com.google.code.facebookapi.FacebookException; import com.google.code.facebookapi.FacebookXmlRestClient; +import com.kysoh.plugins.facebook.FacebookPlugin; public class FacebookSessionUtils { @@ -41,21 +53,26 @@ public static String s_key = "0314c8569f89d0411e5a4823372ae27a"; protected static String mail = ""; - protected static String pass = ""; public static String lastTokenUsed; public static FacebookXmlRestClient lastClientUsed; private static SerializedVector sessionUtils = new SerializedVector(); + + static boolean loggedIn = false; + static String session_key = null; + static String session_secret = null; + static JFrame frame; + static boolean waitFor = true; + private static File lockFile = null; static { sessionUtils.add("0"); sessionUtils.add("1"); sessionUtils.add(mail); - sessionUtils.add(pass); } public static void setMail(String eMail) @@ -65,13 +82,6 @@ } - public static void setPass(String aPass) - { - pass = aPass; - sessionUtils.add(aPass); - } - - public static String getValidSessionID( boolean generateSessionSecret) throws IOException, FacebookException { @@ -79,11 +89,148 @@ FacebookXmlRestClient client = new FacebookXmlRestClient( api_key, s_key ); String token = client.auth_createToken(); - + String url = "http://www.facebook.com/login.php?api_key="+ api_key + "&connect_display=popup&v=1.0"+ + "&next=http://www.facebook.com/connect/login_success.html" + + "&cancel_url=http://www.facebook.com/connect/login_failure.html&"+ + "fbconnect=true&return_session=true&session_key_only=true&email=" + mail; + + //Creating lobo browser panel. + try + { + PlatformInit.getInstance().initLogging(false); + PlatformInit.getInstance().init(false, false); + } + catch (Exception e2) { + ; + } + + frame = new JFrame(); + frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); + frame.setSize(600, 400); + frame.setLocationRelativeTo(null); + + frame.setTitle("Facebook Connect ( Tux Droid )"); + + FramePanel browser = new FramePanel(); + + frame.getContentPane().add(browser); + + frame.setVisible(true); + frame.setAlwaysOnTop( true ); + String loc = FacebookPlugin.class.getProtectionDomain().getCodeSource().getLocation().getPath(); + loc = loc.substring(0, loc.lastIndexOf("/")); + File f = new File(loc + "/../resources/plugin.png"); + ImageIcon img = new ImageIcon(f.getAbsolutePath()); + frame.setIconImage(img.getImage()); + + try + { + browser.navigate(url); + } + catch (MalformedURLException e1) + { + ; + } + + // Add a navigation listener. + browser.addNavigationListener(new NavigationListener() + { + + @Override + public void beforeLocalNavigate(NavigationEvent arg0) throws NavigationVetoException + { + //login success ( login fail in all other cases ). + if(arg0.getURL().toString().contains("login_success.html")) + { + //System.out.println(arg0.getURL().toString()); + //Getting session key. + if(arg0.getURL().toString().contains("session_key")) + { + loggedIn = true; + int bIdx = arg0.getURL().toString().indexOf("session_key"); + session_key = arg0.getURL().toString().substring(bIdx); + + try + { + //Re-encoding url. + session_key = java.net.URLDecoder.decode(session_key, "UTF-8"); + } + catch (UnsupportedEncodingException e) + { + loggedIn = false; + } + String[] params = session_key.split(","); + + for (String str : params ) + { + if (str.contains("session_key")) + { + session_key = str.substring("session_key".length() + 3); + session_key = session_key.substring(0, session_key.length() - 1); + } + + if(str.contains("secret")) + { + session_secret = str.substring("secret".length() + 4); + session_secret = session_secret.substring(0, session_secret.length() - 1); + frame.dispose(); + waitFor = false; + } + } + } + else + { + loggedIn = false; + } + } + } + + @Override + public void beforeNavigate(NavigationEvent arg0) throws NavigationVetoException { + ; + } + + @Override + public void beforeWindowOpen(NavigationEvent arg0) throws NavigationVetoException { + ; + } + + }); + + + frame.addWindowListener(new java.awt.event.WindowAdapter() + { + public void windowClosing(WindowEvent e) + { + waitFor = false; + } + }); + + //Wait for navigator. + while(waitFor) + { + try + { + Thread.sleep(1000); + } + catch (InterruptedException e) + { + ; + } + } + lastTokenUsed = token; lastClientUsed = client; - return ""; + registerSessionSecret(); + registerSessionKey(); + + if(FacebookSessionUtils.lockFile.exists()) + { + FacebookSessionUtils.lockFile.delete(); + } + + return session_key; } @@ -91,9 +238,9 @@ * Register the last known session secret. * @param aSessionSecret */ - public static void registerSessionSecret(String aSessionSecret) + public static void registerSessionSecret() { - sessionUtils.set(0, new String(aSessionSecret)); + sessionUtils.set(0, new String(session_secret)); } @@ -119,22 +266,12 @@ /** - * Return last used pass. - * @return - */ - public static String getPass() - { - return (String) sessionUtils.get( 3 ); - } - - - /** * Register the last known session key. * @param aSessionKey */ - public static void registerSessionKey(String aSessionKey) + public static void registerSessionKey() { - sessionUtils.set(1, new String(aSessionKey)); + sessionUtils.set(1, new String(session_key)); } @@ -144,6 +281,11 @@ */ public static String getSessionKey() { + if (sessionUtils.get(1) != null) + { + session_key = sessionUtils.get(1); + } + return (String) sessionUtils.get( 1 ); } @@ -152,16 +294,15 @@ /** * Load the serialized vector containing old session datas. */ - public static void loadSession() + public static boolean loadSession() { try { - File path = new File(""); - File session = new File( path.getAbsolutePath() + File.separator + "session_" + mail); + File session = new File( getCachePath() ); if(session.isFile()) { - FileInputStream fichier = new FileInputStream("session_" + mail); + FileInputStream fichier = new FileInputStream(session.getAbsolutePath()); ObjectInputStream ois = new ObjectInputStream(fichier); sessionUtils = (SerializedVector) ois.readObject(); } @@ -171,7 +312,7 @@ sessionUtils.add("0"); sessionUtils.add("0"); sessionUtils.add(mail); - sessionUtils.add(pass); + return false; } } catch (java.io.IOException e) @@ -182,6 +323,7 @@ { e.printStackTrace(); } + return true; } @@ -198,8 +340,7 @@ try { - File path = new File(""); - File session = new File( path.getAbsolutePath() + File.separator + "session_" + mail); + File session = new File( getCachePath() ); FileOutputStream file = new FileOutputStream(session.getAbsolutePath()); ObjectOutputStream oos = new ObjectOutputStream(file); oos.writeObject(sessionUtils); @@ -212,6 +353,13 @@ } } + + public static String getCachePath() + { + File f = new File(System.getProperty("java.io.tmpdir") + "/session_" + mail); + return f.getAbsolutePath(); + } + /** * Handle a serialized vector of vector<String> */ @@ -226,4 +374,29 @@ } } + + + public static synchronized Boolean isSingletonProgram(String name) + { + Boolean statut = true; + String tmpdir = System.getProperty("java.io.tmpdir"); + + File temporaryFile = new File(tmpdir + "/" + name +".tmp"); + statut = !temporaryFile.exists(); + + if(statut){ + try + { + temporaryFile.createNewFile(); + temporaryFile.deleteOnExit(); + FacebookSessionUtils.lockFile = temporaryFile; + } catch (IOException e) + { + statut = true; + } + } + + return statut; + } + } |