00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <vos/corelibs/vos/vos.hh>
00022 #include <vos/metaobjects/property/property.hh>
00023 #include <vos/metaobjects/misc/avatar.hh>
00024 #include <vos/metaobjects/misc/talkative.hh>
00025 #include <vos/gui/gui/gui.hh>
00026 #include "privmsg.hh"
00027
00028
00029 #define CREATEOBJ(SITE, NAME, TYPE) meta_cast<TYPE*>((SITE)->createMetaObject(NAME, typeid(TYPE).name(), 0))
00030
00031 using namespace VOSGUI;
00032 using namespace VOSChatGUI;
00033
00034 PrivMsg::PrivMsg(MetaObject* s) : LocalContainer(s), Container(s),
00035 LocalWidget(s), Widget(s), MetaObject(s) {
00036 newMessagePos = -1;
00037 }
00038
00039 PrivMsg::~PrivMsg() {
00040 if(&sender)
00041 sender->removeTalkListener(this);
00042 if(&messages)
00043 messages->selectedObjectList()->removeChildListener(this);
00044 }
00045
00046 MetaObject* PrivMsg::new_PrivMsg(MetaObject* s, const string& type) {
00047 return new PrivMsg(s);
00048 }
00049
00050 void PrivMsg::registerExtenders() {
00051 LocalSite::addLocalObjectExtension(typeid(PrivMsg).name(), &PrivMsg::new_PrivMsg);
00052 LocalSite::addLocalObjectExtension(typeid(SendButton).name(), &SendButton::new_SendButton);
00053 }
00054
00055 void PrivMsg::setup(LocalTalkative* newSender, Talkative* newTarget) {
00056
00057 target = newTarget;
00058 sender = newSender;
00059
00060 sender->addTalkListener(this);
00061
00062 LOG("PrivMsg", 2, "setup: sender is " << sender->getURL().getString() << ". target is " << target->getURL().getString());
00063
00064 vRef<LocalSite> site = meta_cast<LocalSite*>(&getSite());
00065
00066 messages = CREATEOBJ(site, "GUI.PrivMsg.messages", SelectList);
00067 messages->initialize();
00068 messages->setAlignment("fill");
00069 messages->setProportion(3);
00070 messages->selectedObjectList()->addChildListener(this);
00071
00072 insertChild(-1, "messages", &messages);
00073
00074 vRef<Container> replybox = CREATEOBJ(site, "GUI.PrivMsg.replybox", Container);
00075 replybox->initialize();
00076 replybox->setBorder(true);
00077 replybox->setLabel("");
00078 replybox->setAlignment("fill");
00079 insertChild(-1, "replybox", &replybox);
00080
00081 replyInfo = CREATEOBJ(site, "GUI.PrivMsg.replyInfo", Property);
00082 replyInfo->initialize();
00083 vRef<Label> replylabel = CREATEOBJ(site, "GUI.PrivMsg.replylabel", Label);
00084 replylabel->initialize();
00085 replylabel->setLabelObj(&replyInfo);
00086 replybox->insertChild(-1, "label", &replylabel);
00087
00088 messageText = CREATEOBJ(site, "GUI.PrivMsg.messageText", Property);
00089 messageText->initialize();
00090 vRef<Input> input = CREATEOBJ(site, "GUI.PrivMsg.input", Input);
00091 input->initialize();
00092 input->setAlignment("fill");
00093 input->setTargetObj(&messageText);
00094 replybox->insertChild(-1, "input", &input);
00095
00096 vRef<Container> c = CREATEOBJ(site, "GUI.PrivMsg.a_random_container_of_little_consequence", Container);
00097 c->initialize();
00098 c->setAlignment("fill");
00099 c->setLayout("horizontal");
00100
00101
00102 vRef<NumericInput> prior = CREATEOBJ(site, "GUI.PrivMsg.priority", NumericInput);
00103 prior->initialize();
00104 prior->setTarget("0");
00105 prior->setRange(-10, 10);
00106 prior->setIncrement(1);
00107 prior->setAlignment("left");
00108 prior->setLabel("Priority: ");
00109 c->insertChild(-1, "priority", &prior);
00110
00111 priority = prior->getTargetObj();
00112
00113 vRef<SendButton> send = CREATEOBJ(site, "GUI.PrivMsg.send", SendButton);
00114 send->initialize(&NoPropertyAccessControl::static_);
00115 send->setup(this);
00116 send->setAlignment("fill");
00117 send->setIsDefaultButton();
00118 c->insertChild(-1, "send", &send);
00119
00120 replybox->insertChild(-1, "buttons", &c);
00121
00122 }
00123
00124
00125 void PrivMsg::SendButton::push(const string&) {
00126 assert(&data);
00127 assert(&(data->messageText));
00128 assert(&(data->priority));
00129
00130 TalkMessage m(*data->sender);
00131
00132
00133 string pr;
00134 data->priority->read(pr);
00135 m.priority = strtoul(pr.c_str(), NULL, 10);
00136 m.scope = Talkative::PRIVATE;
00137 vRef<Vobject> selected = data->messages->getFirstSelected();
00138 if(&selected) {
00139 vRef<Property> nonce = meta_cast<Property*>(&(selected->findObject("nonce")));
00140 if(&nonce)
00141 nonce->read(m.reply_nonce);
00142 }
00143 m.time = time(NULL);
00144 assert(&(data->target));
00145 LOG("PrivMsg", 2, "Sending message: " << m.text << " with reply_nonce=" << m.reply_nonce << " and priority=" << m.priority);
00146 data->target->do_say(m);
00147 }
00148
00149 void PrivMsg::notifyChildReplaced(VobjectEvent& ev) {
00150 notifyChildRemoved(ev);
00151 notifyChildInserted(ev);
00152 }
00153
00154 void PrivMsg::notifyChildInserted(VobjectEvent& e) {
00155
00156 LOG("PrivMsg", 2, "Message selected.");
00157
00158
00159
00160 string name = "(unknown)";
00161 vRef<Vobject> sndr = e.getChild()->findObject("sender");
00162 if(&sndr) {
00163 Avatar* a = meta_cast<Avatar*>(&sndr);
00164 if(a)
00165 name = a->getNick();
00166 else
00167 name = sndr->getName();
00168 }
00169
00170 vRef<Vobject> tobj = e.getChild()->findObject("time");
00171 Property* p = meta_cast<Property*>(&tobj);
00172 char time[24] ="\?\?/\?\?/\?\? \?\?:\?\?:\?\?";
00173 if(p) {
00174 time_t t = strtol(p->read().c_str(), NULL, 10);
00175 struct tm* ts = localtime(&t);
00176 snprintf(time, sizeof(time), "%d/%d/%d %d:%2d:%2d",
00177 ts->tm_mon, ts->tm_mday, ts->tm_year,
00178 ts->tm_hour, ts->tm_min, ts->tm_sec);
00179 }
00180
00181 replyInfo->replace( string("Reply to ") + name + "'s mesage at " + time + ":");
00182
00183 vRef<Vobject> pobj = e.getChild()->findObject("priority");
00184 Property* r = meta_cast<Property*>(&pobj);
00185 if(r) {
00186 LOG("PrivMsg", 2, "Priority of the selected message was: " << r->read());
00187 priority->replace(r->read());
00188 }
00189
00190 }
00191
00192 void PrivMsg::notifyChildRemoved(VobjectEvent& e) {
00193
00194 replyInfo->replace("");
00195 }
00196
00197
00198 void PrivMsg::notifyTalkMessage(TalkMessage& m) {
00199
00200 vRef<LocalSite> site = meta_cast<LocalSite*>(&getSite());
00201
00202 vRef<Property> msg = CREATEOBJ(site, "GUI.PrivMsg.message", Property);
00203 msg->initialize();
00204 string name = "";
00205 Avatar* a = meta_cast<Avatar*>(&m.sender);
00206 if(a)
00207 name = a->getNick();
00208 else
00209 name = m.sender.getName();
00210 msg->replace( name + ": " + m.text, "text/plain");
00211
00212 vRef<Property> time = CREATEOBJ(site, "GUI.PrivMsg.message.time", Property);
00213 time->initialize();
00214 char t[64];
00215 snprintf(t, sizeof(t), "%ld", m.time);
00216 time->replace(t, "text/x-int");
00217
00218 vRef<Property> priority = CREATEOBJ(site, "GUI.PrivMsg.message.priority", Property);
00219 priority->initialize();
00220 char p[24];
00221 snprintf(p, sizeof(p), "%u", m.priority);
00222 priority->replace(p, "text/x-int");
00223
00224 vRef<Property> nonce = CREATEOBJ(site, "GUI.PrivMsg.message.nonce", Property);
00225 nonce->initialize();
00226 nonce->replace(m.this_nonce);
00227
00228 msg->insertChild(-1, "sender", &(m.sender));
00229 msg->insertChild(-1, "time", &time);
00230 msg->insertChild(-1, "priority", &priority);
00231 msg->insertChild(-1, "nonce", &nonce);
00232 messages->add(&msg, "message", newMessagePos);
00233 }
00234
00235 void PrivMsg::setTarget(Talkative* newTarget) {
00236 target = newTarget;
00237 }
00238
00239 void PrivMsg::setMessagesAtTop(bool v) {
00240 newMessagePos = v ? 0 : -1;
00241 }
00242
00243 void PrivMsg::setMessagesAtBottom(bool v) {
00244 newMessagePos = v ? -1 : 0;
00245 }
00246
00247
00248 void PrivMsg::notifyObjectExcise(RefCounted* obj) {
00249 LOG("PrivMsg", 0, "NOT IMPLEMENTED: object excise");
00250
00251 }
00252
00253