Move the getConsumer method 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         }
30
31         private function save(callback:Function):void {
32             var conn:Connection = Connection.getConnectionInstance();
33             if ( consumer == null )
34                 consumer = conn.getConsumer();
35             if ( accessToken == null )
36                 accessToken = conn.getAccessToken(SharedObject.getLocal("access_token").data);
37         
38             if ( accessToken == null )
39                 getNewToken(callback);
40             else
41                 callback();
42         }
43
44         private function getNewToken(onCompletion:Function):void {
45             var oauthPanel:OAuthPanel = OAuthPanel(
46                 PopUpManager.createPopUp(Application(Application.application), OAuthPanel, true));
47             PopUpManager.centerPopUp(oauthPanel);
48             
49             var listener:Function = function(event:Event):void {
50                 accessToken = oauthPanel.accessToken;
51                 if ( oauthPanel.shouldRemember ) {
52                     var obj:SharedObject = SharedObject.getLocal("access_token");
53                     obj.setProperty("oauth_token", accessToken.key);
54                     obj.setProperty("oauth_token_secret", accessToken.secret);
55                     obj.flush();
56                 }
57                 onCompletion();
58             }
59             oauthPanel.addEventListener(OAuthPanel.ACCESS_TOKEN_EVENT, listener);
60         }
61         
62         private function saveData():void {
63             var saveDialog:SaveDialog = SaveDialog(
64                 PopUpManager.createPopUp(Application(Application.application), SaveDialog, true));
65             PopUpManager.centerPopUp(saveDialog);
66
67                         if (Connection.getConnectionInstance().getActiveChangeset()) {
68                                 saveDialog.dontPrompt();
69                         } else {
70                     Connection.getConnectionInstance().setAppID(consumer);
71                         }
72         }
73     }
74     
75 }
76