1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-11-16 06:25:06 -05:00
k-9/src/com/fsck/k9/search/SearchSpecification.java

185 lines
5.6 KiB
Java
Raw Normal View History

package com.fsck.k9.search;
import android.os.Parcel;
import android.os.Parcelable;
public interface SearchSpecification extends Parcelable {
2012-10-16 16:42:51 -04:00
/**
* Get all the uuids of accounts this search acts on.
* @return Array of uuids.
*/
public String[] getAccountUuids();
2012-10-16 16:42:51 -04:00
/**
* Returns the search's name if it was named.
* @return Name of the search.
*/
2012-10-16 16:42:51 -04:00
public String getName();
/**
* Returns the root node of the condition tree accompanying
* the search.
*
* @return Root node of conditions tree.
*/
public ConditionsTreeNode getConditions();
/*
* Some meta names for certain conditions.
*/
public static final String ALL_ACCOUNTS = "allAccounts";
2012-10-16 16:42:51 -04:00
public static final String GENERIC_INBOX_NAME = "genericInboxName";
///////////////////////////////////////////////////////////////
// ATTRIBUTE enum
///////////////////////////////////////////////////////////////
2012-10-17 14:52:03 -04:00
public enum Attribute {
2012-10-16 16:42:51 -04:00
CONTAINS(false), EQUALS(false), STARTSWITH(false), ENDSWITH(false),
NOT_CONTAINS(true), NOT_EQUALS(true), NOT_STARTSWITH(true), NOT_ENDSWITH(true);
private boolean mNegation;
2012-10-17 14:52:03 -04:00
private Attribute(boolean negation) {
2012-10-16 16:42:51 -04:00
this.mNegation = negation;
}
public String formQuery(String value) {
2012-10-16 16:42:51 -04:00
String queryPart = "";
switch (this) {
case NOT_CONTAINS:
case CONTAINS:
queryPart = "'%"+value+"%'";
break;
case NOT_EQUALS:
case EQUALS:
queryPart = "'"+value+"'";
break;
case NOT_STARTSWITH:
case STARTSWITH:
queryPart = "'%"+value+"'";
break;
case NOT_ENDSWITH:
case ENDSWITH:
queryPart = "'"+value+"%'";
break;
default: queryPart = "'"+value+"'";
}
return (mNegation ? " NOT LIKE " : " LIKE ") + queryPart;
}
2012-10-17 14:52:03 -04:00
}
2012-10-16 16:42:51 -04:00
///////////////////////////////////////////////////////////////
// SEARCHFIELD enum
///////////////////////////////////////////////////////////////
/*
* Using an enum in order to have more robust code. Users ( & coders )
* are prevented from passing illegal fields. No database overhead
* when invalid fields passed.
*
* By result, only the fields in here are searchable.
*
* Fields not in here at this moment ( and by effect not searchable ):
* id, html_content, internal_date, message_id,
* preview, mime_type
2012-10-16 16:42:51 -04:00
*
*/
2012-10-17 14:52:03 -04:00
public enum Searchfield {
SUBJECT("subject"), DATE("date"), UID("uid"), FLAG("flags"),
SENDER("sender_list"), TO("to_list"), CC("cc_list"), FOLDER("folder_id"),
BCC("bcc_list"), REPLY_TO("reply_to_list"), MESSAGE("text_content"),
ATTACHMENT_COUNT("attachment_count"), DELETED("deleted"), THREAD_ROOT("thread_root");
private String dbName;
2012-10-16 16:42:51 -04:00
2012-10-17 14:52:03 -04:00
private Searchfield(String dbName) {
this.dbName = dbName;
}
public String getDatabaseName() {
return dbName;
}
}
2012-10-16 16:42:51 -04:00
///////////////////////////////////////////////////////////////
// SearchCondition class
///////////////////////////////////////////////////////////////
/**
* This class represents 1 value for a certain search field. One
2012-10-16 16:42:51 -04:00
* value consists of three things:
* an attribute: equals, starts with, contains,...
* a searchfield: date, flags, sender, subject,...
* a value: "apple", "jesse",..
2012-10-16 16:42:51 -04:00
*
* @author dzan
*/
2012-10-17 14:52:03 -04:00
public class SearchCondition implements Parcelable {
public String value;
2012-10-17 14:52:03 -04:00
public Attribute attribute;
public Searchfield field;
2012-10-16 16:42:51 -04:00
2012-10-17 14:52:03 -04:00
public SearchCondition(Searchfield field, Attribute attribute, String value) {
this.value = value;
this.attribute = attribute;
this.field = field;
}
private SearchCondition(Parcel in) {
2012-10-16 16:42:51 -04:00
this.value = in.readString();
2012-10-17 14:52:03 -04:00
this.attribute = Attribute.values()[in.readInt()];
this.field = Searchfield.values()[in.readInt()];
2012-10-16 16:42:51 -04:00
}
public String toHumanString() {
return field.toString() + attribute.toString();
}
@Override
public String toString() {
return field.getDatabaseName() + attribute.formQuery(value);
}
2012-10-16 16:42:51 -04:00
@Override
public boolean equals(Object o) {
if (o instanceof SearchCondition) {
SearchCondition tmp = (SearchCondition) o;
2012-10-17 14:52:03 -04:00
if (tmp.attribute == attribute &&
tmp.field == field &&
tmp.value.equals(value)) {
return true;
}
}
2012-10-17 14:52:03 -04:00
return false;
}
2012-10-16 16:42:51 -04:00
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(value);
dest.writeInt(attribute.ordinal());
dest.writeInt(field.ordinal());
}
2012-10-17 14:52:03 -04:00
public static final Parcelable.Creator<SearchCondition> CREATOR =
new Parcelable.Creator<SearchCondition>() {
@Override
2012-10-16 16:42:51 -04:00
public SearchCondition createFromParcel(Parcel in) {
return new SearchCondition(in);
}
2012-10-17 14:52:03 -04:00
@Override
2012-10-16 16:42:51 -04:00
public SearchCondition[] newArray(int size) {
return new SearchCondition[size];
}
};
}
}