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