Move getAccessToken logic to the connection
[potlatch2.git] / net / systemeD / potlatch2 / save / SaveManager.as
1 package net.systemeD.potlatch2.save {
2
3     import flash.events.*;
4     import flash.net.*;
5     import mx.managers.PopUpManager;
6     import mx.core.Application;
7     import net.systemeD.halcyon.connection.*;
8     import org.iotashan.oauth.*;
9
10     public class SaveManager {
11     
12         private static var instance:SaveManager = new SaveManager();
13         
14         private var accessToken:OAuthToken;
15         private var consumer:OAuthConsumer;
16
17         public static function saveChanges():void {
18             instance.save(instance.saveData);
19         }
20
21         public static function getAccessSorted():void {
22             // hacky hack of pond-like clarity
23             instance.save(instance.doNothing);
24         }
25
26         private function doNothing():void {
27             //hack hacky hack hack. Please look the other way...
28             Connection.getConnectionInstance().setAppID(consumer);
29             Connection.getConnectionInstance().setAuthToken(accessToken);
30         }
31
32         private function save(callback:Function):void {
33             var conn:Connection = Connection.getConnectionInstance();
34             if ( consumer == null )
35                 consumer = getConsumer();
36             if ( accessToken == null )
37                 accessToken = conn.getAccessToken(SharedObject.getLocal("access_token").data);
38         
39             if ( accessToken == null )
40                 getNewToken(callback);
41             else
42                 callback();
43         }
44
45         private function getConsumer():OAuthConsumer {
46             var key:String = Connection.getParam("oauth_consumer_key", null);
47             var secret:String = Connection.getParam("oauth_consumer_secret", null);
48             
49             if ( key == null || secret == null )
50                 return null;
51             else    
52                 return new OAuthConsumer(key, secret);
53         }
54         
55         private function getNewToken(onCompletion:Function):void {
56             var oauthPanel:OAuthPanel = OAuthPanel(
57                 PopUpManager.createPopUp(Application(Application.application), OAuthPanel, true));
58             PopUpManager.centerPopUp(oauthPanel);
59             
60             var listener:Function = function(event:Event):void {
61                 accessToken = oauthPanel.accessToken;
62                 if ( oauthPanel.shouldRemember ) {
63                     var obj:SharedObject = SharedObject.getLocal("access_token");
64                     obj.setProperty("oauth_token", accessToken.key);
65                     obj.setProperty("oauth_token_secret", accessToken.secret);
66                     obj.flush();
67                 }
68                 onCompletion();
69             }
70             oauthPanel.addEventListener(OAuthPanel.ACCESS_TOKEN_EVENT, listener);
71         }
72         
73         private function saveData():void {
74             var saveDialog:SaveDialog = SaveDialog(
75                 PopUpManager.createPopUp(Application(Application.application), SaveDialog, true));
76             PopUpManager.centerPopUp(saveDialog);
77
78                         if (Connection.getConnectionInstance().getActiveChangeset()) {
79                                 saveDialog.dontPrompt();
80                         } else {
81                     Connection.getConnectionInstance().setAppID(consumer);
82                     Connection.getConnectionInstance().setAuthToken(accessToken);
83                         }
84         }
85     }
86     
87 }
88