Commit 3489167b authored by Harald Hvaal's avatar Harald Hvaal
Browse files

Automagically clean up folder dirs so that automatically generated folder...

Automagically clean up folder dirs so that automatically generated folder nodes will be purged when there are no elements left. Archives that actually contain a folder node in the internal archive will be kept. (this behavior might be changed.)

svn path=/trunk/KDE/kdeutils/ark/; revision=901807
parent f3aa11cb
......@@ -28,6 +28,7 @@
#include <QMimeData>
#include <QDir>
#include <QtDBus/QtDBus>
#include <QPersistentModelIndex>
#include <KDebug>
#include <KLocale>
......@@ -46,7 +47,7 @@ class ArchiveNode
{
public:
ArchiveNode( ArchiveDirNode *parent, const ArchiveEntry & entry )
: m_parent( parent ), m_row( -1 )
: m_parent( parent )
{
setEntry( entry );
}
......@@ -84,7 +85,6 @@ class ArchiveNode
private:
ArchiveEntry m_entry;
ArchiveDirNode *m_parent;
int m_row;
};
......@@ -152,12 +152,9 @@ class ArchiveDirNode: public ArchiveNode
int ArchiveNode::row()
{
if ( m_row != -1 ) return m_row;
if ( parent() )
{
m_row = parent()->entries().indexOf( const_cast<ArchiveNode*>( this ) );
return m_row;
return parent()->entries().indexOf( const_cast<ArchiveNode*>( this ) );
}
return 0;
}
......@@ -580,7 +577,7 @@ void ArchiveModel::slotEntryRemoved( const QString & path )
{
// TODO: Do something
kDebug (1601) << "Removed node at path " << path;
ArchiveNode *entry = m_rootNode->findByPath( path.split('/') );
ArchiveNode *entry = m_rootNode->findByPath( path.split('/', QString::SkipEmptyParts) );
if ( entry )
{
ArchiveDirNode *parent = entry->parent();
......@@ -765,9 +762,52 @@ DeleteJob* ArchiveModel::deleteFiles( const QList<QVariant> & files )
connect( job, SIGNAL( entryRemoved( const QString & ) ),
this, SLOT( slotEntryRemoved( const QString & ) ) );
connect(job, SIGNAL(finished(KJob*)),
this, SLOT( slotCleanupEmptyDirs() ) );
connect(job, SIGNAL(userQuery(Query*)),
this, SLOT(slotUserQuery(Query*)));
return job;
}
return 0;
}
void ArchiveModel::slotCleanupEmptyDirs()
{
kDebug (1601);
QList<QPersistentModelIndex> queue;
QList<QPersistentModelIndex> nodesToDelete;
//add root nodes
for (int i = 0; i < rowCount(); ++i) {
queue.append(QPersistentModelIndex(index(i, 0)));
}
//breadth-first traverse
while(!queue.isEmpty())
{
QPersistentModelIndex node = queue.takeFirst();
ArchiveEntry entry = entryForIndex(node);
//kDebug(1601) << "Trying " << entry[FileName].toString();
if ( !hasChildren(node) ) {
if (!entry.contains( InternalID )) {
nodesToDelete << node;
}
} else {
for (int i = 0; i < rowCount(node); ++i) {
queue.append(QPersistentModelIndex(index(i, 0, node)));
}
}
}
foreach(const QPersistentModelIndex& node, nodesToDelete) {
ArchiveNode *rawNode = static_cast<ArchiveNode*>( node.internalPointer() );
kDebug() << "Delete with parent entries " << rawNode->parent()->entries() << " and row " << rawNode->row();
beginRemoveRows(parent(node), rawNode->row(), rawNode->row());
delete rawNode->parent()->entries().takeAt(rawNode->row());
endRemoveRows();
//kDebug(1601) << "Removed entry " << entry[FileName].toString();
}
return;
}
......@@ -79,6 +79,7 @@ class ArchiveModel: public QAbstractItemModel
void slotNewEntry( const ArchiveEntry& entry );
void slotEntryRemoved( const QString & path );
void slotUserQuery(Query *query);
void slotCleanupEmptyDirs();
private:
ArchiveDirNode* parentFor( const ArchiveEntry& entry );
......
......@@ -536,7 +536,6 @@ QList<QVariant> Part::selectedFilesWithChildren()
{
Q_ASSERT(m_model);
QStringList toSort;
QModelIndexList toIterate = m_view->selectionModel()->selectedRows();
for (int i = 0; i < toIterate.size(); ++i) {
......@@ -551,18 +550,12 @@ QList<QVariant> Part::selectedFilesWithChildren()
}
//TODO: this needs to be fixed so that it does not assume that the
//InternalID is an actual filename, because sometimes it isn't
QVariantList ret;
foreach( const QModelIndex & index, toIterate )
{
const ArchiveEntry& entry = m_model->entryForIndex( index );
toSort << entry[ InternalID ].toString();
}
toSort.sort();
QVariantList ret;
foreach (const QString &i, toSort) {
ret << i;
if (entry.contains( InternalID ))
ret << entry[ InternalID ];
}
return ret;
}
......@@ -687,7 +680,7 @@ void Part::slotDeleteFilesDone( KJob* job )
void Part::slotDeleteFiles()
{
kDebug( 1601 ) ;
DeleteJob *job = m_model->deleteFiles( selectedFiles() );
DeleteJob *job = m_model->deleteFiles( selectedFilesWithChildren() );
connect( job, SIGNAL( result( KJob* ) ),
this, SLOT( slotDeleteFilesDone( KJob* ) ) );
registerJob(job);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment