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
|
//
// C++ Implementation: UserFileModel
//
// Description:
//
//
// Author: Mikael Gransell <mikael.gransell@bredband.net>, (C) 2005
//
// Copyright: See COPYING file that comes with this distribution
//
//
#include "userfilemodel.h"
#include <QFileIconProvider>
#include <stdexcept>
QVariant UserFileModel::data( const QModelIndex &index, int role ) const
{
QVariant ret;
// If we access an item that is out of range
if (!index.isValid())
return QVariant();
// Get the item this concerns
TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
// Check if it wants the text that should be displayed or the decorator.
switch (role) {
case Qt::DisplayRole:
ret = item->data(index.column());
break;
case Qt::DecorationRole:
// only display icon for the first column
if ( index.column() == 0 ) {
// Get an icon from the icon provider
ret = m_pIconProvider->icon( item->isDir() ? QFileIconProvider::Folder : QFileIconProvider::File );
}
default:
break;
}
return ret;
}
Qt::ItemFlags UserFileModel::flags( const QModelIndex &index ) const
{
if (!index.isValid())
return Qt::ItemIsEnabled;
return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}
QVariant UserFileModel::headerData( int section, Qt::Orientation orientation, int role ) const
{
if (role != Qt::DisplayRole)
return QVariant();
if(orientation==Qt::Horizontal) {
switch( section ) {
case TreeItem::FILE_COLUMN:
return tr("File Name");
case TreeItem::SIZE_COLUMN:
return tr("Size");
case TreeItem::TTH_COLUMN:
return tr("TTH");
}
}
return QVariant();
}
QModelIndex UserFileModel::index( int row, int column, const QModelIndex &parent ) const
{
TreeItem *parentItem;
if (!parent.isValid())
parentItem = root;
else
parentItem = static_cast<TreeItem*>(parent.internalPointer());
TreeItem *childItem = parentItem->child(row);
if (childItem)
return createIndex(row, column, childItem);
else
return QModelIndex();
}
QModelIndex UserFileModel::parent( const QModelIndex &index ) const
{
if (!index.isValid())
return QModelIndex();
TreeItem *childItem = static_cast<TreeItem*>(index.internalPointer());
TreeItem *parentItem = childItem->parent();
if (parentItem == root)
return QModelIndex();
return createIndex(parentItem->row(), 0, parentItem);
}
int UserFileModel::rowCount( const QModelIndex &parent ) const
{
TreeItem *parentItem;
if (!parent.isValid())
parentItem = root;
else
parentItem = static_cast<TreeItem*>(parent.internalPointer());
return parentItem->childCount();
}
int UserFileModel::columnCount(const QModelIndex &parent) const
{
if (parent.isValid())
return static_cast<TreeItem*>(parent.internalPointer())->columnCount();
else
return root->columnCount();
}
UserFileModel::TreeItem* UserFileModel::getItem( const QModelIndex& index ) const
{
if ( !index.isValid() )
throw std::out_of_range("Bad QModelIndex");
TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
return item;
}
int UserFileModel::getUserId() const
{
return userId;
}
bool UserFileModel::setTree( const QString& tree )
{
root = new TreeItem( "", NULL );
// Start parsing the tree.s
handleSubTree( tree, root );
// Calculate the size of each node.
root->calculateSize();
emit layoutChanged();
return true;
}
void UserFileModel::handleSubTree( const QString& tree, TreeItem* parent ) const
{
// Check if this is a directory
int start = 0;
int endDir = -1;
while ( (start = tree.indexOf( '/', endDir + 1 )) != -1 ) {
// Retrieve directory name
QString dirName = tree.mid( endDir + 1, start - (endDir + 1) );
TreeItem* item = new TreeItem( dirName, parent );
//logger->info( "Adding directory " + dirName );
// Find end of this dir
endDir = start + 1;
int level = 1;
while ( level ) {
QChar c = tree[endDir];
// Go down a level if we find a new dir
if( c == '/' ) level++;
// Go up if we find the end of a dir
if( c == '\\' ) level--;
// Step forward
endDir++;
}
QString dir = tree.mid( start + 1, endDir - start - 1 );
handleSubTree( dir, item );
}
// Find the end of this directory
int endFiles = tree.indexOf( '\\', endDir + 1 );
// Get the files string
QString files = tree.mid( endDir + 1, endFiles );
//logger->info( "files: " + files );
// Split all files and go through them
QStringList fileList = files.split( '*' );
for ( int i = 0; i < fileList.size(); i++ ) {
addFile( fileList[i], parent );
}
}
void UserFileModel::addFile( const QString& file, TreeItem* parent ) const
{
// Split the file with sep :
QStringList lst = file.split( '|' );
if ( lst.size() >= 2 ) {
// Pos 1 is the file name
QString name = lst[0];
// Pos 2 is the size
QString size = lst[1];
// Pos 3 is TTH
QString tth = lst[2];
// Create item
TreeItem* item = new TreeItem( name, parent );
item->setSize( size.toLongLong() );
item->setTTH( tth );
//logger->info( "Added file: " + name + " with size: " + size + " and tth: " + tth );
}
else {
//logger->error( "Bad file string format: " + file );
}
}
|