TextScreen: refactor to eliminate dependence on AastraIPPhone classes

Begin building a device independent representation object

Signed-off-by: James Bottomley <JBottomley@Parallels.com>
diff --git a/Makefile b/Makefile
index ae496ff..d1a95a3 100644
--- a/Makefile
+++ b/Makefile
@@ -36,7 +36,8 @@
 	include/BaseAastra.class.php \
 	include/BaseList.class.php \
 	include/DatabaseListManager.class.php \
-	include/ConfigFile.class.php
+	include/ConfigFile.class.php \
+	include/TextScreen.class.php
 
 base-doc-files = \
 	etc/apache-asterisk \
diff --git a/include/AastraIPPhoneFormattedTextScreen.class.php b/include/AastraIPPhoneFormattedTextScreen.class.php
index eeb7e0c..258cc5b 100644
--- a/include/AastraIPPhoneFormattedTextScreen.class.php
+++ b/include/AastraIPPhoneFormattedTextScreen.class.php
@@ -107,7 +107,7 @@
 		foreach($pieces as $value) $this->_entries[] = new AastraIPPhoneFormattedTextScreenEntry($value, $size, $align, $color, 'normal');
 	}
 
-	function setScrollStart($height)
+	function setScrollStart($height = '')
 	{
 		$this->_entries[] = new AastraIPPhoneFormattedTextScreenEntry(NULL, $height, NULL, NULL, 'scrollstart');
 	}
@@ -203,6 +203,13 @@
 		$out .= ">\n";
 
 		# Lines
+		if ($this->_title!='')
+			{
+			$title = $this->escape($this->_title);
+		 	$out .= "<Line Size=\"double\" Align=\"center\">";
+			$out .= $title."</Line>\n";
+			}
+
 		if (isset($this->_entries) && is_array($this->_entries)) 
 			{
 			foreach ($this->_entries as $entry) $out .= $entry->render();
diff --git a/include/BaseList.class.php b/include/BaseList.class.php
index b3fcedc..9c981fe 100644
--- a/include/BaseList.class.php
+++ b/include/BaseList.class.php
@@ -4,11 +4,10 @@
 ##
 # Class implementing a wrapper for List objects
 ##
-require_once('AastraIPPhoneTextMenu.class.php');
+require_once('TextScreen.class.php');
 
-class BaseList extends AastraIPPhoneTextMenu {
+class BaseList extends TextScreen {
 	var $data;
-	var $window = 4;
 	var $page;
 	var $title;
 	var $url;
@@ -30,8 +29,8 @@
 	}
 
 	function build() {
-		$pages = intval((count($this->data)+3)/$this->window);
-		$down = $this->page + 1 == $pages ? null : $this->url.'&page='.($this->page + 1).'&index='.$this->window;
+		$pages = intval((count($this->data)+3)/$this->_phone['entries']);
+		$down = $this->page + 1 == $pages ? null : $this->url.'&page='.($this->page + 1).'&index='.$this->_phone['entries'];
 		$up = $this->page == 0 ? null : $this->url.'&page='.($this->page - 1).'&index=0';
 		parent::setTitle($this->title.' Page: '.($this->page+1).'/'.$pages);
 		$this->setStyle('none');
@@ -44,9 +43,8 @@
 		if (isset($_GET['index'])) {
 			$this->setDefaultIndex($_GET['index']);
 		}
-		$this->setUnitScroll();
-		$start = $this->page * $this->window;
-		$end = ($this->page + 1) * $this->window;
+		$start = $this->page * $this->_phone['entries'];
+		$end = ($this->page + 1) * $this->_phone['entries'];
 		if ($end > count($this->data)) {
 			$end = count($this->data);
 		}
diff --git a/include/Blacklist.class.php b/include/Blacklist.class.php
index bc917e3..2e30fce 100644
--- a/include/Blacklist.class.php
+++ b/include/Blacklist.class.php
@@ -5,7 +5,7 @@
 # Blacklist handling class
 ##
 require_once('BaseAastra.class.php');
-require_once('AastraIPPhoneTextScreen.class.php');
+require_once('TextScreen.class.php');
 require_once('AastraIPPhoneInputScreen.class.php');
 
 class Blacklist extends BaseAastra {
@@ -34,7 +34,7 @@
 			}
 			return;
 		}
-		$this->displayObject(new AastraIPPhoneTextScreen());
+		$this->displayObject(new TextScreen());
 		$this->do->SetText('Are you sure you want to remove '.$this->lastnumber.' from the '.$list.'?');
 		$this->yesno();
 	}
@@ -51,7 +51,7 @@
 			}
 			return;
 		}
-		$this->displayObject(new AastraIPPhoneTextScreen());
+		$this->displayObject(new TextScreen());
 		$this->do->SetText('Are you sure you want to blacklist '.$this->lastnumber.'?');
 		$this->yesno();
 	}
@@ -91,7 +91,7 @@
 	}
 
 	function start() {
-		$this->displayObject(new AastraIPPhoneTextScreen());
+		$this->displayObject(new TextScreen());
 		$text = 'The last number was '.$this->lastnumber;
 		if ($this->blacklist) {
 			$text .= ' which is already blacklisted';
diff --git a/include/TextScreen.class.php b/include/TextScreen.class.php
new file mode 100644
index 0000000..f8fed55
--- /dev/null
+++ b/include/TextScreen.class.php
@@ -0,0 +1,165 @@
+<?php
+##
+# Copyright 2013 by James Bottomley
+##
+# Phone Independent text screen handling class
+##
+require_once('AastraIPPhoneTextScreen.class.php');
+require_once('AastraIPPhoneFormattedTextScreen.class.php');
+require_once('AastraIPPhoneTextMenu.class.php');
+
+class TextScreen {
+	var $_phone = array(
+		# notional: the 6739i uses a variable width font
+		'width' => 38,
+		'height' => 14,
+		'entries' => 4,
+		'formatted' => true,
+		'unitscrollbug' => true,
+	);
+	var $_softkeys = array();
+	var $_entries = array();
+	var $_text = array();
+	var $_title;
+	var $_destroyOnExit;
+	var $_cancelAction;
+	var $_style;
+	var $_scrollUp;
+	var $_scrollDown;
+	var $_defaultIndex;
+	var $_allowDrop;
+
+	function __construct() {
+	}
+
+	function addSoftKey($index, $label, $url, $icon = null) {
+		$this->_softkeys[$index] = array(
+			'label' => $label,
+			'url' => $url,
+			'icon' => $icon,
+		);
+	}
+
+	function setTitle($title) {
+		$this->_title = $title;
+	}
+
+	function setStyle($style) {
+		$this->_style = $style;
+	}
+
+	function setCancelAction($cancelAction) {
+		$this->_cancelAction=$cancelAction;
+	}
+
+	function setDestroyOnExit() {
+		$this->_destroyOnExit = 'yes';
+	}
+
+	function setAllowDrop() {
+		$this->_allowDrop = 'yes';
+	}
+
+	function setDefaultIndex($index) {
+		$this->_defaultIndex = $index;
+	}
+
+	function setScrollUp($uri) {
+		$this->_scrollUp = $uri;
+	}
+
+	function setScrollDown($uri) {
+		$this->_scrollDown = $uri;
+	}
+
+	function addEntry($name, $url, $selection = null, $icon = null,
+			$dial = null, $line = null) {
+		$this->_entries[] = array(
+			'name' => $name,
+			'url' => $url,
+			'selection' => $selection,
+			'icon' => $icon,
+		);
+	}
+
+	function setText($text) {
+		$this->_text = array($text);
+	}
+
+	function addtext($text) {
+		$this->_text[] = $text;
+	}
+
+	function output() {
+		if (count($this->_entries) == 0) {
+			if ($this->_phone['formatted']) {
+				$o = new AastraIpPhoneFormattedTextScreen();
+			} else {
+				$o = new AastraIPPhoneTextScreen();
+			}
+		} else {
+			$o = new AastraIPPhoneTextMenu();
+		}
+		$o->setTitle($this->_title);
+		if ($this->_destroyOnExit) {
+			$o->setDestroyOnExit();
+		}
+		if ($this->_allowDrop) {
+			$o->setAllowDrop();
+		}
+		if ($this->_phone['unitscrollbug'] && method_exists($o, 'setUnitScroll')) {
+			$o->setUnitScroll();
+		}
+		if ($this->_cancelAction) {
+			$o->setCancelAction($this->_cancelAction);
+		}
+		if ($this->_style) {
+			$o->setStyle($this->_style);
+		}
+		if ($this->_scrollUp) {
+			$o->setScrollUp($this->_scrollUp);
+		}
+		if ($this->_scrollDown) {
+			$o->setScrollDown($this->_scrollDown);
+		}
+		if ($this->_defaultIndex) {
+			$o->setDefaultIndex($this->_defaultIndex);
+		}
+		foreach($this->_entries as $e) {
+			$name = $e['name'];
+			$url = $e['url'];
+			$selection = $e['selection'];
+			$icon = $e['icon'];
+			$o->addEntry($name, $url, $selection, $icon);
+		}
+		foreach($this->_softkeys as $k=>$e) {
+			$label = $e['label'];
+			$url = $e['url'];
+			$icon = $e['icon'];
+			$o->addSoftKey($k, $label, $url, $icon);
+		}
+		$o->setTitle($this->_title);
+		if (count($this->_entries) != 0) {
+			#do nothing
+		} else if (count($this->_text) == 0) {
+			$o->setText($this->_text[0]);
+		} else if ($this->_phone['formatted']) {
+			foreach($this->_text as $t) {
+				$s = preg_split('/\n/',wordwrap($t, $this->_phone['width'], "\n\xa0\xa0"));
+				foreach($s as $l) {
+					$o->addLine($l);
+				}
+			}
+		} else {
+			$text = '';
+			foreach($this->_text as $t) {
+				$c = strlen($t) % $this->_phone['width'];
+				$c = $c ? $this->_phone['width'] - $c : 0;
+				error_log("length of t is ".strlen($t).' c='.$c);
+				$text .= $t . str_repeat("\xa0", $c).'|';
+			}
+			$o->setText($text);
+		}
+		$o->output();
+	}
+}
diff --git a/include/Voicemail.class.php b/include/Voicemail.class.php
index 37c58ec..483f018 100644
--- a/include/Voicemail.class.php
+++ b/include/Voicemail.class.php
@@ -1,8 +1,8 @@
 <?php
 require_once('BaseAastra.class.php');
-require_once('AastraIPPhoneTextScreen.class.php');
 require_once('VoicemailListManager.class.php');
 require_once('AastraIPPhoneExecute.class.php');
+require_once('TextScreen.class.php');
 
 class Voicemail extends BaseAastra {
 
@@ -221,7 +221,7 @@
 	function message() {
 		$box = $_GET['box'];
 		if (count($this->box[$box]) == 0) {
-			$this->displayObject(new AastraIPPhoneTextScreen());
+			$this->displayObject(new TextScreen());
 			$this->title = 'Mailbox '.$box;
 			$this->do->setText('is empty');
 			$this->back = $this->url;
@@ -252,8 +252,10 @@
 		}
 		$c = new ConfigFile($this->path.'/'.$box.'/'.$msg.'.txt');
 		$m = $c->section('message');
-		$this->displayObject(new AastraIPPhoneTextScreen());
-		$this->do->setText('Received: '.$m['origdate'].' From: '.$m['callerid'].' length '.$m['duration'].' seconds');
+		$this->displayObject(new TextScreen());
+		$this->do->addText('Received: '.$m['origdate']);
+		$this->do->addText('From: '.$m['callerid']);
+		$this->do->addText('Length '.$m['duration'].' seconds');
 		$this->$ordinary($box, $msg, $i);
 	}
 
@@ -265,18 +267,16 @@
 	}
 
 	function start() {
-		$text = 'Voicemail has';
-		$this->displayObject(new AastraIPPhoneTextScreen());
+		$this->displayObject(new TextScreen());
 		$softkey = 1;
 		foreach ($this->mapping as $k => $v) {
 			$c = count($this->box[$k]);
-			$text .= ' '.$c.' '.$v;
+			$this->do->addText($c.' '.$v);
 			if ($c) {
 				$this->do->addSoftkey($softkey, $v,  $this->url.'?action=mailbox&box='.$k);
 			}
 			$softkey++;
 		}
-		$this->do->setText($text);
 	}
 }
 ?>