Search in sources :

Example 6 with Identity

use of com.fsck.k9.Identity in project k-9 by k9mail.

the class QuotedMessagePresenter method processSourceMessageText.

/**
     * Pull out the parts of the now loaded source message and apply them to the new message
     * depending on the type of message being composed.
     * @param bodyOffset Insertion point for reply.
     * @param bodyLength Length of reply.
     * @param viewMessageContent Update mMessageContentView or not.
     */
private void processSourceMessageText(Part rootMessagePart, int bodyOffset, int bodyLength, boolean viewMessageContent) {
    Part textPart = MimeUtility.findFirstPartByMimeType(rootMessagePart, "text/plain");
    if (textPart == null) {
        return;
    }
    String messageText = MessageExtractor.getTextFromPart(textPart);
    Timber.d("Loading message with offset %d, length %d. Text length is %d.", bodyOffset, bodyLength, messageText.length());
    // and put them in their respective places in the UI.
    if (bodyLength != UNKNOWN_LENGTH) {
        try {
            // Regenerate the quoted text without our user content in it nor added newlines.
            StringBuilder quotedText = new StringBuilder();
            if (bodyOffset == UNKNOWN_LENGTH && messageText.substring(bodyLength, bodyLength + 4).equals("\r\n\r\n")) {
                // top-posting: ignore two newlines at start of quote
                quotedText.append(messageText.substring(bodyLength + 4));
            } else if (bodyOffset + bodyLength == messageText.length() && messageText.substring(bodyOffset - 2, bodyOffset).equals("\r\n")) {
                // bottom-posting: ignore newline at end of quote
                quotedText.append(messageText.substring(0, bodyOffset - 2));
            } else {
                // stuff before the reply
                quotedText.append(messageText.substring(0, bodyOffset));
                quotedText.append(messageText.substring(bodyOffset + bodyLength));
            }
            view.setQuotedText(quotedText.toString());
            messageText = messageText.substring(bodyOffset, bodyOffset + bodyLength);
        } catch (IndexOutOfBoundsException e) {
            // Invalid bodyOffset or bodyLength.  The draft was edited outside of K-9 Mail?
            Timber.d("The identity field from the draft contains an invalid bodyOffset/bodyLength");
        }
    }
    if (viewMessageContent) {
        view.setMessageContentCharacters(messageText);
    }
}
Also used : Part(com.fsck.k9.mail.Part)

Example 7 with Identity

use of com.fsck.k9.Identity in project k-9 by k9mail.

the class SettingsExporter method writeAccount.

private static void writeAccount(XmlSerializer serializer, Account account, Map<String, Object> prefs) throws IOException {
    Set<Integer> identities = new HashSet<>();
    Set<String> folders = new HashSet<>();
    String accountUuid = account.getUuid();
    serializer.startTag(null, ACCOUNT_ELEMENT);
    serializer.attribute(null, UUID_ATTRIBUTE, accountUuid);
    String name = (String) prefs.get(accountUuid + "." + Account.ACCOUNT_DESCRIPTION_KEY);
    if (name != null) {
        serializer.startTag(null, NAME_ELEMENT);
        serializer.text(name);
        serializer.endTag(null, NAME_ELEMENT);
    }
    // Write incoming server settings
    ServerSettings incoming = RemoteStore.decodeStoreUri(account.getStoreUri());
    serializer.startTag(null, INCOMING_SERVER_ELEMENT);
    serializer.attribute(null, TYPE_ATTRIBUTE, incoming.type.name());
    writeElement(serializer, HOST_ELEMENT, incoming.host);
    if (incoming.port != -1) {
        writeElement(serializer, PORT_ELEMENT, Integer.toString(incoming.port));
    }
    if (incoming.connectionSecurity != null) {
        writeElement(serializer, CONNECTION_SECURITY_ELEMENT, incoming.connectionSecurity.name());
    }
    if (incoming.authenticationType != null) {
        writeElement(serializer, AUTHENTICATION_TYPE_ELEMENT, incoming.authenticationType.name());
    }
    writeElement(serializer, USERNAME_ELEMENT, incoming.username);
    writeElement(serializer, CLIENT_CERTIFICATE_ALIAS_ELEMENT, incoming.clientCertificateAlias);
    // XXX For now we don't export the password
    //writeElement(serializer, PASSWORD_ELEMENT, incoming.password);
    Map<String, String> extras = incoming.getExtra();
    if (extras != null && extras.size() > 0) {
        serializer.startTag(null, EXTRA_ELEMENT);
        for (Entry<String, String> extra : extras.entrySet()) {
            writeKeyAndPrettyValueFromSetting(serializer, extra.getKey(), extra.getValue());
        }
        serializer.endTag(null, EXTRA_ELEMENT);
    }
    serializer.endTag(null, INCOMING_SERVER_ELEMENT);
    // Write outgoing server settings
    ServerSettings outgoing = Transport.decodeTransportUri(account.getTransportUri());
    serializer.startTag(null, OUTGOING_SERVER_ELEMENT);
    serializer.attribute(null, TYPE_ATTRIBUTE, outgoing.type.name());
    writeElement(serializer, HOST_ELEMENT, outgoing.host);
    if (outgoing.port != -1) {
        writeElement(serializer, PORT_ELEMENT, Integer.toString(outgoing.port));
    }
    if (outgoing.connectionSecurity != null) {
        writeElement(serializer, CONNECTION_SECURITY_ELEMENT, outgoing.connectionSecurity.name());
    }
    if (outgoing.authenticationType != null) {
        writeElement(serializer, AUTHENTICATION_TYPE_ELEMENT, outgoing.authenticationType.name());
    }
    writeElement(serializer, USERNAME_ELEMENT, outgoing.username);
    writeElement(serializer, CLIENT_CERTIFICATE_ALIAS_ELEMENT, outgoing.clientCertificateAlias);
    // XXX For now we don't export the password
    //writeElement(serializer, PASSWORD_ELEMENT, outgoing.password);
    extras = outgoing.getExtra();
    if (extras != null && extras.size() > 0) {
        serializer.startTag(null, EXTRA_ELEMENT);
        for (Entry<String, String> extra : extras.entrySet()) {
            writeKeyAndPrettyValueFromSetting(serializer, extra.getKey(), extra.getValue());
        }
        serializer.endTag(null, EXTRA_ELEMENT);
    }
    serializer.endTag(null, OUTGOING_SERVER_ELEMENT);
    // Write account settings
    serializer.startTag(null, SETTINGS_ELEMENT);
    for (Map.Entry<String, Object> entry : prefs.entrySet()) {
        String key = entry.getKey();
        String valueString = entry.getValue().toString();
        String[] comps = key.split("\\.", 2);
        if (comps.length < 2) {
            // Skip global settings
            continue;
        }
        String keyUuid = comps[0];
        String keyPart = comps[1];
        if (!keyUuid.equals(accountUuid)) {
            // Setting doesn't belong to the account we're currently writing.
            continue;
        }
        int indexOfLastDot = keyPart.lastIndexOf(".");
        boolean hasThirdPart = indexOfLastDot != -1 && indexOfLastDot < keyPart.length() - 1;
        if (hasThirdPart) {
            String secondPart = keyPart.substring(0, indexOfLastDot);
            String thirdPart = keyPart.substring(indexOfLastDot + 1);
            if (Account.IDENTITY_DESCRIPTION_KEY.equals(secondPart)) {
                // This is an identity key. Save identity index for later...
                try {
                    identities.add(Integer.parseInt(thirdPart));
                } catch (NumberFormatException e) {
                /* ignore */
                }
                // ... but don't write it now.
                continue;
            }
            if (FolderSettings.SETTINGS.containsKey(thirdPart)) {
                // This is a folder key. Save folder name for later...
                folders.add(secondPart);
                // ... but don't write it now.
                continue;
            }
        }
        TreeMap<Integer, SettingsDescription> versionedSetting = AccountSettings.SETTINGS.get(keyPart);
        if (versionedSetting != null) {
            Integer highestVersion = versionedSetting.lastKey();
            SettingsDescription setting = versionedSetting.get(highestVersion);
            if (setting != null) {
                // Only export account settings that can be found in AccountSettings.SETTINGS
                try {
                    writeKeyAndPrettyValueFromSetting(serializer, keyPart, setting, valueString);
                } catch (InvalidSettingValueException e) {
                    Timber.w("Account setting \"%s\" (%s) has invalid value \"%s\" in preference storage. " + "This shouldn't happen!", keyPart, account.getDescription(), valueString);
                }
            }
        }
    }
    serializer.endTag(null, SETTINGS_ELEMENT);
    if (identities.size() > 0) {
        serializer.startTag(null, IDENTITIES_ELEMENT);
        // Sort identity indices (that's why we store them as Integers)
        List<Integer> sortedIdentities = new ArrayList<>(identities);
        Collections.sort(sortedIdentities);
        for (Integer identityIndex : sortedIdentities) {
            writeIdentity(serializer, accountUuid, identityIndex.toString(), prefs);
        }
        serializer.endTag(null, IDENTITIES_ELEMENT);
    }
    if (folders.size() > 0) {
        serializer.startTag(null, FOLDERS_ELEMENT);
        for (String folder : folders) {
            writeFolder(serializer, accountUuid, folder, prefs);
        }
        serializer.endTag(null, FOLDERS_ELEMENT);
    }
    serializer.endTag(null, ACCOUNT_ELEMENT);
}
Also used : ArrayList(java.util.ArrayList) SettingsDescription(com.fsck.k9.preferences.Settings.SettingsDescription) InvalidSettingValueException(com.fsck.k9.preferences.Settings.InvalidSettingValueException) ServerSettings(com.fsck.k9.mail.ServerSettings) Map(java.util.Map) TreeMap(java.util.TreeMap) HashSet(java.util.HashSet)

Example 8 with Identity

use of com.fsck.k9.Identity in project k-9 by k9mail.

the class SettingsExporter method writeIdentity.

private static void writeIdentity(XmlSerializer serializer, String accountUuid, String identity, Map<String, Object> prefs) throws IOException {
    serializer.startTag(null, IDENTITY_ELEMENT);
    String prefix = accountUuid + ".";
    String suffix = "." + identity;
    // Write name belonging to the identity
    String name = (String) prefs.get(prefix + Account.IDENTITY_NAME_KEY + suffix);
    serializer.startTag(null, NAME_ELEMENT);
    serializer.text(name);
    serializer.endTag(null, NAME_ELEMENT);
    // Write email address belonging to the identity
    String email = (String) prefs.get(prefix + Account.IDENTITY_EMAIL_KEY + suffix);
    serializer.startTag(null, EMAIL_ELEMENT);
    serializer.text(email);
    serializer.endTag(null, EMAIL_ELEMENT);
    // Write identity description
    String description = (String) prefs.get(prefix + Account.IDENTITY_DESCRIPTION_KEY + suffix);
    if (description != null) {
        serializer.startTag(null, DESCRIPTION_ELEMENT);
        serializer.text(description);
        serializer.endTag(null, DESCRIPTION_ELEMENT);
    }
    // Write identity settings
    serializer.startTag(null, SETTINGS_ELEMENT);
    for (Map.Entry<String, Object> entry : prefs.entrySet()) {
        String key = entry.getKey();
        String valueString = entry.getValue().toString();
        String[] comps = key.split("\\.");
        if (comps.length < 3) {
            // Skip non-identity config entries
            continue;
        }
        String keyUuid = comps[0];
        String identityKey = comps[1];
        String identityIndex = comps[2];
        if (!keyUuid.equals(accountUuid) || !identityIndex.equals(identity)) {
            // Skip entries that belong to another identity
            continue;
        }
        TreeMap<Integer, SettingsDescription> versionedSetting = IdentitySettings.SETTINGS.get(identityKey);
        if (versionedSetting != null) {
            Integer highestVersion = versionedSetting.lastKey();
            SettingsDescription setting = versionedSetting.get(highestVersion);
            if (setting != null) {
                // Only write settings that have an entry in IdentitySettings.SETTINGS
                try {
                    writeKeyAndPrettyValueFromSetting(serializer, identityKey, setting, valueString);
                } catch (InvalidSettingValueException e) {
                    Timber.w("Identity setting \"%s\" has invalid value \"%s\" in preference storage. " + "This shouldn't happen!", identityKey, valueString);
                }
            }
        }
    }
    serializer.endTag(null, SETTINGS_ELEMENT);
    serializer.endTag(null, IDENTITY_ELEMENT);
}
Also used : SettingsDescription(com.fsck.k9.preferences.Settings.SettingsDescription) InvalidSettingValueException(com.fsck.k9.preferences.Settings.InvalidSettingValueException) Map(java.util.Map) TreeMap(java.util.TreeMap)

Example 9 with Identity

use of com.fsck.k9.Identity in project k-9 by k9mail.

the class MessageReference method parse.

@Nullable
public static MessageReference parse(String identity) {
    if (identity == null || identity.length() < 1 || identity.charAt(0) != IDENTITY_VERSION_1) {
        return null;
    }
    StringTokenizer tokens = new StringTokenizer(identity.substring(2), IDENTITY_SEPARATOR, false);
    if (tokens.countTokens() < 3) {
        return null;
    }
    String accountUuid = Base64.decode(tokens.nextToken());
    String folderName = Base64.decode(tokens.nextToken());
    String uid = Base64.decode(tokens.nextToken());
    if (!tokens.hasMoreTokens()) {
        return new MessageReference(accountUuid, folderName, uid, null);
    }
    Flag flag;
    try {
        flag = Flag.valueOf(tokens.nextToken());
    } catch (IllegalArgumentException e) {
        return null;
    }
    return new MessageReference(accountUuid, folderName, uid, flag);
}
Also used : StringTokenizer(java.util.StringTokenizer) Flag(com.fsck.k9.mail.Flag) Nullable(android.support.annotation.Nullable)

Example 10 with Identity

use of com.fsck.k9.Identity in project k-9 by k9mail.

the class EditIdentity method onCreate.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mIdentity = (Identity) getIntent().getSerializableExtra(EXTRA_IDENTITY);
    mIdentityIndex = getIntent().getIntExtra(EXTRA_IDENTITY_INDEX, -1);
    String accountUuid = getIntent().getStringExtra(EXTRA_ACCOUNT);
    mAccount = Preferences.getPreferences(this).getAccount(accountUuid);
    if (mIdentityIndex == -1) {
        mIdentity = new Identity();
    }
    setContentView(R.layout.edit_identity);
    /*
         * If we're being reloaded we override the original account with the one
         * we saved
         */
    if (savedInstanceState != null && savedInstanceState.containsKey(EXTRA_IDENTITY)) {
        mIdentity = (Identity) savedInstanceState.getSerializable(EXTRA_IDENTITY);
    }
    mDescriptionView = (EditText) findViewById(R.id.description);
    mDescriptionView.setText(mIdentity.getDescription());
    mNameView = (EditText) findViewById(R.id.name);
    mNameView.setText(mIdentity.getName());
    mEmailView = (EditText) findViewById(R.id.email);
    mEmailView.setText(mIdentity.getEmail());
    mReplyTo = (EditText) findViewById(R.id.reply_to);
    mReplyTo.setText(mIdentity.getReplyTo());
    //      mAccountAlwaysBcc = (EditText)findViewById(R.id.bcc);
    //      mAccountAlwaysBcc.setText(mIdentity.getAlwaysBcc());
    mSignatureLayout = (LinearLayout) findViewById(R.id.signature_layout);
    mSignatureUse = (CheckBox) findViewById(R.id.signature_use);
    mSignatureView = (EditText) findViewById(R.id.signature);
    mSignatureUse.setChecked(mIdentity.getSignatureUse());
    mSignatureUse.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                mSignatureLayout.setVisibility(View.VISIBLE);
                mSignatureView.setText(mIdentity.getSignature());
            } else {
                mSignatureLayout.setVisibility(View.GONE);
            }
        }
    });
    if (mSignatureUse.isChecked()) {
        mSignatureView.setText(mIdentity.getSignature());
    } else {
        mSignatureLayout.setVisibility(View.GONE);
    }
}
Also used : Identity(com.fsck.k9.Identity) CompoundButton(android.widget.CompoundButton)

Aggregations

Identity (com.fsck.k9.Identity)10 Account (com.fsck.k9.Account)4 InvalidSettingValueException (com.fsck.k9.preferences.Settings.InvalidSettingValueException)3 Test (org.junit.Test)3 Nullable (android.support.annotation.Nullable)2 View (android.view.View)2 TextView (android.widget.TextView)2 Preferences (com.fsck.k9.Preferences)2 Message (com.fsck.k9.mail.Message)2 Part (com.fsck.k9.mail.Part)2 ServerSettings (com.fsck.k9.mail.ServerSettings)2 MimeMessage (com.fsck.k9.mail.internet.MimeMessage)2 LocalMessage (com.fsck.k9.mailstore.LocalMessage)2 InsertableHtmlContent (com.fsck.k9.message.quote.InsertableHtmlContent)2 SettingsDescription (com.fsck.k9.preferences.Settings.SettingsDescription)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 TreeMap (java.util.TreeMap)2 SuppressLint (android.annotation.SuppressLint)1