File: filesystemtree.cpp

package info (click to toggle)
kde-runtime 4%3A4.8.4-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 24,712 kB
  • sloc: cpp: 130,244; ansic: 2,447; xml: 1,691; perl: 1,570; sh: 504; python: 455; makefile: 13
file content (374 lines) | stat: -rw-r--r-- 8,083 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/*
    This file is part of the Nepomuk KDE project.
    Copyright (C) 2010  Vishesh Handa <handa.vish@gmail.com>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) version 3, or any
    later version accepted by the membership of KDE e.V. (or its
    successor approved by the membership of KDE e.V.), which shall
    act as a proxy defined in Section 6 of version 3 of the license.

    This library 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
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "filesystemtree.h"

#include <QtCore/QListIterator>

//
// FileSystemTree
//

FileSystemTree::FileSystemTree()
{
}


FileSystemTree::~FileSystemTree()
{
    foreach( FileSystemTreeItem * p, m_rootNodes ) {
        delete p;
    }
}


void FileSystemTree::add( FileSystemTreeItem* item )
{
    FileSystemTreeItem::insert( m_rootNodes, item, 0 );
}


void FileSystemTree::remove(FileSystemTreeItem* item)
{
    if( item->parent() == 0 ) {
        m_rootNodes.removeAll( item );
        
        foreach( FileSystemTreeItem * child, item->m_children )
            FileSystemTreeItem::insert( m_rootNodes, child, 0 );

        item->m_children.clear();
        delete item;
    }
    else {
        item->m_parent->m_children.removeAll( item );

        foreach( FileSystemTreeItem * child, item->m_children )
                item->m_parent->add( child );

        item->m_children.clear();
        delete item;
    }
}

void FileSystemTree::remove(const QString& url)
{
    FileSystemTreeItem * t = find( url );
    if( t ) {
        remove( t );
    }
}


FileSystemTreeItem* FileSystemTree::find(const QString& url)
{
    return FileSystemTreeItem::find( m_rootNodes, url );
}


QList<QString> FileSystemTree::toList() const
{
    QList<QString> list;
    foreach( FileSystemTreeItem * item, m_rootNodes ) {
        list << item->toList();
    }
    
    return list;
}

bool FileSystemTree::isEmpty() const
{
    return m_rootNodes.isEmpty();
}

QList< FileSystemTreeItem* > FileSystemTree::rootNodes() const
{
    return m_rootNodes;
}

int FileSystemTree::size() const
{
    int s = 0;
    foreach( const FileSystemTreeItem * item, m_rootNodes )
        s += item->size();
    return s;
}


//
// Tree Item
//

FileSystemTreeItem::FileSystemTreeItem()
{
    m_parent = 0;
}


FileSystemTreeItem::FileSystemTreeItem(const QString& url)
{
    m_parent = 0;
    m_url = url;
    m_isFolder = m_url.endsWith('/');
}


FileSystemTreeItem::FileSystemTreeItem(const QString& url, bool folder)
{
    m_parent = 0;
    m_url = url;
    m_isFolder = folder;
}


FileSystemTreeItem::~FileSystemTreeItem()
{
    foreach( FileSystemTreeItem * t, m_children ) {
        delete t;
    }
}

bool FileSystemTreeItem::shouldBeParentOf(FileSystemTreeItem* item)
{
    return item->depth() > depth() && item->m_url.contains( m_url );
}

bool FileSystemTreeItem::isFile() const
{
    return !isFolder();
}

namespace {
    bool withoutTrailingSlashCompare( const QString & a, const QString & b ) {
        if( a.endsWith('/') ) {
            if( b.endsWith('/') )
                return a < b;
            
            return a.mid( 0, a.length() -1 ) < b;
        }
        else if( b.endsWith('/') ) {
            return a < b.mid( 0, b.length() -1 );
        }
        else
            return a < b;
    }


    bool withoutTrailingSlashEquality( const QString & a, const QString & b ) {
        if( a.endsWith('/') ) {
            if( b.endsWith('/') )
                return a == b;

            return a.mid( 0, a.length() -1 ) == b;
        }
        else if( b.endsWith('/') ) {
            return a == b.mid( 0, b.length() -1 );
        }
        else
            return a == b;
    }
}

// static
void FileSystemTreeItem::insert(QList< FileSystemTreeItem* >& list, FileSystemTreeItem* item, FileSystemTreeItem* parent)
{
    if( list.isEmpty() ) {
        list.append( item );
        item->m_parent = parent;
        return;
    }
    
    // Step 1 : Check for duplicates
    foreach( FileSystemTreeItem *p, list ) {
        if( withoutTrailingSlashEquality( p->m_url, item->m_url ) ) {
            return;
        }
    }
    
    // Step 2 : Check if it should be inserted in this list
    //          If so, call for that FileSystemTreeItem *
    foreach( FileSystemTreeItem * p, list ) {
        if( p->isFolder() && p->shouldBeParentOf( item ) ) {
            insert( p->m_children, item, p );
            return;
        }
    }

    // Step 3 : Insert in the correct position
    QMutableListIterator<FileSystemTreeItem* > iter( list );
    while( iter.hasNext() ) {
        FileSystemTreeItem * p = iter.next();
        
        if( withoutTrailingSlashCompare( item->m_url, p->m_url ) ) {
            iter.previous();
            // Insert it and set it's parent
            iter.insert( item );
            item->m_parent = parent;
            break;
        }
    }

    // Insert in the end if proper position not found
    if( !iter.hasNext() ) {
        list.append( item );
        item->m_parent = parent;
        return;
    }
    
    if( item->isFile() ) {
        return;
    }

    // Insert all the files which are in the list and should be under item
    while( iter.hasNext() ) {
        FileSystemTreeItem * p = iter.next();
        if( item->shouldBeParentOf( p ) ) {
            iter.remove();
            insert( item->m_children, p, item );
        }
    }
}

FileSystemTreeItem* FileSystemTreeItem::find(QList< FileSystemTreeItem* >& list, const QString& url)
{
    foreach( FileSystemTreeItem * item, list ) {
        if( withoutTrailingSlashEquality( item->url(), url ) )
            return item;
    }

    foreach( FileSystemTreeItem * t, list ) {
        if( url.contains( t->m_url ) )
            return t->find( url );
    }
    
    return 0;
}


FileSystemTreeItem* FileSystemTreeItem::child(int row) const
{
    return m_children.at( row );
}



int FileSystemTreeItem::numChildren() const
{
    return m_children.size();
}



FileSystemTreeItem* FileSystemTreeItem::parent() const
{
    return m_parent;
}



int FileSystemTreeItem::parentRowNum()
{
    if( m_parent ) {
        return m_parent->m_children.indexOf( this );
    }

    return -1;
}


int FileSystemTreeItem::size() const
{
    if( m_children.isEmpty() )
        return 1;
    
    int s = 1; // For self
    foreach( const FileSystemTreeItem * child, m_children )
        s += child->size();
    return s;
}



QString FileSystemTreeItem::url() const
{
    return m_url;
}


void FileSystemTreeItem::add(FileSystemTreeItem* item)
{
    if( withoutTrailingSlashEquality( item->url(), url() ) )
        return;
    
    insert( m_children, item, this ); 
}


FileSystemTreeItem* FileSystemTreeItem::find(const QString& url)
{
    if( withoutTrailingSlashEquality( m_url, url ) )
        return this;

    return find( m_children, url );
}


QList<QString> FileSystemTreeItem::toList() const
{
    if( numChildren() == 0 && !m_url.isEmpty() ) {
        return QList<QString>() << m_url;
    }
    
    QList<QString> urls;
    if( !m_url.isEmpty() )
        urls << m_url;
    
    foreach( FileSystemTreeItem * item, m_children )
        urls << item->toList();
    
    return urls;
}


bool FileSystemTreeItem::isFolder() const
{
    return m_isFolder;
}


QList<FileSystemTreeItem* > FileSystemTreeItem::children() const
{
    return m_children;
}


bool FileSystemTreeItem::isNull() const
{
    return m_url.isEmpty() && numChildren() == 0;
}

int FileSystemTreeItem::depth()
{
    int d = m_url.count('/');
    if( isFolder() )
        return d-1;
    else
        return d;
}