source: trunk/macosx/DragOverlayWindow.m @ 2244

Last change on this file since 2244 was 2244, checked in by livings124, 15 years ago

awesome Globe icon (Glow|be) from Jonas Rask

File size: 6.6 KB
Line 
1/******************************************************************************
2 * $Id$
3 *
4 * Copyright (c) 2007 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 "StringAdditions.h"
28
29@implementation DragOverlayWindow
30
31- (id) initWithLib: (tr_handle_t *) lib forWindow: (NSWindow *) window
32{
33    if (self = ([super initWithContentRect: NSMakeRect(0, 0, 1.0, 1.0) styleMask: NSBorderlessWindowMask
34                    backing: NSBackingStoreBuffered defer: NO]))
35    {
36        fLib = lib;
37       
38        [self setBackgroundColor: [NSColor colorWithCalibratedWhite: 0.0 alpha: 0.5]];
39        [self setAlphaValue: 0.0];
40        [self setOpaque: NO];
41        [self setHasShadow: NO];
42       
43        DragOverlayView * view = [[DragOverlayView alloc] initWithFrame: [self frame]];
44        [self setContentView: view];
45        [view release];
46       
47        [self setReleasedWhenClosed: NO];
48        [self setIgnoresMouseEvents: YES];
49       
50        fFadeInAnimation = [[NSViewAnimation alloc] initWithViewAnimations: [NSArray arrayWithObject:
51                                [NSDictionary dictionaryWithObjectsAndKeys: self, NSViewAnimationTargetKey,
52                                NSViewAnimationFadeInEffect, NSViewAnimationEffectKey, nil]]];
53        [fFadeInAnimation setDuration: 0.15];
54       
55        fFadeOutAnimation = [[NSViewAnimation alloc] initWithViewAnimations: [NSArray arrayWithObject:
56                                [NSDictionary dictionaryWithObjectsAndKeys: self, NSViewAnimationTargetKey,
57                                NSViewAnimationFadeOutEffect, NSViewAnimationEffectKey, nil]]];
58        [fFadeOutAnimation setDuration: 0.5];
59        [fFadeOutAnimation setAnimationBlockingMode: NSAnimationNonblockingThreaded];
60       
61        [window addChildWindow: self ordered: NSWindowAbove];
62    }
63    return self;
64}
65
66- (void) dealloc
67{
68    [fFadeInAnimation release];
69    [fFadeOutAnimation release];
70   
71    [super dealloc];
72}
73
74- (void) setTorrents: (NSArray *) files
75{
76    uint64_t size = 0;
77    int count = 0;
78   
79    NSString * name;
80    BOOL folder;
81    int fileCount = 0;
82   
83    NSString * file;
84    NSEnumerator * enumerator = [files objectEnumerator];
85    tr_info_t info;
86    while ((file = [enumerator nextObject]))
87    {
88        if (tr_torrentParse(fLib, [file UTF8String], NULL, &info) == TR_OK)
89        {
90            count++;
91            size += info.totalSize;
92            fileCount += info.fileCount;
93           
94            //only useful when one torrent
95            if (count == 1)
96            {
97                name = [NSString stringWithUTF8String: info.name];
98                folder = info.multifile;
99            }
100        }
101        tr_metainfoFree(&info);
102    }
103   
104    if (count <= 0)
105        return;
106   
107    //set strings and icon
108    NSImage * icon = nil;
109    NSString * secondString = [NSString stringForFileSize: size];
110    if (count > 1 || folder)
111    {
112        NSString * fileString;
113        if (fileCount == 1)
114            fileString = NSLocalizedString(@"1 File, ", "Drag overlay -> torrents");
115        else
116            fileString= [NSString stringWithFormat: NSLocalizedString(@"%d Files, ", "Drag overlay -> torrents"), fileCount];
117         secondString = [fileString stringByAppendingString: secondString];
118    }
119   
120    if (count == 1)
121        icon = [[NSWorkspace sharedWorkspace] iconForFileType: folder ? NSFileTypeForHFSTypeCode('fldr') : [name pathExtension]];
122    else
123    {
124        name = [NSString stringWithFormat: NSLocalizedString(@"%d Torrent Files", "Drag overlay -> torrents"), count];
125        secondString = [secondString stringByAppendingString: @" Total"];
126    }
127   
128    [[self contentView] setOverlay: icon mainLine: name subLine: secondString];
129   
130    //stop other animation and set to same progress
131    if ([fFadeOutAnimation isAnimating])
132    {
133        [fFadeOutAnimation stopAnimation];
134        [fFadeInAnimation setCurrentProgress: 1.0 - [fFadeOutAnimation currentProgress]];
135    }
136    [self setFrame: [[self parentWindow] frame] display: YES];
137    [fFadeInAnimation startAnimation];
138}
139
140- (void) setFile: (NSString *) file
141{
142       
143    [[self contentView] setOverlay: [NSImage imageNamed: @"CreateLarge.png"]
144        mainLine: NSLocalizedString(@"Create a Torrent File", "Drag overlay -> file") subLine: file];
145   
146    //stop other animation and set to same progress
147    if ([fFadeOutAnimation isAnimating])
148    {
149        [fFadeOutAnimation stopAnimation];
150        [fFadeInAnimation setCurrentProgress: 1.0 - [fFadeOutAnimation currentProgress]];
151    }
152    [self setFrame: [[self parentWindow] frame] display: YES];
153    [fFadeInAnimation startAnimation];
154}
155
156
157- (void) setURL: (NSString *) url
158{
159    [[self contentView] setOverlay: [NSImage imageNamed: @"Globe.png"]
160        mainLine: NSLocalizedString(@"Web Address", "Drag overlay -> url") subLine: url];
161   
162    //stop other animation and set to same progress
163    if ([fFadeOutAnimation isAnimating])
164    {
165        [fFadeOutAnimation stopAnimation];
166        [fFadeInAnimation setCurrentProgress: 1.0 - [fFadeOutAnimation currentProgress]];
167    }
168    [self setFrame: [[self parentWindow] frame] display: YES];
169    [fFadeInAnimation startAnimation];
170}
171
172- (void) fadeOut
173{
174    if ([self alphaValue] > 0.0)
175    {
176        //stop other animation and set to same progress
177        if ([fFadeInAnimation isAnimating])
178        {
179            [fFadeInAnimation stopAnimation];
180            [fFadeOutAnimation setCurrentProgress: 1.0 - [fFadeInAnimation currentProgress]];
181        }
182        [fFadeOutAnimation startAnimation];
183    }
184}
185
186@end
Note: See TracBrowser for help on using the repository browser.