Skip to content

Commit

Permalink
Refactor contacts image code to allow sender country flags to be impl…
Browse files Browse the repository at this point in the history
…emented within it
  • Loading branch information
slusarz committed Oct 14, 2013
1 parent f1806bb commit a0222c5
Show file tree
Hide file tree
Showing 12 changed files with 188 additions and 126 deletions.
9 changes: 5 additions & 4 deletions imp/config/conf.xml
Expand Up @@ -141,11 +141,12 @@
<configtab name="contactsimage" desc="Contacts Images">
<configsection name="contactsimage">
<configmultienum name="backends" required="false" desc="The backends to
query for contacts images.">IMP_Contacts_Image_Addressbook
query for contacts images.">IMP_Contacts_Avatar_Addressbook, IMP_Contacts_Flag_Host
<values>
<value desc="Horde address book">IMP_Contacts_Image_Addressbook</value>
<value desc="Gravatar">IMP_Contacts_Image_Gravatar</value>
<value desc="Default (unknown user)">IMP_Contacts_Image_Unknown</value>
<value desc="Horde address book">IMP_Contacts_Avatar_Addressbook</value>
<value desc="Gravatar">IMP_Contacts_Avatar_Gravatar</value>
<value desc="Default (unknown user)">IMP_Contacts_Avatar_Unknown</value>
<value desc="Country Flag based on Hostname of sender">IMP_Contacts_Flag_Host</value>
</values>
</configmultienum>
</configsection>
Expand Down
11 changes: 6 additions & 5 deletions imp/js/dimpcore.js
Expand Up @@ -270,13 +270,14 @@ var DimpCore = {
addr: tmp2.b
}, {
callback: function (r) {
if (r.flagimg) {
tmp.insert({ top: r.flagimg })
.down().addClassName('flagimg');
if (r.flag) {
tmp.insert({
top: new Element('IMG', { title: r.flagname, src: r.flag }).addClassName('contactimg')
});
}
if (r.url) {
if (r.avatar) {
tmp.insert({
top: new Element('IMG', { src: r.url }).addClassName('contactimg')
top: new Element('IMG', { src: r.avatar }).addClassName('contactimg')
});
}
}
Expand Down
22 changes: 13 additions & 9 deletions imp/lib/Ajax/Application/Handler/Dynamic.php
Expand Up @@ -1142,7 +1142,7 @@ public function newFilter()
}

/**
* AJAX action: Return the contacts image for a given e-mail address.
* AJAX action: Return the contacts images for a given e-mail address.
*
* Variables used:
* <pre>
Expand All @@ -1151,22 +1151,26 @@ public function newFilter()
*
* @return object An object with the following properties:
* <pre>
* - flagimg: (string) An IMG tag with the country flag of the sender.
* - url: (string) The URL of the contacts image.
* - avatar: (string) The URL of the avatar image.
* - flag: (string) The URL of the sender's country flag image.
* - flagname: (string) The name of the country of the sender.
* </pre>
*/
public function getContactsImage()
{
$contacts_img = new IMP_Contacts_Image($this->vars->addr);
$out = new stdClass;

try {
$contacts_img = new IMP_Contacts_Image($this->vars->addr);
$out->url = strval($contacts_img->getUrlOb());
$res = $contacts_img->getImage($contacts_img::AVATAR);
$out->avatar = strval($res['url']);
} catch (IMP_Exception $e) {}

$addr = new Horde_Mail_Rfc822_Address($this->vars->addr);
if ($flag = Horde_Core_Ui_FlagImage::generateFlagImageByHost($addr->host)) {
$out->flagimg = $flag;
}
try {
$res = $contacts_img->getImage($contacts_img::FLAG);
$out->flag = strval($res['url']);
$out->flagname = $res['desc'];
} catch (IMP_Exception $e) {}

return $out;
}
Expand Down
33 changes: 13 additions & 20 deletions imp/lib/Basic/Message.php
Expand Up @@ -271,26 +271,19 @@ protected function _init()
/* Build From address links. */
$display_headers['from'] = $imp_ui->buildAddressLinks($envelope->from, $self_link);

/* Add country/flag image. Try X-Originating-IP first, then fall back
* to the sender's domain name. */
$from_img = '';
$origin_host = str_replace(array('[', ']'), '', $mime_headers->getValue('X-Originating-IP'));
if ($origin_host) {
if (!is_array($origin_host)) {
$origin_host = array($origin_host);
}
foreach ($origin_host as $host) {
$from_img .= Horde_Core_Ui_FlagImage::generateFlagImageByHost($host) . ' ';
}
trim($from_img);
}

if (empty($from_img) && !empty($envelope->from)) {
$from_img .= Horde_Core_Ui_FlagImage::generateFlagImageByHost($envelope->from[0]->host) . ' ';
}

if (!empty($from_img)) {
$display_headers['from'] .= '&nbsp;' . $from_img;
/* Add country/flag image. */
if (!empty($envelope->from)) {
$contacts_img = new IMP_Contacts_Image($envelope->from[0]);
try {
$res = $contacts_img->getData($contacts_img::FLAG);
$display_headers['from'] .= '&nbsp;' . Horde_Themes_Image::tag(
$res['url'],
array(
'alt' => $res['desc'],
'fullsrc' => true
)
);
} catch (IMP_Exception $e) {}
}

/* Look for Face information. */
Expand Down
Expand Up @@ -12,19 +12,19 @@
*/

/**
* Generate contact image by using local addressbook.
* Generate contact avatar image by using local addressbook.
*
* @author Michael Slusarz <slusarz@horde.org>
* @category Horde
* @copyright 2013 Horde LLC
* @license http://www.horde.org/licenses/gpl GPL
* @package IMP
*/
class IMP_Contacts_Image_Addressbook implements IMP_Contacts_Image_Backend
class IMP_Contacts_Avatar_Addressbook implements IMP_Contacts_Avatar_Backend
{
/**
*/
public function rawImage($email)
public function avatarImg($email)
{
global $injector, $registry;

Expand Down Expand Up @@ -55,19 +55,15 @@ public function rawImage($email)
$type = $res[$email][0]['phototype'];
}

return Horde_Url_Data::create($type, $data);
return array(
'desc' => '',
'url' => Horde_Url_Data::create($type, $data)
);
}
} catch (Horde_Exception $e) {}
}

return null;
}

/**
*/
public function urlImage($email)
{
return null;
}

}
34 changes: 34 additions & 0 deletions imp/lib/Contacts/Avatar/Backend.php
@@ -0,0 +1,34 @@
<?php
/**
* Copyright 2013 Horde LLC (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (GPL). If you
* did not receive this file, see http://www.horde.org/licenses/gpl.
*
* @category Horde
* @copyright 2013 Horde LLC
* @license http://www.fsf.org/copyleft/gpl.html GPL
* @package IMP
*/

/**
* Interface for a contact avatar image backend.
*
* @author Michael Slusarz <slusarz@horde.org>
* @category Horde
* @copyright 2013 Horde LLC
* @license http://www.horde.org/licenses/gpl GPL
* @package IMP
*/
interface IMP_Contacts_Avatar_Backend
{
/**
* URL of the contact avatar image.
*
* @param string $email An email address.
*
* @return array See IMP_Contacts_Image#getImage().
*/
public function avatarImg($email);

}
Expand Up @@ -12,19 +12,19 @@
*/

/**
* Generate contact image using the Gravatar service.
* Generate contact avatar image using the Gravatar service.
*
* @author Michael Slusarz <slusarz@horde.org>
* @category Horde
* @copyright 2013 Horde LLC
* @license http://www.horde.org/licenses/gpl GPL
* @package IMP
*/
class IMP_Contacts_Image_Gravatar implements IMP_Contacts_Image_Backend
class IMP_Contacts_Avatar_Gravatar implements IMP_Contacts_Avatar_Backend
{
/**
*/
public function rawImage($email)
public function avatarImg($email)
{
if (class_exists('Horde_Service_Gravatar')) {
$gravatar = new Horde_Service_Gravatar(
Expand All @@ -42,9 +42,9 @@ public function rawImage($email)
$img_data = stream_get_contents($data);

if (strlen($img_data)) {
return Horde_Url_Data::create(
'image/jpeg',
$img_data
return array(
'desc' => '',
'url' => Horde_Url_Data::create('image/jpeg', $img_data)
);
}
}
Expand All @@ -53,11 +53,4 @@ public function rawImage($email)
return null;
}

/**
*/
public function urlImage($email)
{
return null;
}

}
Expand Up @@ -12,26 +12,19 @@
*/

/**
* Generate image for an unknown contact.
* Generate avatar image for an unknown contact.
*
* @author Michael Slusarz <slusarz@horde.org>
* @category Horde
* @copyright 2013 Horde LLC
* @license http://www.horde.org/licenses/gpl GPL
* @package IMP
*/
class IMP_Contacts_Image_Unknown implements IMP_Contacts_Image_Backend
class IMP_Contacts_Avatar_Unknown implements IMP_Contacts_Avatar_Backend
{
/**
*/
public function rawImage($email)
{
return null;
}

/**
*/
public function urlImage($email)
public function avatarImg($email)
{
return Horde_Themes::img('unknown_contact.jpg');
}
Expand Down
34 changes: 34 additions & 0 deletions imp/lib/Contacts/Flag/Backend.php
@@ -0,0 +1,34 @@
<?php
/**
* Copyright 2013 Horde LLC (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (GPL). If you
* did not receive this file, see http://www.horde.org/licenses/gpl.
*
* @category Horde
* @copyright 2013 Horde LLC
* @license http://www.fsf.org/copyleft/gpl.html GPL
* @package IMP
*/

/**
* Interface for a contacts flag image backend.
*
* @author Michael Slusarz <slusarz@horde.org>
* @category Horde
* @copyright 2013 Horde LLC
* @license http://www.horde.org/licenses/gpl GPL
* @package IMP
*/
interface IMP_Contacts_Flag_Backend
{
/**
* URL of the contact flag image.
*
* @param string $email An email address.
*
* @return array See IMP_Contacts_Image#getImage().
*/
public function flagImg($email);

}
40 changes: 40 additions & 0 deletions imp/lib/Contacts/Flag/Host.php
@@ -0,0 +1,40 @@
<?php
/**
* Copyright 2013 Horde LLC (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (GPL). If you
* did not receive this file, see http://www.horde.org/licenses/gpl.
*
* @category Horde
* @copyright 2013 Horde LLC
* @license http://www.fsf.org/copyleft/gpl.html GPL
* @package IMP
*/

/**
* Interface for a contacts flag image backend based on the sending host.
*
* @author Michael Slusarz <slusarz@horde.org>
* @category Horde
* @copyright 2013 Horde LLC
* @license http://www.horde.org/licenses/gpl GPL
* @package IMP
*/
class IMP_Contacts_Flag_Host implements IMP_Contacts_Flag_Backend
{
/**
*/
public function flagImg($email)
{
$addr = new Horde_Mail_Rfc822_Address($email);
if ($flag = Horde_Core_Ui_FlagImage::getFlagImageObByHost($addr->host)) {
return array(
'desc' => $flag['name'],
'url' => $flag['ob']->base64img
);
}

return null;
}

}

0 comments on commit a0222c5

Please sign in to comment.