source: trunk/macosx/DragOverlayWindow.m @ 7514

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

eliminate some more enumerator objects

  • 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 7514 2008-12-26 07:25:17Z livings124 $
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    for (NSString * file in files)
85    {
86        if ([[file pathExtension] caseInsensitiveCompare: @"torrent"] == NSOrderedSame)
87        {
88            tr_ctor * ctor = tr_ctorNew(fLib);
89            tr_ctorSetMetainfoFromFile(ctor, [file UTF8String]);
90            tr_info info;
91            if (tr_torrentParse(fLib, ctor, &info) == TR_OK)
92            {
93                count++;
94                size += info.totalSize;
95                fileCount += info.fileCount;
96               
97                //only useful when one torrent
98                if (count == 1)
99                {
100                    name = [NSString stringWithUTF8String: info.name];
101                    folder = info.isMultifile;
102                }
103            }
104            tr_metainfoFree(&info);
105            tr_ctorFree(ctor);
106        }
107    }
108   
109    if (count <= 0)
110        return;
111   
112    //set strings and icon
113    NSString * secondString = [NSString stringForFileSize: size];
114    if (count > 1 || folder)
115    {
116        NSString * fileString;
117        if (fileCount == 1)
118            fileString = NSLocalizedString(@"1 file", "Drag overlay -> torrents");
119        else
120            fileString= [NSString stringWithFormat: NSLocalizedString(@"%d files", "Drag overlay -> torrents"), fileCount];
121        secondString = [NSString stringWithFormat: @"%@, %@", fileString, secondString];
122    }
123   
124    NSImage * icon;
125    if (count == 1)
126        icon = [[NSWorkspace sharedWorkspace] iconForFileType: folder ? NSFileTypeForHFSTypeCode('fldr') : [name pathExtension]];
127    else
128    {
129        name = [NSString stringWithFormat: NSLocalizedString(@"%d Torrent Files", "Drag overlay -> torrents"), count];
130        secondString = [secondString stringByAppendingString: @" total"];
131        icon = [NSImage imageNamed: @"TransmissionDocument.icns"];
132    }
133   
134    [[self contentView] setOverlay: icon mainLine: name subLine: secondString];
135    [self fadeIn];
136}
137
138- (void) setFile: (NSString *) file
139{
140    [[self contentView] setOverlay: [NSImage imageNamed: @"CreateLarge.png"]
141        mainLine: NSLocalizedString(@"Create a Torrent File", "Drag overlay -> file") subLine: file];
142    [self fadeIn];
143}
144
145- (void) setURL: (NSString *) url
146{
147    [[self contentView] setOverlay: [NSImage imageNamed: @"Globe.png"]
148        mainLine: NSLocalizedString(@"Web Address", "Drag overlay -> url") subLine: url];
149    [self fadeIn];
150}
151
152- (void) fadeIn
153{
154    //stop other animation and set to same progress
155    if ([fFadeOutAnimation isAnimating])
156    {
157        [fFadeOutAnimation stopAnimation];
158        [fFadeInAnimation setCurrentProgress: 1.0 - [fFadeOutAnimation currentProgress]];
159    }
160    [self setFrame: [[self parentWindow] frame] display: YES];
161    [fFadeInAnimation startAnimation];
162}
163
164- (void) fadeOut
165{
166    //stop other animation and set to same progress
167    if ([fFadeInAnimation isAnimating])
168    {
169        [fFadeInAnimation stopAnimation];
170        [fFadeOutAnimation setCurrentProgress: 1.0 - [fFadeInAnimation currentProgress]];
171    }
172    if ([self alphaValue] > 0.0f)
173        [fFadeOutAnimation startAnimation];
174}
175
176@end
Note: See TracBrowser for help on using the repository browser.