d591d67b22d29cbe05cd6f991837dc488f16ab19
[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();
19         }
20         
21         private function save():void {
22             if ( consumer == null )
23                 consumer = getConsumer();
24             if ( accessToken == null )
25                 accessToken = getAccessToken();
26         
27             if ( accessToken == null )
28                 getNewToken(saveData);
29             else
30                 saveData();
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", "/potlatch2.swf").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", "/potlatch2.swf");
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             Connection.getConnectionInstance().setAppID(consumer);
79             Connection.getConnectionInstance().setAuthToken(accessToken);
80             
81             var saveDialog:SaveDialog = SaveDialog(
82                 PopUpManager.createPopUp(Application(Application.application), SaveDialog, true));
83             PopUpManager.centerPopUp(saveDialog);
84         }
85     }
86     
87 }
88