File indexing completed on 2024-10-06 04:25:58

0001 /*
0002     SPDX-FileCopyrightText: 1998-2009 Sebastian Trueg <trueg@k3b.org>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "k3bdiroperator.h"
0007 
0008 #include "k3bapplication.h"
0009 #include "k3b.h"
0010 #include "k3bcore.h"
0011 #include "k3baction.h"
0012 
0013 #include <KBookmarkManager>
0014 #include <KBookmarkMenu>
0015 #include <KConfigGroup>
0016 #include <KLocalizedString>
0017 #include <KActionCollection>
0018 #include <KActionMenu>
0019 
0020 #include <QDir>
0021 #include <QAction>
0022 #include <QMenu>
0023 
0024 K3b::DirOperator::DirOperator(const QUrl& url, QWidget* parent )
0025     : KDirOperator( url, parent )
0026 {
0027     setMode( KFile::Files );
0028 
0029     // disable the del-key since we still have a focus problem and users keep
0030     // deleting files when they want to remove project entries
0031     QAction* aTrash = action(KDirOperator::Trash);
0032     if( aTrash ) {
0033         aTrash->setShortcut( 0 );
0034     }
0035 
0036     // add the bookmark stuff
0037 
0038     QString dirPath = QStandardPaths::writableLocation( QStandardPaths::GenericDataLocation );
0039     QDir().mkpath( dirPath );
0040     QString bookmarksFile = dirPath + '/' + QString::fromLatin1("k3b/bookmarks.xml");
0041 
0042 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
0043     KBookmarkManager* bmMan = KBookmarkManager::managerForFile( bookmarksFile, "k3b" );
0044     bmMan->setEditorOptions( i18n("K3b Bookmarks"), false );
0045     bmMan->setUpdate( true );
0046 #else
0047     KBookmarkManager* bmMan = new KBookmarkManager( bookmarksFile, this );
0048 #endif
0049 
0050     m_bmPopup = new KActionMenu( QIcon::fromTheme("bookmarks"),i18n("Bookmarks"), this);
0051     m_bmPopup->setPopupMode( QToolButton::InstantPopup );
0052     m_bmMenu = new KBookmarkMenu(bmMan, this, m_bmPopup->menu());
0053 
0054     m_bmActionAddFileToProject = K3b::createAction( this,i18n("&Add to Project"), 0, Qt::SHIFT|Qt::Key_Return,
0055                                                     this, SLOT(slotAddFilesToProject()),
0056                                                     nullptr, "add_file_to_project");
0057 
0058     connect( this, SIGNAL(fileSelected(KFileItem)),
0059              this, SLOT(slotAddFilesToProject()) );
0060     connect( this, &KDirOperator::contextMenuAboutToShow,
0061              this, &DirOperator::extendContextMenu );
0062 }
0063 
0064 
0065 K3b::DirOperator::~DirOperator()
0066 {
0067     delete m_bmMenu;
0068 }
0069 
0070 
0071 void K3b::DirOperator::readConfig( const KConfigGroup& grp )
0072 {
0073     KDirOperator::readConfig( grp );
0074     
0075     m_bmPopup->setVisible( grp.readEntry( "show bookmarks", false ) );
0076     
0077     // There seems to be a bug in the KDELibs which makes setURL crash on
0078     // some systems when used with a non-existing url
0079     QString lastUrl = grp.readPathEntry( "last url", QDir::home().absolutePath() );
0080     while( !QFile::exists(lastUrl) ) {
0081         QString urlUp = lastUrl.section( '/', 0, -2 );
0082         if( urlUp == lastUrl )
0083             lastUrl = QDir::home().absolutePath();
0084         else
0085             lastUrl = urlUp;
0086     }
0087     
0088     // There seems to be another bug in KDELibs which shows
0089     // neverending busy cursor when we call setUrl() after setView()
0090     // so we call it in the right order (see bug 113649)
0091     setUrl( QUrl::fromLocalFile(lastUrl), true );
0092     setViewMode( KFile::Default );
0093 
0094     emit urlEntered( url() );
0095 }
0096 
0097 
0098 void K3b::DirOperator::writeConfig( KConfigGroup& grp )
0099 {
0100     KDirOperator::writeConfig(grp );
0101     grp.writeEntry( "show bookmarks", m_bmPopup->isVisible() );
0102     grp.writePathEntry( "last url", url().toLocalFile() );
0103 }
0104 
0105 
0106 void K3b::DirOperator::openBookmark(const KBookmark & bm, Qt::MouseButtons, Qt::KeyboardModifiers)
0107 {
0108     setUrl( bm.url(), true );
0109 }
0110 
0111 
0112 QString K3b::DirOperator::currentTitle() const
0113 {
0114     return url().toDisplayString( QUrl::PreferLocalFile | QUrl::StripTrailingSlash );
0115 }
0116 
0117 
0118 QUrl K3b::DirOperator::currentUrl() const
0119 {
0120     return url();
0121 }
0122 
0123 
0124 void K3b::DirOperator::extendContextMenu( const KFileItem&, QMenu* menu )
0125 {
0126     QAction* firstAction = menu->actions().first();
0127     menu->insertAction( firstAction, m_bmActionAddFileToProject );
0128     menu->insertSeparator( firstAction );
0129     menu->addSeparator();
0130     menu->addAction( m_bmPopup );
0131 
0132     bool hasSelection = !selectedItems().isEmpty();
0133     /*
0134       view() && view()->selectedItems() &&
0135       !view()->selectedItems()->isEmpty();
0136     */
0137     m_bmActionAddFileToProject->setEnabled( hasSelection && k3bappcore->k3bMainWindow()->activeView() != 0 );
0138 }
0139 
0140 
0141 void K3b::DirOperator::slotAddFilesToProject()
0142 {
0143     QList<QUrl> files;
0144     QList<KFileItem> items(selectedItems());
0145     Q_FOREACH( const KFileItem& fileItem, items ) {
0146         files.append( fileItem.url() );
0147     }
0148     if( !files.isEmpty() ) {
0149         k3bappcore->k3bMainWindow()->addUrls( files );
0150     }
0151 }
0152 
0153 #include "moc_k3bdiroperator.cpp"