00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #include <presence.hh>
00033
00034 namespace jabberoo
00035 {
00036 Presence::Presence(const judo::Element& t)
00037 : Packet(t)
00038 {
00039
00040 _type = translateType(t.getAttrib("type"));
00041
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
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
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)
00207 return stInvalid;
00208 else
00209 return stOffline;
00210 }
00211
00212 }