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

📄 sync.java

📁 手机邮箱撒的方式方式方式的
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            backupSettings(main.getSettings());
        } catch (MyException ex) {
            ex.printStackTrace();
        }
        
        //close OutputStream
        //#ifdef MUJMAIL_FS
        if (syncMode == LOCAL) {
            try {
                if (DEBUG) { System.out.println("DEBUG Sync.backupData -CLOSING OUTPUTSTREAM"); }
                dos.close();
                conn.close();
                if (DEBUG) { System.out.println("DEBUG Sync.backupData - OUTPUTSTREAM CLOSED"); }
            } catch (Exception e) {
                MujMail.mujmail.alert.setAlert(Lang.get(Lang.EXP_FS_CLOSE_FILE), AlertType.ERROR);
            }
        } else if (syncMode == REMOTE) {
        //#endif
            //String tag, reply;
            try {
                execute("Done", false);
                connection.getLine();
            } catch (MyException ex) {
            	//close(null, null);
                ex.printStackTrace();
            }
            //TODO Error handling
        //#ifdef MUJMAIL_FS
        }
        //#endif
    }
    
    /**
     * This function restores mujMail user data from given location.
     *
     *  @param syncMode            indicates where to restore user data from
     */
    public void restoreData(int syncMode) {
        String messageHeader = null;
        
        //#ifdef MUJMAIL_FS
        if (syncMode == LOCAL) {
            //choose file
            //open file as InputStream
            try {
                conn = FilesystemFactory.getFileConnection(filePath, Connector.READ);
                dis = conn.openInputStream();
            } catch (Exception ex) {
                MujMail.mujmail.alert.setAlert(Lang.get(Lang.EXP_FS_OPEN_FILE), AlertType.ERROR);
                ex.printStackTrace();
            }
            messageHeader = "Syncentry ";
        } else if (syncMode == REMOTE) {
        //#endif
            //open srv as InputStream
            try {
                if (isConnected() == false) {
                    open(null);
                }
                //Send "Xmujmail-synccli"
                execute("Xmujmail-synccli " + serverAcct.getUserName() + " " + serverAcct.getPassword(), false);
            } catch (MyException ex) {
                main.alert.setAlert(null, null, Lang.get(Lang.EXP_PROTOCOL_CANNOT_CONNECT), MyAlert.DEFAULT, AlertType.WARNING);
            }
            messageHeader = "* Syncentry";
        //#ifdef MUJMAIL_FS
        }
        //#endif
        
        try {
            //Receive first account
            String message = getData();
            //While this message is not "Done"
            while (message.startsWith(messageHeader)) {
                //parse message
                String syncDataStr = message.substring(message.indexOf(messageHeader) + messageHeader.length());
                
                String entryType = syncDataStr.substring(syncDataStr.indexOf("EntryType: ") + 11,
                                                         syncDataStr.indexOf("\n"));
                
                //get rid of first line, which contains "EntryType: blahblah"
                syncDataStr = syncDataStr.substring(syncDataStr.indexOf("\n") + 1);
                
                if ("Account".equals(entryType)) {
                    MailAccount account = MailAccountPrimary.parseAccountString(syncDataStr);
                    String accountKey   = account.getEmail();
                    if (account.getType() == MailAccount.POP3) {
                        account.setProtocol( new POP3(account) );
                    } else {
                        account.setProtocol( new IMAP4(account) );
                    }
                    //save argument as account
                    main.getAccountSettings().saveAccount(accountKey, account);
                } else if ("Contact".equals(entryType)) {
                    AddressBook addressBook = main.getAddressBook();
                    AddressBook.Contact contact = AddressBook.Contact.parseContact(syncDataStr);
                    
                    //remove the contact if it already exists
                    AddressBook.Contact existingContact = (AddressBook.Contact)addressBook.getEmailHash().get(contact.getEmail());
                    if (existingContact != null) {
                        int index = main.getAddressBook().getAddresses().indexOf(existingContact);
                        main.getAddressBook().delete(index, true);
                    }
                    
                    main.getAddressBook().saveContact(contact);
                } else if ("Settings".equals(entryType)) {
                    main.getSettings().parseAndSetup(syncDataStr);
                }

                //get next syncData
                message = getData();
                if (message == null)
                    break;
            }
        } catch (Exception ex) {
            MujMail.mujmail.alert.setAlert("Cannot retrieve configuration data", AlertType.ERROR);
            return;
        }
        
        if (DEBUG) { System.out.println("DEBUG Sync.restoreData - RESTORE ENDED: MODE="+MODE); }
        //#ifdef MUJMAIL_FS
        if (MODE == LOCAL) {
            try {
                if (DEBUG) { System.out.println("DEBUG Sync.restoreData - CLOSING CONNECTION"); }
                dis.close();
                conn.close();
                if (DEBUG) { System.out.println("DEBUG Sync.restoreData - CONNECTION CLOSED"); }
            } catch (Exception ex) {
                MujMail.mujmail.alert.setAlert(Lang.get(Lang.EXP_FS_CLOSE_FILE), AlertType.ERROR);
            }
        }
        //#endif
    }
    
    /**
     * Backs up mail accounts created by user.
     * @param accounts List of accounts to be saved.
     */
    protected synchronized void backupAccounts(Hashtable accounts) throws MyException {
        //String tag;
        if (accounts.isEmpty()) {
            return;
        }
        
        Enumeration accountsList = accounts.elements();
        //While exists non-sent account
        while (accountsList.hasMoreElements()) {
            //send this account
            MailAccount acct = (MailAccount)accountsList.nextElement();
            execute("Syncentry EntryType: Account\n" + acct.toString(), false);
        }
    }
    
    /**
     * This function backs up address book. Throws
     * MyException exception.
     */
    protected void backupAddressBook(AddressBook addressbook) throws MyException {
        
        Vector addresses = addressbook.getAddresses();
        if (addresses.isEmpty()) return;
        
        Enumeration addressList = addresses.elements();
        
        //While exists non-sent account
        while (addressList.hasMoreElements()) {
            //send this account
            AddressBook.Contact contact = (AddressBook.Contact)addressList.nextElement();
            execute("Syncentry " + "EntryType: Contact\n" + contact.toString(), false);
            
            //parse reply
/*          connection.getLine();
            if (reply.startsWith(tag + "OK") == false)
                throw new MyException(MyException.PROTOCOL_BASE,
                                      "200: " + "Internal error - unknown server reply 2 - " + reply); */
        }
    }

    protected void backupSettings(Settings settings) throws MyException {
        //String tag, reply;
        if (settings == null) return;
        
        execute("Syncentry " + "EntryType: Settings\n" + settings.toString(), false);
        
        return;
    }

    /**
     * This function gets synchronization data string, which is a
     * string of characters of the following structure:
     *
     * Param1: Value1\nParam2: Value2\n...\nLastParam: LastValue\n\n
     *
     * @return                    returns synchronization data string
     */
    protected String getData() {
        String str = null;
        StringBuffer sb = null;
        StringBuffer result = null;
        int ch;
        
        //#ifdef MUJMAIL_FS
        if (MODE == LOCAL)
            sb = new StringBuffer();
        //#endif
        
        //Get first line
        try {
            
            if (MODE == REMOTE) {
                str = connection.getLine();
            }
            //#ifdef MUJMAIL_FS
            else {
                // MODE == LOCAL
                try {
                    while ((ch = dis.read()) != -1) {
                        sb.append((char)ch);
                        if ((char)ch == '\n')
                            break;
                    }
                    if (ch == -1)
                        return null;
                    str = sb.toString();
                    sb.delete(0, sb.length());
                } catch (IOException ex) {
                    MujMail.mujmail.alert.setAlert(Lang.get(Lang.EXP_FS_FILE_READ_ERROR), AlertType.WARNING);
                }
            }
            //#endif
            
            //Case when we receive "TAG OK Completed"
            if (str.length() > 1 && str.indexOf(":") == -1) {
                return null;
            }
    
            result = new StringBuffer(str);
            while (!str.startsWith("\n")) {
                if (MODE == REMOTE) {
                    str = connection.getLine();
                }
                //#ifdef MUJMAIL_FS
                else {
                    // MODE == LOCAL
                    try {
                        while ((ch = dis.read()) != -1) {
                            sb.append((char)ch);
                            if ((char)ch == '\n')
                                break;
                        }
                        if (ch == -1)
                            return null;
                        str = sb.toString();
                        sb.delete(0, sb.length());
                    } catch (IOException ex) {
                        MujMail.mujmail.alert.setAlert(Lang.get(Lang.EXP_FS_FILE_READ_ERROR), AlertType.WARNING);
                    }
                }
                //#endif
                result.append(str);
            }
            
            //Eat up newline
            if (MODE == REMOTE)
                connection.getLine();
        }
        catch(MyException e) {
            e.printStackTrace();
        }

        if (DEBUG) { System.out.println("DEBUG Sync.getData - RESULT OF GETDATA=START"+result+"END"); }
        return result.toString();
    }

    public void commandAction(Command c, Displayable d) {
        //#ifdef MUJMAIL_FS
        if (d == smDlg) {
            if (c == smDlg.Ok) {
                MODE = smDlg.syncModeCG.getSelectedIndex();
        //#else
//#                     MODE = Sync.REMOTE; // No File system support --> only remote synchronisation
        //#endif
                if (this.ACTION == Sync.BACKUP) {
                    if (DEBUG) { System.out.println("DEBUG Sync.CommandAction - MODE="+MODE); }
                    if (MODE == REMOTE) {
                    	// Backup procedure has to be started in a
                    	// different thread than event handler thread
                    	// (thread running commandAction()), otherwise
                    	// deadlock can occur
                    	Thread th = new Thread() {
                    		public void run() {
                                backupData(MODE);                    			
                    		}
                    	};
                    	th.start();
                        main.getDisplay().setCurrent(main.getMenu());
                    }
                    //#ifdef MUJMAIL_FS
                    else { // MODE == LOCAL
                        if (DEBUG) { System.out.println("DEBUG Sync.CommandAction - Starting FileSystemBrowser"); }
                        Callback action = new BackupFSBrowserOKAction();
                        FileSystemBrowser FSBrowser = new FileSystemBrowser(this.main, this.main.getMenu(), action, FileSystemBrowser.ChoosingModes.DIRECTORIES, Lang.get(Lang.FS_BROWSER_SELECT_FILE));
                        FSBrowser.startBrowser(StartupModes.IN_NEW_THREAD);
                    }
                    //#endif
                } else { //RESTORE
                    if (DEBUG) { System.out.println("DEBUG Sync.CommandAction - MODE="+MODE); }
                    if (MODE == REMOTE) {
                    	// Backup procedure has to be started in a
                    	// different thread than event handler thread
                    	// (thread running commandAction()), otherwise
                    	// deadlock can occur
                    	Thread th = new Thread() {
                    		public void run() {
                                restoreData(MODE);                    			
                    		}
                    	};
                    	th.start();
                        //main.getDisplay().setCurrent(main.getMenu());
                    }
                    //#ifdef MUJMAIL_FS
                    else { // MODE == LOCAL
                        if (DEBUG) { System.out.println("DEBUG Sync.CommandAction - Starting FileSystemBrowser"); }
                        Callback action = new RestoreFSBrowserOKAction();
                        FileSystemBrowser FSBrowser = new FileSystemBrowser(this.main, this.main.getMenu(), action, FileSystemBrowser.ChoosingModes.FILES, Lang.get(Lang.FS_BROWSER_SELECT_FILE));
                        FSBrowser.startBrowser(StartupModes.IN_NEW_THREAD);
                    }
                //#endif
                }
            //#ifdef MUJMAIL_FS
            } else if (c == smDlg.Cancel) {
                main.getDisplay().setCurrent(main.getMenu());
            }
            //#endif
        //#ifdef MUJMAIL_FS
        }
        //#endif
    }

}

⌨️ 快捷键说明

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