]> git.openstreetmap.org Git - potlatch2.git/blob - net/systemeD/potlatch2/save/SaveManager.as
Make the mygpx dialog set up the tokens first
[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         public static function getAccessSorted():void {
22             // hacky hack of pond-like clarity
23             instance.save(instance.doNothing);
24         }
25
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);
30         }
31
32         private function save(callback:Function):void {
33             if ( consumer == null )
34                 consumer = getConsumer();
35             if ( accessToken == null )
36                 accessToken = getAccessToken();
37         
38             if ( accessToken == null )
39                 getNewToken(callback);
40             else
41                 callback();
42         }
43
44         private function getAccessToken():OAuthToken {
45             var key:String = Connection.getParam("oauth_token", null);
46             var secret:String = Connection.getParam("oauth_token_secret", null);
47             
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"];
52             }
53             
54             if ( key == null || secret == null )
55                 return null;
56             else    
57                 return new OAuthToken(key, secret);
58         }
59
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);
63             
64             if ( key == null || secret == null )
65                 return null;
66             else    
67                 return new OAuthConsumer(key, secret);
68         }
69         
70         private function getNewToken(onCompletion:Function):void {
71             var oauthPanel:OAuthPanel = OAuthPanel(
72                 PopUpManager.createPopUp(Application(Application.application), OAuthPanel, true));
73             PopUpManager.centerPopUp(oauthPanel);
74             
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);
81                     obj.flush();
82                 }
83                 onCompletion();
84             }
85             oauthPanel.addEventListener(OAuthPanel.ACCESS_TOKEN_EVENT, listener);
86         }
87         
88         private function saveData():void {
89             var saveDialog:SaveDialog = SaveDialog(
90                 PopUpManager.createPopUp(Application(Application.application), SaveDialog, true));
91             PopUpManager.centerPopUp(saveDialog);
92
93                         if (Connection.getConnectionInstance().getActiveChangeset()) {
94                                 saveDialog.dontPrompt();
95                         } else {
96                     Connection.getConnectionInstance().setAppID(consumer);
97                     Connection.getConnectionInstance().setAuthToken(accessToken);
98                         }
99         }
100     }
101     
102 }
103