From 075454604ba84c915e74b5d52f1be39cfe78a188 Mon Sep 17 00:00:00 2001
From: Mitchell Livingston <livings124@transmissionbt.com>
Date: Fri, 5 Aug 2011 23:18:02 +0000
Subject: [PATCH] generate the dock menu exclusively in code

---
 macosx/Controller.h          |   1 -
 macosx/Controller.m          |  68 +++++-------------
 macosx/en.lproj/MainMenu.xib | 136 +----------------------------------
 3 files changed, 22 insertions(+), 183 deletions(-)

diff --git a/macosx/Controller.h b/macosx/Controller.h
index 9fa95a5c7..5934261a9 100644
--- a/macosx/Controller.h
+++ b/macosx/Controller.h
@@ -98,7 +98,6 @@ typedef enum
     BOOL                            fPauseOnLaunch;
     
     Badger                          * fBadger;
-    IBOutlet NSMenu                 * fDockMenu;
     
     NSMutableArray                  * fAutoImportedNames;
     NSTimer                         * fAutoImportTimer;
diff --git a/macosx/Controller.m b/macosx/Controller.m
index fb5a91aba..ed5e9e81e 100644
--- a/macosx/Controller.m
+++ b/macosx/Controller.m
@@ -125,9 +125,6 @@ typedef enum
 
 #define UPDATE_UI_SECONDS   1.0
 
-#define DOCK_SEEDING_TAG        101
-#define DOCK_DOWNLOADING_TAG    102
-
 #define TRANSFER_PLIST  @"/Library/Application Support/Transmission/Transfers.plist"
 
 #define WEBSITE_URL @"http://www.transmissionbt.com/"
@@ -3880,68 +3877,41 @@ static void sleepCallback(void * controller, io_service_t y, natural_t messageTy
 - (NSMenu *) applicationDockMenu: (NSApplication *) sender
 {
     NSInteger seeding = 0, downloading = 0;
-    for (Torrent * torrent in fTorrents)
+    if (!fQuitting)
     {
-        if ([torrent isSeeding])
-            seeding++;
-        else if ([torrent isActive])
-            downloading++;
-        else;
+        for (Torrent * torrent in fTorrents)
+        {
+            if ([torrent isSeeding])
+                seeding++;
+            else if ([torrent isActive])
+                downloading++;
+            else;
+        }
     }
     
-    NSMenuItem * seedingItem = [fDockMenu itemWithTag: DOCK_SEEDING_TAG],
-            * downloadingItem = [fDockMenu itemWithTag: DOCK_DOWNLOADING_TAG];
-    
-    BOOL hasSeparator = seedingItem || downloadingItem;
+    NSMenu * menu = [[NSMenu alloc] init];
     
     if (seeding > 0)
     {
         NSString * title = [NSString stringWithFormat: NSLocalizedString(@"%d Seeding", "Dock item - Seeding"), seeding];
-        if (!seedingItem)
-        {
-            seedingItem = [[[NSMenuItem alloc] initWithTitle: title action: nil keyEquivalent: @""] autorelease];
-            [seedingItem setTag: DOCK_SEEDING_TAG];
-            [fDockMenu insertItem: seedingItem atIndex: 0];
-        }
-        else
-            [seedingItem setTitle: title];
-    }
-    else
-    {
-        if (seedingItem)
-            [fDockMenu removeItem: seedingItem];
+        [menu addItemWithTitle: title action: nil keyEquivalent: @""];
     }
     
     if (downloading > 0)
     {
         NSString * title = [NSString stringWithFormat: NSLocalizedString(@"%d Downloading", "Dock item - Downloading"), downloading];
-        if (!downloadingItem)
-        {
-            downloadingItem = [[[NSMenuItem alloc] initWithTitle: title action: nil keyEquivalent: @""] autorelease];
-            [downloadingItem setTag: DOCK_DOWNLOADING_TAG];
-            [fDockMenu insertItem: downloadingItem atIndex: seeding > 0 ? 1 : 0];
-        }
-        else
-            [downloadingItem setTitle: title];
-    }
-    else
-    {
-        if (downloadingItem)
-            [fDockMenu removeItem: downloadingItem];
+        [menu addItemWithTitle: title action: nil keyEquivalent: @""];
     }
     
     if (seeding > 0 || downloading > 0)
-    {
-        if (!hasSeparator)
-            [fDockMenu insertItem: [NSMenuItem separatorItem] atIndex: (seeding > 0 && downloading > 0) ? 2 : 1];
-    }
-    else
-    {
-        if (hasSeparator)
-            [fDockMenu removeItemAtIndex: 0];
-    }
+        [menu addItem: [NSMenuItem separatorItem]];
+    
+    [menu addItemWithTitle: NSLocalizedString(@"Pause All", "Dock item") action: @selector(stopAllTorrents:) keyEquivalent: @""];
+    [menu addItemWithTitle: NSLocalizedString(@"Resume All", "Dock item") action: @selector(resumeAllTorrents:) keyEquivalent: @""];
+    [menu addItem: [NSMenuItem separatorItem]];
+    [menu addItemWithTitle: NSLocalizedString(@"Speed Limit", "Dock item") action: @selector(toggleSpeedLimit:) keyEquivalent: @""];
     
-    return fDockMenu;
+    return [menu autorelease];
 }
 
 - (NSRect) windowWillUseStandardFrame: (NSWindow *) window defaultFrame: (NSRect) defaultFrame
diff --git a/macosx/en.lproj/MainMenu.xib b/macosx/en.lproj/MainMenu.xib
index aa9314ce9..f8780532f 100644
--- a/macosx/en.lproj/MainMenu.xib
+++ b/macosx/en.lproj/MainMenu.xib
@@ -54,7 +54,7 @@
 				<nil key="NSUserInterfaceItemIdentifier"/>
 				<string key="NSWindowContentMinSize">{350, 5}</string>
 				<object class="NSView" key="NSWindowView" id="303120456">
-					<reference key="NSNextResponder"/>
+					<nil key="NSNextResponder"/>
 					<int key="NSvFlags">256</int>
 					<array class="NSMutableArray" key="NSSubviews">
 						<object class="NSButton" id="673219624">
@@ -62,7 +62,6 @@
 							<int key="NSvFlags">292</int>
 							<string key="NSFrame">{{36, 3}, {36, 18}}</string>
 							<reference key="NSSuperview" ref="303120456"/>
-							<reference key="NSWindow"/>
 							<reference key="NSNextKeyView" ref="52857407"/>
 							<bool key="NSEnabled">YES</bool>
 							<object class="NSButtonCell" key="NSCell" id="109224088">
@@ -92,7 +91,6 @@
 							<int key="NSvFlags">292</int>
 							<string key="NSFrame">{{72, 3}, {36, 18}}</string>
 							<reference key="NSSuperview" ref="303120456"/>
-							<reference key="NSWindow"/>
 							<reference key="NSNextKeyView" ref="403739499"/>
 							<bool key="NSEnabled">YES</bool>
 							<object class="NSButtonCell" key="NSCell" id="617399467">
@@ -118,7 +116,6 @@
 							<int key="NSvFlags">292</int>
 							<string key="NSFrame">{{0, 3}, {36, 18}}</string>
 							<reference key="NSSuperview" ref="303120456"/>
-							<reference key="NSWindow"/>
 							<reference key="NSNextKeyView" ref="673219624"/>
 							<bool key="NSEnabled">YES</bool>
 							<object class="NSButtonCell" key="NSCell" id="751933283">
@@ -144,8 +141,6 @@
 							<int key="NSvFlags">258</int>
 							<string key="NSFrame">{{118, 5}, {279, 14}}</string>
 							<reference key="NSSuperview" ref="303120456"/>
-							<reference key="NSWindow"/>
-							<reference key="NSNextKeyView"/>
 							<bool key="NSEnabled">YES</bool>
 							<object class="NSTextFieldCell" key="NSCell" id="696487414">
 								<int key="NSCellFlags">67239488</int>
@@ -190,8 +185,7 @@
 											<int key="NSvFlags">256</int>
 											<string key="NSFrameSize">{515, 224}</string>
 											<reference key="NSSuperview" ref="306237956"/>
-											<reference key="NSWindow"/>
-											<reference key="NSNextKeyView" ref="1068338331"/>
+											<reference key="NSNextKeyView" ref="365285506"/>
 											<bool key="NSEnabled">YES</bool>
 											<object class="_NSCornerView" key="NSCornerView">
 												<nil key="NSNextResponder"/>
@@ -631,7 +625,6 @@ AAAAAAAAAAA</bytes>
 									</array>
 									<string key="NSFrameSize">{515, 224}</string>
 									<reference key="NSSuperview" ref="87647552"/>
-									<reference key="NSWindow"/>
 									<reference key="NSNextKeyView" ref="675318801"/>
 									<reference key="NSDocView" ref="675318801"/>
 									<reference key="NSBGColor" ref="139722115"/>
@@ -642,7 +635,6 @@ AAAAAAAAAAA</bytes>
 									<int key="NSvFlags">-2147483392</int>
 									<string key="NSFrame">{{453, 0}, {15, 173}}</string>
 									<reference key="NSSuperview" ref="87647552"/>
-									<reference key="NSWindow"/>
 									<reference key="NSNextKeyView" ref="306218447"/>
 									<reference key="NSTarget" ref="87647552"/>
 									<string key="NSAction">_doScroller:</string>
@@ -653,7 +645,6 @@ AAAAAAAAAAA</bytes>
 									<int key="NSvFlags">-2147483392</int>
 									<string key="NSFrame">{{-100, -100}, {451, 15}}</string>
 									<reference key="NSSuperview" ref="87647552"/>
-									<reference key="NSWindow"/>
 									<reference key="NSNextKeyView" ref="306237956"/>
 									<int key="NSsFlags">1</int>
 									<reference key="NSTarget" ref="87647552"/>
@@ -663,8 +654,7 @@ AAAAAAAAAAA</bytes>
 							</array>
 							<string key="NSFrame">{{0, 24}, {515, 224}}</string>
 							<reference key="NSSuperview" ref="303120456"/>
-							<reference key="NSWindow"/>
-							<reference key="NSNextKeyView" ref="365285506"/>
+							<reference key="NSNextKeyView" ref="306237956"/>
 							<int key="NSsFlags">133648</int>
 							<reference key="NSVScroller" ref="1068338331"/>
 							<reference key="NSHScroller" ref="365285506"/>
@@ -673,8 +663,6 @@ AAAAAAAAAAA</bytes>
 						</object>
 					</array>
 					<string key="NSFrameSize">{515, 248}</string>
-					<reference key="NSSuperview"/>
-					<reference key="NSWindow"/>
 					<reference key="NSNextKeyView" ref="87647552"/>
 				</object>
 				<string key="NSScreenRect">{{0, 0}, {2560, 1440}}</string>
@@ -2040,50 +2028,6 @@ AAAAAAAAAAA</bytes>
 					</object>
 				</array>
 			</object>
-			<object class="NSMenu" id="500860248">
-				<string key="NSTitle">Menu</string>
-				<array class="NSMutableArray" key="NSMenuItems">
-					<object class="NSMenuItem" id="546389610">
-						<reference key="NSMenu" ref="500860248"/>
-						<string key="NSTitle">Pause All</string>
-						<string key="NSKeyEquiv"/>
-						<int key="NSKeyEquivModMask">1048576</int>
-						<int key="NSMnemonicLoc">2147483647</int>
-						<reference key="NSOnImage" ref="246127200"/>
-						<reference key="NSMixedImage" ref="330170270"/>
-					</object>
-					<object class="NSMenuItem" id="441858331">
-						<reference key="NSMenu" ref="500860248"/>
-						<string key="NSTitle">Resume All</string>
-						<string key="NSKeyEquiv"/>
-						<int key="NSKeyEquivModMask">1048576</int>
-						<int key="NSMnemonicLoc">2147483647</int>
-						<reference key="NSOnImage" ref="246127200"/>
-						<reference key="NSMixedImage" ref="330170270"/>
-					</object>
-					<object class="NSMenuItem" id="250856123">
-						<reference key="NSMenu" ref="500860248"/>
-						<bool key="NSIsDisabled">YES</bool>
-						<bool key="NSIsSeparator">YES</bool>
-						<string key="NSTitle"/>
-						<string key="NSKeyEquiv"/>
-						<int key="NSKeyEquivModMask">1048576</int>
-						<int key="NSMnemonicLoc">2147483647</int>
-						<reference key="NSOnImage" ref="246127200"/>
-						<reference key="NSMixedImage" ref="330170270"/>
-					</object>
-					<object class="NSMenuItem" id="1036623186">
-						<reference key="NSMenu" ref="500860248"/>
-						<string key="NSTitle">Speed Limit</string>
-						<string key="NSKeyEquiv"/>
-						<int key="NSKeyEquivModMask">1048576</int>
-						<int key="NSMnemonicLoc">2147483647</int>
-						<reference key="NSOnImage" ref="246127200"/>
-						<reference key="NSMixedImage" ref="330170270"/>
-					</object>
-				</array>
-				<string key="NSName"/>
-			</object>
 			<object class="NSCustomObject" id="456998591">
 				<string key="NSClassName">SUUpdater</string>
 			</object>
@@ -2749,22 +2693,6 @@ AAAAAAAAAAA</bytes>
 					</object>
 					<int key="connectionID">477</int>
 				</object>
-				<object class="IBConnectionRecord">
-					<object class="IBActionConnection" key="connection">
-						<string key="label">resumeAllTorrents:</string>
-						<reference key="source" ref="30116300"/>
-						<reference key="destination" ref="441858331"/>
-					</object>
-					<int key="connectionID">585</int>
-				</object>
-				<object class="IBConnectionRecord">
-					<object class="IBActionConnection" key="connection">
-						<string key="label">stopAllTorrents:</string>
-						<reference key="source" ref="30116300"/>
-						<reference key="destination" ref="546389610"/>
-					</object>
-					<int key="connectionID">586</int>
-				</object>
 				<object class="IBConnectionRecord">
 					<object class="IBActionConnection" key="connection">
 						<string key="label">showInfo:</string>
@@ -3261,14 +3189,6 @@ AAAAAAAAAAA</bytes>
 					</object>
 					<int key="connectionID">1862</int>
 				</object>
-				<object class="IBConnectionRecord">
-					<object class="IBOutletConnection" key="connection">
-						<string key="label">fDockMenu</string>
-						<reference key="source" ref="30116300"/>
-						<reference key="destination" ref="500860248"/>
-					</object>
-					<int key="connectionID">1876</int>
-				</object>
 				<object class="IBConnectionRecord">
 					<object class="IBActionConnection" key="connection">
 						<string key="label">setSortReverse:</string>
@@ -3389,14 +3309,6 @@ AAAAAAAAAAA</bytes>
 					</object>
 					<int key="connectionID">2144</int>
 				</object>
-				<object class="IBConnectionRecord">
-					<object class="IBActionConnection" key="connection">
-						<string key="label">toggleSpeedLimit:</string>
-						<reference key="source" ref="30116300"/>
-						<reference key="destination" ref="1036623186"/>
-					</object>
-					<int key="connectionID">2165</int>
-				</object>
 				<object class="IBConnectionRecord">
 					<object class="IBActionConnection" key="connection">
 						<string key="label">togglePiecesBar:</string>
@@ -4822,38 +4734,6 @@ AAAAAAAAAAA</bytes>
 						<array class="NSMutableArray" key="children"/>
 						<reference key="parent" ref="612287796"/>
 					</object>
-					<object class="IBObjectRecord">
-						<int key="objectID">581</int>
-						<reference key="object" ref="500860248"/>
-						<array class="NSMutableArray" key="children">
-							<reference ref="441858331"/>
-							<reference ref="546389610"/>
-							<reference ref="1036623186"/>
-							<reference ref="250856123"/>
-						</array>
-						<reference key="parent" ref="0"/>
-						<string key="objectName">DockMenu</string>
-					</object>
-					<object class="IBObjectRecord">
-						<int key="objectID">582</int>
-						<reference key="object" ref="441858331"/>
-						<reference key="parent" ref="500860248"/>
-					</object>
-					<object class="IBObjectRecord">
-						<int key="objectID">583</int>
-						<reference key="object" ref="546389610"/>
-						<reference key="parent" ref="500860248"/>
-					</object>
-					<object class="IBObjectRecord">
-						<int key="objectID">1574</int>
-						<reference key="object" ref="1036623186"/>
-						<reference key="parent" ref="500860248"/>
-					</object>
-					<object class="IBObjectRecord">
-						<int key="objectID">1575</int>
-						<reference key="object" ref="250856123"/>
-						<reference key="parent" ref="500860248"/>
-					</object>
 					<object class="IBObjectRecord">
 						<int key="objectID">589</int>
 						<reference key="object" ref="439147874"/>
@@ -5618,8 +5498,6 @@ AAAAAAAAAAA</bytes>
 				<string key="1566.IBPluginDependency">com.apple.InterfaceBuilder.CocoaPlugin</string>
 				<string key="1567.IBPluginDependency">com.apple.InterfaceBuilder.CocoaPlugin</string>
 				<string key="157.IBPluginDependency">com.apple.InterfaceBuilder.CocoaPlugin</string>
-				<string key="1574.IBPluginDependency">com.apple.InterfaceBuilder.CocoaPlugin</string>
-				<string key="1575.IBPluginDependency">com.apple.InterfaceBuilder.CocoaPlugin</string>
 				<string key="1578.IBPluginDependency">com.apple.InterfaceBuilder.CocoaPlugin</string>
 				<string key="1591.IBPluginDependency">com.apple.InterfaceBuilder.CocoaPlugin</string>
 				<string key="160.IBPluginDependency">com.apple.InterfaceBuilder.CocoaPlugin</string>
@@ -5789,9 +5667,6 @@ AAAAAAAAAAA</bytes>
 				<string key="56.IBPluginDependency">com.apple.InterfaceBuilder.CocoaPlugin</string>
 				<string key="57.IBPluginDependency">com.apple.InterfaceBuilder.CocoaPlugin</string>
 				<string key="58.IBPluginDependency">com.apple.InterfaceBuilder.CocoaPlugin</string>
-				<string key="581.IBPluginDependency">com.apple.InterfaceBuilder.CocoaPlugin</string>
-				<string key="582.IBPluginDependency">com.apple.InterfaceBuilder.CocoaPlugin</string>
-				<string key="583.IBPluginDependency">com.apple.InterfaceBuilder.CocoaPlugin</string>
 				<string key="589.IBPluginDependency">com.apple.InterfaceBuilder.CocoaPlugin</string>
 				<string key="598.IBPluginDependency">com.apple.InterfaceBuilder.CocoaPlugin</string>
 				<string key="611.IBPluginDependency">com.apple.InterfaceBuilder.CocoaPlugin</string>
@@ -5830,7 +5705,6 @@ AAAAAAAAAAA</bytes>
 						<string key="fActionMenu">NSMenu</string>
 						<string key="fCheckRatioItem">NSMenuItem</string>
 						<string key="fClearCompletedButton">NSButton</string>
-						<string key="fDockMenu">NSMenu</string>
 						<string key="fDownloadLimitItem">NSMenuItem</string>
 						<string key="fDownloadMenu">NSMenu</string>
 						<string key="fDownloadNoLimitItem">NSMenuItem</string>
@@ -5867,10 +5741,6 @@ AAAAAAAAAAA</bytes>
 							<string key="name">fClearCompletedButton</string>
 							<string key="candidateClassName">NSButton</string>
 						</object>
-						<object class="IBToOneOutletInfo" key="fDockMenu">
-							<string key="name">fDockMenu</string>
-							<string key="candidateClassName">NSMenu</string>
-						</object>
 						<object class="IBToOneOutletInfo" key="fDownloadLimitItem">
 							<string key="name">fDownloadLimitItem</string>
 							<string key="candidateClassName">NSMenuItem</string>
-- 
2.40.0