org.jivesoftware.smackx.privacy.packet.PrivacyItem Java Examples

The following examples show how to use org.jivesoftware.smackx.privacy.packet.PrivacyItem. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PrivacyManager.java    From Spark with Apache License 2.0 6 votes vote down vote up
public SparkPrivacyList createPrivacyList(String listName) {
    PrivacyItem item = new PrivacyItem(true,999999);
    ArrayList<PrivacyItem> items = new ArrayList<>();
    items.add(item);
    SparkPrivacyList sparklist = null;
    try {
        privacyManager.createPrivacyList(listName, items);
        privacyManager.getPrivacyList(listName).getItems().remove(item);
        sparklist = new SparkPrivacyList(privacyManager.getPrivacyList(listName));
        _privacyLists.add(sparklist);
        sparklist.addSparkPrivacyListener(_presenceHandler);
    } catch (XMPPException | SmackException | InterruptedException e) {
        Log.warning("Could not create PrivacyList "+listName, e);
    }
    
    return sparklist;
    
}
 
Example #2
Source File: PrivacyListTree.java    From Spark with Apache License 2.0 6 votes vote down vote up
/**
 * Adds a node to a parent on the jtree using the defaultModel
 * 
 * @param node
 *            the node that should be added. this is the childnode
 * @param parent
 *            the parent node, where the node should be added to
 */
private void addListNode(PrivacyTreeNode node, DefaultMutableTreeNode parent) {


    _model.insertNodeInto(node, parent, 0);

    SparkPrivacyList plist = node.getPrivacyList();
  
    PrivacyTreeNode contacts = new PrivacyTreeNode(Res.getString("privacy.node.contacts"));
    contacts.setisContactGroup(true);
    _model.insertNodeInto(contacts, node, 0);
    PrivacyTreeNode groups = new PrivacyTreeNode(Res.getString("privacy.node.groups"));
    groups.setisGroupNode(true);
    _model.insertNodeInto(groups, node, 0);

    for (PrivacyItem pI : plist.getPrivacyItems()) {
        if (pI.getType().equals(PrivacyItem.Type.jid)) {
            _model.insertNodeInto(new PrivacyTreeNode(pI), contacts, 0);
        } else if (pI.getType().equals(PrivacyItem.Type.group)) {
            _model.insertNodeInto(new PrivacyTreeNode(pI), groups, 0);
        }
    }

}
 
Example #3
Source File: PrivacyProvider.java    From Smack with Apache License 2.0 6 votes vote down vote up
private static void parseList(XmlPullParser parser, Privacy privacy) throws XmlPullParserException, IOException {
    boolean done = false;
    String listName = parser.getAttributeValue("", "name");
    ArrayList<PrivacyItem> items = new ArrayList<>();
    while (!done) {
        XmlPullParser.Event eventType = parser.next();
        if (eventType == XmlPullParser.Event.START_ELEMENT) {
            if (parser.getName().equals("item")) {
                // CHECKSTYLE:OFF
                items.add(parseItem(parser));
                // CHECKSTYLE:ON
            }
        }
        else if (eventType == XmlPullParser.Event.END_ELEMENT) {
            if (parser.getName().equals("list")) {
                done = true;
            }
        }
    }

    privacy.setPrivacyList(listName, items);
// CHECKSTYLE:OFF
}
 
Example #4
Source File: SparkPrivacyList.java    From Spark with Apache License 2.0 5 votes vote down vote up
/**
 * Search privancyItem using Type & value
 * 
 * @param value value of item
 * @return privacyItem or null if Item not found
 */
private PrivacyItem searchPrivacyItem(String value) {
    for (PrivacyItem privacyItem : getPrivacyItems()) {
        if ( privacyItem.getValue().equalsIgnoreCase(value)) {
            return privacyItem;
        }
    }
    return null; //error
}
 
Example #5
Source File: PrivacyManager.java    From Spark with Apache License 2.0 5 votes vote down vote up
private PrivacyList ensureGloballyInvisibleListExists() {
	if (!_active)
		return null; 
	
    PrivacyList list = null;
    try 
    {
        list = privacyManager.getPrivacyList(INVISIBLE_LIST_NAME);
        if (list != null)
            return list;
        
    } catch (XMPPException | SmackException | InterruptedException e1) {
        Log.debug("PrivacyManager#ensureGloballyInvisibleListExists: Could not find globally invisible list. We need to create one");
    }

    try {
        PrivacyItem item = new PrivacyItem(false, 1);
        item.setFilterPresenceOut(true);

        List<PrivacyItem> items = Arrays.asList(item);
        privacyManager.createPrivacyList(INVISIBLE_LIST_NAME, items);
        list = privacyManager.getPrivacyList(INVISIBLE_LIST_NAME);
        Log.debug("List \"" + INVISIBLE_LIST_NAME + "\" has been created ");
    } 
    catch (XMPPException | SmackException | InterruptedException e)
    {
        Log.warning("PrivacyManager#ensureGloballyInvisibleListExists: Could not create PrivacyList " + INVISIBLE_LIST_NAME, e);
    }
    
    return list;
}
 
Example #6
Source File: PrivacyListTree.java    From Spark with Apache License 2.0 5 votes vote down vote up
/**
 * If the selected Node is a GroupNode (TreeItems "Contacts" and "Groups"
 * are GroupNodes) add the specified options to the menu
 * 
 * @param menu
 *            where the method should add the remove option
 * @param node
 *            the node what is selected
 */
private void addMenuForGroupNodes(JPopupMenu menu, final PrivacyTreeNode node) {
    String showStringforAdd;
    if (node.isContactGroup()) {
        showStringforAdd = Res.getString("privacy.menu.add.contacts");
    } else {
        showStringforAdd = Res.getString("privacy.menu.add.groups");
    }
    PrivacyTreeNode listnode = (PrivacyTreeNode) _tree.getSelectionPath().getPathComponent(1);
    final SparkPrivacyList list = listnode.getPrivacyList();
    final PrivacyTreeNode parent = (PrivacyTreeNode) _tree.getSelectionPath().getPathComponent(2);
    JMenuItem addContact = new JMenuItem(showStringforAdd);
    addContact.setIcon(SparkRes.getImageIcon(SparkRes.SMALL_ADD_IMAGE));
    addContact.addActionListener( e -> {
        PrivacyAddDialogUI browser = new PrivacyAddDialogUI();
        Collection<PrivacyItem> col = browser.showRoster(_comp, node.isContactGroup() ? false : true);
        try
        {
            for (PrivacyItem pI : col) {
                final PrivacyItem clone = new PrivacyItem( pI.getType(), pI.getValue(), pI.isAllow(), list.getNewItemOrder() );
                list.addItem(clone);
                PrivacyTreeNode newChild = new PrivacyTreeNode(clone);
                _model.insertNodeInto(newChild, parent, 0);
            }
            list.save();
        }
        catch ( SmackException.NotConnectedException e1 )
        {
            Log.warning( "Unable to add item to privacy list.", e1 );
        }


    } );

    menu.add(addContact);
}
 
Example #7
Source File: SparkPrivacyList.java    From Spark with Apache License 2.0 5 votes vote down vote up
/**
 * @param item user removed from blackList
 */
private void fireItemRemoved( PrivacyItem item ) throws SmackException.NotConnectedException
{
    for ( final SparkPrivacyItemListener listener : _listeners )
    {
        try
        {
            listener.itemRemoved( item, _listName );
        }
        catch ( Exception e )
        {
            Log.error( "A SparkPrivacyItemListener (" + listener + ") threw an exception while processing a 'itemRemoved' event for: " + item + " on list: " + _listName, e );
        }
    }
}
 
Example #8
Source File: SparkPrivacyList.java    From Spark with Apache License 2.0 5 votes vote down vote up
/**
 * @param item user was added into blockList
 */
private void fireItemAdded( PrivacyItem item ) throws SmackException.NotConnectedException
{
    for ( final SparkPrivacyItemListener listener : _listeners )
    {
        try
        {
            listener.itemAdded( item, _listName );
        }
        catch ( Exception e )
        {
            Log.error( "A SparkPrivacyItemListener (" + listener + ") threw an exception while processing a 'itemAdded' event for: " + item + " on list: " + _listName, e );
        }
    }
}
 
Example #9
Source File: SparkPrivacyList.java    From Spark with Apache License 2.0 5 votes vote down vote up
/**
 * Store PrivacyList on server
 * 
 * @throws XMPPException
 */
public void save() {
    try {
        PrivacyItem item = new PrivacyItem(true,999999);
       _privacyItems.add(item);
        PrivacyManager.getInstance().getPrivacyListManager().updatePrivacyList(getListName(), _privacyItems);
        PrivacyManager.getInstance().getPrivacyListManager().getPrivacyList(_listName).getItems().remove(item);
        _privacyItems.remove(item);
    } catch (XMPPException | SmackException | InterruptedException e) {
        Log.warning("Could not save PrivacyList "+_listName);
        e.printStackTrace();
    }
}
 
Example #10
Source File: SparkPrivacyList.java    From Spark with Apache License 2.0 5 votes vote down vote up
public void removeItem(String name) throws SmackException.NotConnectedException
{
    List<PrivacyItem> tempList = new ArrayList<>( _privacyItems );
    for (PrivacyItem item: tempList)
    {
        if (item.getValue().equals(name))
        {
            _privacyItems.remove(item);
            fireItemRemoved(item);
        }
    }
}
 
Example #11
Source File: SparkPrivacyList.java    From Spark with Apache License 2.0 5 votes vote down vote up
/**
 * Search privancyItem using Type & value
 *
 * @param type type of privacy item
 * @param value value of item
 * @return privacyItem id of item into PrivacyItems or -1 on Item not found
 */
public ArrayList<PrivacyItem> searchPrivacyItems(PrivacyItem.Type type, String value) {
    ArrayList<PrivacyItem> items = new ArrayList<>();
    for (PrivacyItem privacyItem : getPrivacyItems()) {
        if ( privacyItem.getValue().equalsIgnoreCase(value) && privacyItem.getType() == type ) {
            items.add(privacyItem);
        }
    }
    return items; //error
}
 
Example #12
Source File: SparkPrivacyList.java    From Spark with Apache License 2.0 5 votes vote down vote up
/**
 * Search privancyItem using Type & value
 *
 * @param type type of privacy item
 * @param value value of item
 * @return privacyItem or null if Item not found
 */
//TODO REMOVE
@SuppressWarnings("unused")
private PrivacyItem searchPrivacyItem(PrivacyItem.Type type, String value) {
    for (PrivacyItem privacyItem : getPrivacyItems()) {
        if ( privacyItem.getValue().equalsIgnoreCase(value) && privacyItem.getType() == type ) {
            return privacyItem;
        }
    }
    return null; //error
}
 
Example #13
Source File: SparkPrivacyList.java    From Spark with Apache License 2.0 5 votes vote down vote up
/**
 * Get last PrivacyItem from list ordered by PrivacyItem.order
 *
 * @return last PrivacyItem ordered by Item order
 */
public PrivacyItem getLastItem() {
    long order = 0;
    PrivacyItem item = null;
    for (PrivacyItem privacyItem : _privacyItems) {
        if ( order < privacyItem.getOrder() ) {
            order = privacyItem.getOrder();
            item = privacyItem;
        }
    }
    return item;
}
 
Example #14
Source File: SparkPrivacyList.java    From Spark with Apache License 2.0 5 votes vote down vote up
private void loadItems() throws SmackException.NotConnectedException
{
   List<PrivacyItem> itemList = _myPrivacyList.getItems();
   
   for (PrivacyItem item: itemList)
   {
       if (item.getValue() == null || item.getType() == null)
           removeItem(item);
       else
       _privacyItems.add(item);
   }   
}
 
Example #15
Source File: PrivacyProvider.java    From Smack with Apache License 2.0 5 votes vote down vote up
private static void parseItemChildElements(XmlPullParser parser, PrivacyItem privacyItem) throws XmlPullParserException, IOException {
    final int initialDepth = parser.getDepth();

    outerloop: while (true) {
        XmlPullParser.Event eventType = parser.next();
        switch (eventType) {
        case START_ELEMENT:
            String name = parser.getName();
            switch (name) {
            case "iq":
                privacyItem.setFilterIQ(true);
                break;
            case "message":
                privacyItem.setFilterMessage(true);
                break;
            case "presence-in":
                privacyItem.setFilterPresenceIn(true);
                break;
            case "presence-out":
                privacyItem.setFilterPresenceOut(true);
                break;
            }
            break;
        case END_ELEMENT:
            if (parser.getDepth() == initialDepth) {
                break outerloop;
            }
            break;
        default:
            // Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
            break;
        }
    }
}
 
Example #16
Source File: PrivacyListManager.java    From Smack with Apache License 2.0 5 votes vote down vote up
/**
 * Answer the privacy list items under listName with the allowed and blocked permissions.
 *
 * @param listName the name of the list to get the allowed and blocked permissions.
 * @return a list of privacy items under the list listName.
 * @throws XMPPErrorException if there was an XMPP error returned.
 * @throws NoResponseException if there was no response from the remote entity.
 * @throws NotConnectedException if the XMPP connection is not connected.
 * @throws InterruptedException if the calling thread was interrupted.
 */
private List<PrivacyItem> getPrivacyListItems(String listName) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException  {
    assert StringUtils.isNotEmpty(listName);
    // The request of the list is an privacy message with an empty list
    Privacy request = new Privacy();
    request.setPrivacyList(listName, new ArrayList<PrivacyItem>());

    // Send the package to the server and get the answer
    Privacy privacyAnswer = getRequest(request);

    return privacyAnswer.getPrivacyList(listName);
}
 
Example #17
Source File: PrivacyList.java    From Smack with Apache License 2.0 5 votes vote down vote up
protected PrivacyList(boolean isActiveList, boolean isDefaultList,
        String listName, List<PrivacyItem> privacyItems) {
    super();
    this.isActiveList = isActiveList;
    this.isDefaultList = isDefaultList;
    this.listName = listName;
    this.items = privacyItems;
}
 
Example #18
Source File: PrivacyProviderTest.java    From Smack with Apache License 2.0 5 votes vote down vote up
@Test
public void parsePrivacyList() throws Exception {
    // @formatter:off
    final String xmlPrivacyList =
    "<iq type='result' id='getlist2' to='[email protected]/orchard'>"
      + "<query xmlns='jabber:iq:privacy'>"
      + "<list name='public'>"
      + "<item type='jid' "
      + "value='[email protected]' "
      + "action='deny' "
      + "order='1'/>"
      + "<item action='allow' order='2'/>"
      + "</list>"
      + "</query>"
      + "</iq>";
    // @formatter:on
    IQ iqPrivacyList = PacketParserUtils.parseStanza(xmlPrivacyList);
    assertTrue(iqPrivacyList instanceof Privacy);

    Privacy privacyList = (Privacy) iqPrivacyList;
    List<PrivacyItem> pl = privacyList.getPrivacyList("public");

    PrivacyItem first = pl.get(0);
    assertEquals(PrivacyItem.Type.jid, first.getType());
    assertEquals("[email protected]", first.getValue());
    assertEquals(false, first.isAllow());
    assertEquals(1L, first.getOrder().nativeRepresentation());

    PrivacyItem second = pl.get(1);
    assertEquals(true, second.isAllow());
    assertEquals(2L, second.getOrder().nativeRepresentation());
}
 
Example #19
Source File: PrivacyProviderTest.java    From Smack with Apache License 2.0 5 votes vote down vote up
@Test
public void parsePrivacyListWithFallThroughInclChildElements() throws Exception {
    // @formatter:off
    final String xmlPrivacyList =
    "<iq type='result' id='getlist2' to='[email protected]/orchard'>"
      + "<query xmlns='jabber:iq:privacy'>"
      + "<list name='public'>"
      + "<item type='jid' "
      + "value='[email protected]' "
      + "action='deny' "
      + "order='1'/>"
      + "<item action='allow' order='2'>"
        + "<message/>"
        + "<presence-in/>"
      + "</item>"
      + "</list>"
      + "</query>"
      + "</iq>";
    // @formatter:on
    IQ iqPrivacyList = PacketParserUtils.parseStanza(xmlPrivacyList);
    assertTrue(iqPrivacyList instanceof Privacy);

    Privacy privacyList = (Privacy) iqPrivacyList;
    List<PrivacyItem> pl = privacyList.getPrivacyList("public");

    PrivacyItem first = pl.get(0);
    assertEquals(PrivacyItem.Type.jid, first.getType());
    assertEquals("[email protected]", first.getValue());
    assertEquals(false, first.isAllow());
    assertEquals(1L, first.getOrder().nativeRepresentation());

    PrivacyItem second = pl.get(1);
    assertTrue(second.isAllow());
    assertEquals(2L, second.getOrder().nativeRepresentation());
    assertTrue(second.isFilterMessage());
    assertTrue(second.isFilterPresenceIn());
    assertFalse(second.isFilterPresenceOut());
    assertFalse(second.isFilterIQ());
}
 
Example #20
Source File: SparkPrivacyList.java    From Spark with Apache License 2.0 4 votes vote down vote up
public void addItem (PrivacyItem item) throws SmackException.NotConnectedException
{
    _privacyItems.add(item);
    fireItemAdded(item);
}
 
Example #21
Source File: SparkPrivacyList.java    From Spark with Apache License 2.0 4 votes vote down vote up
public void removeItem(PrivacyItem item) throws SmackException.NotConnectedException
{
    _privacyItems.remove(item);
    fireItemRemoved(item);
}
 
Example #22
Source File: PrivacyList.java    From Smack with Apache License 2.0 4 votes vote down vote up
public List<PrivacyItem> getItems() {
    return items;
}
 
Example #23
Source File: PrivacyListManager.java    From Smack with Apache License 2.0 3 votes vote down vote up
/**
 * Remove a privacy list.
 *
 * @param listName the list that has changed its content.
 * @throws XMPPErrorException if there was an XMPP error returned.
 * @throws NoResponseException if there was no response from the remote entity.
 * @throws NotConnectedException if the XMPP connection is not connected.
 * @throws InterruptedException if the calling thread was interrupted.
 */
public void deletePrivacyList(String listName) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
    // The request of the list is an privacy message with an empty list
    Privacy request = new Privacy();
    request.setPrivacyList(listName, new ArrayList<PrivacyItem>());

    // Send the package to the server
    setRequest(request);
}
 
Example #24
Source File: PrivacyListManager.java    From Smack with Apache License 2.0 3 votes vote down vote up
/**
 * The client has edited an existing list. It updates the server content with the resulting
 * list of privacy items. The {@link PrivacyItem} list MUST contain all elements in the
 * list (not the "delta").
 *
 * @param listName the list that has changed its content.
 * @param privacyItems a List with every privacy item in the list.
 * @throws XMPPErrorException if there was an XMPP error returned.
 * @throws NoResponseException if there was no response from the remote entity.
 * @throws NotConnectedException if the XMPP connection is not connected.
 * @throws InterruptedException if the calling thread was interrupted.
 */
public void updatePrivacyList(String listName, List<PrivacyItem> privacyItems) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException  {
    // Build the privacy package to add or update the new list
    Privacy request = new Privacy();
    request.setPrivacyList(listName, privacyItems);

    // Send the package to the server
    setRequest(request);
}
 
Example #25
Source File: PrivacyListListener.java    From Smack with Apache License 2.0 2 votes vote down vote up
/**
 * Set or update a privacy list with PrivacyItem.
 *
 * @param listName the name of the new or updated privacy list.
 * @param listItem the PrivacyItems that rules the list.
 */
void setPrivacyList(String listName, List<PrivacyItem> listItem);
 
Example #26
Source File: SparkPrivacyList.java    From Spark with Apache License 2.0 2 votes vote down vote up
/**
 * Answer the privacy list items with the allowed and blocked permissions.
 * @return list items
 */
public ArrayList<PrivacyItem> getPrivacyItems() {
    return new ArrayList<>( _privacyItems );
}
 
Example #27
Source File: SparkPrivacyItemListener.java    From Spark with Apache License 2.0 2 votes vote down vote up
/**
 * Item removed from PrivacyList
 *
 * @param item privacyItem jid
 * @param listname name of the privacy list.
 */
void itemRemoved(PrivacyItem item, String listname) throws SmackException.NotConnectedException;
 
Example #28
Source File: Privacy.java    From Yahala-Messenger with MIT License 2 votes vote down vote up
@Override
public void setPrivacyList(String listName, List<PrivacyItem> listItem) {

}
 
Example #29
Source File: PrivacyTreeNode.java    From Spark with Apache License 2.0 2 votes vote down vote up
/**
    * Creates a Node with a reference to the PrivacyItem and changes the Name
    * to item's name
    * 
    * @param item
    *            the privacyItem which should be displayed as a node
    */
   public PrivacyTreeNode(PrivacyItem item) {
_item = item;
   }
 
Example #30
Source File: PrivacyTreeNode.java    From Spark with Apache License 2.0 2 votes vote down vote up
/**
    * Get the privacyItem this node refers to
    * 
    * @return the privacyitem if node represents a privacyItem, if not null
    */
   public PrivacyItem getPrivacyItem() {
return _item;
   }