diff --git a/macosx/Controller.h b/macosx/Controller.h index 99b7c0079..dcd56a30c 100644 --- a/macosx/Controller.h +++ b/macosx/Controller.h @@ -23,9 +23,9 @@ #ifndef CONTROLLER_H #define CONTROLLER_H -#include -#include -#include "PrefsController.h" +#import +#import +#import "PrefsController.h" @class TorrentTableView; diff --git a/macosx/Controller.m b/macosx/Controller.m index 297fd3842..c4231ac00 100644 --- a/macosx/Controller.m +++ b/macosx/Controller.m @@ -20,12 +20,13 @@ * DEALINGS IN THE SOFTWARE. *****************************************************************************/ -#include +#import -#include "NameCell.h" -#include "ProgressCell.h" -#include "Utils.h" -#include "TorrentTableView.h" +#import "NameCell.h" +#import "ProgressCell.h" +#import "StringAdditions.h" +#import "Utils.h" +#import "TorrentTableView.h" #import "PrefsController.h" @@ -556,19 +557,17 @@ static void sleepCallBack( void * controller, io_service_t y, //Update the global DL/UL rates tr_torrentRates( fHandle, &dl, &ul ); - [fTotalDLField setStringValue: [NSString stringWithFormat: - @"Total DL: %.2f KB/s", dl]]; - [fTotalULField setStringValue: [NSString stringWithFormat: - @"Total UL: %.2f KB/s", ul]]; + [fTotalDLField setStringValue: [NSString stringForSpeed: dl]]; + [fTotalULField setStringValue: [NSString stringForSpeed: ul]]; //Update DL/UL totals in the Info panel row = [fTableView selectedRow]; if( row >= 0 ) { [fInfoDownloaded setStringValue: - stringForFileSize( fStat[row].downloaded )]; + [NSString stringForFileSize: fStat[row].downloaded]]; [fInfoUploaded setStringValue: - stringForFileSize( fStat[row].uploaded )]; + [NSString stringForFileSize: fStat[row].uploaded]]; } //check if torrents have recently ended. @@ -663,11 +662,11 @@ static void sleepCallBack( void * controller, io_service_t y, [fInfoAnnounce setStringValue: [NSString stringWithCString: fStat[row].info.trackerAnnounce]]; [fInfoSize setStringValue: - stringForFileSize( fStat[row].info.totalSize )]; + [NSString stringForFileSize: fStat[row].info.totalSize]]; [fInfoPieces setStringValue: [NSString stringWithFormat: @"%d", fStat[row].info.pieceCount]]; [fInfoPieceSize setStringValue: - stringForFileSize( fStat[row].info.pieceSize )]; + [NSString stringForFileSize: fStat[row].info.pieceSize]]; [fInfoFolder setStringValue: [[NSString stringWithUTF8String: tr_torrentGetFolder( fHandle, row )] lastPathComponent]]; diff --git a/macosx/NameCell.h b/macosx/NameCell.h index bbccbcf69..eaa20ffa2 100644 --- a/macosx/NameCell.h +++ b/macosx/NameCell.h @@ -23,9 +23,9 @@ #ifndef NAMECELL_H #define NAMECELL_H -#include -#include -#include "Controller.h" +#import +#import +#import "Controller.h" @interface NameCell : NSCell { diff --git a/macosx/NameCell.m b/macosx/NameCell.m index 8bcabb470..c569c7f5a 100644 --- a/macosx/NameCell.m +++ b/macosx/NameCell.m @@ -20,8 +20,9 @@ * DEALINGS IN THE SOFTWARE. *****************************************************************************/ -#include "NameCell.h" -#include "Utils.h" +#import "NameCell.h" +#import "StringAdditions.h" +#import "Utils.h" @implementation NameCell @@ -31,7 +32,7 @@ fNameString = [NSString stringWithUTF8String: stat->info.name]; fSizeString = [NSString stringWithFormat: @" (%@)", - stringForFileSize( stat->info.totalSize )]; + [NSString stringForFileSize: stat->info.totalSize]]; fTimeString = @""; fPeersString = @""; @@ -105,9 +106,9 @@ forKey: NSFontAttributeName]; pen.x += 5; pen.y += 5; - string = [NSString stringWithFormat: @"%@%@", - stringFittingInWidth( fNameString, cellFrame.size.width - - 35 - widthForString( fSizeString, 12 ), 12 ), fSizeString]; + string = [[fNameString stringFittingInWidth: cellFrame.size.width - + 35 - [fSizeString sizeWithAttributes: attributes].width + withAttributes: attributes] stringByAppendingString: fSizeString]; [string drawAtPoint: pen withAttributes: attributes]; [attributes setObject: [NSFont messageFontOfSize: 10.0] @@ -117,8 +118,8 @@ [fTimeString drawAtPoint: pen withAttributes: attributes]; pen.x += 0; pen.y += 15; - string = stringFittingInWidth( fPeersString, - cellFrame.size.width - 40, 10 ); + string = [fPeersString stringFittingInWidth: cellFrame.size.width - + 40 withAttributes: attributes]; [string drawAtPoint: pen withAttributes: attributes]; [view unlockFocus]; diff --git a/macosx/PrefsController.h b/macosx/PrefsController.h index f561d9a6f..3f895ec43 100644 --- a/macosx/PrefsController.h +++ b/macosx/PrefsController.h @@ -20,8 +20,8 @@ * DEALINGS IN THE SOFTWARE. *****************************************************************************/ -#include -#include +#import +#import @interface PrefsController : NSObject diff --git a/macosx/PrefsController.m b/macosx/PrefsController.m index 3df6f795b..2f0679ad6 100644 --- a/macosx/PrefsController.m +++ b/macosx/PrefsController.m @@ -20,7 +20,7 @@ * DEALINGS IN THE SOFTWARE. *****************************************************************************/ -#include "PrefsController.h" +#import "PrefsController.h" #define DEFAULT_UPLOAD @"20" #define MIN_PORT 1 diff --git a/macosx/ProgressCell.h b/macosx/ProgressCell.h index 77854ec57..e32391b20 100644 --- a/macosx/ProgressCell.h +++ b/macosx/ProgressCell.h @@ -23,8 +23,8 @@ #ifndef PROGRESSCELL_H #define PROGRESSCELL_H -#include -#include +#import +#import @interface ProgressCell : NSCell { diff --git a/macosx/ProgressCell.m b/macosx/ProgressCell.m index 489247173..30f903ad5 100644 --- a/macosx/ProgressCell.m +++ b/macosx/ProgressCell.m @@ -20,7 +20,8 @@ * DEALINGS IN THE SOFTWARE. *****************************************************************************/ -#include "ProgressCell.h" +#import "ProgressCell.h" +#import "StringAdditions.h" @implementation ProgressCell @@ -121,10 +122,10 @@ static uint32_t kGreen[] = fWhiteText = w; /* Update the strings to be displayed */ - fDlString = [NSString stringWithFormat: - @"DL: %.2f KB/s", fStat->rateDownload]; - fUlString = [NSString stringWithFormat: - @"UL: %.2f KB/s", fStat->rateUpload]; + fDlString = [@"DL: " stringByAppendingString: + [NSString stringForSpeed: fStat->rateDownload]]; + fUlString = [@"UL: " stringByAppendingString: + [NSString stringForSpeed: fStat->rateUpload]]; /* Reset our bitmap to the background image... */ in = [fBackgroundBmp bitmapData]; diff --git a/macosx/StringAdditions.h b/macosx/StringAdditions.h new file mode 100644 index 000000000..679f10bb1 --- /dev/null +++ b/macosx/StringAdditions.h @@ -0,0 +1,18 @@ +// +// StringAdditions.h +// Transmission +// +// Created by Mitchell Livingston on 1/16/06. +// Copyright 2006 __MyCompanyName__. All rights reserved. +// + +#import + +@interface NSString (StringAdditions) + ++ (NSString *) stringForFileSize: (uint64_t) size; ++ (NSString *) stringForSpeed: (float) speed; +- (NSString *) stringFittingInWidth: (float) width + withAttributes: (NSDictionary *) attributes; + +@end diff --git a/macosx/StringAdditions.m b/macosx/StringAdditions.m new file mode 100644 index 000000000..3a435d709 --- /dev/null +++ b/macosx/StringAdditions.m @@ -0,0 +1,51 @@ +// +// StringAdditions.m +// Transmission +// +// Created by Mitchell Livingston on 1/16/06. +// Copyright 2006 __MyCompanyName__. All rights reserved. +// + +#import "StringAdditions.h" +#import "Utils.h" + +@implementation NSString (StringAdditions) + ++ (NSString *) stringForFileSize: (uint64_t) size +{ + if (size < 1024) + return [NSString stringWithFormat: @"%lld bytes", size]; + else if (size < 1048576) + return [NSString stringWithFormat: @"%lld.%lld KB", + size / 1024, ( size % 1024 ) / 103]; + else if (size < 1073741824) + return [NSString stringWithFormat: @"%lld.%lld MB", + size / 1048576, ( size % 1048576 ) / 104858]; + else + return [NSString stringWithFormat: @"%lld.%lld GB", + size / 1073741824, ( size % 1073741824 ) / 107374183]; +} + ++ (NSString *) stringForSpeed: (float) speed +{ + if (speed < 1024) + return [NSString stringWithFormat: @"%.1f KB/s", speed]; + else if (speed < 1048576) + return [NSString stringWithFormat: @"%.2f MB/s", speed / 1024]; + else + return [NSString stringWithFormat: @"%.2f GB/s", speed / 1048576]; +} + +- (NSString *) stringFittingInWidth: (float) width + withAttributes: (NSDictionary *) attributes +{ + NSString * newString = self; + + unsigned i; + for (i = [self length]; [newString sizeWithAttributes: attributes].width > width; i--) + newString = [[self substringToIndex: i] stringByAppendingString: NS_ELLIPSIS]; + + return newString; +} + +@end diff --git a/macosx/TorrentTableView.h b/macosx/TorrentTableView.h index db9f83354..e7e1549be 100644 --- a/macosx/TorrentTableView.h +++ b/macosx/TorrentTableView.h @@ -1,5 +1,5 @@ -#include -#include +#import +#import @class Controller; diff --git a/macosx/TorrentTableView.m b/macosx/TorrentTableView.m index 859ccf35d..dd53a7c73 100644 --- a/macosx/TorrentTableView.m +++ b/macosx/TorrentTableView.m @@ -1,5 +1,5 @@ -#include "TorrentTableView.h" -#include "Controller.h" +#import "TorrentTableView.h" +#import "Controller.h" @implementation TorrentTableView diff --git a/macosx/Transmission.xcodeproj/project.pbxproj b/macosx/Transmission.xcodeproj/project.pbxproj index f2cdc29a4..a3c572800 100644 --- a/macosx/Transmission.xcodeproj/project.pbxproj +++ b/macosx/Transmission.xcodeproj/project.pbxproj @@ -23,6 +23,7 @@ 4DA6FDBB0911233800450CB1 /* PauseOff.png in Resources */ = {isa = PBXBuildFile; fileRef = 4DA6FDB90911233800450CB1 /* PauseOff.png */; }; 4DA6FDC5091141AD00450CB1 /* ResumeOff.png in Resources */ = {isa = PBXBuildFile; fileRef = 4DA6FDC3091141AD00450CB1 /* ResumeOff.png */; }; 4DA6FDC6091141AD00450CB1 /* ResumeOn.png in Resources */ = {isa = PBXBuildFile; fileRef = 4DA6FDC4091141AD00450CB1 /* ResumeOn.png */; }; + 4DE5CC9D0980656F00BE280E /* StringAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = 4DE5CC9C0980656F00BE280E /* StringAdditions.m */; }; 4DF0C5AB0899190500DD8943 /* Controller.m in Sources */ = {isa = PBXBuildFile; fileRef = 4DF0C5A90899190500DD8943 /* Controller.m */; }; 4DF0C5AE08991C1600DD8943 /* libtransmission.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 4DF0C5AD08991C1600DD8943 /* libtransmission.a */; }; 4DF7500C08A103AD007B0D70 /* Open.png in Resources */ = {isa = PBXBuildFile; fileRef = 4DF7500708A103AD007B0D70 /* Open.png */; }; @@ -90,6 +91,8 @@ 4DA6FDB90911233800450CB1 /* PauseOff.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = PauseOff.png; path = Images/PauseOff.png; sourceTree = ""; }; 4DA6FDC3091141AD00450CB1 /* ResumeOff.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = ResumeOff.png; path = Images/ResumeOff.png; sourceTree = ""; }; 4DA6FDC4091141AD00450CB1 /* ResumeOn.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = ResumeOn.png; path = Images/ResumeOn.png; sourceTree = ""; }; + 4DE5CC9B0980656F00BE280E /* StringAdditions.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = StringAdditions.h; sourceTree = ""; }; + 4DE5CC9C0980656F00BE280E /* StringAdditions.m */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.objc; path = StringAdditions.m; sourceTree = ""; }; 4DF0C5A90899190500DD8943 /* Controller.m */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.objc; path = Controller.m; sourceTree = ""; }; 4DF0C5AA0899190500DD8943 /* Controller.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = Controller.h; sourceTree = ""; }; 4DF0C5AD08991C1600DD8943 /* libtransmission.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libtransmission.a; path = ../libtransmission/libtransmission.a; sourceTree = SOURCE_ROOT; }; @@ -127,6 +130,8 @@ 4D118E1908CB46B20033958F /* PrefsController.m */, 4D364D9E091FBB2C00377D12 /* TorrentTableView.h */, 4D364D9F091FBB2C00377D12 /* TorrentTableView.m */, + 4DE5CC9B0980656F00BE280E /* StringAdditions.h */, + 4DE5CC9C0980656F00BE280E /* StringAdditions.m */, ); name = Classes; sourceTree = ""; @@ -303,6 +308,7 @@ 4D096C13089FB4E20091B166 /* ProgressCell.m in Sources */, 4D118E1A08CB46B20033958F /* PrefsController.m in Sources */, 4D364DA0091FBB2C00377D12 /* TorrentTableView.m in Sources */, + 4DE5CC9D0980656F00BE280E /* StringAdditions.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/macosx/Utils.h b/macosx/Utils.h index 3a4ddedd7..e6c0f71bb 100644 --- a/macosx/Utils.h +++ b/macosx/Utils.h @@ -20,54 +20,4 @@ * DEALINGS IN THE SOFTWARE. *****************************************************************************/ -static NSString * stringForFileSize( uint64_t size ) -{ - if( size < 1024 ) - { - return [NSString stringWithFormat: @"%lld bytes", size]; - } - if( size < 1048576 ) - { - return [NSString stringWithFormat: @"%lld.%lld KB", - size / 1024, ( size % 1024 ) / 103]; - } - if( size < 1073741824 ) - { - return [NSString stringWithFormat: @"%lld.%lld MB", - size / 1048576, ( size % 1048576 ) / 104858]; - } - return [NSString stringWithFormat: @"%lld.%lld GB", - size / 1073741824, ( size % 1073741824 ) / 107374183]; -} - -static float widthForString( NSString * string, float fontSize ) -{ - NSMutableDictionary * attributes = - [NSMutableDictionary dictionaryWithCapacity: 1]; - [attributes setObject: [NSFont messageFontOfSize: fontSize] - forKey: NSFontAttributeName]; - - return [string sizeWithAttributes: attributes].width; -} - #define NS_ELLIPSIS [NSString stringWithUTF8String: "\xE2\x80\xA6"] - -static NSString * stringFittingInWidth( NSString * oldString, float width, - float fontSize ) -{ - NSString * newString = NULL; - unsigned i; - - for( i = 0; i < [oldString length]; i++ ) - { - newString = [NSString stringWithFormat: @"%@%@", - [oldString substringToIndex: [oldString length] - i], - i ? NS_ELLIPSIS : @""]; - - if( widthForString( newString, fontSize ) <= width ) - { - break; - } - } - return newString; -} diff --git a/macosx/main.m b/macosx/main.m index 874d94b40..cef863257 100644 --- a/macosx/main.m +++ b/macosx/main.m @@ -20,7 +20,7 @@ * DEALINGS IN THE SOFTWARE. *****************************************************************************/ -#include +#import int main( int argc, char ** argv ) {