######################################################################
##
## Copyright (c) LIFL 1999
## The GOODE team of the LIFL
## University of Lille, France
## All rights reserved
##
## The CorbaWeb script associated to the OMG IDL ChatService::Chat
## interface.
##
######################################################################

# Each action must have two arguments: the first one refers to the
# current CORBA object and the second one refers to a CorbaWeb
# ActionContext instance.

######################################################################
## This action generates forms to execute ChatService::Chat operations.
######################################################################

proc view(chat, ac)
{
  name = chat.name

  # generates <h1>...</h1>
  ac.h1("The chat <b>" + name + "</b> description is " + chat.description)
  ac.h1("Current connected persons")

  # gets present nicknames
  nicknames = chat.list_nicknames()

  ac.println("<UL>")
  for nn in nicknames {
    ac.println("<LI> " + nn)
  }
  ac.println("</UL>")

  # generates a HTML form referring the do_subscribe action.
  ac.do_form(chat, "do_subscribe")
    # generates the submit button.
    ac.submit_button("Subscribes")
    ac.print("with the nickname ")
    ac.text_input("a_nickname", 20)
  ac.end_form()
}

######################################################################
## This action executes the subscribe operation on a ChatService::Chat.
######################################################################

proc do_subscribe(chat, ac)
{
  # gets the form variable "a_nickname".
  a_nickname = ac.getString("a_nickname")

  if (a_nickname == "") {
    ac._reply.setContentType("text/html")
    ac.h1("ERROR: Empty nickname!")
    ac.hr()
    view(chat, ac)
    return
  }

  try {
    proxy = chat.subscribe(a_nickname, ChatService.User._nil)
  } catch(ChatService.AlreadyUsed e) {
    ac._reply.setContentType("text/html")
    ac.h1("ERROR: Nickname <b>" + a_nickname + "</b> is already used!")
    ac.hr()
    view(chat, ac)
    return
  }
  
  ac._reply.setContentType("text/html")

  ac.println("<html>")
  ac.println("<frameset rows=\"50%,50%\">")
  ac.println("<frameset cols=\"50%,50%\">")
  ac.println("<frame src=\"" + ac.getActionURL(chat, "do_list") + "\">")
  ac.println("<frame src=\"" + ac.getDoURL(proxy, "output_frame") + "\">")
  ac.println("</frameset>")
  ac.println("<frame src=\"" + ac.getActionURL(proxy, "input_frame") + "\">")
  ac.println("</frameset>")
  ac.println("</html>")
}

proc do_list(chat, ac)
{
  name = chat.name

  # generates <h1>...</h1>
  ac.h1("The chat <b>" + name + "</b> description is " + chat.description)
  ac.h1("Current connected persons")

  # gets present nicknames
  nicknames = chat.list_nicknames()

  ac.println("<UL>")
  for nn in nicknames {
    ac.println("<LI> " + nn)
  }
  ac.println("</UL>")

  # generates a HTML form referring the do_list action.
  ac.action_form(chat, "do_list")
    # generates the submit button.
    ac.submit_button("Lists")
  ac.end_form()
}

######################################################################
## Declaration of these actions into the CorbaWeb ActionRepository.
######################################################################

declareAction("view", view, "Views forms to act on the ChatService::Chat object")
declareAction("do_subscribe", do_subscribe, "Subscribe a chat")
declareAction("do_list", do_list, "List a chat")

######################################################################
# end of file 'CorbaWeb/actions/ChatService_Chat'
######################################################################
