| 1 | /* |
| 2 | * PushrSettings.m |
| 3 | * --------------- |
| 4 | * |
| 5 | * Author: Chris Lee <clee@mg8.org> |
| 6 | * License: GPL v2 <http://www.opensource.org/licenses/gpl-license.php> |
| 7 | */ |
| 8 | |
| 9 | #import <Foundation/Foundation.h> |
| 10 | #import <UIKit/CDStructures.h> |
| 11 | #import <UIKit/UIKit.h> |
| 12 | #import <UIKit/UIView.h> |
| 13 | #import <UIKit/UIView-Geometry.h> |
| 14 | #import <UIKit/UINavigationBar.h> |
| 15 | #import <UIKit/UITransitionView.h> |
| 16 | #import <UIKit/UIPreferencesTable.h> |
| 17 | #import <UIKit/UIPreferencesTextTableCell.h> |
| 18 | #import <UIKit/UIPreferencesControlTableCell.h> |
| 19 | #import <UIKit/UIPreferencesTableCell.h> |
| 20 | #import <UIKit/UISegmentedControl.h> |
| 21 | #import <UIKit/UISwitchControl.h> |
| 22 | #import <UIKit/UITextLabel.h> |
| 23 | #import <UIKit/UIAlertSheet.h> |
| 24 | #import <UIKit/UIWindow.h> |
| 25 | |
| 26 | #import "PushrSettings.h" |
| 27 | #import "Flickr.h" |
| 28 | |
| 29 | @implementation PushrGlobalTags |
| 30 | |
| 31 | - (void)show |
| 32 | { |
| 33 | struct CGRect settingsRect = [UIHardware fullScreenApplicationContentRect]; |
| 34 | settingsRect.origin.x = 0.0f; // */ settingsRect.size.width; |
| 35 | settingsRect.origin.y = 0.0f; |
| 36 | NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; |
| 37 | Flickr *flickr = [[Flickr alloc] initWithPushr: _pushr]; |
| 38 | |
| 39 | _availableTags = [[flickr tags] retain]; |
| 40 | _tagsView = [[UIView alloc] initWithFrame: settingsRect]; |
| 41 | _navBar = [[UINavigationBar alloc] initWithFrame: CGRectMake(0.0f, 0.0f, settingsRect.size.width, 44.0f)]; |
| 42 | [_navBar setBarStyle: 1]; |
| 43 | [_navBar setDelegate: self]; |
| 44 | [_navBar showLeftButton: nil withStyle: 0 rightButton: @"Done" withStyle: 0]; |
| 45 | |
| 46 | UINavigationItem *title = [[UINavigationItem alloc] initWithTitle: @"Tags"]; |
| 47 | [_navBar pushNavigationItem: title]; |
| 48 | [title release]; |
| 49 | |
| 50 | [_tagsView addSubview: _navBar]; |
| 51 | |
| 52 | _tagsTable = [[UIPreferencesTable alloc] initWithFrame: CGRectMake(0.0f, 44.0f, settingsRect.size.width, settingsRect.size.height - 44.0f)]; |
| 53 | [_tagsTable setDataSource: self]; |
| 54 | [_tagsTable setDelegate: self]; |
| 55 | [_tagsTable reloadData]; |
| 56 | [_tagsView addSubview: _tagsTable]; |
| 57 | |
| 58 | UITransitionView *transitionView = [[UITransitionView alloc] initWithFrame: settingsRect]; |
| 59 | [_mainWindow setContentView: transitionView]; |
| 60 | [transitionView transition: 0 toView: _prefView]; |
| 61 | [transitionView transition: 1 fromView: _prefView toView: _tagsView]; |
| 62 | [transitionView release]; |
| 63 | } |
| 64 | |
| 65 | - (id)initFromWindow: (UIWindow *)window withPushr: (MobilePushr *)pushr withView: (UIView *)view withTable: (UIPreferencesTable *)table |
| 66 | { |
| 67 | if (![super init]) |
| 68 | return nil; |
| 69 | |
| 70 | _pushr = [pushr retain]; |
| 71 | _mainWindow = window; |
| 72 | _prefView = view; |
| 73 | _prefTable = table; |
| 74 | |
| 75 | [self show]; |
| 76 | |
| 77 | return self; |
| 78 | } |
| 79 | |
| 80 | - (void)dealloc |
| 81 | { |
| 82 | [_availableTags release]; |
| 83 | [_navBar release]; |
| 84 | [_prefView release]; |
| 85 | [_prefTable release]; |
| 86 | [_tagsView release]; |
| 87 | [_tagsTable release]; |
| 88 | [_mainWindow release]; |
| 89 | [_pushr release]; |
| 90 | [super dealloc]; |
| 91 | } |
| 92 | |
| 93 | #pragma mark PushrGlobalTags delegates |
| 94 | |
| 95 | - (int)numberOfGroupsInPreferencesTable: (id)preferencesTable |
| 96 | { |
| 97 | return 1; |
| 98 | } |
| 99 | |
| 100 | - (int)preferencesTable: (UIPreferencesTable *)preferencesTable numberOfRowsInGroup: (int)group |
| 101 | { |
| 102 | return [_availableTags count]; |
| 103 | } |
| 104 | |
| 105 | - (id)preferencesTable: (UIPreferencesTable *)preferencesTable titleForGroup: (int)group |
| 106 | { |
| 107 | return @"Default tags for photos"; |
| 108 | } |
| 109 | |
| 110 | - (float)preferencesTable: (id)preferencesTable heightForRow:(int)row inGroup: (int)group withProposedHeight: (float)proposedHeight |
| 111 | { |
| 112 | return 48.0f; |
| 113 | } |
| 114 | |
| 115 | - (id)preferencesTable: (id)preferencesTable cellForRow: (int)row inGroup: (int)group |
| 116 | { |
| 117 | struct CGRect rect = [UIHardware fullScreenApplicationContentRect]; |
| 118 | NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; |
| 119 | |
| 120 | UIPreferencesTableCell *thisCell = [[UIPreferencesTableCell alloc] initWithFrame: CGRectMake(0.0f, 0.0f, rect.size.width, 48.0f)]; |
| 121 | [thisCell setTitle: [_availableTags objectAtIndex: row]]; |
| 122 | [thisCell setChecked: [[defaults arrayForKey: @"defaultTags"] containsObject: [_availableTags objectAtIndex: row]]]; |
| 123 | return [thisCell autorelease]; |
| 124 | } |
| 125 | |
| 126 | - (void)tableRowSelected: (NSNotification *)notification |
| 127 | { |
| 128 | id row = [_tagsTable cellAtRow: [_tagsTable selectedRow] column: 0]; |
| 129 | [row setChecked: ![row isChecked]]; |
| 130 | } |
| 131 | |
| 132 | - (void)navigationBar: (UINavigationBar *)navBar buttonClicked: (int)button |
| 133 | { |
| 134 | NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; |
| 135 | struct CGRect settingsRect = [UIHardware fullScreenApplicationContentRect]; |
| 136 | UITransitionView *transitionView = [[UITransitionView alloc] initWithFrame: CGRectMake(0.0f, 0.0f, settingsRect.size.width, settingsRect.size.height)]; |
| 137 | [_mainWindow setContentView: transitionView]; |
| 138 | [transitionView transition: 0 toView: _tagsView]; |
| 139 | |
| 140 | NSMutableArray *checkedTags = [NSMutableArray array]; |
| 141 | for (int tagIndex = 0; tagIndex < [_availableTags count]; tagIndex++) |
| 142 | if ([[_tagsTable cellAtRow: tagIndex + 1 column: 0] isChecked]) |
| 143 | [checkedTags addObject: [_availableTags objectAtIndex: tagIndex]]; |
| 144 | |
| 145 | [defaults setObject: checkedTags forKey: @"defaultTags"]; |
| 146 | [transitionView transition: 2 fromView: _tagsView toView: _prefView]; |
| 147 | [_prefTable reloadData]; |
| 148 | [_prefTable selectRow: -1 byExtendingSelection: NO]; |
| 149 | |
| 150 | [transitionView release]; |
| 151 | } |
| 152 | |
| 153 | @end |
| 154 | |
| 155 | @implementation PushrGlobalPrivacy |
| 156 | |
| 157 | - (void)show |
| 158 | { |
| 159 | struct CGRect settingsRect = [UIHardware fullScreenApplicationContentRect]; |
| 160 | settingsRect.origin.x = 0.0f; // */ settingsRect.size.width; |
| 161 | settingsRect.origin.y = 0.0f; |
| 162 | NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; |
| 163 | |
| 164 | _privacyView = [[UIView alloc] initWithFrame: settingsRect]; |
| 165 | _navBar = [[UINavigationBar alloc] initWithFrame: CGRectMake(0.0f, 0.0f, settingsRect.size.width, 44.0f)]; |
| 166 | [_navBar setBarStyle: 1]; |
| 167 | [_navBar setDelegate: self]; |
| 168 | [_navBar showLeftButton: nil withStyle: 0 rightButton: @"Done" withStyle: 0]; |
| 169 | |
| 170 | UINavigationItem *title = [[UINavigationItem alloc] initWithTitle: @"Privacy"]; |
| 171 | [_navBar pushNavigationItem: title]; |
| 172 | [title release]; |
| 173 | |
| 174 | [_privacyView addSubview: _navBar]; |
| 175 | |
| 176 | _privacyTable = [[UIPreferencesTable alloc] initWithFrame: CGRectMake(0.0f, 44.0f, settingsRect.size.width, settingsRect.size.height - 44.0f)]; |
| 177 | [_privacyTable setDataSource: self]; |
| 178 | [_privacyTable setDelegate: self]; |
| 179 | [_privacyTable reloadData]; |
| 180 | [_privacyView addSubview: _privacyTable]; |
| 181 | |
| 182 | UITransitionView *transitionView = [[UITransitionView alloc] initWithFrame: settingsRect]; |
| 183 | [_mainWindow setContentView: transitionView]; |
| 184 | [transitionView transition: 0 toView: _prefView]; |
| 185 | [transitionView transition: 1 fromView: _prefView toView: _privacyView]; |
| 186 | [transitionView release]; |
| 187 | } |
| 188 | |
| 189 | - (id)initFromWindow: (UIWindow *)window withPushr: (MobilePushr *)pushr withView: (UIView *)view withTable: (UIPreferencesTable *)table |
| 190 | { |
| 191 | if (![super init]) |
| 192 | return nil; |
| 193 | |
| 194 | _pushr = [pushr retain]; |
| 195 | _mainWindow = window; |
| 196 | _prefView = view; |
| 197 | _prefTable = table; |
| 198 | _availablePrivacy = [[NSArray arrayWithObjects: @"Private", @"Friends", @"Family", @"Public", nil] retain]; |
| 199 | |
| 200 | [self show]; |
| 201 | |
| 202 | return self; |
| 203 | } |
| 204 | |
| 205 | - (void)dealloc |
| 206 | { |
| 207 | [_availablePrivacy release]; |
| 208 | [_navBar release]; |
| 209 | [_prefView release]; |
| 210 | [_prefTable release]; |
| 211 | [_privacyTable release]; |
| 212 | [_privacyView release]; |
| 213 | [_mainWindow release]; |
| 214 | [_pushr release]; |
| 215 | [super dealloc]; |
| 216 | } |
| 217 | |
| 218 | #pragma mark PushrGlobalPrivacy delegates |
| 219 | |
| 220 | - (int)numberOfGroupsInPreferencesTable: (id)preferencesTable |
| 221 | { |
| 222 | return 1; |
| 223 | } |
| 224 | |
| 225 | - (int)preferencesTable: (UIPreferencesTable *)preferencesTable numberOfRowsInGroup: (int)group |
| 226 | { |
| 227 | return [_availablePrivacy count]; |
| 228 | } |
| 229 | |
| 230 | - (id)preferencesTable: (UIPreferencesTable *)preferencesTable titleForGroup: (int)group |
| 231 | { |
| 232 | return @"Default privacy for photos"; |
| 233 | } |
| 234 | |
| 235 | - (float)preferencesTable: (id)preferencesTable heightForRow:(int)row inGroup: (int)group withProposedHeight: (float)proposedHeight |
| 236 | { |
| 237 | return 48.0f; |
| 238 | } |
| 239 | |
| 240 | - (BOOL)table: (id)table shouldIndentRow: (int)row |
| 241 | { |
| 242 | if (row == 1 || row == 2) |
| 243 | return YES; |
| 244 | return NO; |
| 245 | } |
| 246 | |
| 247 | - (id)preferencesTable: (id)preferencesTable cellForRow: (int)row inGroup: (int)group |
| 248 | { |
| 249 | struct CGRect rect = [UIHardware fullScreenApplicationContentRect]; |
| 250 | NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; |
| 251 | |
| 252 | UIPreferencesTableCell *thisCell = [[UIPreferencesTableCell alloc] initWithFrame: CGRectMake(0.0f, 0.0f, rect.size.width, 48.0f)]; |
| 253 | // Ugly, ugly hack. If shouldIndentRow just worked properly this would be unnecessary. |
| 254 | if (row == 1 || row == 2) |
| 255 | [thisCell setTitle: [NSString stringWithFormat: @" %@", [_availablePrivacy objectAtIndex: row]]]; |
| 256 | else |
| 257 | [thisCell setTitle: [_availablePrivacy objectAtIndex: row]]; |
| 258 | [thisCell setChecked: [[defaults arrayForKey: @"defaultPrivacy"] containsObject: [_availablePrivacy objectAtIndex: row]]]; |
| 259 | return [thisCell autorelease]; |
| 260 | } |
| 261 | |
| 262 | /* |
| 263 | * This is a little tricky, it turns out! There are invalid combinations of privacy settings - |
| 264 | * for example, 'Friends' + 'Public' simply wouldn't make sense. |
| 265 | * This code allows the user to toggle friends/family rows, but prevents invalid combinations. |
| 266 | */ |
| 267 | - (void)tableRowSelected: (NSNotification *)notification |
| 268 | { |
| 269 | id row = [_privacyTable cellAtRow: [_privacyTable selectedRow] column: 0]; |
| 270 | switch ([_privacyTable selectedRow]) { |
| 271 | case 1: |
| 272 | case 2: |
| 273 | case 3: { |
| 274 | [row setChecked: ![row isChecked]]; |
| 275 | [[_privacyTable cellAtRow: 1 column: 0] setChecked: YES]; |
| 276 | [[_privacyTable cellAtRow: 4 column: 0] setChecked: NO]; |
| 277 | break; |
| 278 | } |
| 279 | case 4: { |
| 280 | for (int currentRow = 1; currentRow < 4; currentRow++) { |
| 281 | [[_privacyTable cellAtRow: currentRow column: 0] setChecked: NO]; |
| 282 | } |
| 283 | [row setChecked: YES]; |
| 284 | } |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | - (void)navigationBar: (UINavigationBar *)navBar buttonClicked: (int)button |
| 289 | { |
| 290 | NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; |
| 291 | struct CGRect settingsRect = [UIHardware fullScreenApplicationContentRect]; |
| 292 | UITransitionView *transitionView = [[UITransitionView alloc] initWithFrame: CGRectMake(0.0f, 0.0f, settingsRect.size.width, settingsRect.size.height)]; |
| 293 | [_mainWindow setContentView: transitionView]; |
| 294 | [transitionView transition: 0 toView: _privacyView]; |
| 295 | |
| 296 | NSMutableArray *checkedPrivacySettings = [NSMutableArray array]; |
| 297 | for (int settingIndex = 0; settingIndex < [_availablePrivacy count]; settingIndex++) |
| 298 | if ([[_privacyTable cellAtRow: settingIndex + 1 column: 0] isChecked]) |
| 299 | [checkedPrivacySettings addObject: [_availablePrivacy objectAtIndex: settingIndex]]; |
| 300 | |
| 301 | [defaults setObject: checkedPrivacySettings forKey: @"defaultPrivacy"]; |
| 302 | [transitionView transition: 2 fromView: _privacyView toView: _prefView]; |
| 303 | [_prefTable reloadData]; |
| 304 | [_prefTable selectRow: -1 byExtendingSelection: NO]; |
| 305 | |
| 306 | [transitionView release]; |
| 307 | } |
| 308 | |
| 309 | @end |
| 310 | |
| 311 | @implementation PushrSettings |
| 312 | |
| 313 | - (void)show |
| 314 | { |
| 315 | struct CGRect settingsRect = [UIHardware fullScreenApplicationContentRect]; |
| 316 | settingsRect.origin.x = 0.0f; // */ settingsRect.size.width; |
| 317 | settingsRect.origin.y = 0.0f; |
| 318 | NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; |
| 319 | |
| 320 | _prefView = [[UIView alloc] initWithFrame: settingsRect]; |
| 321 | UITransitionView *transitionView = [[UITransitionView alloc] initWithFrame: settingsRect]; |
| 322 | |
| 323 | _navBar = [[UINavigationBar alloc] initWithFrame: CGRectMake(0.0f, 0.0f, settingsRect.size.width, 44.0f)]; |
| 324 | [_navBar setBarStyle: 1]; |
| 325 | [_navBar setDelegate: self]; |
| 326 | [_navBar showLeftButton: nil withStyle: 0 rightButton: @"Done" withStyle: 1]; |
| 327 | |
| 328 | UINavigationItem *title = [[UINavigationItem alloc] initWithTitle: @"Settings"]; |
| 329 | [_navBar pushNavigationItem: title]; |
| 330 | [title release]; |
| 331 | |
| 332 | [_prefView addSubview: _navBar]; |
| 333 | |
| 334 | _prefTable = [[UIPreferencesTable alloc] initWithFrame: CGRectMake(0.0f, 44.0f, settingsRect.size.width, settingsRect.size.height - 44.0f)]; |
| 335 | [_prefTable setDataSource: self]; |
| 336 | [_prefTable setDelegate: self]; |
| 337 | [_prefTable reloadData]; |
| 338 | [_prefView addSubview: _prefTable]; |
| 339 | |
| 340 | _photoView = [[_mainWindow contentView] retain]; |
| 341 | |
| 342 | // This doesn't seem like the right way to do this but it seems to work... |
| 343 | [_mainWindow setContentView: transitionView]; |
| 344 | [transitionView transition: 0 toView: _photoView]; |
| 345 | [transitionView transition: 1 fromView: _photoView toView: _prefView]; |
| 346 | [transitionView release]; |
| 347 | } |
| 348 | |
| 349 | #pragma mark PushrSettings delegates |
| 350 | - (void)navigationBar: (UINavigationBar *)navBar buttonClicked: (int)button |
| 351 | { |
| 352 | struct CGRect settingsRect = [UIHardware fullScreenApplicationContentRect]; |
| 353 | UITransitionView *transitionView = [[UITransitionView alloc] initWithFrame: CGRectMake(0.0f, 0.0f, settingsRect.size.width, settingsRect.size.height)]; |
| 354 | [_mainWindow setContentView: transitionView]; |
| 355 | [transitionView transition: 0 toView: _prefView]; |
| 356 | |
| 357 | switch (button) { |
| 358 | default: { |
| 359 | [transitionView transition: 2 fromView: _prefView toView: _photoView]; |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | [transitionView release]; |
| 364 | } |
| 365 | |
| 366 | - (int)numberOfGroupsInPreferencesTable:(id)preferencesTable { return 1; } |
| 367 | |
| 368 | - (int)preferencesTable: (UIPreferencesTable *)preferencesTable numberOfRowsInGroup: (int)group |
| 369 | { |
| 370 | switch (group) { |
| 371 | default: { |
| 372 | return 2; |
| 373 | } |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | - (id)preferencesTable: (UIPreferencesTable *)preferencesTable cellForRow: (int)row inGroup: (int)group |
| 378 | { |
| 379 | if (group > 0) |
| 380 | return nil; |
| 381 | |
| 382 | NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; |
| 383 | |
| 384 | switch (row) { |
| 385 | case 0: { |
| 386 | id tagsCell = [[UIPreferencesTableCell alloc] initWithFrame: CGRectMake(0.0f, 0.0f, 320.0f, 48.0f)]; |
| 387 | [tagsCell setTitle: @"Tags"]; |
| 388 | [tagsCell setValue: [[defaults arrayForKey: @"defaultTags"] componentsJoinedByString: @", "]]; |
| 389 | [tagsCell setShowDisclosure: YES]; |
| 390 | return [tagsCell autorelease]; |
| 391 | } |
| 392 | case 1: { |
| 393 | id privacyCell = [[UIPreferencesTableCell alloc] initWithFrame: CGRectMake(0.0f, 0.0f, 320.0f, 48.0f)]; |
| 394 | [privacyCell setTitle: @"Privacy"]; |
| 395 | [privacyCell setValue: [[defaults arrayForKey: @"defaultPrivacy"] componentsJoinedByString: @" + "]]; |
| 396 | [privacyCell setShowDisclosure: YES]; |
| 397 | return [privacyCell autorelease]; |
| 398 | } |
| 399 | default: |
| 400 | return nil; |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | - (id)preferencesTable: (UIPreferencesTable *)preferencesTable titleForGroup: (int)group |
| 405 | { |
| 406 | return @"Defaults for all photos"; |
| 407 | } |
| 408 | |
| 409 | - (float)preferencesTable: (id)preferencesTable heightForRow:(int)row inGroup: (int)group withProposedHeight: (float)proposedHeight |
| 410 | { |
| 411 | return 48.0f; |
| 412 | } |
| 413 | |
| 414 | - (void)tableRowSelected:(NSNotification *)notification |
| 415 | { |
| 416 | int i = [_prefTable selectedRow]; |
| 417 | NSLog(@"Selected row %d", i); |
| 418 | switch (i) { |
| 419 | case 1: { |
| 420 | [[PushrGlobalTags alloc] initFromWindow: _mainWindow withPushr: _pushr withView: _prefView withTable: _prefTable]; |
| 421 | break; |
| 422 | } |
| 423 | case 2: { |
| 424 | [[PushrGlobalPrivacy alloc] initFromWindow: _mainWindow withPushr: _pushr withView: _prefView withTable: _prefTable]; |
| 425 | break; |
| 426 | } |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | - (id)initFromWindow: (UIWindow *)window withPushr: (MobilePushr *)pushr |
| 431 | { |
| 432 | if (![super init]) |
| 433 | return nil; |
| 434 | |
| 435 | _pushr = [pushr retain]; |
| 436 | _mainWindow = window; |
| 437 | |
| 438 | [self show]; |
| 439 | |
| 440 | return self; |
| 441 | } |
| 442 | |
| 443 | - (void)dealloc |
| 444 | { |
| 445 | [_navBar release]; |
| 446 | [_prefView release]; |
| 447 | [_prefTable release]; |
| 448 | [_photoView release]; |
| 449 | [_mainWindow release]; |
| 450 | [_pushr release]; |
| 451 | [super dealloc]; |
| 452 | } |
| 453 | |
| 454 | @end |