StropheCappuccino API 1.0.0
/tmp/tempDoc.doc/TNStrophePrivateStorage.j
Go to the documentation of this file.
00001 /*
00002  * TNStrophePrivateStorage.j
00003  *
00004  * Copyright (C) 2010 Antoine Mercadal <antoine.mercadal@inframonde.eu>
00005  * This program is free software: you can redistribute it and/or modify
00006  * it under the terms of the GNU Affero General Public License as
00007  * published by the Free Software Foundation, either version 3 of the
00008  * License, or (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU Affero General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU Affero General Public License
00016  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00017  */
00018 
00019 
00020 TNStrophePrivateStorageGetErrorNotification = @"TNStrophePrivateStorageGetErrorNotification";
00021 TNStrophePrivateStorageSetErrorNotification = @"TNStrophePrivateStorageSetErrorNotification";
00022 TNStrophePrivateStorageSetNotification      = @"TNStrophePrivateStorageSetNotification";
00023 
00024 
00025 
00026 
00030 @implementation TNStrophePrivateStorage : CPObject
00031 {
00032     CPString            _namespace;
00033     TNStropheConnection _connection;
00034 }
00035 
00036 
00037 #pragma mark -
00038 #pragma mark Initialization
00039 
00045 + (TNStropheConnection)strophePrivateStorageWithConnection:(TNStropheConnection)aConnection namespace:(CPString)aNamespace
00046 {
00047     return [[TNStrophePrivateStorage alloc] initWithConnection:aConnection namespace:aNamespace];
00048 }
00049 
00055 - (TNStropheConnection)initWithConnection:(TNStropheConnection)aConnection namespace:(CPString)aNamespace
00056 {
00057     if (self = [super init])
00058     {
00059         _connection = aConnection;
00060         _namespace  = aNamespace
00061     }
00062 
00063     return self;
00064 }
00065 
00066 
00067 #pragma mark -
00068 #pragma mark Storage
00069 
00074 - (void)setObject:(id)anObject forKey:(CPString)aKey
00075 {
00076     var data        = [CPKeyedArchiver archivedDataWithRootObject:anObject],
00077         uid         = [_connection getUniqueId],
00078         stanza      = [TNStropheStanza iqWithAttributes:{@"id": uid, @"type": @"set"}],
00079         params      = [CPDictionary dictionaryWithObjectsAndKeys:uid, @"id"];
00080 
00081     [stanza addChildWithName:@"query" andAttributes:{@"xmlns": Strophe.NS.PRIVATE_STORAGE}];
00082     [stanza addChildWithName:aKey andAttributes:{@"xmlns": _namespace}];
00083     [stanza addTextNode:[data rawString]];
00084     [_connection registerSelector:@selector(_didSetObject:object:) ofObject:self withDict:params userInfo:anObject];
00085     [_connection send:stanza];
00086 }
00087 
00093 - (BOOL)_didSetObject:(TNStropheStanza)aStanza object:(id)anObject
00094 {
00095     if ([aStanza type] == @"result")
00096         [[CPNotificationCenter defaultCenter] postNotificationName:TNStrophePrivateStorageSetNotification object:self userInfo:anObject];
00097     else
00098         [[CPNotificationCenter defaultCenter] postNotificationName:TNStrophePrivateStorageSetErrorNotification object:self userInfo:aStanza];
00099 
00100     return NO;
00101 }
00102 
00108 - (id)objectForKey:(CPString)aKey target:(id)aTarget selector:(SEL)aSelector
00109 {
00110     var uid         = [_connection getUniqueId],
00111         stanza      = [TNStropheStanza iqWithAttributes:{@"id": uid, @"type": @"get"}],
00112         params      = [CPDictionary dictionaryWithObjectsAndKeys:uid, @"id"],
00113         listener    = {@"target": aTarget, @"selector": aSelector, @"key": aKey};
00114 
00115     [stanza addChildWithName:@"query" andAttributes:{@"xmlns": Strophe.NS.PRIVATE_STORAGE}];
00116     [stanza addChildWithName:aKey andAttributes:{@"xmlns": _namespace}];
00117     [_connection registerSelector:@selector(_didReceiveObject:userInfo:) ofObject:self withDict:params userInfo:listener];
00118     [_connection send:stanza];
00119 }
00120 
00126 - (BOOL)_didReceiveObject:(TNStropheStanza)aStanza userInfo:(id)listener
00127 {
00128     if ([aStanza type] == @"result")
00129     {
00130         var dataString = [[aStanza firstChildWithName:listener.key] text];
00131 
00132         // check if a an LPCrashReporter is here.
00133         // and if yes deactive it during parsing
00134         // data
00135         try
00136         {
00137             if (dataString)
00138                 var obj =  [CPKeyedUnarchiver unarchiveObjectWithData:[CPData dataWithRawString:TNStropheStripHTMLCharCode(dataString)]];
00139         }
00140         catch(ex)
00141         {
00142             [[CPNotificationCenter defaultCenter] postNotificationName:TNStrophePrivateStorageGetErrorNotification object:self userInfo:ex];
00143         }
00144     }
00145 
00146     [listener.target performSelector:listener.selector withObject:aStanza withObject:obj];
00147 
00148     return NO;
00149 }
00150 
00151 @end
00152 
00153 @implementation TNStrophePrivateStorage (CPSynthesizedAccessors)
00154 
00158 - (CPString)namespace
00159 {
00160     return _namespace;
00161 }
00162 
00166 - (void)setNamespace:(CPString)aValue
00167 {
00168     _namespace = aValue;
00169 }
00170 
00174 - (TNStropheConnection)connection
00175 {
00176     return _connection;
00177 }
00178 
00182 - (void)setConnection:(TNStropheConnection)aValue
00183 {
00184     _connection = aValue;
00185 }
00186 
00187 @end
 All Classes Namespaces Files Functions Variables