From d6104b6f716874db91ae556fb9e763b1a084c780 Mon Sep 17 00:00:00 2001
From: Mitchell Livingston <livings124@transmissionbt.com>
Date: Sat, 30 Dec 2006 19:24:09 +0000
Subject: [PATCH] move ratio from mac code to libT code

---
 libtransmission/transmission.c | 11 ++++++++++-
 libtransmission/transmission.h |  4 ++++
 macosx/InfoWindowController.m  |  2 +-
 macosx/StringAdditions.h       |  2 +-
 macosx/StringAdditions.m       | 13 +++++++------
 macosx/Torrent.m               | 10 ++++------
 6 files changed, 27 insertions(+), 15 deletions(-)

diff --git a/libtransmission/transmission.c b/libtransmission/transmission.c
index a80201ac3..ae9226d70 100644
--- a/libtransmission/transmission.c
+++ b/libtransmission/transmission.c
@@ -560,7 +560,16 @@ tr_stat_t * tr_torrentStat( tr_torrent_t * tor )
 
     s->downloaded = tor->downloadedCur + tor->downloadedPrev;
     s->uploaded   = tor->uploadedCur   + tor->uploadedPrev;
-
+    
+    if( s->downloaded == 0 )
+    {
+        s->ratio = s->uploaded == 0 ? TR_RATIO_NA : TR_RATIO_INF;
+    }
+    else
+    {
+        s->ratio = (float)s->uploaded / (float)s->downloaded;
+    }
+    
     tr_lockUnlock( &tor->lock );
 
     return s;
diff --git a/libtransmission/transmission.h b/libtransmission/transmission.h
index 8e27e4a92..4a2c32cb4 100644
--- a/libtransmission/transmission.h
+++ b/libtransmission/transmission.h
@@ -391,6 +391,10 @@ struct tr_stat_s
     uint64_t            downloaded;
     uint64_t            uploaded;
     float               swarmspeed;
+
+#define TR_RATIO_NA  -1
+#define TR_RATIO_INF -2
+    float               ratio;
 };
 
 struct tr_peer_stat_s
diff --git a/macosx/InfoWindowController.m b/macosx/InfoWindowController.m
index c4c43dc88..364f663f6 100644
--- a/macosx/InfoWindowController.m
+++ b/macosx/InfoWindowController.m
@@ -330,7 +330,7 @@
                                         stringByAppendingFormat: @" (%.2f%%)", 100.0 * [torrent progress]]];
         
         [fStateField setStringValue: [torrent stateString]];
-        [fRatioField setStringValue: [NSString stringForRatioWithDownload: downloadedTotal upload: uploadedTotal]];
+        [fRatioField setStringValue: [NSString stringForRatio: [torrent ratio]]];
         [fSwarmSpeedField setStringValue: [torrent isActive] ? [NSString stringForSpeed: [torrent swarmSpeed]] : @""];
         
         NSString * errorMessage = [torrent errorMessage];
diff --git a/macosx/StringAdditions.h b/macosx/StringAdditions.h
index 02b9208fc..747a564b1 100644
--- a/macosx/StringAdditions.h
+++ b/macosx/StringAdditions.h
@@ -34,7 +34,7 @@
 + (NSString *) stringForFileSize: (uint64_t) size;
 + (NSString *) stringForSpeed: (float) speed;
 + (NSString *) stringForSpeedAbbrev: (float) speed;
-+ (NSString *) stringForRatioWithDownload: (uint64_t) down upload: (uint64_t) up;
++ (NSString *) stringForRatio: (float) ratio;
 
 - (NSAttributedString *) attributedStringFittingInWidth: (float) width
                                 attributes: (NSDictionary *) attributes;
diff --git a/macosx/StringAdditions.m b/macosx/StringAdditions.m
index 8c55f61eb..7f027ade4 100644
--- a/macosx/StringAdditions.m
+++ b/macosx/StringAdditions.m
@@ -23,6 +23,7 @@
  *****************************************************************************/
 
 #import "StringAdditions.h"
+#import <transmission.h>
 
 @implementation NSString (StringAdditions)
 
@@ -93,13 +94,13 @@
         return [NSString stringWithFormat: @"%.2f G", speed / 1048576.0];
 }
 
-+ (NSString *) stringForRatioWithDownload: (uint64_t) down upload: (uint64_t) up
++ (NSString *) stringForRatio: (float) ratio
 {
-    if (down == 0)
-        return up == 0 ? NSLocalizedString(@"N/A", "No Ratio") : [NSString stringWithUTF8String: "\xE2\x88\x9E"];
-
-    float ratio = (float) up / (float) down;
-    if (ratio < 10.0)
+    if (ratio == TR_RATIO_NA)
+        return NSLocalizedString(@"N/A", "No Ratio");
+    else if (ratio == TR_RATIO_INF)
+        return [NSString stringWithUTF8String: "\xE2\x88\x9E"];
+    else if (ratio < 10.0)
         return [NSString stringWithFormat: @"%.2f", ratio];
     else if (ratio < 100.0)
         return [NSString stringWithFormat: @"%.1f", ratio];
diff --git a/macosx/Torrent.m b/macosx/Torrent.m
index 5e2b354b1..179298277 100644
--- a/macosx/Torrent.m
+++ b/macosx/Torrent.m
@@ -284,7 +284,7 @@ static uint32_t kRed   = BE(0xFF6450FF), //255, 100, 80
     else
         [fProgressString appendFormat: NSLocalizedString(@"%@, uploaded %@ (Ratio: %@)", "Torrent -> progress string"),
                 [NSString stringForFileSize: [self size]], [NSString stringForFileSize: [self uploadedTotal]],
-                [NSString stringForRatioWithDownload: [self downloadedTotal] upload: [self uploadedTotal]]];
+                [NSString stringForRatio: [self ratio]]];
 
     switch (fStat->status)
     {
@@ -398,8 +398,7 @@ static uint32_t kRed   = BE(0xFF6450FF), //255, 100, 80
         }
         else
         {
-            NSString * ratioString = [NSString stringForRatioWithDownload: [self downloadedTotal]
-                                                upload: [self uploadedTotal]];
+            NSString * ratioString = [NSString stringForRatio: [self ratio]];
         
             [fShortStatusString setString: [NSString stringWithFormat: NSLocalizedString(@"Ratio: %@, ",
                                             "Torrent -> status string"), ratioString]];
@@ -504,8 +503,7 @@ static uint32_t kRed   = BE(0xFF6450FF), //255, 100, 80
 
 - (float) ratio
 {
-    float downloaded = [self downloadedTotal];
-    return downloaded > 0 ? (float)[self uploadedTotal] / downloaded : -1;
+    return fStat->ratio;
 }
 
 - (BOOL) customRatioSetting
@@ -1129,7 +1127,7 @@ static uint32_t kRed   = BE(0xFF6450FF), //255, 100, 80
 {
     //if finished downloading sort by ratio instead of progress
     float progress = [self progress];
-    return [NSNumber numberWithFloat: progress < 1.0 ? progress : 2.0 + [self ratio]];
+    return [NSNumber numberWithFloat: progress < 1.0 ? progress : 100.0 + [self ratio]];
 }
 
 @end