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 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);
39 if ( accessToken == null )
40 getNewToken(callback);
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);
49 if ( key == null || secret == null )
52 return new OAuthConsumer(key, secret);
55 private function getNewToken(onCompletion:Function):void {
56 var oauthPanel:OAuthPanel = OAuthPanel(
57 PopUpManager.createPopUp(Application(Application.application), OAuthPanel, true));
58 PopUpManager.centerPopUp(oauthPanel);
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);
70 oauthPanel.addEventListener(OAuthPanel.ACCESS_TOKEN_EVENT, listener);
73 private function saveData():void {
74 var saveDialog:SaveDialog = SaveDialog(
75 PopUpManager.createPopUp(Application(Application.application), SaveDialog, true));
76 PopUpManager.centerPopUp(saveDialog);
78 if (Connection.getConnectionInstance().getActiveChangeset()) {
79 saveDialog.dontPrompt();
81 Connection.getConnectionInstance().setAppID(consumer);
82 Connection.getConnectionInstance().setAuthToken(accessToken);