Add XMPPStorage and XMPPJSONFileStorage.

This commit is contained in:
Jonathan Schleifer 2012-02-06 12:40:00 +01:00
parent 4a02854dec
commit 640fdb7da8
5 changed files with 332 additions and 1 deletions

View file

@ -18,6 +18,9 @@
4B19F5A314D1779E005D52DC /* ObjOpenSSL.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 4B19F5A214D1779E005D52DC /* ObjOpenSSL.framework */; };
4B19F5D114D20B01005D52DC /* XMPPMulticastDelegate.h in Headers */ = {isa = PBXBuildFile; fileRef = 4B19F5CF14D20B01005D52DC /* XMPPMulticastDelegate.h */; settings = {ATTRIBUTES = (Public, ); }; };
4B19F5D214D20B01005D52DC /* XMPPMulticastDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 4B19F5D014D20B01005D52DC /* XMPPMulticastDelegate.m */; };
4B23D6C114DFF2B3001DBDDD /* XMPPJSONFileStorage.h in Headers */ = {isa = PBXBuildFile; fileRef = 4B23D6BE14DFF2B3001DBDDD /* XMPPJSONFileStorage.h */; settings = {ATTRIBUTES = (Public, ); }; };
4B23D6C214DFF2B3001DBDDD /* XMPPJSONFileStorage.m in Sources */ = {isa = PBXBuildFile; fileRef = 4B23D6BF14DFF2B3001DBDDD /* XMPPJSONFileStorage.m */; };
4B23D6C314DFF2B3001DBDDD /* XMPPStorage.h in Headers */ = {isa = PBXBuildFile; fileRef = 4B23D6C014DFF2B3001DBDDD /* XMPPStorage.h */; settings = {ATTRIBUTES = (Public, ); }; };
4B4844F5138BBC7500EB48A5 /* XMPPSRVLookup.h in Headers */ = {isa = PBXBuildFile; fileRef = 4B4844F3138BBC7400EB48A5 /* XMPPSRVLookup.h */; settings = {ATTRIBUTES = (Public, ); }; };
4B4844F6138BBC7500EB48A5 /* XMPPSRVLookup.m in Sources */ = {isa = PBXBuildFile; fileRef = 4B4844F4138BBC7500EB48A5 /* XMPPSRVLookup.m */; };
4B484501138BBEEB00EB48A5 /* libresolv.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 4B484500138BBEEB00EB48A5 /* libresolv.dylib */; };
@ -75,6 +78,9 @@
4B19F5A214D1779E005D52DC /* ObjOpenSSL.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = ObjOpenSSL.framework; path = /Library/Frameworks/ObjOpenSSL.framework; sourceTree = "<group>"; };
4B19F5CF14D20B01005D52DC /* XMPPMulticastDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = XMPPMulticastDelegate.h; path = src/XMPPMulticastDelegate.h; sourceTree = SOURCE_ROOT; };
4B19F5D014D20B01005D52DC /* XMPPMulticastDelegate.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = XMPPMulticastDelegate.m; path = src/XMPPMulticastDelegate.m; sourceTree = SOURCE_ROOT; };
4B23D6BE14DFF2B3001DBDDD /* XMPPJSONFileStorage.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = XMPPJSONFileStorage.h; path = src/XMPPJSONFileStorage.h; sourceTree = SOURCE_ROOT; };
4B23D6BF14DFF2B3001DBDDD /* XMPPJSONFileStorage.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = XMPPJSONFileStorage.m; path = src/XMPPJSONFileStorage.m; sourceTree = SOURCE_ROOT; };
4B23D6C014DFF2B3001DBDDD /* XMPPStorage.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = XMPPStorage.h; path = src/XMPPStorage.h; sourceTree = SOURCE_ROOT; };
4B4844F3138BBC7400EB48A5 /* XMPPSRVLookup.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = XMPPSRVLookup.h; path = src/XMPPSRVLookup.h; sourceTree = SOURCE_ROOT; };
4B4844F4138BBC7500EB48A5 /* XMPPSRVLookup.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = XMPPSRVLookup.m; path = src/XMPPSRVLookup.m; sourceTree = SOURCE_ROOT; };
4B484500138BBEEB00EB48A5 /* libresolv.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libresolv.dylib; path = usr/lib/libresolv.dylib; sourceTree = SDKROOT; };
@ -197,6 +203,8 @@
4BC559E11337AC0900E345C7 /* XMPPIQ.m */,
4BC559E21337AC0900E345C7 /* XMPPJID.h */,
4BC559E31337AC0900E345C7 /* XMPPJID.m */,
4B23D6BE14DFF2B3001DBDDD /* XMPPJSONFileStorage.h */,
4B23D6BF14DFF2B3001DBDDD /* XMPPJSONFileStorage.m */,
4BC559E41337AC0900E345C7 /* XMPPMessage.h */,
4BC559E51337AC0900E345C7 /* XMPPMessage.m */,
4B19F5CF14D20B01005D52DC /* XMPPMulticastDelegate.h */,
@ -215,6 +223,7 @@
4B4844F4138BBC7500EB48A5 /* XMPPSRVLookup.m */,
4BC559FE1337AC1800E345C7 /* XMPPStanza.h */,
4BC559FF1337AC1800E345C7 /* XMPPStanza.m */,
4B23D6C014DFF2B3001DBDDD /* XMPPStorage.h */,
4B48B95C14DC260000546D39 /* XMPPXMLElementBuilder.h */,
4B48B95D14DC260000546D39 /* XMPPXMLElementBuilder.m */,
4B01D01F137C7E7D005624EA /* namespaces.h */,
@ -246,6 +255,7 @@
4B19F57C14D17082005D52DC /* XMPPEXTERNALAuth.h in Headers */,
4BC559F21337AC0900E345C7 /* XMPPIQ.h in Headers */,
4BC559F41337AC0900E345C7 /* XMPPJID.h in Headers */,
4B23D6C114DFF2B3001DBDDD /* XMPPJSONFileStorage.h in Headers */,
4BC559F61337AC0900E345C7 /* XMPPMessage.h in Headers */,
4B19F5D114D20B01005D52DC /* XMPPMulticastDelegate.h in Headers */,
4BC559F81337AC0900E345C7 /* XMPPPLAINAuth.h in Headers */,
@ -255,6 +265,7 @@
4BC559FC1337AC0900E345C7 /* XMPPSCRAMAuth.h in Headers */,
4B4844F5138BBC7500EB48A5 /* XMPPSRVLookup.h in Headers */,
4BC55A011337AC1800E345C7 /* XMPPStanza.h in Headers */,
4B23D6C314DFF2B3001DBDDD /* XMPPStorage.h in Headers */,
4B48B95F14DC260000546D39 /* XMPPXMLElementBuilder.h in Headers */,
4B01D020137C7E7D005624EA /* namespaces.h in Headers */,
);
@ -364,6 +375,7 @@
4B4844F6138BBC7500EB48A5 /* XMPPSRVLookup.m in Sources */,
4B19F5D214D20B01005D52DC /* XMPPMulticastDelegate.m in Sources */,
4B48B96014DC260000546D39 /* XMPPXMLElementBuilder.m in Sources */,
4B23D6C214DFF2B3001DBDDD /* XMPPJSONFileStorage.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};

View file

@ -12,6 +12,7 @@ SRCS = XMPPAuthenticator.m \
XMPPEXTERNALAuth.m \
XMPPIQ.m \
XMPPJID.m \
XMPPJSONFileStorage.m \
XMPPMessage.m \
XMPPMulticastDelegate.m \
XMPPPLAINAuth.m \
@ -25,7 +26,8 @@ SRCS = XMPPAuthenticator.m \
INCLUDES = ${SRCS:.m=.h} \
namespaces.h \
ObjXMPP.h
ObjXMPP.h \
XMPPStorage.h
include ../buildsys.mk

36
src/XMPPJSONFileStorage.h Normal file
View file

@ -0,0 +1,36 @@
/*
* Copyright (c) 2012, Jonathan Schleifer <js@webkeks.org>
*
* https://webkeks.org/hg/objxmpp/
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice is present in all copies.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#import <ObjFW/OFObject.h>
#import "XMPPStorage.h"
@class OFMutableDictionary;
@interface XMPPJSONFileStorage: OFObject <XMPPStorage>
{
OFString *file;
OFMutableDictionary *data;
}
- initWithFile: (OFString*)file;
@end

235
src/XMPPJSONFileStorage.m Normal file
View file

@ -0,0 +1,235 @@
/*
* Copyright (c) 2012, Jonathan Schleifer <js@webkeks.org>
*
* https://webkeks.org/hg/objxmpp/
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice is present in all copies.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#import <ObjFW/OFString.h>
#import <ObjFW/OFArray.h>
#import <ObjFW/OFDictionary.h>
#import <ObjFW/OFNumber.h>
#import <ObjFW/OFAutoreleasePool.h>
#import <ObjFW/OFNotImplementedException.h>
#import "XMPPJSONFileStorage.h"
@implementation XMPPJSONFileStorage
- init
{
Class c = isa;
[self release];
@throw [OFNotImplementedException exceptionWithClass: c];
}
- initWithFile: (OFString*)file_
{
self = [super init];
@try {
OFAutoreleasePool *pool = [OFAutoreleasePool new];
file = [file_ copy];
@try {
data = [[[OFString stringWithContentsOfFile:
file] JSONValue] retain];
} @catch (id e) {
data = [OFMutableDictionary new];
}
[pool release];
} @catch (id e) {
[self release];
@throw e;
}
return self;
}
- (void)dealloc
{
[file release];
[data release];
[super dealloc];
}
- (void)save
{
[[data JSONRepresentation] writeToFile: file];
}
- (void)XMPP_setObject: (id)object
forPath: (OFString*)path
{
OFArray *pathComponents = [path componentsSeparatedByString: @"."];
OFMutableDictionary *iter = data;
OFEnumerator *enumerator = [pathComponents objectEnumerator];
OFString *component;
size_t i = 0, components = [pathComponents count];
while ((component = [enumerator nextObject]) != nil) {
if (i++ == components - 1)
continue;
OFMutableDictionary *iter2 = [iter objectForKey: component];
if (iter2 == nil) {
iter2 = [OFMutableDictionary dictionary];
[iter setObject: iter2
forKey: component];
}
iter = iter2;
}
[iter setObject: object
forKey: [pathComponents lastObject]];
}
- (id)XMPP_objectForPath: (OFString*)path
{
OFArray *pathComponents = [path componentsSeparatedByString: @"."];
OFEnumerator *enumerator = [pathComponents objectEnumerator];
OFString *component;
id object = data;
while ((component = [enumerator nextObject]) != nil)
object = [object objectForKey: component];
return object;
}
- (void)setStringValue: (OFString*)string
forPath: (OFString*)path
{
OFAutoreleasePool *pool = [OFAutoreleasePool new];
[self XMPP_setObject: string
forPath: path];
[pool release];
}
- (OFString*)stringValueForPath: (OFString*)path
{
OFAutoreleasePool *pool = [OFAutoreleasePool new];
OFString *string;
string = [self XMPP_objectForPath: path];
[pool release];
return string;
}
- (void)setBooleanValue: (BOOL)boolean
forPath: (OFString*)path
{
OFAutoreleasePool *pool = [OFAutoreleasePool new];
[self XMPP_setObject: [OFNumber numberWithBool: boolean]
forPath: path];
[pool release];
}
- (BOOL)booleanValueForPath: (OFString*)path
{
OFAutoreleasePool *pool = [OFAutoreleasePool new];
BOOL boolean;
boolean = [[self XMPP_objectForPath: path] boolValue];
[pool release];
return boolean;
}
- (void)setIntegerValue: (intmax_t)integer
forPath: (OFString*)path
{
OFAutoreleasePool *pool = [OFAutoreleasePool new];
[self XMPP_setObject: [OFNumber numberWithIntMax: integer]
forPath: path];
[pool release];
}
- (intmax_t)integerValueForPath: (OFString*)path
{
OFAutoreleasePool *pool = [OFAutoreleasePool new];
intmax_t integer;
integer = [[self XMPP_objectForPath: path] intMaxValue];
[pool release];
return integer;
}
- (void)setArray: (OFArray*)array
forPath: (OFString*)path
{
OFAutoreleasePool *pool = [OFAutoreleasePool new];
[self XMPP_setObject: array
forPath: path];
[pool release];
}
- (OFArray*)arrayForPath: (OFString*)path
{
OFAutoreleasePool *pool = [OFAutoreleasePool new];
OFArray *array;
array = [self XMPP_objectForPath: path];
[pool release];
return array;
}
- (void)setDictionary: (OFDictionary*)dictionary
forPath: (OFString*)path
{
OFAutoreleasePool *pool = [OFAutoreleasePool new];
[self XMPP_setObject: dictionary
forPath: path];
[pool release];
}
- (OFDictionary*)dictionaryForPath: (OFString*)path
{
OFAutoreleasePool *pool = [OFAutoreleasePool new];
OFDictionary *dictionary;
dictionary = [self XMPP_objectForPath: path];
[pool release];
return dictionary;
}
@end

46
src/XMPPStorage.h Normal file
View file

@ -0,0 +1,46 @@
/*
* Copyright (c) 2012, Jonathan Schleifer <js@webkeks.org>
*
* https://webkeks.org/hg/objxmpp/
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice is present in all copies.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdint.h>
@class OFString;
@class OFArray;
@class OFDictionary;
@protocol XMPPStorage <OFObject>
- (void)save;
- (void)setStringValue: (OFString*)string
forPath: (OFString*)path;
- (OFString*)stringValueForPath: (OFString*)path;
- (void)setBooleanValue: (BOOL)boolean
forPath: (OFString*)path;
- (BOOL)booleanValueForPath: (OFString*)path;
- (void)setIntegerValue: (intmax_t)integer
forPath: (OFString*)path;
- (intmax_t)integerValueForPath: (OFString*)path;
- (void)setArray: (OFArray*)array
forPath: (OFString*)path;
- (OFArray*)arrayForPath: (OFString*)path;
- (void)setDictionary: (OFDictionary*)dictionary
forPath: (OFString*)path;
- (OFDictionary*)dictionaryForPath: (OFString*)path;
@end