File: identifiermodel.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 (207 lines) | stat: -rw-r--r-- 5,687 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
/*
    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 "identifiermodel.h"
#include "identifiermodeltree.h"

#include <Nepomuk/Vocabulary/NIE>
#include <Nepomuk/Vocabulary/NFO>
#include <Soprano/Vocabulary/RDF>

#include <Soprano/QueryResultIterator>
#include <Soprano/Model>
#include <Soprano/Statement>

#include <Nepomuk/ResourceManager>

#include <KUrl>
#include <KDebug>
#include <KRandom>

Nepomuk::IdentifierModel::IdentifierModel(QObject* parent): QAbstractItemModel(parent)
{
    m_tree = new IdentifierModelTree();
}


Nepomuk::IdentifierModel::~IdentifierModel()
{
    delete m_tree;
}


QVariant Nepomuk::IdentifierModel::data(const QModelIndex& index, int role) const
{
    if( !index.isValid() )
        return QVariant();
    
    IdentifierModelTreeItem *item = static_cast<IdentifierModelTreeItem *>(index.internalPointer());

    switch(role) {
        case LabelRole:
        case Qt::DisplayRole:
            return  item->toString();
            
        case SizeRole:
            //FIXME: How am I supposed to get the size?
            return KRandom::random();
            
        case TypeRole:
            return item->type();
            
        case ResourceRole:
            return item->resourceUri();
            
        case IdentifiedResourceRole:
            return item->identifiedUri();
            
        case DiscardedRole:
            return item->discarded();
    }
    
    return QVariant();
}


int Nepomuk::IdentifierModel::columnCount(const QModelIndex& parent) const
{
    Q_UNUSED( parent );
    // It is always 1 for now
    return 1;
}


int Nepomuk::IdentifierModel::rowCount(const QModelIndex& parent) const
{
    if( parent.column() > 0 )
        return 0;
    
    if( !parent.isValid() ) {
        return ( m_tree->isEmpty() ? 0 : m_tree->rootNodes().size() );
    }
    
    IdentifierModelTreeItem * parentItem = static_cast<IdentifierModelTreeItem *>(parent.internalPointer());

    return parentItem->numChildren();
}


QModelIndex Nepomuk::IdentifierModel::parent(const QModelIndex& index) const
{
    if( !index.isValid() )
        return QModelIndex();

    IdentifierModelTreeItem *child = static_cast<IdentifierModelTreeItem*>( index.internalPointer() );
    IdentifierModelTreeItem *parent = static_cast< IdentifierModelTreeItem*>( child->parent() );
    if( parent == 0 )
        return QModelIndex();

    if( parent->parent() == 0 ) {
        // Its parent is one of the root nodes
        int rootPos = m_tree->rootNodes().indexOf( parent );
        if( rootPos == -1 )
            return QModelIndex();

        return createIndex( rootPos, 0, parent );
    }

    return createIndex( parent->parentRowNum(), 0, parent );
}


QModelIndex Nepomuk::IdentifierModel::index(int row, int column, const QModelIndex& parent) const
{
    if( column > 0 )
        return QModelIndex();
    
    if( !parent.isValid() ) {
        // One of the root nodes
        if( m_tree->isEmpty() || row >= m_tree->rootNodes().size() )
            return QModelIndex();
        
        return createIndex( row, column, m_tree->rootNodes().at( row ) );
    }
    
    IdentifierModelTreeItem *parentItem = static_cast<IdentifierModelTreeItem *>( parent.internalPointer() );
    if( row >= parentItem->numChildren() )
        return QModelIndex();
    
    return createIndex(row, column, parentItem->child( row ) );
}


void Nepomuk::IdentifierModel::identified( const QUrl& oldUri, const QUrl& newUri)
{
//    kDebug() << oldUri << " -----> " << newUri;

    //FIXME: Should probably emit dataChanged
    emit layoutAboutToBeChanged();
    
    IdentifierModelTreeItem* item = m_tree->findByUri( oldUri );
    if( item ) {
        kDebug() << item->url() << " --- URL --> " << newUri;
        item->setIdentified( newUri );
    }
    
    emit layoutChanged();
}


void Nepomuk::IdentifierModel::notIdentified( const QList< Soprano::Statement >& sts )
{
    if( sts.isEmpty() )
        return;
    
    beginResetModel();

    // If already exists - Remove it
    QUrl resUri = sts.first().subject().uri();
    IdentifierModelTreeItem* it = m_tree->findByUri( resUri );
    if( it ) {
        m_tree->remove( it );
    }

    // Insert into the tree
    IdentifierModelTreeItem* item = IdentifierModelTreeItem::fromStatementList( sts );
    item->setUnidentified();
    m_tree->add( item );

    endResetModel();
}

Qt::ItemFlags Nepomuk::IdentifierModel::flags(const QModelIndex& index) const
{
    Q_UNUSED( index );
    //return QAbstractItemModel::flags(index);
    return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
}

void Nepomuk::IdentifierModel::ignoreAll()
{
    emit layoutAboutToBeChanged();
    
    m_tree->discardAll();
    
    emit layoutChanged();
}

#include "identifiermodel.moc"