1/**
2 * $RCSfile$
3/**
4 * $RCSfile$
5 * $Revision$
6 * $Date$
7 *
8 * Copyright 2003-2007 Jive Software.
9 *
10 * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 *     http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 */
22
23package org.jivesoftware.smackx.muc;
24
25import java.util.Date;
26
27import org.jivesoftware.smackx.packet.MUCInitialPresence;
28
29/**
30 * The DiscussionHistory class controls the number of characters or messages to receive
31 * when entering a room. The room will decide the amount of history to return if you don't
32 * specify a DiscussionHistory while joining a room.<p>
33 *
34 * You can use some or all of these variable to control the amount of history to receive:
35 * <ul>
36 *  <li>maxchars -> total number of characters to receive in the history.
37 *  <li>maxstanzas -> total number of messages to receive in the history.
38 *  <li>seconds -> only the messages received in the last "X" seconds will be included in the
39 * history.
40 *  <li>since -> only the messages received since the datetime specified will be included in
41 * the history.
42 * </ul>
43 *
44 * Note: Setting maxchars to 0 indicates that the user requests to receive no history.
45 *
46 * @author Gaston Dombiak
47 */
48public class DiscussionHistory {
49
50    private int maxChars = -1;
51    private int maxStanzas = -1;
52    private int seconds = -1;
53    private Date since;
54
55    /**
56     * Returns the total number of characters to receive in the history.
57     *
58     * @return total number of characters to receive in the history.
59     */
60    public int getMaxChars() {
61        return maxChars;
62    }
63
64    /**
65     * Returns the total number of messages to receive in the history.
66     *
67     * @return the total number of messages to receive in the history.
68     */
69    public int getMaxStanzas() {
70        return maxStanzas;
71    }
72
73    /**
74     * Returns the number of seconds to use to filter the messages received during that time.
75     * In other words, only the messages received in the last "X" seconds will be included in
76     * the history.
77     *
78     * @return the number of seconds to use to filter the messages received during that time.
79     */
80    public int getSeconds() {
81        return seconds;
82    }
83
84    /**
85     * Returns the since date to use to filter the messages received during that time.
86     * In other words, only the messages received since the datetime specified will be
87     * included in the history.
88     *
89     * @return the since date to use to filter the messages received during that time.
90     */
91    public Date getSince() {
92        return since;
93    }
94
95    /**
96     * Sets the total number of characters to receive in the history.
97     *
98     * @param maxChars the total number of characters to receive in the history.
99     */
100    public void setMaxChars(int maxChars) {
101        this.maxChars = maxChars;
102    }
103
104    /**
105     * Sets the total number of messages to receive in the history.
106     *
107     * @param maxStanzas the total number of messages to receive in the history.
108     */
109    public void setMaxStanzas(int maxStanzas) {
110        this.maxStanzas = maxStanzas;
111    }
112
113    /**
114     * Sets the number of seconds to use to filter the messages received during that time.
115     * In other words, only the messages received in the last "X" seconds will be included in
116     * the history.
117     *
118     * @param seconds the number of seconds to use to filter the messages received during
119     * that time.
120     */
121    public void setSeconds(int seconds) {
122        this.seconds = seconds;
123    }
124
125    /**
126     * Sets the since date to use to filter the messages received during that time.
127     * In other words, only the messages received since the datetime specified will be
128     * included in the history.
129     *
130     * @param since the since date to use to filter the messages received during that time.
131     */
132    public void setSince(Date since) {
133        this.since = since;
134    }
135
136    /**
137     * Returns true if the history has been configured with some values.
138     *
139     * @return true if the history has been configured with some values.
140     */
141    private boolean isConfigured() {
142        return maxChars > -1 || maxStanzas > -1 || seconds > -1 || since != null;
143    }
144
145    /**
146     * Returns the History that manages the amount of discussion history provided on entering a
147     * room.
148     *
149     * @return the History that manages the amount of discussion history provided on entering a
150     * room.
151     */
152    MUCInitialPresence.History getMUCHistory() {
153        // Return null if the history was not properly configured
154        if (!isConfigured()) {
155            return null;
156        }
157
158        MUCInitialPresence.History mucHistory = new MUCInitialPresence.History();
159        if (maxChars > -1) {
160            mucHistory.setMaxChars(maxChars);
161        }
162        if (maxStanzas > -1) {
163            mucHistory.setMaxStanzas(maxStanzas);
164        }
165        if (seconds > -1) {
166            mucHistory.setSeconds(seconds);
167        }
168        if (since != null) {
169            mucHistory.setSince(since);
170        }
171        return mucHistory;
172    }
173}
174