Beispiel #1
0
  private void moveOrCopyMessages(
      List<? extends Message> messages, String folderName, boolean isMove)
      throws MessagingException {
    String[] uids = new String[messages.size()];

    for (int i = 0, count = messages.size(); i < count; i++) {
      uids[i] = messages.get(i).getUid();
    }
    String messageBody;
    Map<String, String> headers = new HashMap<String, String>();
    Map<String, String> uidToUrl = getMessageUrls(uids);
    String[] urls = new String[uids.length];

    for (int i = 0, count = uids.length; i < count; i++) {
      urls[i] = uidToUrl.get(uids[i]);
      if (urls[i] == null && messages.get(i) instanceof WebDavMessage) {
        WebDavMessage wdMessage = (WebDavMessage) messages.get(i);
        urls[i] = wdMessage.getUrl();
      }
    }

    messageBody = store.getMoveOrCopyMessagesReadXml(urls, isMove);
    WebDavFolder destFolder = (WebDavFolder) store.getFolder(folderName);
    headers.put("Destination", destFolder.mFolderUrl);
    headers.put("Brief", "t");
    headers.put("If-Match", "*");
    String action = (isMove ? "BMOVE" : "BCOPY");
    Log.i(LOG_TAG, "Moving " + messages.size() + " messages to " + destFolder.mFolderUrl);

    store.processRequest(mFolderUrl, action, messageBody, headers, false);
  }
Beispiel #2
0
  /**
   * Fetches and parses the message envelopes for the supplied messages. The idea is to have this be
   * recursive so that we do a series of medium calls instead of one large massive call or a large
   * number of smaller calls. Call it a happy balance
   */
  private void fetchEnvelope(
      List<WebDavMessage> startMessages, MessageRetrievalListener<WebDavMessage> listener)
      throws MessagingException {
    Map<String, String> headers = new HashMap<String, String>();
    String messageBody;
    String[] uids;
    List<WebDavMessage> messages = new ArrayList<WebDavMessage>(10);

    if (startMessages == null || startMessages.isEmpty()) {
      return;
    }

    if (startMessages.size() > 10) {
      List<WebDavMessage> newMessages = new ArrayList<WebDavMessage>(startMessages.size() - 10);
      for (int i = 0, count = startMessages.size(); i < count; i++) {
        if (i < 10) {
          messages.add(i, startMessages.get(i));
        } else {
          newMessages.add(i - 10, startMessages.get(i));
        }
      }

      fetchEnvelope(newMessages, listener);
    } else {
      messages.addAll(startMessages);
    }

    uids = new String[messages.size()];

    for (int i = 0, count = messages.size(); i < count; i++) {
      uids[i] = messages.get(i).getUid();
    }

    messageBody = store.getMessageEnvelopeXml(uids);
    headers.put("Brief", "t");
    DataSet dataset = store.processRequest(this.mFolderUrl, "SEARCH", messageBody, headers);

    Map<String, ParsedMessageEnvelope> envelopes = dataset.getMessageEnvelopes();

    int count = messages.size();
    for (int i = messages.size() - 1; i >= 0; i--) {
      WebDavMessage message = messages.get(i);
      if (listener != null) {
        listener.messageStarted(messages.get(i).getUid(), i, count);
      }

      ParsedMessageEnvelope envelope = envelopes.get(message.getUid());
      if (envelope != null) {
        message.setNewHeaders(envelope);
        message.setFlagInternal(Flag.SEEN, envelope.getReadStatus());
      } else {
        Log.e(LOG_TAG, "Asked to get metadata for a non-existent message: " + message.getUid());
      }

      if (listener != null) {
        listener.messageFinished(messages.get(i), i, count);
      }
    }
  }
Beispiel #3
0
  /**
   * Fetches and sets the message flags for the supplied messages. The idea is to have this be
   * recursive so that we do a series of medium calls instead of one large massive call or a large
   * number of smaller calls.
   */
  private void fetchFlags(
      List<WebDavMessage> startMessages, MessageRetrievalListener<WebDavMessage> listener)
      throws MessagingException {
    HashMap<String, String> headers = new HashMap<String, String>();
    String messageBody;
    List<Message> messages = new ArrayList<Message>(20);
    String[] uids;

    if (startMessages == null || startMessages.isEmpty()) {
      return;
    }

    if (startMessages.size() > 20) {
      List<WebDavMessage> newMessages = new ArrayList<WebDavMessage>(startMessages.size() - 20);
      for (int i = 0, count = startMessages.size(); i < count; i++) {
        if (i < 20) {
          messages.add(startMessages.get(i));
        } else {
          newMessages.add(startMessages.get(i));
        }
      }

      fetchFlags(newMessages, listener);
    } else {
      messages.addAll(startMessages);
    }

    uids = new String[messages.size()];

    for (int i = 0, count = messages.size(); i < count; i++) {
      uids[i] = messages.get(i).getUid();
    }

    messageBody = store.getMessageFlagsXml(uids);
    headers.put("Brief", "t");
    DataSet dataset = store.processRequest(this.mFolderUrl, "SEARCH", messageBody, headers);

    if (dataset == null) {
      throw new MessagingException("Data Set from request was null");
    }

    Map<String, Boolean> uidToReadStatus = dataset.getUidToRead();

    for (int i = 0, count = messages.size(); i < count; i++) {
      if (!(messages.get(i) instanceof WebDavMessage)) {
        throw new MessagingException("WebDavStore fetch called with non-WebDavMessage");
      }
      WebDavMessage wdMessage = (WebDavMessage) messages.get(i);

      try {
        wdMessage.setFlagInternal(Flag.SEEN, uidToReadStatus.get(wdMessage.getUid()));
      } catch (NullPointerException e) {
        Log.v(
            LOG_TAG,
            "Under some weird circumstances, setting the read status when syncing from webdav threw an NPE. Skipping.");
      }
    }
  }
Beispiel #4
0
  private void deleteServerMessages(String[] uids) throws MessagingException {
    Map<String, String> uidToUrl = getMessageUrls(uids);

    for (String uid : uids) {
      Map<String, String> headers = new HashMap<String, String>();
      String url = uidToUrl.get(uid);
      String destinationUrl = generateDeleteUrl(url);

      /** If the destination is the same as the origin, assume delete forever */
      if (destinationUrl.equals(url)) {
        headers.put("Brief", "t");
        store.processRequest(url, "DELETE", null, headers, false);
      } else {
        headers.put("Destination", generateDeleteUrl(url));
        headers.put("Brief", "t");
        store.processRequest(url, "MOVE", null, headers, false);
      }
    }
  }
Beispiel #5
0
  private Map<String, String> getMessageUrls(String[] uids) throws MessagingException {
    Map<String, String> headers = new HashMap<String, String>();
    String messageBody;

    /** Retrieve and parse the XML entity for our messages */
    messageBody = store.getMessageUrlsXml(uids);
    headers.put("Brief", "t");

    DataSet dataset = store.processRequest(this.mFolderUrl, "SEARCH", messageBody, headers);

    return dataset.getUidToUrl();
  }
Beispiel #6
0
  @Override
  public List<WebDavMessage> getMessages(
      int start, int end, Date earliestDate, MessageRetrievalListener<WebDavMessage> listener)
      throws MessagingException {
    List<WebDavMessage> messages = new ArrayList<WebDavMessage>();
    String[] uids;
    Map<String, String> headers = new HashMap<String, String>();
    int uidsLength;

    String messageBody;
    int prevStart = start;

    /** Reverse the message range since 0 index is newest */
    start = this.mMessageCount - end;
    end = start + (end - prevStart);

    if (start < 0 || end < 0 || end < start) {
      throw new MessagingException(
          String.format(Locale.US, "Invalid message set %d %d", start, end));
    }

    if (start == 0 && end < 10) {
      end = 10;
    }

    /** Verify authentication */
    messageBody = store.getMessagesXml();

    headers.put("Brief", "t");
    headers.put("Range", "rows=" + start + "-" + end);
    DataSet dataset = store.processRequest(this.mFolderUrl, "SEARCH", messageBody, headers);
    uids = dataset.getUids();
    Map<String, String> uidToUrl = dataset.getUidToUrl();
    uidsLength = uids.length;

    for (int i = 0; i < uidsLength; i++) {
      if (listener != null) {
        listener.messageStarted(uids[i], i, uidsLength);
      }
      WebDavMessage message = new WebDavMessage(uids[i], this);
      message.setUrl(uidToUrl.get(uids[i]));
      messages.add(message);

      if (listener != null) {
        listener.messageFinished(message, i, uidsLength);
      }
    }

    return messages;
  }
Beispiel #7
0
  private void markServerMessagesRead(String[] uids, boolean read) throws MessagingException {
    String messageBody;
    Map<String, String> headers = new HashMap<String, String>();
    Map<String, String> uidToUrl = getMessageUrls(uids);
    String[] urls = new String[uids.length];

    for (int i = 0, count = uids.length; i < count; i++) {
      urls[i] = uidToUrl.get(uids[i]);
    }

    messageBody = store.getMarkMessagesReadXml(urls, read);
    headers.put("Brief", "t");
    headers.put("If-Match", "*");

    store.processRequest(this.mFolderUrl, "BPROPPATCH", messageBody, headers, false);
  }
Beispiel #8
0
  private int getMessageCount(boolean read) throws MessagingException {
    String isRead;
    int messageCount = 0;
    Map<String, String> headers = new HashMap<String, String>();
    String messageBody;

    if (read) {
      isRead = "True";
    } else {
      isRead = "False";
    }

    messageBody = store.getMessageCountXml(isRead);
    headers.put("Brief", "t");
    DataSet dataset = store.processRequest(this.mFolderUrl, "SEARCH", messageBody, headers);
    if (dataset != null) {
      messageCount = dataset.getMessageCount();
    }
    if (K9MailLib.isDebug() && DEBUG_PROTOCOL_WEBDAV) {
      Log.v(LOG_TAG, "Counted messages and webdav returned: " + messageCount);
    }

    return messageCount;
  }