⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rmscontactmanager.java

📁 The Funambol J2ME Mail Client aims to be a light, easy to use, free email client for J2ME devices.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        }
    }
    
    /**
     * Filter class to be passed in order to filter RecordStore records
     * during search operations
     */
    private class ContactFilter implements ObjectFilter {
        byte[] filter;
        boolean result = false;
        /**
         * Contructor
         * @param newFilter is the filter string to be found
         */
        public ContactFilter(byte[] newFilter) {
            this.filter = newFilter;
        }
        
        /**
         * RecordFilter interface method implementation
         */
        public boolean matches(byte[] record) {
            if (this.filter==null) {
                return true;
            } else {
                ByteArrayInputStream bais= new ByteArrayInputStream(record);
                DataInputStream din = new DataInputStream(bais);
                boolean match = false;
                try {
                    //Add Visible name filtering
                    boolean existsNickName = din.readBoolean();
                    if (existsNickName) {
                        String nickName = din.readUTF();
                        match = compareString(nickName.getBytes());
                        if (match==true) {
                            return match;
                        }
                    }
                    boolean existsFirstName = din.readBoolean();
                    if (existsFirstName) {
                        String firstName = din.readUTF();
                        match = compareString(firstName.getBytes());
                        if (match==true) {
                            return match;
                        }
                    }
                    boolean existsLastName = din.readBoolean();
                    if (existsLastName) {
                        String lastName = din.readUTF();
                        match = compareString(lastName.getBytes());
                        if (match==true) {
                            return match;
                        }
                    }
                    //put here decisions about email visiblename
                    if (((!match)||((!existsFirstName)&&(!existsLastName)))&&
                            din.readBoolean()) {
                        String email = din.readUTF();
                        match = compareString(email.getBytes());
                        if (match==true) {
                            return match;
                        }
                    }
                } catch (IOException ex) {
                    ex.printStackTrace();
                    Log.info(this, "Error Reading contact record");
                }
                return match;
            }
        }
        
        /**
         * Compares the filter given in the constructor with the fields
         * retrieved in the record.
         * @param field is the byte array related to the given String filter
         */
        private boolean compareString(byte[] field) {
            byte[] tmp = new byte[filter.length];
            int initIndex = 0;
            String filterString = new String(filter);
            String fieldString = new String(field);
            Log.trace("Field: " + fieldString);
            Log.trace("Filter: " + filterString);
            
            if (fieldString.length()==filterString.length()) {
                Log.trace("Field and filter have the same length");
                Log.trace("Field: " + fieldString);
                Log.trace("Filter: " + filterString);
                
                if ((filterString.toLowerCase()).
                        equals(fieldString.toLowerCase())
                        ) {
                    Log.trace("Field Found");
                    return true;
                }
                Log.trace("Field and filter aren't equals");
                
            }
            while (initIndex <= field.length - filter.length) {
                for (int i=initIndex; i < initIndex + filter.length ; i++) {
                    tmp[i-initIndex] = field[i];
                }
                initIndex++;
                Log.trace((new String(tmp)).toLowerCase());
                Log.trace((new String(filter)).toLowerCase());
                
                if (((new String(tmp)).toLowerCase()).
                        equals((new String(filter)).toLowerCase())
                        ) {
                    return true;
                }
            }
            return false;
        }
    }
    
    /**
     * Store a new record in the Recordstore
     * @param contact the object of type Contact that will be saved
     * @returns the id of the new record
     */
    public int addContact(Contact contact) throws ContactManagerException {
        return addContact(contact, false);
    }
    /**
     * Store a new record in the Recordstore
     * @param contact the object of type Contact that will be saved
     * @returns the id of the new record
     */
    public int addContact(Contact contact, boolean enabledContactSearch)
    throws ContactManagerException {
        if (contactStore.size() >= defaultContactNumber && defaultContactNumber!=-1) {
            Log.error("Contact List is full. ContactStore size: " + 
                    contactStore.size() + " - defaultContactNumber: " + defaultContactNumber);
            throw new ContactListFullException("Contact List Full");
        }
        SerializableContact sc =
                new SerializableContact(
                        contact.getNickName(),
                        contact.getFirstName(),
                        contact.getLastName(),
                        contact.getDefaultEmail(),
                        contact.getEmail_2(),
                        contact.getEmail_3(),
                        contact.getHomePhone(),
                        contact.getJobPhone(),
                        contact.getMobilePhone());
       
/**        existentContact=null;
        if (enabledContactSearch) {
            this.existentContact = exists(sc.getDefaultEmail());
        } 
     
        if (existentContact!=null) {
            throw new AlreadyExistentContactException(
                    "AlreadyExistent Contact - Cannot add ", existentContact);
        } else { */
            try {
                int id = contactStore.store(sc);
                //FIX-ME:
                contact.setContactId(id);
                if (enabledContactSearch) {
                    addSyncItem(String.valueOf(contact.getContactId()), 'N');
                }
            } catch (RecordStoreException ex) {
                ex.printStackTrace();

                Log.error("[addContact] RecordStoreException: " + ex.toString());

                throw new ContactManagerException("Contact manager error: " +
                        " unable to add contact");
            } catch (IOException ex) {

                Log.error("[addContact] IOException: " + ex.toString());

                ex.printStackTrace();
            }
            
            return contact.getContactId();
        //}
        
    }
    
    /**
     * Scan contact store to understand if the given contact already exists
     * @param contactField is the given SerializableContact field
     * @return boolean true if the object exists
     */
    private Contact exists(String contactField) throws ContactManagerException {
        Enumeration re = searchContacts(contactField);
        
        while (re.hasMoreElements()) {
            Contact c = (Contact) re.nextElement();
            //Log.debug(c.getDefaultEmail()+"\n"+contactField);
            if (c.getDefaultEmail().equals(contactField)) {
                return c;
            }
        }
        return null;
    }
    
    
    public void removeSyncItem(String key) {
        syncItemList.removeItemInfo(key);
    }
    
    public void addSyncItem(String key, char status){
        syncItemList.addItemInfo(key, status);
    }
    
    
    public Enumeration getSyncItems(char state) {
        return syncItemList.getItemInfos(state);
    }
    
    public void sync() throws ConfigException, SyncException {
        Account a = ConfigManager.getConfig().getMailAccount();
        SyncClient client = SyncClient.getSyncClient(a);
        SyncSource ss = new ContactSyncSource(
                                client.loadSourceConfig(
                                    SourceConfig.CONTACT,
                                    SourceConfig.VCARD_TYPE,
                                    a.getPimRemoteURI()));

        Log.debug(a.toString());
        
        try {
            client.sync(ss);
        } finally {
            client.dispose();
            Log.debug("Client ready for synchronization");
        }
        
    }
    
    public void sync(int mode) throws ConfigException,
                                        ContactManagerException, SyncException {
        Account a = ConfigManager.getConfig().getMailAccount();
        SyncClient client = SyncClient.getSyncClient(a);
        SyncSource ss = new ContactSyncSource(client.loadSourceConfig(
                                            SourceConfig.CONTACT,
                                            SourceConfig.VCARD_TYPE,
                                            a.getPimRemoteURI()));
        Log.debug(a.toString());
        try {
            client.sync(ss, mode);
        } finally {
            client.dispose();
        }
        
    }
    
    public PimItem getSyncItemInfo(String key) {
        return (PimItem) syncItemList.getItemInfo(key);
    }
    
    public void resetContactList() {
        try {
            contactStore.close();
            RecordStore.deleteRecordStore(CONTACT_STORE);
            contactStore.create(CONTACT_STORE);
            contactStore.open(CONTACT_STORE);
            
            syncItemList.resetPimItemQueue();
        } catch (Exception ex) {
            Log.error(this, "resetContactList() exception: " + ex.toString());
            ex.printStackTrace();
            
        }
    }
    
    private void removeSyncItem(char state) {
        PimItemEnumeration itemToRemove = (PimItemEnumeration) getSyncItems(state);
        while (itemToRemove.hasMoreElements()) {
            PimItem nextItem = (PimItem) itemToRemove.nextElement();
            syncItemList.removeItemInfo(nextItem.getKey());
        }
        itemToRemove=null;
    }

    public void setDefaultContactNumber(int newDefaultContactNumber) {
        this.defaultContactNumber = newDefaultContactNumber;
    }

    public int getDefaultContactNumber() {
        return this.defaultContactNumber;
    }

    public int getContactCount() {
        return contactStore.size();
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -