Refactor avatar updating.

git-svn-id: http://svn.igniterealtime.org/svn/repos/spark/trunk@9067 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
Derek DeMoro
2007-09-06 16:57:10 +00:00
committed by derek
parent a1ed95f6c9
commit f07d6bce84
3 changed files with 53 additions and 71 deletions

View File

@ -15,13 +15,14 @@ import org.jivesoftware.resource.Res;
import org.jivesoftware.resource.SparkRes; import org.jivesoftware.resource.SparkRes;
import org.jivesoftware.smack.XMPPConnection; import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.packet.Presence; import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smackx.debugger.EnhancedDebuggerWindow;
import org.jivesoftware.spark.SparkManager; import org.jivesoftware.spark.SparkManager;
import org.jivesoftware.spark.ui.ChatFrame; import org.jivesoftware.spark.ui.ChatFrame;
import org.jivesoftware.spark.util.BrowserLauncher; import org.jivesoftware.spark.util.BrowserLauncher;
import org.jivesoftware.spark.util.GraphicUtils; import org.jivesoftware.spark.util.GraphicUtils;
import org.jivesoftware.spark.util.ResourceUtils; import org.jivesoftware.spark.util.ResourceUtils;
import org.jivesoftware.spark.util.SwingTimerTask;
import org.jivesoftware.spark.util.SwingWorker; import org.jivesoftware.spark.util.SwingWorker;
import org.jivesoftware.spark.util.TaskEngine;
import org.jivesoftware.spark.util.URLFileSystem; import org.jivesoftware.spark.util.URLFileSystem;
import org.jivesoftware.spark.util.log.Log; import org.jivesoftware.spark.util.log.Log;
import org.jivesoftware.sparkimpl.plugin.alerts.InputTextAreaDialog; import org.jivesoftware.sparkimpl.plugin.alerts.InputTextAreaDialog;
@ -30,6 +31,20 @@ import org.jivesoftware.sparkimpl.settings.local.LocalPreferences;
import org.jivesoftware.sparkimpl.settings.local.SettingsManager; import org.jivesoftware.sparkimpl.settings.local.SettingsManager;
import org.jivesoftware.sparkimpl.updater.CheckUpdates; import org.jivesoftware.sparkimpl.updater.CheckUpdates;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
import java.util.TimerTask;
import javax.swing.AbstractAction; import javax.swing.AbstractAction;
import javax.swing.Action; import javax.swing.Action;
import javax.swing.ImageIcon; import javax.swing.ImageIcon;
@ -44,22 +59,6 @@ import javax.swing.JSplitPane;
import javax.swing.JTextPane; import javax.swing.JTextPane;
import javax.swing.JToolBar; import javax.swing.JToolBar;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
import java.io.File;
import java.io.IOException;
import java.sql.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;
/** /**
* The <code>MainWindow</code> class acts as both the DockableHolder and the proxy * The <code>MainWindow</code> class acts as both the DockableHolder and the proxy
* to the Workspace in Spark. * to the Workspace in Spark.
@ -465,15 +464,14 @@ public final class MainWindow extends ChatFrame implements ActionListener {
} }
}); });
int delay = 15000; // 15 sec // Execute spark update checker after one minute.
Date timeToRun = new Date(System.currentTimeMillis() + delay); final TimerTask task = new SwingTimerTask() {
Timer timer = new Timer(); public void doRun() {
timer.schedule(new TimerTask() {
public void run() {
checkForUpdates(false); checkForUpdates(false);
} }
}, timeToRun); };
TaskEngine.getInstance().schedule(task, 60000);
} }

View File

@ -18,15 +18,10 @@ import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smack.packet.Presence; import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.RosterPacket; import org.jivesoftware.smack.packet.RosterPacket;
import org.jivesoftware.smack.util.StringUtils; import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smackx.packet.VCard;
import org.jivesoftware.spark.ChatManager; import org.jivesoftware.spark.ChatManager;
import org.jivesoftware.spark.PresenceManager; import org.jivesoftware.spark.PresenceManager;
import org.jivesoftware.spark.SparkManager; import org.jivesoftware.spark.SparkManager;
import org.jivesoftware.spark.util.GraphicUtils;
import org.jivesoftware.spark.util.ModelUtil; import org.jivesoftware.spark.util.ModelUtil;
import org.jivesoftware.spark.util.TaskEngine;
import org.jivesoftware.spark.util.log.Log;
import org.jivesoftware.sparkimpl.profile.VCardManager;
import org.jivesoftware.sparkimpl.settings.local.LocalPreferences; import org.jivesoftware.sparkimpl.settings.local.LocalPreferences;
import org.jivesoftware.sparkimpl.settings.local.SettingsManager; import org.jivesoftware.sparkimpl.settings.local.SettingsManager;
@ -35,14 +30,11 @@ import java.awt.Font;
import java.awt.GridBagConstraints; import java.awt.GridBagConstraints;
import java.awt.GridBagLayout; import java.awt.GridBagLayout;
import java.awt.Insets; import java.awt.Insets;
import java.awt.image.BufferedImage;
import java.io.File; import java.io.File;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.Icon; import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel; import javax.swing.JLabel;
import javax.swing.JPanel; import javax.swing.JPanel;
import javax.swing.UIManager; import javax.swing.UIManager;
@ -257,7 +249,7 @@ public class ContactItem extends JPanel {
this.hash = hash; this.hash = hash;
if (!hashExists(hash)) { if (!hashExists(hash)) {
updateAvatar(hash); updateAvatar();
} }
} }
} }
@ -300,41 +292,9 @@ public class ContactItem extends JPanel {
/** /**
* Persists the avatar locally based on the new hash. * Persists the avatar locally based on the new hash.
*
* @param hash the new hash.
*/ */
private void updateAvatar(final String hash) { private void updateAvatar() {
Runnable updateRunnable = new Runnable() { SparkManager.getVCardManager().addToQueue(getJID());
public void run() {
contactsDir.mkdirs();
final File imageFile = new File(contactsDir, hash);
VCard vcard = SparkManager.getVCardManager().reloadVCard(getJID());
try {
byte[] bytes = vcard.getAvatar();
if (bytes != null) {
ImageIcon icon = new ImageIcon(bytes);
icon = VCardManager.scale(icon);
if (icon != null && icon.getIconWidth() != -1) {
BufferedImage image = GraphicUtils.convert(icon.getImage());
if (image == null) {
Log.warning("Unable to write out avatar for " + getJID());
}
else {
ImageIO.write(image, "PNG", imageFile);
}
}
}
}
catch (Exception e) {
Log.error("Unable to update avatar in Contact Item.", e);
}
}
};
TaskEngine.getInstance().submit(updateRunnable);
} }
public String toString() { public String toString() {

View File

@ -87,6 +87,8 @@ public class VCardManager {
private LinkedBlockingQueue<String> queue = new LinkedBlockingQueue<String>(); private LinkedBlockingQueue<String> queue = new LinkedBlockingQueue<String>();
private File contactsDir;
/** /**
* Initialize VCardManager. * Initialize VCardManager.
@ -111,6 +113,10 @@ public class VCardManager {
vcardStorageDirectory = new File(SparkManager.getUserDirectory(), "vcards"); vcardStorageDirectory = new File(SparkManager.getUserDirectory(), "vcards");
vcardStorageDirectory.mkdirs(); vcardStorageDirectory.mkdirs();
// Set the current user directory.
contactsDir = new File(SparkManager.getUserDirectory(), "contacts");
contactsDir.mkdirs();
initializeUI(); initializeUI();
// Intercept all presence packets being sent and append vcard information. // Intercept all presence packets being sent and append vcard information.
@ -178,7 +184,7 @@ public class VCardManager {
* *
* @param jid the jid to lookup. * @param jid the jid to lookup.
*/ */
private void addToQueue(String jid) { public void addToQueue(String jid) {
if (!queue.contains(jid)) { if (!queue.contains(jid)) {
queue.add(jid); queue.add(jid);
} }
@ -580,6 +586,24 @@ public class VCardManager {
byte[] bytes = vcard.getAvatar(); byte[] bytes = vcard.getAvatar();
if (bytes != null) { if (bytes != null) {
vcard.setAvatar(bytes); vcard.setAvatar(bytes);
try {
String hash = org.jivesoftware.spark.util.StringUtils.hash(bytes);
final File avatarFile = new File(contactsDir, hash);
ImageIcon icon = new ImageIcon(bytes);
icon = VCardManager.scale(icon);
if (icon != null && icon.getIconWidth() != -1) {
BufferedImage image = GraphicUtils.convert(icon.getImage());
if (image == null) {
Log.warning("Unable to write out avatar for " + jid);
}
else {
ImageIO.write(image, "PNG", avatarFile);
}
}
}
catch (Exception e) {
Log.error("Unable to update avatar in Contact Item.", e);
}
} }
// Set timestamp // Set timestamp