Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

XmppEndpoint.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * Copyright (c) 2006-2015 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.parser.xmpp;
  23. import javax.annotation.Nonnull;
  24. import org.jivesoftware.smack.packet.Presence;
  25. import org.jivesoftware.smack.packet.Presence.Mode;
  26. import org.jivesoftware.smack.packet.Presence.Type;
  27. /**
  28. * Represents a single endpoint of one contact.
  29. */
  30. public class XmppEndpoint implements Comparable<XmppEndpoint> {
  31. /** The mode of the last presence update. */
  32. private Presence.Mode mode;
  33. /** The type of the last presence update. */
  34. private Presence.Type type;
  35. /** The status from the last presence update. */
  36. private String status;
  37. /** The priority of this endpoint. */
  38. private int priority;
  39. /**
  40. * Gets the mode of this endpoint.
  41. *
  42. * @return The last seen presence mode
  43. */
  44. public Mode getMode() {
  45. return mode;
  46. }
  47. /**
  48. * Sets the mode of this endpoint.
  49. *
  50. * @param mode The most recently received presence mode
  51. */
  52. public void setMode(final Mode mode) {
  53. this.mode = mode;
  54. }
  55. /**
  56. * Gets the status of this endpoint.
  57. *
  58. * @return The last seen presence status
  59. */
  60. public String getStatus() {
  61. return status;
  62. }
  63. /**
  64. * Sets the status of this endpoint.
  65. *
  66. * @param status The most recently received presence status
  67. */
  68. public void setStatus(final String status) {
  69. this.status = status;
  70. }
  71. /**
  72. * Gets the type of this endpoint.
  73. *
  74. * @return The last seen presence type
  75. */
  76. public Type getType() {
  77. return type;
  78. }
  79. /**
  80. * Sets the type of this endpoint.
  81. *
  82. * @param type The most recently received presence type
  83. */
  84. public void setType(final Type type) {
  85. this.type = type;
  86. }
  87. /**
  88. * Gets the priority of this endpoint.
  89. *
  90. * @return The priority of this endpoint
  91. */
  92. public int getPriority() {
  93. return priority;
  94. }
  95. /**
  96. * Sets the priority of this endpoint.
  97. *
  98. * @param priority The new priority for this endpoint
  99. */
  100. public void setPriority(final int priority) {
  101. this.priority = priority;
  102. }
  103. /**
  104. * Gets a user-friendly string describing this endpoint's presence.
  105. *
  106. * @return The presence of this endpoint in a user-friendly format
  107. */
  108. public String getPresence() {
  109. final String statusText = (status == null || status.isEmpty()) ? "" : " (" + status + ")";
  110. if (type == Type.unavailable) {
  111. return "Unavailable" + statusText;
  112. } else if (mode == null) {
  113. return "Available" + statusText;
  114. } else {
  115. return getFriendlyMode(mode) + statusText;
  116. }
  117. }
  118. /**
  119. * Returns a friendly representation of the specified presence mode.
  120. *
  121. * @param mode The mode being represented
  122. *
  123. * @return A user-friendly string corresponding to the mode
  124. */
  125. private static String getFriendlyMode(final Mode mode) {
  126. switch (mode) {
  127. case available:
  128. return "Available";
  129. case away:
  130. return "Away";
  131. case chat:
  132. return "Chatty";
  133. case dnd:
  134. return "Do not disturb";
  135. case xa:
  136. return "Extended Away";
  137. default:
  138. return mode.toString();
  139. }
  140. }
  141. @Override
  142. public int compareTo(@Nonnull final XmppEndpoint o) {
  143. return o.getPriority() - priority;
  144. }
  145. }