/*
 * Copyright (C) 2003-2004 Adam Geitgey <adam@rootnode.org>
 * Copyright (c) 2005 Ryan Nickell <p0z3r@earthlink.net>
 *
 * This file is part of SuperKaramba.
 *
 *  SuperKaramba 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.
 *
 *  SuperKaramba 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 SuperKaramba; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 ****************************************************************************/
#include <twinmodule.h>
#include <netwm.h>
#include <twin.h>

#include "karambaapp.h"
#include "showdesktop.h"
#include "showdesktop.moc"

ShowDesktop* ShowDesktop::the()
{
    static ShowDesktop showDesktop;
    return &showDesktop;
}

ShowDesktop::ShowDesktop()
  : TQObject()
  , showingDesktop( false )
  , tWinModule( 0 )
{
    tWinModule = new TWinModule( this );

    // on desktop changes or when a window is deiconified, we abort the show desktop mode
    connect( tWinModule, TQ_SIGNAL(currentDesktopChanged(int)),
             TQ_SLOT(slotCurrentDesktopChanged(int)));
    connect( tWinModule, TQ_SIGNAL(windowChanged(WId,unsigned int)),
             TQ_SLOT(slotWindowChanged(WId,unsigned int)));
}

void ShowDesktop::slotCurrentDesktopChanged(int)
{
    showDesktop( false );
}

void ShowDesktop::slotWindowChanged(WId w, unsigned int dirty)
{
  if (!showingDesktop)
    return;

  // SELI this needs checking for twin_iii (_NET_SHOWING_DESKTOP)
  if ( dirty & NET::XAWMState )
  {
    NETWinInfo inf(tqt_xdisplay(), w, tqt_xrootwin(),
                   NET::XAWMState | NET::WMWindowType);
    NET::WindowType windowType = inf.windowType(NET::AllTypesMask);
    if ((windowType == NET::Normal || windowType == NET::Unknown)
        && inf.mappingState() == NET::Visible )
    {
      // a window was deiconified, abort the show desktop mode.
      iconifiedList.clear();
      showingDesktop = false;
      emit desktopShown( false );
    }
  }
}

void ShowDesktop::showDesktop( bool b )
{
    if( b == showingDesktop ) return;
    showingDesktop = b;

    if ( b ) {
        // this code should move to KWin after supporting NETWM1.2
        iconifiedList.clear();
        const TQValueList<WId> windows = tWinModule->windows();
        TQValueList<WId>::ConstIterator it;
        TQValueList<WId>::ConstIterator end( windows.end() );
        for ( it=windows.begin(); it!=end; ++it ) {
            WId w = *it;
            NETWinInfo info( tqt_xdisplay(), w, tqt_xrootwin(),
                             NET::XAWMState | NET::WMDesktop );
            if ( info.mappingState() == NET::Visible &&
                 ( info.desktop() == NETWinInfo::OnAllDesktops
                   || info.desktop() == (int) tWinModule->currentDesktop() )
                ) {
                iconifiedList.append( w );
            }
        }
        // find first, hide later, otherwise transients may get minimized
        // with the window they're transient for
        TQValueList<WId>::ConstIterator endInconifiedList( iconifiedList.end() );
        for ( it=iconifiedList.begin(); it!=endInconifiedList; ++it ) {
            KWin::iconifyWindow( *it, false );
        }
    } else {
        TQValueList<WId>::ConstIterator it;
        TQValueList<WId>::ConstIterator end( iconifiedList.end() );
        for ( it=iconifiedList.begin(); it!=end; ++it ) {
            KWin::deIconifyWindow( *it, false  );
        }
    }

    emit desktopShown( showingDesktop );
}
