Index: ObjXMPP.xcodeproj/project.pbxproj ================================================================== --- ObjXMPP.xcodeproj/project.pbxproj +++ ObjXMPP.xcodeproj/project.pbxproj @@ -16,10 +16,13 @@ 4B19F57C14D17082005D52DC /* XMPPEXTERNALAuth.h in Headers */ = {isa = PBXBuildFile; fileRef = 4B19F57814D17081005D52DC /* XMPPEXTERNALAuth.h */; settings = {ATTRIBUTES = (Public, ); }; }; 4B19F57D14D17082005D52DC /* XMPPEXTERNALAuth.m in Sources */ = {isa = PBXBuildFile; fileRef = 4B19F57914D17081005D52DC /* XMPPEXTERNALAuth.m */; }; 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 */; }; 4B48B95E14DC260000546D39 /* ObjXMPP.h in Headers */ = {isa = PBXBuildFile; fileRef = 4B48B95B14DC260000546D39 /* ObjXMPP.h */; settings = {ATTRIBUTES = (Public, ); }; }; 4B48B95F14DC260000546D39 /* XMPPXMLElementBuilder.h in Headers */ = {isa = PBXBuildFile; fileRef = 4B48B95C14DC260000546D39 /* XMPPXMLElementBuilder.h */; settings = {ATTRIBUTES = (Public, ); }; }; @@ -73,10 +76,13 @@ 4B19F57814D17081005D52DC /* XMPPEXTERNALAuth.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = XMPPEXTERNALAuth.h; path = src/XMPPEXTERNALAuth.h; sourceTree = SOURCE_ROOT; }; 4B19F57914D17081005D52DC /* XMPPEXTERNALAuth.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = XMPPEXTERNALAuth.m; path = src/XMPPEXTERNALAuth.m; sourceTree = SOURCE_ROOT; }; 4B19F5A214D1779E005D52DC /* ObjOpenSSL.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = ObjOpenSSL.framework; path = /Library/Frameworks/ObjOpenSSL.framework; sourceTree = ""; }; 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; }; 4B48B95B14DC260000546D39 /* ObjXMPP.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ObjXMPP.h; path = src/ObjXMPP.h; sourceTree = SOURCE_ROOT; }; 4B48B95C14DC260000546D39 /* XMPPXMLElementBuilder.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = XMPPXMLElementBuilder.h; path = src/XMPPXMLElementBuilder.h; sourceTree = SOURCE_ROOT; }; @@ -195,10 +201,12 @@ 4B19F57914D17081005D52DC /* XMPPEXTERNALAuth.m */, 4BC559E01337AC0900E345C7 /* XMPPIQ.h */, 4BC559E11337AC0900E345C7 /* XMPPIQ.m */, 4BC559E21337AC0900E345C7 /* XMPPJID.h */, 4BC559E31337AC0900E345C7 /* XMPPJID.m */, + 4B23D6BE14DFF2B3001DBDDD /* XMPPJSONFileStorage.h */, + 4B23D6BF14DFF2B3001DBDDD /* XMPPJSONFileStorage.m */, 4BC559E41337AC0900E345C7 /* XMPPMessage.h */, 4BC559E51337AC0900E345C7 /* XMPPMessage.m */, 4B19F5CF14D20B01005D52DC /* XMPPMulticastDelegate.h */, 4B19F5D014D20B01005D52DC /* XMPPMulticastDelegate.m */, 4BC559E61337AC0900E345C7 /* XMPPPLAINAuth.h */, @@ -213,10 +221,11 @@ 4BC559FD1337AC1800E345C7 /* XMPPSCRAMAuth.m */, 4B4844F3138BBC7400EB48A5 /* XMPPSRVLookup.h */, 4B4844F4138BBC7500EB48A5 /* XMPPSRVLookup.m */, 4BC559FE1337AC1800E345C7 /* XMPPStanza.h */, 4BC559FF1337AC1800E345C7 /* XMPPStanza.m */, + 4B23D6C014DFF2B3001DBDDD /* XMPPStorage.h */, 4B48B95C14DC260000546D39 /* XMPPXMLElementBuilder.h */, 4B48B95D14DC260000546D39 /* XMPPXMLElementBuilder.m */, 4B01D01F137C7E7D005624EA /* namespaces.h */, ); path = ObjXMPP; @@ -244,19 +253,21 @@ 4BC559EE1337AC0900E345C7 /* XMPPConnection.h in Headers */, 4BC559F01337AC0900E345C7 /* XMPPExceptions.h in Headers */, 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 */, 4BC559FA1337AC0900E345C7 /* XMPPPresence.h in Headers */, 4BDEF8071340B240000156D1 /* XMPPRoster.h in Headers */, 4BD9BF59134003F700DAB43A /* XMPPRosterItem.h in Headers */, 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 */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -362,10 +373,11 @@ 4BD9BF5A134003F700DAB43A /* XMPPRosterItem.m in Sources */, 4BDEF8081340B240000156D1 /* XMPPRoster.m in Sources */, 4B4844F6138BBC7500EB48A5 /* XMPPSRVLookup.m in Sources */, 4B19F5D214D20B01005D52DC /* XMPPMulticastDelegate.m in Sources */, 4B48B96014DC260000546D39 /* XMPPXMLElementBuilder.m in Sources */, + 4B23D6C214DFF2B3001DBDDD /* XMPPJSONFileStorage.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; /* End PBXSourcesBuildPhase section */ Index: src/Makefile ================================================================== --- src/Makefile +++ src/Makefile @@ -10,10 +10,11 @@ XMPPConnection.m \ XMPPExceptions.m \ XMPPEXTERNALAuth.m \ XMPPIQ.m \ XMPPJID.m \ + XMPPJSONFileStorage.m \ XMPPMessage.m \ XMPPMulticastDelegate.m \ XMPPPLAINAuth.m \ XMPPPresence.m \ XMPPRoster.m \ @@ -23,11 +24,12 @@ XMPPStanza.m \ XMPPXMLElementBuilder.m INCLUDES = ${SRCS:.m=.h} \ namespaces.h \ - ObjXMPP.h + ObjXMPP.h \ + XMPPStorage.h include ../buildsys.mk CPPFLAGS += -I.. -DHAVE_CONFIG_H LD = ${OBJC} ADDED src/XMPPJSONFileStorage.h Index: src/XMPPJSONFileStorage.h ================================================================== --- /dev/null +++ src/XMPPJSONFileStorage.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2012, Jonathan Schleifer + * + * 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 + +#import "XMPPStorage.h" + +@class OFMutableDictionary; + +@interface XMPPJSONFileStorage: OFObject +{ + OFString *file; + OFMutableDictionary *data; +} + +- initWithFile: (OFString*)file; +@end ADDED src/XMPPJSONFileStorage.m Index: src/XMPPJSONFileStorage.m ================================================================== --- /dev/null +++ src/XMPPJSONFileStorage.m @@ -0,0 +1,235 @@ +/* + * Copyright (c) 2012, Jonathan Schleifer + * + * 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 +#import +#import +#import +#import + +#import + +#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 ADDED src/XMPPStorage.h Index: src/XMPPStorage.h ================================================================== --- /dev/null +++ src/XMPPStorage.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2012, Jonathan Schleifer + * + * 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 + +@class OFString; +@class OFArray; +@class OFDictionary; + +@protocol XMPPStorage +- (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