Request #7044 - Add 'unit' param to Quota config
authorMichael M Slusarz <slusarz@curecanti.org>
Wed, 17 Dec 2008 07:02:56 +0000 (00:02 -0700)
committerMichael M Slusarz <slusarz@curecanti.org>
Wed, 17 Dec 2008 07:02:56 +0000 (00:02 -0700)
imp/config/servers.php.dist
imp/docs/CHANGES
imp/lib/IMP.php
imp/lib/Quota.php

index 60608e1..da26af0 100644 (file)
  *
  *        Set this to an empty value to disable quota (the DEFAULT).
  *
+ *        The optional 'unit' parameter indicates what storage unit the quota
+ *        messages hould be displayed in. It can be one of three possible
+ *        values: 'GB', 'MB' (DEFAULT), or 'KB'.
+ *
  *        The optional 'format' parameter is supported by all drivers and
  *        specifies the formats of the quota messages in the user
  *        interface. The parameter must be specified as a hash with the four
  *        possible elements 'long', 'short', 'nolimit_long', and
  *        'nolimit_short' with according versions of the quota message. The
  *        strings will be passed through sprintf().
- *
  *        These are the built-in default values, though they may appear
- *        differently in some translations:
- *          'long'          -- Quota status: %.2f MB / %.2f MB  (%.2f%%)
- *          'short'         -- %.0f%% of %.0f MB
- *          'nolimit_long'  -- Quota status: %.2f MB / NO LIMIT
- *          'nolimit_short' -- %.0f MB
+ *        differently in some translations ([UNIT] will be replaced with the
+ *        value of the 'unit' parameter):
+ *          'long'          -- Quota status: %.2f [UNIT] / %.2f [UNIT] (%.2f%%)
+ *          'short'         -- %.0f%% of %.0f [UNIT]
+ *          'nolimit_long'  -- Quota status: %.2f [UNIT] / NO LIMIT
+ *          'nolimit_short' -- %.0f [UNIT]
  *
  *        Currently available drivers:
  *          'command'    --  Use the UNIX quota command to handle quotas.
@@ -233,8 +237,10 @@ $servers['cyrus'] = array(
     ),
     'quota' => array(
         'driver' => 'imap',
-        'hide_when_unlimited' => true,
-        'params' => array()
+        'params' => array(
+            'hide_when_unlimited' => true,
+            'unit' => 'MB'
+        )
     ),
     'acl' => true,
     'cache' => false,
@@ -285,7 +291,10 @@ if ($GLOBALS['conf']['kolab']['enabled']) {
         'preferred' => '',
         'quota' => array(
             'driver' => 'imap',
-            'params' => array('hide_quota_when_unlimited' => true),
+            'params' => array(
+                'hide_quota_when_unlimited' => true,
+                'unit' => 'MB'
+            )
         ),
         'acl' => true,
         'cache' => false,
index a54c5ba..790128a 100644 (file)
@@ -2,6 +2,8 @@
 v5.0-cvs
 --------
 
+[mms] Add 'unit' parameter for quota display (Carlos Pires <acmpires@sapo.pt>,
+      Request #7044).
 [mms] Add support for '$Forwarded' IMAP keyword (Request #3402).
 [mms] Attachments in a signed message now can be downloaded via the
       download all attachments link (Bug #2939).
index b8fc862..bf97cb9 100644 (file)
@@ -737,11 +737,12 @@ class IMP
         }
 
         $strings = $quotaDriver->getMessages();
+        list($calc, $unit) = $quotaDriver->getUnit();
         $ret = array('percent' => 0);
 
         if ($quota['limit'] != 0) {
-            $quota['usage'] = $quota['usage'] / (1024 * 1024.0);
-            $quota['limit'] = $quota['limit'] / (1024 * 1024.0);
+            $quota['usage'] = $quota['usage'] / $calc;
+            $quota['limit'] = $quota['limit'] / $calc;
             $ret['percent'] = ($quota['usage'] * 100) / $quota['limit'];
             if ($ret['percent'] >= 90) {
                 $ret['class'] = 'quotaalert';
@@ -750,13 +751,11 @@ class IMP
             } else {
                 $ret['class'] = 'control';
             }
-            if ($long) {
-                $ret['message'] = sprintf($strings['long'], $quota['usage'],
-                                          $quota['limit'], $ret['percent']);
-            } else {
-                $ret['message'] = sprintf($strings['short'], $ret['percent'],
-                                          $quota['limit']);
-            }
+
+            $ret['message'] = $long
+                ? sprintf($strings['long'], $quota['usage'], $unit, $quota['limit'], $unit, $ret['percent'])
+                : sprintf($strings['short'], $ret['percent'], $quota['limit'], $unit);
+            $ret['percent'] = sprintf("%.2f", $ret['percent']);
         } else {
             // Hide unlimited quota message?
             if (!empty($_SESSION['imp']['quota']['hide_when_unlimited'])) {
@@ -765,20 +764,15 @@ class IMP
 
             $ret['class'] = 'control';
             if ($quota['usage'] != 0) {
-                $quota['usage'] = $quota['usage'] / (1024 * 1024.0);
-                if ($long) {
-                    $ret['message'] = sprintf($strings['nolimit_long'],
-                                              $quota['usage']);
-                } else {
-                    $ret['message'] = sprintf($strings['nolimit_short'],
-                                              $quota['usage']);
-                }
+                $quota['usage'] = $quota['usage'] / $calc;
+
+                $ret['message'] = $long
+                    ? sprintf($strings['nolimit_long'], $quota['usage'], $unit)
+                    : sprintf($strings['nolimit_short'], $quota['usage'], $unit);
             } else {
-                if ($long) {
-                    $ret['message'] = sprintf(_("Quota status: NO LIMIT"));
-                } else {
-                    $ret['message'] = _("No limit");
-                }
+                $ret['message'] = $long
+                    ? sprintf(_("Quota status: NO LIMIT"))
+                    : _("No limit");
             }
         }
 
index 73ad5e1..3a37fb7 100644 (file)
@@ -72,7 +72,7 @@ class IMP_Quota
      *
      * @param array $params  Hash containing connection parameters.
      */
-    function __construct($params = array())
+    public function __construct($params = array())
     {
         $this->_params = $params;
 
@@ -106,16 +106,44 @@ class IMP_Quota
         return array(
             'long' => isset($this->_params['format']['long'])
                 ? $this->_params['format']['long']
-                : _("Quota status: %.2f MB / %.2f MB  (%.2f%%)"),
+                : _("Quota status: %.2f %s / %.2f %s  (%.2f%%)"),
             'short' => isset($this->_params['format']['short'])
                 ? $this->_params['format']['short']
-                : _("%.0f%% of %.0f MB"),
+                : _("%.0f%% of %.0f %s"),
             'nolimit_long' => isset($this->_params['format']['nolimit_long'])
                 ? $this->_params['format']['nolimit_long']
-                : _("Quota status: %.2f MB / NO LIMIT"),
+                : _("Quota status: %.2f %s / NO LIMIT"),
             'nolimit_short' => isset($this->_params['format']['nolimit_short'])
                 ? $this->_params['format']['nolimit_short']
-                : _("%.0f MB")
+                : _("%.0f %s")
        );
     }
+
+    /**
+     * Determine the units of storage to display in the quota message.
+     *
+     * @return array  An array of size and unit type.
+     */
+    public function getUnit()
+    {
+        $unit = isset($this->_params['unit']) ? $this->_params['unit'] : 'MB';
+
+        switch ($unit) {
+        case 'GB':
+            $calc = 1024 * 1024 * 1024.0;
+            break;
+
+        case 'KB':
+            $calc = 1024.0;
+            break;
+
+        case 'MB':
+        default:
+            $calc = 1024 * 1024.0;
+            $unit = 'MB';
+            break;
+        }
+
+        return array($calc, $unit);
+    }
 }