1 package net.systemeD.potlatch2.save {
5 import mx.managers.PopUpManager;
6 import mx.core.Application;
7 import net.systemeD.halcyon.connection.*;
8 import org.iotashan.oauth.*;
10 public class SaveManager {
12 private static var instance:SaveManager = new SaveManager();
14 private var accessToken:OAuthToken;
15 private var consumer:OAuthConsumer;
17 public static function saveChanges():void {
18 instance.save(instance.saveData);
21 public static function getAccessSorted():void {
22 // hacky hack of pond-like clarity
23 instance.save(instance.doNothing);
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);
32 private function save(callback:Function):void {
33 if ( consumer == null )
34 consumer = getConsumer();
35 if ( accessToken == null )
36 accessToken = getAccessToken();
38 if ( accessToken == null )
39 getNewToken(callback);
44 private function getAccessToken():OAuthToken {
45 var key:String = Connection.getParam("oauth_token", null);
46 var secret:String = Connection.getParam("oauth_token_secret", null);
48 if ( key == null || secret == null ) {
49 var data:Object = SharedObject.getLocal("access_token").data;
50 key = data["oauth_token"];
51 secret = data["oauth_token_secret"];
54 if ( key == null || secret == null )
57 return new OAuthToken(key, secret);
60 private function getConsumer():OAuthConsumer {
61 var key:String = Connection.getParam("oauth_consumer_key", null);
62 var secret:String = Connection.getParam("oauth_consumer_secret", null);
64 if ( key == null || secret == null )
67 return new OAuthConsumer(key, secret);
70 private function getNewToken(onCompletion:Function):void {
71 var oauthPanel:OAuthPanel = OAuthPanel(
72 PopUpManager.createPopUp(Application(Application.application), OAuthPanel, true));
73 PopUpManager.centerPopUp(oauthPanel);
75 var listener:Function = function(event:Event):void {
76 accessToken = oauthPanel.accessToken;
77 if ( oauthPanel.shouldRemember ) {
78 var obj:SharedObject = SharedObject.getLocal("access_token");
79 obj.setProperty("oauth_token", accessToken.key);
80 obj.setProperty("oauth_token_secret", accessToken.secret);
85 oauthPanel.addEventListener(OAuthPanel.ACCESS_TOKEN_EVENT, listener);
88 private function saveData():void {
89 var saveDialog:SaveDialog = SaveDialog(
90 PopUpManager.createPopUp(Application(Application.application), SaveDialog, true));
91 PopUpManager.centerPopUp(saveDialog);
93 if (Connection.getConnectionInstance().getActiveChangeset()) {
94 saveDialog.dontPrompt();
96 Connection.getConnectionInstance().setAppID(consumer);
97 Connection.getConnectionInstance().setAuthToken(accessToken);