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