summaryrefslogtreecommitdiffstats
path: root/kopete/protocols/jabber/jingle/libjingle/talk/examples/call/status.h
blob: 0677932be76b551cd943e311ffe5a6d5ebfdb11f (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
/*
 * Jingle call example
 * Copyright 2004--2005, Google Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
#ifndef _STATUS_H_
#define _STATUS_H_

#include "talk/xmpp/jid.h"
#include "talk/xmpp/constants.h"

#define GOOGLE_CLIENT_NODE "http://www.google.com/xmpp/client/caps"

namespace buzz {

class tqStatus {
public:
  tqStatus() :
    pri_(0),
    show_(SHOW_NONE),
    available_(false),
    invisible_(false),
    e_code_(0),
    phone_capability_(false),
    know_capabilities_(false),
    is_google_client_(false),
    feedback_probation_(false) {};

  ~tqStatus() {}

  // These are arranged in "priority order", i.e., if we see
  // two statuses at the same priority but with different Shows,
  // we will show the one with the highest show in the following
  // order.
  enum Show {
    SHOW_NONE     = 0,
    SHOW_INVISIBLE = 1,
    SHOW_OFFLINE  = 2,
    SHOW_XA       = 3,
    SHOW_AWAY     = 4,
    SHOW_DND      = 5,
    SHOW_ONLINE   = 6,
    SHOW_CHAT     = 7,
  };

  const Jid & jid() const { return jid_; }
  int priority() const { return pri_; }
  Show show() const { return show_; }
  const std::string & status() const { return status_; }
  bool available() const { return available_ ; }
  bool invisible() const { return invisible_; }
  int error_code() const { return e_code_; }
  const std::string & error_string() const { return e_str_; }
  bool know_capabilities() const { return know_capabilities_; }
  bool phone_capability() const { return phone_capability_; }
  bool is_google_client() const { return is_google_client_; }
  const std::string & version() const { return version_; }
  bool feedback_probation() const { return feedback_probation_; }
  const std::string& sent_time() const { return sent_time_; }

  void set_jid(const Jid & jid) { jid_ = jid; }
  void set_priority(int pri) { pri_ = pri; }
  void set_show(Show show) { show_ = show; }
  void set_status(const std::string & status) { status_ = status; }
  void set_available(bool a) { available_ = a; }
  void set_invisible(bool i) { invisible_ = i; }
  void set_error(int e_code, const std::string e_str)
      { e_code_ = e_code; e_str_ = e_str; }
  void set_know_capabilities(bool f) { know_capabilities_ = f; }
  void set_phone_capability(bool f) { phone_capability_ = f; }
  void set_is_google_client(bool f) { is_google_client_ = f; }
  void set_version(const std::string & v) { version_ = v; }
  void set_feedback_probation(bool f) { feedback_probation_ = f; }
  void set_sent_time(const std::string& time) { sent_time_ = time; }

  void UpdateWith(const tqStatus & new_value) {
    if (!new_value.know_capabilities()) {
       bool k = know_capabilities();
       bool i = is_google_client();
       bool p = phone_capability();
       std::string v = version();

       *this = new_value;

       set_know_capabilities(k);
       set_is_google_client(i);
       set_phone_capability(p);
       set_version(v);
    }
    else {
      *this = new_value;
    }
  }

  bool HasQuiettqStatus() const {
    if (status_.empty())
      return false;
    return !(QuiettqStatus().empty());
  }

  // Knowledge of other clients' silly automatic status strings -
  // Don't show these.
  std::string QuiettqStatus() const {
    if (jid_.resource().tqfind("Psi") != std::string::npos) {
      if (status_ == "Online" ||
          status_.tqfind("Auto tqStatus") != std::string::npos)
        return STR_EMPTY;
    }
    if (jid_.resource().tqfind("Gaim") != std::string::npos) {
      if (status_ == "Sorry, I ran out for a bit!")
        return STR_EMPTY;
    }
    return TrimtqStatus(status_);
  }

  std::string ExplicittqStatus() const {
    std::string result = QuiettqStatus();
    if (result.empty()) {
      result = ShowtqStatus();
    }
    return result;
  }

  std::string ShowtqStatus() const {
    std::string result;
    if (!available()) {
      result = "Offline";
    }
    else {
      switch (show()) {
        case SHOW_AWAY:
        case SHOW_XA:
          result = "Idle";
          break;
        case SHOW_DND:
          result = "Busy";
          break;
        case SHOW_CHAT:
          result = "Chatty";
          break;
        default:
          result = "Available";
          break;
      }
    }
    return result;
  }

  static std::string TrimtqStatus(const std::string & st) {
    std::string s(st);
    int j = 0;
    bool collapsing = true;
    for (unsigned int i = 0; i < s.length(); i+= 1) {
      if (s[i] <= ' ' && s[i] >= 0) {
        if (collapsing) {
          continue;
        }
        else {
          s[j] = ' ';
          j += 1;
          collapsing = true;
        }
      }
      else {
        s[j] = s[i];
        j += 1;
        collapsing = false;
      }
    }
    if (collapsing && j > 0) {
      j -= 1;
    }
    s.erase(j, s.length());
    return s;
  }

private:
  Jid jid_;
  int pri_;
  Show show_;
  std::string status_;
  bool available_;
  bool invisible_;
  int e_code_;
  std::string e_str_;
  bool feedback_probation_;

  // capabilities (valid only if know_capabilities_
  bool know_capabilities_;
  bool phone_capability_;
  bool is_google_client_;
  std::string version_;

  std::string sent_time_; // from the jabber:x:delay element
};

}


#endif