/***********************************************************
** Copyright (c) 2009 NGuide
** 
** Contact: vasa_pmr@mail.ru
**
** This file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** If you are unsure which license is appropriate for your use, please
** contact me   
 **********************************************************/

#include "itaskbar.h"
#include "comintf.h"

#include <QtCore/QString>
#include <QtCore/QSysInfo>

using namespace Windows7Support;
using namespace Windows7Support::Private;

static ITaskBar *m_instance = 0;

ITaskBar::ITaskBar()
{
    m_instance = this;
}

ITaskBar *ITaskBar::instance()
{
    return m_instance;
}

ITaskbarList3 *ITaskBar::initInterface() const
{
    ITaskbarList3 *ITASKBARLIST_REFERENSE;

    if (QSysInfo::WindowsVersion != QSysInfo::WV_WINDOWS7)
        return 0; // not windows 7

    if (CoCreateInstance(CLSID_TaskbarList, NULL, CLSCTX_INPROC_SERVER,
                         IID_ITaskbarList3, (void**)&ITASKBARLIST_REFERENSE) != S_OK)
        return 0; // error

    if (ITASKBARLIST_REFERENSE->HrInit() != S_OK)
        return 0; // error

    return ITASKBARLIST_REFERENSE;
}

void ITaskBar::releaseInterface(ITaskbarList3 *intf) const
{
    intf->Release();
}


//! [BUG SOURCE]

HICON ITaskBar::iconToHICON(const QIcon &icon) const
{
//    Uncomment this for no explorer crashing.
    if (icon.isNull())
        return 0;
    else
        return icon.pixmap(32).toWinHICON();
}

//! [BUG SOURCE]

///////////////////////////////////////////////////////

bool  ITaskBar::setProgressState(WId window, TaskBarProgress state) const
{
    CHECK_HRESULT_AND_RELEASE(ITASKBARLIST_REFERENSE->SetProgressState(window,
        (TBPFLAG)state));
}

bool ITaskBar::setProgressValue(WId window, ULONGLONG completed, ULONGLONG total) const
{
    CHECK_HRESULT_AND_RELEASE(ITASKBARLIST_REFERENSE->SetProgressValue(window, completed, total))
}

bool ITaskBar::setOverlayIcon(WId window, const QIcon &overlayIcon, const QString &description) const
{
    CHECK_HRESULT_AND_RELEASE(ITASKBARLIST_REFERENSE->SetOverlayIcon(window,
        iconToHICON(overlayIcon), (LPWSTR)description.utf16()))
}

bool ITaskBar::setThumbinalBarButtons(WId window, const QList<ThumbinalBarButton> &buttons) const
{
    THUMBBUTTON p_buttons[255];

    for (int i = 0; i < buttons.count(); i++) {
        p_buttons[i].iId = buttons.at(i).id;
        p_buttons[i].hIcon = iconToHICON(buttons.at(i).icon);
        //wcsncpy((wchar_t*)p_buttons[i].szTip, (wchar_t*)buttons.at(i).tip.utf16(), 260);
        p_buttons[i].dwMask = THB_ICON | /*THB_TOOLTIP |*/ THB_FLAGS;

        // FLAGS
        if (!buttons.at(i).enabled)
            p_buttons[i].dwFlags = p_buttons[i].dwFlags | THBF_DISABLED;
        if (!buttons.at(i).visible)
            p_buttons[i].dwFlags = p_buttons[i].dwFlags | THBF_HIDDEN;
        if (!buttons.at(i).interactive)
            p_buttons[i].dwFlags = p_buttons[i].dwFlags | THBF_NONINTERACTIVE;
        if (!buttons.at(i).drawBackground)
            p_buttons[i].dwFlags = p_buttons[i].dwFlags | THBF_NOBACKGROUND;
        if (buttons.at(i).closeThumbBarAfterClick)
            p_buttons[i].dwFlags = p_buttons[i].dwFlags | THBF_DISMISSONCLICK;
    }

    CHECK_HRESULT_AND_RELEASE(ITASKBARLIST_REFERENSE->ThumbBarAddButtons(window,
                                                      buttons.count(), p_buttons))
}
