use of org.jivesoftware.smack.roster.RosterEntry in project Spark by igniterealtime.
the class PrivacyAddDialogUI method createList.
private void createList() {
_userList.clear();
final Roster roster = Roster.getInstanceFor(SparkManager.getConnection());
if (_showGroups) {
for (RosterGroup group : roster.getGroups()) {
_showOffCheckbox.setVisible(false);
ContactItem item = new ContactItem(group.getName(), null, group.getName());
_userList.add(item);
}
} else {
for (RosterEntry entry : roster.getEntries()) {
Presence presence = PresenceManager.getPresence(entry.getUser());
if (presence.isAvailable()) {
ContactItem item = new ContactItem(entry.getName(), null, entry.getUser());
item.setPresence(presence);
_userList.add(item);
} else if (_showOffCheckbox.isSelected()) {
ContactItem item = new ContactItem(entry.getName(), null, entry.getUser());
item.setPresence(presence);
_userList.add(item);
}
}
}
Collections.sort(_userList, itemComparator);
model.clear();
for (ContactItem item : _userList) {
model.addElement(item);
}
}
use of org.jivesoftware.smack.roster.RosterEntry in project Spark by igniterealtime.
the class RosterPickList method showRoster.
/**
* Displays a pick list of available users within their roster.
*
* @param parent the parent container.
* @return all items choosen in the pick list.
*/
public Collection<String> showRoster(JDialog parent) {
final List<ContactItem> userList = new ArrayList<>();
// Populate Invite Panel with Available users.
final Roster roster = Roster.getInstanceFor(SparkManager.getConnection());
for (RosterEntry entry : roster.getEntries()) {
Presence presence = PresenceManager.getPresence(entry.getUser());
if (presence.isAvailable()) {
ContactItem item = UIComponentRegistry.createContactItem(entry.getName(), null, entry.getUser());
item.setPresence(presence);
userList.add(item);
}
}
// Sort Users
Collections.sort(userList, itemComparator);
for (ContactItem item : userList) {
model.addElement(item);
}
final JOptionPane pane;
TitlePanel titlePanel;
// Create the title panel for this dialog
titlePanel = new TitlePanel(Res.getString("title.roster"), Res.getString("message.select.one.or.more"), SparkRes.getImageIcon(SparkRes.BLANK_IMAGE), true);
// Construct main panel w/ layout.
final JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());
mainPanel.add(titlePanel, BorderLayout.NORTH);
// The user should only be able to close this dialog.
Object[] options = { Res.getString("ok"), Res.getString("cancel") };
pane = new JOptionPane(this, JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null, options, options[0]);
mainPanel.add(pane, BorderLayout.CENTER);
final JOptionPane p = new JOptionPane();
final JDialog dlg = p.createDialog(parent, Res.getString("title.roster"));
dlg.setModal(true);
dlg.pack();
dlg.setSize(350, 450);
dlg.setResizable(true);
dlg.setContentPane(mainPanel);
dlg.setLocationRelativeTo(parent);
PropertyChangeListener changeListener = e -> {
String value = (String) pane.getValue();
if (Res.getString("cancel").equals(value)) {
rosterList.clearSelection();
pane.setValue(JOptionPane.UNINITIALIZED_VALUE);
dlg.dispose();
} else if (Res.getString("ok").equals(value)) {
pane.setValue(JOptionPane.UNINITIALIZED_VALUE);
dlg.dispose();
}
};
pane.addPropertyChangeListener(changeListener);
dlg.setVisible(true);
dlg.toFront();
dlg.requestFocus();
List<String> selectedContacts = new ArrayList<>();
Object[] values = rosterList.getSelectedValues();
final int no = values != null ? values.length : 0;
for (int i = 0; i < no; i++) {
try {
ContactItem item = (ContactItem) values[i];
selectedContacts.add(item.getJID());
} catch (NullPointerException e) {
Log.error(e);
}
}
return selectedContacts;
}
use of org.jivesoftware.smack.roster.RosterEntry in project Spark by igniterealtime.
the class ContactList method showPopup.
/**
* Shows popup for right-clicking of ContactItem.
*
* @param e the MouseEvent
* @param item the ContactItem
* @param component the owning component
*/
public void showPopup(Component component, MouseEvent e, final ContactItem item) {
if (item.getJID() == null) {
return;
}
activeItem = item;
final JPopupMenu popup = new JPopupMenu();
// Add Start Chat Menu
popup.add(chatMenu);
// Add Send File Action
Action sendAction = new AbstractAction() {
private static final long serialVersionUID = -7519717310558205566L;
public void actionPerformed(ActionEvent actionEvent) {
SparkManager.getTransferManager().sendFileTo(item);
}
};
// See if we should disable the option to transfer files and images
if (!Default.getBoolean("DISABLE_FILE_XFER") && Enterprise.containsFeature(Enterprise.FILE_TRANSFER_FEATURE)) {
sendAction.putValue(Action.SMALL_ICON, SparkRes.getImageIcon(SparkRes.DOCUMENT_16x16));
sendAction.putValue(Action.NAME, Res.getString("menuitem.send.a.file"));
if (item.getPresence() != null)
popup.add(sendAction);
}
popup.addSeparator();
String groupName = item.getGroupName();
ContactGroup contactGroup = getContactGroup(groupName);
// Only show "Remove Contact From Group" if the user belongs to more than one group.
if (!contactGroup.isSharedGroup() && !contactGroup.isOfflineGroup() && contactGroup != getUnfiledGroup()) {
Roster roster = Roster.getInstanceFor(SparkManager.getConnection());
RosterEntry entry = roster.getEntry(item.getJID());
if (entry != null) {
int groupCount = entry.getGroups().size();
// which would put them into the unfiled group.
if (groupCount > 1) {
popup.add(removeContactFromGroupMenu);
}
}
}
// Define remove entry action
Action removeAction = new AbstractAction() {
private static final long serialVersionUID = -2565914214685979320L;
public void actionPerformed(ActionEvent e) {
removeContactFromRoster(item);
}
};
removeAction.putValue(Action.NAME, Res.getString("menuitem.remove.from.roster"));
removeAction.putValue(Action.SMALL_ICON, SparkRes.getImageIcon(SparkRes.SMALL_CIRCLE_DELETE));
// Check if user is in shared group.
boolean isInSharedGroup = false;
for (ContactGroup cGroup : new ArrayList<>(getContactGroups())) {
if (cGroup.isSharedGroup()) {
ContactItem it = cGroup.getContactItemByJID(item.getJID());
if (it != null) {
isInSharedGroup = true;
}
}
}
// See if we should disable the option to remove a contact
if (!Default.getBoolean("DISABLE_REMOVALS") && Enterprise.containsFeature(Enterprise.REMOVALS_FEATURE)) {
if (!contactGroup.isSharedGroup() && !isInSharedGroup)
popup.add(removeAction);
}
// See if we should disable the option to rename a contact
if (!Default.getBoolean("DISABLE_RENAMES") && Enterprise.containsFeature(Enterprise.RENAMES_FEATURE))
popup.add(renameMenu);
Action viewProfile = new AbstractAction() {
private static final long serialVersionUID = -2562731455090634805L;
public void actionPerformed(ActionEvent e) {
VCardManager vcardSupport = SparkManager.getVCardManager();
String jid = item.getJID();
vcardSupport.viewProfile(jid, SparkManager.getWorkspace());
}
};
viewProfile.putValue(Action.SMALL_ICON, SparkRes.getImageIcon(SparkRes.PROFILE_IMAGE_16x16));
viewProfile.putValue(Action.NAME, Res.getString("menuitem.view.profile"));
popup.add(viewProfile);
popup.addSeparator();
Action lastActivityAction = new AbstractAction() {
private static final long serialVersionUID = -4884230635430933060L;
public void actionPerformed(ActionEvent actionEvent) {
try {
String client = "";
if (item.getPresence().getType() != Presence.Type.unavailable) {
client = item.getPresence().getFrom();
if ((client != null) && (client.lastIndexOf("/") != -1)) {
client = client.substring(client.lastIndexOf("/"));
} else
client = "/";
}
LastActivity activity = LastActivityManager.getInstanceFor(SparkManager.getConnection()).getLastActivity(item.getJID() + client);
long idleTime = (activity.getIdleTime() * 1000);
String time = ModelUtil.getTimeFromLong(idleTime);
UIManager.put("OptionPane.okButtonText", Res.getString("ok"));
JOptionPane.showMessageDialog(getGUI(), Res.getString("message.idle.for", time), Res.getString("title.last.activity"), JOptionPane.INFORMATION_MESSAGE);
} catch (Exception e1) {
UIManager.put("OptionPane.okButtonText", Res.getString("ok"));
JOptionPane.showMessageDialog(getGUI(), Res.getString("message.unable.to.retrieve.last.activity", item.getJID()), Res.getString("title.error"), JOptionPane.ERROR_MESSAGE);
}
}
};
lastActivityAction.putValue(Action.NAME, Res.getString("menuitem.view.last.activity"));
lastActivityAction.putValue(Action.SMALL_ICON, SparkRes.getImageIcon(SparkRes.SMALL_USER1_STOPWATCH));
if (contactGroup == offlineGroup || item.getPresence().isAway() || (item.getPresence().getType() == Presence.Type.unavailable) || (item.getPresence().getType() == null)) {
popup.add(lastActivityAction);
}
Action subscribeAction = new AbstractAction() {
private static final long serialVersionUID = -7754905015338902300L;
public void actionPerformed(ActionEvent e) {
String jid = item.getJID();
Presence response = new Presence(Presence.Type.subscribe);
response.setTo(jid);
try {
SparkManager.getConnection().sendStanza(response);
} catch (SmackException.NotConnectedException e1) {
Log.warning("Unable to send subscribe to " + jid, e1);
}
}
};
subscribeAction.putValue(Action.SMALL_ICON, SparkRes.getImageIcon(SparkRes.SMALL_USER1_INFORMATION));
subscribeAction.putValue(Action.NAME, Res.getString("menuitem.subscribe.to"));
Roster roster = Roster.getInstanceFor(SparkManager.getConnection());
RosterEntry entry = roster.getEntry(item.getJID());
if (entry != null && entry.getType() == RosterPacket.ItemType.from) {
popup.add(subscribeAction);
} else if (entry != null && entry.getType() != RosterPacket.ItemType.both && entry.getStatus() == RosterPacket.ItemStatus.SUBSCRIPTION_PENDING) {
popup.add(subscribeAction);
}
// Fire Context Menu Listener
fireContextMenuListenerPopup(popup, item);
ContactGroup group = getContactGroup(item.getGroupName());
if (component == null) {
popup.show(group.getList(), e.getX(), e.getY());
} else {
popup.show(component, e.getX(), e.getY());
popup.requestFocus();
}
}
use of org.jivesoftware.smack.roster.RosterEntry in project Spark by igniterealtime.
the class ContactList method updateUserPresence.
/**
* Updates the users presence.
*
* @param presence the user to update.
* @throws Exception if there is a problem while updating the user's presence.
*/
private synchronized void updateUserPresence(Presence presence) throws Exception {
if (presence.getError() != null) {
// We ignore this.
return;
}
final Roster roster = Roster.getInstanceFor(SparkManager.getConnection());
final String bareJID = XmppStringUtils.parseBareJid(presence.getFrom());
RosterEntry entry = roster.getEntry(bareJID);
boolean isPending = entry != null && (entry.getType() == RosterPacket.ItemType.none || entry.getType() == RosterPacket.ItemType.from) && RosterPacket.ItemStatus.SUBSCRIPTION_PENDING == entry.getStatus();
if (presence.getType() == Presence.Type.available && offlineGroup.getContactItemByJID(bareJID) != null || (presence.getFrom().contains("workgroup."))) {
changeOfflineToOnline(bareJID, entry, presence);
} else if (presence.getType() == Presence.Type.available) {
updateContactItemsPresence(presence, entry, bareJID);
} else if (presence.getType() == Presence.Type.unavailable && !isPending) {
// If not available, move to offline group.
Presence rosterPresence = PresenceManager.getPresence(bareJID);
if (!rosterPresence.isAvailable()) {
moveToOfflineGroup(presence, bareJID);
} else {
updateContactItemsPresence(rosterPresence, entry, bareJID);
}
}
}
use of org.jivesoftware.smack.roster.RosterEntry in project Spark by igniterealtime.
the class ContactList method actionPerformed.
public void actionPerformed(ActionEvent e) {
if (e.getSource() == addingGroupButton) {
new RosterDialog().showRosterDialog();
} else if (e.getSource() == chatMenu) {
if (activeItem != null) {
SparkManager.getChatManager().activateChat(activeItem.getJID(), activeItem.getDisplayName());
}
} else if (e.getSource() == addContactMenu) {
RosterDialog rosterDialog = new RosterDialog();
if (activeGroup != null) {
rosterDialog.setDefaultGroup(activeGroup);
}
rosterDialog.showRosterDialog();
} else if (e.getSource() == removeContactFromGroupMenu) {
if (activeItem != null) {
removeContactFromGroup(activeItem);
}
} else if (e.getSource() == renameMenu) {
if (activeItem == null) {
return;
}
String oldAlias = activeItem.getAlias();
String newAlias = JOptionPane.showInputDialog(this, Res.getString("label.rename.to") + ":", oldAlias);
// if user removed alias, output will be an empty String.
if (newAlias != null) {
if (!ModelUtil.hasLength(newAlias)) {
// allows you to remove an alias.
newAlias = null;
}
String address = activeItem.getJID();
ContactGroup contactGroup = getContactGroup(activeItem.getGroupName());
ContactItem contactItem = contactGroup.getContactItemByDisplayName(activeItem.getDisplayName());
contactItem.setAlias(newAlias);
final Roster roster = Roster.getInstanceFor(SparkManager.getConnection());
RosterEntry entry = roster.getEntry(address);
try {
entry.setName(newAlias);
final String user = XmppStringUtils.parseBareJid(address);
for (ContactGroup cg : groupList) {
ContactItem ci = cg.getContactItemByJID(user);
if (ci != null) {
ci.setAlias(newAlias);
}
}
} catch (XMPPException.XMPPErrorException | SmackException.NotConnectedException | SmackException.NoResponseException e1) {
Log.warning("Unable to set new alias '" + newAlias + "' for roster entry " + address, e1);
}
}
}
}
Aggregations