1/* vim: set ft=objc ts=4 nowrap: */ 2/* All Rights reserved */ 3 4#include <AppKit/AppKit.h> 5#include "FreeDBView.h" 6 7#include <Cddb/Cddb.h> 8 9 10static FreeDBView *singleInstance = nil; 11 12@interface FreeDBView (Private) 13 14- (void) initializeFromDefaults; 15 16@end 17 18 19@implementation FreeDBView (Private) 20 21- (void) initializeFromDefaults 22{ 23 if ( [[NSUserDefaults standardUserDefaults] objectForKey: @"FreedbSite"] ) { 24 [serverTextField setStringValue: [[NSUserDefaults standardUserDefaults] 25 stringForKey: @"FreedbSite"]]; 26 27 } 28 if ([[NSUserDefaults standardUserDefaults] objectForKey: @"AutoQueryCddb"]) { 29 [autoQueryButton setState: [[[NSUserDefaults standardUserDefaults] objectForKey: 30 @"AutoQueryCddb"] intValue]]; 31 } 32} 33 34@end 35 36 37@implementation FreeDBView 38 39- (id) init 40{ 41 return [self initWithNibName: @"FreeDB"]; 42} 43 44- (id) initWithNibName: (NSString *) nibName 45{ 46 int i; 47 NSBundle *bundle; 48 NSString *bundlePath; 49 NSArray *searchPaths; 50 51 if (singleInstance) { 52 [self dealloc]; 53 } else { 54 self = [super init]; 55 56 // try to load the Cddb bundle 57 searchPaths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, 58 NSUserDomainMask|NSLocalDomainMask|NSSystemDomainMask, YES); 59 for (i = 0; i < [searchPaths count]; i++) { 60 bundlePath = [NSString stringWithFormat: @"%@/Bundles/Cddb.bundle", 61 [searchPaths objectAtIndex: i]]; 62 63 bundle = [NSBundle bundleWithPath: bundlePath]; 64 if (bundle) { 65 cddbClass = [bundle principalClass]; 66 if (cddbClass) { 67 break; 68 } else { 69 NSLog(@"Failed to get Class from Cddb.bundle"); 70 } 71 } else { 72 NSLog(@"Failed to get Cddb.bundle from %@", bundlePath); 73 } 74 } // for (i = 0; i < [searchPaths count]; i++) 75 76 // We link our view 77 if (cddbClass) { 78 if (![NSBundle loadNibNamed: nibName owner: self]) { 79 NSLog (@"FreeDB: Could not load nib \"%@\".", nibName); 80 [self dealloc]; 81 } else { 82 view = [window contentView]; 83 [view retain]; 84 85 siteList = [NSMutableArray new]; 86 87 // We get our defaults for this panel 88 [self initializeFromDefaults]; 89 90 singleInstance = self; 91 } 92 } 93 } 94 return singleInstance; 95} 96 97- (void) dealloc 98{ 99 singleInstance = nil; 100 TEST_RELEASE(view); 101 RELEASE(siteList); 102 [super dealloc]; 103} 104 105// access methods 106 107- (NSString *) name 108{ 109 return @"FreeDB"; 110} 111 112- (NSView *) view 113{ 114 return view; 115} 116 117// Data Source 118- (void) tableViewSelectionDidChange: (NSNotification *) not 119{ 120 [serverTextField setStringValue: [[siteList objectAtIndex: [[not object] selectedRow]] 121 objectForKey: @"site"]]; 122} 123 124- (int) numberOfRowsInTableView: (NSTableView *) aView 125{ 126 return [siteList count]; 127} 128 129- (id) tableView: (NSTableView *) aView 130 objectValueForTableColumn: (NSTableColumn *) aColumn 131 row: (int) row 132{ 133 return [[siteList objectAtIndex: row] objectForKey: [aColumn identifier]]; 134} 135 136 137- (void) listOfSites: (id)sender 138{ 139 /* insert your code here */ 140 Cddb *cddb; 141 NSArray *siteArray = nil; 142 int i; 143 144 cddb = [cddbClass new]; 145 146 [siteList removeAllObjects]; 147 // Always get list from main site (www.freedb.org). 148 [cddb setDefaultSite: @"http://freedb.freedb.org:80/~cddb/cddb.cgi"]; 149 150 if([cddb connect]) { 151 siteArray = [cddb sites]; 152 [cddb disconnect]; 153 } 154 155 if (siteArray == nil) { 156 NSRunAlertPanel(_(@"Can't get list from internet"), 157 _(@"Can't get the list of public freedb sites.\nMake sure you have internet connection and the connection to the official freedb site (www.freedb.org) works."), 158 _(@"OK"), 159 nil, 160 nil); 161 return; 162 } 163 164 for(i = 0; i < [siteArray count]; i++) { 165 NSMutableDictionary *dict = [NSMutableDictionary new]; 166 NSString *site; 167 if([[[siteArray objectAtIndex: i] objectForKey: @"protocol"] 168 isEqualToString: @"http"]) { 169 site = [NSString stringWithFormat: @"%@://%@:%@%@", 170 [[siteArray objectAtIndex: i] objectForKey: @"protocol"], 171 [[siteArray objectAtIndex: i] objectForKey: @"site"], 172 [[siteArray objectAtIndex: i] objectForKey: @"port"], 173 [[siteArray objectAtIndex: i] objectForKey: @"address"]]; 174 } else { 175 site = [NSString stringWithFormat: @"%@://%@:%@", 176 [[siteArray objectAtIndex: i] objectForKey: @"protocol"], 177 [[siteArray objectAtIndex: i] objectForKey: @"site"], 178 [[siteArray objectAtIndex: i] objectForKey: @"port"]]; 179 } 180 [dict setObject: site forKey: @"site"]; 181 [dict setObject: [[siteArray objectAtIndex: i] objectForKey: @"description"] 182 forKey: @"location"]; 183 [siteList addObject: dict]; 184 } 185 [serversTableView reloadData]; 186} 187 188- (void) saveChanges 189{ 190 [[NSUserDefaults standardUserDefaults] setObject: [serverTextField stringValue] 191 forKey: @"FreedbSite"]; 192} 193 194 195// 196// class methods 197// 198+ (id) singleInstance 199{ 200 if (singleInstance == nil) { 201 singleInstance = [[FreeDBView alloc] initWithNibName: @"FreeDB"]; 202 } 203 204 return singleInstance; 205} 206 207@end 208