[gui-dev] Suspend/Resume uploads button patch

Jens-Uwe Mager <[email protected]> Wed, 25 Aug 2004 17:55:46 +0200
Newsgroups gmane.network.gnutella.limewire.gui.devel
Message-ID <[email protected]>
Here is a patch to implement a Suspend/Resume button for uploads. The
button icon choosen is a bit bad, but I had no nice pause button handy.
-- 
Jens-Uwe Mager	<pgp-mailto:F476EBC2>

_______________________________________________
gui-dev mailing list
[email protected]
http://www.limewire.org/mailman/listinfo/gui-dev
suspendresume.patch (text/plain, 10.1 KB)
Index: gui/com/limegroup/gnutella/gui/upload/SuspendResumeListener.java
===================================================================
--- /dev/null	Wed Aug 25 12:58:17 2004
+++ gui/com/limegroup/gnutella/gui/upload/SuspendResumeListener.java	Sun Jan 11 05:06:14 2004
@@ -0,0 +1,27 @@
+package com.limegroup.gnutella.gui.upload;
+
+import com.limegroup.gnutella.gui.*;
+import javax.swing.*;
+import java.awt.event.*;
+
+
+/**
+ * This class is an <tt>ActionListener</tt> that suspends or resumes all
+ * uploads in progress.
+ */
+final class SuspendResumeListener implements ActionListener {
+
+    private UploadMediator um;
+    
+    SuspendResumeListener(UploadMediator um) {
+        this.um = um;
+    }    
+	
+	public void actionPerformed(ActionEvent ae) {
+		try {
+                        um.toggleUploads();
+		} catch(Exception e) {
+			GUIMediator.showInternalError(e);
+		}
+	}
+}
Index: lib/messagebundles/MessagesBundle.properties
===================================================================
RCS file: /cvs/lib/messagebundles/MessagesBundle.properties,v
retrieving revision 1.122
diff -u -r1.122 MessagesBundle.properties
--- lib/messagebundles/MessagesBundle.properties	21 Aug 2004 20:05:02 -0000	1.122
+++ lib/messagebundles/MessagesBundle.properties	25 Aug 2004 13:02:32 -0000
@@ -543,6 +554,10 @@
 UPLOAD_CLEAR_BUTTON_TIP=Remove Inactive Uploads
 UPLOAD_CHAT_BUTTON_LABEL=Chat with Host
 UPLOAD_CHAT_BUTTON_TIP=Chat with another user of the selected host
+UPLOAD_SUSPEND_BUTTON_LABEL=Suspend Uploads
+UPLOAD_SUSPEND_BUTTON_TIP=Temporarily suspend all uploads
+UPLOAD_RESUME_BUTTON_LABEL=Resume Uploads
+UPLOAD_RESUME_BUTTON_TIP=Resume all uploads
 UPLOAD_TABLE_STRING_NAME=Name
 UPLOAD_TABLE_STRING_HOST=Host
 UPLOAD_TABLE_STRING_SIZE=Size
Index: lib/messagebundles/MessagesBundle_de.properties
===================================================================
RCS file: /cvs/lib/messagebundles/MessagesBundle_de.properties,v
retrieving revision 1.10
diff -u -r1.10 MessagesBundle_de.properties
--- lib/messagebundles/MessagesBundle_de.properties	23 Apr 2004 21:53:08 -0000	1.10
+++ lib/messagebundles/MessagesBundle_de.properties	25 Aug 2004 13:02:35 -0000
@@ -522,6 +542,10 @@
 UPLOAD_CLEAR_BUTTON_TIP=Löscht die Beendeten Uploads.
 UPLOAD_CHAT_BUTTON_LABEL=Mit Benutzer chatten
 UPLOAD_CHAT_BUTTON_TIP=Mit einem anderen Benutzer am Rechner chatten.
+UPLOAD_SUSPEND_BUTTON_LABEL=Uploads anhalten
+UPLOAD_SUSPEND_BUTTON_TIP=Alle Uploads zeitweilig anhalten.
+UPLOAD_RESUME_BUTTON_LABEL=Uploads fortsetzen
+UPLOAD_RESUME_BUTTON_TIP=Alle Uploads fortsetzen
 UPLOAD_TABLE_STRING_NAME=Name
 UPLOAD_TABLE_STRING_HOST=Rechner
 UPLOAD_TABLE_STRING_SIZE=Größe
Index: core/com/limegroup/gnutella/UploadManager.java
===================================================================
RCS file: /cvs/core/com/limegroup/gnutella/UploadManager.java,v
retrieving revision 1.165
diff -u -r1.165 UploadManager.java
--- core/com/limegroup/gnutella/UploadManager.java	23 Aug 2004 17:38:25 -0000	1.165
+++ core/com/limegroup/gnutella/UploadManager.java	25 Aug 2004 13:02:44 -0000
@@ -103,6 +103,8 @@
 	/** set to true when an upload has been succesfully completed. */
 	private volatile boolean _hadSuccesfulUpload=false;
 
+	/** set to true if uploads are currently suspended */
+	private volatile boolean _suspended = false;
     
 	/**
 	 * LOCKING: obtain this' monitor before modifying any 
@@ -1607,6 +1609,14 @@
         Assert.that(upman.measuredUploadSpeed()==80);
     }
 
+    public boolean isSuspended() {
+    	return _suspended;
+    }
+    
+    public void setSuspended(boolean val) {
+    	_suspended = val;
+    }
+	
 	/**
 	 * This class keeps track of client requests.
 	 * 
Index: core/com/limegroup/gnutella/uploader/NormalUploadState.java
===================================================================
RCS file: /cvs/core/com/limegroup/gnutella/uploader/NormalUploadState.java,v
retrieving revision 1.70
diff -u -r1.70 NormalUploadState.java
--- core/com/limegroup/gnutella/uploader/NormalUploadState.java	23 Aug 2004 17:38:30 -0000	1.70
+++ core/com/limegroup/gnutella/uploader/NormalUploadState.java	25 Aug 2004 13:02:45 -0000
@@ -207,6 +207,13 @@
         // have to reconstruct new byte arrays every BLOCK_SIZE.
         byte[] buf = new byte[BLOCK_SIZE];
         while (true) {
+            while (RouterService.getUploadManager().isSuspended()) {
+                try {
+                    Thread.sleep(1000);
+                } catch (InterruptedException ex) {
+                }
+            }
+
             BandwidthThrottle throttle =
                 _uploader.isUDPTransfer() ? UDP_THROTTLE : THROTTLE;
             throttle.setRate(getUploadSpeed());
Index: gui/com/limegroup/gnutella/gui/resources/icon_mapping.properties
===================================================================
RCS file: /cvs/gui/com/limegroup/gnutella/gui/resources/icon_mapping.properties,v
retrieving revision 1.4
diff -u -r1.4 icon_mapping.properties
--- gui/com/limegroup/gnutella/gui/resources/icon_mapping.properties	4 Aug 2004 18:30:47 -0000	1.4
+++ gui/com/limegroup/gnutella/gui/resources/icon_mapping.properties	25 Aug 2004 13:02:50 -0000
@@ -10,6 +10,7 @@
 DOWNLOAD_CLEAR=clear
 UPLOAD_REMOVE=kill
 #UPLOAD_CHAT=
+UPLOAD_SUSPEND=stop
 UPLOAD_BROWSE_HOST=browse
 UPLOAD_CLEAR=clear
 LIBRARY_LAUNCH=preview
Index: gui/com/limegroup/gnutella/gui/upload/UploadButtons.java
===================================================================
RCS file: /cvs/gui/com/limegroup/gnutella/gui/upload/UploadButtons.java,v
retrieving revision 1.5
diff -u -r1.5 UploadButtons.java
--- gui/com/limegroup/gnutella/gui/upload/UploadButtons.java	3 Aug 2004 22:15:16 -0000	1.5
+++ gui/com/limegroup/gnutella/gui/upload/UploadButtons.java	25 Aug 2004 13:02:52 -0000
@@ -32,6 +32,37 @@
 	 * The index of the clear button in the button row.
 	 */
 	static final int CLEAR_BUTTON  = 2;
+
+	/**
+	 * The index of the suspend/resume button in the button row.
+	 */
+	static final int SUSPEND_RESUME_BUTTON  = 3;
+
+        /**
+         * The string for the resume button label...
+         */
+        private static final String RESUME_BUTTON_LABEL =
+            GUIMediator.getStringResource("UPLOAD_RESUME_BUTTON_LABEL");
+
+        /**
+         * The string for the resume button tip...
+         */
+        private static final String RESUME_BUTTON_TIP =
+            GUIMediator.getStringResource("UPLOAD_RESUME_BUTTON_TIP");
+
+
+        /**
+         * The string for the suspend button label...
+         */
+        private static final String SUSPEND_BUTTON_LABEL =
+            GUIMediator.getStringResource("UPLOAD_SUSPEND_BUTTON_LABEL");
+
+        /**
+         * The string for the suspend button tip...
+         */
+        private static final String SUSPEND_BUTTON_TIP =
+            GUIMediator.getStringResource("UPLOAD_SUSPEND_BUTTON_TIP");
+
 	
 	/**
 	 * The constructor creates the row of buttons with their associated
@@ -41,24 +72,28 @@
         String[] buttonLabelKeys = {
 			"UPLOAD_KILL_BUTTON_LABEL",
 			"GENERAL_BROWSE_HOST_LABEL",
-			"UPLOAD_CLEAR_BUTTON_LABEL"
+			"UPLOAD_CLEAR_BUTTON_LABEL",
+                        "UPLOAD_SUSPEND_BUTTON_LABEL"
 		};
         String[] buttonTipKeys = {
 			"UPLOAD_KILL_BUTTON_TIP",
 			"GENERAL_BROWSE_HOST_TIP",
-			"UPLOAD_CLEAR_BUTTON_TIP"
+			"UPLOAD_CLEAR_BUTTON_TIP",
+                        "UPLOAD_SUSPEND_BUTTON_TIP"
 		};
 
 		ActionListener[] buttonListeners = {
 			um.REMOVE_LISTENER,
 			um.BROWSE_LISTENER,
-			um.CLEAR_LISTENER
+			um.CLEAR_LISTENER,
+                        um.SUSPEND_RESUME_LISTENER
 		};
 		
 		String[] iconNames =  {
 		    "UPLOAD_REMOVE",
 		    "UPLOAD_BROWSE_HOST",
-		    "UPLOAD_CLEAR"
+		    "UPLOAD_CLEAR",
+                    "UPLOAD_SUSPEND"
 		};				
 
 		BUTTONS = new ButtonRow(buttonLabelKeys,buttonTipKeys,buttonListeners,
@@ -66,5 +101,21 @@
 	}
 	
 	ButtonRow getComponent() { return BUTTONS; }
-	
+
+        /**
+         * Sets the mutable 'resume' button to the resume button.
+         */
+        void transformSuspendButton() {
+            BUTTONS.transformButton(SUSPEND_RESUME_BUTTON, RESUME_BUTTON_LABEL,
+                                    RESUME_BUTTON_TIP);
+        }
+
+        /**
+         * Sets the mutable 'resume' button to the suspend button.
+         */
+        void transformResumeButton() {
+            BUTTONS.transformButton(SUSPEND_RESUME_BUTTON, SUSPEND_BUTTON_LABEL,
+                                    SUSPEND_BUTTON_TIP);
+        }
+
 }
Index: gui/com/limegroup/gnutella/gui/upload/UploadMediator.java
===================================================================
RCS file: /cvs/gui/com/limegroup/gnutella/gui/upload/UploadMediator.java,v
retrieving revision 1.20
diff -u -r1.20 UploadMediator.java
--- gui/com/limegroup/gnutella/gui/upload/UploadMediator.java	23 Aug 2004 17:38:34 -0000	1.20
+++ gui/com/limegroup/gnutella/gui/upload/UploadMediator.java	25 Aug 2004 13:02:52 -0000
@@ -39,6 +39,9 @@
 	ActionListener CHAT_LISTENER;
 	ActionListener CLEAR_LISTENER;
 	ActionListener BROWSE_LISTENER;
+	ActionListener SUSPEND_RESUME_LISTENER;
+
+        private UploadButtons buttonRow;
 	
 	private static final String UPLOAD_TITLE =
 	    GUIMediator.getStringResource("UPLOAD_TITLE");
@@ -84,6 +87,7 @@
 	    CHAT_LISTENER = new ChatListener(this);
 	    CLEAR_LISTENER = new ClearListener(this);
 	    BROWSE_LISTENER = new BrowseListener(this);
+	    SUSPEND_RESUME_LISTENER = new SuspendResumeListener(this);
 	}
 
 	/**
@@ -93,7 +97,8 @@
 		MAIN_PANEL = new PaddedPanel(UPLOAD_TITLE);
 		DATA_MODEL = new UploadModel();
 		TABLE = new LimeJTable(DATA_MODEL);
-		BUTTON_ROW = (new UploadButtons(this)).getComponent();
+		buttonRow = new UploadButtons(this);
+		BUTTON_ROW = buttonRow.getComponent();
     }
 
 	/**
@@ -338,6 +343,16 @@
 		setButtonEnabled(UploadButtons.KILL_BUTTON, false);
 		setButtonEnabled(UploadButtons.BROWSE_BUTTON, false);
 	}
+
+        public void toggleUploads() {
+            UploadManager um = RouterService.getUploadManager();
+            um.setSuspended(!um.isSuspended());
+            if (um.isSuspended()) {
+                buttonRow.transformSuspendButton();
+            } else {
+                buttonRow.transformResumeButton();
+            }
+        }
 }