source: trunk/macosx/DragOverlayWindow.m @ 10025

Last change on this file since 10025 was 10025, checked in by livings124, 12 years ago

add a missing removeObserver: to the message window controller

  • Property Id set to /Users/mitchell/Desktop/Transmission/macosx/ActionMenuRatioToDisplayRatioTransformer.h
  • Property svn:keywords set to Date Rev Author Id
File size: 6.9 KB
Line 
1/******************************************************************************
2 * $Id: DragOverlayWindow.m 10025 2010-01-28 03:16:55Z livings124 $
3 *
4 * Copyright (c) 2007-2010 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: NSZeroRect 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        [self setFrame: [window frame] display: NO];
70       
71        [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(resizeWindow)
72            name: NSWindowDidResizeNotification object: window];
73    }
74    return self;
75}
76
77- (void) dealloc
78{
79    [[NSNotificationCenter defaultCenter] removeObserver: self];
80   
81    [fFadeInAnimation release];
82    [fFadeOutAnimation release];
83   
84    [super dealloc];
85}
86
87- (void) setTorrents: (NSArray *) files
88{
89    uint64_t size = 0;
90    NSInteger count = 0;
91   
92    NSString * name;
93    BOOL folder;
94    NSInteger fileCount = 0;
95   
96    for (NSString * file in files)
97    {
98        if ([[[NSWorkspace sharedWorkspace] typeOfFile: file error: NULL] isEqualToString: @"org.bittorrent.torrent"]
99            || [[file pathExtension] caseInsensitiveCompare: @"torrent"] == NSOrderedSame)
100        {
101            tr_ctor * ctor = tr_ctorNew(fLib);
102            tr_ctorSetMetainfoFromFile(ctor, [file UTF8String]);
103            tr_info info;
104            if (tr_torrentParse(ctor, &info) == TR_PARSE_OK)
105            {
106                count++;
107                size += info.totalSize;
108                fileCount += info.fileCount;
109               
110                //only useful when one torrent
111                if (count == 1)
112                {
113                    name = [NSString stringWithUTF8String: info.name];
114                    folder = info.isMultifile;
115                }
116            }
117            tr_metainfoFree(&info);
118            tr_ctorFree(ctor);
119        }
120    }
121   
122    if (count <= 0)
123        return;
124   
125    //set strings and icon
126    NSString * secondString = [NSString stringForFileSize: size];
127    if (count > 1 || folder)
128    {
129        NSString * fileString;
130        if (fileCount == 1)
131            fileString = NSLocalizedString(@"1 file", "Drag overlay -> torrents");
132        else
133            fileString= [NSString stringWithFormat: NSLocalizedString(@"%d files", "Drag overlay -> torrents"), fileCount];
134        secondString = [NSString stringWithFormat: @"%@, %@", fileString, secondString];
135    }
136   
137    NSImage * icon;
138    if (count == 1)
139        icon = [[NSWorkspace sharedWorkspace] iconForFileType: folder ? NSFileTypeForHFSTypeCode('fldr') : [name pathExtension]];
140    else
141    {
142        name = [NSString stringWithFormat: NSLocalizedString(@"%d Torrent Files", "Drag overlay -> torrents"), count];
143        secondString = [secondString stringByAppendingString: @" total"];
144        icon = [NSImage imageNamed: @"TransmissionDocument.icns"];
145    }
146   
147    [[self contentView] setOverlay: icon mainLine: name subLine: secondString];
148    [self fadeIn];
149}
150
151- (void) setFile: (NSString *) file
152{
153    [[self contentView] setOverlay: [NSImage imageNamed: @"CreateLarge.png"]
154        mainLine: NSLocalizedString(@"Create a Torrent File", "Drag overlay -> file") subLine: file];
155    [self fadeIn];
156}
157
158- (void) setURL: (NSString *) url
159{
160    [[self contentView] setOverlay: [NSImage imageNamed: @"Globe.png"]
161        mainLine: NSLocalizedString(@"Web Address", "Drag overlay -> url") subLine: url];
162    [self fadeIn];
163}
164
165- (void) fadeIn
166{
167    //stop other animation and set to same progress
168    if ([fFadeOutAnimation isAnimating])
169    {
170        [fFadeOutAnimation stopAnimation];
171        [fFadeInAnimation setCurrentProgress: 1.0 - [fFadeOutAnimation currentProgress]];
172    }
173    [self setFrame: [[self parentWindow] frame] display: YES];
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
Note: See TracBrowser for help on using the repository browser.