/***************************************************************************
 *   Copyright (C) 2005 by the G System Team                               *
 *   http://www.g-system.at                                                *
 *                                                                         *
 *   Permission is hereby granted, free of charge, to any person obtaining *
 *   a copy of this software and associated documentation files (the       *
 *   "Software"), to deal in the Software without restriction, including   *
 *   without limitation the rights to use, copy, modify, merge, publish,   *
 *   distribute, sublicense, and/or sell copies of the Software, and to    *
 *   permit persons to whom the Software is furnished to do so, subject to *
 *   the following conditions:                                             *
 *                                                                         *
 *   The above copyright notice and this permission notice shall be        *
 *   included in all copies or substantial portions of the Software.       *
 *                                                                         *
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       *
 *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    *
 *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*
 *   IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR     *
 *   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
 *   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
 *   OTHER DEALINGS IN THE SOFTWARE.                                       *
 ***************************************************************************/

#include "GCommunicationWidget.h"

#include <qframe.h>
#include <qlayout.h>
#include <qsplitter.h>
#include <qtimer.h>
#include <qtooltip.h>
#include <qgroupbox.h>

#include <klistbox.h>
#include <klineedit.h>
#include <ktextedit.h>

GCommunicationWidget::GCommunicationWidget(QWidget* parent, const char* name, WFlags f)
  : QWidget(parent, name, f)
{
  QGridLayout* mainlayout = new QGridLayout(this,1,1,0,0,"mainlayout");
  
  QSplitter* serverlistsplitter = new QSplitter(Qt::Horizontal,this,"serverlistsplitter");
  
  mainlayout->addWidget(serverlistsplitter,0,0);
  
  //set up some GUI
//   KListBox* listview = new KListBox(NULL,"serverlistview");
//   listview->setAcceptDrops(false);
//   listview->setItemsMovable(false);
//   listview->setItemsRenamable(false);
  
  QGroupBox* commbox = new QGroupBox(serverlistsplitter,"Communication");
  QGridLayout* commlayout = new QGridLayout(commbox,2,1,2,2,"commboxlayout");
  
  QSplitter* internalexternalsplitter = new QSplitter(Qt::Vertical,serverlistsplitter,"internalexternalsplitter");
  
  InternalServerList = new KListBox(internalexternalsplitter,"internalserverlist");
  
  ExternalServerList = new KListBox(internalexternalsplitter,"externalserverlist");
  
  this->MessageHistory = new KTextEdit(commbox,"commmessagehistory");
  MessageHistory->setReadOnly(true);
  MessageHistory->append("Communication Log");
  
  QSplitter* sendsplitter = new QSplitter(Qt::Horizontal,commbox,"sendsplitter");
  
  this->MessageDestination = new KLineEdit("<destination> (XMPP JID)",sendsplitter,"commdestfield");
  this->MessageText = new KLineEdit("<message> (press enter to send)",sendsplitter,"commmessagefield");
  
  commlayout->addWidget(MessageHistory,0,0);
  commlayout->addWidget(sendsplitter,1,0);
  
  connect(MessageText,SIGNAL(returnPressed()),this,SLOT(prepareMessageForSending()));
  
  connect(InternalServerList,SIGNAL(selected( const QString& )),MessageDestination,SLOT(setText( const QString& )));
  connect(InternalServerList,SIGNAL(selected( const QString& )),MessageText,SLOT(setFocus()));
  
  connect(ExternalServerList,SIGNAL(selected( const QString& )),MessageDestination,SLOT(setText( const QString& )));
  connect(ExternalServerList,SIGNAL(selected( const QString& )),MessageText,SLOT(setFocus()));
  
  QToolTip::add(InternalServerList,"Lists all known internal contacts, double click to set as current communication partner");
  QToolTip::add(ExternalServerList,"Lists all known external contacts, double click to set as current communication partner");
  QToolTip::add(MessageHistory,"Communication history");
  QToolTip::add(MessageDestination,"The XMPP JID address of the communication partner, use * to broadcast");
  QToolTip::add(MessageText,"The message that is sent to the communication partner, press Enter to send");
}


GCommunicationWidget::~GCommunicationWidget()
{
}

void GCommunicationWidget::replaceInternalContactList(QStringList contacts)
{
  this->clearInternalContacts();
  this->InternalServerList->insertStringList(contacts);
}

void GCommunicationWidget::replaceExternalContactList(QStringList contacts)
{
  this->clearExternalContacts();
  this->ExternalServerList->insertStringList(contacts);
}

void GCommunicationWidget::clearInternalContacts()
{
  this->InternalServerList->clear();
}

void GCommunicationWidget::clearExternalContacts()
{
  this->ExternalServerList->clear();
}

void GCommunicationWidget::addInternalContact(QString contact)
{
  this->InternalServerList->insertItem(contact);
}

void GCommunicationWidget::addExternalContact(QString contact)
{
  this->ExternalServerList->insertItem(contact);
}

void GCommunicationWidget::removeInternalContact(QString contact)
{
  QListBoxItem* item = this->InternalServerList->findItem(contact);
  if (item)
    this->InternalServerList->removeItem(InternalServerList->index(item));
}

void GCommunicationWidget::removeExternalContact(QString contact)
{
  QListBoxItem* item = this->ExternalServerList->findItem(contact);
  if (item)
    this->ExternalServerList->removeItem(ExternalServerList->index(item));
}

void GCommunicationWidget::updateContactState(QString contact, bool available, bool internal)
{
  if (!available)
  {
    this->removeExternalContact(contact);
    this->removeInternalContact(contact);
  }
  else
  {
    if (internal)
    {
      this->removeExternalContact(contact);
      if (this->InternalServerList->findItem(contact)==NULL)
        this->addInternalContact(contact);
    }
    else
    {
      this->removeInternalContact(contact);
      if (this->ExternalServerList->findItem(contact)==NULL)
        this->addExternalContact(contact);
    }
  }
}

//END CONTACT MANAGEMENT

//BEGIN MESSAGE MANAGEMENT

void GCommunicationWidget::prepareMessageForSending()
{
  if (this->MessageText->text().isEmpty() || this->MessageDestination->text().isEmpty())
  {
    qWarning("Not sending message, message text and/or destination empty.");
    return;
  }
    
  QString dest = this->MessageDestination->text();
  QString msg = this->MessageText->text();
  
  qDebug("Sending User Message:");
  qDebug(QString(" destination: %1").arg(dest));
  qDebug(QString(" message: %1").arg(msg));
  
  if (dest=="*")
  {
      //broadcast message
    int n = this->InternalServerList->numRows();
    for (int i=0; i<n; i++)
    {
      QString destination = this->InternalServerList->text(i);
      if (!destination.isEmpty())
        emit this->sendMessage(msg,destination);
    }
    
//     n = this->ExternalServerList->numRows();
//     for (int i=0; i<n; i++)
//     {
//       QString destination = this->ExternalServerList->text(i);
//       if (!destination.isEmpty())
//         emit this->sendMessage(msg,destination);
//     }
      
    this->MessageHistory->append(QString("SENT TO ALL INTERNAL: ") + msg);
    this->MessageText->clear();
  }
  else
  {
    //ordinary mssage
    emit this->sendMessage(msg,dest);
    this->MessageHistory->append(QString("SENT TO ") + dest + ": " + msg);
    this->MessageText->clear();
  }
}

void GCommunicationWidget::receiveMessage(QString message, QString from, bool internal)
{
  QString m;
  if (!internal)
    m.append("EXTERNAL: ");
  m.append(from);
  m.append(": ");
  m.append(message);
  this->MessageHistory->append(m);
}

//END MESSAGE MANAGEMENT


syntax highlighted by Code2HTML, v. 0.9.1