aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/pixart/messenger/ui/util/MucConfiguration.java
blob: 1d4f4168b702bed410cb18ec86eb44e78489df25 (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
package de.pixart.messenger.ui.util;

import android.content.Context;
import android.os.Bundle;
import androidx.annotation.StringRes;

import de.pixart.messenger.R;
import de.pixart.messenger.entities.MucOptions;

public class MucConfiguration {

    public final @StringRes
    int title;
    public final String[] names;
    public final boolean[] values;
    public final Option[] options;

    private MucConfiguration(@StringRes int title, String[] names, boolean[] values, Option[] options) {
        this.title = title;
        this.names = names;
        this.values = values;
        this.options = options;
    }

    public static MucConfiguration get(Context context, boolean advanced, MucOptions mucOptions) {
        if (mucOptions.isPrivateAndNonAnonymous()) {
            String[] names = new String[]{
                    context.getString(R.string.allow_participants_to_edit_subject),
                    context.getString(R.string.allow_participants_to_invite_others)
            };
            boolean[] values = new boolean[]{
                    mucOptions.participantsCanChangeSubject(),
                    mucOptions.allowInvites()
            };
            final Option[] options = new Option[]{
                    new Option("muc#roomconfig_changesubject"),
                    new Option("muc#roomconfig_allowinvites")
            };
            return new MucConfiguration(R.string.conference_options, names, values, options);
        } else {
            final String[] names;
            final boolean[] values;
            final Option[] options;
            if (advanced) {
                names = new String[]{
                        context.getString(R.string.non_anonymous),
                        context.getString(R.string.allow_participants_to_edit_subject),
                        context.getString(R.string.moderated)
                };
                values = new boolean[]{
                        mucOptions.nonanonymous(),
                        mucOptions.participantsCanChangeSubject(),
                        mucOptions.moderated()
                };
                options = new Option[]{
                        new Option("muc#roomconfig_whois", "anyone", "moderators"),
                        new Option("muc#roomconfig_changesubject"),
                        new Option("muc#roomconfig_moderatedroom")
                };
            } else {
                names = new String[]{
                        context.getString(R.string.non_anonymous),
                        context.getString(R.string.allow_participants_to_edit_subject),
                };
                values = new boolean[]{
                        mucOptions.nonanonymous(),
                        mucOptions.participantsCanChangeSubject()
                };
                options = new Option[]{
                        new Option("muc#roomconfig_whois", "anyone", "moderators"),
                        new Option("muc#roomconfig_changesubject")
                };
            }
            return new MucConfiguration(R.string.channel_options, names, values, options);
        }
    }

    public static String describe(final Context context, final MucOptions mucOptions) {
        final StringBuilder builder = new StringBuilder();
        if (mucOptions.isPrivateAndNonAnonymous()) {
            if (mucOptions.participantsCanChangeSubject()) {
                builder.append(context.getString(R.string.anyone_can_edit_subject));
            } else {
                builder.append(context.getString(R.string.owners_can_edit_subject));
            }
            builder.append(' ');
            if (mucOptions.allowInvites()) {
                builder.append(context.getString(R.string.anyone_can_invite_others));
            } else {
                builder.append(context.getString(R.string.owners_can_invite_others));
            }
        } else {
            if (mucOptions.nonanonymous()) {
                builder.append(context.getString(R.string.jabber_ids_are_visible_to_anyone));
            } else {
                builder.append(context.getString(R.string.jabber_ids_are_visible_to_admins));
            }
            builder.append(' ');
            if (mucOptions.participantsCanChangeSubject()) {
                builder.append(context.getString(R.string.anyone_can_edit_subject));
            } else {
                builder.append(context.getString(R.string.admins_can_edit_subject));
            }
        }
        return builder.toString();
    }

    public Bundle toBundle(boolean[] values) {
        Bundle bundle = new Bundle();
        for (int i = 0; i < values.length; ++i) {
            final Option option = options[i];
            bundle.putString(option.name, option.values[values[i] ? 0 : 1]);
        }
        return bundle;
    }

    private static class Option {
        public final String name;
        public final String[] values;

        private Option(String name) {
            this.name = name;
            this.values = new String[]{"1", "0"};
        }

        private Option(String name, String on, String off) {
            this.name = name;
            this.values = new String[]{on, off};
        }
    }

}