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

📄 uicontroller.java

📁 moblie syncml mail javame
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
    }

    public static void sendMessage(Message message) {
        //checks if an address has been set to 
        if (!checkValidAddresses(message)) {
            showErrorDetailedAlert(
                    Localization.getMessages().INVALID_ADDRESS_FOUND_MSG, 
                    Localization.getMessages().INVALID_ADDRESS_FOUND_LONG_MSG
                    );
            return;
        }
        
        UIController.showInboxScreen();

        /*
         * this is to avoid that a message edited and sent from within the
         * Drafts once sent remains in the Drafts while it is in the Outbox
         */
        if (message.getParent() != null) {
            if (Store.DRAFTS.equals(message.getParent().getName())) {
                try {
                    messageManager.deleteMessage(message);
                } catch (Exception ex) {
                    Log.error("sendMessage: " + ex);
                    ex.printStackTrace();
                }
            }
        }

        try {
            message.setSentDate(new Date());
            message.setReceivedDate(new Date());
            message.getFlags().setFlag(MessageFlags.OPENED, true);
            messageManager.appendMessageToFolder(message, MessageManager.OUTBOX);

        //sync(false);

        } catch (Exception ex) {
            Log.error("Error moving message to Outbox");
            ex.printStackTrace();
            showErrorAlert(Localization.getMessages().SAVE_OUTBOX_ERROR);
        } finally {
        //    destroyAddressList();
        }
        removeBBSavedConfigIfNeeded();
        sync(isFirstSync);

    }

    private static boolean checkValidAddresses(Message message) {
        Address[] to = message.getTo();
        Address[] cc = message.getCc();
        Address[] bcc = message.getBcc();
        Address[] replyTo = message.getReplyTo();
        
        for (int i=0; i<to.length; i++) {
            if (to[i].getEmail().equals(Address.INVALID_ADDRESS)) {
                return false;
            }
        }
        
        for (int i=0; i<cc.length; i++) {
            if (cc[i].getEmail().equals(Address.INVALID_ADDRESS)) {
                return false;
            }
        }
        
        for (int i=0; i<bcc.length; i++) {
            if (bcc[i].getEmail().equals(Address.INVALID_ADDRESS)) {
                return false;
            }
        }
        
        for (int i=0; i<bcc.length; i++) {
            if (replyTo[i].getEmail().equals(Address.INVALID_ADDRESS)) {
                return false;
            }
        }
        
        return true;
    }

    /**
     * Start the sync of the messages, and optionally of the contacts
     * if the parameter syncContacts is true. The sync is done using the
     * default sync type defined for the source.
     */
    public static void sync(boolean syncContacts) {
        sync(-1, syncContacts);
    }

    /**
     * Start the SyncClient to send/receive email and, if
     * syncContacts is true, also to update the address book.
     * NOTE: if the contact sync is disabled in the config,
     *       the contact sync is not started, regardless of the value of
     *       the parameter syncContacts.
     *
     * @param syncMode the mode of the sync, as defined in SyncML class.
     *                 If the value -1 is passed, the default
     *                 value defined for the source will be used.
     * @param syncContacts true if the SyncClient should sync contacts too.
     */
    public static void sync(int syncMode, boolean syncContacts) {
        if (!syncContacts) {
            sync(SyncClient.MESSAGES, syncMode);
        } else {

            int repositories[] = new int[2];
            int modes[] = new int[2];

            repositories[0] = SyncClient.MESSAGES;
            repositories[1] = SyncClient.CONTACTS;

            modes[0] = syncMode;
            modes[1] = syncMode;
            sync(repositories, modes);
        }

    }

    /**
     * sync a single repository with given mode
     * @param repository
     * @param mode
     */
    public static void sync(int repository, int mode) {

        int repositories[] = {repository};
        int modes[] = {mode};
        sync(repositories, modes);


    }

    private static void sync(int[] repositories, int[] modes) {

        //#ifdef isBlackberry
        //# if (RadioInfo.getState() == RadioInfo.STATE_OFF 
        //#        || RadioInfo.getSignalLevel() == RadioInfo.LEVEL_NO_COVERAGE) {
        //#    FunambolAlert a = new FunambolAlert(  
        //# Localization.getMessages().CONNECTION_UNAVAILABLE,
        //# Localization.getMessages().PLEASE_TURN_ON_RADIO, AlertType.ERROR );
        //#    display.setCurrent(a, display.getCurrent());
        //#    return;
        //# }
        //#endif

        SyncClient sc = SyncClient.getSyncClient();

        MailSyncListener ml = new MailSyncListener();
        sc.setMailListener(ml);

        ContactSyncListener cl = new ContactSyncListener();
        sc.setContactListener(cl);

        sc.sync(repositories, modes);
    }

    public static void sendLaterMessage(Message message) {

        if (!checkValidAddresses(message)) {
            showErrorDetailedAlert(
                    Localization.getMessages().INVALID_ADDRESS_FOUND_MSG, 
                    Localization.getMessages().INVALID_ADDRESS_FOUND_LONG_MSG
                    );
            return;
        }
        
        try {

            removeDuplicates(message);
            message.setSentDate(new Date());
            message.setReceivedDate(new Date());
            messageManager.appendMessageToFolder(message, MessageManager.OUTBOX);
        //Log.debug("Message moved to outbox: "+ message.getParent().getName());
        } catch (Exception ex) {
            Log.error("SendLaterMessage: " + ex.toString());
            ex.printStackTrace();
        }
        //updateOutboxMessageList(message);
        //updateDraftMessageList(message);
        //updateInboxMessageList(null);   //XXX why this???

        showAlert(
                Localization.getMessages().COMPOSE_MESSAGE_FORM_SAVED_TO_OUTBOX,
                getInboxMessageList());

    // destroyAddressList();
    }

    public static void saveMessage(Message message) {
        try {
            removeDuplicates(message);
            messageManager.appendMessageToFolder(message, MessageManager.DRAFTS);
            messageManager.updateMessageFlag(message, MessageFlags.DRAFT, true);
            //message.getFlags().setFlag(MessageFlags.DRAFT, true);
            //updateDraftMessageList(message);
            //updateInboxMessageList(null);
            showAlert(
                    Localization.getMessages().COMPOSE_MESSAGE_FORM_SAVED_TO_DRAFT,
                    getInboxMessageList());
        //destroyAddressList();
        //Log.debug("message saved to draft");
        } catch (Exception ex) {
            Log.error("UIC.savemessage: " + ex.toString());
            showErrorAlert(
                    Localization.getMessages().UNABLE_TO_SAVE_MESSAGE_TO_DRAFT,
                    getInboxMessageList());

        //  destroyAddressList();
        }
    }

    private static void removeDuplicates(final Message message) throws MailException, ConfigException {

        /*
         * this is to avoid that a message in the Drafts once edited and
         * saved to be sent later (i.e. stored in the Outbox) remains in the
         * Drafts while it is in the Outbox
         */
        if (message.getParent() != null) {
            if ("/Outbox".equals(message.getParent().getFullName()) ||
                    "/Drafts".equals(message.getParent().getFullName())) {
                messageManager.deleteMessage(message);
            }
        }
    }

    public static String getMyAddress() {
        mailClientConfig = getConfig();
        return (mailClientConfig.getMailAccount().getAddress());
    }

    /**
     * build the reply message of a given message.
     *
     */
//TODO: check the cc field, it seems not to work.
    public static void replyAll(Message message) {
        Message m = new Message();
        try {
            m.setCc(message.getCc());
            m.setTo(message.getReplyTo());
            Address[] to = message.getTo();
            for (int i = 0; i < to.length; i++) {
                if (!StringUtil.equalsIgnoreCase(to[i].getEmail(), getMyAddress())) {
                    to[i].setType(Address.CC);
                    m.addRecipient(to[i]);
                }
            }
            m.setSubject(RE + message.getSubject());
            //setRepliedFlag(message);
            setDelayedFlag(message, MessageFlags.ANSWERED);
            updateComposeMessageScreen(m, getReplyText(message));
            // getContactList().emptySearchString();
            showComposeMessageScreen(display.getCurrent(), getComposeMessageScreen().body);
        } catch (MailException ex) {
            Log.error("Error in reply all message function: " + ex.getMessage());
            ex.printStackTrace();
        }
    }

    /**
     * Get the complete message (up to the maxObjSize).
     * This method relied on SyncClient.syncSingleMessage which is non blocking,
     * therefore this method is non blocking as well. The message can be got
     * by using a SingleMessageListener.
     */
    public static void getFullMessage(Message message) {

        Log.info("Get Full Message");
        try {
            String folder = message.getParent().getName();
            String luid = message.getKey();
            MailSyncSingleMessageListener lis =
                    new MailSyncSingleMessageListener(viewMessageScreen);
            SyncClient.getSyncClient().setSingleMessageListener(lis);
            SyncClient.getSyncClient().syncSingleMessage(folder, luid, false);
        } catch (ConfigException e) {
            Log.error("getFullMessage: " + e.toString());
            e.printStackTrace();
        } catch (MailException e) {
            Log.error("getFullMessage: " + e.toString());
            e.printStackTrace();
        }
    }

    private static String getReplyText(Message message) {
        StringBuffer sb = new StringBuffer();
        sb.append("\n\n");

        //added a delimiter
        sb.append("----\n");

        try {
            //TODO: use right timiezone offset here
            Date dateObj = message.getSentDate();
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(dateObj);
            calendar.setTimeZone(TimeZone.getDefault());

            // this is only for visualization purposes
            //TODO: check if the variables in formatLocalTime are really used
            String dateTime =
                    MailDateFormatter.getReplyDateString(
                    MailDateFormatter.formatLocalTime(calendar.getTime()));


            String date = dateTime.substring(0, dateTime.lastIndexOf(' '));
            String time = dateTime.substring(dateTime.lastIndexOf(' '));

            sb.append(Localization.getMessages().STRING_ON).
                    append(" ").append(date).append(" ").append(Localization.getMessages().STRING_AT).append(time).append("m, ").
                    append((message.getFrom() == null) ? "" : message.getFrom().getVisibleName()).
                    append(" ").append(Localization.getMessages().REPLY_WROTE).
                    append("\n").append(message.getTextContent());

        } catch (MailException ex) {
            ex.printStackTrace();
            Log.error("getReplyText() mailexception ");
        }

        //Log.debug("replty text: \n----------------------\n" + sb.toString() + "\n-------------------------");
        return sb.toString();

    }

    private static FunambolAlert getFunambolAlert(String message,
            AlertType alertType, boolean autoDismiss) {

        FunambolAlert messageAlert = new FunambolAlert(
                Localization.getMessages().ALERT_TITLE,
                message,
                alertType);

        if (autoDismiss) {
            messageAlert.setTimeout(FunambolAlert.DEFAULT_TIMEOUT);
        } else {
            messageAlert.setTimeout(Alert.FOREVER);
        }

        return messageAlert;

    }

    private static FunambolAlert getFunambolDetailedAlert(String message, String longMessage,
            AlertType alertType, boolean autoDismiss) {

        FunambolAlert messageAlert = new FunambolAlert(
                Localization.getMessages().ALERT_TITLE,
                message,
                longMessage,
                alertType);

        if (autoDi

⌨️ 快捷键说明

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