/***************************************************************************
                          dcconnectionmanager.h  -  description
                             -------------------
    begin                : Mon Oct 1 2001
    copyright            : (C) 2001-2005 by Mathias Küster
    email                : mathen@users.berlios.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#ifndef DCCONNECTIONMANAGER_H
#define DCCONNECTIONMANAGER_H

/**
 *@author Mathias Küster
 */

#include <QObject>
#include <QTimer>
#include <QEvent>
#include <QList>
#include <QMap>
#include <QIcon>
//#include <QtGlobal>

#include <dclib/cconnectionmanager.h>

#include "dcgui.h"

//#if QT_VERSION >= 0x040500
//#define QT_45
//#endif


class QMdiArea;
class QMdiSubWindow;
class QToolBar;
class QTabBar;
class DCClient;
class CDCMessage;

class DCConnectionManager : public QObject, public CConnectionManager {
    Q_OBJECT

public:
    /** construtor */
    DCConnectionManager(QObject * parent = 0);
    /** destructor */
    virtual ~DCConnectionManager();

    /** */
    void InitDocument();

    /** */
    void AutoConnect();
    /** */
    void Connect(CString hubname, CString server, bool sslconnect = false);

    /** */
    void OPKick(QString hubname, QString hubhost, QString nick, QString message);
    /** */
    void OPForceMove(QString hubname, QString hubhost, QString nick, QString message, QString host);

    /** */
    void OpenPrivateChat(QString hubname, QString hubhost, QString nick);
    /** */
    void CloseAllChats(bool onlyOffline);
    /** */
    void DisconnectAllClients();
    /** */
    void CloseDisconnectedHubs();
    /** */
    void CloseAllTabs();

    /** */
    void InitTabBar(QToolBar * toolbar);
    /** */
    DCTabBar *getTabBar() const
    { return m_pTabBar; }
    /** */
    void addTab(QWidget*, QString);
    /** */
    void remTab(QWidget *wgt, bool disc = true, QWidget *par = DCGuiApp::Instance());
    /** */
    void repaintTabbarIcons();

    /** callback function */
    virtual int DC_CallBack(CDCMessage *);

    /** */
    void HubEvent(DCClient * client);
    /** */
    void CaptionChanged(DCClient * client);
    /** */
    void HubClosing(DCClient * client);

    /**
     * Finds the DCClient for a hub.
     * Needed for User Commands in Filelist browser, transfer list
     * and search.
     */
    DCClient * GetClientForHub(CString hubname, CString hubhost);

    /**
     * Just to stop every window having it's own pointer.
     */
    QMdiArea * GetMdiArea() {
        return m_pMdiArea;
    }

    /** */
    QWidget *GetWidgetForTab(int) const;

    /** */
    static void SetInstance(DCConnectionManager *);
    /** */
    static DCConnectionManager *Instance();

private:
    /** */
    QTimer m_Timer;
    /** */
    DCTabBar * m_pTabBar;
    /** */
    QList<CDCMessage*> * m_pMessageList;
    /** */
    QMdiArea * m_pMdiArea;
    /** */
    int m_nSelectedTab;
    /** */
    QMap<QWidget*, int> windowsList;
    /** */
    static DCConnectionManager *g_pConnectionManager;

private slots:
    /** */
    void timerDone();
    /** */
    void slotTabSelected(int);
    /** widget from mdi area activated */
    void slotMdiSubWindowActivated(QMdiSubWindow * w);

    /** */
    void slotCloseTabButtonClicked(int);
    /** */
    void slotTabMoved(int from, int to);

};

namespace DC {
    class DCConnectionManager;
}

/** */
inline void DCConnectionManager::SetInstance(DCConnectionManager *mgr) {
    g_pConnectionManager = mgr;
}

/** */
inline DCConnectionManager *DCConnectionManager::Instance() {
    return g_pConnectionManager;
}

#endif
