File indexing completed on 2024-05-05 04:48:24
0001 /**************************************************************************************** 0002 * Copyright (c) 2003 Scott Wheeler <wheeler@kde.org> * 0003 * Copyright (c) 2004 Max Howell <max.howell@methylblue.com> * 0004 * Copyright (c) 2004-2008 Mark Kretschmann <kretschmann@kde.org> * 0005 * Copyright (c) 2008 Seb Ruiz <ruiz@kde.org> * 0006 * Copyright (c) 2013 Ralf Engels <ralf-engels@gmx.de> * 0007 * * 0008 * This program is free software; you can redistribute it and/or modify it under * 0009 * the terms of the GNU General Public License as published by the Free Software * 0010 * Foundation; either version 2 of the License, or (at your option) any later * 0011 * version. * 0012 * * 0013 * This program is distributed in the hope that it will be useful, but WITHOUT ANY * 0014 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A * 0015 * PARTICULAR PURPOSE. See the GNU General Public License for more details. * 0016 * * 0017 * You should have received a copy of the GNU General Public License along with * 0018 * this program. If not, see <http://www.gnu.org/licenses/>. * 0019 ****************************************************************************************/ 0020 0021 #ifndef AMAROK_COLLECTIONSETUP_H 0022 #define AMAROK_COLLECTIONSETUP_H 0023 0024 #include <QFileSystemModel> 0025 0026 #include "core/support/Debug.h" 0027 0028 #include "ui_CollectionConfig.h" 0029 0030 class QAction; 0031 class QCheckBox; 0032 0033 namespace CollectionFolder { class Model; } 0034 0035 class CollectionSetup : public QWidget 0036 { 0037 Q_OBJECT 0038 0039 friend class CollectionFolder::Model; 0040 0041 public: 0042 static CollectionSetup* instance() { return s_instance; } 0043 0044 explicit CollectionSetup( QWidget* ); 0045 ~CollectionSetup() override {} 0046 0047 void writeConfig(); 0048 bool hasChanged() const; 0049 0050 bool recursive() const; 0051 bool monitor() const; 0052 0053 const QString modelFilePath( const QModelIndex &index ) const; 0054 Transcoding::SelectConfigWidget *transcodingConfig() const { return m_ui.transcodingConfig; } 0055 0056 Q_SIGNALS: 0057 void changed(); 0058 0059 private Q_SLOTS: 0060 void importCollection(); 0061 0062 /** Shows a context menu if the right mouse button is pressed over a directory. */ 0063 void slotPressed( const QModelIndex &index ); 0064 void slotRescanDirTriggered(); 0065 0066 private: 0067 /** Returns true if the given path is contained in the primary collection 0068 * @author Peter Zhou 0069 */ 0070 bool isDirInCollection( const QString& path ) const; 0071 0072 static CollectionSetup* s_instance; 0073 0074 Ui::CollectionConfig m_ui; 0075 CollectionFolder::Model *m_model; 0076 0077 QAction *m_rescanDirAction; 0078 /** This is the directory where the rescanDirAction was triggered */ 0079 QString m_currDir; 0080 0081 QCheckBox *m_recursive; 0082 QCheckBox *m_monitor; 0083 }; 0084 0085 0086 namespace CollectionFolder //just to keep it out of the global namespace 0087 { 0088 class Model : public QFileSystemModel 0089 { 0090 public: 0091 explicit Model( QObject *parent ); 0092 0093 Qt::ItemFlags flags( const QModelIndex &index ) const override; 0094 QVariant data( const QModelIndex& index, int role = Qt::DisplayRole ) const override; 0095 bool setData( const QModelIndex& index, const QVariant& value, int role = Qt::EditRole ) override; 0096 int columnCount( const QModelIndex& ) const override { return 1; } 0097 0098 /** Set the currently checked directories according to dirs */ 0099 void setDirectories(const QStringList &dirs ); 0100 0101 /** Returns the currently checked directories in this model. */ 0102 QStringList directories() const; 0103 0104 private: 0105 bool isForbiddenPath( const QString &path ) const; 0106 0107 /** Returns true if one of the parent paths is checked. */ 0108 bool ancestorChecked( const QString &path ) const; 0109 0110 /** 0111 * Get a list of all checked paths that are an ancestor of 0112 * the given path. 0113 */ 0114 QStringList allCheckedAncestors( const QString &path ) const; 0115 0116 /** Returns true if at least one of the subpaths are checked */ 0117 bool descendantChecked( const QString &path ) const; 0118 0119 /** 0120 * Check the logical recursive difference of root and excludePath and 0121 * updates m_checked. 0122 * For example, if excludePath is a grandchild of root, then this method 0123 * will check all of the children of root except the one that is the 0124 * parent of excludePath, as well as excludePath's siblings. 0125 */ 0126 void checkRecursiveSubfolders( const QString &root, const QString &excludePath ); 0127 0128 inline bool recursive() const 0129 { 0130 // Simply for convenience 0131 return CollectionSetup::instance() && CollectionSetup::instance()->recursive(); 0132 } 0133 0134 static inline QString normalPath( const QString &path ) 0135 { 0136 return path.endsWith( QLatin1Char('/') ) ? path : path + QLatin1Char('/'); 0137 } 0138 0139 QSet<QString> m_checked; 0140 }; 0141 0142 } // end namespace CollectionFolder 0143 0144 #endif 0145