/***************************************************************************
 *   Copyright (C) 2004 by Massimo Callegari                               *
 *   massimocallegari@yahoo.it                                             *
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 *   This program 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 General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

// Qt includes
#include <qapplication.h>
#include <qlabel.h>
#include <qtooltip.h>
#include <qstring.h>
#include <qwidget.h>
#include <qlayout.h>
#include <qpainter.h>
#include <qheader.h>
#include <qvaluelist.h>
#include <qpixmap.h>
#include <qtoolbutton.h>
#include <qtoolbar.h>
#include <qdir.h>
#include <qpushbutton.h>

// KDE includes
#include <klibloader.h>
#include <kinstance.h>
#include <klocale.h>
#include <kiconloader.h>
#include <kmessagebox.h>
#include <kdialogbase.h>
#include <kcombobox.h>
#include <ktoolbarbutton.h>
#include <kaction.h>
#include <kactionclasses.h>
#include <kconfigdialog.h>

// plugin specific includes
// #include "smb4k_konqplugin.moc"
#include "smb4k_konqplugin.h"
#include "../smb4k/core/smb4kcore.h"
#include "../smb4k/core/smb4kdefs.h"

smb4kWidget::smb4kWidget( QWidget * parent, const char * name, WFlags f )
                 : QVBox(parent, name, f)
{
 // Nothing exciting to do !
}

KonqSidebar_Smb4K::KonqSidebar_Smb4K(KInstance *inst,QObject *parent,QWidget *widgetParent, QString &desktopName, const char* name):
                   KonqSidebarPlugin(inst,parent,widgetParent,desktopName,name)
{

  widget = new smb4kWidget( widgetParent );

  KToolBar  *topBar = new KToolBar( widget, "Topbar" );
  topBar->setIconSize(16);
  topBar->insertButton( "reload",    0, SIGNAL( clicked() ), this, SLOT( slotRescan() ) , TRUE, i18n( "Scan Network" ) );
  topBar->insertButton( "find",      1, SIGNAL( clicked() ), this, SLOT( slotSearch() ) , TRUE,  i18n( "Search" ) );
  topBar->insertButton( "configure", 2, SIGNAL( clicked() ), this, SLOT( slotSmb4KOptionsDlg() ) , TRUE,  i18n( "Configure" ) );

  //
  // Browser widget:
  //
  KLibFactory *browser_factory = KLibLoader::self()->factory( "libsmb4knetworkbrowser" );

  if ( browser_factory )
  {
    m_browser_part = static_cast<KParts::Part *>( browser_factory->create( widget, "BrowserPart", "KParts::Part", QStringList( "konqplugin=\"true\"" ) ) );

    // Do nothing here. The network scan (and the mounting of recently used
    // shares) will be done by Smb4KCore::init() below.
  }
  else
  {
    KMessageBox::error( 0, "<qt>"+KLibLoader::self()->lastErrorMessage()+"</qt>" );

    return;
  }

  connect( Smb4KCore::mounter(), SIGNAL( mountedShare( const QString &) ), this, SLOT( slotMountedShare( const QString & ) ) );
  connect( Smb4KCore::mounter(), SIGNAL( aboutToUnmount( const QString& ) ), this, SLOT( slotPrepareUnmount( const QString& ) ) );

  // Scan the network and remount recently used shares:
  Smb4KCore::self()->init();

}


KonqSidebar_Smb4K::~KonqSidebar_Smb4K()
{

}

extern "C"
{
    void* create_konqsidebar_smb4k(KInstance *instance,QObject *par,QWidget *widp,QString &desktopname,const char *name)
    {
     KGlobal::locale()->insertCatalogue("smb4k");
     return new KonqSidebar_Smb4K(instance,par,widp,desktopname,name);
    }
};

extern "C"
{
 bool add_konqsidebar_smb4k(QString* fn, QString* /*param*/, QMap<QString,QString> *map)
     {
      map->insert("Type","Link");
      map->insert("Icon","smb4k");
      map->insert("Name",i18n("Samba Browser"));
      map->insert("Open","false");
      map->insert("X-KDE-KonqSidebarModule","konqsidebar_smb4k");
      fn->setLatin1("smb4k%1.desktop");
      return true;
     }
}

void KonqSidebar_Smb4K::handleURL(const KURL &url)
{
 currentKonquerorURL = url.path();
 //kdDebug() << "KonqSidebar_Smb4K::handleURL - "<< currentKonquerorURL << endl;
}

void KonqSidebar_Smb4K::slotRescan()
{
  // Send a custom rescan event to the browser part.
  QApplication::postEvent( m_browser_part, new QCustomEvent( EVENT_SCAN_NETWORK ) );
}

void KonqSidebar_Smb4K::slotSearch()
{
  // Load the factory of the config dialog:
  KLibFactory *search_factory = KLibLoader::self()->factory( "libsmb4ksearchdialog" );

  if ( search_factory )
  {
    KDialogBase *searchDialog = new KDialogBase( KDialogBase::Plain, i18n( "Search Dialog" ), KDialogBase::Close,
                                              KDialogBase::NoDefault, widget, "sd", true, true );

    QFrame *frame = searchDialog->plainPage();

    m_search_part = static_cast<KParts::Part *>( search_factory->create( frame, "SearchDialogPart", "KParts::Part" ) );

    if ( m_search_part )
    {
      QGridLayout *layout = new QGridLayout( frame );
      layout->setSpacing( 10 );
      layout->setMargin( 0 );
      layout->addWidget ( m_search_part->widget(), 0, 0, 0 );
      searchDialog->resize(400,300);
      // We do not want the dialog to be closed if the user presses return.
      // Instead we want that a search is started:
      searchDialog->actionButton( KDialogBase::Close )->setAutoDefault( false );

      //connect( m_search_dialog, SIGNAL( searchResult( Smb4KHostItem * ) ), this, SLOT( slotInsertItem( Smb4KHostItem * ) ) );

      searchDialog->show();
    }
    else
    {
      delete searchDialog;
    }
  }
  else
  {
    KMessageBox::error( 0, "<qt>"+KLibLoader::self()->lastErrorMessage()+"</qt>" );

    return;
  }
}

void KonqSidebar_Smb4K::slotSmb4KOptionsDlg()
{
  // Load the factory of the config dialog:
  KLibFactory *config_factory = KLibLoader::self()->factory( "libsmb4kconfigdialog" );

  if ( config_factory )
  {
    KConfigDialog *dlg = static_cast<KConfigDialog *>( config_factory->create( widget, "ConfigDialog", "KConfigDialog" ) );

    if ( dlg )
    {
      connect( dlg,  SIGNAL( settingsChanged() ), this, SLOT( slotSettingsChanged() ) );

      dlg->show();
    }
  }
  else
  {
    KMessageBox::error( 0, "<qt>"+KLibLoader::self()->lastErrorMessage()+"</qt>" );

    return;
  }

}

void KonqSidebar_Smb4K::slotSettingsChanged()
{
  QApplication::postEvent( m_browser_part, new QCustomEvent( EVENT_LOAD_SETTINGS ) );
}

void KonqSidebar_Smb4K::slotMountedShare( const QString &mountedShare )
{
 emit openURLRequest( KURL(mountedShare) );
}

void KonqSidebar_Smb4K::slotPrepareUnmount( const QString &mountpoint )
{
  if ( QString::compare( currentKonquerorURL, mountpoint ) == 0 )
  {
    emit openURLRequest( KURL( QDir::home().canonicalPath() ) );
  }
  else
  {
    // Nothing to do
  }
}

#include "smb4k_konqplugin.moc"





