1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
|
/*
* MobilePushr.m
* -------------
* The main UIApplication subclass - everything starts and ends here.
*
* Author: Chris Lee <clee@mg8.org>
* License: GPL v2 <http://www.opensource.org/licenses/gpl-license.php>
*/
#import <Foundation/Foundation.h>
#import <CoreFoundation/CoreFoundation.h>
#import <GraphicsServices/GraphicsServices.h>
#import <UIKit/CDStructures.h>
#import <UIKit/UIKit.h>
#import <UIKit/UIPushButton.h>
#import <UIKit/UIPushButton-Original.h>
#import <UIKit/UIControl.h>
#import <UIKit/UITableCell.h>
#import <UIKit/UIPreferencesTableCell.h>
#import <UIKit/UIHardware.h>
#import <UIKit/UIImage.h>
#import <UIKit/UIImageView.h>
#import <UIKit/UIView.h>
#import <UIKit/UIWindow.h>
#import <UIKit/UIProgressBar.h>
#import <UIKit/UITextField.h>
#import <UIKit/UITextTraits.h>
#import <UIKit/UIAlertSheet.h>
#import <UIKit/UIValueButton.h>
#import <UIKit/UIView-Hierarchy.h>
#import <UIKit/UIView-Rendering.h>
#import <UIKit/UIThreePartButton.h>
#import <UIKit/UIThreePartImageView.h>
#import "MobilePushr.h"
#import "PushrNetUtil.h"
#import "Flickr.h"
#import "PushrSettings.h"
#import "PushablePhotos.h"
#import "ExtendedAttributes.h"
#pragma mark Island of Misfit Toys
typedef enum {
kUIControlEventMouseDown = 1 << 0,
kUIControlEventMouseMovedInside = 1 << 2, // mouse moved inside control target
kUIControlEventMouseMovedOutside = 1 << 3, // mouse moved outside control target
kUIControlEventMouseUpInside = 1 << 6, // mouse up inside control target
kUIControlEventMouseUpOutside = 1 << 7, // mouse up outside control target
kUIControlAllEvents = (kUIControlEventMouseDown | kUIControlEventMouseMovedInside | kUIControlEventMouseMovedOutside | kUIControlEventMouseUpInside | kUIControlEventMouseUpOutside)
} UIControlEventMasks;
@implementation MobilePushr
#pragma mark MobilePushr Methods
- (NSArray *)cameraRollPhotos
{
_settings = [NSUserDefaults standardUserDefaults];
NSString *cameraRollDir = [NSHomeDirectory() stringByAppendingPathComponent:@"Media/DCIM"];
NSMutableArray *photos = [NSMutableArray array];
NSDirectoryEnumerator *dirEnum = [[NSFileManager defaultManager] enumeratorAtPath: cameraRollDir];
NSString *jpg;
while ((jpg = [dirEnum nextObject])) {
if ([[jpg pathExtension] isEqualToString: @"JPG"]) {
NSArray *attributes = [ExtendedAttributes allKeysAtPath: [cameraRollDir stringByAppendingPathComponent: jpg]];
if ([attributes containsObject: MIGRATED_ATTRIBUTE])
continue;
// TODO: Ignore this file for now, but in the future, support showing previously-ignored files
if ([attributes containsObject: IGNORED_ATTRIBUTE])
continue;
if ([attributes containsObject: PUSHED_ATTRIBUTE])
continue;
[photos addObject: [cameraRollDir stringByAppendingPathComponent: jpg]];
}
}
return [NSArray arrayWithArray: photos];
}
- (void)popupFailureAlertSheet
{
UIAlertSheet *alertSheet = [[UIAlertSheet alloc] initWithFrame: CGRectMake(0.0f, 0.0f, 320.0f, 240.0f)];
[alertSheet setTitle: @"Bad news, everyone"];
[alertSheet setBodyText: @"Somewhere, there's a leak in the pipes, and this application's not Plumbr..."];
[alertSheet addButtonWithTitle: @"Accept"];
[alertSheet setDelegate: self];
[alertSheet setRunsModal: YES];
[alertSheet popupAlertAnimated: YES];
}
- (void)popupEmptyCameraRollAlertSheet
{
UIAlertSheet *alertSheet = [[UIAlertSheet alloc] initWithFrame: CGRectMake(0.0f, 0.0f, 320.0f, 240.0f)];
[alertSheet setTitle: @"There are no photos to push"];
[alertSheet setBodyText: @"Use the Camera application to put photos into the Camera Roll album."];
[alertSheet addButtonWithTitle: @"Quit Pushr"];
[alertSheet setDelegate: self];
[alertSheet setRunsModal: YES];
[alertSheet popupAlertAnimated: YES];
}
- (void)migratePhotoMetadata
{
_settings = [NSUserDefaults standardUserDefaults];
NSString *cameraRollDir = [NSHomeDirectory() stringByAppendingPathComponent:@"Media/DCIM"];
int currentPhotoIndex = 0;
NSDirectoryEnumerator *dirEnum = [[NSFileManager defaultManager] enumeratorAtPath: cameraRollDir];
NSString *jpg;
while ((jpg = [dirEnum nextObject])) {
if ([[jpg pathExtension] isEqualToString: @"JPG"]) {
currentPhotoIndex = [[[[jpg pathComponents] lastObject] substringWithRange: NSMakeRange(4, 4)] intValue];
if (currentPhotoIndex <= [_settings integerForKey: @"lastPushedPhotoIndex"])
[ExtendedAttributes setString: @"true" forKey: MIGRATED_ATTRIBUTE atPath: [cameraRollDir stringByAppendingPathComponent: jpg]];
}
}
[_settings removeObjectForKey: @"lastPushedPhotoIndex"];
}
- (void)checkNetworkType
{
_netUtil = [[PushrNetUtil alloc] initWithPushr: self];
if ([_netUtil hasWiFi])
return;
if ([_netUtil hasEDGE])
[_netUtil warnUserAboutSlowEDGE];
else
[_netUtil drownWithoutNetwork];
}
- (void)checkCameraRoll
{
_settings = [NSUserDefaults standardUserDefaults];
if ([_settings integerForKey: @"lastPushedPhotoIndex"]) {
[self migratePhotoMetadata];
[_settings removeObjectForKey: @"lastPushedPhotoIndex"];
}
if ([[self cameraRollPhotos] count] == 0)
[self popupEmptyCameraRollAlertSheet];
}
- (void)loadConfiguration
{
_settings = [NSUserDefaults standardUserDefaults];
_flickr = [[Flickr alloc] initWithPushr: self];
if ([_settings boolForKey: @"sentToGetToken"] != TRUE) {
NSLog(@"Have to send the user to Flickr to get permission to upload pics.");
[_flickr sendToGrantPermission];
return;
}
if ([_settings stringForKey: @"frob"] != nil) {
NSLog(@"We had a frob - trade it in for a token, the user's NSID, and username.");
[_flickr tradeFrobForToken];
}
if ([_settings stringForKey: @"token"] != nil) {
NSLog(@"We have a token - test it to make sure it works.");
[_flickr checkToken];
}
NSLog(@"Our token is: %@", [_settings stringForKey: @"token"]);
}
- (void)loadUserInterface
{
struct CGRect hwRect = [UIHardware fullScreenApplicationContentRect];
_window = [[UIWindow alloc] initWithContentRect: hwRect];
struct CGRect appRect = CGRectMake(0.0f, 0.0f, hwRect.size.width, hwRect.size.height);
UIView *mainView = [[UIView alloc] initWithFrame: appRect];
[_window orderFront: self];
[_window makeKey: self];
[_window setContentView: mainView];
[_window _setHidden: NO];
_pushablePhotos = [[PushablePhotos alloc] initWithFrame: appRect application: self inWindow: _window];
[mainView addSubview: _pushablePhotos];
struct CGRect topBarRect = CGRectMake(0.0f, 0.0f, appRect.size.width, 44.0f);
UINavigationBar *topBar = [[UINavigationBar alloc] initWithFrame: topBarRect];
[topBar setBarStyle: 1];
[topBar setDelegate: self];
[topBar showLeftButton: nil withStyle: 0 rightButton: @"Settings" withStyle: 0];
[mainView addSubview: topBar];
UINavigationItem *topBarTitle = [[UINavigationItem alloc] initWithTitle: @"Pushr"];
GSFontRef titleFont = GSFontCreateWithName("Helvetica", kGSFontTraitBold, 24.0f);
[topBarTitle setFont: titleFont];
CFRelease(titleFont);
struct CGRect botBarRect = CGRectMake(0.0f, (appRect.size.height - 96.0f), appRect.size.width, 96.0f);
UIThreePartImageView *bottomBar = [[UIThreePartImageView alloc] initWithFrame: botBarRect];
[bottomBar setImage: [UIImage imageNamed: @"bottombar.png"]];
CDAnonymousStruct4 barSlices = {
.left = { .origin = { .x = 0.0f, .y = 0.0f }, .size = { .width = 2.0f, .height = 96.0f } },
.middle = { .origin = { .x = 2.0f, .y = 0.0f }, .size = { .width = 2.0f, .height = 96.0f } },
.right = { .origin = { .x = 4.0f, .y = 0.0f }, .size = { .width = 2.0f, .height = 96.0f } },
};
[bottomBar setSlices: barSlices];
[mainView addSubview: bottomBar];
_button = [[[UIThreePartButton alloc] initWithTitle: @"Push to Flickr" autosizesToFit: YES] autorelease];
GSFontRef buttonFont = GSFontCreateWithName("Helvetica", kGSFontTraitBold, 22.0f);
[_button setTitleFont: buttonFont];
CFRelease(buttonFont);
float blackColor[4] = { 0.0f, 0.0f, 0.0f, 1.0f };
struct CGRect buttonRect = CGRectMake(20.0f, (appRect.size.height - 74.0f), appRect.size.width - 40.0f, 52.0f);
[_button setFrame: buttonRect];
[_button setPressedBackgroundImage: [UIImage imageNamed: @"mainbutton_pressed.png"]];
[_button setBackgroundImage: [UIImage imageNamed: @"mainbutton.png"]];
// [_button setImage:[UIImage imageNamed:@"refresh.png"]];
// Pieces as deduced from the ChooseAudioPhone PNG from MobilePhone.app
CDAnonymousStruct4 buttonPieces = {
.left = { .origin = { .x = 0.0f, .y = 0.0f }, .size = { .width = 14.0f, .height = 52.0f } },
.middle = { .origin = { .x = 15.0f, .y = 0.0f }, .size = { .width = 2.0f, .height = 52.0f } },
.right = { .origin = { .x = 17.0f, .y = 0.0f }, .size = { .width = 14.0f, .height = 52.0f } },
};
[_button setBackgroundSlices: buttonPieces];
[_button setShadowColor: CGColorCreate(CGColorSpaceCreateDeviceRGB(), blackColor)];
[_button setShadowOffset: -1.0f];
[_button setDrawsShadow: YES];
[_button addTarget: self action: @selector(buttonReleased) forEvents: kUIControlEventMouseUpInside];
[_button setDrawContentsCentered: YES];
[_button setEnabled: YES];
[mainView addSubview: _button];
[UIView beginAnimations: nil];
[UIView setAnimationCurve: kUIAnimationCurveEaseIn];
[UIView setAnimationDuration: 1.0];
[topBar pushNavigationItem: topBarTitle];
[UIView endAnimations];
[topBar release];
[topBarTitle release];
[bottomBar release];
[mainView release];
_thumbnailView = nil;
}
- (void)loadSettings
{
[[PushrSettings alloc] initFromWindow: _window withPushr: self];
}
- (void)buttonReleased
{
[_button setEnabled: NO];
[_button setBackgroundImage: [UIImage imageNamed: @"mainbutton_inactive.png"]];
id mainView = [_button superview];
float blackColor[4] = { 0.0f, 0.0f, 0.0f, 0.9f };
float transparent[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
float white[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
_shade = [[UIView alloc] initWithFrame: [mainView frame]];
[_shade setBackgroundColor: CGColorCreate(CGColorSpaceCreateDeviceRGB(), blackColor)];
[mainView addSubview: _shade];
struct CGRect hwRect = [UIHardware fullScreenApplicationContentRect];
_label = [[UITextLabel alloc] initWithFrame: CGRectMake(hwRect.origin.x + 20.0f, hwRect.origin.y + 60.0f, hwRect.size.width - 40.0f, 20.0f)];
[_label setText: @"Please Wait"];
[_label setBackgroundColor: CGColorCreate(CGColorSpaceCreateDeviceRGB(), transparent)];
[_label setColor: CGColorCreate(CGColorSpaceCreateDeviceRGB(), white)];
[_label setCentersHorizontally: YES];
[_shade addSubview: _label];
_progress = [[UIProgressBar alloc] initWithFrame: CGRectMake(hwRect.origin.x + 20.0f, hwRect.origin.y + 80.0f, hwRect.size.width - 40.0f, 60.0f)];
[_progress setProgress: 0];
[_progress setStyle: 0];
[_shade addSubview: _progress];
[NSThread detachNewThreadSelector: @selector(triggerUpload:) toTarget: _flickr withObject: [self cameraRollPhotos]];
}
- (void)startingToPush: (NSString *)photoPath
{
struct CGRect hwRect = [UIHardware fullScreenApplicationContentRect];
NSString *thumbnailPath = [[photoPath stringByDeletingPathExtension] stringByAppendingPathExtension: @"THM"];
UIImage *thumbnailImage = [UIImage imageAtPath: thumbnailPath];
struct CGRect thumbnailRect = CGRectMake(hwRect.origin.x + ((hwRect.size.width - [thumbnailImage size].width) / 2.0), hwRect.origin.y + ((hwRect.size.height - [thumbnailImage size].height) / 2.0), [thumbnailImage size].width, [thumbnailImage size].height);
_thumbnailView = [[UIImageView alloc] initWithFrame: thumbnailRect];
[_thumbnailView setImage: thumbnailImage];
[_shade addSubview: _thumbnailView];
}
- (void)donePushing: (NSString *)photoPath
{
[_thumbnailView removeFromSuperview];
[_thumbnailView release];
}
- (void)setLabelText: (NSString *)labelText
{
[_label setText: labelText];
}
- (void)updateProgress: (NSNumber *)currentProgress
{
[_progress setProgress: [currentProgress floatValue]];
}
- (void)allDone: (NSArray *)responses
{
[_progress removeFromSuperview];
[_label removeFromSuperview];
[_shade removeFromSuperview];
[_pushablePhotos emptyRoll];
NSMutableArray *photoIDs = [NSMutableArray array];
NSEnumerator *enumerator = [responses objectEnumerator];
id responseString = nil;
while ((responseString = [enumerator nextObject])) {
NSLog(@"ResponseData: %@", responseString);
[photoIDs addObject: [[[_flickr getXMLNodesNamed: @"photoid" fromResponse: [responseString dataUsingEncoding: NSUTF8StringEncoding]] lastObject] stringValue]];
}
[_pushablePhotos promptUserToEditPhotos: photoIDs];
[_button setEnabled: YES];
[_button setBackgroundImage: [UIImage imageNamed: @"mainbutton.png"]];
}
- (void)applicationDidFinishLaunching: (id) unused
{
[self checkNetworkType];
[self checkCameraRoll];
[self loadConfiguration];
[self loadUserInterface];
}
#pragma mark Delegate functions here
- (void)alertSheet: (UIAlertSheet *)sheet buttonClicked: (int)button
{
[sheet dismiss];
[sheet release];
switch (button) {
default: {
[self terminate];
}
}
}
- (void)navigationBar: (UINavigationBar *)navBar buttonClicked: (int)button
{
switch (button) {
// Show the Settings page
case 0: {
NSLog(@"Calling loadSettings");
[self loadSettings];
break;
}
// Shouldn't happen since there is no left navbar button
case 1: {
NSLog(@"Shouldn't be here...");
break;
}
}
}
- (void)dealloc
{
[_progress release];
[_label release];
[_shade release];
[_button release];
[_window release];
[_flickr release];
[_netUtil release];
[super dealloc];
}
@end
|