/* 
 * © 2007-2008 MySQL AB, 2008-2009 Sun Microsystems, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "stdafx.h"

#include "grt_message_list.h"
#include "grt_manager.h"

using namespace bec;


MessageListBE::MessageListBE(GRTManager *grtm)
: _grtm(grtm)
{
  _error_icon= IconManager::get_instance()->get_icon_id("mini_error.png");
  _warning_icon= IconManager::get_instance()->get_icon_id("mini_warning.png");
  _info_icon= IconManager::get_instance()->get_icon_id("mini_notice.png");

  if (_grtm)
    _grtm->set_message_callback(sigc::mem_fun(this, &MessageListBE::handle_message));

  _notified= false;
}


void MessageListBE::clear()
{
  _entries.clear();
}


bool MessageListBE::get_field(const NodeId &node, int column, std::string &value)
{
  switch ((Column)column)
  {
  case Time:
    if (node[0] < (int)_entries.size())
    {
      char buffer[100];
      const struct tm *tm= localtime(&_entries[node[0]].timestamp);
      
      strftime(buffer, sizeof(buffer), "%H:%M:%S", tm);
      value= buffer;
      return true;
    }
    break;

  case Message:
    if (node[0] < (int)_entries.size())
    {
      value= _entries[node[0]].message;
      return true;
    }
    break;
  case Detail:
    if (node[0] < (int)_entries.size())
    {
      value= _entries[node[0]].detail;
      return true;
    }
  }
  return false;
}


IconId MessageListBE::get_field_icon(const NodeId &node, int column, IconSize size)
{
  if (node[0] < (int)_entries.size())
    return _entries[node[0]].icon;

  return 0;
}


int MessageListBE::count()
{
  _notified= false;
  return (int)_entries.size();
}


MenuItemList MessageListBE::get_popup_items_for_nodes(const std::vector<NodeId> &nodes)
{
  MenuItemList menu;
  MenuItem item;

  item.type= bec::MenuAction;
  item.name= "clear_messages";
  item.caption= _("Clear");
  item.enabled= true;
  menu.push_back(item);

  return menu;
}


bool MessageListBE::activate_popup_item_for_nodes(const std::string &name, const std::vector<NodeId> &nodes)
{
  if (name == "clear_messages")
  {
    clear();
    do_ui_refresh();
  }
  else
    return false;
  
  return true;
}


void MessageListBE::handle_message(const grt::Message &msg)
{
  MessageEntry entry;
  bool visible= false;

  switch (msg.type)
  {
  case grt::ErrorMsg:
    entry.icon= _error_icon; 
    visible= true;
    break;
  case grt::WarningMsg:
    entry.icon= _warning_icon;
    visible= true;
    break;
  case grt::InfoMsg: 
    entry.icon= _info_icon;
    visible= true;
    break;

  case grt::ControlMsg:
    if (msg.text == "show")
      _list_show.emit();
    return;

  default: entry.icon= 0; break;
  }
  entry.timestamp= msg.timestamp;
  entry.message= msg.text;
  std::string::size_type end= entry.message.size();
  while (end > 0 && entry.message[end-1] == '\n') --end;
  entry.message= entry.message.substr(0, end);
  entry.detail= msg.detail;
  _entries.push_back(entry);

  if (!_notified && visible)
  {
    _notified= true;
    _list_changed.emit();
  }
}
