2007-09-16 01:02:06 +00:00
|
|
|
/******************************************************************************
|
2012-01-14 17:12:04 +00:00
|
|
|
* Copyright (c) 2007-2012 Transmission authors and contributors
|
2007-09-16 01:02:06 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
#import "DragOverlayWindow.h"
|
|
|
|
#import "DragOverlayView.h"
|
|
|
|
#import "NSStringAdditions.h"
|
|
|
|
|
2021-12-26 03:12:32 +00:00
|
|
|
#include <libtransmission/transmission.h>
|
|
|
|
#include <libtransmission/torrent-metainfo.h>
|
|
|
|
|
2010-01-28 03:16:55 +00:00
|
|
|
@interface DragOverlayWindow (Private)
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)resizeWindow;
|
2010-01-28 03:16:55 +00:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
2007-09-16 01:02:06 +00:00
|
|
|
@implementation DragOverlayWindow
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (instancetype)initWithLib:(tr_session*)lib forWindow:(NSWindow*)window
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2021-10-31 15:18:27 +00:00
|
|
|
if ((self = ([super initWithContentRect:window.frame
|
|
|
|
styleMask:NSWindowStyleMaskBorderless
|
|
|
|
backing:NSBackingStoreBuffered
|
|
|
|
defer:NO])))
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
|
|
|
fLib = lib;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
self.backgroundColor = [NSColor colorWithCalibratedWhite:0.0 alpha:0.5];
|
2021-08-07 07:27:56 +00:00
|
|
|
self.alphaValue = 0.0;
|
|
|
|
self.opaque = NO;
|
|
|
|
self.hasShadow = NO;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
DragOverlayView* view = [[DragOverlayView alloc] initWithFrame:self.frame];
|
2021-08-07 07:27:56 +00:00
|
|
|
self.contentView = view;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-07 07:27:56 +00:00
|
|
|
self.releasedWhenClosed = NO;
|
|
|
|
self.ignoresMouseEvents = YES;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
fFadeInAnimation = [[NSViewAnimation alloc] initWithViewAnimations:@[
|
|
|
|
@{ NSViewAnimationTargetKey : self, NSViewAnimationEffectKey : NSViewAnimationFadeInEffect }
|
|
|
|
]];
|
2021-08-07 07:27:56 +00:00
|
|
|
fFadeInAnimation.duration = 0.15;
|
|
|
|
fFadeInAnimation.animationBlockingMode = NSAnimationNonblockingThreaded;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
fFadeOutAnimation = [[NSViewAnimation alloc] initWithViewAnimations:@[
|
|
|
|
@{ NSViewAnimationTargetKey : self, NSViewAnimationEffectKey : NSViewAnimationFadeOutEffect }
|
|
|
|
]];
|
2021-08-07 07:27:56 +00:00
|
|
|
fFadeOutAnimation.duration = 0.5;
|
|
|
|
fFadeOutAnimation.animationBlockingMode = NSAnimationNonblockingThreaded;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
[window addChildWindow:self ordered:NSWindowAbove];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(resizeWindow) name:NSWindowDidResizeNotification
|
|
|
|
object:window];
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)dealloc
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
[NSNotificationCenter.defaultCenter removeObserver:self];
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)setTorrents:(NSArray*)files
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
|
|
|
uint64_t size = 0;
|
2008-11-01 22:08:02 +00:00
|
|
|
NSInteger count = 0;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
NSString* name;
|
2008-11-01 22:08:02 +00:00
|
|
|
NSInteger fileCount = 0;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
for (NSString* file in files)
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
if ([[NSWorkspace.sharedWorkspace typeOfFile:file error:NULL] isEqualToString:@"org.bittorrent.torrent"] ||
|
|
|
|
[file.pathExtension caseInsensitiveCompare:@"torrent"] == NSOrderedSame)
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2021-12-26 03:12:32 +00:00
|
|
|
auto metainfo = tr_torrent_metainfo{};
|
|
|
|
if (metainfo.parseTorrentFile(file.UTF8String))
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2021-12-26 03:12:32 +00:00
|
|
|
++count;
|
|
|
|
|
|
|
|
size += metainfo.totalSize();
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-12-26 03:12:32 +00:00
|
|
|
auto const n_files = std::size(metainfo.files());
|
|
|
|
fileCount += n_files;
|
|
|
|
if (n_files == 1)
|
2007-12-22 03:31:22 +00:00
|
|
|
{
|
2021-12-26 03:12:32 +00:00
|
|
|
name = @(metainfo.name().c_str());
|
2007-12-22 03:31:22 +00:00
|
|
|
}
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2007-09-16 01:02:06 +00:00
|
|
|
if (count <= 0)
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2007-09-16 01:02:06 +00:00
|
|
|
return;
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2007-09-16 01:02:06 +00:00
|
|
|
//set strings and icon
|
2021-08-15 09:41:48 +00:00
|
|
|
NSString* secondString = [NSString stringForFileSize:size];
|
2021-12-26 03:12:32 +00:00
|
|
|
if (count > 1)
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSString* fileString;
|
2007-09-16 01:02:06 +00:00
|
|
|
if (fileCount == 1)
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2008-04-07 13:25:55 +00:00
|
|
|
fileString = NSLocalizedString(@"1 file", "Drag overlay -> torrents");
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2007-09-16 01:02:06 +00:00
|
|
|
else
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
|
|
|
fileString = [NSString stringWithFormat:NSLocalizedString(@"%@ files", "Drag overlay -> torrents"),
|
|
|
|
[NSString formattedUInteger:fileCount]];
|
|
|
|
}
|
|
|
|
secondString = [NSString stringWithFormat:@"%@, %@", fileString, secondString];
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
NSImage* icon;
|
2007-09-16 01:02:06 +00:00
|
|
|
if (count == 1)
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2021-12-26 03:12:32 +00:00
|
|
|
icon = [NSWorkspace.sharedWorkspace iconForFileType:name ? name.pathExtension : NSFileTypeForHFSTypeCode(kGenericFolderIcon)];
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2007-09-16 01:02:06 +00:00
|
|
|
else
|
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
name = [NSString stringWithFormat:NSLocalizedString(@"%@ Torrent Files", "Drag overlay -> torrents"),
|
|
|
|
[NSString formattedUInteger:count]];
|
|
|
|
secondString = [secondString stringByAppendingString:@" total"];
|
|
|
|
icon = [NSImage imageNamed:@"TransmissionDocument.icns"];
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
[self.contentView setOverlay:icon mainLine:name subLine:secondString];
|
2007-09-16 01:02:06 +00:00
|
|
|
[self fadeIn];
|
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)setFile:(NSString*)file
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
[self.contentView setOverlay:[NSImage imageNamed:@"CreateLarge"]
|
|
|
|
mainLine:NSLocalizedString(@"Create a Torrent File", "Drag overlay -> file")
|
|
|
|
subLine:file];
|
2007-09-16 01:02:06 +00:00
|
|
|
[self fadeIn];
|
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)setURL:(NSString*)url
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
[self.contentView setOverlay:[NSImage imageNamed:@"Globe"] mainLine:NSLocalizedString(@"Web Address", "Drag overlay -> url")
|
|
|
|
subLine:url];
|
2007-09-16 01:02:06 +00:00
|
|
|
[self fadeIn];
|
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)fadeIn
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
|
|
|
//stop other animation and set to same progress
|
2021-08-07 07:27:56 +00:00
|
|
|
if (fFadeOutAnimation.animating)
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
|
|
|
[fFadeOutAnimation stopAnimation];
|
2021-08-07 07:27:56 +00:00
|
|
|
fFadeInAnimation.currentProgress = 1.0 - fFadeOutAnimation.currentProgress;
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
|
|
|
[fFadeInAnimation startAnimation];
|
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)fadeOut
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
|
|
|
//stop other animation and set to same progress
|
2021-08-07 07:27:56 +00:00
|
|
|
if (fFadeInAnimation.animating)
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
|
|
|
[fFadeInAnimation stopAnimation];
|
2021-08-07 07:27:56 +00:00
|
|
|
fFadeOutAnimation.currentProgress = 1.0 - fFadeInAnimation.currentProgress;
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
2021-08-07 07:27:56 +00:00
|
|
|
if (self.alphaValue > 0.0)
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2008-03-24 02:57:59 +00:00
|
|
|
[fFadeOutAnimation startAnimation];
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
2010-01-28 03:16:55 +00:00
|
|
|
|
|
|
|
@implementation DragOverlayWindow (Private)
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)resizeWindow
|
2010-01-28 03:16:55 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
[self setFrame:self.parentWindow.frame display:NO];
|
2010-01-28 03:16:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|