Java tutorial
/* * LumaQQ - Java QQ Client * * Copyright (C) 2004 luma <stubma@163.com> * * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ package edu.tsinghua.lumaqq; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.Hashtable; import java.util.List; import java.util.Map; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.graphics.ImageData; import org.eclipse.swt.widgets.Display; import edu.tsinghua.lumaqq.events.IQQShowListener; /** * ?QQ show * * @author luma */ public class QQShowManager { // Log private static Log log = LogFactory.getLog(QQShowManager.class); protected static final String QQ_SHOW_SERVER = "http://qqshow-user.tencent.com/"; protected static final String QQ_SHOW_PATH = "/10/00/00.gif"; protected static String QQ_SHOW_CACHE_DIR; private static QQShowManager instance; // QQ private Map<Integer, Image> cache; // ? private Map<Integer, QQShowDownloadThread> threads; // QQ???QQ? private List<IQQShowListener> listeners; /** * ? * @param userDir */ private QQShowManager(String userDir) { QQ_SHOW_CACHE_DIR = userDir + "/qqshow/"; File file = new File(QQ_SHOW_CACHE_DIR); file.mkdirs(); cache = new Hashtable<Integer, Image>(); threads = new Hashtable<Integer, QQShowDownloadThread>(); listeners = new ArrayList<IQQShowListener>(); } /** * ? * @param userDir * @return */ public static QQShowManager getInstance(String userDir) { if (instance != null) instance.close(); instance = new QQShowManager(userDir); return instance; } /** * @return ??null */ public static QQShowManager getInstance() { return instance; } /** * ?manager? */ private void close() { // ?? for (Image image : cache.values()) image.dispose(); } /** * ?QQ Show?URL * * @param qqNum * ?QQ? * @return * QQ? */ protected String getQQShowUrlString(int qqNum) { return QQ_SHOW_SERVER + qqNum + QQ_SHOW_PATH; } /** * ?QQ?? * * @param qqNum * ?QQ? * @return * true? */ public boolean isCached(int qqNum) { if (cache.containsKey(qqNum)) return true; else { File file = new File(QQ_SHOW_CACHE_DIR + qqNum + ".gif"); if (file.exists()) { // Image image = createStockImage(Display.getCurrent(), file); if (image == null) return false; else { cache.put(qqNum, image); return true; } } else return false; } } /** * QQ * * @param qqNum * ?QQ? * @return * QQ */ public Image getQQShowImage(int qqNum) { return cache.get(qqNum); } /** * QQ?? * * @param qqNum * ?QQ? */ public void downloadQQShowImage(int qqNum) { if (threads.containsKey(qqNum)) return; // QQShowDownloadThread thread = new QQShowDownloadThread(qqNum); // threads.put(qqNum, thread); // QQ? if (cache.containsKey(qqNum)) { cache.remove(qqNum).dispose(); File file = new File(QQ_SHOW_CACHE_DIR + qqNum + ".gif"); file.delete(); } // ? thread.start(); } /** * ???? * * @param qqNum * ?QQ? */ protected synchronized void threadCallback(int qqNum) { // threads.remove(qqNum); // ? fireQQShowEvent(qqNum); } /** * ?QQ * * @param qqNum * ?QQ?QQ? */ private void fireQQShowEvent(int qqNum) { for (IQQShowListener listener : listeners) listener.qqShowDownloaded(qqNum); } /** * @param display * the display * @param path * the relative path to the icon * * Image */ private Image createStockImage(Display display, File file) { InputStream stream = null; try { stream = new FileInputStream(file); if (stream != null) { ImageData imageData = new ImageData(stream); if (imageData != null) { ImageData mask = imageData.getTransparencyMask(); return new Image(display, imageData, mask); } } } catch (Exception e) { log.error(e.getMessage()); } finally { try { if (stream != null) stream.close(); } catch (IOException e1) { log.error(e1.getMessage()); } } return null; } /** * QQ? * * @param listener * IQQShowListener */ public void addQQShowListener(IQQShowListener listener) { listeners.add(listener); } /** * QQ? * * @param listener * IQQShowListener */ public void removeQQShowListener(IQQShowListener listener) { listeners.remove(listener); } }