Don't ask for the requestToken if we haven't told OAuthPanel which connection to...
[potlatch2.git] / net / systemeD / potlatch2 / save / OAuthPanel.mxml
index 6e5b68ccf7faccf4a7bd63e1909f4f272ea2e4f4..f972f927ffa220c0ef0a881d4e24e473394715a0 100644 (file)
                indeterminate="true"/>
         <mx:Spacer width="100%"/>
 
-           <mx:Button label="Cancel" click="PopUpManager.removePopUp(this);"/>
-           <mx:Button id="tryAccessButton" label="Try Access" click="getAccessToken()" enabled="false"/>
+           <mx:Button label="Cancel" click="PopUpManager.removePopUp(this);" styleName="titleWindowButton" />
+           <mx:Button id="tryAccessButton" label="Try Access" click="getAccessToken()" enabled="false" styleName="titleWindowButton" />
        </mx:ControlBar>
        
        <mx:Script><![CDATA[
+               import flash.display.InteractiveObject;
         import flash.events.Event;
         import flash.net.*;
+               import flash.system.Capabilities;
         import mx.managers.PopUpManager;
         import net.systemeD.halcyon.connection.*;
         import org.iotashan.oauth.*;
+               import flash.external.ExternalInterface;
 
         private var connection:Connection;
         private var requestToken:OAuthToken;
         private var _accessToken:OAuthToken;
         private var authoriseURL:String;
         private var lastHTTPStatus:int = 0;
+        private var waiting:Boolean = false;
         
         public static var ACCESS_TOKEN_EVENT:String = "gotAccessToken";
         
         private function getAuthText():String {
             return "To save data you must authorise this application to edit "+
-                    Connection.serverName + " on your behalf.";
+                    connection.serverName + " on your behalf.";
         }
         
+               public function setConnection(connection:Connection):void {
+                       this.connection=connection;
+                       if (waiting) { waiting=false; getRequestToken(); }
+               }
+
         private function openURL(url:String):void {
-            var urlRequest:URLRequest = new URLRequest(url);
-            navigateToURL(urlRequest, "_blank");
-        }
+                       if (ExternalInterface.available) {
+                               var winH:int = 560;
+                               var winW:int = 600;
+                               var leftPos:int  = (Capabilities.screenResolutionX - winW) / 2;
+                               var topPos:int = (Capabilities.screenResolutionY - winH) / 2;
+                               ExternalInterface.call( "window.open", url,"oAuthWin","height=" + winH + ",width=" + winW +",top=" + topPos + ", left=" + leftPos +", toolbar=no,scrollbars=no,status=no,location=no,menubar=no,directories=no");
+                               }               
+                       else
+                       {
+                               var urlRequest:URLRequest = new URLRequest(url);
+                               navigateToURL(urlRequest,"_blank");
+                       }
+               }
         
         private function getRequestToken():void {
-            connection = Connection.getConnectionInstance();
-            
+            if (!connection) { waiting=true; return; } // in case the connection hasn't been set yet
+
             var sig:IOAuthSignatureMethod = new OAuthSignatureMethod_HMAC_SHA1();
             var consumer:OAuthConsumer = getConsumer();
-            var url:String = Connection.getParam("oauth_request_url", "http://127.0.0.1:3000/oauth/request_token");
+            var url:String = connection.getParam("oauth_request_url", "http://127.0.0.1:3000/oauth/request_token");
             
             var params:Object = new Object();
             var oauthRequest:OAuthRequest = new OAuthRequest("GET", url, params, consumer, null);
             var urlStr:Object = oauthRequest.buildRequest(sig, OAuthRequest.RESULT_TYPE_URL_STRING)
-            
+
+                       //register the "pressTry" function so the oAuth callback page can try to advance the editor directly to the next step
+                       ExternalInterface.addCallback("pressTry", pressTry);            
+                       
             // build the actual request
             var urlReq:URLRequest = new URLRequest(String(urlStr));
             var loader:URLLoader = new URLLoader();
             trace("Yay! response: "+URLLoader(event.target).data);
             requestToken = getResponseToken(URLLoader(event.target));
             
-            var url:String = Connection.getParam("oauth_auth_url", "http://127.0.0.1:3000/oauth/authorize");            
+            var url:String = connection.getParam("oauth_auth_url", "http://127.0.0.1:3000/oauth/authorize");            
             link.label = url;
-            authoriseURL = url + "?oauth_token="+requestToken.key;
-            progress.visible = false;
+                       authoriseURL = url + "?oauth_token="+requestToken.key;
+                       progress.visible = false;
             gotLinkBox.visible = true;
         }
 
         private function getAccessToken():void {
             var sig:IOAuthSignatureMethod = new OAuthSignatureMethod_HMAC_SHA1();
             var consumer:OAuthConsumer = getConsumer();
-            var url:String = Connection.getParam("oauth_access_url", "http://127.0.0.1:3000/oauth/access_token");
+            var url:String = connection.getParam("oauth_access_url", "http://127.0.0.1:3000/oauth/access_token");
 
             var oauthRequest:OAuthRequest = new OAuthRequest("GET", url, null, consumer, requestToken);
             var urlStr:Object = oauthRequest.buildRequest(sig, OAuthRequest.RESULT_TYPE_URL_STRING)
             PopUpManager.removePopUp(this);
             
             _accessToken = getResponseToken(URLLoader(event.target));
-            Connection.getConnectionInstance().setAuthToken(_accessToken);
+            connection.setAuthToken(_accessToken);
             dispatchEvent(new Event(ACCESS_TOKEN_EVENT));
         }
         
         }
         
         private function getConsumer():OAuthConsumer {
-            var key:String = Connection.getParam("oauth_consumer_key", "");
-            var secret:String = Connection.getParam("oauth_consumer_secret", "");
+            var key:String = connection.getParam("oauth_consumer_key", "");
+            var secret:String = connection.getParam("oauth_consumer_secret", "");
             return new OAuthConsumer(key, secret);
         }
+               
+               public function pressTry():void {
+                       getAccessToken();
+               }
         
        ]]></mx:Script>
 </mx:TitleWindow>