~ubuntu-branches/ubuntu/natty/kdebase-runtime/natty-proposed

« back to all changes in this revision

Viewing changes to nepomuk/services/backupsync/gui/identifiermodel.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2010-11-24 11:07:10 UTC
  • mto: (0.8.7 upstream)
  • mto: This revision was merged to the branch mainline in revision 129.
  • Revision ID: james.westby@ubuntu.com-20101124110710-6dbsyw0yh21qvn82
Tags: upstream-4.5.80
ImportĀ upstreamĀ versionĀ 4.5.80

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    This file is part of the Nepomuk KDE project.
 
3
    Copyright (C) 2010  Vishesh Handa <handa.vish@gmail.com>
 
4
 
 
5
   This library is free software; you can redistribute it and/or
 
6
   modify it under the terms of the GNU Lesser General Public
 
7
   License as published by the Free Software Foundation; either
 
8
   version 2.1 of the License, or (at your option) version 3, or any
 
9
   later version accepted by the membership of KDE e.V. (or its
 
10
   successor approved by the membership of KDE e.V.), which shall
 
11
   act as a proxy defined in Section 6 of version 3 of the license.
 
12
 
 
13
   This library is distributed in the hope that it will be useful,
 
14
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
16
   Lesser General Public License for more details.
 
17
 
 
18
   You should have received a copy of the GNU Lesser General Public
 
19
   License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 
20
*/
 
21
 
 
22
 
 
23
#include "identifiermodel.h"
 
24
#include "identifiermodeltree.h"
 
25
 
 
26
#include <Nepomuk/Vocabulary/NIE>
 
27
#include <Nepomuk/Vocabulary/NFO>
 
28
#include <Soprano/Vocabulary/RDF>
 
29
 
 
30
#include <Soprano/QueryResultIterator>
 
31
#include <Soprano/Model>
 
32
#include <Soprano/Statement>
 
33
 
 
34
#include <Nepomuk/ResourceManager>
 
35
 
 
36
#include <KUrl>
 
37
#include <KDebug>
 
38
#include <KRandom>
 
39
 
 
40
Nepomuk::IdentifierModel::IdentifierModel(QObject* parent): QAbstractItemModel(parent)
 
41
{
 
42
    m_tree = new IdentifierModelTree();
 
43
}
 
44
 
 
45
 
 
46
Nepomuk::IdentifierModel::~IdentifierModel()
 
47
{
 
48
    delete m_tree;
 
49
}
 
50
 
 
51
 
 
52
QVariant Nepomuk::IdentifierModel::data(const QModelIndex& index, int role) const
 
53
{
 
54
    IdentifierModelTreeItem *item = static_cast<IdentifierModelTreeItem *> (m_tree->root());
 
55
    if( index.isValid() )
 
56
        item = static_cast<IdentifierModelTreeItem *>(index.internalPointer());
 
57
    
 
58
    switch(role) {
 
59
        case LabelRole:
 
60
        case Qt::DisplayRole:
 
61
            //FIXME: Make this look good
 
62
            return QString::fromLatin1("Text for item %1").arg( item->prettyString() );
 
63
            
 
64
        case SizeRole:
 
65
            //FIXME: How am I supposed to get the size?
 
66
            return KRandom::random();
 
67
            
 
68
        case TypeRole:
 
69
            return item->url();
 
70
            
 
71
        case ResourceRole:
 
72
            return QUrl( item->resourceUri() );
 
73
            
 
74
        case IdentifiedResourceRole:
 
75
            //FIXME!
 
76
            return QUrl( item->resourceUri() );
 
77
            
 
78
        case DiscardedRole:
 
79
            return true;
 
80
            //FIXME!
 
81
            //return m_discardedResource.contains(data(index,ResourceRole).toUrl());
 
82
    }
 
83
    
 
84
    return item->url();
 
85
}
 
86
 
 
87
 
 
88
int Nepomuk::IdentifierModel::columnCount(const QModelIndex& parent) const
 
89
{
 
90
    Q_UNUSED( parent );
 
91
    // It is always 1 for now
 
92
    return 1;
 
93
}
 
94
 
 
95
 
 
96
int Nepomuk::IdentifierModel::rowCount(const QModelIndex& parent) const
 
97
{
 
98
    if( parent.column() > 0 )
 
99
        return 0;
 
100
    
 
101
    if( !parent.isValid() ) {
 
102
        //kDebug() << "NOT VALID - Probably root";
 
103
        int r = m_tree->root()->isEmpty() ? 0 : 1;
 
104
        //kDebug() << r;
 
105
        return r;
 
106
    }
 
107
    
 
108
    IdentifierModelTreeItem * parentItem;
 
109
 
 
110
    //kDebug() << "Else cluase !!";
 
111
    parentItem = static_cast<IdentifierModelTreeItem *>(parent.internalPointer());
 
112
    //if( parentItem == m_tree->root() )
 
113
    //    kDebug() << "ROOT!! :-D";
 
114
    
 
115
    int r = parentItem->numChildren();
 
116
    //kDebug() << r;
 
117
    return r;
 
118
}
 
119
 
 
120
 
 
121
QModelIndex Nepomuk::IdentifierModel::parent(const QModelIndex& index) const
 
122
{
 
123
    if( !index.isValid() )
 
124
        return QModelIndex();
 
125
 
 
126
    IdentifierModelTreeItem *childItem = static_cast<IdentifierModelTreeItem*>( index.internalPointer() );
 
127
    if( childItem == m_tree->root() )
 
128
        return QModelIndex();
 
129
 
 
130
    FileSystemTreeItem<IdentificationData> *parentItem = static_cast<FileSystemTreeItem<IdentificationData>*>( childItem->parent() );
 
131
    if( !parentItem )
 
132
        return QModelIndex();
 
133
 
 
134
    if( parentItem == m_tree->root() )
 
135
        return createIndex( 0, 0, m_tree->root() );
 
136
    
 
137
    return createIndex( parentItem->parentRowNum(), 0, parentItem );
 
138
}
 
139
 
 
140
 
 
141
QModelIndex Nepomuk::IdentifierModel::index(int row, int column, const QModelIndex& parent) const
 
142
{
 
143
    if (!parent.isValid()) {
 
144
//         IdentifierModelTreeItem * root = dynamic_cast<IdentifierModelTreeItem*>( );
 
145
 
 
146
        if( m_tree->root()->isEmpty() )
 
147
            return QModelIndex();
 
148
 
 
149
        return createIndex( row, column, m_tree->root() );
 
150
    }
 
151
    else {
 
152
        IdentifierModelTreeItem *parentItem = static_cast<IdentifierModelTreeItem *>( parent.internalPointer() );
 
153
        FileSystemTreeItem<IdentificationData> *childItem = static_cast<FileSystemTreeItem<IdentificationData>*>( parentItem->child(row) );
 
154
        return createIndex(row, column, childItem);
 
155
    }
 
156
}
 
157
 
 
158
 
 
159
void Nepomuk::IdentifierModel::identified(int id, const QString& oldUri, const QString& newUri)
 
160
{
 
161
    Q_UNUSED( id );
 
162
    kDebug() << oldUri << " -----> " << newUri;
 
163
    
 
164
    emit layoutAboutToBeChanged();
 
165
 
 
166
    m_tree->remove( oldUri );
 
167
    
 
168
    emit layoutChanged();
 
169
}
 
170
 
 
171
 
 
172
void Nepomuk::IdentifierModel::notIdentified(int id, const QList< Soprano::Statement >& sts)
 
173
{
 
174
    kDebug();
 
175
    emit layoutAboutToBeChanged();
 
176
 
 
177
    IdentifierModelTreeItem* item = IdentifierModelTreeItem::fromStatementList(id, sts);
 
178
    item->setUnidentified();;
 
179
    m_tree->add( item );
 
180
 
 
181
    emit layoutChanged();
 
182
}
 
183
 
 
184
 
 
185
void Nepomuk::IdentifierModel::debug_identified(int id, const QString& nieUrl)
 
186
{/*
 
187
    Q_UNUSED( id )
 
188
    emit layoutAboutToBeChanged();
 
189
    m_tree->remove( nieUrl );
 
190
    emit layoutChanged();*/
 
191
}
 
192
 
 
193
 
 
194
void Nepomuk::IdentifierModel::debug_notIdentified(int id, const QString& resUri, const QString& nieUrl, bool folder)
 
195
{
 
196
    
 
197
    emit layoutAboutToBeChanged();
 
198
 
 
199
    Soprano::Statement st( Soprano::Node( QUrl(resUri) ),
 
200
                           Soprano::Node( Nepomuk::Vocabulary::NIE::url() ),
 
201
                           Soprano::Node( QUrl(nieUrl) ) );
 
202
    QList<Soprano::Statement> stList;
 
203
    stList.append( st );
 
204
    if( folder ) {
 
205
        stList << Soprano::Statement( QUrl(resUri), Soprano::Vocabulary::RDF::type(), Nepomuk::Vocabulary::NFO::Folder() );
 
206
    }
 
207
    
 
208
    //kDebug() << m_tree->toList();
 
209
    m_tree->add( IdentifierModelTreeItem::fromStatementList( id, stList ) );
 
210
    kDebug() << m_tree->toList();
 
211
    emit layoutChanged();
 
212
}
 
213
 
 
214
Qt::ItemFlags Nepomuk::IdentifierModel::flags(const QModelIndex& index) const
 
215
{
 
216
    Q_UNUSED( index );
 
217
    //return QAbstractItemModel::flags(index);
 
218
    return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
 
219
}
 
220
 
 
221
#include "identifiermodel.moc"