Section 1
//Storage (Storage)
package {
import flash.events.*;
import flash.display.*;
import flash.text.*;
import flash.net.*;
import flash.external.*;
import flash.system.*;
public class Storage extends Sprite {
private var logFn:String;
private var logText:TextField;
private var LSOName:String;// = "SwfStore"
private var dataStore:SharedObject;
public function Storage(){
super();
if (!ExternalInterface.available){
localLog("External Interface is not avaliable! (No communication with JavaScript.) Exiting.");
return;
};
if (this.loaderInfo.parameters.logfn){
logFn = this.loaderInfo.parameters.logfn;
};
log("Initializing...");
Security.allowDomain("*");
Security.allowInsecureDomain("*");
if (this.loaderInfo.parameters.LSOName){
LSOName = this.loaderInfo.parameters.LSOName;
};
try {
dataStore = SharedObject.getLocal(LSOName);
} catch(error:Error) {
log(("Unable to create a local shared object. Exiting - " + error.message));
onError();
return;
};
try {
ExternalInterface.addCallback("set", setValue);
ExternalInterface.addCallback("get", getValue);
ExternalInterface.addCallback("getAll", getAllValues);
ExternalInterface.addCallback("clear", clearValue);
log("Ready! Firing onload if provided");
if (this.loaderInfo.parameters.onload){
ExternalInterface.call(this.loaderInfo.parameters.onload);
};
} catch(error:SecurityError) {
log((("A SecurityError occurred: " + error.message) + "\n"));
onError();
} catch(error:Error) {
log((("An Error occurred: " + error.message) + "\n"));
onError();
};
}
private function clearValue(_arg1:String):void{
var key = _arg1;
try {
log(("Deleting " + key));
delete dataStore.data[key];
} catch(error:Error) {
log(("Error deleting key - " + error.message));
};
}
private function onError():void{
try {
if (((ExternalInterface.available) && (this.loaderInfo.parameters.onerror))){
ExternalInterface.call(this.loaderInfo.parameters.onerror);
};
} catch(error:Error) {
log(("Error attempting to fire JS onerror callback - " + error.message));
};
}
private function log(_arg1:String):void{
var str = _arg1;
if (logFn){
try {
ExternalInterface.call(logFn, "debug", "swfStore", str);
} catch(error:Error) {
localLog(("Error logging to js: " + error.message));
};
} else {
localLog(str);
};
}
private function getAllValues():Object{
return (dataStore.data);
}
private function setValue(_arg1:String, _arg2):void{
var key = _arg1;
var val = _arg2;
try {
if (typeof(val) != "string"){
val = val.toString();
};
log(((("Setting " + key) + "=") + val));
dataStore.data[key] = val;
} catch(error:Error) {
log(("Unable to save data - " + error.message));
};
var flushStatus:String;
try {
flushStatus = dataStore.flush(10000);
} catch(error:Error) {
log(("Error...Could not write SharedObject to disk - " + error.message));
};
if (flushStatus != null){
switch (flushStatus){
case SharedObjectFlushStatus.PENDING:
log("Requesting permission to save object...");
dataStore.addEventListener(NetStatusEvent.NET_STATUS, onFlushStatus);
break;
case SharedObjectFlushStatus.FLUSHED:
break;
};
};
}
private function localLog(_arg1:String):void{
if (!logText){
logText = new TextField();
logText.width = 450;
addChild(logText);
};
logText.appendText((_arg1 + "\n"));
}
private function getValue(_arg1:String):String{
var key = _arg1;
try {
log(("Reading " + key));
return (dataStore.data[key]);
} catch(error:Error) {
log(("Unable to read data - " + error.message));
};
return (null);
}
private function onFlushStatus(_arg1:NetStatusEvent):void{
log("User closed permission dialog...");
switch (_arg1.info.code){
case "SharedObject.Flush.Success":
log("User granted permission -- value saved.");
break;
case "SharedObject.Flush.Failed":
log("User denied permission -- value not saved.");
break;
};
dataStore.removeEventListener(NetStatusEvent.NET_STATUS, onFlushStatus);
}
}
}//package