bc9ab3d56beed65c667d1602d06254e9fc791539
[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             
37             var listener:Function = function(event:Event):void {
38                 var accessToken:OAuthToken = oauthPanel.accessToken;
39                 if ( oauthPanel.shouldRemember ) {
40                     var obj:SharedObject = SharedObject.getLocal("access_token");
41                     obj.setProperty("oauth_token", accessToken.key);
42                     obj.setProperty("oauth_token_secret", accessToken.secret);
43                     obj.flush();
44                 }
45                 onCompletion();
46             }
47             oauthPanel.addEventListener(OAuthPanel.ACCESS_TOKEN_EVENT, listener);
48         }
49         
50         private function saveData():void {
51             var saveDialog:SaveDialog = SaveDialog(
52                 PopUpManager.createPopUp(Application(Application.application), SaveDialog, true));
53                         saveDialog.setConnection(_connection);
54             PopUpManager.centerPopUp(saveDialog);
55
56                         if (_connection.getActiveChangeset()) saveDialog.dontPrompt();
57         }
58     }
59     
60 }
61