18d97cd7ec1a78bcfa152b64a2202744f87ed596
[potlatch2.git] / net / systemeD / potlatch2 / save / SaveManager.as
1 package net.systemeD.potlatch2.save {
2
3     import flash.events.*;
4     import mx.managers.PopUpManager;
5     import mx.core.Application;
6     import net.systemeD.halcyon.connection.*;
7     import org.iotashan.oauth.*;
8
9     public class SaveManager {
10     
11         private static var instance:SaveManager = new SaveManager();
12         
13         private var accessToken:OAuthToken;
14         private var consumer:OAuthConsumer;
15
16         public static function saveChanges():void {
17             instance.save();
18         }
19         
20         private function save():void {
21             if ( consumer == null )
22                 consumer = getConsumer();
23             if ( accessToken == null )
24                 accessToken = getAccessToken();
25         
26             if ( accessToken == null )
27                 getNewToken(saveData);
28             else
29                 saveData();
30         }
31     
32         private function getAccessToken():OAuthToken {
33             var key:String = Connection.getParam("oauth_token", null);
34             var secret:String = Connection.getParam("oauth_token_secret", null);
35             
36             if ( key == null || secret == null )
37                 return null;
38             else    
39                 return new OAuthToken(key, secret);
40         }
41
42         private function getConsumer():OAuthConsumer {
43             var key:String = Connection.getParam("oauth_consumer_key", null);
44             var secret:String = Connection.getParam("oauth_consumer_secret", null);
45             
46             if ( key == null || secret == null )
47                 return null;
48             else    
49                 return new OAuthConsumer(key, secret);
50         }
51         
52         private function getNewToken(onCompletion:Function):void {
53             var oauthPanel:OAuthPanel = OAuthPanel(
54                 PopUpManager.createPopUp(Application(Application.application), OAuthPanel, true));
55             PopUpManager.centerPopUp(oauthPanel);
56             
57             var listener:Function = function(event:Event):void {
58                 accessToken = oauthPanel.accessToken;
59                 onCompletion();
60             }
61             oauthPanel.addEventListener(OAuthPanel.ACCESS_TOKEN_EVENT, listener);
62         }
63         
64         private function saveData():void {
65             Connection.getConnectionInstance().setAppID(consumer);
66             Connection.getConnectionInstance().setAuthToken(accessToken);
67             
68             var saveDialog:SaveDialog = SaveDialog(
69                 PopUpManager.createPopUp(Application(Application.application), SaveDialog, true));
70             PopUpManager.centerPopUp(saveDialog);
71         }
72     }
73     
74 }
75