2019-02-13 09:24:06 +00:00
|
|
|
package eu.faircode.email;
|
|
|
|
|
|
|
|
/*
|
|
|
|
This file is part of FairEmail.
|
|
|
|
|
|
|
|
FairEmail is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
FairEmail is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2024-01-01 07:50:49 +00:00
|
|
|
Copyright 2018-2024 by Marcel Bokhorst (M66B)
|
2019-02-13 09:24:06 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
import android.database.Cursor;
|
|
|
|
|
2019-03-14 18:47:10 +00:00
|
|
|
import androidx.lifecycle.LiveData;
|
2019-02-13 09:24:06 +00:00
|
|
|
import androidx.room.Dao;
|
|
|
|
import androidx.room.Insert;
|
|
|
|
import androidx.room.Query;
|
|
|
|
import androidx.room.Update;
|
|
|
|
|
2019-04-17 18:21:44 +00:00
|
|
|
import java.util.List;
|
|
|
|
|
2019-02-13 09:24:06 +00:00
|
|
|
@Dao
|
|
|
|
public interface DaoContact {
|
2019-03-14 18:47:10 +00:00
|
|
|
@Query("SELECT * FROM contact" +
|
2019-03-17 08:26:17 +00:00
|
|
|
" WHERE account = :account")
|
|
|
|
List<EntityContact> getContacts(long account);
|
|
|
|
|
2022-04-18 21:12:24 +00:00
|
|
|
@Query("SELECT contact.*, account.name AS accountName, identity.email AS identityEmail" +
|
2019-03-17 08:26:17 +00:00
|
|
|
" FROM contact" +
|
2019-06-07 16:33:35 +00:00
|
|
|
" JOIN account ON account.id = contact.account" +
|
2022-04-18 21:12:24 +00:00
|
|
|
" LEFT JOIN identity ON identity.id = contact.identity" +
|
2022-03-26 10:46:32 +00:00
|
|
|
" WHERE (:account IS NULL OR contact.account = :account)" +
|
2019-03-16 18:30:56 +00:00
|
|
|
" ORDER BY times_contacted DESC, last_contacted DESC")
|
2022-03-26 10:46:32 +00:00
|
|
|
LiveData<List<TupleContactEx>> liveContacts(Long account);
|
2019-03-14 18:47:10 +00:00
|
|
|
|
2020-03-16 10:44:25 +00:00
|
|
|
@Query("SELECT email, name, avatar FROM contact" +
|
2019-03-17 08:26:17 +00:00
|
|
|
" WHERE state <> " + EntityContact.STATE_IGNORE +
|
2021-09-19 05:43:43 +00:00
|
|
|
" AND (type = " + EntityContact.TYPE_TO +
|
|
|
|
" OR type = " + EntityContact.TYPE_FROM + ")" +
|
2019-03-16 16:58:57 +00:00
|
|
|
" ORDER BY" +
|
2019-03-17 08:26:17 +00:00
|
|
|
" CASE WHEN state = " + EntityContact.STATE_FAVORITE + " THEN 0 ELSE 1 END" +
|
2019-03-17 09:49:50 +00:00
|
|
|
", CASE WHEN avatar IS NULL THEN 1 ELSE 0 END" +
|
2019-03-16 16:58:57 +00:00
|
|
|
", times_contacted DESC" +
|
2020-03-16 10:44:25 +00:00
|
|
|
", last_contacted DESC")
|
|
|
|
Cursor getFrequentlyContacted();
|
2019-03-14 19:22:13 +00:00
|
|
|
|
2022-03-27 07:10:21 +00:00
|
|
|
@Query("SELECT * FROM contact WHERE id = :id")
|
|
|
|
EntityContact getContact(long id);
|
|
|
|
|
2022-04-18 21:57:46 +00:00
|
|
|
@Query("SELECT DISTINCT identity FROM contact" +
|
|
|
|
" WHERE email = :email" +
|
2022-12-18 11:01:10 +00:00
|
|
|
" AND type = :type" +
|
2022-04-18 21:57:46 +00:00
|
|
|
" AND NOT identity IS NULL")
|
2022-12-18 11:01:10 +00:00
|
|
|
List<Long> getIdentities(String email, int type);
|
2022-04-18 21:57:46 +00:00
|
|
|
|
2019-02-13 09:24:06 +00:00
|
|
|
@Query("SELECT *" +
|
|
|
|
" FROM contact" +
|
2019-03-17 08:26:17 +00:00
|
|
|
" WHERE account = :account" +
|
|
|
|
" AND type = :type" +
|
2019-04-24 09:59:28 +00:00
|
|
|
" AND email = :email COLLATE NOCASE")
|
2019-03-17 08:26:17 +00:00
|
|
|
EntityContact getContact(long account, int type, String email);
|
2019-02-13 09:24:06 +00:00
|
|
|
|
2022-03-27 08:21:06 +00:00
|
|
|
@Query("SELECT -1 AS _id, `group` AS title" +
|
|
|
|
", COUNT(*) AS summ_count" +
|
|
|
|
", :name AS account_name, :type AS account_type" +
|
|
|
|
" FROM contact" +
|
|
|
|
" WHERE (:account IS NULL OR account = :account)" +
|
|
|
|
" AND `group` IS NOT NULL" +
|
|
|
|
" GROUP BY `group`" +
|
|
|
|
" ORDER BY `group` COLLATE NOCASE")
|
|
|
|
Cursor getGroups(Long account, String name, String type);
|
|
|
|
|
|
|
|
@Query("SELECT * FROM contact" +
|
|
|
|
" WHERE `group` = :group" +
|
|
|
|
" AND type <> " + EntityContact.TYPE_JUNK)
|
|
|
|
List<EntityContact> getContacts(String group);
|
|
|
|
|
2020-03-30 08:34:32 +00:00
|
|
|
@Query("SELECT *" +
|
2019-02-13 09:24:06 +00:00
|
|
|
" FROM contact" +
|
2019-03-17 08:26:17 +00:00
|
|
|
" WHERE (:account IS NULL OR account = :account)" +
|
|
|
|
" AND (:type IS NULL OR type = :type)" +
|
2019-03-17 09:23:05 +00:00
|
|
|
" AND (email LIKE :query COLLATE NOCASE OR name LIKE :query COLLATE NOCASE)" +
|
2019-03-17 09:55:25 +00:00
|
|
|
" AND state <> " + EntityContact.STATE_IGNORE +
|
2020-03-30 11:53:55 +00:00
|
|
|
" GROUP BY name, email")
|
2020-03-30 08:34:32 +00:00
|
|
|
List<EntityContact> searchContacts(Long account, Integer type, String query);
|
2019-02-13 09:24:06 +00:00
|
|
|
|
2022-06-13 16:03:27 +00:00
|
|
|
@Query("SELECT COUNT(*) FROM contact" +
|
|
|
|
" WHERE (type = " + EntityContact.TYPE_TO +
|
|
|
|
" OR type = " + EntityContact.TYPE_FROM + ")")
|
|
|
|
int countContacts();
|
|
|
|
|
2023-09-02 09:39:14 +00:00
|
|
|
@Query("SELECT COUNT(*) FROM contact" +
|
|
|
|
" WHERE account = :account" +
|
|
|
|
" AND type = " + EntityContact.TYPE_JUNK)
|
|
|
|
int countBlocked(long account);
|
|
|
|
|
2019-02-13 09:24:06 +00:00
|
|
|
@Insert
|
|
|
|
long insertContact(EntityContact contact);
|
|
|
|
|
|
|
|
@Update
|
|
|
|
int updateContact(EntityContact contact);
|
2019-02-17 16:45:19 +00:00
|
|
|
|
2023-03-16 07:22:39 +00:00
|
|
|
@Query("UPDATE contact SET folder = :folder WHERE id=:id")
|
|
|
|
int setContactFolder(long id, Long folder);
|
|
|
|
|
|
|
|
@Query("UPDATE contact SET folder = NULL")
|
|
|
|
int clearContactFolders();
|
|
|
|
|
2019-08-16 08:27:10 +00:00
|
|
|
@Query("DELETE FROM contact WHERE id = :id")
|
|
|
|
int deleteContact(long id);
|
|
|
|
|
2021-09-18 18:37:59 +00:00
|
|
|
@Query("DELETE FROM contact" +
|
|
|
|
" WHERE account = :account" +
|
|
|
|
" AND type = :type")
|
|
|
|
int deleteContact(long account, int type);
|
|
|
|
|
2019-09-12 15:44:36 +00:00
|
|
|
@Query("DELETE FROM contact" +
|
|
|
|
" WHERE account = :account" +
|
|
|
|
" AND type = :type" +
|
|
|
|
" AND email = :email")
|
|
|
|
int deleteContact(long account, int type, String email);
|
|
|
|
|
2020-11-29 11:14:01 +00:00
|
|
|
@Query("UPDATE contact SET state = :state WHERE id = :id AND NOT (state IS :state)")
|
2019-03-15 11:31:26 +00:00
|
|
|
int setContactState(long id, int state);
|
2019-03-15 08:36:23 +00:00
|
|
|
|
2019-03-15 08:50:31 +00:00
|
|
|
@Query("DELETE FROM contact" +
|
|
|
|
" WHERE last_contacted IS NOT NULL" +
|
|
|
|
" AND last_contacted < :before" +
|
2021-10-26 06:00:38 +00:00
|
|
|
" AND state <> " + EntityContact.STATE_FAVORITE +
|
|
|
|
" AND (type = " + EntityContact.TYPE_TO +
|
|
|
|
" OR type = " + EntityContact.TYPE_FROM + ")")
|
2019-03-15 08:50:31 +00:00
|
|
|
int deleteContacts(long before);
|
|
|
|
|
2021-10-26 06:00:38 +00:00
|
|
|
@Query("DELETE FROM contact" +
|
2022-03-26 11:53:06 +00:00
|
|
|
" WHERE (:account IS NULL OR account = :account)" +
|
|
|
|
" AND type IN (:types)")
|
|
|
|
int clearContacts(Long account, int[] types);
|
2019-02-13 09:24:06 +00:00
|
|
|
}
|