<commit_before>/*****************************************************************************
 * infopanels.cpp : Panels for the information dialogs
 ****************************************************************************
 * Copyright (C) 2006-2007 the VideoLAN team
 * $Id$
 *
 * Authors: <NAME> <<EMAIL>>
 *          <NAME> <<EMAIL>>
 *
 * 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 "qt4.hpp"
#include "components/infopanels.hpp"

#include <QTreeWidget>
#include <QListView>
#include <QPushButton>
#include <QHeaderView>
#include <QList>
#include <QStringList>
#include <QGridLayout>
#include <QLineEdit>
#include <QLabel>
#include <QSpinBox>
#include <QTabWidget>

/************************************************************************
 * Single panels
 ************************************************************************/

/**
 * First Panel - Meta Info
 * All the usual MetaData are displayed and can be changed.
 **/
MetaPanel::MetaPanel( QWidget *parent,
                      intf_thread_t *_p_intf )
                      : QWidget( parent ), p_intf( _p_intf )
{
    QGridLayout *l = new QGridLayout( this );
    int line = 0; /* Counter for GridLayout */
    p_input = NULL;

#define ADD_META( string, widget ) {                             \
    l->addWidget( new QLabel( qtr( string ) + " :" ), line, 0 ); \
    widget = new QLineEdit;                                      \
    l->addWidget( widget, line, 1, 1, 9 );                       \
    line++;            }

    /* Title, artist and album*/
    ADD_META( VLC_META_TITLE, title_text ); /* OK */
    ADD_META( VLC_META_ARTIST, artist_text ); /* OK */
    ADD_META( VLC_META_COLLECTION, collection_text ); /* OK */

    /* Genre Name */
    /* FIXME List id3genres.h is not includable yet ? */
    genre_text = new QLineEdit;
    l->addWidget( new QLabel( qtr( VLC_META_GENRE ) + " :" ), line, 0 );
    l->addWidget( genre_text, line, 1, 1, 6 );

    /* Date (Should be in years) */
    date_text = new QSpinBox; setSpinBounds( date_text );
    l->addWidget( new QLabel( qtr( VLC_META_DATE ) + " :" ), line, 7 );
    l->addWidget( date_text, line, 8, 1, 2 );
    line++;

    /* Number and Rating */
    l->addWidget( new QLabel( qtr( "Track number/Position" )  + " :" ),
                  line, 0 );
    seqnum_text = new QSpinBox; setSpinBounds( seqnum_text );
    l->addWidget( seqnum_text, line, 1, 1, 4 );

    l->addWidget( new QLabel( qtr( VLC_META_RATING ) + " :" ), line, 5 );
    rating_text = new QSpinBox; setSpinBounds( rating_text) ;
    l->addWidget( rating_text, line, 6, 1, 4 );
    line++;

    /* Now Playing ? */
    ADD_META( VLC_META_NOW_PLAYING, nowplaying_text );

    /* Language and settings */
    l->addWidget( new QLabel( qfu( VLC_META_LANGUAGE ) + " :" ), line, 0 );
    language_text = new QLineEdit;
    l->addWidget( language_text, line, 1, 1, 4 );
    l->addWidget( new QLabel( qtr( VLC_META_SETTING ) + " :" ), line, 5 );
    setting_text = new QLineEdit;
    l->addWidget( setting_text, line, 6, 1, 4 );
    line++;

    /* ART_URL */
    art_cover = new QLabel( "" );
    art_cover->setMinimumHeight( 128 );
    art_cover->setMinimumWidth( 128 );
    art_cover->setMaximumHeight( 128 );
    art_cover->setMaximumWidth( 128 );
    art_cover->setScaledContents( true );
    art_cover->setPixmap( QPixmap( ":/noart.png" ) );
    l->addWidget( art_cover, line, 8, 4, 2 );

#define ADD_META_2( string, widget ) {                             \
    l->addWidget( new QLabel( qtr( string ) + " :" ), line, 0 ); \
    widget = new QLineEdit;                                      \
    l->addWidget( widget, line, 1, 1, 7 );                       \
    line++;            }

    ADD_META_2( VLC_META_COPYRIGHT, copyright_text );
    ADD_META_2( VLC_META_PUBLISHER, publisher_text );

    ADD_META_2( VLC_META_ENCODED_BY, publisher_text );
    ADD_META_2( VLC_META_DESCRIPTION, description_text );

    /*  ADD_META( TRACKID )  Useless ? */
    /*  ADD_URI - DO not show it, done outside */

#undef ADD_META
#undef ADD_META_2
}

MetaPanel::~MetaPanel(){}

/**
 * Save the MetaData, triggered by parent->save Button
 **/
void MetaPanel::saveMeta()
{
    playlist_t *p_playlist;

    meta_export_t p_export;
    p_export.p_item = p_input;

    if( p_input == NULL )
        return;

    /* we can write meta data only in a file */
    if( ( p_input->i_type == ITEM_TYPE_AFILE ) || \
        ( p_input->i_type == ITEM_TYPE_VFILE ) )
        /* some audio files are detected as video files */
    {
        char *psz_uri = p_input->psz_uri;
        if( !strncmp( psz_uri, "file://", 7 ) )
            psz_uri += 7; /* strlen("file://") = 7 */

        p_export.psz_file = strndup( psz_uri, PATH_MAX );
    }
    else
        return;

    /* now we read the modified meta data */
    free( p_input->p_meta->psz_artist );
    p_input->p_meta->psz_artist = strdup( qtu( artist_text->text() ) );
    free( p_input->p_meta->psz_album );
    p_input->p_meta->psz_album = strdup( qtu( collection_text->text() ) );
    free( p_input->p_meta->psz_genre );
    p_input->p_meta->psz_genre = strdup( qtu( genre_text->text() ) );
    free( p_input->p_meta->psz_date );
    p_input->p_meta->psz_date = (char*) malloc(5);
    snprintf( p_input->p_meta->psz_date, 5, "%d", date_text->value() );
    free( p_input->p_meta->psz_tracknum );
    p_input->p_meta->psz_tracknum = (char*) malloc(5);
    snprintf( p_input->p_meta->psz_tracknum, 5, "%d", seqnum_text->value() );
    free( p_input->p_meta->psz_title );
    p_input->p_meta->psz_title = strdup( qtu( title_text->text() ) );

    p_playlist = pl_Yield( p_intf );

    PL_LOCK;
    p_playlist->p_private = &p_export;

    module_t *p_mod = module_Need( p_playlist, "meta writer", NULL, 0 );
    if( p_mod )
        module_Unneed( p_playlist, p_mod );
    PL_UNLOCK;
    pl_Release( p_playlist );
}

/**
 * Update all the MetaData and art on an "item-changed" event
 **/
void MetaPanel::update( input_item_t *p_item )
{
    if( !p_item->p_meta )
        return;
    char *psz_meta; 
#define UPDATE_META( meta, widget ) {               \
    psz_meta = p_item->p_meta->psz_##meta;          \
    if( !EMPTY_STR( psz_meta ) )                    \
        widget->setText( qfu( psz_meta ) );         \
    else                                            \
        widget->setText( "" ); }

#define UPDATE_META_INT( meta, widget ) {           \
    psz_meta = p_item->p_meta->psz_##meta;          \
    if( !EMPTY_STR( psz_meta ) )                    \
        widget->setValue( atoi( psz_meta ) ); }

    /* Name / Title */
    psz_meta = p_item->p_meta->psz_title;
    if( !EMPTY_STR( psz_meta ) )
        title_text->setText( qfu( psz_meta ) );
    else if( !EMPTY_STR( p_item->psz_name ) )
        title_text->setText( qfu( p_item->psz_name ) );
    else title_text->setText( "" );

    /* URL / URI */
    psz_meta = p_item->p_meta->psz_url;
    if( !EMPTY_STR( psz_meta ) )
        emit uriSet( QString( psz_meta ) );
    else if( !EMPTY_STR( p_item->psz_uri ) )
        emit uriSet( QString( p_item->psz_uri ) );

    /* Other classic though */
    UPDATE_META( artist, artist_text );
    UPDATE_META( genre, genre_text );
    UPDATE_META( copyright, copyright_text );
    UPDATE_META( album, collection_text );
    UPDATE_META( description, description_text );
    UPDATE_META( language, language_text );
    UPDATE_META( nowplaying, nowplaying_text );
    UPDATE_META( publisher, publisher_text );
    UPDATE_META( setting, setting_text );

    UPDATE_META_INT( date, date_text );
    UPDATE_META_INT( tracknum, seqnum_text );
    UPDATE_META_INT( rating, rating_text );

#undef UPDATE_META

    /* Art Urls */
    psz_meta = p_item->p_meta->psz_arturl;
    if( psz_meta && !strncmp( psz_meta, "file://", 7 ) )
    {
        QString artUrl = qfu( psz_meta ).replace( "file://",QString("" ) );
        art_cover->setPixmap( QPixmap( artUrl ) );
    }
    else
        art_cover->setPixmap( QPixmap( ":/noart.png" ) );
}

/*
 * Clear all the metadata widgets
 * Unused yet
 */
void MetaPanel::clear(){}

/**
 * Second Panel - Shows the extra metadata in a tree, non editable.
 **/
ExtraMetaPanel::ExtraMetaPanel( QWidget *parent,
                                intf_thread_t *_p_intf )
                                : QWidget( parent ), p_intf( _p_intf )
{
     QGridLayout *layout = new QGridLayout(this);

     QLabel *topLabel = new QLabel( qtr( "Extra metadata and other information"
                 " are shown in this list.\n" ) );
     topLabel->setWordWrap( true );
     layout->addWidget( topLabel, 0, 0 );

     extraMetaTree = new QTreeWidget( this );
     extraMetaTree->setAlternatingRowColors( true );
     extraMetaTree->setColumnCount( 2 );
     extraMetaTree->header()->hide();
/*     QStringList headerList = ( QStringList() << qtr( "Type" )
 *                                             << qtr( "Value" ) );
 * Useless, add this header if you think it would help the user          **
 */

     layout->addWidget( extraMetaTree, 1, 0 );
}

/**
 * Update the Extra Metadata from p_meta->i_extras 
 **/
void ExtraMetaPanel::update( input_item_t *p_item )
{
    vlc_meta_t *p_meta = p_item->p_meta;
    if( !p_meta )
        return;
    QStringList tempItem;

    QList<QTreeWidgetItem *> items;
    char ** ppsz_allkey = vlc_dictionary_all_keys( p_meta->extra_meta );
    for (int i = 0; ppsz_allkey[i] ; i++ )
    {
        const char * psz_value =
            vlc_dictionary_value_for_key( p_meta->extra_meta, ppsz_allkey[i] );
        tempItem.append( qfu( ppsz_allkey[i] ) + " : ");
        tempItem.append( qfu( psz_value );
        items.append( new QTreeWidgetItem ( extraMetaTree, tempItem ) );
        free( ppsz_allkey[i] );
    }<commit_msg>modules/gui/qt4: Attempt to fix previous commit.<commit_after>/*****************************************************************************
 * infopanels.cpp : Panels for the information dialogs
 ****************************************************************************
 * Copyright (C) 2006-2007 the VideoLAN team
 * $Id$
 *
 * Authors: <NAME> <<EMAIL>>
 *          <NAME> <<EMAIL>>
 *
 * 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 "qt4.hpp"
#include "components/infopanels.hpp"

#include <QTreeWidget>
#include <QListView>
#include <QPushButton>
#include <QHeaderView>
#include <QList>
#include <QStringList>
#include <QGridLayout>
#include <QLineEdit>
#include <QLabel>
#include <QSpinBox>
#include <QTabWidget>

/************************************************************************
 * Single panels
 ************************************************************************/

/**
 * First Panel - Meta Info
 * All the usual MetaData are displayed and can be changed.
 **/
MetaPanel::MetaPanel( QWidget *parent,
                      intf_thread_t *_p_intf )
                      : QWidget( parent ), p_intf( _p_intf )
{
    QGridLayout *l = new QGridLayout( this );
    int line = 0; /* Counter for GridLayout */
    p_input = NULL;

#define ADD_META( string, widget ) {                             \
    l->addWidget( new QLabel( qtr( string ) + " :" ), line, 0 ); \
    widget = new QLineEdit;                                      \
    l->addWidget( widget, line, 1, 1, 9 );                       \
    line++;            }

    /* Title, artist and album*/
    ADD_META( VLC_META_TITLE, title_text ); /* OK */
    ADD_META( VLC_META_ARTIST, artist_text ); /* OK */
    ADD_META( VLC_META_COLLECTION, collection_text ); /* OK */

    /* Genre Name */
    /* FIXME List id3genres.h is not includable yet ? */
    genre_text = new QLineEdit;
    l->addWidget( new QLabel( qtr( VLC_META_GENRE ) + " :" ), line, 0 );
    l->addWidget( genre_text, line, 1, 1, 6 );

    /* Date (Should be in years) */
    date_text = new QSpinBox; setSpinBounds( date_text );
    l->addWidget( new QLabel( qtr( VLC_META_DATE ) + " :" ), line, 7 );
    l->addWidget( date_text, line, 8, 1, 2 );
    line++;

    /* Number and Rating */
    l->addWidget( new QLabel( qtr( "Track number/Position" )  + " :" ),
                  line, 0 );
    seqnum_text = new QSpinBox; setSpinBounds( seqnum_text );
    l->addWidget( seqnum_text, line, 1, 1, 4 );

    l->addWidget( new QLabel( qtr( VLC_META_RATING ) + " :" ), line, 5 );
    rating_text = new QSpinBox; setSpinBounds( rating_text) ;
    l->addWidget( rating_text, line, 6, 1, 4 );
    line++;

    /* Now Playing ? */
    ADD_META( VLC_META_NOW_PLAYING, nowplaying_text );

    /* Language and settings */
    l->addWidget( new QLabel( qfu( VLC_META_LANGUAGE ) + " :" ), line, 0 );
    language_text = new QLineEdit;
    l->addWidget( language_text, line, 1, 1, 4 );
    l->addWidget( new QLabel( qtr( VLC_META_SETTING ) + " :" ), line, 5 );
    setting_text = new QLineEdit;
    l->addWidget( setting_text, line, 6, 1, 4 );
    line++;

    /* ART_URL */
    art_cover = new QLabel( "" );
    art_cover->setMinimumHeight( 128 );
    art_cover->setMinimumWidth( 128 );
    art_cover->setMaximumHeight( 128 );
    art_cover->setMaximumWidth( 128 );
    art_cover->setScaledContents( true );
    art_cover->setPixmap( QPixmap( ":/noart.png" ) );
    l->addWidget( art_cover, line, 8, 4, 2 );

#define ADD_META_2( string, widget ) {                             \
    l->addWidget( new QLabel( qtr( string ) + " :" ), line, 0 ); \
    widget = new QLineEdit;                                      \
    l->addWidget( widget, line, 1, 1, 7 );                       \
    line++;            }

    ADD_META_2( VLC_META_COPYRIGHT, copyright_text );
    ADD_META_2( VLC_META_PUBLISHER, publisher_text );

    ADD_META_2( VLC_META_ENCODED_BY, publisher_text );
    ADD_META_2( VLC_META_DESCRIPTION, description_text );

    /*  ADD_META( TRACKID )  Useless ? */
    /*  ADD_URI - DO not show it, done outside */

#undef ADD_META
#undef ADD_META_2
}

MetaPanel::~MetaPanel(){}

/**
 * Save the MetaData, triggered by parent->save Button
 **/
void MetaPanel::saveMeta()
{
    playlist_t *p_playlist;

    meta_export_t p_export;
    p_export.p_item = p_input;

    if( p_input == NULL )
        return;

    /* we can write meta data only in a file */
    if( ( p_input->i_type == ITEM_TYPE_AFILE ) || \
        ( p_input->i_type == ITEM_TYPE_VFILE ) )
        /* some audio files are detected as video files */
    {
        char *psz_uri = p_input->psz_uri;
        if( !strncmp( psz_uri, "file://", 7 ) )
            psz_uri += 7; /* strlen("file://") = 7 */

        p_export.psz_file = strndup( psz_uri, PATH_MAX );
    }
    else
        return;

    /* now we read the modified meta data */
    free( p_input->p_meta->psz_artist );
    p_input->p_meta->psz_artist = strdup( qtu( artist_text->text() ) );
    free( p_input->p_meta->psz_album );
    p_input->p_meta->psz_album = strdup( qtu( collection_text->text() ) );
    free( p_input->p_meta->psz_genre );
    p_input->p_meta->psz_genre = strdup( qtu( genre_text->text() ) );
    free( p_input->p_meta->psz_date );
    p_input->p_meta->psz_date = (char*) malloc(5);
    snprintf( p_input->p_meta->psz_date, 5, "%d", date_text->value() );
    free( p_input->p_meta->psz_tracknum );
    p_input->p_meta->psz_tracknum = (char*) malloc(5);
    snprintf( p_input->p_meta->psz_tracknum, 5, "%d", seqnum_text->value() );
    free( p_input->p_meta->psz_title );
    p_input->p_meta->psz_title = strdup( qtu( title_text->text() ) );

    p_playlist = pl_Yield( p_intf );

    PL_LOCK;
    p_playlist->p_private = &p_export;

    module_t *p_mod = module_Need( p_playlist, "meta writer", NULL, 0 );
    if( p_mod )
        module_Unneed( p_playlist, p_mod );
    PL_UNLOCK;
    pl_Release( p_playlist );
}

/**
 * Update all the MetaData and art on an "item-changed" event
 **/
void MetaPanel::update( input_item_t *p_item )
{
    if( !p_item->p_meta )
        return;
    char *psz_meta; 
#define UPDATE_META( meta, widget ) {               \
    psz_meta = p_item->p_meta->psz_##meta;          \
    if( !EMPTY_STR( psz_meta ) )                    \
        widget->setText( qfu( psz_meta ) );         \
    else                                            \
        widget->setText( "" ); }

#define UPDATE_META_INT( meta, widget ) {           \
    psz_meta = p_item->p_meta->psz_##meta;          \
    if( !EMPTY_STR( psz_meta ) )                    \
        widget->setValue( atoi( psz_meta ) ); }

    /* Name / Title */
    psz_meta = p_item->p_meta->psz_title;
    if( !EMPTY_STR( psz_meta ) )
        title_text->setText( qfu( psz_meta ) );
    else if( !EMPTY_STR( p_item->psz_name ) )
        title_text->setText( qfu( p_item->psz_name ) );
    else title_text->setText( "" );

    /* URL / URI */
    psz_meta = p_item->p_meta->psz_url;
    if( !EMPTY_STR( psz_meta ) )
        emit uriSet( QString( psz_meta ) );
    else if( !EMPTY_STR( p_item->psz_uri ) )
        emit uriSet( QString( p_item->psz_uri ) );

    /* Other classic though */
    UPDATE_META( artist, artist_text );
    UPDATE_META( genre, genre_text );
    UPDATE_META( copyright, copyright_text );
    UPDATE_META( album, collection_text );
    UPDATE_META( description, description_text );
    UPDATE_META( language, language_text );
    UPDATE_META( nowplaying, nowplaying_text );
    UPDATE_META( publisher, publisher_text );
    UPDATE_META( setting, setting_text );

    UPDATE_META_INT( date, date_text );
    UPDATE_META_INT( tracknum, seqnum_text );
    UPDATE_META_INT( rating, rating_text );

#undef UPDATE_META

    /* Art Urls */
    psz_meta = p_item->p_meta->psz_arturl;
    if( psz_meta && !strncmp( psz_meta, "file://", 7 ) )
    {
        QString artUrl = qfu( psz_meta ).replace( "file://",QString("" ) );
        art_cover->setPixmap( QPixmap( artUrl ) );
    }
    else
        art_cover->setPixmap( QPixmap( ":/noart.png" ) );
}

/*
 * Clear all the metadata widgets
 * Unused yet
 */
void MetaPanel::clear(){}

/**
 * Second Panel - Shows the extra metadata in a tree, non editable.
 **/
ExtraMetaPanel::ExtraMetaPanel( QWidget *parent,
                                intf_thread_t *_p_intf )
                                : QWidget( parent ), p_intf( _p_intf )
{
     QGridLayout *layout = new QGridLayout(this);

     QLabel *topLabel = new QLabel( qtr( "Extra metadata and other information"
                 " are shown in this list.\n" ) );
     topLabel->setWordWrap( true );
     layout->addWidget( topLabel, 0, 0 );

     extraMetaTree = new QTreeWidget( this );
     extraMetaTree->setAlternatingRowColors( true );
     extraMetaTree->setColumnCount( 2 );
     extraMetaTree->header()->hide();
/*     QStringList headerList = ( QStringList() << qtr( "Type" )
 *                                             << qtr( "Value" ) );
 * Useless, add this header if you think it would help the user          **
 */

     layout->addWidget( extraMetaTree, 1, 0 );
}

/**
 * Update the Extra Metadata from p_meta->i_extras 
 **/
void ExtraMetaPanel::update( input_item_t *p_item )
{
    vlc_meta_t *p_meta = p_item->p_meta;
    if( !p_meta )
        return;
    QStringList tempItem;

    QList<QTreeWidgetItem *> items;
    vlc_dictionary_t * p_dict = &p_meta->extra_tags;
    char ** ppsz_allkey = vlc_dictionary_all_keys( p_dict );
    for (int i = 0; ppsz_allkey[i] ; i++ )
    {
        const char * psz_value =
            vlc_dictionary_value_for_key( p_dict, ppsz_allkey[i] );
        tempItem.append( qfu( ppsz_allkey[i] ) + " : ");
        tempItem.append( qfu( psz_value ) );
        items.append( new QTreeWidgetItem ( extraMetaTree, tempItem ) );
        free( ppsz_allkey[i] );
    }
{'fraction_non_alphanumeric': 0.16338470319634704, 'fraction_numerical': 0.008704337899543379, 'mean_word_length': 2.637543252595156, 'pattern_counts': {'":': 4, '<': 47, '<?xml version=': 0, '>': 230, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4029943', 'n_tokens_mistral': 7343, 'n_tokens_neox': 6993, 'n_words': 1706}