/* Copyright (c) 2008, Adobe Systems Incorporated All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of Adobe Systems Incorporated nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ import com.adobe.onair.flump.FlickrPeoplePager; import com.adobe.onair.flump.FlickrPhotoListDownloader; import com.adobe.onair.flump.FlumpSettings; import com.adobe.onair.flump.events.FlickrPageErrorEvent; import com.adobe.onair.flump.events.FlickrPageEvent; import com.adobe.onair.flump.events.FlickrPageProgressEvent; import com.adobe.onair.flump.events.FlumpProgressEvent; import com.adobe.utils.StringUtil; import com.adobe.webapis.flickr.FlickrService; import com.adobe.webapis.flickr.User; import com.adobe.webapis.flickr.events.FlickrResultEvent; import com.adobe.webapis.flickr.methodgroups.People; import com.adobe.webapis.flickr.authorization.events.AuthorizationEvent; import com.adobe.webapis.flickr.authorization.AuthorizationView; import flash.events.Event; import flash.filesystem.File; import flash.filesystem.FileMode; import flash.filesystem.FileStream; import flash.net.URLRequest; import flash.net.navigateToURL; import mx.core.IFlexDisplayObject; import mx.managers.PopUpManager; private var f:FlickrPeoplePager; private const ABOUT_URL:String = "http://code.google.com/p/onairbustour/wiki/flump"; //replace this with your own API key private var apiKey:String = "97b54c0828eb28a87131286419140167"; private var apiSecret:String = "d2dd2a19f5a4310f"; private var downloader:FlickrPhotoListDownloader; //dir reference where images will be saved private var saveDIR:File; //dedult name of dir private var saveDIRName:String = "flump"; //dir reference for browsing for directory private var browseDirRef:File; //private var user:User; private var defaultDIR:File; private static const MAIN_STATE:String = ""; private static const DOWNLOAD_STATE:String = "download"; private static const PAGES_STATE:String = "pages"; private var flickr:FlickrService; private var people:People; //private var authorizationToken:String; private var settings:FlumpSettings; private static const SETTINGS_FILE_NAME:String = "flump.settings"; // Embedded PNG for the main interface [Embed(source="/images/chrome.png", scaleGridTop="46", scaleGridBottom="282", scaleGridLeft="90", scaleGridRight="409")] [Bindable] public var chrome:Class; //initialize application private function onCreationComplete():void { setVersionInfo(); loadSettings(); updateLoginLink(); defaultDIR = File.desktopDirectory.resolvePath(saveDIRName); saveDIR = defaultDIR; updateDirPath(saveDIR); updateNameMask(); } private function setVersionInfo():void { var appDescriptor:XML = NativeApplication.nativeApplication.applicationDescriptor; var ns:Namespace = appDescriptor.namespace(); var appVersion:String = appDescriptor.ns::version; versionField.text = appVersion; } private function onLoginClick():void { if(settings.flickrAuthorizationToken == null) { openAuthorization(); } else { settings.flickrAuthorizationToken = null; settings.user = null; updateLoginLink(); saveSettings(); } } private function saveSettings():void { var f:File = File.applicationStorageDirectory.resolvePath(SETTINGS_FILE_NAME); var fs:FileStream = new FileStream(); fs.open(f, FileMode.WRITE); fs.writeObject(settings); fs.close(); } private function loadSettings():void { var f:File = File.applicationStorageDirectory.resolvePath(SETTINGS_FILE_NAME); if(!f.exists) { settings = new FlumpSettings(); return; } var fs:FileStream = new FileStream(); fs.open(f, FileMode.READ); settings = fs.readObject() as FlumpSettings; fs.close(); } //open the authorization panel private function openAuthorization():void { //look into making this a NativeWindow var p:IFlexDisplayObject = PopUpManager.createPopUp(this, AuthorizationView, true); var auth:AuthorizationView = AuthorizationView(p); auth.flickrAPIKey = apiKey; auth.flickrAPISecret = apiSecret; auth.isPopUp = false; p.addEventListener(Event.CLOSE, onAuthorizationClose); p.addEventListener(AuthorizationEvent.AUTHORIZATION_COMPLETE, onAuthorizationComplete); PopUpManager.centerPopUp(p); } private function onAuthorizationClose(e:Event):void { PopUpManager.removePopUp(IFlexDisplayObject(e.target)); } private static const LOGIN_LABEL:String = "login"; private function updateLoginLink():void { if(settings.user != null) { loginLink.label = settings.user.username; userIDField.text = settings.user.username; usernameRadio.selected = true; } else { loginLink.label = LOGIN_LABEL; } } private function onLoginRollOver():void { loginLink.setStyle("textDecoration", "none"); } private function onLoginRollOut():void { loginLink.setStyle("textDecoration", "underline"); } private function onAuthorizationComplete(e:AuthorizationEvent):void { settings.user = e.user; settings.flickrAuthorizationToken = e.authToken; //user = e.user; //authorizationToken = e.authToken; updateLoginLink(); saveSettings(); } private function onNameMaskChange():void { updateNameMask(); } private function updateNameMask():void { maskOutputField.text = nameMaskField.text + "00.xxx"; } private function updateDirPath(f:File):void { dir.text = f.nativePath; } private function onIdChange():void { var input:String = StringUtil.trim(userIDField.text); var hasText:Boolean = (input.length > 0); startButton.enabled = hasText; } private function onStartPress():void { if(idRadio.selected) { startDownload(userIDField.text); return; } else if(settings.user != null && userIDField.text == settings.user.username) { startDownload(settings.user.nsid); return; } else { getUserID(userIDField.text); } } private function getUserID(username:String):void { flickr = new FlickrService(apiKey); people = new People(flickr); flickr.addEventListener(FlickrResultEvent.PEOPLE_FIND_BY_USERNAME, onFindByUsername); messageField.text = "Retrieving account ID..."; people.findByUsername(username); } private function onFindByUsername(e:FlickrResultEvent):void { flickr.removeEventListener(FlickrResultEvent.PEOPLE_FIND_BY_USERNAME, onFindByUsername); flickr = null; people = null; if(!e.success) { messageField.text = "Could not retrieve user id. Please make sure user name is correct."; return; } var user:User = User(e.data.user); //userIDField.text = user.nsid; messageField.text = ""; startDownload(user.nsid); } private function startDownload(userID:String):void { //validate flickr id / name here messageField.text = ""; this.currentState=PAGES_STATE; f = new FlickrPeoplePager(apiKey, settings.flickrAuthorizationToken, apiSecret); //broadcast when all image data has been downloaded f.addEventListener(FlickrPageEvent.ON_PAGE_COMPLETE, onComplete); //broadcast as a page is downloaded f.addEventListener(FlumpProgressEvent.ON_INDIVIDUAL_PAGE_PROGRESS, onIPageProgress); //broadcast when a page has been downloaded f.addEventListener(FlickrPageProgressEvent.ON_TOTAL_PAGE_PROGRESS, onTotalPageProgress); f.addEventListener(FlickrPageErrorEvent.ON_FLICKR_ERROR, onFlickrError); f.downloadPhotoList(userID); } private function isAuthorized():Boolean { if(settings.flickrAuthorizationToken == null) { return false; } if(usernameRadio.selected) { return (userIDField.text == settings.user.username); } if(idRadio.selected) { return (userIDField.text == settings.user.nsid); } return false; } private function onFlickrError(e:FlickrPageErrorEvent):void { messageField.text = e.message + " Make sure Flickr ID is correct and that the account is a Pro account."; trace("error"); cleanUpListeners(); currentState = MAIN_STATE; } private function onCurrentStateChange():void { if(currentState == "") { height = 289; width = 460; } } private function cleanUpListeners():void { trace("cleanUpPagingListeners"); return; if(f != null) { f.removeEventListener(FlickrPageEvent.ON_PAGE_COMPLETE, onComplete); f.removeEventListener(FlumpProgressEvent.ON_INDIVIDUAL_PAGE_PROGRESS, onIPageProgress); f.removeEventListener(FlickrPageProgressEvent.ON_TOTAL_PAGE_PROGRESS, onTotalPageProgress); f.removeEventListener(FlickrPageErrorEvent.ON_FLICKR_ERROR, onFlickrError); f = null; } if(downloaded != null) { downloader.removeEventListener(FlumpProgressEvent.ON_INDIVIDUAL_PHOTO_PROGRESS, onIPhotoProgress); downloader.removeEventListener(FlickrPageProgressEvent.ON_TOTAL_IMAGE_PROGRESS, onTotalImageProgress); downloader = null; } } private function onBrowseClick():void { if(browseDirRef == null) { browseDirRef = File.desktopDirectory; browseDirRef.addEventListener(Event.SELECT, onDirSelect); } browseDirRef.browseForDirectory("Select Directory"); } private function onDirSelect(e:Event):void { var t:File = File(e.target); saveDIR = t; updateDirPath(t); } private function onTotalImageProgress(e:FlickrPageProgressEvent):void { if(this.currentState == DOWNLOAD_STATE) { totalPhotoPbar.setProgress(e.current, e.total); setProgressCount(e.current, e.total); } } private function onAboutClick():void { navigateToURL(new URLRequest(ABOUT_URL)); } private function onDefaultClick():void { dirButton.enabled = !dirCB.selected; if(dirCB.selected) { saveDIR = defaultDIR; updateDirPath(saveDIR); } } private function onTotalPageProgress(e:FlickrPageProgressEvent):void { //trace(e.current + ":" + e.total); //if(e.current == e.total - 1) //{ //this.currentState = DOWNLOAD_STATE; //f.removeEventListener(FlumpProgressEvent.ON_INDIVIDUAL_PAGE_PROGRESS, onIPageProgress); //f.removeEventListener(FlickrPageProgressEvent.ON_TOTAL_PAGE_PROGRESS, onTotalPageProgress); //return; //} totalPagePbar.setProgress(e.current, e.total); setProgressCount(e.current, e.total); } private function setProgressCount(count:uint, total:uint):void { countLabel.text = count + " of " + total; } private function onIPageProgress(e:FlumpProgressEvent):void { if(this.currentState == PAGES_STATE) { pagePbar.setProgress(e.bytesLoaded, e.bytesTotal); } } private function onTotalImageComplete(e:FlickrPageProgressEvent):void { currentState = MAIN_STATE; messageField.text = "Downloads complete. " + e.downloadCount + " of " + e.total + " images downloaded from Flickr."; cleanUpListeners(); } private function onIPhotoProgress(e:FlumpProgressEvent):void { if(this.currentState == DOWNLOAD_STATE) { photoPbar.setProgress(e.bytesLoaded, e.bytesTotal); } } private function onComplete(e:FlickrPageEvent):void { this.currentState = DOWNLOAD_STATE; pagePbar.setProgress(0,100); totalPagePbar.setProgress(0,100); //var dir:File = File.desktopDirectory.resolve("flump"); downloader = new FlickrPhotoListDownloader(saveDIR, nameMaskField.text); //broadcast as individual images are downloaded downloader.addEventListener(FlumpProgressEvent.ON_INDIVIDUAL_PHOTO_PROGRESS, onIPhotoProgress); //broadcast when an image has completed download. downloader.addEventListener(FlickrPageProgressEvent.ON_TOTAL_IMAGE_PROGRESS, onTotalImageProgress); downloader.addEventListener(FlickrPageProgressEvent.ON_TOTAL_IMAGE_COMPLETE, onTotalImageComplete); downloader.downloadPhotos(e.photos, isAuthorized()); }