Logo Search packages:      
Sourcecode: kmess version File versions

contactsidebar.cpp

/***************************************************************************
                          contactsidebar.cpp  -  description
                             -------------------
    begin                : Thu Jan 16 2003
    copyright            : (C) 2003 by Mike K. Bennett
    email                : mkb137b@hotmail.com
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "contactsidebar.h"

#include <qglobal.h>
#include <qlayout.h>
#include <qscrollview.h>
#include <qvbox.h>
#include <qobjectlist.h>

#include <kdebug.h>

#include "../contact/contactbase.h"
#include "../contact/contactdrag.h"
#include "../currentaccount.h"
#include "../kmessdebug.h"
#include "contactframe.h"

// The constructor
ContactSidebar::ContactSidebar(QWidget *parent, const char *name )
 : QWidget(parent, name)
{
  contactFrames_.setAutoDelete( true );

  setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred, false);  // Stretch widget

  // Create a layout within the scrollview so that added widgets are laid out nicely
  viewBox_ = new QVBoxLayout( this, 0, -1, "viewBox" );
//  viewBox_->setResizeMode( QLayout::Minimum );

  // Configure this QScrollView
  setAcceptDrops( true );                       // Allow drag&drop

  // put the viewBoxHolder (and hence the layout holding the frames) into the viewport
  viewBox_->addItem( new QSpacerItem(20, 20, QSizePolicy::Ignored, QSizePolicy::Expanding) );
}



// The destructor
ContactSidebar::~ContactSidebar()
{
}



// A contact joined the chat
void ContactSidebar::contactJoined(QString handle)
{
  ContactBase *contact;
  ContactFrame *contactFrame;
  // See if the contact already has a frame
  contactFrame = getContactFrameByHandle( handle );
  if ( contactFrame != 0 )
  {
    // Reactivate the contact's frame
    contactFrame->reactivate();
    return;
  }

  // Get the given contact
  contact = CurrentAccount::instance()->getContactByHandle( handle );
  if( KMESS_NULL(contact) ) return;

  // Get the first available contact frame
  contactFrame = getFirstInactiveContactFrame();
  if( KMESS_NULL(contactFrame) ) return;

  // Activate the frame
  contactFrame->activate( contact );
}



// A contact left the chat
void ContactSidebar::contactLeft(QString handle)
{
  ContactFrame *contactFrame = getContactFrameByHandle( handle );

  if ( contactFrame != 0 )
  {
    // deactivate (grey out) the contact frame
    contactFrame->deactivate();

    // the following code only works correctly on Qt > 3.1.0
    #if (QT_VERSION >= 0x030100)
    // frame is deactivated, so remove it from the layout
    viewBox_->remove( contactFrame );
    // and add it to the end of the layout
    viewBox_->insertWidget( children()->count() - 2, contactFrame );
    // ... this means that contacts in the chat move to the top,
    //  while others go to the bottom.
    #endif
  }
}



// A contact is typing
void ContactSidebar::contactTyping(QString handle, QString)
{
  ContactFrame *contactFrame = getContactFrameByHandle( handle );
  if ( contactFrame != 0 )
  {
    contactFrame->startTyping();
  }
}



// do we want this drop?
void ContactSidebar::contentsDropEvent( QDropEvent *event )
{
  if ( event->provides( ContactDragInfo::mimeType ) )
  {
    const ContactBase *contact = CurrentAccount::instance()->getContactLastDragged();
    if(contact != 0)
    {
      emit inviteContact( contact->getHandle() );
    }
  }

  event->ignore();
}



// something being dragged has entered our widget
void ContactSidebar::contentsDragEnterEvent( QDragEnterEvent *event )
{
  if ( event->provides( ContactDragInfo::mimeType ) )
  {
    event->accept( true );
  }
}



// something being dragged has been moved in our widget
void ContactSidebar::contentsDragMoveEvent( QDragMoveEvent *event )
{
  if ( event->provides( ContactDragInfo::mimeType ) )
  {
    event->acceptAction( true );
  }
}



// Find the contact frame with the given handle
ContactFrame* ContactSidebar::getContactFrameByHandle(const QString& handle)
{
  for ( ContactFrame *contactFrame = contactFrames_.first(); contactFrame; contactFrame = contactFrames_.next() )
  {
    if ( contactFrame->getHandle() == handle )
    {
      return contactFrame;
    }
  }
  return 0;
}



// Return the first inactive contact frame
ContactFrame* ContactSidebar::getFirstInactiveContactFrame()
{
  ContactFrame *contactFrame;
  for ( ContactFrame *contactFrame = contactFrames_.first(); contactFrame; contactFrame = contactFrames_.next() )
  {
    if ( !contactFrame->isActivated() )
    {
      return contactFrame;
    }
  }

  // Well we don't have any more, so make another.
  contactFrame = new ContactFrame( this, "ContactFrame" );
  contactFrame->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Preferred, false);

  // add it to the viewBox so it appears in the sidebar
  viewBox_->insertWidget( children()->count() - 2, contactFrame );

  // put it in the list of frames so we can find it again.
  contactFrames_.append( contactFrame );

  return contactFrame;
}



// Initialize the object
bool ContactSidebar::initialize()
{
  // The important frame-creating code has to go in the constructor, so there's less to initialize, here.

  return true;
}



// A message was received from one of the contacts... notify its frame
void ContactSidebar::messageReceived(const QString& handle)
{
  ContactFrame *frame;
  frame = getContactFrameByHandle( handle );
  if ( frame != 0 )
  {
    frame->messageReceived();
  }
}


#include "contactsidebar.moc"

Generated by  Doxygen 1.6.0   Back to index