Simple email application for Android. Original source code: https://framagit.org/dystopia-project/simple-email
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
3.5 KiB

  1. package eu.faircode.email;
  2. /*
  3. This file is part of Safe email.
  4. Safe email is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. NetGuard is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with NetGuard. If not, see <http://www.gnu.org/licenses/>.
  14. Copyright 2018 by Marcel Bokhorst (M66B)
  15. */
  16. import android.arch.persistence.room.Entity;
  17. import android.arch.persistence.room.ForeignKey;
  18. import android.arch.persistence.room.Index;
  19. import android.arch.persistence.room.PrimaryKey;
  20. import android.content.Context;
  21. import android.content.Intent;
  22. import android.support.annotation.NonNull;
  23. import android.support.v4.content.LocalBroadcastManager;
  24. import android.util.Log;
  25. import org.json.JSONArray;
  26. import static android.arch.persistence.room.ForeignKey.CASCADE;
  27. @Entity(
  28. tableName = EntityOperation.TABLE_NAME,
  29. foreignKeys = {
  30. @ForeignKey(childColumns = "message", entity = EntityMessage.class, parentColumns = "id", onDelete = CASCADE)
  31. },
  32. indices = {
  33. @Index(value = {"message"})
  34. }
  35. )
  36. public class EntityOperation {
  37. static final String TABLE_NAME = "operation";
  38. @PrimaryKey(autoGenerate = true)
  39. public Long id;
  40. @NonNull
  41. public Long message;
  42. @NonNull
  43. public String name;
  44. public String args;
  45. public static final String SEEN = "seen";
  46. public static final String ADD = "add";
  47. public static final String MOVE = "move";
  48. public static final String DELETE = "delete";
  49. public static final String SEND = "send";
  50. static void queue(Context context, EntityMessage message, String name) {
  51. JSONArray jsonArray = new JSONArray();
  52. queue(context, message, name, jsonArray);
  53. }
  54. static void queue(Context context, EntityMessage message, String name, Object value) {
  55. JSONArray jsonArray = new JSONArray();
  56. jsonArray.put(value);
  57. queue(context, message, name, jsonArray);
  58. }
  59. private static void queue(Context context, EntityMessage message, String name, JSONArray jsonArray) {
  60. DaoOperation dao = DB.getInstance(context).operation();
  61. int purged = 0;
  62. if (SEEN.equals(name)) {
  63. if (message.uid == null) {
  64. // local message
  65. return;
  66. }
  67. purged = dao.deleteOperations(message.id, name);
  68. } else if (DELETE.equals(name)) {
  69. if (message.uid == null)
  70. return;
  71. }
  72. EntityOperation operation = new EntityOperation();
  73. operation.message = message.id;
  74. operation.name = name;
  75. operation.args = jsonArray.toString();
  76. operation.id = dao.insertOperation(operation);
  77. Log.i(Helper.TAG, "Queued op=" + operation.id + "/" + name +
  78. " args=" + operation.args +
  79. " msg=" + message.folder + "/" + message.id + " uid=" + message.uid +
  80. " purged=" + purged);
  81. LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context);
  82. lbm.sendBroadcast(
  83. new Intent(ServiceSynchronize.ACTION_PROCESS_OPERATIONS + message.folder));
  84. }
  85. }