File indexing completed on 2024-10-13 04:22:10

0001 /*
0002     SPDX-FileCopyrightText: 1998-2009 Sebastian Trueg <trueg@k3b.org>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "k3bfileview.h"
0007 #include "k3b.h"
0008 #include "k3bdiroperator.h"
0009 #include "k3bapplication.h"
0010 
0011 #include <KFileFilterCombo>
0012 #include <KFileItem>
0013 #include <KLocalizedString>
0014 #include <KDirLister>
0015 #include <KActionMenu>
0016 #include <KToolBarSpacerAction>
0017 #include <KActionCollection>
0018 #include <KToolBar>
0019 
0020 #include <QDebug>
0021 #include <QDir>
0022 #include <QUrl>
0023 #include <QIcon>
0024 #include <QAction>
0025 #include <QHBoxLayout>
0026 #include <QLayout>
0027 #include <QLabel>
0028 #include <QProgressBar>
0029 #include <QToolButton>
0030 #include <QVBoxLayout>
0031 #include <QWidget>
0032 
0033 
0034 class K3b::FileView::Private
0035 {
0036 public:
0037     KToolBar* toolBox;
0038     DirOperator* dirOp;
0039     KFileFilterCombo* filterWidget;
0040     QAction* actionShowBookmarks;
0041 };
0042 
0043 
0044 K3b::FileView::FileView(QWidget *parent )
0045     : K3b::ContentsView( false, parent),
0046       d( new Private )
0047 {
0048     d->dirOp = new K3b::DirOperator( QUrl::fromLocalFile(QDir::home().absolutePath()), this );
0049     d->toolBox = new KToolBar( this );
0050     d->toolBox->setToolButtonStyle( Qt::ToolButtonIconOnly );
0051 
0052     QVBoxLayout* layout = new QVBoxLayout( this );
0053     layout->setContentsMargins( 0, 0, 0, 0 );
0054     layout->setSpacing( 0 );
0055     layout->addWidget( d->toolBox );
0056     layout->addWidget( d->dirOp, 1 );
0057 
0058     // setup actions
0059     QAction* actionBack = d->dirOp->action(KDirOperator::Back);
0060     QAction* actionForward = d->dirOp->action(KDirOperator::Forward);
0061     QAction* actionUp = d->dirOp->action(KDirOperator::Up);
0062     QAction* actionReload = d->dirOp->action(KDirOperator::Reload);
0063 
0064     // create filter selection combobox
0065     QWidget* filterBox = new QWidget( d->toolBox );
0066     QHBoxLayout* filterLayout = new QHBoxLayout( filterBox );
0067     filterLayout->addWidget( new QLabel( i18n("Filter:"), filterBox ) );
0068     d->filterWidget = new KFileFilterCombo( filterBox );
0069     filterLayout->addWidget( d->filterWidget );
0070     filterLayout->setContentsMargins( 0, 0, 0, 0 );
0071 
0072     d->filterWidget->setEditable( true );
0073 
0074 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
0075     QString filter = "*|" + i18n("All Files");
0076     filter += '\n' + "audio/x-mp3 audio/x-wav application/x-ogg|" + i18n("Sound Files");
0077     filter += '\n' + "audio/x-wav |" + i18n("Wave Sound Files");
0078     filter += '\n' + "audio/x-mp3 |" + i18n("MP3 Sound Files");
0079     filter += '\n' + "application/x-ogg |" + i18n("Ogg Vorbis Sound Files");
0080     filter += '\n' + "video/mpeg |" + i18n("MPEG Video Files");
0081     d->filterWidget->setFilter(filter);
0082 #else
0083     const QList<KFileFilter> filters {
0084         KFileFilter(i18n("All Files"), {"*"}, {}),
0085         KFileFilter(i18n("Sound Files"), {}, {"audio/x-mp3", "audio/x-wav", "application/x-ogg"}),
0086         KFileFilter(i18n("Wave Sound Files"), {}, {"audio/x-wav"}),
0087         KFileFilter(i18n("MP3 Sound Files"), {}, {"audio/x-mp3"}),
0088         KFileFilter(i18n("Ogg Vorbis Sound Files"), {}, {"application/x-ogg"}),
0089         KFileFilter(i18n("MPEG Video Files"), {}, {"video/mpeg"}),
0090     };
0091     d->filterWidget->setFilters(filters, filters.first());
0092 #endif
0093 
0094     d->actionShowBookmarks = new QAction( i18n("Show Bookmarks"), d->toolBox );
0095     d->actionShowBookmarks->setCheckable( true );
0096 
0097     KActionMenu* actionOptions = new KActionMenu( QIcon::fromTheme("configure"), i18n("Options"), d->toolBox );
0098     actionOptions->setPopupMode( QToolButton::InstantPopup );
0099     actionOptions->addAction( d->dirOp->action(KDirOperator::SortMenu) );
0100     actionOptions->addAction( d->dirOp->action(KDirOperator::ViewModeMenu) );
0101     actionOptions->addSeparator();
0102     actionOptions->addAction( d->dirOp->action(KDirOperator::DecorationMenu) );
0103     actionOptions->addSeparator();
0104     actionOptions->addAction( d->dirOp->action(KDirOperator::ShowHiddenFiles) );
0105     actionOptions->addAction( d->actionShowBookmarks );
0106     actionOptions->addAction( d->dirOp->action(KDirOperator::ShowPreview) );
0107 
0108     d->toolBox->addAction( actionBack );
0109     d->toolBox->addAction( actionForward );
0110     d->toolBox->addAction( actionUp );
0111     d->toolBox->addAction( actionReload );
0112     d->toolBox->addSeparator();
0113     d->toolBox->addAction( d->dirOp->action(KDirOperator::ShortView) );
0114     d->toolBox->addAction( d->dirOp->action(KDirOperator::DetailedView) );
0115     d->toolBox->addSeparator();
0116     d->toolBox->addSeparator();
0117     d->toolBox->addWidget( filterBox );
0118     d->toolBox->addAction( new KToolBarSpacerAction( d->toolBox ) );
0119     d->toolBox->addAction( actionOptions );
0120     d->toolBox->addAction( d->dirOp->bookmarkMenu() );
0121 
0122     if( QAction* action = d->dirOp->action(KDirOperator::ShowHiddenFiles) ) {
0123         action->setShortcut( Qt::ALT + Qt::Key_Period );
0124         action->setShortcutContext( Qt::ApplicationShortcut );
0125     }
0126 
0127     connect( d->dirOp, SIGNAL(urlEntered(QUrl)), this, SIGNAL(urlEntered(QUrl)) );
0128     connect( d->filterWidget, SIGNAL(filterChanged()), SLOT(slotFilterChanged()) );
0129     connect( d->actionShowBookmarks, SIGNAL(toggled(bool)), d->dirOp->bookmarkMenu(), SLOT(setVisible(bool)) );
0130 }
0131 
0132 
0133 K3b::FileView::~FileView()
0134 {
0135     delete d;
0136 }
0137 
0138 
0139 void K3b::FileView::setUrl(const QUrl& url, bool forward)
0140 {
0141     qDebug() << url;
0142     d->dirOp->setUrl( url, forward );
0143 }
0144 
0145 
0146 QUrl K3b::FileView::url()
0147 {
0148     return d->dirOp->url();
0149 }
0150 
0151 void K3b::FileView::slotFilterChanged()
0152 {
0153 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
0154     QString filter = d->filterWidget->currentFilter();
0155     d->dirOp->clearFilter();
0156 
0157     if( filter.indexOf( '/' ) > -1 ) {
0158         QStringList types = filter.split( ' ' );
0159         types.prepend( "inode/directory" );
0160         d->dirOp->setMimeFilter( types );
0161     }
0162     else
0163         d->dirOp->setNameFilter( filter );
0164 #else
0165     const KFileFilter filter = d->filterWidget->currentFilter();
0166     d->dirOp->clearFilter();
0167 
0168     if( !filter.mimePatterns().isEmpty() ) {
0169         QStringList types = filter.mimePatterns();
0170         types.prepend( "inode/directory" );
0171         d->dirOp->setMimeFilter( types );
0172     }
0173     else
0174         d->dirOp->setNameFilter( filter.filePatterns().join( QLatin1Char(' ') ) );
0175 #endif
0176 
0177     d->dirOp->rereadDir();
0178 }
0179 
0180 
0181 void K3b::FileView::reload()
0182 {
0183     d->dirOp->rereadDir();
0184 }
0185 
0186 
0187 void K3b::FileView::saveConfig( KConfigGroup grp )
0188 {
0189     d->dirOp->writeConfig(grp);
0190 }
0191 
0192 
0193 void K3b::FileView::readConfig( const KConfigGroup& grp )
0194 {
0195     d->dirOp->readConfig(grp);
0196     d->actionShowBookmarks->setChecked( d->dirOp->bookmarkMenu()->isVisible() );
0197 }
0198 
0199 #include "moc_k3bfileview.cpp"