/*
 * Copyright 2003 by Richard J. Moore, rich@kde.org
 */


#include <tdehtml_part.h> // this plugin applies to a tdehtml part
#include <kdebug.h>
#include "autorefresh.h"
#include <tdeaction.h>
#include <tdeinstance.h>
#include <kiconloader.h>
#include <tqmessagebox.h>
#include <tdelocale.h>
#include <tqtimer.h>
#include <kgenericfactory.h>
 
AutoRefresh::AutoRefresh( TQObject* parent, const char* name, const TQStringList & /*args*/ )
    : Plugin( parent, name )
{
   timer = new TQTimer( this );
   connect( timer, TQ_SIGNAL( timeout() ), this, TQ_SLOT( slotRefresh() ) );
   
    refresher = new TDESelectAction( i18n("&Auto Refresh"),
                                   "reload", 0,
                                   this, TQ_SLOT(slotIntervalChanged()),
                                   actionCollection(), "autorefresh" );
    TQStringList sl;
    sl << i18n("None");
    sl << i18n("Every 15 Seconds");
    sl << i18n("Every 30 Seconds");
    sl << i18n("Every Minute");
    sl << i18n("Every 5 Minutes");
    sl << i18n("Every 10 Minutes");
    sl << i18n("Every 15 Minutes");
    sl << i18n("Every 30 Minutes");
    sl << i18n("Every 60 Minutes");

    refresher->setItems( sl );
    refresher->setCurrentItem( 0 );
}
 
AutoRefresh::~AutoRefresh()
{
}

void AutoRefresh::slotIntervalChanged()
{
   int idx = refresher->currentItem();
   int timeout = 0;
   switch (idx) {
     case 1:
         timeout = ( 15*1000 );
         break;
     case 2:
         timeout = ( 30*1000 );
         break;
     case 3:
         timeout = ( 60*1000 );
         break;
     case 4:
         timeout = ( 5*60*1000 );
         break;
     case 5:
         timeout = ( 10*60*1000 );
         break;
     case 6:
         timeout = ( 15*60*1000 );
         break;
     case 7:
         timeout = ( 30*60*1000 );
         break;
     case 8:
         timeout = ( 60*60*1000 );
         break;
     default:
         break;
   }

   timer->stop();
   if ( timeout )
      timer->start( timeout );
}

void AutoRefresh::slotRefresh()
{
    if ( !parent()->inherits("KParts::ReadOnlyPart") ) {
        TQString title = i18n( "Cannot Refresh Source" );
        TQString text = i18n( "<qt>This plugin cannot auto-refresh the current part.</qt>" );
 
        TQMessageBox::warning( 0, title, text );
    }
    else
    {
        KParts::ReadOnlyPart *part = (KParts::ReadOnlyPart *) parent();

        // Get URL
        KURL url = part->url();
        part->openURL( url );
    }
}

K_EXPORT_COMPONENT_FACTORY( libautorefresh, KGenericFactory<AutoRefresh>( "autorefresh" ) )

#include "autorefresh.moc"

