Skip to content

Commit

Permalink
Fixed warnings and memory leaks for BooksWindowController
Browse files Browse the repository at this point in the history
Also performed some refactoring. This has been tested
pretty well. Not much has really changed.
  • Loading branch information
gregsexton committed Jul 10, 2010
1 parent 8f89ad9 commit 09bdc6f
Show file tree
Hide file tree
Showing 11 changed files with 399 additions and 70 deletions.
5 changes: 3 additions & 2 deletions AuthorsWindowController.h
Expand Up @@ -21,6 +21,7 @@

#import <Cocoa/Cocoa.h>
#import "author.h"
#import "AuthorsWindowControllerDelegate.h"


@interface AuthorsWindowController : NSObject {
Expand All @@ -34,9 +35,9 @@
author *initialSelection;
NSManagedObjectContext *managedObjectContext;

id *delegate;
id<AuthorsWindowControllerDelegate> delegate;
}
@property (nonatomic, assign) id *delegate;
@property (nonatomic, assign) id<AuthorsWindowControllerDelegate> delegate;

- (IBAction)saveClicked:(id)sender;
- (IBAction)cancelClicked:(id)sender;
Expand Down
31 changes: 31 additions & 0 deletions AuthorsWindowControllerDelegate.h
@@ -0,0 +1,31 @@
//
// AuthorsWindowControllerDelegate.h
//
// Copyright 2010 Greg Sexton
//
// This file is part of Sofia.
//
// Sofia is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Sofia is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Sofia. If not, see <http://www.gnu.org/licenses/>.
//

#import <Cocoa/Cocoa.h>


@protocol AuthorsWindowControllerDelegate

@optional

- (void) savedWithAuthorSelection:(author*)selectedAuthor;

@end
23 changes: 13 additions & 10 deletions BooksWindowController.h
Expand Up @@ -25,8 +25,10 @@
#import "author.h"
#import "amazonInterface.h"
#import "BooksWindowControllerDelegate.h"
#import "AuthorsWindowControllerDelegate.h"
#import "SubjectWindowControllerDelegate.h"

@interface BooksWindowController : NSObjectController {
@interface BooksWindowController : NSObjectController <AuthorsWindowControllerDelegate, SubjectWindowControllerDelegate> {
IBOutlet NSTextField *txt_search;

IBOutlet NSTextField *txt_isbn10;
Expand Down Expand Up @@ -97,24 +99,25 @@
@property (nonatomic, assign) id delegate;
@property (nonatomic) BOOL displaySearch;

- (IBAction) searchClicked:(id)sender;
- (IBAction) clearClicked:(id)sender;
- (IBAction) cancelClicked:(id)sender;
- (IBAction) saveClicked:(id)sender;
- (IBAction) addAuthorClicked:(id)sender;
- (IBAction) addSubjectClicked:(id)sender;
- (IBAction) cancelClicked:(id)sender;
- (IBAction) clearClicked:(id)sender;
- (IBAction) copiesValueChanged:(id)sender;
- (IBAction) saveClicked:(id)sender;
- (IBAction) searchClicked:(id)sender;

- (BOOL) updateUIFromAmazon;
- (BOOL) updateUIFromISBNDb;
- (NSFetchRequest*) authorExistsWithName:(NSString*)authorName;
- (NSFetchRequest*) subjectExistsWithName:(NSString*)subjectName;
- (void) displayManagedAuthorsWithSelectedAuthor:(author*)authorObj;
- (void) displayManagedSubjectsWithSelectedSubject:(subject*)subjectObj;
- (void) updateUIFromManagedObject;
- (NSFetchRequest*)entity:(NSString*)entity existsWithName:(NSString*)entityName;
- (id) initWithManagedObject:(book*)object withSearch:(BOOL)withSearch;
- (void) clearAllFields;
- (void) displayManagedAuthorsWithSelectedAuthor:(author*)authorObj;
- (void) displayManagedSubjectsWithSelectedSubject:(subject*)subjectObj;
- (void) saveManagedObjectContext:(NSManagedObjectContext*)context;
- (BOOL) updateUIFromISBNDb;
- (BOOL) updateUIFromAmazon;
- (void) updateManagedObjectFromUI;
- (void) updateSummaryTabView;
- (void) updateUIFromManagedObject;
@end
45 changes: 28 additions & 17 deletions BooksWindowController.m
Expand Up @@ -64,9 +64,9 @@ - (void) awakeFromNib {
}

- (NSManagedObjectContext *) managedObjectContext{
if (managedObjectContext != nil) {
if (managedObjectContext != nil)
return managedObjectContext;
}
return nil;
}

- (void) searchForISBN:(NSString*)isbn{
Expand Down Expand Up @@ -157,27 +157,34 @@ - (BOOL) updateUIFromAmazon {
amazonInterface* amazon = [[amazonInterface alloc] init];

if(![amazon searchISBN:[txt_search stringValue]]){
NSRunInformationalAlertPanel(@"Download Error", @"Unable to retrieve information from Amazon. Please check internet connectivity and valid access keys in your preferences." , @"Ok", nil, nil);
NSRunInformationalAlertPanel(@"Download Error",
@"Unable to retrieve information from Amazon. Please check internet connectivity and valid access keys in your preferences." , @"Ok", nil, nil);
[amazon release];
return false;
}

//NSLog([amazon imageURL]);
[img_summary_cover setImage:[amazon frontCover]];
[img_cover setImage:[amazon frontCover]];

[amazon release];

return true;
}

- (BOOL) updateUIFromISBNDb {

isbndbInterface *isbndb = [[isbndbInterface alloc] init];
if(![isbndb searchISBN:[txt_search stringValue]]){
NSRunInformationalAlertPanel(@"Download Error", @"Unable to retrieve information from ISBNDb. Please check internet connectivity and a valid access key in your preferences." , @"Ok", nil, nil);
NSRunInformationalAlertPanel(@"Download Error",
@"Unable to retrieve information from ISBNDb. Please check internet connectivity and a valid access key in your preferences." , @"Ok", nil, nil);
[isbndb release];
return false;
}

if(![isbndb successfullyFoundBook]){
NSRunInformationalAlertPanel(@"Search Error", @"No results found for this ISBN on ISBNDb." , @"Ok", nil, nil);
[isbndb release];
return false;
}

Expand Down Expand Up @@ -245,35 +252,35 @@ - (BOOL) updateUIFromISBNDb {

//lastly update the summary tab
[self updateSummaryTabView];
[isbndb release];
return true;
}

- (NSFetchRequest*) authorExistsWithName:(NSString*)authorName{
//returns the request in order to get hold of these authors
//otherwise returns nil if the author cannot be found.
NSError *error;
NSString *predicate = [[NSString alloc] initWithFormat:@"name MATCHES '%@'", authorName];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"author" inManagedObjectContext:managedObjectContext]];
[request setPredicate:[NSPredicate predicateWithFormat:predicate]];
if([managedObjectContext countForFetchRequest:request error:&error] > 0){
return request;
}else{
return nil;
}
return [self entity:@"author" existsWithName:authorName];
}

- (NSFetchRequest*) subjectExistsWithName:(NSString*)subjectName{
//returns the request in order to get hold of these subjects
//otherwise returns nil if the subject cannot be found.
return [self entity:@"subject" existsWithName:subjectName];
}

- (NSFetchRequest*)entity:(NSString*)entity existsWithName:(NSString*)entityName{
NSError *error;
NSString *predicate = [[NSString alloc] initWithFormat:@"name MATCHES '%@'", subjectName];
NSString *predicate = [[NSString alloc] initWithFormat:@"name MATCHES '%@'", entityName];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"subject" inManagedObjectContext:managedObjectContext]];
[request setEntity:[NSEntityDescription entityForName:entity inManagedObjectContext:managedObjectContext]];
[request setPredicate:[NSPredicate predicateWithFormat:predicate]];

[predicate release];

if([managedObjectContext countForFetchRequest:request error:&error] > 0){
return request;
return [request autorelease];
}else{
[request release];
return nil;
}
}
Expand Down Expand Up @@ -304,6 +311,7 @@ - (void) updateManagedObjectFromUI {
}

- (IBAction) searchClicked:(id)sender {
//TODO: check that searching for isbn
//check if the book already exists in library
NSError *error;
NSString *predicate = [[NSString alloc] initWithFormat:@"isbn10 MATCHES '%@' OR isbn13 MATCHES '%@'",
Expand All @@ -312,6 +320,7 @@ - (IBAction) searchClicked:(id)sender {
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"book" inManagedObjectContext:managedObjectContext]];
[request setPredicate:[NSPredicate predicateWithFormat:predicate]];
[predicate release];
if([managedObjectContext countForFetchRequest:request error:&error] > 0){
//[[NSApplication sharedApplication] presentError:error];
int alertReturn;
Expand All @@ -327,8 +336,10 @@ - (IBAction) searchClicked:(id)sender {
[self setObj:[[managedObjectContext executeFetchRequest:request error:&error] objectAtIndex:0]];
[self updateUIFromManagedObject];
}
[request release];
return;
}
[request release];

[progIndicator setUsesThreadedAnimation:true];
[progIndicator startAnimation:self];
Expand Down
Empty file modified GHUnitTestMain.m 100755 → 100644
Empty file.
3 changes: 1 addition & 2 deletions SidebarOutlineView.m
Expand Up @@ -21,7 +21,6 @@

#import "SidebarOutlineView.h"

// TODO: fix renaming!!!
// TODO: refactor out logic to a controller.

@implementation SidebarOutlineView
Expand Down Expand Up @@ -342,7 +341,7 @@ - (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id
}

- (id)outlineView:(NSOutlineView *)outlineView persistentObjectForItem:(id)item{
return item; //TODO: no idea what this really does! haha
return item;
}

- (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item{
Expand Down
5 changes: 3 additions & 2 deletions SubjectWindowController.h
Expand Up @@ -23,6 +23,7 @@
#import "subject.h"
#import "book.h"
#import "BooksWindowController.h"
#import "SubjectWindowControllerDelegate.h"


@interface SubjectWindowController : NSObject {
Expand All @@ -36,9 +37,9 @@
subject *initialSelection;
NSManagedObjectContext *managedObjectContext;

id *delegate;
id<SubjectWindowControllerDelegate> delegate;
}
@property (nonatomic, assign) id *delegate;
@property (nonatomic, assign) id<SubjectWindowControllerDelegate> delegate;

- (IBAction)saveClicked:(id)sender;
- (IBAction)cancelClicked:(id)sender;
Expand Down
31 changes: 31 additions & 0 deletions SubjectWindowControllerDelegate.h
@@ -0,0 +1,31 @@
//
// SubjectWindowControllerDelegate.h
//
// Copyright 2010 Greg Sexton
//
// This file is part of Sofia.
//
// Sofia is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Sofia is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Sofia. If not, see <http://www.gnu.org/licenses/>.
//

#import <Cocoa/Cocoa.h>


@protocol SubjectWindowControllerDelegate

@optional

- (void) savedWithSubjectSelection:(subject*)selectedSubject;

@end
21 changes: 12 additions & 9 deletions books.xcodeproj/greg.mode1v3
Expand Up @@ -232,6 +232,8 @@
<key>Layout</key>
<array>
<dict>
<key>BecomeActive</key>
<true/>
<key>ContentConfiguration</key>
<dict>
<key>PBXBottomSmartGroupGIDs</key>
Expand Down Expand Up @@ -280,7 +282,7 @@
<key>PBXSmartGroupTreeModuleOutlineStateSelectionKey</key>
<array>
<array>
<integer>19</integer>
<integer>20</integer>
<integer>17</integer>
<integer>16</integer>
<integer>0</integer>
Expand Down Expand Up @@ -317,26 +319,24 @@
<key>Dock</key>
<array>
<dict>
<key>BecomeActive</key>
<true/>
<key>ContentConfiguration</key>
<dict>
<key>PBXProjectModuleGUID</key>
<string>1CE0B20306471E060097A5F4</string>
<key>PBXProjectModuleLabel</key>
<string>PredicateEditorWindowControllerDelegate.h</string>
<string>AuthorsWindowControllerDelegate.h</string>
<key>PBXSplitModuleInNavigatorKey</key>
<dict>
<key>Split0</key>
<dict>
<key>PBXProjectModuleGUID</key>
<string>1CE0B20406471E060097A5F4</string>
<key>PBXProjectModuleLabel</key>
<string>PredicateEditorWindowControllerDelegate.h</string>
<string>AuthorsWindowControllerDelegate.h</string>
<key>_historyCapacity</key>
<integer>0</integer>
<key>bookmark</key>
<string>37FF4F6111E7CEA6001B8ECC</string>
<string>37FF4FCC11E8AE7B001B8ECC</string>
<key>history</key>
<array>
<string>37FF4DB111E291FB001B8ECC</string>
Expand All @@ -345,7 +345,11 @@
<string>37FF4F4B11E7CCE2001B8ECC</string>
<string>37FF4F4C11E7CCE2001B8ECC</string>
<string>37FF4F4D11E7CCE2001B8ECC</string>
<string>37FF4F4E11E7CCE2001B8ECC</string>
<string>37FF4FB911E8ADB6001B8ECC</string>
<string>37FF4FBA11E8ADB6001B8ECC</string>
<string>37FF4FBB11E8ADB6001B8ECC</string>
<string>37FF4FBC11E8ADB6001B8ECC</string>
<string>37FF4FBD11E8ADB6001B8ECC</string>
</array>
</dict>
<key>SplitCount</key>
Expand Down Expand Up @@ -541,7 +545,6 @@
<integer>5</integer>
<key>WindowOrderList</key>
<array>
<string>37FF4F6211E7CEA6001B8ECC</string>
<string>37FF4DC511E29265001B8ECC</string>
<string>37FF4DBF11E291FB001B8ECC</string>
<string>37FF4DC011E291FB001B8ECC</string>
Expand Down Expand Up @@ -572,7 +575,7 @@
<key>PBXProjectModuleGUID</key>
<string>1CD0528F0623707200166675</string>
<key>PBXProjectModuleLabel</key>
<string>SidebarOutlineView.m</string>
<string>BooksWindowController.m</string>
<key>StatusBarVisibility</key>
<true/>
</dict>
Expand Down

0 comments on commit 09bdc6f

Please sign in to comment.