<commit_before>/******************************************************************************
 *
 * Copyright 2008 <NAME> <<EMAIL>>
 *
 * Based on kmfoldertree.cpp which was Copyrighted by the KMail Development
 * Team and didn't report detailed author list.
 *
 * 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.
 *
 * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#include "mainfolderview.h"

#include "globalsettings.h"

#include "kmfolder.h"
#include "kmfoldermgr.h"
#include "kmmainwidget.h"
#include "util.h"

#include <kmenu.h>
#include <kicon.h>
#include <klocale.h>

#include <QMenu>
#include <QAction>
#include <QVariant>

#include <libkdepim/broadcaststatus.h>

namespace KMail
{

MainFolderView::MainFolderView( KMMainWidget *mainWidget, FolderViewManager *manager, QWidget *parent, const char *name )
: FolderView( mainWidget, manager, parent, "MainFolderView", name )
{
  setIconSize( QSize( 16, 16 ) ); // default to smaller icons
}

FolderViewItem * MainFolderView::createItem(
    FolderViewItem *parent,
    const QString &label,
    KMFolder *folder,
    KPIM::FolderTreeWidgetItem::Protocol proto,
    KPIM::FolderTreeWidgetItem::FolderType type
  )
{
  if ( parent )
    return new FolderViewItem( parent, label, folder, proto, type );
  return new FolderViewItem( this, label, folder, proto, type );
}

//=====================================================================================
// Contextual menu related stuff
//

void MainFolderView::fillContextMenuTreeStructureRelatedActions( KMenu *menu, FolderViewItem *item, bool multiSelection )
{
  KMFolder *folder = item->folder();

  if ( !multiSelection )
  {
    // Treat the special case of the root and account folders 
    if ( ( !folder ) || ( folder->noContent() && ( item->folderType() == FolderViewItem::Root ) ) )
    {
      if ( folder || ( ( item->folderType() == FolderViewItem::Root ) && ( item->protocol() == FolderViewItem::Local ) ) )
      {
        // local root or account folder
        QString createChild = folder ? i18n( "&New Subfolder..." ) : i18n( "&New Folder..." );

        menu->addAction(
            KIcon("folder-new"), createChild,
            this, SLOT( slotAddChildFolder() )
          );
      }
    } else {
      // regular (non root, non account) folders

      if ( !folder->noChildren() )
        menu->addAction(
            KIcon( "folder-new" ), i18n( "&New Subfolder..." ),
            this, SLOT( slotAddChildFolder() )
          );
    }
  }

  if ( folder )
  {
    // These two will work also for multiple folders

    if( item->folderType() != FolderViewItem::Root ) // disable copy/move for root folders since the underlying copy/move code screws up completly on this
    {
      // copy folder
      QMenu *copyMenu = menu->addMenu( KIcon("edit-copy"),
                                       multiSelection ? i18n( "&Copy Folders To" ) :
                                                        i18n( "&Copy Folder To" ) );
      folderToPopupMenu( CopyFolder, this, copyMenu );

      // FIXME: if AT LEAST ONE folder is moveable
      if ( folder && folder->isMoveable() )
      {
        QMenu *moveMenu = menu->addMenu( KIcon( "go-jump" ),
                                         multiSelection ? i18n( "&Move Folders To" ) :
                                                          i18n( "&Move Folder To" ) );
        folderToPopupMenu( MoveFolder, this, moveMenu );
      }
    }

    if ( !multiSelection && !folder->isSystemFolder() )
      menu->addAction( mainWidget()->action( "delete_folder" ) );
  }
}


void MainFolderView::fillContextMenuViewStructureRelatedActions( KMenu *menu, FolderViewItem *item, bool /*multiSelection*/ )
{
  KMFolder * folder = item->folder();
  if ( !folder )
    return;

  if ( folder->noContent() )
    return;

  if ( !GlobalSettings::self()->enableFavoriteFolderView() )
    return;

  // this works for multiple folders too
  menu->addAction(
     KIcon("bookmark-new"), i18n("Add to Favorite Folders"),
     this, SLOT( slotAddToFavorites() )
   );
}

//=====================================================================================
// Folder Tree Structure to Popup Menu
//

void MainFolderView::folderToPopupMenuInternal(
      MenuAction action, QObject * target, QMenu *menu, QTreeWidgetItem * parentItem
    )
{
  int childCount = parentItem->childCount();

  for ( int idx = 0; idx < childCount; idx++ )
  {
    FolderViewItem * fvi = static_cast< FolderViewItem * >( parentItem->child( idx ) );

    if ( !fvi )
      continue;

    // skip search folders
    if ( fvi->protocol() == KPIM::FolderTreeWidgetItem::Search )
      continue;

    QString label = fvi->labelText();
    label.replace( "&","&&" );

    if ( fvi->childCount() > 0 )
    {
      // new level
      QMenu* popup = new QMenu( menu );

      popup->setObjectName( "subMenu" );
      popup->setTitle( label );

      folderToPopupMenuInternal( action, target, popup, fvi );

      bool subMenu = false;
      if ( ( action == MoveMessage || action == CopyMessage ) &&
           fvi->folder() && !fvi->folder()->noContent() )
        subMenu = true;
      if ( ( action == MoveFolder || action == CopyFolder )
          && ( !fvi->folder() || ( fvi->folder() && !fvi->folder()->noChildren() ) ) )<commit_msg>krazy fix: single-char QString operations for efficiency.<commit_after>/******************************************************************************
 *
 * Copyright 2008 <NAME> <<EMAIL>>
 *
 * Based on kmfoldertree.cpp which was Copyrighted by the KMail Development
 * Team and didn't report detailed author list.
 *
 * 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.
 *
 * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#include "mainfolderview.h"

#include "globalsettings.h"

#include "kmfolder.h"
#include "kmfoldermgr.h"
#include "kmmainwidget.h"
#include "util.h"

#include <kmenu.h>
#include <kicon.h>
#include <klocale.h>

#include <QMenu>
#include <QAction>
#include <QVariant>

#include <libkdepim/broadcaststatus.h>

namespace KMail
{

MainFolderView::MainFolderView( KMMainWidget *mainWidget, FolderViewManager *manager, QWidget *parent, const char *name )
: FolderView( mainWidget, manager, parent, "MainFolderView", name )
{
  setIconSize( QSize( 16, 16 ) ); // default to smaller icons
}

FolderViewItem * MainFolderView::createItem(
    FolderViewItem *parent,
    const QString &label,
    KMFolder *folder,
    KPIM::FolderTreeWidgetItem::Protocol proto,
    KPIM::FolderTreeWidgetItem::FolderType type
  )
{
  if ( parent )
    return new FolderViewItem( parent, label, folder, proto, type );
  return new FolderViewItem( this, label, folder, proto, type );
}

//=====================================================================================
// Contextual menu related stuff
//

void MainFolderView::fillContextMenuTreeStructureRelatedActions( KMenu *menu, FolderViewItem *item, bool multiSelection )
{
  KMFolder *folder = item->folder();

  if ( !multiSelection )
  {
    // Treat the special case of the root and account folders 
    if ( ( !folder ) || ( folder->noContent() && ( item->folderType() == FolderViewItem::Root ) ) )
    {
      if ( folder || ( ( item->folderType() == FolderViewItem::Root ) && ( item->protocol() == FolderViewItem::Local ) ) )
      {
        // local root or account folder
        QString createChild = folder ? i18n( "&New Subfolder..." ) : i18n( "&New Folder..." );

        menu->addAction(
            KIcon("folder-new"), createChild,
            this, SLOT( slotAddChildFolder() )
          );
      }
    } else {
      // regular (non root, non account) folders

      if ( !folder->noChildren() )
        menu->addAction(
            KIcon( "folder-new" ), i18n( "&New Subfolder..." ),
            this, SLOT( slotAddChildFolder() )
          );
    }
  }

  if ( folder )
  {
    // These two will work also for multiple folders

    if( item->folderType() != FolderViewItem::Root ) // disable copy/move for root folders since the underlying copy/move code screws up completly on this
    {
      // copy folder
      QMenu *copyMenu = menu->addMenu( KIcon("edit-copy"),
                                       multiSelection ? i18n( "&Copy Folders To" ) :
                                                        i18n( "&Copy Folder To" ) );
      folderToPopupMenu( CopyFolder, this, copyMenu );

      // FIXME: if AT LEAST ONE folder is moveable
      if ( folder && folder->isMoveable() )
      {
        QMenu *moveMenu = menu->addMenu( KIcon( "go-jump" ),
                                         multiSelection ? i18n( "&Move Folders To" ) :
                                                          i18n( "&Move Folder To" ) );
        folderToPopupMenu( MoveFolder, this, moveMenu );
      }
    }

    if ( !multiSelection && !folder->isSystemFolder() )
      menu->addAction( mainWidget()->action( "delete_folder" ) );
  }
}


void MainFolderView::fillContextMenuViewStructureRelatedActions( KMenu *menu, FolderViewItem *item, bool /*multiSelection*/ )
{
  KMFolder * folder = item->folder();
  if ( !folder )
    return;

  if ( folder->noContent() )
    return;

  if ( !GlobalSettings::self()->enableFavoriteFolderView() )
    return;

  // this works for multiple folders too
  menu->addAction(
     KIcon("bookmark-new"), i18n("Add to Favorite Folders"),
     this, SLOT( slotAddToFavorites() )
   );
}

//=====================================================================================
// Folder Tree Structure to Popup Menu
//

void MainFolderView::folderToPopupMenuInternal(
      MenuAction action, QObject * target, QMenu *menu, QTreeWidgetItem * parentItem
    )
{
  int childCount = parentItem->childCount();

  for ( int idx = 0; idx < childCount; idx++ )
  {
    FolderViewItem * fvi = static_cast< FolderViewItem * >( parentItem->child( idx ) );

    if ( !fvi )
      continue;

    // skip search folders
    if ( fvi->protocol() == KPIM::FolderTreeWidgetItem::Search )
      continue;

    QString label = fvi->labelText();
    label.replace( '&',"&&" );

    if ( fvi->childCount() > 0 )
    {
      // new level
      QMenu* popup = new QMenu( menu );

      popup->setObjectName( "subMenu" );
      popup->setTitle( label );

      folderToPopupMenuInternal( action, target, popup, fvi );

      bool subMenu = false;
      if ( ( action == MoveMessage || action == CopyMessage ) &&
           fvi->folder() && !fvi->folder()->noContent() )
        subMenu = true;
      if ( ( action == MoveFolder || action == CopyFolder )
          && ( !fvi->folder() || ( fvi->folder() && !fvi->folder()->noChildren() ) ) )
{'fraction_non_alphanumeric': 0.17283641820910456, 'fraction_numerical': 0.006336501584125396, 'mean_word_length': 2.9496213368455715, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 93, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1517689', 'n_tokens_mistral': 3482, 'n_tokens_neox': 3308, 'n_words': 998}