Skip to content

Commit

Permalink
Add PMPackage and PMDatabase classes
Browse files Browse the repository at this point in the history
Signed-off-by: Sebastian Nowicki <sebnow@gmail.com>
  • Loading branch information
sebnow committed Dec 16, 2008
1 parent 641ab83 commit b62a9dc
Show file tree
Hide file tree
Showing 4 changed files with 248 additions and 0 deletions.
33 changes: 33 additions & 0 deletions Classes/PMDatabase.h
@@ -0,0 +1,33 @@
/*
* Copyright (c) 2008 Sebastian Nowicki <sebnow@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/

#import <Foundation/Foundation.h>

@interface PMDatabase : NSObject {
@package
struct __pmdb_t *_database;
}

- (NSArray *) filteredPackagesUsingPredicate:(NSPredicate *)thePredicate;
- (NSArray *) packages;

@end
68 changes: 68 additions & 0 deletions Classes/PMDatabase.m
@@ -0,0 +1,68 @@
/*
* Copyright (c) 2008 Sebastian Nowicki <sebnow@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/

#import "PMDatabase.h"
#import <alpm.h>
#import "PMPackage.h"

@implementation PMDatabase

- (void) dealloc
{
alpm_db_unregister(_database);
[super dealloc];
}

- (NSArray *) packages
{
size_t count;
NSMutableArray *packages;
alpm_list_t *list;
alpm_list_t *node;
PMPackage *package;
pmpkg_t *alpmPackage;
NSString *name;
NSArray *result;

list = alpm_db_getpkgcache(_database);
count = alpm_list_count(list);
packages = [[NSMutableArray alloc] initWithCapacity:count];
for(node = list; node != NULL; node = alpm_list_next(node)) {
alpmPackage = alpm_list_getdata(node);
name = [[NSString alloc] initWithUTF8String:alpm_pkg_get_name(alpmPackage)];
package = [[PMPackage alloc] initWithName:name fromDatabase:self];
[name release];
[packages addObject:package];
[package release];
}
result = [NSArray arrayWithArray:packages];
[packages release];
return result;
}

- (NSArray *) filteredPackagesUsingPredicate:(NSPredicate *)thePredicate
{
NSArray *packages = [self packages];
return [packages filteredArrayUsingPredicate:thePredicate];
}

@end
49 changes: 49 additions & 0 deletions Classes/PMPackage.h
@@ -0,0 +1,49 @@
/*
* Copyright (c) 2008 Sebastian Nowicki <sebnow@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/

#import <Foundation/Foundation.h>

@class PMDatabase;

@interface PMPackage : NSObject {
@package
struct __pmpkg_t *_package;
PMDatabase *_database;
NSString *_name;
NSString *_version;
NSURL *_url;
NSString *_description;
NSString *_packager;
NSNumber *_size;
}

@property (nonatomic, readonly) NSString *name;
@property (nonatomic, readonly) NSString *version;
@property (nonatomic, readonly) NSURL *url;
@property (nonatomic, readonly) PMDatabase *database;
@property (nonatomic, readonly) NSString *description;
@property (nonatomic, readonly) NSString *packager;
@property (nonatomic, readonly) NSNumber *size;

- (id) initWithName:(NSString *)aName fromDatabase:(PMDatabase *)theDatabase;

@end
98 changes: 98 additions & 0 deletions Classes/PMPackage.m
@@ -0,0 +1,98 @@
/*
* Copyright (c) 2008 Sebastian Nowicki <sebnow@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/

#import "PMPackage.h"
#import <alpm.h>
#import <alpm_list.h>
#import "PMDatabase.h"

@implementation PMPackage
@synthesize name = _name;
@synthesize version = _version;
@synthesize database = _database;
@synthesize description = _description;
@synthesize packager = _packager;
@synthesize size = _size;

- (id) initWithName:(NSString *)aName fromDatabase:(PMDatabase *)theDatabase
{
self = [super init];
if(self != nil) {
_package = alpm_db_get_pkg(theDatabase->_database, [aName UTF8String]);
if(_package == NULL) {
return nil;
}
_database = [theDatabase retain];
_name = [[NSString alloc] initWithUTF8String:alpm_pkg_get_name(_package)];
_version = [[NSString alloc] initWithUTF8String:alpm_pkg_get_version(_package)];
}
return self;
}

- (void) dealloc
{
[_database release];
[_name release];
[_packager release];
[_version release];
[_size release];
[_description release];
[_url release];
[super dealloc];
}

- (NSString *) packager
{
if(_packager == nil) {
_packager = [[NSString alloc] initWithUTF8String:alpm_pkg_get_packager(_package)];
}
return _packager;
}

- (NSNumber *) size
{
if(_size == nil) {
unsigned long long size = alpm_pkg_get_size(_package);
_size = [[NSNumber alloc] initWithUnsignedLongLong:size];
}
return _size;
}

- (NSString *) description
{
if(_description == nil) {
_description = [[NSString alloc] initWithUTF8String:alpm_pkg_get_desc(_package)];
}
return _description;
}

- (NSURL *) url
{
if(_url == nil) {
NSString *url = [[NSString alloc] initWithUTF8String:alpm_pkg_get_url(_package)];
_url = [[NSURL alloc] initWithString:url];
[url release];
}
return _url;
}

@end

0 comments on commit b62a9dc

Please sign in to comment.