1/****************************************************************************** 2 * $Id: DragOverlayWindow.m 13340 2012-06-10 02:35:58Z livings124 $ 3 * 4 * Copyright (c) 2007-2012 Transmission authors and contributors 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the "Software"), 8 * to deal in the Software without restriction, including without limitation 9 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 * and/or sell copies of the Software, and to permit persons to whom the 11 * Software is furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be included in 14 * all copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 * DEALINGS IN THE SOFTWARE. 23 *****************************************************************************/ 24 25#import "DragOverlayWindow.h" 26#import "DragOverlayView.h" 27#import "NSStringAdditions.h" 28 29@interface DragOverlayWindow (Private) 30 31- (void) resizeWindow; 32 33@end 34 35@implementation DragOverlayWindow 36 37- (id) initWithLib: (tr_session *) lib forWindow: (NSWindow *) window 38{ 39 if ((self = ([super initWithContentRect: [window frame] styleMask: NSBorderlessWindowMask 40 backing: NSBackingStoreBuffered defer: NO]))) 41 { 42 fLib = lib; 43 44 [self setBackgroundColor: [NSColor colorWithCalibratedWhite: 0.0 alpha: 0.5]]; 45 [self setAlphaValue: 0.0]; 46 [self setOpaque: NO]; 47 [self setHasShadow: NO]; 48 49 DragOverlayView * view = [[DragOverlayView alloc] initWithFrame: [self frame]]; 50 [self setContentView: view]; 51 [view release]; 52 53 [self setReleasedWhenClosed: NO]; 54 [self setIgnoresMouseEvents: YES]; 55 56 fFadeInAnimation = [[NSViewAnimation alloc] initWithViewAnimations: [NSArray arrayWithObject: 57 [NSDictionary dictionaryWithObjectsAndKeys: self, NSViewAnimationTargetKey, 58 NSViewAnimationFadeInEffect, NSViewAnimationEffectKey, nil]]]; 59 [fFadeInAnimation setDuration: 0.15]; 60 [fFadeInAnimation setAnimationBlockingMode: NSAnimationNonblockingThreaded]; 61 62 fFadeOutAnimation = [[NSViewAnimation alloc] initWithViewAnimations: [NSArray arrayWithObject: 63 [NSDictionary dictionaryWithObjectsAndKeys: self, NSViewAnimationTargetKey, 64 NSViewAnimationFadeOutEffect, NSViewAnimationEffectKey, nil]]]; 65 [fFadeOutAnimation setDuration: 0.5]; 66 [fFadeOutAnimation setAnimationBlockingMode: NSAnimationNonblockingThreaded]; 67 68 [window addChildWindow: self ordered: NSWindowAbove]; 69 70 [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(resizeWindow) 71 name: NSWindowDidResizeNotification object: window]; 72 } 73 return self; 74} 75 76- (void) dealloc 77{ 78 [[NSNotificationCenter defaultCenter] removeObserver: self]; 79 80 [fFadeInAnimation release]; 81 [fFadeOutAnimation release]; 82 83 [super dealloc]; 84} 85 86- (void) setTorrents: (NSArray *) files 87{ 88 uint64_t size = 0; 89 NSInteger count = 0; 90 91 NSString * name; 92 BOOL folder; 93 NSInteger fileCount = 0; 94 95 for (NSString * file in files) 96 { 97 if ([[[NSWorkspace sharedWorkspace] typeOfFile: file error: NULL] isEqualToString: @"org.bittorrent.torrent"] 98 || [[file pathExtension] caseInsensitiveCompare: @"torrent"] == NSOrderedSame) 99 { 100 tr_ctor * ctor = tr_ctorNew(fLib); 101 tr_ctorSetMetainfoFromFile(ctor, [file UTF8String]); 102 tr_info info; 103 if (tr_torrentParse(ctor, &info) == TR_PARSE_OK) 104 { 105 count++; 106 size += info.totalSize; 107 fileCount += info.fileCount; 108 109 //only useful when one torrent 110 if (count == 1) 111 { 112 name = [NSString stringWithUTF8String: info.name]; 113 folder = info.isMultifile; 114 } 115 } 116 tr_metainfoFree(&info); 117 tr_ctorFree(ctor); 118 } 119 } 120 121 if (count <= 0) 122 return; 123 124 //set strings and icon 125 NSString * secondString = [NSString stringForFileSize: size]; 126 if (count > 1 || folder) 127 { 128 NSString * fileString; 129 if (fileCount == 1) 130 fileString = NSLocalizedString(@"1 file", "Drag overlay -> torrents"); 131 else 132 fileString= [NSString stringWithFormat: NSLocalizedString(@"%@ files", "Drag overlay -> torrents"), 133 [NSString formattedUInteger: fileCount]]; 134 secondString = [NSString stringWithFormat: @"%@, %@", fileString, secondString]; 135 } 136 137 NSImage * icon; 138 if (count == 1) 139 icon = [[NSWorkspace sharedWorkspace] iconForFileType: folder ? NSFileTypeForHFSTypeCode(kGenericFolderIcon) : [name pathExtension]]; 140 else 141 { 142 name = [NSString stringWithFormat: NSLocalizedString(@"%@ Torrent Files", "Drag overlay -> torrents"), 143 [NSString formattedUInteger: count]]; 144 secondString = [secondString stringByAppendingString: @" total"]; 145 icon = [NSImage imageNamed: @"TransmissionDocument.icns"]; 146 } 147 148 [[self contentView] setOverlay: icon mainLine: name subLine: secondString]; 149 [self fadeIn]; 150} 151 152- (void) setFile: (NSString *) file 153{ 154 [[self contentView] setOverlay: [NSImage imageNamed: @"CreateLarge"] 155 mainLine: NSLocalizedString(@"Create a Torrent File", "Drag overlay -> file") subLine: file]; 156 [self fadeIn]; 157} 158 159- (void) setURL: (NSString *) url 160{ 161 [[self contentView] setOverlay: [NSImage imageNamed: @"Globe"] 162 mainLine: NSLocalizedString(@"Web Address", "Drag overlay -> url") subLine: url]; 163 [self fadeIn]; 164} 165 166- (void) fadeIn 167{ 168 //stop other animation and set to same progress 169 if ([fFadeOutAnimation isAnimating]) 170 { 171 [fFadeOutAnimation stopAnimation]; 172 [fFadeInAnimation setCurrentProgress: 1.0 - [fFadeOutAnimation currentProgress]]; 173 } 174 [fFadeInAnimation startAnimation]; 175} 176 177- (void) fadeOut 178{ 179 //stop other animation and set to same progress 180 if ([fFadeInAnimation isAnimating]) 181 { 182 [fFadeInAnimation stopAnimation]; 183 [fFadeOutAnimation setCurrentProgress: 1.0 - [fFadeInAnimation currentProgress]]; 184 } 185 if ([self alphaValue] > 0.0) 186 [fFadeOutAnimation startAnimation]; 187} 188 189@end 190 191@implementation DragOverlayWindow (Private) 192 193- (void) resizeWindow 194{ 195 [self setFrame: [[self parentWindow] frame] display: NO]; 196} 197 198@end 199