source: trunk/macosx/DragOverlayWindow.m @ 7468

Last change on this file since 7468 was 7468, checked in by charles, 12 years ago

(trunk) patch from wereHamster to finish off tr_handle

  • Property Id set to /Users/mitchell/Desktop/Transmission/macosx/ActionMenuRatioToDisplayRatioTransformer.h
  • Property svn:keywords set to Date Rev Author Id
File size: 6.4 KB
Line 
1/******************************************************************************
2 * $Id: DragOverlayWindow.m 7468 2008-12-22 19:14:43Z charles $
3 *
4 * Copyright (c) 2007-2008 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@implementation DragOverlayWindow
30
31- (id) initWithLib: (tr_session *) 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.0f alpha: 0.5f]];
39        [self setAlphaValue: 0.0f];
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        [fFadeInAnimation setAnimationBlockingMode: NSAnimationNonblockingThreaded];
55       
56        fFadeOutAnimation = [[NSViewAnimation alloc] initWithViewAnimations: [NSArray arrayWithObject:
57                                [NSDictionary dictionaryWithObjectsAndKeys: self, NSViewAnimationTargetKey,
58                                NSViewAnimationFadeOutEffect, NSViewAnimationEffectKey, nil]]];
59        [fFadeOutAnimation setDuration: 0.5];
60        [fFadeOutAnimation setAnimationBlockingMode: NSAnimationNonblockingThreaded];
61       
62        [window addChildWindow: self ordered: NSWindowAbove];
63    }
64    return self;
65}
66
67- (void) dealloc
68{
69    [fFadeInAnimation release];
70    [fFadeOutAnimation release];
71   
72    [super dealloc];
73}
74
75- (void) setTorrents: (NSArray *) files
76{
77    uint64_t size = 0;
78    NSInteger count = 0;
79   
80    NSString * name;
81    BOOL folder;
82    NSInteger fileCount = 0;
83   
84    NSString * file;
85    NSEnumerator * enumerator = [files objectEnumerator];
86    while ((file = [enumerator nextObject]))
87    {
88        if ([[file pathExtension] caseInsensitiveCompare: @"torrent"] == NSOrderedSame)
89        {
90            tr_ctor * ctor = tr_ctorNew(fLib);
91            tr_ctorSetMetainfoFromFile(ctor, [file UTF8String]);
92            tr_info info;
93            if (tr_torrentParse(fLib, ctor, &info) == TR_OK)
94            {
95                count++;
96                size += info.totalSize;
97                fileCount += info.fileCount;
98               
99                //only useful when one torrent
100                if (count == 1)
101                {
102                    name = [NSString stringWithUTF8String: info.name];
103                    folder = info.isMultifile;
104                }
105            }
106            tr_metainfoFree(&info);
107            tr_ctorFree(ctor);
108        }
109    }
110   
111    if (count <= 0)
112        return;
113   
114    //set strings and icon
115    NSString * secondString = [NSString stringForFileSize: size];
116    if (count > 1 || folder)
117    {
118        NSString * fileString;
119        if (fileCount == 1)
120            fileString = NSLocalizedString(@"1 file", "Drag overlay -> torrents");
121        else
122            fileString= [NSString stringWithFormat: NSLocalizedString(@"%d files", "Drag overlay -> torrents"), fileCount];
123        secondString = [NSString stringWithFormat: @"%@, %@", fileString, secondString];
124    }
125   
126    NSImage * icon;
127    if (count == 1)
128        icon = [[NSWorkspace sharedWorkspace] iconForFileType: folder ? NSFileTypeForHFSTypeCode('fldr') : [name pathExtension]];
129    else
130    {
131        name = [NSString stringWithFormat: NSLocalizedString(@"%d Torrent Files", "Drag overlay -> torrents"), count];
132        secondString = [secondString stringByAppendingString: @" total"];
133        icon = [NSImage imageNamed: @"TransmissionDocument.icns"];
134    }
135   
136    [[self contentView] setOverlay: icon mainLine: name subLine: secondString];
137    [self fadeIn];
138}
139
140- (void) setFile: (NSString *) file
141{
142    [[self contentView] setOverlay: [NSImage imageNamed: @"CreateLarge.png"]
143        mainLine: NSLocalizedString(@"Create a Torrent File", "Drag overlay -> file") subLine: file];
144    [self fadeIn];
145}
146
147- (void) setURL: (NSString *) url
148{
149    [[self contentView] setOverlay: [NSImage imageNamed: @"Globe.png"]
150        mainLine: NSLocalizedString(@"Web Address", "Drag overlay -> url") subLine: url];
151    [self fadeIn];
152}
153
154- (void) fadeIn
155{
156    //stop other animation and set to same progress
157    if ([fFadeOutAnimation isAnimating])
158    {
159        [fFadeOutAnimation stopAnimation];
160        [fFadeInAnimation setCurrentProgress: 1.0 - [fFadeOutAnimation currentProgress]];
161    }
162    [self setFrame: [[self parentWindow] frame] display: YES];
163    [fFadeInAnimation startAnimation];
164}
165
166- (void) fadeOut
167{
168    //stop other animation and set to same progress
169    if ([fFadeInAnimation isAnimating])
170    {
171        [fFadeInAnimation stopAnimation];
172        [fFadeOutAnimation setCurrentProgress: 1.0 - [fFadeInAnimation currentProgress]];
173    }
174    if ([self alphaValue] > 0.0f)
175        [fFadeOutAnimation startAnimation];
176}
177
178@end
Note: See TracBrowser for help on using the repository browser.