6a3843bb473ac76a6780ec37eb42e3f33749730d
[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         private function save(callback:Function):void {
22             if ( consumer == null )
23                 consumer = getConsumer();
24             if ( accessToken == null )
25                 accessToken = getAccessToken();
26         
27             if ( accessToken == null )
28                 getNewToken(callback);
29             else
30                 callback();
31         }
32     
33         private function getAccessToken():OAuthToken {
34             var key:String = Connection.getParam("oauth_token", null);
35             var secret:String = Connection.getParam("oauth_token_secret", null);
36             
37             if ( key == null || secret == null ) {
38                 var data:Object = SharedObject.getLocal("access_token").data;
39                 key = data["oauth_token"];
40                 secret = data["oauth_token_secret"];
41             }
42             
43             if ( key == null || secret == null )
44                 return null;
45             else    
46                 return new OAuthToken(key, secret);
47         }
48
49         private function getConsumer():OAuthConsumer {
50             var key:String = Connection.getParam("oauth_consumer_key", null);
51             var secret:String = Connection.getParam("oauth_consumer_secret", null);
52             
53             if ( key == null || secret == null )
54                 return null;
55             else    
56                 return new OAuthConsumer(key, secret);
57         }
58         
59         private function getNewToken(onCompletion:Function):void {
60             var oauthPanel:OAuthPanel = OAuthPanel(
61                 PopUpManager.createPopUp(Application(Application.application), OAuthPanel, true));
62             PopUpManager.centerPopUp(oauthPanel);
63             
64             var listener:Function = function(event:Event):void {
65                 accessToken = oauthPanel.accessToken;
66                 if ( oauthPanel.shouldRemember ) {
67                     var obj:SharedObject = SharedObject.getLocal("access_token");
68                     obj.setProperty("oauth_token", accessToken.key);
69                     obj.setProperty("oauth_token_secret", accessToken.secret);
70                     obj.flush();
71                 }
72                 onCompletion();
73             }
74             oauthPanel.addEventListener(OAuthPanel.ACCESS_TOKEN_EVENT, listener);
75         }
76         
77         private function saveData():void {
78             var saveDialog:SaveDialog = SaveDialog(
79                 PopUpManager.createPopUp(Application(Application.application), SaveDialog, true));
80             PopUpManager.centerPopUp(saveDialog);
81
82                         if (Connection.getConnectionInstance().getActiveChangeset()) {
83                                 saveDialog.dontPrompt();
84                         } else {
85                     Connection.getConnectionInstance().setAppID(consumer);
86                     Connection.getConnectionInstance().setAuthToken(accessToken);
87                         }
88         }
89     }
90     
91 }
92