2018-08-02 13:33:06 +00:00
package eu.faircode.email ;
/ *
2018-08-14 05:53:24 +00:00
This file is part of FairEmail .
2018-08-02 13:33:06 +00:00
2018-08-14 05:53:24 +00:00
FairEmail is free software : you can redistribute it and / or modify
2018-08-02 13:33:06 +00:00
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 .
2018-10-29 10:46:49 +00:00
FairEmail is distributed in the hope that it will be useful ,
2018-08-02 13:33:06 +00:00
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
2018-10-29 10:46:49 +00:00
along with FairEmail . If not , see < http : //www.gnu.org/licenses/>.
2018-08-02 13:33:06 +00:00
2018-12-31 08:04:33 +00:00
Copyright 2018 - 2019 by Marcel Bokhorst ( M66B )
2018-08-02 13:33:06 +00:00
* /
2018-08-08 06:55:47 +00:00
import androidx.lifecycle.LiveData ;
import androidx.room.Dao ;
import androidx.room.Insert ;
import androidx.room.Query ;
2019-04-17 18:21:44 +00:00
import java.util.List ;
2018-08-02 13:33:06 +00:00
@Dao
public interface DaoFolder {
2019-07-07 06:17:27 +00:00
@Query ( " SELECT * FROM folder " +
" WHERE account = :account " +
2019-07-07 07:25:52 +00:00
" AND (NOT :writable OR NOT read_only) " +
2019-07-07 06:17:27 +00:00
" AND (NOT :selectable OR selectable) " )
2019-07-07 07:25:52 +00:00
List < EntityFolder > getFolders ( long account , boolean writable , boolean selectable ) ;
2018-08-05 06:18:43 +00:00
2019-05-25 20:51:44 +00:00
@Query ( " SELECT folder.* " +
2019-06-08 07:51:00 +00:00
" , account.id AS accountId, account.`order` AS accountOrder, account.name AS accountName, account.color AS accountColor, account.state AS accountState " +
2019-06-28 08:37:22 +00:00
" , COUNT(DISTINCT CASE WHEN rule.enabled THEN rule.id ELSE NULL END) rules " +
" , COUNT(DISTINCT CASE WHEN message.ui_hide THEN NULL ELSE message.id END) AS messages " +
" , COUNT(DISTINCT CASE WHEN message.content = 1 AND NOT message.ui_hide THEN message.id ELSE NULL END) AS content " +
" , COUNT(DISTINCT CASE WHEN message.ui_seen = 0 AND NOT message.ui_hide THEN message.id ELSE NULL END) AS unseen " +
" , COUNT(DISTINCT CASE WHEN operation.state = 'executing' THEN operation.id ELSE NULL END) AS executing " +
2019-05-25 20:51:44 +00:00
" FROM folder " +
" LEFT JOIN account ON account.id = folder.account " +
2019-06-27 06:09:22 +00:00
" LEFT JOIN message ON message.folder = folder.id " +
" LEFT JOIN rule ON rule.folder = folder.id " +
" LEFT JOIN operation ON operation.folder = folder.id " +
2019-05-25 20:51:44 +00:00
" WHERE folder.account = :account AND account.synchronize " +
" GROUP BY folder.id " )
List < TupleFolderEx > getFoldersEx ( long account ) ;
2018-12-16 07:40:25 +00:00
@Query ( " SELECT folder.* FROM folder " +
2019-06-07 16:33:35 +00:00
" JOIN account ON account.id = folder.account " +
2019-02-28 15:19:15 +00:00
" WHERE account.synchronize " +
" AND folder.synchronize AND unified " )
2018-12-11 18:01:44 +00:00
List < EntityFolder > getFoldersSynchronizingUnified ( ) ;
2018-12-03 15:29:49 +00:00
2019-01-22 14:29:45 +00:00
@Query ( " SELECT folder.* FROM folder " +
2019-06-07 16:33:35 +00:00
" JOIN account ON account.id = folder.account " +
2019-01-26 15:49:49 +00:00
" WHERE folder.id = :folder " +
2019-01-23 09:31:31 +00:00
" AND (:search OR (account.synchronize AND account.browse)) " )
2019-01-26 15:49:49 +00:00
EntityFolder getBrowsableFolder ( long folder , boolean search ) ;
2019-01-22 14:29:45 +00:00
2019-05-01 05:58:45 +00:00
@Query ( " SELECT folder.* " +
2019-05-06 20:34:18 +00:00
" , account.`order` AS accountOrder, account.name AS accountName " +
2019-05-01 05:58:45 +00:00
" FROM folder " +
2019-06-07 16:33:35 +00:00
" JOIN account ON account.id = folder.account " +
2019-05-01 05:58:45 +00:00
" WHERE account.`synchronize` " )
2019-05-08 18:33:40 +00:00
List < TupleFolderSort > getSortedFolders ( ) ;
2019-05-01 05:58:45 +00:00
2019-02-27 19:55:33 +00:00
@Query ( " SELECT folder.* " +
2019-06-08 07:51:00 +00:00
" , account.id AS accountId, account.`order` AS accountOrder, account.name AS accountName, account.color AS accountColor, account.state AS accountState " +
2019-06-28 08:37:22 +00:00
" , COUNT(DISTINCT CASE WHEN rule.enabled THEN rule.id ELSE NULL END) rules " +
" , COUNT(DISTINCT CASE WHEN message.ui_hide THEN NULL ELSE message.id END) AS messages " +
" , COUNT(DISTINCT CASE WHEN message.content = 1 AND NOT message.ui_hide THEN message.id ELSE NULL END) AS content " +
" , COUNT(DISTINCT CASE WHEN message.ui_seen = 0 AND NOT message.ui_hide THEN message.id ELSE NULL END) AS unseen " +
" , COUNT(DISTINCT CASE WHEN operation.state = 'executing' THEN operation.id ELSE NULL END) AS executing " +
2018-08-02 13:33:06 +00:00
" FROM folder " +
" LEFT JOIN account ON account.id = folder.account " +
2019-06-27 06:09:22 +00:00
" LEFT JOIN message ON message.folder = folder.id " +
" LEFT JOIN rule ON rule.folder = folder.id " +
" LEFT JOIN operation ON operation.folder = folder.id " +
2018-12-02 09:09:21 +00:00
" WHERE CASE WHEN :account IS NULL " +
" THEN folder.unified AND account.synchronize " +
2019-03-18 13:35:45 +00:00
" ELSE folder.account = :account AND account.synchronize " +
2018-12-02 09:09:21 +00:00
" END " +
2018-08-02 13:33:06 +00:00
" GROUP BY folder.id " )
2019-05-25 20:09:15 +00:00
LiveData < List < TupleFolderEx > > liveFolders ( Long account ) ;
2018-08-05 15:42:02 +00:00
2019-02-27 19:55:33 +00:00
@Query ( " SELECT folder.* " +
2019-06-08 07:51:00 +00:00
" , account.id AS accountId, account.`order` AS accountOrder, account.name AS accountName, account.color AS accountColor, account.state AS accountState " +
2019-06-28 08:37:22 +00:00
" , COUNT(DISTINCT CASE WHEN rule.enabled THEN rule.id ELSE NULL END) rules " +
" , COUNT(DISTINCT CASE WHEN message.ui_hide THEN NULL ELSE message.id END) AS messages " +
" , COUNT(DISTINCT CASE WHEN message.content = 1 AND NOT message.ui_hide THEN message.id ELSE NULL END) AS content " +
" , COUNT(DISTINCT CASE WHEN message.ui_seen = 0 AND NOT message.ui_hide THEN message.id ELSE NULL END) AS unseen " +
" , COUNT(DISTINCT CASE WHEN operation.state = 'executing' THEN operation.id ELSE NULL END) AS executing " +
2018-08-23 11:43:27 +00:00
" FROM folder " +
" JOIN account ON account.id = folder.account " +
2019-06-27 06:09:22 +00:00
" LEFT JOIN message ON message.folder = folder.id " +
" LEFT JOIN rule ON rule.folder = folder.id " +
" LEFT JOIN operation ON operation.folder = folder.id " +
2018-08-24 18:16:08 +00:00
" WHERE account.`synchronize` " +
2019-07-22 11:44:25 +00:00
" AND ((:type IS NULL AND folder.unified) OR folder.type = :type) " +
2018-08-23 11:43:27 +00:00
" GROUP BY folder.id " )
2019-07-22 11:44:25 +00:00
LiveData < List < TupleFolderEx > > liveUnified ( String type ) ;
2018-08-23 11:43:27 +00:00
2019-04-29 08:14:55 +00:00
@Query ( " SELECT folder.* " +
2019-05-06 20:34:18 +00:00
" , account.`order` AS accountOrder, account.name AS accountName, account.color AS accountColor " +
2019-04-29 08:14:55 +00:00
" , SUM(CASE WHEN message.ui_seen = 0 THEN 1 ELSE 0 END) AS unseen " +
2019-05-11 19:30:09 +00:00
" , (SELECT COUNT(operation.id) FROM operation WHERE operation.folder = folder.id) AS operations " +
" , (SELECT COUNT(operation.id) FROM operation WHERE operation.folder = folder.id AND operation.state = 'executing') AS executing " +
2019-04-29 08:14:55 +00:00
" FROM folder " +
2019-06-07 16:33:35 +00:00
" LEFT JOIN account ON account.id = folder.account " +
2019-06-28 16:31:01 +00:00
" LEFT JOIN message ON message.folder = folder.id AND message.ui_hide = 0 " +
" WHERE account.id IS NULL " +
" OR (account.`synchronize` AND folder.navigation) " +
2019-04-29 08:14:55 +00:00
" GROUP BY folder.id " )
LiveData < List < TupleFolderNav > > liveNavigation ( ) ;
2018-12-13 09:43:30 +00:00
@Query ( " SELECT folder.* FROM folder " +
2019-06-07 16:33:35 +00:00
" JOIN account ON account.id = folder.account " +
2019-03-06 17:08:29 +00:00
" WHERE account.synchronize " +
" AND account.`primary` " +
" AND folder.type = ' " + EntityFolder . DRAFTS + " ' " )
2018-12-13 09:43:30 +00:00
LiveData < EntityFolder > livePrimaryDrafts ( ) ;
2019-03-20 14:52:51 +00:00
@Query ( " SELECT COUNT(id) FROM folder " +
" WHERE sync_state = 'syncing' " +
" AND folder.type <> ' " + EntityFolder . OUTBOX + " ' " )
LiveData < Integer > liveSynchronizing ( ) ;
2019-02-27 19:55:33 +00:00
@Query ( " SELECT folder.* " +
2019-06-08 07:51:00 +00:00
" , account.id AS accountId, account.`order` AS accountOrder, account.name AS accountName, account.color AS accountColor, account.state AS accountState " +
2019-06-28 08:37:22 +00:00
" , COUNT(DISTINCT CASE WHEN rule.enabled THEN rule.id ELSE NULL END) rules " +
" , COUNT(DISTINCT CASE WHEN message.ui_hide THEN NULL ELSE message.id END) AS messages " +
" , COUNT(DISTINCT CASE WHEN message.content = 1 AND NOT message.ui_hide THEN message.id ELSE NULL END) AS content " +
" , COUNT(DISTINCT CASE WHEN message.ui_seen = 0 AND NOT message.ui_hide THEN message.id ELSE NULL END) AS unseen " +
" , COUNT(DISTINCT CASE WHEN operation.state = 'executing' THEN operation.id ELSE NULL END) AS executing " +
2018-08-02 13:33:06 +00:00
" FROM folder " +
" LEFT JOIN account ON account.id = folder.account " +
2019-06-27 06:09:22 +00:00
" LEFT JOIN message ON message.folder = folder.id " +
" LEFT JOIN rule ON rule.folder = folder.id " +
" LEFT JOIN operation ON operation.folder = folder.id " +
2019-06-21 06:39:36 +00:00
" WHERE folder.id = :id " +
" GROUP BY folder.id " )
2018-08-02 13:33:06 +00:00
LiveData < TupleFolderEx > liveFolderEx ( long id ) ;
2019-04-27 19:43:48 +00:00
@Query ( " SELECT * FROM folder ORDER BY account, name COLLATE NOCASE " )
2018-11-26 16:04:32 +00:00
List < EntityFolder > getFolders ( ) ;
2018-12-05 10:50:07 +00:00
@Query ( " SELECT * FROM folder " +
" WHERE folder.account = :account " +
" AND type <> ' " + EntityFolder . USER + " ' " )
List < EntityFolder > getSystemFolders ( long account ) ;
2019-07-19 06:27:44 +00:00
@Query ( " SELECT folder.type " +
" FROM folder " +
" JOIN account ON account.id = folder.account " +
2019-07-21 07:50:17 +00:00
" WHERE account.synchronize " +
2019-07-22 11:44:25 +00:00
" AND folder.type <> ' " + EntityFolder . SYSTEM + " ' " +
2019-07-19 06:48:14 +00:00
" AND folder.type <> ' " + EntityFolder . USER + " ' " +
2019-07-19 06:27:44 +00:00
" GROUP BY folder.type " +
" HAVING COUNT(folder.id) > 1 " )
LiveData < List < String > > liveUnifiedTypes ( ) ;
2018-08-02 13:33:06 +00:00
@Query ( " SELECT * FROM folder WHERE id = :id " )
EntityFolder getFolder ( Long id ) ;
@Query ( " SELECT * FROM folder WHERE account = :account AND name = :name " )
2018-08-04 18:52:09 +00:00
EntityFolder getFolderByName ( Long account , String name ) ;
2018-08-02 13:33:06 +00:00
@Query ( " SELECT folder.* FROM folder " +
2018-08-04 18:52:09 +00:00
" WHERE account = :account AND type = :type " )
EntityFolder getFolderByType ( long account , String type ) ;
2018-08-02 13:33:06 +00:00
2018-08-10 09:45:36 +00:00
@Query ( " SELECT folder.* FROM folder " +
2019-06-07 16:33:35 +00:00
" JOIN account ON account.id = folder.account " +
2018-08-10 09:45:36 +00:00
" WHERE `primary` AND type = ' " + EntityFolder . DRAFTS + " ' " )
EntityFolder getPrimaryDrafts ( ) ;
2018-08-08 11:21:19 +00:00
2018-10-21 07:48:18 +00:00
@Query ( " SELECT folder.* FROM folder " +
2019-06-07 16:33:35 +00:00
" JOIN account ON account.id = folder.account " +
2018-10-21 07:48:18 +00:00
" WHERE `primary` AND type = ' " + EntityFolder . ARCHIVE + " ' " )
EntityFolder getPrimaryArchive ( ) ;
2018-08-09 07:02:41 +00:00
@Query ( " SELECT * FROM folder WHERE type = ' " + EntityFolder . OUTBOX + " ' " )
2018-08-02 13:33:06 +00:00
EntityFolder getOutbox ( ) ;
2019-06-07 16:33:35 +00:00
@Query ( " SELECT download FROM folder WHERE id = :id " )
2018-12-25 08:37:19 +00:00
boolean getFolderDownload ( long id ) ;
2018-08-12 08:16:41 +00:00
@Insert
2018-08-02 13:33:06 +00:00
long insertFolder ( EntityFolder folder ) ;
2019-05-25 15:14:51 +00:00
@Query ( " UPDATE folder SET unified = :unified WHERE id = :id " )
int setFolderUnified ( long id , boolean unified ) ;
@Query ( " UPDATE folder SET navigation = :navigation WHERE id = :id " )
int setFolderNavigation ( long id , boolean navigation ) ;
@Query ( " UPDATE folder SET notify = :notify WHERE id = :id " )
int setFolderNotify ( long id , boolean notify ) ;
2019-05-03 13:18:32 +00:00
@Query ( " UPDATE folder SET synchronize = :synchronize WHERE id = :id " )
int setFolderSynchronize ( long id , boolean synchronize ) ;
2018-08-13 14:44:47 +00:00
@Query ( " UPDATE folder SET state = :state WHERE id = :id " )
int setFolderState ( long id , String state ) ;
2019-06-24 06:34:17 +00:00
@Query ( " UPDATE folder SET state = :state WHERE account = :account " )
int setFolderStates ( long account , String state ) ;
2018-12-03 12:41:36 +00:00
@Query ( " UPDATE folder SET sync_state = :state WHERE id = :id " )
int setFolderSyncState ( long id , String state ) ;
2019-03-09 08:20:46 +00:00
@Query ( " UPDATE folder SET total = :total WHERE id = :id " )
int setFolderTotal ( long id , Integer total ) ;
2018-08-13 14:44:47 +00:00
@Query ( " UPDATE folder SET error = :error WHERE id = :id " )
int setFolderError ( long id , String error ) ;
2019-04-25 15:23:56 +00:00
@Query ( " UPDATE folder SET subscribed = :subscribed WHERE id = :id " )
int setFolderSubscribed ( long id , Boolean subscribed ) ;
2018-08-13 14:44:47 +00:00
@Query ( " UPDATE folder SET type = :type WHERE id = :id " )
int setFolderType ( long id , String type ) ;
2018-12-22 17:39:16 +00:00
@Query ( " UPDATE folder SET display = :display WHERE id = :id " )
int setFolderDisplay ( long id , String display ) ;
2019-04-29 11:09:11 +00:00
@Query ( " UPDATE folder SET `order` = :order WHERE id = :id " )
int setFolderOrder ( long id , Integer order ) ;
2019-03-17 19:32:57 +00:00
@Query ( " UPDATE folder SET parent = :parent WHERE id = :id " )
int setFolderParent ( long id , Long parent ) ;
2019-03-18 08:33:40 +00:00
@Query ( " UPDATE folder SET collapsed = :collapsed WHERE id = :id " )
int setFolderCollapsed ( long id , boolean collapsed ) ;
2018-08-15 09:50:39 +00:00
@Query ( " UPDATE folder " +
" SET type = ' " + EntityFolder . USER + " ' " +
2018-12-31 10:04:18 +00:00
" WHERE account = :account " +
2019-02-02 11:50:57 +00:00
" AND type <> ' " + EntityFolder . INBOX + " ' " +
2018-12-31 10:04:18 +00:00
" AND type <> ' " + EntityFolder . SYSTEM + " ' " )
2018-10-22 18:02:06 +00:00
int setFoldersUser ( long account ) ;
2018-08-14 06:40:36 +00:00
2018-09-21 15:19:37 +00:00
@Query ( " UPDATE folder " +
2018-12-22 11:40:47 +00:00
" SET display = :display " +
2018-12-06 12:43:00 +00:00
" , unified = :unified " +
2019-04-29 08:14:55 +00:00
" , navigation = :navigation " +
2018-12-14 11:31:25 +00:00
" , notify = :notify " +
2019-06-15 09:58:56 +00:00
" , hide = :hide " +
2018-09-21 15:19:37 +00:00
" , synchronize = :synchronize " +
2018-12-05 09:25:35 +00:00
" , poll = :poll " +
2018-12-14 11:31:25 +00:00
" , download = :download " +
2018-11-14 09:49:59 +00:00
" , `sync_days` = :sync_days " +
" , `keep_days` = :keep_days " +
2019-05-01 09:26:32 +00:00
" , auto_delete = :auto_delete " +
2018-09-21 15:19:37 +00:00
" WHERE id = :id " )
2018-12-02 09:09:21 +00:00
int setFolderProperties (
long id ,
2019-06-15 09:58:56 +00:00
String display , boolean unified , boolean navigation , boolean notify , boolean hide ,
2018-12-14 11:31:25 +00:00
boolean synchronize , boolean poll , boolean download ,
2019-05-01 09:26:32 +00:00
int sync_days , int keep_days , boolean auto_delete ) ;
2018-08-02 13:33:06 +00:00
2018-11-26 08:12:44 +00:00
@Query ( " UPDATE folder SET keywords = :keywords WHERE id = :id " )
int setFolderKeywords ( long id , String keywords ) ;
2018-09-14 06:28:23 +00:00
@Query ( " UPDATE folder SET name = :name WHERE account = :account AND name = :old " )
int renameFolder ( long account , String old , String name ) ;
2019-06-24 10:45:52 +00:00
@Query ( " UPDATE folder SET initialize = :days WHERE id = :id " )
int setFolderInitialize ( long id , int days ) ;
@Query ( " UPDATE folder SET keep_days = :days WHERE id = :id " )
int setFolderKeep ( long id , int days ) ;
2018-12-11 11:18:26 +00:00
2019-01-01 18:49:21 +00:00
@Query ( " UPDATE folder SET last_sync = :last_sync WHERE id = :id " )
int setFolderSync ( long id , long last_sync ) ;
2019-04-30 07:06:32 +00:00
@Query ( " UPDATE folder SET read_only = :read_only WHERE id = :id " )
int setFolderReadOnly ( long id , boolean read_only ) ;
2019-02-04 10:08:09 +00:00
@Query ( " UPDATE folder SET tbc = null WHERE id = :id " )
2018-12-22 11:40:47 +00:00
int resetFolderTbc ( long id ) ;
2018-12-06 12:43:00 +00:00
@Query ( " UPDATE folder SET tbd = 1 WHERE id = :id " )
int setFolderTbd ( long id ) ;
2018-09-14 08:15:45 +00:00
@Query ( " DELETE FROM folder WHERE id = :id " )
void deleteFolder ( long id ) ;
2018-12-22 11:40:47 +00:00
@Query ( " DELETE FROM folder WHERE account = :account AND name = :name " )
void deleteFolder ( long account , String name ) ;
2018-08-02 13:33:06 +00:00
}