Objective-C: SQLite Wrapper

November 28, 2008 | Filed Under Mac OS X, iPhone | 4 Comments

I’ve written a simple SQLite Wrapper with two Examples one for Mac and one for the iPhone. The Wrapper class has the same source code for both platforms.

Here you can find the Mac Example Source Code and the iPhone Example Source Code.

iPhone Test SQLite App

This is a simple usage example:

Sqlite *sqlite = [[Sqlite alloc] init];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                             NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory
                        stringByAppendingPathComponent:@"SQLiteTest.db"];
if (![sqlite open:writableDBPath])
  return;

[sqlite executeNonQuery:@"CREATE TABLE test (key TEXT NOT NULL, value TEXT);"];
[sqlite executeNonQuery:@"DELETE FROM test;"];
[sqlite executeNonQuery:@"INSERT INTO test VALUES (?, ?);",
                        [Sqlite createUuid], @"PROVA"];
[sqlite executeNonQuery:@"INSERT INTO test VALUES (?, ?);",
                         [Sqlite createUuid], @"PROVA 2"];
[sqlite executeNonQuery:@"INSERT INTO test VALUES (?, ?);",
                         [Sqlite createUuid], @"PROVA 3"];

NSArray *results = [sqlite executeQuery:@"SELECT * FROM test;"];
for (NSDictionary *dictionary in results) {
  NSLog(@"Row");
  for (NSString *key in [dictionary keyEnumerator])
      NSLog(@" - %@ %@", key, [dictionary objectForKey:key]);
}

[results release];
[sqlite release];

OpenSSL: License Key with RSA

November 26, 2008 | Filed Under Tips | No Comments

In the last days I’ve played a bit with C libraries like OpenSSL and SQLite. The first post after Site downtime is dedicated to OpenSSL.
how do can you create your own license system for your application? With OpenSSL and less than 10 lines of code, you can do it. Take a look at the code below.

...
unsigned char checkDigest[SHA_DIGEST_LENGTH];
unsigned char shaDigest[SHA_DIGEST_LENGTH];
const char *userKey = "Matteo License";
unsigned char *signature = NULL;
unsigned int signatureLength = 0;

/* Generate Your RSA Key Pair */
RSA *rsa = RSA_generate_key(512, RSA_F4, NULL, NULL);

/* Generate SHA1 of User Key */
SHA1(userKey, strlen(userKey), shaDigest);

/* Create License Key for the User Key */
signature = OPENSSL_malloc(RSA_size(rsa));
signatureLength = RSA_private_encrypt(SHA_DIGEST_LENGTH, shaDigest,
                                 signature, rsa, RSA_PKCS1_PADDING);

/* Check if User Signature is a valid License Key */
if (RSA_public_decrypt(signatureLength, signature, checkDigest,
                                rsa, RSA_PKCS1_PADDING) != SHA_DIGEST_LENGTH)
{
      /* Valid License Key */
} else {
      /* Invalid License Key */
}

free(signature);
RSA_free(rsa);
...

You need to store the RSA Public Key in your app and then give to each user a generated signature, and it’s all.
Ok, this is a really base example (less than 10 lines of code) if you don’t want reinvent a License Key system, take a look at AquaticPrime Framework (http://www.aquaticmac.com/).

Cocoa: VBox View and NSScrollView

November 16, 2008 | Filed Under Cocoa | 1 Comment

Following the post of yesterday, today I’ve implemented a simple VBoxView (Vertical Box Layout) that allow you to lines up NSViews vertically.

Cocoa Scrollable NSViews VBox

Cocoa Scrollable NSViews VBox


Here you can find the example Source Code.

Cocoa: Drawing Images, Texts and Shadows

November 15, 2008 | Filed Under Cocoa | No Comments

Today I’m playing a bit with Quartz and Cocoa Drawing system (that is based on Quartz).

I’ve made a simple example that you can see below where I’ve used custom NSView that draws an Image and a Bezier Path with some text inside, and around the Bezier Path there’s a Shadow.

Cocoa Drawing Example

Cocoa Drawing Example

…And this is the drawRect method source code of the custom NSView.

- (void)drawRect:(NSRect)frameRect {
  [NSGraphicsContext saveGraphicsState];

  [[NSColor colorWithCalibratedRed:0.64 green:0.66 blue:0.71 alpha:1.0] set];
  NSRectFill(frameRect);

  /* Draw Shadow */
  NSShadow *shadow = [[NSShadow alloc] init];
  [shadow setShadowColor:[[NSColor blackColor] colorWithAlphaComponent:0.5]];
  [shadow setShadowOffset:NSMakeSize(4.0, -4.0)];
  [shadow setShadowBlurRadius:3.0];
  [shadow set];

  /* Draw Control */
  NSRect myRect = NSMakeRect(80, 50, frameRect.size.width - 120, 60);
  NSBezierPath *path = [NSBezierPath bezierPath];
  [path setLineJoinStyle:NSRoundLineJoinStyle];
  [path appendBezierPathWithRoundedRect:myRect xRadius:8.0 yRadius:8.0];    

  [path moveToPoint:NSMakePoint(80, 75)];
  [path lineToPoint:NSMakePoint(65, 85)];
  [path lineToPoint:NSMakePoint(80, 95)];

  NSColor *startingColor = [NSColor colorWithCalibratedRed:0.90
                                             green:0.92 blue:0.85 alpha:1.0];
  NSColor *endingColor = [NSColor colorWithCalibratedRed:0.81
                                           green:0.83 blue:0.76 alpha:1.0];
  NSGradient *gradient = [[[NSGradient alloc] initWithStartingColor:startingColor
                                 endingColor:endingColor] autorelease];
  [path fill];
  [gradient drawInBezierPath:path angle:90];

  [NSGraphicsContext restoreGraphicsState];

  [shadow release];

  NSImage *image = [NSImage imageNamed:NSImageNameUser];
  [image drawInRect:NSMakeRect(15, 50, 50, 50) fromRect:NSZeroRect
                operation:NSCompositeSourceOver fraction:1.0];

  NSString *string = [NSString stringWithString:@"Text\nMore Text"];
  [string drawInRect:NSMakeRect(90, 60, frameRect.size.width - 140, 45)
                        withAttributes:nil];
}

Cocoa: Filterbar Control

November 9, 2008 | Filed Under Cocoa | No Comments

This morning I’ve created this FilterBar (take a look at the picture below). You can add custom NSView and you can manage you groups with delegates.

Cocoa Filterbar Demo

Cocoa Filterbar Demo

Below, I’ve added a movie… but it doesn’t [sizeToFit] the page… so here is the link of Cocoa Filterbar Demo. And here you can find the Cocoa Filterbar Souce.

Cocoa: Horizontal Box

November 8, 2008 | Filed Under Cocoa | No Comments

It’s a couple of days that I’m searching for something like QHBoxLayout (Qt Horizonal Box Layout) in Cocoa. There’s NSMatrix that do something like this… but it uses NSCell and it sets the same size for all the cells, so this is not what I want. My solution is a simple NSView subclass

Example: Cocoa Horizontal Box

Example: Cocoa Horizontal Box

Continue reading Cocoa: Horizontal Box…

Quick Look: Look before you launch

November 2, 2008 | Filed Under Cocoa | No Comments

Quick Look is a technology introduced in Mac OS X version 10.5 that enables client applications, such as Spotlight and the Finder, to display thumbnail images and full-size previews of documents.

Quick Look Example

Quick Look Example

Here you can Find the source code example: TestQuickLook.zip

Cocoa: Controls Gallery

November 1, 2008 | Filed Under Cocoa, Mac OS X | No Comments

Today I don’t have inspiration…

So, I don’t have some code to publish now, but i’ve made a couple of screenshots that contain some of Cocoa’s Controls.

Cocoa Controls

Cocoa Controls

Continue reading Cocoa: Controls Gallery…