/*
* Gamers Own Instant Messenger
* Copyright (C) 2005-2006 Herbert Poul (kahless@sphene.net)
* http://goim.sphene.net
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
package net.sphene.goim.rcp.extensionpoints;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import net.sphene.goim.rcp.GOIMPlugin;
import net.sphene.goim.rcp.preferences.PreferenceConstants;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.IExtensionRegistry;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.osgi.framework.Bundle;
import org.xmlpull.mxp1.MXParser;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
public class IconSetExtensionPoint {
private static final String EXTENSION_POINT = "net.sphene.goim.rcp.icons";
public static final String ICONSET_TYPE_EMOTICONS = "emoticons";
public static final String ICONSET_TYPE_STATUSICONS = "statusicons";
private static final String ICONDEF_ATTRIBUTE = "icondef";
private static final String ID_ATTRIBUTE = "id";
static List<IconSetExtension> extensions;
protected static Logger logger = Logger.getLogger(IconSetExtensionPoint.class.getName());
public static List<IconSetExtension> getIconSetExtensions() {
if(extensions != null) return extensions;
extensions = new ArrayList<IconSetExtension>();
IExtensionRegistry registry = Platform.getExtensionRegistry();
IExtensionPoint extensionPoint = registry.getExtensionPoint(EXTENSION_POINT);
for(IConfigurationElement element : extensionPoint.getConfigurationElements()) {
extensions.add(new IconSetExtension(element));
}
return extensions;
}
public static IconSetExtension getActiveIconSet(String type) {
//IconSetExtension extension = null;
String id = null;
if(type.equals(ICONSET_TYPE_STATUSICONS)) // TODO: NOT YET IMPLEMENTED
return null;
else
id = GOIMPlugin.getDefault().getMyPreferenceStore().getString(PreferenceConstants.P_EMOTICONSET);
getIconSetExtensions();
for(IconSetExtension ex : extensions) {
if(ex.id.equals(id))
return ex;
}
return null;
}
public static class IconSetExtension {
public String id;
public String type;
public String icondef;
public String rootpath;
public String name;
public String version;
public String author;
public String description;
public String creation;
List<IconDef> icons = new ArrayList<IconDef>();
public List<IconDef> getIcons() { return icons; }
public IconSetExtension(IConfigurationElement element) {
type = element.getName();
id = element.getAttribute(ID_ATTRIBUTE);
icondef = element.getAttribute(ICONDEF_ATTRIBUTE);
MXParser parser = new MXParser();
Bundle bundle = Platform.getBundle(element.getDeclaringExtension().getNamespaceIdentifier());
URL icondefurl = FileLocator.find(bundle,new Path(icondef),null);
IPath path2 = new Path(icondef).removeLastSegments(1);
URL path2url = FileLocator.find(bundle,path2,null);
String path = icondefurl.getPath();
logger.finer("path: " + path);
logger.finer("path2url: " + path2url.toString());
try {
URL url = FileLocator.resolve(path2url);
logger.finer("path2url-resolved: " + url.toString());
rootpath = url.toString();
} catch (IOException e1) {
e1.printStackTrace();
}
try {
parser.setInput(icondefurl.openStream(),"UTF-8");
parseIconDef(parser);
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
protected void parseIconDef(XmlPullParser parser) {
try {
while(parser.next() != XmlPullParser.END_DOCUMENT) {
if(parser.getEventType() != XmlPullParser.START_TAG) continue;
String tagname = parser.getName();
if(tagname.equals("meta"))
parseMeta(parser);
else if(tagname.equals("icon"))
parseIcon(parser);
}
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
protected void parseMeta(XmlPullParser parser) {
try {
while(parser.nextTag() != XmlPullParser.END_TAG || !parser.getName().equals("meta")) {
String tag = parser.getName();
if(tag.equals("name"))
name = parser.nextText();
else if(tag.equals("version"))
version = parser.nextText();
else if(tag.equals("author"))
author = parser.nextText();
else if(tag.equals("description"))
description = parser.nextText();
else if(tag.equals("creation"))
creation = parser.nextText();
}
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
protected void parseIcon(XmlPullParser parser) {
IconDef def = new IconDef();
try {
logger.finest("Parsing Icon ..");
while(parser.next() != XmlPullParser.END_TAG || !parser.getName().equals("icon")) {
if(parser.getEventType() == XmlPullParser.START_TAG) {
String tag = parser.getName();
if(tag.equals("text"))
def.text.add(parser.nextText());
else if(tag.equals("object") && parser.getAttributeValue("","mime").startsWith("image")) {
def.image = parser.nextText();
}
def.rootpath = rootpath;
}
}
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if(def.text.size() > 0 && def.image != null)
icons.add(def);
}
public static class IconDef {
public String rootpath;
public List<String> text = new ArrayList<String>();
public String image;
private Image imageObj;
private String fullPath;
public Image getImage() {
if(imageObj != null) return imageObj;
try {
imageObj = new Image(Display.getCurrent(),new URL(rootpath + "/" + image).openStream());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return imageObj;
}
public String getFullPath() {
if(fullPath != null) return fullPath;
return fullPath = rootpath + "/" + image;
}
}
}
}