Index: mailhandler.admin.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/mailhandler/Attic/mailhandler.admin.inc,v
retrieving revision 1.1.2.20
diff -u -r1.1.2.20 mailhandler.admin.inc
--- mailhandler.admin.inc	28 Jan 2011 11:09:12 -0000	1.1.2.20
+++ mailhandler.admin.inc	28 Jan 2011 17:54:49 -0000
@@ -24,6 +24,12 @@
     return;
   }
 
+  // If there is a not finished form submission, get the current form values and
+  // populate the form fields.
+  if (isset($form_state['mailbox'])) {
+    $mailbox = $form_state['mailbox'] + (array)$mailbox;
+  }
+
   // Attach default fields for new created mailboxes.
   $mailbox = $mailbox ? $mailbox : mailhandler_mailbox_defaults();
 
@@ -189,6 +195,7 @@
   $form['actions']['test'] = array(
     '#type' => 'submit',
     '#value' => t('Test connection'),
+    '#submit' => array('mailhandler_mailbox_test_submit'),
   );
 
   // Save mailbox Id for edited mailboxes only. Also show Delete button.
@@ -244,6 +251,25 @@
 }
 
 /**
+ * Submit handler for Test mailbox button on mailhandler_add_edit_mailbox form.
+ *
+ * This submit handler will not save any data, but the submitted values must be
+ * available for the next form building operation.
+ */
+function mailhandler_mailbox_test_submit($form, &$form_state) {
+  $mailbox = $form_state['values'];
+  
+  mailhandler_mailbox_test($mailbox, $form, $form_state);
+  if (!$errors = form_get_errors()) {
+    drupal_set_message('Mailhandler was able to connect to the mailbox.');
+  }
+
+  // Do not save the data, but keep the form values updated.
+  $form_state['mailbox'] = $mailbox;
+  $form_state['rebuild'] = TRUE;
+}
+
+/**
  * Submit handler for Delete button on mailhandler_add_edit_mailbox form.
  */
 function mailhandler_delete_mailbox_submit($form, &$form_state) {
@@ -297,31 +323,6 @@
   mailhandler_mailbox_delete($mailbox['mid']);
 }
 
-
-/**
- * Test connection to a mailbox, and output results as a message
- */
-function mailhandler_test_mailbox(&$form_state) {
-  module_load_include('inc', 'mailhandler', 'mailhandler.retrieve');
-  $mailbox = $form_state['values'];
-  $result = mailhandler_open_mailbox($mailbox);
-
-  if ($result) {
-    drupal_set_message('Mailhandler was able to connect to the mailbox.');
-    imap_close($result);
-  }
-  else {
-    if ($mailbox['domain']) {
-      form_set_error('mailhandler', t('%c connection to %m failed.', array('%c' => ($mailbox['imap'] ? 'IMAP' : 'POP3'), '%m' => $mailbox['mail'])));
-    }
-    else {
-      form_set_error('mailhandler', t('Mailhandler could not access local folder: %m', array('%m' => $mailbox['mail'])));
-    }
-  }
-
-  return;
-}
-
 /**
  * Displays a list of mailboxes with operations.
  *
Index: mailhandler.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/mailhandler/Attic/mailhandler.module,v
retrieving revision 1.96.2.37
diff -u -r1.96.2.37 mailhandler.module
--- mailhandler.module	28 Jan 2011 11:09:12 -0000	1.96.2.37
+++ mailhandler.module	28 Jan 2011 18:00:22 -0000
@@ -326,15 +326,6 @@
   if ($mailbox['imap'] == 0 && $mailbox['delete_after_read'] == 0) {
     form_set_error('delete_after_read', t('You must check off "Delete messages after they are processed" when using a POP3 mailbox.'));
   }
-  
-  // Test POP/IMAP settings are valid in all cases, and test connection if requested
-  if ($mailbox_appears_ok && $form_state['clicked_button']['#value'] == t('Test connection')) {
-    // Call the test function
-    mailhandler_test_mailbox($form_state);
-
-    // We don't want to save, so we set an 'invisible error' that means the form is not submitted
-    form_set_error('mailhandler');
-  }
 }
 
 /**
@@ -417,6 +408,53 @@
  */
 
 /**
+ * Test the connection to a mailbox.
+ *
+ * Attempt a mailbox connection and set form errors. Even if this function is
+ * not used in a form (e.g. testing mailboxes created programatically), the
+ * function will still set form errors. These errors can be later recalled using
+ * the function form_get_errors().
+ *
+ * <code>
+ *   mailhandler_mailbox_test($mailbox);
+ *   if ($errors = form_get_errors()) {
+ *     do_my_error_handling($errors);
+ *   }
+ *   else {
+ *     mailhandler_mailbox_save($mailbox);
+ *   }
+ * </code>
+ *
+ * This function can be called safely from within a form.
+ *
+ * @param $mailbox
+ *   A mailbox settings array.
+ * @param $form
+ *   The $form where mailbox is being edited.
+ * @param $form_state
+ *   The $form_state values of the form being validated.
+ */
+function mailhandler_mailbox_test($mailbox, $form = array(), &$form_state = array()) {
+  // @todo this include must not be required by this function. Remove it.
+  module_load_include('inc', 'mailhandler', 'mailhandler.retrieve');
+
+  // @todo: mailhandler_open_mailbox and imap_close should have been moved to:
+  // - mailhandler_mailbox_open
+  // - mailhandler_mailbox_close
+  if ($result = mailhandler_open_mailbox($mailbox)){
+    imap_close($result);
+  }
+  else {
+    if ($mailbox['domain']) {
+      form_set_error('mailhandler', t('%c connection to %m failed.', array('%c' => ($mailbox['imap'] ? 'IMAP' : 'POP3'), '%m' => $mailbox['mail'])));
+    }
+    else {
+      form_set_error('mailhandler', t('Mailhandler could not access local folder: %m', array('%m' => $mailbox['mail'])));
+    }
+  }
+}
+
+/**
  * Return a default content type if the user has not chosen a specific type on the settings page
  * In order of priority, return blog, story, page
  * This assumes that one of these basic types is in use on a site (page and story are active by default)

