<commit_before>/*
    ksubscription.cpp

    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.
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software Foundation,
    Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, US
*/

#include "ksubscription.h"
#include "kaccount.h"

#include <qlayout.h>
#include <qtimer.h>
#include <qlabel.h>
#include <qpushbutton.h>

#include <kseparator.h>
#include <kiconloader.h>
#include <klocale.h>
#include <kdebug.h>
#include <klineedit.h>


//=============================================================================

KGroupInfo::KGroupInfo(const QString &name, const QString &description, 
    bool newGroup, bool subscribed, 
    Status status, QString path)
  : name(name), description(description), 
    newGroup(newGroup), subscribed(subscribed),
    status(status), path(path)
{
}

//-----------------------------------------------------------------------------
bool KGroupInfo::operator== (const KGroupInfo &gi2)
{
  return (name == gi2.name);
}

//-----------------------------------------------------------------------------
bool KGroupInfo::operator< (const KGroupInfo &gi2)
{
  return (name < gi2.name);
}

//=============================================================================

GroupItem::GroupItem( QListView *v, const KGroupInfo &gi, KSubscription* browser, 
    bool isCheckItem )
  : QCheckListItem( v, gi.name, isCheckItem ? CheckBox : Controller ), 
    mInfo( gi ), mBrowser( browser ), mIsCheckItem( isCheckItem )
{
  if (listView()->columns() > 1)
    setDescription();
}

//-----------------------------------------------------------------------------
GroupItem::GroupItem( QListViewItem *i, const KGroupInfo &gi, KSubscription* browser, 
    bool isCheckItem )
  : QCheckListItem( i, gi.name, isCheckItem ? CheckBox : Controller ), 
    mInfo( gi ), mBrowser( browser ), mIsCheckItem( isCheckItem )
{
  if (listView()->columns() > 1)
    setDescription();
}

//-----------------------------------------------------------------------------
void GroupItem::setDescription()
{
  setText(1, mInfo.description);  
}

//-----------------------------------------------------------------------------
void GroupItem::setOn( bool on )
{
  if (mBrowser->isLoading())
  {
    mInfo.subscribed = on;
  }
  if (isCheckItem())
    QCheckListItem::setOn(on);
}

//------------------------------------------------------------------------------
void GroupItem::stateChange( bool on )
{
  // delegate to parent
  mBrowser->changeItemState(this, on);
}

//------------------------------------------------------------------------------
void GroupItem::setVisible( bool b )
{
  if (b)
  {
    QListViewItem::setVisible(b);
    setEnabled(true);
  }
  else
  {
    if (isCheckItem())
    {
      bool setInvisible = true;
      for (QListViewItem * lvchild = firstChild(); lvchild != 0; 
          lvchild = lvchild->nextSibling())
      {
        if (lvchild->isVisible()) // item has a visible child
          setInvisible = false;
      }
      if (setInvisible)
        QListViewItem::setVisible(b);
      else
      {
        // leave it visible so that childs remain visible
        setOpen(true);
        setEnabled(false);
      }
    }
    else
    {
      // non-checkable item
      QPtrList<QListViewItem> moveItems;

      for (QListViewItem * lvchild = firstChild(); lvchild != 0; 
          lvchild = lvchild->nextSibling())
      {
        if (static_cast<GroupItem*>(lvchild)->isCheckItem())
        {
          // remember the items
          moveItems.append(lvchild);
        }
      }
      QPtrListIterator<QListViewItem> it( moveItems );
      for ( ; it.current(); ++it)
      {
        // move the checkitem to top
        QListViewItem* parent = it.current()->parent();
        if (parent) parent->takeItem(it.current());
        listView()->insertItem(it.current());
      }
      QListViewItem::setVisible(false);
    }
  }
}

//-----------------------------------------------------------------------------
void GroupItem::paintCell( QPainter * p, const QColorGroup & cg,
    int column, int width, int align )
{
  if (mIsCheckItem)
    return QCheckListItem::paintCell( p, cg, column, width, align );
  else
    return QListViewItem::paintCell( p, cg, column, width, align );
}

//-----------------------------------------------------------------------------
void GroupItem::paintFocus( QPainter * p, const QColorGroup & cg,
    const QRect & r )
{
  if (mIsCheckItem)
    QCheckListItem::paintFocus(p, cg, r);
  else
    QListViewItem::paintFocus(p, cg, r);
}

//-----------------------------------------------------------------------------
int GroupItem::width( const QFontMetrics& fm, const QListView* lv, int column) const
{
  if (mIsCheckItem)
    return QCheckListItem::width(fm, lv, column);
  else
    return QListViewItem::width(fm, lv, column);
}

//-----------------------------------------------------------------------------
void GroupItem::setup()
{
  if (mIsCheckItem)
    QCheckListItem::setup();
  else
    QListViewItem::setup();
}


//=============================================================================

KSubscription::KSubscription( QWidget *parent, const QString &caption,
    KAccount * acct, int buttons, const QString &user1, bool descriptionColumn )
  : KDialogBase( parent, 0, true, caption, buttons | Help | Ok | Cancel, Ok, 
      true, i18n("Reload &List"), user1 ),
    mAcct( acct )
{
  mLoading = true;

  // create Widgets
  page = new QWidget(this);
  setMainWidget(page);  

  filterEdit = new KLineEdit(page);
  QLabel *l = new QLabel(filterEdit,i18n("S&earch:"), page);

  // checkboxes
  noTreeCB = new QCheckBox(i18n("Disable &tree view"), page);
  noTreeCB->setChecked(false);
  subCB = new QCheckBox(i18n("&Subscribed only"), page);
  subCB->setChecked(false);
  newCB = new QCheckBox(i18n("&New only"), page);
  newCB->setChecked(false);

  KSeparator *sep = new KSeparator(KSeparator::HLine, page);  <commit_msg>Fix the magically multiplying folders bug in the subscription dialog by making sure that it doesn't hang around.<commit_after>/*
    ksubscription.cpp

    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.
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software Foundation,
    Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, US
*/

#include "ksubscription.h"
#include "kaccount.h"

#include <qlayout.h>
#include <qtimer.h>
#include <qlabel.h>
#include <qpushbutton.h>

#include <kseparator.h>
#include <kiconloader.h>
#include <klocale.h>
#include <kdebug.h>
#include <klineedit.h>


//=============================================================================

KGroupInfo::KGroupInfo(const QString &name, const QString &description, 
    bool newGroup, bool subscribed, 
    Status status, QString path)
  : name(name), description(description), 
    newGroup(newGroup), subscribed(subscribed),
    status(status), path(path)
{
}

//-----------------------------------------------------------------------------
bool KGroupInfo::operator== (const KGroupInfo &gi2)
{
  return (name == gi2.name);
}

//-----------------------------------------------------------------------------
bool KGroupInfo::operator< (const KGroupInfo &gi2)
{
  return (name < gi2.name);
}

//=============================================================================

GroupItem::GroupItem( QListView *v, const KGroupInfo &gi, KSubscription* browser, 
    bool isCheckItem )
  : QCheckListItem( v, gi.name, isCheckItem ? CheckBox : Controller ), 
    mInfo( gi ), mBrowser( browser ), mIsCheckItem( isCheckItem )
{
  if (listView()->columns() > 1)
    setDescription();
}

//-----------------------------------------------------------------------------
GroupItem::GroupItem( QListViewItem *i, const KGroupInfo &gi, KSubscription* browser, 
    bool isCheckItem )
  : QCheckListItem( i, gi.name, isCheckItem ? CheckBox : Controller ), 
    mInfo( gi ), mBrowser( browser ), mIsCheckItem( isCheckItem )
{
  if (listView()->columns() > 1)
    setDescription();
}

//-----------------------------------------------------------------------------
void GroupItem::setDescription()
{
  setText(1, mInfo.description);  
}

//-----------------------------------------------------------------------------
void GroupItem::setOn( bool on )
{
  if (mBrowser->isLoading())
  {
    mInfo.subscribed = on;
  }
  if (isCheckItem())
    QCheckListItem::setOn(on);
}

//------------------------------------------------------------------------------
void GroupItem::stateChange( bool on )
{
  // delegate to parent
  mBrowser->changeItemState(this, on);
}

//------------------------------------------------------------------------------
void GroupItem::setVisible( bool b )
{
  if (b)
  {
    QListViewItem::setVisible(b);
    setEnabled(true);
  }
  else
  {
    if (isCheckItem())
    {
      bool setInvisible = true;
      for (QListViewItem * lvchild = firstChild(); lvchild != 0; 
          lvchild = lvchild->nextSibling())
      {
        if (lvchild->isVisible()) // item has a visible child
          setInvisible = false;
      }
      if (setInvisible)
        QListViewItem::setVisible(b);
      else
      {
        // leave it visible so that childs remain visible
        setOpen(true);
        setEnabled(false);
      }
    }
    else
    {
      // non-checkable item
      QPtrList<QListViewItem> moveItems;

      for (QListViewItem * lvchild = firstChild(); lvchild != 0; 
          lvchild = lvchild->nextSibling())
      {
        if (static_cast<GroupItem*>(lvchild)->isCheckItem())
        {
          // remember the items
          moveItems.append(lvchild);
        }
      }
      QPtrListIterator<QListViewItem> it( moveItems );
      for ( ; it.current(); ++it)
      {
        // move the checkitem to top
        QListViewItem* parent = it.current()->parent();
        if (parent) parent->takeItem(it.current());
        listView()->insertItem(it.current());
      }
      QListViewItem::setVisible(false);
    }
  }
}

//-----------------------------------------------------------------------------
void GroupItem::paintCell( QPainter * p, const QColorGroup & cg,
    int column, int width, int align )
{
  if (mIsCheckItem)
    return QCheckListItem::paintCell( p, cg, column, width, align );
  else
    return QListViewItem::paintCell( p, cg, column, width, align );
}

//-----------------------------------------------------------------------------
void GroupItem::paintFocus( QPainter * p, const QColorGroup & cg,
    const QRect & r )
{
  if (mIsCheckItem)
    QCheckListItem::paintFocus(p, cg, r);
  else
    QListViewItem::paintFocus(p, cg, r);
}

//-----------------------------------------------------------------------------
int GroupItem::width( const QFontMetrics& fm, const QListView* lv, int column) const
{
  if (mIsCheckItem)
    return QCheckListItem::width(fm, lv, column);
  else
    return QListViewItem::width(fm, lv, column);
}

//-----------------------------------------------------------------------------
void GroupItem::setup()
{
  if (mIsCheckItem)
    QCheckListItem::setup();
  else
    QListViewItem::setup();
}


//=============================================================================

KSubscription::KSubscription( QWidget *parent, const QString &caption,
    KAccount * acct, int buttons, const QString &user1, bool descriptionColumn )
  : KDialogBase( parent, 0, true, caption, buttons | Help | Ok | Cancel, Ok, 
      true, i18n("Reload &List"), user1 ),
    mAcct( acct )
{
  mLoading = true;
  setWFlags( getWFlags() | WDestructiveClose );

  // create Widgets
  page = new QWidget(this);
  setMainWidget(page);  

  filterEdit = new KLineEdit(page);
  QLabel *l = new QLabel(filterEdit,i18n("S&earch:"), page);

  // checkboxes
  noTreeCB = new QCheckBox(i18n("Disable &tree view"), page);
  noTreeCB->setChecked(false);
  subCB = new QCheckBox(i18n("&Subscribed only"), page);
  subCB->setChecked(false);
  newCB = new QCheckBox(i18n("&New only"), page);
  newCB->setChecked(false);

  KSeparator *sep = new KSeparator(KSeparator::HLine, page);  
{'fraction_non_alphanumeric': 0.2798001427551749, 'fraction_numerical': 0.0058688238559758904, 'mean_word_length': 2.03125, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1984063', 'n_tokens_mistral': 3620, 'n_tokens_neox': 3500, 'n_words': 908}