Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members  

jabberoo-presence.cpp

00001 /* jabberoo-presence.cc
00002  * Jabber Presence
00003  *
00004  * Original Code Copyright (C) 1999-2001 Dave Smith (dave@jabber.org)
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or (at your option) any later version.
00010  * 
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  * 
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  *
00020  * Contributor(s): Julian Missig
00021  *
00022  * This Original Code has been modified by IBM Corporation. Modifications 
00023  * made by IBM described herein are Copyright (c) International Business 
00024  * Machines Corporation, 2002.
00025  *
00026  * Date             Modified by     Description of modification
00027  * 01/20/2002       IBM Corp.       Updated to libjudo 1.1.1
00028  * 2002-03-05       IBM Corp.       Updated to libjudo 1.1.5
00029  */
00030 
00031 
00032 #include <presence.hh>
00033 
00034 namespace jabberoo
00035 {
00036 Presence::Presence(const judo::Element& t)
00037      : Packet(t)
00038 {
00039      // Determine the presence type
00040      _type = translateType(t.getAttrib("type"));
00041      // Determine the show value
00042      _show = translateShow(_type, t.getChildCData("show"));
00043 }
00044 
00045 Presence::Presence(const std::string& jid, Presence::Type ptype, Presence::Show stype, const std::string& status, const std::string& priority)
00046      : Packet("presence")
00047 {
00048      if (!jid.empty())
00049           setTo(jid);
00050      setType(ptype);
00051      setStatus(status);
00052      setShow(stype);
00053      setPriority(priority);
00054 }
00055 
00056 void Presence::setType(Presence::Type ptype)
00057 {
00058      if (ptype != Presence::ptAvailable)
00059           _base.putAttrib("type", translateType(ptype));
00060      _type = ptype;
00061 }
00062 
00063 void Presence::setStatus(const std::string& status)
00064 {
00065      if (!status.empty())
00066           _base.addElement("status", status);
00067 }
00068 
00069 void Presence::setShow(Presence::Show stype)
00070 {
00071      if (stype > stOnline)
00072      {
00073           _base.addElement("show", translateShow(stype));
00074      }
00075      _show = stype;
00076 }
00077 
00078 void Presence::setPriority(const std::string& priority)
00079 {
00080      if (priority != "0")
00081      {
00082           _base.addElement("priority", priority);
00083      }
00084      _priority = atoi(priority.c_str());
00085 }
00086 
00087 Presence::Type Presence::getType() const
00088 {
00089      return _type;
00090 }
00091 
00092 const std::string Presence::getStatus() const
00093 {
00094      if (_base.getChildCData("status").empty())
00095           return _base.getChildCData("error");
00096      else
00097           return _base.getChildCData("status");
00098 }
00099 
00100 Presence::Show Presence::getShow() const
00101 {
00102      return _show;
00103 }
00104 
00105 const std::string Presence::getShow_str() const
00106 {
00107      return translateShow(_show);
00108 }
00109 
00110 int Presence::getPriority() const
00111 {
00112      if (_type == Presence::ptUnavailable)
00113           return -1;
00114      else
00115           return atoi(_base.getChildCData("priority").c_str());
00116 }
00117 
00118 std::string Presence::translateType(Type ptype)
00119 {
00120      switch(ptype)
00121      {
00122      case ptAvailable: 
00123           return std::string("");
00124      case ptUnavailable: 
00125           return std::string("unavailable");
00126      case ptSubRequest: 
00127           return std::string("subscribe");
00128      case ptUnsubRequest: 
00129           return std::string("unsubscribe");
00130      case ptSubscribed : 
00131           return std::string("subscribed");
00132      case ptUnsubscribed:
00133           return std::string("unsubscribed");
00134      case ptError:
00135           return std::string("error");
00136      case ptInvisible:
00137           return std::string("invisible");
00138      }
00139      return std::string("");
00140 }
00141 
00142 std::string Presence::translateShow(Show stype)
00143 {
00144      switch(stype)
00145      {
00146      case stInvalid:
00147           return std::string("");
00148      case stOnline:
00149           return std::string("online");
00150      case stOffline:
00151           return std::string("offline");
00152      case stChat:
00153           return std::string("chat");
00154      case stAway:
00155           return std::string("away");
00156      case stXA:
00157           return std::string("xa");
00158      case stDND:
00159           return std::string("dnd");
00160      }
00161      return std::string("");
00162 }
00163 
00164 Presence::Type Presence::translateType(const std::string& ptype)
00165 {
00166      // Determine what type of presence packet this is
00167      if (ptype.empty())
00168           return ptAvailable;
00169      else if (ptype == "subscribe")
00170           return ptSubRequest;
00171      else if (ptype == "unsubscribe")
00172           return ptUnsubRequest;
00173      else if (ptype == "subscribed")
00174           return ptSubscribed;
00175      else if (ptype == "unsubscribed")
00176           return ptUnsubscribed;
00177      else if (ptype == "error")
00178           return ptError;
00179      else if (ptype == "invisible")
00180           return ptInvisible;
00181      else
00182           return ptUnavailable;
00183 }
00184 
00185 Presence::Show Presence::translateShow(Type ptype, const std::string& show)
00186 {
00187      // Determine what value show should return
00188      if (ptype == ptError)
00189           return stOffline;
00190      else if ((ptype == ptAvailable) && (show.empty()))
00191           return stOnline;
00192      else if ((ptype == ptAvailable) && (show == "online"))
00193           return stOnline;
00194      else if ((ptype == ptUnavailable) && (show.empty()))
00195           return stOffline;
00196      else if ((ptype == ptUnavailable) && (show == "offline"))
00197           return stOffline;
00198      else if (show == "chat")
00199           return stChat;
00200      else if (show == "away")
00201           return stAway;
00202      else if (show == "xa")
00203           return stXA;
00204      else if (show == "dnd")
00205           return stDND;
00206      else if (ptype == ptAvailable) // Maybe we should return stOnline?
00207           return stInvalid;         // Only way this could occur is if it's and unknown <show>
00208      else
00209           return stOffline;
00210 }
00211 
00212 } // namespace jabberoo

Generated on Thu Jul 24 13:31:50 2003 for jabberoo by doxygen1.3-rc3