Remove the deprectated Application.application
[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 mx.core.FlexGlobals;
8     import net.systemeD.halcyon.connection.*;
9     import org.iotashan.oauth.*;
10
11     public class SaveManager {
12     
13         private static var instance:SaveManager = new SaveManager();
14                 private var _connection:Connection;
15
16         public static function saveChanges(connection:Connection):void {
17             instance.save(instance.saveData,connection);
18         }
19
20         public static function ensureAccess(callback:Function, connection:Connection):void {
21             instance.save(callback,connection);
22         }
23
24         private function save(callback:Function, connection:Connection):void {
25                         _connection=connection;
26             if (connection.hasAccessToken()) {
27                 callback();
28             } else {
29                 getNewToken(callback);
30             }
31         }
32
33         private function getNewToken(onCompletion:Function):void {
34             var oauthPanel:OAuthPanel = OAuthPanel(
35                 PopUpManager.createPopUp(Application(FlexGlobals.topLevelApplication), OAuthPanel, true));
36             PopUpManager.centerPopUp(oauthPanel);
37                         oauthPanel.setConnection(_connection);
38             
39             var listener:Function = function(event:Event):void {
40                 var accessToken:OAuthToken = oauthPanel.accessToken;
41                 if ( oauthPanel.shouldRemember ) {
42                     var obj:SharedObject = SharedObject.getLocal("access_token");
43                     obj.setProperty("oauth_token", accessToken.key);
44                     obj.setProperty("oauth_token_secret", accessToken.secret);
45                     obj.flush();
46                 }
47                 onCompletion();
48             }
49             oauthPanel.addEventListener(OAuthPanel.ACCESS_TOKEN_EVENT, listener);
50         }
51         
52         private function saveData():void {
53             var saveDialog:SaveDialog = SaveDialog(
54                 PopUpManager.createPopUp(Application(FlexGlobals.topLevelApplication), SaveDialog, true));
55                         saveDialog.setConnection(_connection);
56             PopUpManager.centerPopUp(saveDialog);
57
58                         if (_connection.getActiveChangeset()) saveDialog.dontPrompt();
59         }
60     }
61     
62 }
63