Look improvements. Can identify unread message and switch from a message to
[banana.git] / banana / spool.inc.php
index ed898e2..6ce1751 100644 (file)
@@ -9,7 +9,7 @@
 
 require_once dirname(__FILE__) . '/banana.inc.php';
 
-define('BANANA_SPOOL_VERSION', '0.4');
+define('BANANA_SPOOL_VERSION', '0.5.1');
 
 /** Class spoolhead
  *  class used in thread overviews
@@ -22,6 +22,7 @@ class BananaSpoolHead
     public $subject;
     /** author */
     public $from;
+    public $color;
     /** reference of parent */
     public $parent = null;
     /** paren is direct */
@@ -51,6 +52,7 @@ class BananaSpoolHead
         $this->date       = $message['date'];
         $this->subject    = $message['subject'];
         $this->from       = $message['from'];
+        $this->color      = sprintf('#%06x', abs(crc32($this->from) % 0xffffff));
         $this->desc       = 1;
         $this->isread     = true;
         $this->descunread = 0;
@@ -71,6 +73,8 @@ class BananaSpool
     public $ids;
     /** thread starts */
     public $roots;
+    /** protocole specific data */
+    public $storage = array();
 
     private $unreadnb = 0;
 
@@ -86,13 +90,13 @@ class BananaSpool
         $this->group      = $group;
     }
 
-    public static function getSpool($group, $since = 0, $clean = false)
+    public static function &getSpool($group, $since = 0, $clean = false)
     {
         if (!is_null(Banana::$spool) && Banana::$spool->group == $group) {
             $spool =& Banana::$spool;
         } else {
-            $spool = BananaSpool::readFromFile($group);
-        }        
+            $spool =& BananaSpool::readFromFile($group);
+        }
         if (is_null($spool)) {
             $spool = new BananaSpool($group);
         }
@@ -114,15 +118,17 @@ class BananaSpool
         return $file . Banana::$protocole->filename();
     }
 
-    private static function readFromFile($group)
+    private static function &readFromFile($group)
     {
+        $spool = null;
         $file = BananaSpool::spoolFilename($group);
         if (!file_exists($file)) {
-            return null;
+            return $spool;
         }
         $spool =  unserialize(file_get_contents($file));
         if ($spool->version != BANANA_SPOOL_VERSION || $spool->mode != Banana::SPOOL_ALL) {
-            return null;
+            $spool = null;
+            return $spool;
         }
         $spool->markAllAsRead();
         return $spool;
@@ -130,24 +136,24 @@ class BananaSpool
 
     private function compare($a, $b)
     {
-        return ($b->date >= $a->date);
+        return ($this->overview[$b]->date >= $this->overview[$a]->date);
     }
 
     private function saveToFile()
     {
         $file = BananaSpool::spoolFilename($this->group);
-        uasort($this->overview, array($this, 'compare'));
 
         $this->roots = Array();
-        foreach($this->overview as $id=>$msg) {
+        foreach($this->overview as $id=>&$msg) {
             if (is_null($msg->parent)) {
                 $this->roots[] = $id;
             }
         }
+        usort($this->roots, array($this, 'compare'));
 
         if ($this->mode == Banana::SPOOL_ALL) {
             file_put_contents($file, serialize($this));
-        }    
+        }
     }
 
     private function build()
@@ -157,7 +163,7 @@ class BananaSpool
         // Compute the range of indexes
         list($msgnum, $first, $last) = Banana::$protocole->getIndexes();
         if ($last < $first) {
-            $threshold = $firt + $msgnum - $last;
+            $threshold = $first + $msgnum - $last;
             $threshold = (int)(log($threshold)/log(2));
             $threshold = (2 ^ ($threshold + 1)) - 1;
         }
@@ -169,12 +175,12 @@ class BananaSpool
         }
         $clean  = $this->clean($first, $last, $msgnum);
         $update = $this->update($first, $last, $msgnum);
-        
+
         if ($clean || $update) {
             $this->saveToFile();
         }
     }
-    
+
     private function clean(&$first, &$last, $msgnum)
     {
         $do_save = false;
@@ -196,7 +202,7 @@ class BananaSpool
 
     private function update(&$first, &$last, $msgnum)
     {
-        if ($first > $last || !$msgnum) {       
+        if ($first > $last || !$msgnum) {
             return false;
         }
 
@@ -235,7 +241,7 @@ class BananaSpool
                         $p = $this->overview[$p]->parent;
                     } else {
                         $p = null;
-                    }    
+                    }
                 }
             }
         }
@@ -243,7 +249,7 @@ class BananaSpool
         return true;
     }
 
-    private function updateUnread($since)
+    public function updateUnread($since)
     {
         if (empty($since)) {
             return;
@@ -277,6 +283,10 @@ class BananaSpool
         $this->mode = $mode;
         switch ($mode) {
           case Banana::SPOOL_UNREAD:
+            $num = max(array_keys($this->overview));
+            if ($this->overview[$num]->isread) {
+                break;
+            }
             foreach ($this->roots as $k=>$i) {
                 if ($this->overview[$i]->descunread == 0) {
                     $this->killdesc($i);
@@ -383,7 +393,7 @@ class BananaSpool
             if ($msgid !== false) {
                 unset($this->roots[$msgid]);
             }
-            
+
             if ($write) {
                 $this->saveToFile();
             }
@@ -401,8 +411,10 @@ class BananaSpool
             $format = _b_('hier')." %H:%M";
         } elseif ($today < 7 + $dday) {
             $format = '%a %H:%M';
-        } else {
+        } elseif ($today < 90 + $dday) {
             $format = '%a %e %b';
+        } else {
+            $format = '%a %e %b %Y';
         }
         return strftime($format, $stamp);
     }
@@ -412,18 +424,18 @@ class BananaSpool
      * @param $_index INTEGER linear number of post in the tree
      * @param $_first INTEGER linear number of first post displayed
      * @param $_last INTEGER linear number of last post displayed
-     * @param $_ref STRING MSGNUM of current post 
+     * @param $_ref STRING MSGNUM of current post
      * @param $_pfx_node STRING prefix used for current node
      * @param $_pfx_end STRING prefix used for children of current node
      * @param $_head BOOLEAN true if first post in thread
      *
      * If you want to analyse subject, you can define the function hook_formatDisplayHeader
      */
-    private function _to_html($_id, $_index, $_first=0, $_last=0, $_ref="", $_pfx_node="", $_pfx_end="", $_head=true)
+    private function _to_html($_id, $_index, $_first=0, $_last=0, $_ref="", $_pfx_node="", $_pfx_end="", $_head=true, $_pfx_id="")
     {
         static $spfx_f, $spfx_n, $spfx_Tnd, $spfx_Lnd, $spfx_snd, $spfx_T, $spfx_L, $spfx_s, $spfx_e, $spfx_I;
         if (!isset($spfx_f)) {
-            $spfx_f   = Banana::$page->makeImg(Array('img' => 'k1',       'alt' => 'o', 'height' => 21,  'width' => 9)); 
+            $spfx_f   = Banana::$page->makeImg(Array('img' => 'k1',       'alt' => 'o', 'height' => 21,  'width' => 9));
             $spfx_n   = Banana::$page->makeImg(Array('img' => 'k2',       'alt' => '*', 'height' => 21,  'width' => 9));
             $spfx_Tnd = Banana::$page->makeImg(Array('img' => 'T-direct', 'alt' => '+', 'height' => 21, 'width' => 12));
             $spfx_Lnd = Banana::$page->makeImg(Array('img' => 'L-direct', 'alt' => '`', 'height' => 21, 'width' => 12));
@@ -444,16 +456,16 @@ class BananaSpool
         if ($_index >= $_first) {
             $hc = empty($overview->children);
 
-            $res .= '<tr class="' . ($_index%2 ? 'pair' : 'impair') . ($overview->isread ? '' : ' new') . "\">\n";
+            $res .= '<tr id="'.$_pfx_id.$_id.'" class="' . ($_index%2 ? 'pair' : 'impair') . ($overview->isread ? '' : ' new') . "\">\n";
             $res .= '<td class="date">' . $this->formatDate($overview->date) . " </td>\n";
             $res .= '<td class="subj' . ($_index == $_ref ? ' cur' : '') . '"><div class="tree">'
                 . $_pfx_node .($hc ? ($_head ? $spfx_f : ($overview->parent_direct ? $spfx_s : $spfx_snd)) : $spfx_n)
                 . '</div>';
-            $subject = $overview->subject;
+            $popup = $subject = $overview->subject;
             if (function_exists('hook_formatDisplayHeader')) {
                 list($subject, $link) = hook_formatDisplayHeader('subject', $subject, true);
             } else {
-                $subject = banana_catchFormats(banana_htmlentities(stripslashes($subject)));
+                $subject = banana_catchFormats(banana_entities(stripslashes($subject)));
                 $link = null;
             }
             if (empty($subject)) {
@@ -461,7 +473,7 @@ class BananaSpool
             }
             if ($_index != $_ref) {
                 $subject = Banana::$page->makeLink(Array('group' => $this->group, 'artid' => $_id,
-                                                    'text'  => $subject, 'popup' => $subject));
+                                                    'text'  => $subject, 'popup' => $popup));
             }
             $res .= '&nbsp;' . $subject . $link;
             $res .= "</td>\n<td class='from'>" . BananaMessage::formatFrom($overview->from) . "</td>\n</tr>";
@@ -469,7 +481,7 @@ class BananaSpool
             if ($hc) {
                 return $res;
             }
-        } 
+        }
 
         $_index ++;
         $children = $overview->children;
@@ -482,11 +494,11 @@ class BananaSpool
                 if (sizeof($children)) {
                     $res .= $this->_to_html($child, $_index, $_first, $_last, $_ref,
                             $_pfx_end . ($overview->parent_direct ? $spfx_T : $spfx_Tnd),
-                            $_pfx_end . $spfx_I, false);
+                            $_pfx_end . $spfx_I, false,$_id.'_');
                 } else {
                     $res .= $this->_to_html($child, $_index, $_first, $_last, $_ref,
                             $_pfx_end . ($overview->parent_direct ? $spfx_L : $spfx_Lnd),
-                            $_pfx_end . $spfx_e, false);
+                            $_pfx_end . $spfx_e, false,$_id.'_');
                 }
             }
             $_index += $overview->desc;
@@ -502,7 +514,8 @@ class BananaSpool
      */
     public function toHtml($first = 0, $overview = false)
     {
-        $res = '';
+        $res = Banana::$page->makeJs('jquery');
+        $res .= Banana::$page->makeJs('spool_toggle');
 
         if (!$overview) {
             $_first = $first;
@@ -527,6 +540,70 @@ class BananaSpool
         return $res;
     }
 
+
+    public function _buildTree($id, BananaSpoolHead &$head, $current) {
+        static $t_e, $u_h, $u_ht, $u_vt, $u_l, $u_f, $r_h, $r_ht, $r_vt, $r_l, $r_f;
+        if (!isset($spfx_f)) {
+            $t_e   = Banana::$page->makeImg(Array('img' => 'e',  'alt' => '&nbsp;', 'height' => 18,  'width' => 14));
+            $u_h   = Banana::$page->makeImg(Array('img' => 'h2', 'alt' => '-', 'height' => 18,  'width' => 14));
+            $u_ht  = Banana::$page->makeImg(Array('img' => 'T2', 'alt' => '+', 'height' => 18, 'width' => 14));
+            $u_vt  = Banana::$page->makeImg(Array('img' => 't2', 'alt' => '`', 'height' => 18, 'width' => 14));
+            $u_l   = Banana::$page->makeImg(Array('img' => 'l2', 'alt' => '|', 'height' => 18, 'width' => 14));
+            $u_f   = Banana::$page->makeImg(Array('img' => 'f2', 'alt' => 't', 'height' => 18, 'width' => 14));
+            $r_h   = Banana::$page->makeImg(Array('img' => 'h2r', 'alt' => '-', 'height' => 18,  'width' => 14));
+            $r_ht  = Banana::$page->makeImg(Array('img' => 'T2r', 'alt' => '+', 'height' => 18, 'width' => 14));
+            $r_vt  = Banana::$page->makeImg(Array('img' => 't2r', 'alt' => '`', 'height' => 18, 'width' => 14));
+            $r_l   = Banana::$page->makeImg(Array('img' => 'l2r', 'alt' => '|', 'height' => 18, 'width' => 14));
+            $r_f   = Banana::$page->makeImg(Array('img' => 'f2r', 'alt' => 't', 'height' => 18, 'width' => 14));
+        }
+        $style = 'background-color:' . $head->color . '; text-decoration: none';
+        $prof  = 1;
+        $text = '<span style="' . $style . '" title="' . banana_entities($head->from) . '">' .
+                '<input type="radio" name="banana_tree" '. ($id == $current ? 'checked="checked" ' : ' ' ) .
+                (Banana::$msgshow_javascript ? 'onchange="window.location=\'' .
+                    Banana::$page->makeURL(array('group' => $this->group, 'artid' => $id)) . '\'"' : ' disabled="disabled"')
+                .' />' .
+                '</span>';
+        $array = array($text);
+        foreach ($head->children as $key=>&$child) {
+            $msg =& $this->overview[$child];
+            list($tpr, $tree) = $this->_buildTree($child, $msg, $current);
+            $last = $key == count($head->children) - 1;
+            foreach ($tree as $kt=>&$line) {
+                if ($kt == 0 && $key == 0 && !$last) {
+                    $array[0] .= ($msg->isread ? $r_ht : $u_ht) . $line;
+                } else if($kt == 0 && $key == 0) {
+                    $array[0] .= ($msg->isread ? $r_h : $u_h)  . $line;
+                } else if ($kt == 0 && $last) {
+                    $array[] = $t_e . ($msg->isread ? $r_vt : $u_vt) . $line;
+                } else if ($kt == 0) {
+                    $array[] = $t_e . ($msg->isread ? $r_f : $u_f) . $line;
+                } else if ($last) {
+                    $array[] = $t_e . $t_e . $line;
+                } else {
+                    $array[] = $t_e . ($msg->isread ? $r_l : $u_l) . $line;
+                }
+            }
+            if ($tpr > $prof) {
+                $prof = $tpr + 1;
+            }
+        }
+        return array($prof, $array);
+    }
+
+    /** build the spool tree associated with the given message
+     */
+    public function buildTree($id) {
+        $pos      =  $id;
+        $overview =& $this->overview[$id];
+        while (!is_null($overview->parent)) {
+            $pos = $overview->parent;
+            $overview =& $this->overview[$pos];
+        }
+        list($prof, $tree) = $this->_buildTree($pos, $overview, $id);
+        return implode("\n", $tree);
+    }
+
     /** computes linear post index
      * @param $_id INTEGER MSGNUM of post
      * @return INTEGER linear index of post
@@ -539,7 +616,7 @@ class BananaSpool
             $id_parent = $this->overview[$id_cur]->parent;
             if (is_null($id_parent)) break;
             $pos       = array_search($id_cur, $this->overview[$id_parent]->children);
-        
+
             for ($i = 0; $i < $pos ; $i++) {
                 $ndx += $this->overview[$this->overview[$id_parent]->children[$i]]->desc;
             }
@@ -571,6 +648,32 @@ class BananaSpool
         return $id_cur;
     }
 
+    /** Return the last post id with the given subject
+     * @param subject
+     */
+    public function getPostId($subject)
+    {
+        $subject = trim($subject);
+        $id = max(array_keys($this->overview));
+        while (isset($this->overview[$id])) {
+            $test = $this->overview[$id]->subject;
+            if (function_exists('hook_formatDisplayHeader')) {
+                $val = hook_formatDisplayHeader('subject', $test, true);
+                if (is_array($val)) {
+                    $test = banana_html_entity_decode($val[0]);
+                } else {
+                    $test = banana_html_entity_decode($val);
+                }
+            }
+            $test = trim($test);
+            if ($test == $subject) {
+                return $id;
+            }
+            $id--;
+        }
+        return -1;
+    }
+
     /** Returns previous thread root index
      * @param id INTEGER message number
      */
@@ -632,7 +735,7 @@ class BananaSpool
         if (count($this->overview[$id]->children) != 0) {
             return $this->overview[$id]->children[0];
         }
-        
+
         $cur    = $id;
         while (true) {
             $parent = $this->overview[$cur]->parent;
@@ -665,7 +768,7 @@ class BananaSpool
             $unread = $this->_nextUnread($child);
             if (!is_null($unread)) {
                 return $unread;
-            }    
+            }
         }
         return null;
     }
@@ -713,7 +816,7 @@ class BananaSpool
             $cur = $parent;
         } while(!is_null($cur));
         return null;
-    }    
+    }
 }
 
 // vim:set et sw=4 sts=4 ts=4 enc=utf-8: