/***************************************************************************
*                                tdemainwidget.cpp
*                             -------------------
*
*    begin        : Tue Jan 29 2002
*    copyright    : (C) 2002 by Patrick Charbonnier
*                 : Based On Caitoo v.0.7.3 (c) 1998 - 2000, Matej Koss
*    email        : pch@freeshell.org
*    Copyright (C) 2002 Carsten Pfeiffer <pfeiffer@kde.org>
*
****************************************************************************/

/***************************************************************************
 *
 *   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.
 *
 ***************************************************************************/
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <unistd.h>

#ifdef __svr4__
#define map BULLSHIT            // on Solaris it conflicts with STL ?
#include <net/if.h>
#undef map
#include <sys/sockio.h>         // needed for SIOCGIFFLAGS
#else
#include <net/if.h>
#endif

#include <tqclipboard.h>
#include <tqregexp.h>
#include <tqdragobject.h>
#include <tqwhatsthis.h>
#include <tqtooltip.h>
#include <tqtimer.h>
#include <tqdropsite.h>
#include <tqpopupmenu.h>
#include <tqfileinfo.h>
#include <kinputdialog.h>

#include <kprotocolinfo.h>
#include <tdefiledialog.h>
#include <tdeapplication.h>
#include <kstandarddirs.h>
#include <kiconloader.h>
#include <kurl.h>
#include <kurldrag.h>
#include <tdelocale.h>
#include <tdeglobal.h>
#include <twin.h>
#include <tdemessagebox.h>
#include <kstdaction.h>
#include <khelpmenu.h>
#include <kedittoolbar.h>
#include <kstatusbar.h>
#include <tdeconfig.h>
#include <tdeio/netaccess.h>
#include <knotifyclient.h>
#include <knotifydialog.h>
#include <tdemenubar.h>
#include <tdeio/renamedlg.h>

#include "safedelete.h"
#include "settings.h"
#include "transfer.h"
#include "transferlist.h"
#include "tdemainwidget.h"
#include "tdefileio.h"
#include "dlgPreferences.h"
#include "logwindow.h"
#include "docking.h"
#include "droptarget.h"
#include <assert.h>



#include <tdeio/authinfo.h>
#include <tdeio/global.h>
#include <tqiconset.h>

#include "version.h"
#include "slave.h"
#include "slaveevent.h"

struct KURLPair
{
	KURL dest;
	KURL src;
};

TDEMainWidget *tdemain = 0L;

#define LOAD_ICON(X) TDEGlobal::iconLoader()->loadIcon(X, TDEIcon::MainToolbar)

DropTarget *kdrop = 0L;

Settings ksettings;             // this object contains all settings

static int sockets_open();


// socket constants
int ipx_sock = -1;              /* IPX socket */
int ax25_sock = -1;             /* AX.25 socket */
int inet_sock = -1;             /* INET socket */
int ddp_sock = -1;              /* Appletalk DDP socket */



TDEMainWidget::TDEMainWidget(bool bStartDocked)
    : KGetIface( "KGet-Interface" ),
      TDEMainWindow(0, "kget mainwindow",0),
      prefDlg( 0 ), kdock( 0 )
{
#ifdef _DEBUG
    sDebugIn << endl;
#endif

    {
        TDEConfig cfg( "tdeioslaverc", false, false);
        cfg.setGroup(TQString());
        cfg.writeEntry("AutoResume", true);
        cfg.sync();
    }

    b_connected = TRUE;
    b_viewLogWindow = FALSE;
    b_viewPreferences = FALSE;

    myTransferList = 0L;
    tdemain = this;

    // Set log time, needed for the name of log file
    TQDate date = TQDateTime::currentDateTime().date();
    TQTime time = TQDateTime::currentDateTime().time();
    TQString tmp;

    tmp.sprintf("log%d:%d:%d-%d:%d:%d", date.day(), date.month(), date.year(), time.hour(), time.minute(), time.second());

    logFileName = locateLocal("appdata", "logs/");
    logFileName += tmp;

    lastClipboard = TQApplication::clipboard()->text( TQClipboard::Clipboard ).stripWhiteSpace();
    // Load all settings from TDEConfig
    ksettings.load();

    // Setup log window
    logWindow = new LogWindow();

    m_showDropTarget = false;

    setCaption(KGETVERSION);

    setupGUI();
    setupWhatsThis();

    log(i18n("Welcome to KGet"));

    setCentralWidget(myTransferList);

    connect(kapp, TQT_SIGNAL(saveYourself()), TQT_SLOT(slotSaveYourself()));

    // Enable dropping
    setAcceptDrops(true);

    // Setup connection timer
    connectionTimer = new TQTimer(this);
    connect(connectionTimer, TQT_SIGNAL(timeout()), TQT_SLOT(slotCheckConnection()));

    // setup socket for checking connection
    if ((_sock = sockets_open()) < 0) {
        log(i18n("Could not create valid socket"), false);
    } else {
        connectionTimer->start(5000);   // 5 second interval for checking connection
    }

    checkOnline();
    ksettings.b_offline=( !b_connected || ksettings.b_offlineMode );

    // Setup animation timer
    animTimer = new TQTimer(this);
    animCounter = 0;
    connect(animTimer, TQT_SIGNAL(timeout()), TQT_SLOT(slotAnimTimeout()));

    if (ksettings.b_useAnimation) {
        animTimer->start(400);
    } else {
        animTimer->start(1000);
    }

    // Setup transfer timer for scheduled downloads and checkQueue()
    transferTimer = new TQTimer(this);
    connect(transferTimer, TQT_SIGNAL(timeout()), TQT_SLOT(slotTransferTimeout()));
    transferTimer->start(10000);        // 10 secs time interval

    // Setup autosave timer
    autosaveTimer = new TQTimer(this);
    connect(autosaveTimer, TQT_SIGNAL(timeout()), TQT_SLOT(slotAutosaveTimeout()));
    setAutoSave();

    // Setup clipboard timer
    clipboardTimer = new TQTimer(this);
    connect(clipboardTimer, TQT_SIGNAL(timeout()), TQT_SLOT(slotCheckClipboard()));
    if (ksettings.b_autoPaste) {
        clipboardTimer->start(1000);
    }

    readTransfers();

    // Setup special windows
    kdrop = new DropTarget(this);
    kdock = new DockWidget(this);
    connect(kdock, TQT_SIGNAL(quitSelected()), TQT_SLOT(slotQuit()));

    // Set geometry
    if (ksettings.mainPosition.x() != -1) {
        resize(ksettings.mainSize);
        move(ksettings.mainPosition);
        KWin::setState(winId(), ksettings.mainState);
    } else {
        resize(650, 180);
    }

    // update actions
    m_paUseAnimation->setChecked(ksettings.b_useAnimation);
    m_paExpertMode->setChecked(ksettings.b_expertMode);
    m_paUseLastDir->setChecked(ksettings.b_useLastDir);

    if (ksettings.connectionType != PERMANENT) {
        m_paAutoDisconnect->setChecked(ksettings.b_autoDisconnect);
    }
    setAutoDisconnect();

    m_paAutoShutdown->setChecked(ksettings.b_autoShutdown);


    m_paOfflineMode->setChecked(ksettings.b_offline);
    if (!ksettings.b_offlineMode) 
        m_paOfflineMode->setIconSet(LOAD_ICON("tool_offline_mode_on"));

    if (ksettings.b_offline) {
        setCaption(i18n("Offline"), false);
        log(i18n("Starting offline"));
    } else
        setCaption(TQString(), false);
    
    m_paAutoPaste->setChecked(ksettings.b_autoPaste);
    m_paShowLog->setChecked(b_viewLogWindow);

    if (!bStartDocked && ksettings.b_showMain)
        show();

    kdock->show();

    KNotifyClient::startDaemon();

    setStandardToolBarMenuEnabled(true);

#ifdef _DEBUG
    sDebugOut << endl;
#endif
}


TDEMainWidget::~TDEMainWidget()
{
#ifdef _DEBUG
    sDebugIn << endl;
#endif

    delete prefDlg;
    delete kdrop;
    writeTransfers();
    writeLog();

#ifdef _DEBUG
    sDebugOut << endl;
#endif
    delete logWindow;
}


void TDEMainWidget::log(const TQString & message, bool statusbar)
{
#ifdef _DEBUG
    sDebugIn <<" message= "<< message << endl;
#endif

    logWindow->logGeneral(message);

    if (statusbar) {
        statusBar()->message(message, 1000);
    }

#ifdef _DEBUG
    sDebugOut << endl;
#endif
}


void TDEMainWidget::slotSaveYourself()
{
#ifdef _DEBUG
    sDebugIn << endl;
#endif

    writeTransfers();
    ksettings.save();

#ifdef _DEBUG
    sDebugOut << endl;
#endif
}


void TDEMainWidget::setupGUI()
{
#ifdef _DEBUG
    sDebugIn << endl;
#endif

    // setup transfer list
    myTransferList = new TransferList(this, "transferList");
    myTransferList->setSorting(-1);
    setListFont();

    TDEActionCollection *coll = actionCollection();

    connect(myTransferList, TQT_SIGNAL(selectionChanged()), this, TQT_SLOT(slotUpdateActions()));
    connect(myTransferList, TQT_SIGNAL(transferSelected(Transfer *)), this, TQT_SLOT(slotOpenIndividual()));
    connect(myTransferList, TQT_SIGNAL(popupMenu(Transfer *)), this, TQT_SLOT(slotPopupMenu(Transfer *)));

    // file actions
    m_paOpenTransfer = KStdAction::open(TQT_TQOBJECT(this), TQT_SLOT(slotOpenTransfer()), coll, "open_transfer");
    m_paPasteTransfer = KStdAction::paste(TQT_TQOBJECT(this), TQT_SLOT(slotPasteTransfer()), coll, "paste_transfer");

    m_paExportTransfers = new TDEAction(i18n("&Export Transfer List..."), 0, TQT_TQOBJECT(this), TQT_SLOT(slotExportTransfers()), coll, "export_transfers");
    m_paImportTransfers = new TDEAction(i18n("&Import Transfer List..."), 0, TQT_TQOBJECT(this), TQT_SLOT(slotImportTransfers()), coll, "import_transfers");

    m_paImportText = new TDEAction(i18n("Import Text &File..."), 0, TQT_TQOBJECT(this), TQT_SLOT(slotImportTextFile()), coll, "import_text");

    m_paQuit = KStdAction::quit(TQT_TQOBJECT(this), TQT_SLOT(slotQuit()), coll, "quit");


    // transfer actions
    m_paCopy = new TDEAction(i18n("&Copy URL to Clipboard"), 0, TQT_TQOBJECT(this), TQT_SLOT(slotCopyToClipboard()), coll, "copy_url");
    m_paIndividual = new TDEAction(i18n("&Open Individual Window"), 0, TQT_TQOBJECT(this), TQT_SLOT(slotOpenIndividual()), coll, "open_individual");

    m_paMoveToBegin = new TDEAction(i18n("Move to &Beginning"), 0, TQT_TQOBJECT(this), TQT_SLOT(slotMoveToBegin()), coll, "move_begin");

    m_paMoveToEnd = new TDEAction(i18n("Move to &End"), 0, TQT_TQOBJECT(this), TQT_SLOT(slotMoveToEnd()), coll, "move_end");
#ifdef _DEBUG
    sDebug << "Loading pics" << endl;
#endif
    m_paResume = new TDEAction(i18n("&Resume"),"tool_resume", 0, TQT_TQOBJECT(this), TQT_SLOT(slotResumeCurrent()), coll, "resume");
    m_paPause = new TDEAction(i18n("&Pause"),"tool_pause", 0, TQT_TQOBJECT(this), TQT_SLOT(slotPauseCurrent()), coll, "pause");
    m_paDelete = new TDEAction(i18n("&Delete"),"edit-delete", TQt::Key_Delete, TQT_TQOBJECT(this), TQT_SLOT(slotDeleteCurrent()), coll, "delete");
    m_paRestart = new TDEAction(i18n("Re&start"),"tool_restart", 0, TQT_TQOBJECT(this), TQT_SLOT(slotRestartCurrent()), coll, "restart");

    m_paQueue = new TDERadioAction(i18n("&Queue"),"tool_queue", 0, TQT_TQOBJECT(this), TQT_SLOT(slotQueueCurrent()), coll, "queue");
    m_paTimer = new TDERadioAction(i18n("&Timer"),"tool_timer", 0, TQT_TQOBJECT(this), TQT_SLOT(slotTimerCurrent()), coll, "timer");
    m_paDelay = new TDERadioAction(i18n("De&lay"),"tool_delay", 0, TQT_TQOBJECT(this), TQT_SLOT(slotDelayCurrent()), coll, "delay");

    m_paQueue->setExclusiveGroup("TransferMode");
    m_paTimer->setExclusiveGroup("TransferMode");
    m_paDelay->setExclusiveGroup("TransferMode");

    // options actions
    m_paUseAnimation   =  new TDEToggleAction(i18n("Use &Animation"), 0, TQT_TQOBJECT(this), TQT_SLOT(slotToggleAnimation()), coll, "toggle_animation");
    m_paExpertMode     =  new TDEToggleAction(i18n("&Expert Mode"),"tool_expert", 0, TQT_TQOBJECT(this), TQT_SLOT(slotToggleExpertMode()), coll, "expert_mode");
    m_paUseLastDir     =  new TDEToggleAction(i18n("&Use-Last-Folder Mode"),"tool_uselastdir", 0, TQT_TQOBJECT(this), TQT_SLOT(slotToggleUseLastDir()), coll, "use_last_dir");
    m_paAutoDisconnect =  new TDEToggleAction(i18n("Auto-&Disconnect Mode"),"tool_disconnect", 0, TQT_TQOBJECT(this), TQT_SLOT(slotToggleAutoDisconnect()), coll, "auto_disconnect");
    m_paAutoShutdown   =  new TDEToggleAction(i18n("Auto-S&hutdown Mode"), "tool_shutdown", 0, TQT_TQOBJECT(this), TQT_SLOT(slotToggleAutoShutdown()), coll, "auto_shutdown");
    m_paOfflineMode    =  new TDEToggleAction(i18n("&Offline Mode"),"tool_offline_mode_off", 0, TQT_TQOBJECT(this), TQT_SLOT(slotToggleOfflineMode()), coll, "offline_mode");
    m_paAutoPaste      =  new TDEToggleAction(i18n("Auto-Pas&te Mode"),"tool_clipboard", 0, TQT_TQOBJECT(this), TQT_SLOT(slotToggleAutoPaste()), coll, "auto_paste");

    m_paPreferences    =  KStdAction::preferences(TQT_TQOBJECT(this), TQT_SLOT(slotPreferences()), coll);

    KStdAction::keyBindings(guiFactory(), TQT_SLOT(configureShortcuts()), coll);
    KStdAction::configureToolbars(TQT_TQOBJECT(this), TQT_SLOT(slotConfigureToolbars()), coll);
    KStdAction::configureNotifications(TQT_TQOBJECT(this), TQT_SLOT(slotConfigureNotifications()), coll);

    m_menubarAction = KStdAction::showMenubar(TQT_TQOBJECT(this), TQT_SLOT(slotShowMenubar()), coll, "settings_showmenubar" );
    m_menubarAction->setChecked( !menuBar()->isHidden() );

    // view actions
    createStandardStatusBarAction();

    m_paShowLog      = new TDEToggleAction(i18n("Show &Log Window"),"tool_logwindow", 0, TQT_TQOBJECT(this), TQT_SLOT(slotToggleLogWindow()), coll, "toggle_log");
    m_paShowLog->setCheckedState(i18n("Hide &Log Window"));
    m_paDropTarget   = new TDEAction(i18n("Show Drop &Target"),"tool_drop_target", 0, TQT_TQOBJECT(this), TQT_SLOT(slotToggleDropTarget()), coll, "drop_target");
    m_paKonquerorIntegration = new TDEAction(i18n("Enable &KGet as Konqueror Download Manager"), "konqueror", 0, TQT_TQOBJECT(this), TQT_SLOT(slotKonquerorIntegration()), coll, "konqueror_integration");
    if (ksettings.b_KonquerorIntegration) {
        m_paKonquerorIntegration->setText(i18n("Disable &KGet as Konqueror Download Manager"));
    }

    menuHelp = new KHelpMenu(this, TDEGlobal::instance()->aboutData());
    KStdAction::whatsThis(menuHelp, TQT_SLOT(contextHelpActivated()), coll, "whats_this");

    createGUI("kgetui.rc");

    // setup statusbar
    statusBar()->insertFixedItem(i18n(" Transfers: %1 ").arg(99), ID_TOTAL_TRANSFERS);
    statusBar()->insertFixedItem(i18n(" Files: %1 ").arg(555), ID_TOTAL_FILES);
    statusBar()->insertFixedItem(i18n(" Size: %1 KB ").arg("134.56"), ID_TOTAL_SIZE);
    statusBar()->insertFixedItem(i18n(" Time: %1 ").arg(TDEIO::convertSeconds(0)), ID_TOTAL_TIME);
    statusBar()->insertFixedItem(i18n(" %1 KB/s ").arg("123.34"), ID_TOTAL_SPEED);

    setAutoSaveSettings( "MainWindow", false /*Settings takes care of size & pos & state */ );

    slotUpdateActions();

    updateStatusBar();

#ifdef _DEBUG
    sDebugOut << endl;
#endif
}


void TDEMainWidget::setupWhatsThis()
{
#ifdef _DEBUG
    sDebugIn << endl;
#endif

    TQString tmp;

    tmp = i18n("<b>Resume</b> button starts selected transfers\n" "and sets their mode to <i>queued</i>.");
    m_paResume->setWhatsThis(tmp);

    tmp = i18n("<b>Pause</b> button stops selected transfers\n" "and sets their mode to <i>delayed</i>.");
    m_paPause->setWhatsThis(tmp);

    tmp = i18n("<b>Delete</b> button removes selected transfers\n" "from the list.");
    m_paDelete->setWhatsThis(tmp);

    tmp = i18n("<b>Restart</b> button is a convenience button\n" "that simply does Pause and Resume.");
    m_paRestart->setWhatsThis(tmp);

    tmp = i18n("<b>Queued</b> button sets the mode of selected\n" "transfers to <i>queued</i>.\n" "\n" "It is a radio button -- you can choose between\n" "three modes.");
    m_paQueue->setWhatsThis(tmp);

    tmp = i18n("<b>Scheduled</b> button sets the mode of selected\n" "transfers to <i>scheduled</i>.\n" "\n" "It is a radio button -- you can choose between\n" "three modes.");
    m_paTimer->setWhatsThis(tmp);

    tmp = i18n("<b>Delayed</b> button sets the mode of selected\n" "transfers to <i>delayed</i>." "This also causes the selected transfers to stop.\n" "\n" "It is a radio button -- you can choose between\n" "three modes.");
    m_paDelay->setWhatsThis(tmp);

    tmp = i18n("<b>Preferences</b> button opens a preferences dialog\n" "where you can set various options.\n" "\n" "Some of these options can be more easily set using the toolbar.");
    m_paPreferences->setWhatsThis(tmp);

    tmp = i18n("<b>Log window</b> button opens a log window.\n" "The log window records all program events that occur\n" "while KGet is running.");
    m_paShowLog->setWhatsThis(tmp);

    tmp = i18n("<b>Paste transfer</b> button adds a URL from\n" "the clipboard as a new transfer.\n" "\n" "This way you can easily copy&paste URLs between\n" "applications.");
    m_paPasteTransfer->setWhatsThis(tmp);

    tmp = i18n("<b>Expert mode</b> button toggles the expert mode\n" "on and off.\n" "\n" "Expert mode is recommended for experienced users.\n" "When set, you will not be \"bothered\" by confirmation\n" "messages.\n" "<b>Important!</b>\n" "Turn it on if you are using auto-disconnect or\n" "auto-shutdown features and you want KGet to disconnect \n" "or shut down without asking.");
    m_paExpertMode->setWhatsThis(tmp);

    tmp = i18n("<b>Use last folder</b> button toggles the\n" "use-last-folder feature on and off.\n" "\n" "When set, KGet will ignore the folder settings\n" "and put all new added transfers into the folder\n" "where the last transfer was put.");
    m_paUseLastDir->setWhatsThis(tmp);

    tmp = i18n("<b>Auto disconnect</b> button toggles the auto-disconnect\n" "mode on and off.\n" "\n" "When set, KGet will disconnect automatically\n" "after all queued transfers are finished.\n" "\n" "<b>Important!</b>\n" "Also turn on the expert mode when you want KGet\n" "to disconnect without asking.");
    m_paAutoDisconnect->setWhatsThis(tmp);

    tmp = i18n("<b>Auto shutdown</b> button toggles the auto-shutdown\n" "mode on and off.\n" "\n" "When set, KGet will quit automatically\n" "after all queued transfers are finished.\n" "<b>Important!</b>\n" "Also turn on the expert mode when you want KGet\n" "to quit without asking.");
    m_paAutoShutdown->setWhatsThis(tmp);

    tmp = i18n("<b>Offline mode</b> button toggles the offline mode\n" "on and off.\n" "\n" "When set, KGet will act as if it was not connected\n" "to the Internet.\n" "\n" "You can browse offline, while still being able to add\n" "new transfers as queued.");
    m_paOfflineMode->setWhatsThis(tmp);

    tmp = i18n("<b>Auto paste</b> button toggles the auto-paste mode\n" "on and off.\n" "\n" "When set, KGet will periodically scan the clipboard\n" "for URLs and paste them automatically.");
    m_paAutoPaste->setWhatsThis(tmp);

    tmp = i18n("<b>Drop target</b> button toggles the window style\n" "between a normal window and a drop target.\n" "\n" "When set, the main window will be hidden and\n" "instead a small shaped window will appear.\n" "\n" "You can show/hide a normal window with a simple click\n" "on a shaped window.");
    m_paDropTarget->setWhatsThis(tmp);
    /*
        tmp = i18n("<b>Dock widget</b> button toggles the window style\n" "between a normal window and a docked widget.\n" "\n" "When set, the main window will be hidden and\n" "instead a docked widget will appear on the panel.\n" "\n" "You can show/hide a normal window by simply clicking\n" "on a docked widget.");
        m_paDockWindow->setWhatsThis(tmp);

        tmp = i18n("<b>Normal window</b> button sets\n" "\n" "the window style to normal window");
        m_paNormal->setWhatsThis(tmp);
      */


#ifdef _DEBUG
    sDebugOut << endl;
#endif
}


void TDEMainWidget::slotConfigureToolbars()
{
#ifdef _DEBUG
    sDebugIn << endl;
#endif

    saveMainWindowSettings( TDEGlobal::config(), "MainWindow" );
    KEditToolbar edit(factory());
    connect(&edit, TQT_SIGNAL( newToolbarConfig() ), this, TQT_SLOT( slotNewToolbarConfig() ));
    edit.exec();

#ifdef _DEBUG
    sDebugOut << endl;
#endif
}


void TDEMainWidget::slotNewToolbarConfig()
{
#ifdef _DEBUG
    sDebugIn << endl;
#endif

    createGUI("kgetui.rc");
    applyMainWindowSettings( TDEGlobal::config(), "MainWindow" );

#ifdef _DEBUG
    sDebugOut << endl;
#endif
}


void TDEMainWidget::slotImportTextFile()
{
#ifdef _DEBUG
    sDebugIn << endl;
#endif

    TQString tmpFile;
    TQString list;
    int i, j;

    KURL filename = KFileDialog::getOpenURL(ksettings.lastDirectory);
    if (!filename.isValid())
        return;

    if (TDEIO::NetAccess::download(filename, tmpFile, this)) {
        list = kFileToString(tmpFile);
        TDEIO::NetAccess::removeTempFile(tmpFile);
    } else
        list = kFileToString(filename.path()); // file not accessible -> give error message

    i = 0;
    while ((j = list.find('\n', i)) != -1) {
        TQString newtransfer = list.mid(i, j - i);
        addTransfer(newtransfer);
        i = j + 1;
    }

#ifdef _DEBUG
    sDebugOut << endl;
#endif
}


void TDEMainWidget::slotImportTransfers()
{
#ifdef _DEBUG
    sDebugIn << endl;
#endif

    readTransfers(true);

#ifdef _DEBUG
    sDebugOut << endl;
#endif
}


void TDEMainWidget::readTransfers(bool ask_for_name)
{
#ifdef _DEBUG
    sDebugIn << endl;
#endif

    KURL url;

    if (ask_for_name)
        url = KFileDialog::getOpenURL(ksettings.lastDirectory, i18n("*.kgt|*.kgt\n*|All Files"));
    else
        url.setPath( locateLocal("appdata", "transfers.kgt") );

    readTransfersEx(url);

#ifdef _DEBUG
    sDebugOut << endl;
#endif
}

void TDEMainWidget::readTransfersEx(const KURL & file)
{
#ifdef _DEBUG
  sDebugIn << endl;
#endif

    if (!file.isValid()) {

#ifdef _DEBUG
        sDebugOut<< " string empty" << endl;
#endif
        return;
    }
#ifdef _DEBUG
    sDebug << "Read from file: " << file << endl;
#endif
    myTransferList->readTransfers(file);
    checkQueue();
    slotTransferTimeout();
    myTransferList->clearSelection();


#ifdef _DEBUG
    sDebugOut << endl;
#endif
}


void TDEMainWidget::slotExportTransfers()
{
#ifdef _DEBUG
    sDebugIn << endl;
#endif

    writeTransfers(true);

#ifdef _DEBUG
    sDebugOut << endl;
#endif
}

void TDEMainWidget::writeTransfers(bool ask_for_name)
{
#ifdef _DEBUG
    sDebugIn << endl;
#endif

    TQString str;
    TQString txt;

    if (ask_for_name)
        txt = KFileDialog::getSaveFileName(ksettings.lastDirectory, i18n("*.kgt|*.kgt\n*|All Files"));
    else
        txt = locateLocal("appdata", "transfers.kgt");



    if (txt.isEmpty())
    {
#ifdef _DEBUG
        sDebugOut<< " because Destination File name isEmpty"<< endl;
#endif
        return;
    }
    if (!txt.endsWith(".kgt"))
        txt += ".kgt";

#ifdef _DEBUG
    sDebug << "Writing transfers " << txt << endl;
#endif

    myTransferList->writeTransfers(txt);

#ifdef _DEBUG
    sDebugOut << endl;
#endif
}


void TDEMainWidget::writeLog()
{
#ifdef _DEBUG
    sDebugIn << "Writing log to file : " << logFileName.ascii() << endl;
#endif


    kCStringToFile(logWindow->getText().local8Bit(), logFileName, false, false);

#ifdef _DEBUG
    sDebugOut << endl;
#endif
}


void TDEMainWidget::slotQuit()
{
#ifdef _DEBUG
    sDebugIn << endl;
#endif

    Transfer *item;
    TransferIterator it(myTransferList);

    log(i18n("Quitting..."));

    for (; it.current(); ++it) {
        item = it.current();
        if (item->getStatus() == Transfer::ST_RUNNING && !ksettings.b_expertMode) {
            if (KMessageBox::warningContinueCancel(this, i18n("Some transfers are still running.\nAre you sure you want to quit KGet?"), i18n("Warning"), KStdGuiItem::quit()) != KMessageBox::Continue) {
#ifdef _DEBUG
                sDebugOut << endl;
#endif

                return;
            }
            break;
        }
    }

    ksettings.save();

#ifdef _DEBUG
    sDebugOut << endl;
#endif

    kapp->quit();
}


void TDEMainWidget::slotResumeCurrent()
{
#ifdef _DEBUG
    sDebugIn << endl;
#endif

    TransferIterator it(myTransferList);

    for (; it.current(); ++it)
        if (it.current()->isSelected())
            it.current()->slotResume();
    slotUpdateActions();


#ifdef _DEBUG
    sDebugOut << endl;
#endif
}


void TDEMainWidget::slotPauseCurrent()
{
#ifdef _DEBUG
    sDebugIn << endl;
#endif

    TransferIterator it(myTransferList);

    m_paPause->setEnabled(false);
    m_paRestart->setEnabled(false);
    update();

    for (; it.current(); ++it)
        if (it.current()->isSelected())
            it.current()->slotRequestPause();
    slotUpdateActions();


#ifdef _DEBUG
    sDebugOut << endl;
#endif
}



void TDEMainWidget::slotRestartCurrent()
{
#ifdef _DEBUG
    sDebugIn << endl;
#endif

    TransferIterator it(myTransferList);

    for (; it.current(); ++it)
        if (it.current()->isSelected())
            it.current()->slotRequestRestart();
    slotUpdateActions();


#ifdef _DEBUG
    sDebugOut << endl;
#endif
}


void TDEMainWidget::slotDeleteCurrent()
{
#ifdef _DEBUG
    sDebugIn << endl;
#endif

    m_paDelete->setEnabled(false);
    m_paPause->setEnabled(false);
    update();
    TransferIterator it(myTransferList);
    TQValueList<TQGuardedPtr<Transfer> > selectedItems;
    TQStringList itemNames;

    while (it.current()) {
        if (it.current()->isSelected()) {
            selectedItems.append( TQGuardedPtr<Transfer>( it.current() ));
            itemNames.append( it.current()->getSrc().prettyURL() );
        }
        ++it;
    }

    if (!ksettings.b_expertMode)
    {
        if ( selectedItems.count() > 1 )
        {
            if (KMessageBox::warningContinueCancelList(this, i18n("Are you sure you want to delete these transfers?"),
                                               itemNames, i18n("Question"),
                                               KStdGuiItem::del(),
                                               TQString("multiple_delete_transfer"))
                != KMessageBox::Continue)
                return; // keep 'em
        }
        else
        {
            if (KMessageBox::warningContinueCancel(this, i18n("Are you sure you want to delete this transfer?"),
                                           i18n("Question"), KStdGuiItem::del(),
                                           TQString("delete_transfer"))
                != KMessageBox::Continue)
                return;
        }
    }

    // If we reach this, we want to delete all selected transfers
    // Some of them might have finished in the meantime tho. Good that
    // we used a TQGuardedPtr :)

    int transferFinishedMeanwhile = 0;
    TQValueListConstIterator<TQGuardedPtr<Transfer> > lit = selectedItems.begin();;
    while ( lit != selectedItems.end() )
    {
        if ( *lit )
            (*lit)->slotRequestRemove();
        else
            ++transferFinishedMeanwhile;

        ++lit;
    }

    checkQueue(); // needed !

    if ( !ksettings.b_expertMode && transferFinishedMeanwhile > 0 )
        KMessageBox::information(this, i18n("The transfer you wanted to delete completed before it could be deleted.",
                                            "%n transfers you wanted to delete completed before they could be deleted.",
                                            transferFinishedMeanwhile ),
                                 TQString(), "completedBeforeDeletion" );

#ifdef _DEBUG
    sDebugOut << endl;
#endif
}


void TDEMainWidget::stopAll()
{
#ifdef _DEBUG
    sDebugIn << endl;
#endif

    log(i18n("Stopping all jobs"), false);

    TransferIterator it(myTransferList);
    Transfer::TransferStatus Status;
    for (; it.current(); ++it) {
        Status = it.current()->getStatus();
        if (Status == Transfer::ST_TRYING || Status == Transfer::ST_RUNNING)
            it.current()->slotStop();
    }
    slotUpdateActions();

#ifdef _DEBUG
    sDebugOut << endl;
#endif
}


void TDEMainWidget::slotQueueCurrent()
{
#ifdef _DEBUG
    sDebugIn << endl;
#endif

    TransferIterator it(myTransferList);

    for (; it.current(); ++it) {
        if (it.current()->isSelected()) {
            it.current()->slotQueue();
        }
    }

    //    myTransferList->clearSelection();
    slotUpdateActions();

#ifdef _DEBUG
    sDebugOut << endl;
#endif
}


void TDEMainWidget::slotTimerCurrent()
{
#ifdef _DEBUG
    sDebugIn << endl;
#endif


    TransferIterator it(myTransferList);

    for (; it.current(); ++it)
        if (it.current()->isSelected())
            it.current()->slotRequestSchedule();

#ifdef _DEBUG
    sDebugOut << endl;
#endif

}


void TDEMainWidget::slotDelayCurrent()
{
#ifdef _DEBUG
    sDebugIn << endl;
#endif

    TransferIterator it(myTransferList);

    for (; it.current(); ++it)
        if (it.current()->isSelected())
            it.current()->slotRequestDelay();

#ifdef _DEBUG
    sDebugOut << endl;
#endif
}


void TDEMainWidget::slotOpenTransfer()
{
#ifdef _DEBUG
    sDebugIn << endl;
#endif

    TQString newtransfer;
    bool ok = false;

#ifdef _DEBUG
    //      newtransfer = "ftp://localhost/home/pch/test.gz";
    //      newtransfer = "http://www.kernel.org/pub/linux/kernel/v2.4/linux-2.4.18.tar.gz";
    newtransfer = "ftp://darkmoon/pub/test.gz";
#endif

    while (!ok) {
        newtransfer = KInputDialog::getText(i18n("Open Transfer"), i18n("Open transfer:"), newtransfer, &ok, this);

        // user presses cancel
        if (!ok) {
            return;
        }

        KURL url = KURL::fromPathOrURL(newtransfer);

        if (!url.isValid()) {
            KMessageBox::error(this, i18n("Malformed URL:\n%1").arg(newtransfer), i18n("Error"));
            ok = false;
        }
    }

    addTransfer(newtransfer);

#ifdef _DEBUG
    sDebugOut << endl;
#endif
}



void TDEMainWidget::slotCheckClipboard()
{
#ifdef _DEBUG
    //sDebugIn << endl;
#endif

    TQString clipData = TQApplication::clipboard()->text( TQClipboard::Clipboard ).stripWhiteSpace();

    if (clipData != lastClipboard) {
        sDebug << "New clipboard event" << endl;

        lastClipboard = clipData;
        if ( lastClipboard.isEmpty() )
            return;

        KURL url = KURL::fromPathOrURL( lastClipboard );

        if (url.isValid() && !url.isLocalFile())
            slotPasteTransfer();
    }

#ifdef _DEBUG
    //sDebugOut << endl;
#endif
}


void TDEMainWidget::slotPasteTransfer()
{
#ifdef _DEBUG
    sDebugIn << endl;
#endif

    TQString newtransfer;

    newtransfer = TQApplication::clipboard()->text();
    newtransfer = newtransfer.stripWhiteSpace();

    if (!ksettings.b_expertMode) {
        bool ok = false;
        newtransfer = KInputDialog::getText(i18n("Open Transfer"), i18n("Open transfer:"), newtransfer, &ok, this);

        if (!ok) {
            // cancelled
#ifdef _DEBUG
            sDebugOut << endl;
#endif
            return;
        }

    }

    if (!newtransfer.isEmpty())
        addTransfer(newtransfer);


#ifdef _DEBUG
    sDebugOut << endl;
#endif
}

// destFile must be a filename, not a directory! And it will be deleted, if
// it exists already, without further notice.
void TDEMainWidget::addTransferEx(const KURL& url, const KURL& destFile)
{
#ifdef _DEBUG
    sDebugIn << endl;
#endif

    if ( !sanityChecksSuccessful( url ) )
        return;

    KURL destURL = destFile;

    // Malformed destination url means one of two things.
    // 1) The URL is empty.
    // 2) The URL is only a filename, like a default (suggested) filename.
    if ( !destURL.isValid() )
    {
        // Setup destination
        TQString destDir = getSaveDirectoryFor( url.fileName() );
        bool b_expertMode = ksettings.b_expertMode;
        bool bDestisMalformed = true;

        while (bDestisMalformed)
        {
            if (!b_expertMode) {
                // open the filedialog for confirmation
                KFileDialog dlg( destDir, TQString(),
                                  0L, "save_as", true);
                dlg.setCaption(i18n("Save As"));
                dlg.setOperationMode(KFileDialog::Saving);

                // Use the destination name if not empty...
                if (destURL.isEmpty())
                    dlg.setSelection(url.fileName());
                else
                    dlg.setSelection(destURL.url());

                if ( dlg.exec() == TQDialog::Rejected )
                {
#ifdef _DEBUG
                    sDebugOut << endl;
#endif
                    return;
                }
                else
                {
                    destURL = dlg.selectedURL();
                    ksettings.lastDirectory = destURL.directory();
                }
            }
            else {
                // in expert mode don't open the filedialog
                // if destURL is not empty, it's the suggested filename
                destURL = KURL::fromPathOrURL( destDir + "/" +
                                               ( destURL.isEmpty() ?
                                                   url.fileName() : destURL.url() ));
            }

            //check if destination already exists
            if(TDEIO::NetAccess::exists(destURL, false, this))
            {
                if (KMessageBox::warningYesNo(this,i18n("Destination file \n%1\nalready exists.\nDo you want to overwrite it?").arg( destURL.prettyURL()), TQString(), i18n("Overwrite"), i18n("Do Not Overwrite") )
                                              == KMessageBox::Yes)
                {
                    bDestisMalformed=false;
                    SafeDelete::deleteFile( destURL );
                }
                else
                {
                    b_expertMode=false;
                    ksettings.lastDirectory = destURL.directory();
                }
            }
            else
                bDestisMalformed=false;
        }
    }

    else // destURL was given, check whether the file exists already
    {
        // simply delete it, the calling process should have asked if you
        // really want to delete (at least tdehtml does)
        if(TDEIO::NetAccess::exists(destURL, false, this))
            SafeDelete::deleteFile( destURL );
    }

    // create a new transfer item
    Transfer *item = myTransferList->addTransfer(url, destURL);
    KNotifyClient::event(kdock->winId(), "added", i18n("<i>%1</i> has been added.").arg(url.prettyURL()));
    item->updateAll(); // update the remaining fields

    if (ksettings.b_showIndividual)
        item->showIndividual();

    myTransferList->clearSelection();

    checkQueue();
#ifdef _DEBUG
    sDebugOut << endl;
#endif
}


void TDEMainWidget::addTransfers( const KURL::List& src, const TQString& destDir )
{
    TQValueList<KURLPair> urls_orig;

    for ( KURL::List::ConstIterator it = src.begin(); it != src.end(); ++it )
    {
        KURLPair url;
        url.src = *it;
        if ( url.src.fileName().endsWith( ".kgt" ) )
            readTransfersEx(url.src);
        else
            urls_orig.append( url );
    }

    if ( urls_orig.isEmpty() )
        return;

    if ( urls_orig.count() == 1 ) // just one file -> ask for filename
    {
        KURL destFile;

        if ( !destDir.isEmpty() )
        {
            // create a proper destination file from destDir
            KURL destURL = KURL::fromPathOrURL( destDir );
            TQString fileName = urls_orig.first().src.fileName();

            // in case the fileName is empty, we simply ask for a filename in
            // addTransferEx. Do NOT attempt to use an empty filename, that
            // would be a directory (and we don't want to overwrite that!)
            if ( !fileName.isEmpty() )
            {
                destURL.adjustPath( +1 );
                destURL.setFileName( fileName );
                if(TDEIO::NetAccess::exists(destURL, false, this))
                {
                    if (KMessageBox::warningYesNo(this,i18n("Destination file \n%1\nalready exists.\nDo you want to overwrite it?").arg( destURL.prettyURL()), TQString(), i18n("Overwrite"), i18n("Do Not Overwrite") )
                        == KMessageBox::Yes)
                    {
                        SafeDelete::deleteFile( destURL );
                        destFile = destURL;
                    }
                }
                else 
                {
                  destFile = destURL;
                }
            }
        }

        addTransferEx( urls_orig.first().src, destFile );
        return;
    }

    // multiple files -> ask for directory, not for every single filename
	
	bool dir_accepted = false;
	TQValueList<KURLPair>::Iterator it;
	TQValueList<KURLPair> urls;
	KURL::List urlsToDelete;
	while ( !dir_accepted )
	{
		urlsToDelete.clear();
		urls = urls_orig;	// copy the list here, urls might be changed, yet when we return here (Cancel),
							// we want to start again with the original list 
		dir_accepted = true; //Set to false later when Cancel is pressed
		KURL dest;
		if ( destDir.isEmpty() || !TQFileInfo( destDir ).isDir() )
		{
			if ( !destDir.isEmpty()  )
				dest.setPath( destDir );
			else
				dest.setPath( getSaveDirectoryFor( src.first().fileName() ) );

            // ask in any case, when destDir is empty
			if ( destDir.isEmpty() || !TQFileInfo( dest.path() ).isDir() )
			{
				TQString dir = KFileDialog::getExistingDirectory( dest.path() );
				if ( dir.isEmpty() ) // aborted
					return;

				dest.setPath( dir );
				ksettings.lastDirectory = dir;
			}
		}

        // dest is now finally the real destination directory for all the files
		dest.adjustPath(+1);

        // create new transfer items
		bool skip_all = false;
		bool overwrite_all = false;
		it = urls.begin();
		TDEIO::RenameDlg_Result result;
		while ( it != urls.end() )
		{
			
			if ( !sanityChecksSuccessful( (*it).src ) )
			{
				it = urls.erase( it );
				continue; // shouldn't we notify the user??
			}
			
			(*it).dest = dest;
			TQString fileName = (*it).src.fileName();
			if ( fileName.isEmpty() ) // simply use the full url as filename
				fileName = KURL::encode_string_no_slash( (*it).src.prettyURL() );

			(*it).dest.setFileName( fileName );

			if( TDEIO::NetAccess::exists((*it).dest, false, this))
			{
				TQString newdest;
				if (skip_all)
					result = TDEIO::R_SKIP;
				else if( overwrite_all )
					result = TDEIO::R_OVERWRITE;
				else
				{
					TQFileInfo finfo( (*it).dest.path() );
					TQString caption = i18n( "File Already exists" ) + " - KGet";
					result = TDEIO::open_RenameDlg( caption, (*it).src.url(), (*it).dest.url(), TDEIO::RenameDlg_Mode(TDEIO::M_OVERWRITE|TDEIO::M_SKIP|TDEIO::M_MULTI), newdest, (TDEIO::filesize_t) -1, (TDEIO::filesize_t)finfo.size(), (time_t) -1, (time_t) -1, (time_t) -1, finfo.lastModified().toTime_t());
				}
				switch (result)
				{
					case TDEIO::R_RENAME:
						(*it).dest = KURL::fromPathOrURL( newdest );
						break;
					case TDEIO::R_OVERWRITE_ALL:
						overwrite_all = true; //fall through
					case TDEIO::R_OVERWRITE:
						urlsToDelete.append( (*it).dest );
						break;
					case TDEIO::R_AUTO_SKIP:
						skip_all = true;	
					case TDEIO::R_SKIP:			//fall through
						it = urls.erase( it );
						continue;
						break;
					default:               // Cancel, ask again for directory
						dir_accepted = false;
				}
				if ( !dir_accepted )
					break;
			}   // if(TDEIO::NetAccess::exists
			++it;
		} //  while ( it != urls.end() )
	} //  while ( !dir_accepted )

	KURL::List::Iterator it_1 = urlsToDelete.begin();
	for ( ; it_1 != urlsToDelete.end(); ++it_1 )
	{
		SafeDelete::deleteFile( *it_1 );
	}
	
	int numdl = 0;
	it = urls.begin();
	for ( ; it != urls.end(); ++it )
	{
		Transfer *item = myTransferList->addTransfer((*it).src, (*it).dest);
		item->updateAll(); // update the remaining fields
		numdl++;
	}

    KNotifyClient::event(kdock->winId(), "added", i18n("1 download has been added.", "%n downloads have been added.", numdl));

    myTransferList->clearSelection();

    checkQueue();
}

void TDEMainWidget::addTransfer(const TQString& src)
{
#ifdef _DEBUG
    sDebugIn << endl;
#endif

    if ( src.isEmpty() )
        return;

    addTransferEx( KURL::fromPathOrURL( src ) );

#ifdef _DEBUG
    sDebugOut << endl;
#endif

}


void TDEMainWidget::checkQueue()
{
#ifdef _DEBUG
    sDebugIn << endl;
#endif

    uint numRun = 0;
    int status;
    Transfer *item;
    TransferIterator it(myTransferList);

    if (!ksettings.b_offline) {

        // count running transfers
        for (; it.current(); ++it) {
            status = it.current()->getStatus();
            if (status == Transfer::ST_RUNNING || status == Transfer::ST_TRYING)
                numRun++;
        }
        sDebug << "Found " << numRun << " Running Jobs" << endl;
        it.reset();
        bool isRunning;
        bool isQuequed;
        for (; it.current() && numRun < ksettings.maxSimultaneousConnections; ++it) {
            item = it.current();
            isRunning = (item->getStatus() == Transfer::ST_RUNNING) || ((item->getStatus() == Transfer::ST_TRYING));

            isQuequed = (item->getMode() == Transfer::MD_QUEUED || item->getMode() == Transfer::MD_NEW);

            if     (!isRunning && isQuequed && !ksettings.b_offline)
            {
                log(i18n("Starting another queued job."));
                item->slotResume();
                numRun++;
            }
        }

        slotUpdateActions();

        updateStatusBar();

//    } else {//TODO this has to be solved different
//        log(i18n("Cannot continue offline status"));
    }

    it.reset();
    for (; it.current(); ++it)
    {
        item = it.current();
        if (item->getMode() == Transfer::MD_NEW && item->getStatus() == Transfer::ST_STOPPED)
        {
            item->checkCache();
        }
    }
#ifdef _DEBUG
    sDebugOut << endl;
#endif

}


void TDEMainWidget::slotAnimTimeout()
{
#ifdef _DEBUG
    //sDebugIn << endl;
#endif

    animCounter++;
    if (animCounter == myTransferList->getPhasesNum()) {
       //updateStatusBar();
        animCounter = 0;
    }
    // update status of all items of transferList
    myTransferList->updateStatus(animCounter);

    //if (this->isVisible()) {
        updateStatusBar();
    //}
#ifdef _DEBUG
    //sDebugOut << endl;
#endif

}


void TDEMainWidget::slotTransferTimeout()
{
#ifdef _DEBUG
    //sDebugIn << endl;
#endif

    Transfer *item;
    TransferIterator it(myTransferList);

    bool flag = false;

    for (; it.current(); ++it) {
        item = it.current();
        if (item->getMode() == Transfer::MD_SCHEDULED && item->getStartTime() <= TQDateTime::currentDateTime()) {
            item->setMode(Transfer::MD_QUEUED);
            flag = true;
        }
    }

    if (flag) {
        checkQueue();
    }

    if (ksettings.b_autoDisconnect && ksettings.b_timedDisconnect && ksettings.disconnectTime <= TQTime::currentTime() && ksettings.disconnectDate == TQDate::currentDate()) {
        onlineDisconnect();
    }

#ifdef _DEBUG
    //sDebugOut << endl;
#endif
}


void TDEMainWidget::slotAutosaveTimeout()
{
#ifdef _DEBUG
    //sDebugIn << endl;
#endif

    writeTransfers();

#ifdef _DEBUG
    //sDebugOut << endl;
#endif
}


void TDEMainWidget::slotStatusChanged(Transfer * item, int _operation)
{
#ifdef _DEBUG
    //sDebugIn << endl;
#endif

    switch (_operation) {

    case Transfer::OP_FINISHED:
    {
        TQString srcurl = item->getSrc().prettyURL();
        if (ksettings.b_removeOnSuccess && !item->keepDialogOpen() )
        {
            delete item;
            item = 0L;
        }
        else
            item->setMode(Transfer::MD_NONE);

        if (!myTransferList->areTransfersQueuedOrScheduled()) {
            // no items or only delayed and finished items in the TransferList
            if (ksettings.b_autoDisconnect)
                onlineDisconnect();

            if (ksettings.b_autoShutdown) {
                slotQuit();
                return;
            }
            KNotifyClient::event(kdock->winId(), "finishedall", i18n("All the downloads are finished."));
        }
        else
        {
            KNotifyClient::event(kdock->winId(), "finished", i18n("<i>%1</i> successfully downloaded.").arg(srcurl));
        }

        if ( item )
            item->slotUpdateActions();

        break;
    }
    case Transfer::OP_RESUMED:
        slotUpdateActions();
        item->slotUpdateActions();
        //                play(ksettings.audioStarted);
        break;
    case Transfer::OP_PAUSED:
        break;
    case Transfer::OP_REMOVED:
        delete item;
        return;                 // checkQueue() will be called only once after all deletions

    case Transfer::OP_ABORTED:
        break;
    case Transfer::OP_DELAYED:
    case Transfer::OP_QUEUED:
        slotUpdateActions();
        item->slotUpdateActions();
        break;
    case Transfer::OP_SCHEDULED:
        slotUpdateActions();
        item->slotUpdateActions();
        slotTransferTimeout();  // this will check schedule times
        return;                 // checkQueue() is called from slotTransferTimeout()
    }
    checkQueue();

#ifdef _DEBUG
    sDebugOut << endl;
#endif
}


void TDEMainWidget::dragEnterEvent(TQDragEnterEvent * event)
{
#ifdef _DEBUG
    //sDebugIn << endl;
#endif

    event->accept(KURLDrag::canDecode(event) || TQTextDrag::canDecode(event));

#ifdef _DEBUG
    sDebugOut << endl;
#endif
}


void TDEMainWidget::dropEvent(TQDropEvent * event)
{
#ifdef _DEBUG
    //sDebugIn << endl;
#endif

    KURL::List list;
    TQString str;

    if (KURLDrag::decode(event, list)) {
        addTransfers(list);
    } else if (TQTextDrag::decode(event, str)) {
        addTransfer(str);
    }
    sDebugOut << endl;
}


void TDEMainWidget::slotCopyToClipboard()
{
#ifdef _DEBUG
    //sDebugIn << endl;
#endif

    Transfer *item = (Transfer *) myTransferList->currentItem();

    if (item) {
        TQString url = item->getSrc().url();
        TQClipboard *cb = TQApplication::clipboard();
        cb->setText( url, TQClipboard::Selection );
        cb->setText( url, TQClipboard::Clipboard);
        myTransferList->clearSelection();
    }

#ifdef _DEBUG
    sDebugOut << endl;
#endif
}


void TDEMainWidget::slotMoveToBegin()
{
#ifdef _DEBUG
    sDebugIn << endl;
#endif

    myTransferList->moveToBegin((Transfer *) myTransferList->currentItem());


#ifdef _DEBUG
    sDebugOut << endl;
#endif
}


void TDEMainWidget::slotMoveToEnd()
{
#ifdef _DEBUG
    sDebugIn << endl;
#endif

    myTransferList->moveToEnd((Transfer *) myTransferList->currentItem());

#ifdef _DEBUG
    sDebugOut << endl;
#endif
}


void TDEMainWidget::slotOpenIndividual()
{
#ifdef _DEBUG
    sDebugIn << endl;
#endif

    Transfer *item = (Transfer *) myTransferList->currentItem();
    if (item)
        item->showIndividual();


#ifdef _DEBUG
    sDebugOut << endl;
#endif
}

bool TDEMainWidget::queryClose()
{
    if( kapp->sessionSaving())
        return true;
    hide();
    return false;
}

void TDEMainWidget::setAutoSave()
{
#ifdef _DEBUG
    sDebugIn << endl;
#endif

    autosaveTimer->stop();
    if (ksettings.b_autoSave) {
        autosaveTimer->start(ksettings.autoSaveInterval * 60000);
    }

#ifdef _DEBUG
    sDebugOut << endl;
#endif
}



void TDEMainWidget::setAutoDisconnect()
{
#ifdef _DEBUG
    sDebugIn << endl;
#endif

    // disable action when we are connected permanently
    m_paAutoDisconnect->setEnabled(ksettings.connectionType != PERMANENT);

#ifdef _DEBUG
    sDebugOut << endl;
#endif
}

void TDEMainWidget::slotPreferences()
{
#ifdef _DEBUG
    sDebugIn << endl;
#endif

    if ( !prefDlg )
        prefDlg = new DlgPreferences(this);

    prefDlg->show();

#ifdef _DEBUG
    sDebugOut << endl;
#endif
}

void TDEMainWidget::slotConfigureNotifications()
{
    KNotifyDialog::configure(this);
}

void TDEMainWidget::slotToggleLogWindow()
{
#ifdef _DEBUG
    sDebugIn << endl;
#endif

    b_viewLogWindow = !b_viewLogWindow;
    if (b_viewLogWindow)
        logWindow->show();
    else
        logWindow->hide();

#ifdef _DEBUG
    sDebugOut << endl;
#endif
}


void TDEMainWidget::slotToggleAnimation()
{
#ifdef _DEBUG
    sDebugIn << endl;
#endif

    ksettings.b_useAnimation = !ksettings.b_useAnimation;

    if (!ksettings.b_useAnimation && animTimer->isActive()) {
        animTimer->stop();
        animTimer->start(1000);
        animCounter = 0;
    } else {
        animTimer->stop();
        animTimer->start(400);
    }

#ifdef _DEBUG
    sDebugOut << endl;
#endif
}

void TDEMainWidget::slotToggleOfflineMode()
{
#ifdef _DEBUG
    sDebugIn "ksettings.b_offlineMode = " << ksettings.b_offlineMode << endl;
#endif

    ksettings.b_offlineMode = !ksettings.b_offlineMode;
    ksettings.b_offline=(ksettings.b_offlineMode || !b_connected);
    if (ksettings.b_offline) {
        log(i18n("Offline mode on."));
        stopAll();
        setCaption(i18n("Offline"), false);
        m_paOfflineMode->setIconSet(LOAD_ICON("tool_offline_mode_off"));
    } else {
        log(i18n("Offline mode off."));
        setCaption(TQString(), false);
        m_paOfflineMode->setIconSet(LOAD_ICON("tool_offline_mode_on"));
    }

    m_paOfflineMode->setChecked(ksettings.b_offline);


    slotUpdateActions();
    checkQueue();
#ifdef _DEBUG
    sDebugOut << endl;
#endif
}


void TDEMainWidget::slotToggleExpertMode()
{
#ifdef _DEBUG
    sDebugIn << endl;
#endif

    ksettings.b_expertMode = !ksettings.b_expertMode;

    if (ksettings.b_expertMode) {
        log(i18n("Expert mode on."));
    } else {
        log(i18n("Expert mode off."));
    }
    m_paExpertMode->setChecked(ksettings.b_expertMode);

#ifdef _DEBUG
    sDebugOut << endl;
#endif
}


void TDEMainWidget::slotToggleUseLastDir()
{
#ifdef _DEBUG
    sDebugIn << endl;
#endif

    ksettings.b_useLastDir = !ksettings.b_useLastDir;

    if (ksettings.b_useLastDir) {
        log(i18n("Use last folder on."));
    } else {
        log(i18n("Use last folder off."));
    }

#ifdef _DEBUG
    sDebugOut << endl;
#endif
}


void TDEMainWidget::slotToggleAutoDisconnect()
{
#ifdef _DEBUG
    sDebugIn << endl;
#endif

    ksettings.b_autoDisconnect = !ksettings.b_autoDisconnect;

    if (ksettings.b_autoDisconnect) {
        log(i18n("Auto disconnect on."));
    } else {
        log(i18n("Auto disconnect off."));
    }
    m_paAutoDisconnect->setChecked(ksettings.b_autoDisconnect);

#ifdef _DEBUG
    sDebugOut << endl;
#endif
}


void TDEMainWidget::slotToggleAutoShutdown()
{
#ifdef _DEBUG
    sDebugIn << endl;
#endif

    ksettings.b_autoShutdown = !ksettings.b_autoShutdown;

    if (ksettings.b_autoShutdown) {
        log(i18n("Auto shutdown on."));
    } else {
        log(i18n("Auto shutdown off."));
    }

    m_paAutoShutdown->setChecked(ksettings.b_autoShutdown);


#ifdef _DEBUG
    sDebugOut << endl;
#endif
}


void TDEMainWidget::slotToggleAutoPaste()
{
#ifdef _DEBUG
    sDebugIn << endl;
#endif

    ksettings.b_autoPaste = !ksettings.b_autoPaste;

    if (ksettings.b_autoPaste) {
        log(i18n("Auto paste on."));
        clipboardTimer->start(1000);
    } else {
        log(i18n("Auto paste off."));
        clipboardTimer->stop();
    }
    m_paAutoPaste->setChecked(ksettings.b_autoPaste);

#ifdef _DEBUG
    sDebugOut << endl;
#endif
}


void TDEMainWidget::slotToggleDropTarget()
{
#ifdef _DEBUG
    sDebugIn << endl;
#endif
    m_showDropTarget = !m_showDropTarget;

    if (m_showDropTarget) {
        kdrop->show();
        kdrop->updateStickyState();
        m_paDropTarget->setText(i18n("Hide Drop &Target"));
    }
    else {
        kdrop->hide();
        m_paDropTarget->setText(i18n("Show Drop &Target"));
    }


#ifdef _DEBUG
    sDebugOut << endl;
#endif
}


void TDEMainWidget::slotKonquerorIntegration()
{
#ifdef _DEBUG
    sDebugIn << endl;
#endif

        bool bIsKonquiEnable=!ksettings.b_KonquerorIntegration;
        ksettings.b_KonquerorIntegration=!ksettings.b_KonquerorIntegration;
        TDEConfig cfg("konquerorrc", false, false);
        cfg.setGroup("HTML Settings");
        cfg.writePathEntry("DownloadManager",TQString((bIsKonquiEnable)?"kget":""));
        cfg.sync();
        if (bIsKonquiEnable) 
        {
            m_paKonquerorIntegration->setText(i18n("Disable &KGet as Konqueror Download Manager"));
        }
        else
        {
            m_paKonquerorIntegration->setText(i18n("Enable &KGet as Konqueror Download Manager"));
        }

#ifdef _DEBUG
    sDebugOut << endl;
#endif
}


void TDEMainWidget::slotPopupMenu(Transfer * item)
{
#ifdef _DEBUG
    sDebugIn << endl;
#endif

    // select current item
    myTransferList->setCurrentItem(item);

    // set action properties
    slotUpdateActions();

    // popup transfer menu at the position
    TQWidget *menu = guiFactory()->container("transfer",this);
    ((TQPopupMenu *) menu)->popup(TQCursor::pos());


#ifdef _DEBUG
    sDebugOut << endl;
#endif
}


void TDEMainWidget::setListFont()
{
#ifdef _DEBUG
    sDebugIn << endl;
#endif

    myTransferList->setFont(ksettings.listViewFont);

#ifdef _DEBUG
    sDebugOut << endl;
#endif
}

void TDEMainWidget::slotUpdateActions()
{
#ifdef _DEBUG
    sDebugIn << endl;
#endif


    // disable all signals
    m_paQueue->blockSignals(true);
    m_paTimer->blockSignals(true);
    m_paDelay->blockSignals(true);

    // at first turn off all buttons like when nothing is selected
    m_paQueue->setChecked(false);
    m_paTimer->setChecked(false);
    m_paDelay->setChecked(false);

    m_paQueue->setEnabled(false);
    m_paTimer->setEnabled(false);
    m_paDelay->setEnabled(false);

    m_paDelete->setEnabled(false);
    m_paResume->setEnabled(false);
    m_paPause->setEnabled(false);
    m_paRestart->setEnabled(false);

    m_paCopy->setEnabled(false);
    m_paIndividual->setEnabled(false);
    m_paMoveToBegin->setEnabled(false);
    m_paMoveToEnd->setEnabled(false);

    Transfer *item;
    Transfer *first_item = 0L;
    TransferIterator it(myTransferList);
    int index = 0;
    int totals_items = 0;
    int sel_items = 0;

    for (; it.current(); ++it, ++totals_items) {

        // update action on visibles windows
        if (it.current()->isVisible())
            it.current()->slotUpdateActions();

        if (it.current()->isSelected()) {
            item = it.current();
            sel_items = totals_items;
            index++;            // counting number of selected items
            if (index == 1) {
                first_item = item;      // store first selected item
                if (totals_items > 0)
                    m_paMoveToBegin->setEnabled(true);

                m_paMoveToEnd->setEnabled(true);
            } else {

                m_paMoveToBegin->setEnabled(false);
                m_paMoveToEnd->setEnabled(false);
            }
            // enable PAUSE, RESUME and RESTART only when we are online and not in offline mode
#ifdef _DEBUG
            sDebug << "-->ONLINE= " << ksettings.b_offline << endl;
#endif
            if (item == first_item && !ksettings.b_offline) {
                switch (item->getStatus()) {
                case Transfer::ST_TRYING:
                case Transfer::ST_RUNNING:
                    m_paResume->setEnabled(false);
                    m_paPause->setEnabled(true);
                    m_paRestart->setEnabled(true);
                    break;
                case Transfer::ST_STOPPED:
                    m_paResume->setEnabled(true);
                    m_paPause->setEnabled(false);
                    m_paRestart->setEnabled(false);
#ifdef _DEBUG
                    sDebug << "STATUS IS  stopped" << item->getStatus() << endl;
#endif
                    break;
                case Transfer::ST_FINISHED:
                    m_paResume->setEnabled(false);
                    m_paPause->setEnabled(false);
                    m_paRestart->setEnabled(false);
                    break;


                }               //end switch

            } else if (item->getStatus() != first_item->getStatus()) {
                // disable all when all selected items don't have the same status
                m_paResume->setEnabled(false);
                m_paPause->setEnabled(false);
                m_paRestart->setEnabled(false);
            }


            if (item == first_item) {
                m_paDelete->setEnabled(true);
                m_paCopy->setEnabled(true);
                m_paIndividual->setEnabled(true);
                if (item->getStatus() != Transfer::ST_FINISHED) {
                    m_paQueue->setEnabled(true);
                    m_paTimer->setEnabled(true);
                    m_paDelay->setEnabled(true);

                    switch (item->getMode()) {
                    case Transfer::MD_QUEUED:
#ifdef _DEBUG
                        sDebug << "....................THE MODE  IS  MD_QUEUED " << item->getMode() << endl;
#endif
                        m_paQueue->setChecked(true);
                        break;
                    case Transfer::MD_SCHEDULED:
#ifdef _DEBUG
                        sDebug << "....................THE MODE  IS  MD_SCHEDULED " << item->getMode() << endl;
#endif
                        m_paTimer->setChecked(true);
                        break;
                    case Transfer::MD_DELAYED:
#ifdef _DEBUG
                        sDebug << "....................THE MODE  IS  MD_DELAYED " << item->getMode() << endl;
#endif
                        m_paDelay->setChecked(true);
                        break;
                    }
                }
            } else if (item->getMode() != first_item->getMode()) {
                // unset all when all selected items don't have the same mode
                m_paQueue->setChecked(false);
                m_paTimer->setChecked(false);
                m_paDelay->setChecked(false);
                m_paMoveToBegin->setEnabled(false);
                m_paMoveToEnd->setEnabled(false);
                m_paQueue->setEnabled(false);
                m_paTimer->setEnabled(false);
                m_paDelay->setEnabled(false);
            }

        }                       // when item is selected
    }                           // loop



    if (sel_items == totals_items - 1)
        m_paMoveToEnd->setEnabled(false);

    // enable all signals



    m_paQueue->blockSignals(false);
    m_paTimer->blockSignals(false);
    m_paDelay->blockSignals(false);


#ifdef _DEBUG
    sDebugOut << endl;
#endif
}


void TDEMainWidget::updateStatusBar()
{
#ifdef _DEBUG
    //sDebugIn << endl;
#endif

    Transfer *item;
    TQString tmpstr;

    int totalFiles = 0;
    TDEIO::filesize_t totalSize = 0;
    int totalSpeed = 0;
    unsigned int remTime = 0;

    TransferIterator it(myTransferList);

    for (; it.current(); ++it) {
        item = it.current();
        if (item->getTotalSize() != 0) {
            totalSize += (item->getTotalSize() - item->getProcessedSize());
        }
        totalFiles++;
        totalSpeed += item->getSpeed();

        if (item->getRemainingTime() > remTime) {
            remTime = item->getRemainingTime();
        }
    }

    statusBar()->changeItem(i18n(" Transfers: %1 ").arg(myTransferList->childCount()), ID_TOTAL_TRANSFERS);
    statusBar()->changeItem(i18n(" Files: %1 ").arg(totalFiles), ID_TOTAL_FILES);
    statusBar()->changeItem(i18n(" Size: %1 ").arg(TDEIO::convertSize(totalSize)), ID_TOTAL_SIZE);
    statusBar()->changeItem(i18n(" Time: %1 ").arg(TDEIO::convertSeconds(remTime)), ID_TOTAL_TIME);
    statusBar()->changeItem(i18n(" %1/s ").arg(TDEIO::convertSize(totalSpeed)), ID_TOTAL_SPEED);
    //update size for each statusbar field
    statusBar()->setItemFixed(ID_TOTAL_TRANSFERS, -1);
    statusBar()->setItemFixed(ID_TOTAL_FILES, -1);
    statusBar()->setItemFixed(ID_TOTAL_SIZE, -1);
    statusBar()->setItemFixed(ID_TOTAL_TIME, -1);
    statusBar()->setItemFixed(ID_TOTAL_SPEED, -1);

    if (kdock) {
        tmpstr = i18n("<b>Transfers:</b> %1 ").arg(myTransferList->childCount()) +
                 i18n("<br /><b>Files:</b> %1 ").arg(totalFiles) +
                 i18n("<br /><b>Size:</b> %1 ").arg(TDEIO::convertSize(totalSize)) +
                 i18n("<br /><b>Time:</b> %1 ").arg(TDEIO::convertSeconds(remTime)) +
                 i18n("<br /><b>Speed:</b> %1/s").arg(TDEIO::convertSize(totalSpeed));
        kdock->updateToolTip( tmpstr );
        //trayicon changes if download is in progress
        if (totalSpeed == 0)
        {
            kdock->changeIcon( "kget_dock" );
        }
        else
        {
            kdock->changeIcon( "kget_dock_download" );
        }
    }
#ifdef _DEBUG
    //sDebugOut << endl;
#endif
}


void TDEMainWidget::onlineDisconnect()
{
#ifdef _DEBUG
    sDebugIn << endl;
#endif

    if (!b_connected) {
        return;
    }

    if (!ksettings.b_expertMode) {
        if (KMessageBox::questionYesNo(this, i18n("Do you really want to disconnect?"),
                                       i18n("Question"),
                                       i18n("Disconnect"), i18n("Stay Connected"),
                                       "kget_AutoOnlineDisconnect")
            != KMessageBox::Yes) {
            return;
        }
    }
    log(i18n("Disconnecting..."));
    if (system(TQFile::encodeName(ksettings.disconnectCommand)) < 0) {
	// Error!
    }

#ifdef _DEBUG
    sDebugOut << endl;
#endif
}


void TDEMainWidget::slotCheckConnection()
{
#ifdef _DEBUG
    //sDebugIn << endl;
#endif
    bool old = b_connected;
    checkOnline();
    if (b_connected != old) {
        if (b_connected) {
            log(i18n("We are online."));
            setCaption(TQString(), false);
            ksettings.b_offline=ksettings.b_offlineMode;
            checkQueue();
        } else {
            log(i18n("We are offline."));
            setCaption(i18n("Offline"), false);
            ksettings.b_offline=true;
            stopAll();
        }
    }
#ifdef _DEBUG
    //sDebugOut << endl;
#endif
}


void TDEMainWidget::checkOnline()
{
#ifdef _DEBUG
    //sDebugIn << endl;
#endif

    struct ifreq ifr;

    memset(&ifr, 0, sizeof(ifreq));

    // setup the device name according to the type of connection and link number
    snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s%d", ConnectionDevices[ksettings.connectionType].ascii(), ksettings.linkNumber);

    bool flag = false;

    if (ksettings.connectionType != PERMANENT) {
        // get the flags for particular device
        if (ioctl(_sock, SIOCGIFFLAGS, &ifr) < 0) {
            flag = true;
            b_connected = false;
        } else if (ifr.ifr_flags == 0) {
#ifdef _DEBUG
            sDebug << "Can't get flags from interface " << ifr.ifr_name << endl;
#endif
            b_connected = false;
        } else if (ifr.ifr_flags & IFF_UP) {    // if (ifr.ifr_flags & IFF_RUNNING)
            b_connected = true;
        } else {
            b_connected = false;
        }
    } else {
        b_connected = true;        // PERMANENT connection
    }

    m_paOfflineMode->setEnabled(b_connected);

    if (flag) {
#ifdef _DEBUG
            sDebug << "Unknown interface " << ifr.ifr_name << endl;
#endif
        }
#ifdef _DEBUG
    //sDebugOut << endl;
#endif
}


// Helper method for opening device socket




static int sockets_open()
{
#ifdef _DEBUG
    sDebugIn << endl;
#endif

    inet_sock = socket(AF_INET, SOCK_DGRAM, 0);
#ifdef AF_IPX
    ipx_sock = socket(AF_IPX, SOCK_DGRAM, 0);
#else
    ipx_sock = -1;
#endif

#ifdef AF_AX25
    ax25_sock = socket(AF_AX25, SOCK_DGRAM, 0);
#else
    ax25_sock = -1;
#endif

    ddp_sock = socket(AF_APPLETALK, SOCK_DGRAM, 0);
    /*
     *    Now pick any (exisiting) useful socket family for generic queries
     */

    sDebug << "<<<<Leaving -> sockets_open () " << endl;
    if (inet_sock != -1)
        return inet_sock;
    if (ipx_sock != -1)
        return ipx_sock;
    if (ax25_sock != -1)
        return ax25_sock;
    /*
     *    If this is -1 we have no known network layers and its time to jump.
     */

#ifdef _DEBUG
    sDebugOut << endl;
#endif

    return ddp_sock;
}


/** No descriptions */
void TDEMainWidget::customEvent(TQCustomEvent * _e)
{
#ifdef _DEBUG
    //sDebugIn << endl;
#endif


    SlaveEvent *e = (SlaveEvent *) _e;
    unsigned int result = e->getEvent();

    switch (result) {

        // running cases..
    case Slave::SLV_PROGRESS_SIZE:
        e->getItem()->slotProcessedSize(e->getData());
        break;
    case Slave::SLV_PROGRESS_SPEED:
        e->getItem()->slotSpeed(e->getData());
        break;

    case Slave::SLV_RESUMED:
        e->getItem()->slotExecResume();
        break;

        // stopping cases
    case Slave::SLV_FINISHED:
        e->getItem()->slotFinished();
        break;
    case Slave::SLV_PAUSED:
        e->getItem()->slotExecPause();
        break;
    case Slave::SLV_SCHEDULED:
        e->getItem()->slotExecSchedule();
        break;

    case Slave::SLV_DELAYED:
        e->getItem()->slotExecDelay();
        break;
    case Slave::SLV_CONNECTED:
        e->getItem()->slotExecConnected();
        break;

    case Slave::SLV_CAN_RESUME:
        e->getItem()->slotCanResume((bool) e->getData());
        break;

    case Slave::SLV_TOTAL_SIZE:
        e->getItem()->slotTotalSize(e->getData());
        break;

    case Slave::SLV_ERROR:
        e->getItem()->slotExecError();
        break;

    case Slave::SLV_BROKEN:
        e->getItem()->slotExecBroken();
        break;

    case Slave::SLV_REMOVED:
        e->getItem()->slotExecRemove();
        break;

    case Slave::SLV_INFO:
        e->getItem()->logMessage(e->getMsg());
        break;
        
        case Slave::SLV_NOTINCACHE:
        e->getItem()->NotInCache();
        break;
    default:
#ifdef _DEBUG
        sDebug << "Unknown Result..die" << result << endl;
#endif
        assert(0);
    }


#ifdef _DEBUG
    //sDebugOut << endl;
#endif
}

TQString TDEMainWidget::getSaveDirectoryFor( const TQString& filename ) const
{
    // first set destination directory to current directory ( which is also last used )
    TQString destDir = ksettings.lastDirectory;

    if (!ksettings.b_useLastDir) {
        // check wildcards for default directory
        DirList::Iterator it;
        for (it = ksettings.defaultDirList.begin(); it != ksettings.defaultDirList.end(); ++it) {
            TQRegExp rexp((*it).extRegexp);

            rexp.setWildcard(true);

            if ((rexp.search( filename )) != -1) {
                destDir = (*it).defaultDir;
                break;
            }
        }
    }

    return destDir;
}

bool TDEMainWidget::sanityChecksSuccessful( const KURL& url )
{
    if (!url.isValid() || !KProtocolInfo::supportsReading( url ) )
    {
        if (!ksettings.b_expertMode)
            KMessageBox::error(this, i18n("Malformed URL:\n%1").arg(url.prettyURL()), i18n("Error"));

        return false;
    }
    // if we find this URL in the list
    Transfer *transfer = myTransferList->find( url );
    if ( transfer )
    {
        if ( transfer->getStatus() != Transfer::ST_FINISHED )
        {
            if ( !ksettings.b_expertMode )
            {
                KMessageBox::error(this, i18n("Already saving URL\n%1").arg(url.prettyURL()), i18n("Error"));
            }

            transfer->showIndividual();
            return false;
        }

        else // transfer is finished, ask if we want to download again
        {
            if ( ksettings.b_expertMode ||
                 (KMessageBox::questionYesNo(this, i18n("Already saved URL\n%1\nDownload again?").arg(url.prettyURL()),i18n("Question"),i18n("Download Again"),KStdGuiItem::cancel() )
                     == KMessageBox::Yes) )
            {
                transfer->slotRequestRemove();
                checkQueue();
                return true;
            }
        }

        return false;
    }

    // why restrict this to ftp and http? (pfeiffer)
//     // don't download file URL's TODO : uncomment?
//     if (url.protocol() == "http" && url.protocol() != "ftp") {
//         KMessageBox::error(this, i18n("File protocol not accepted!\n%1").arg(url.prettyURL()), i18n("Error"));
// #ifdef _DEBUG
//         sDebugOut << endl;
// #endif
//         return false;
//     }

    return true;
}

bool TDEMainWidget::isDropTargetVisible() const
{
    return m_showDropTarget;
}

void TDEMainWidget::setDropTargetVisible( bool setVisible )
{
    if ( setVisible != isDropTargetVisible() )
    {
        m_paDropTarget->activate();
    }
}

void TDEMainWidget::setOfflineMode( bool offline )
{
    if ( ksettings.b_offlineMode != offline )
        slotToggleOfflineMode();
}

bool TDEMainWidget::isOfflineMode() const
{
    return ksettings.b_offlineMode;
}

void TDEMainWidget::activateDropTarget()
{
    setDropTargetVisible( true );
}

void TDEMainWidget::slotShowMenubar()
{
    if(m_menubarAction->isChecked())
        menuBar()->show();
    else
        menuBar()->hide();
}

#include "tdemainwidget.moc"
