1/*
2 * libjingle
3 * Copyright 2004--2005, Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 *  1. Redistributions of source code must retain the above copyright notice,
9 *     this list of conditions and the following disclaimer.
10 *  2. Redistributions in binary form must reproduce the above copyright notice,
11 *     this list of conditions and the following disclaimer in the documentation
12 *     and/or other materials provided with the distribution.
13 *  3. The name of the author may not be used to endorse or promote products
14 *     derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#ifndef THIRD_PARTY_LIBJINGLE_FILES_TALK_XMPP_PRESENCESTATUS_H_
29#define THIRD_PARTY_LIBJINGLE_FILES_TALK_XMPP_PRESENCESTATUS_H_
30
31#include "talk/xmpp/constants.h"
32#include "talk/xmpp/jid.h"
33
34namespace buzz {
35
36class PresenceStatus {
37public:
38  PresenceStatus();
39  ~PresenceStatus() {}
40
41  // These are arranged in "priority order", i.e., if we see
42  // two statuses at the same priority but with different Shows,
43  // we will show the one with the highest show in the following
44  // order.
45  enum Show {
46    SHOW_NONE     = 0,
47    SHOW_OFFLINE  = 1,
48    SHOW_XA       = 2,
49    SHOW_AWAY     = 3,
50    SHOW_DND      = 4,
51    SHOW_ONLINE   = 5,
52    SHOW_CHAT     = 6,
53  };
54
55  const Jid& jid() const { return jid_; }
56  int priority() const { return pri_; }
57  Show show() const { return show_; }
58  const std::string& status() const { return status_; }
59  const std::string& nick() const { return nick_; }
60  bool available() const { return available_ ; }
61  int error_code() const { return e_code_; }
62  const std::string& error_string() const { return e_str_; }
63  bool know_capabilities() const { return know_capabilities_; }
64  bool voice_capability() const { return voice_capability_; }
65  bool pmuc_capability() const { return pmuc_capability_; }
66  bool video_capability() const { return video_capability_; }
67  bool camera_capability() const { return camera_capability_; }
68  const std::string& caps_node() const { return caps_node_; }
69  const std::string& version() const { return version_; }
70  bool feedback_probation() const { return feedback_probation_; }
71  const std::string& sent_time() const { return sent_time_; }
72
73  void set_jid(const Jid& jid) { jid_ = jid; }
74  void set_priority(int pri) { pri_ = pri; }
75  void set_show(Show show) { show_ = show; }
76  void set_status(const std::string& status) { status_ = status; }
77  void set_nick(const std::string& nick) { nick_ = nick; }
78  void set_available(bool a) { available_ = a; }
79  void set_error(int e_code, const std::string e_str)
80      { e_code_ = e_code; e_str_ = e_str; }
81  void set_know_capabilities(bool f) { know_capabilities_ = f; }
82  void set_voice_capability(bool f) { voice_capability_ = f; }
83  void set_pmuc_capability(bool f) { pmuc_capability_ = f; }
84  void set_video_capability(bool f) { video_capability_ = f; }
85  void set_camera_capability(bool f) { camera_capability_ = f; }
86  void set_caps_node(const std::string& f) { caps_node_ = f; }
87  void set_version(const std::string& v) { version_ = v; }
88  void set_feedback_probation(bool f) { feedback_probation_ = f; }
89  void set_sent_time(const std::string& time) { sent_time_ = time; }
90
91  void UpdateWith(const PresenceStatus& new_value);
92
93  bool HasQuietStatus() const {
94    if (status_.empty())
95      return false;
96    return !(QuietStatus().empty());
97  }
98
99  // Knowledge of other clients' silly automatic status strings -
100  // Don't show these.
101  std::string QuietStatus() const {
102    if (jid_.resource().find("Psi") != std::string::npos) {
103      if (status_ == "Online" ||
104          status_.find("Auto Status") != std::string::npos)
105        return STR_EMPTY;
106    }
107    if (jid_.resource().find("Gaim") != std::string::npos) {
108      if (status_ == "Sorry, I ran out for a bit!")
109        return STR_EMPTY;
110    }
111    return TrimStatus(status_);
112  }
113
114  std::string ExplicitStatus() const {
115    std::string result = QuietStatus();
116    if (result.empty()) {
117      result = ShowStatus();
118    }
119    return result;
120  }
121
122  std::string ShowStatus() const {
123    std::string result;
124    if (!available()) {
125      result = "Offline";
126    }
127    else {
128      switch (show()) {
129        case SHOW_AWAY:
130        case SHOW_XA:
131          result = "Idle";
132          break;
133        case SHOW_DND:
134          result = "Busy";
135          break;
136        case SHOW_CHAT:
137          result = "Chatty";
138          break;
139        default:
140          result = "Available";
141          break;
142      }
143    }
144    return result;
145  }
146
147  static std::string TrimStatus(const std::string& st) {
148    std::string s(st);
149    int j = 0;
150    bool collapsing = true;
151    for (unsigned int i = 0; i < s.length(); i+= 1) {
152      if (s[i] <= ' ' && s[i] >= 0) {
153        if (collapsing) {
154          continue;
155        }
156        else {
157          s[j] = ' ';
158          j += 1;
159          collapsing = true;
160        }
161      }
162      else {
163        s[j] = s[i];
164        j += 1;
165        collapsing = false;
166      }
167    }
168    if (collapsing && j > 0) {
169      j -= 1;
170    }
171    s.erase(j, s.length());
172    return s;
173  }
174
175private:
176  Jid jid_;
177  int pri_;
178  Show show_;
179  std::string status_;
180  std::string nick_;
181  bool available_;
182  int e_code_;
183  std::string e_str_;
184  bool feedback_probation_;
185
186  // capabilities (valid only if know_capabilities_
187  bool know_capabilities_;
188  bool voice_capability_;
189  bool pmuc_capability_;
190  bool video_capability_;
191  bool camera_capability_;
192  std::string caps_node_;
193  std::string version_;
194
195  std::string sent_time_; // from the jabber:x:delay element
196};
197
198class MucPresenceStatus : public PresenceStatus {
199};
200
201} // namespace buzz
202
203
204#endif // THIRD_PARTY_LIBJINGLE_FILES_TALK_XMPP_PRESENCESTATUS_H_
205
206