aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/pixart/messenger/entities/Bookmark.java
blob: a5fa6fb35948bdea736273fef5b19f5bd6d4cf7a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package de.pixart.messenger.entities;

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import de.pixart.messenger.utils.Namespace;
import de.pixart.messenger.utils.StringUtils;
import de.pixart.messenger.utils.UIHelper;
import de.pixart.messenger.xml.Element;
import de.pixart.messenger.xmpp.InvalidJid;
import rocks.xmpp.addr.Jid;

public class Bookmark extends Element implements ListItem {

    private Account account;
    private WeakReference<Conversation> conversation;
    private Jid jid;

    public Bookmark(final Account account, final Jid jid) {
        super("conference");
        this.jid = jid;
        this.setAttribute("jid", jid.toString());
        this.account = account;
    }

    private Bookmark(Account account) {
        super("conference");
        this.account = account;
    }

    public static Map<Jid, Bookmark> parseFromStorage(Element storage, Account account) {
        if (storage == null) {
            return Collections.emptyMap();
        }
        final HashMap<Jid, Bookmark> bookmarks = new HashMap<>();
        for (final Element item : storage.getChildren()) {
            if (item.getName().equals("conference")) {
                final Bookmark bookmark = Bookmark.parse(item, account);
                if (bookmark != null) {
                    final Bookmark old = bookmarks.put(bookmark.jid, bookmark);
                    if (old != null && old.getBookmarkName() != null && bookmark.getBookmarkName() == null) {
                        bookmark.setBookmarkName(old.getBookmarkName());
                    }
                }
            }
        }
        return bookmarks;
    }

    public static Map<Jid, Bookmark> parseFromPubsub(Element pubsub, Account account) {
        if (pubsub == null) {
            return Collections.emptyMap();
        }
        final Element items = pubsub.findChild("items");
        if (items != null && Namespace.BOOKMARKS2.equals(items.getAttribute("node"))) {
            final Map<Jid, Bookmark> bookmarks = new HashMap<>();
            for (Element item : items.getChildren()) {
                if (item.getName().equals("item")) {
                    final Bookmark bookmark = Bookmark.parseFromItem(item, account);
                    if (bookmark != null) {
                        bookmarks.put(bookmark.jid, bookmark);
                    }
                }
            }
            return bookmarks;
        }
        return Collections.emptyMap();
    }

    public static Bookmark parse(Element element, Account account) {
        Bookmark bookmark = new Bookmark(account);
        bookmark.setAttributes(element.getAttributes());
        bookmark.setChildren(element.getChildren());
        bookmark.jid = InvalidJid.getNullForInvalid(bookmark.getAttributeAsJid("jid"));
        if (bookmark.jid == null) {
            return null;
        }
        return bookmark;
    }

    public static Bookmark parseFromItem(Element item, Account account) {
        final Element conference = item.findChild("conference", Namespace.BOOKMARKS2);
        if (conference == null) {
            return null;
        }
        final Bookmark bookmark = new Bookmark(account);
        bookmark.jid = InvalidJid.getNullForInvalid(item.getAttributeAsJid("id"));
        if (bookmark.jid == null) {
            return null;
        }
        bookmark.setBookmarkName(conference.getAttribute("name"));
        bookmark.setAutojoin(conference.getAttributeAsBoolean("autojoin"));
        bookmark.setNick(conference.findChildContent("nick"));
        return bookmark;
    }

    public void setAutojoin(boolean autojoin) {
        if (autojoin) {
            this.setAttribute("autojoin", "true");
        } else {
            this.setAttribute("autojoin", "false");
        }
    }

    @Override
    public int compareTo(final @NonNull ListItem another) {
        return this.getDisplayName().compareToIgnoreCase(
                another.getDisplayName());
    }

    @Override
    public String getDisplayName() {
        final Conversation c = getConversation();
        final String name = getBookmarkName();
        if (c != null) {
            return c.getName().toString();
        } else if (printableValue(name, false)) {
            return name.trim();
        } else {
            Jid jid = this.getJid();
            return jid != null && jid.getLocal() != null ? jid.getLocal() : "";
        }
    }

    @Override
    public int getOffline() {
        return 0;
    }

    public static boolean printableValue(@Nullable String value, boolean permitNone) {
        return value != null && !value.trim().isEmpty() && (permitNone || !"None".equals(value));
    }

    public static boolean printableValue(@Nullable String value) {
        return printableValue(value, true);
    }

    @Override
    public Jid getJid() {
        return this.jid;
    }

    public Jid getFullJid() {
        final String nick = getNick();
        return jid == null || nick == null || nick.trim().isEmpty() ? jid : jid.withResource(nick);
    }

    @Override
    public List<Tag> getTags(Context context) {
        ArrayList<Tag> tags = new ArrayList<>();
        for (Element element : getChildren()) {
            if (element.getName().equals("group") && element.getContent() != null) {
                String group = element.getContent();
                tags.add(new Tag(group, UIHelper.getColorForName(group, true), 0, account));
            }
        }
        return tags;
    }

    public String getNick() {
        return this.findChildContent("nick");
    }

    public void setNick(String nick) {
        Element element = this.findChild("nick");
        if (element == null) {
            element = this.addChild("nick");
        }
        element.setContent(nick);
    }

    public boolean autojoin() {
        return this.getAttributeAsBoolean("autojoin");
    }

    public String getPassword() {
        return this.findChildContent("password");
    }

    public void setPassword(String password) {
        Element element = this.findChild("password");
        if (element != null) {
            element.setContent(password);
        }
    }

    @Override
    public boolean match(Context context, String needle) {
        if (needle == null) {
            return true;
        }
        needle = needle.toLowerCase(Locale.US);
        final Jid jid = getJid();
        return (jid != null && jid.toString().contains(needle)) ||
                getDisplayName().toLowerCase(Locale.US).contains(needle) ||
                matchInTag(context, needle);
    }

    private boolean matchInTag(Context context, String needle) {
        needle = needle.toLowerCase(Locale.US);
        for (Tag tag : getTags(context)) {
            if (tag.getName().toLowerCase(Locale.US).contains(needle)) {
                return true;
            }
        }
        return false;
    }

    public Account getAccount() {
        return this.account;
    }

    public synchronized Conversation getConversation() {
        return this.conversation != null ? this.conversation.get() : null;
    }

    public synchronized void setConversation(Conversation conversation) {
        if (this.conversation != null) {
            this.conversation.clear();
        }
        if (conversation == null) {
            this.conversation = null;
        } else {
            this.conversation = new WeakReference<>(conversation);
            conversation.getMucOptions().notifyOfBookmarkNick(getNick());
        }
    }

    public String getBookmarkName() {
        return this.getAttribute("name");
    }

    public boolean setBookmarkName(String name) {
        String before = getBookmarkName();
        if (name != null) {
            this.setAttribute("name", name);
        } else {
            this.removeAttribute("name");
        }
        return StringUtils.changed(before, name);
    }

    @Override
    public int getAvatarBackgroundColor() {
        return UIHelper.getColorForName(jid != null ? jid.asBareJid().toString() : getDisplayName());
    }
}