1 /***************************************************************************
2 * Copyright (C) 2007 by Sebastian Trueg <trueg@kde.org> *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
20 #include "metadatawidget.h"
22 #include "commentwidget.h"
24 #include <config-nepomuk.h>
28 #include <KMessageBox>
30 #include <QtCore/QEvent>
31 #include <QtCore/QMutex>
32 #include <QtCore/QMutexLocker>
33 #include <QtCore/QThread>
34 #include <QtGui/QLabel>
35 #include <QtGui/QGridLayout>
36 #include <QtGui/QTextEdit>
39 #include "nepomukmassupdatejob.h"
40 #include <nepomuk/kmetadatatagwidget.h>
41 #include <nepomuk/resourcemanager.h>
42 #include <nepomuk/resource.h>
43 #include <nepomuk/variant.h>
44 #include <nepomuk/kratingwidget.h>
45 #include <Soprano/Vocabulary/Xesam>
46 #include "resourcetaggingwidget.h"
50 bool MetaDataWidget::metaDataAvailable()
53 return !Nepomuk::ResourceManager::instance()->init();
60 class MetaDataWidget::Private
64 void loadComment(const QString
& comment
);
66 CommentWidget
* editComment
;
67 KRatingWidget
* ratingWidget
;
68 Nepomuk::ResourceTaggingWidget
* tagWidget
;
70 // shared data between the GUI-thread and
71 // the loader-thread (see LoadFilesThread):
77 QList
<Nepomuk::Resource
> fileRes
;
78 QMap
<KUrl
, Nepomuk::Resource
> files
;
82 * Loads the meta data of files and writes
83 * the result into a shared data pool that
84 * can be used by the widgets in the GUI thread.
86 class LoadFilesThread
: public QThread
89 LoadFilesThread(SharedData
* sharedData
, QMutex
* mutex
);
90 void setFiles(const KUrl::List
& urls
);
94 SharedData
* m_sharedData
;
99 LoadFilesThread
* loadFilesThread
;
104 void MetaDataWidget::Private::loadComment(const QString
& comment
)
106 editComment
->setComment( comment
);
109 MetaDataWidget::Private::LoadFilesThread::LoadFilesThread(
110 MetaDataWidget::Private::SharedData
* sharedData
,
112 m_sharedData(sharedData
),
118 void MetaDataWidget::Private::LoadFilesThread::setFiles(const KUrl::List
& urls
)
120 QMutexLocker
locker( m_mutex
);
124 void MetaDataWidget::Private::LoadFilesThread::run()
126 QMutexLocker
locker( m_mutex
);
127 const KUrl::List urls
= m_urls
;
131 QList
<Nepomuk::Resource
> fileRes
;
132 QMap
<KUrl
, Nepomuk::Resource
> files
;
133 unsigned int rating
= 0;
135 Q_FOREACH( const KUrl
&url
, urls
) {
136 Nepomuk::Resource
file( url
, Soprano::Vocabulary::Xesam::File() );
137 files
.insert( url
, file
);
138 fileRes
.append( file
);
140 if ( !first
&& rating
!= file
.rating() ) {
141 rating
= 0; // reset rating
144 rating
= file
.rating();
147 if ( !first
&& comment
!= file
.description() ) {
151 comment
= file
.description();
157 m_sharedData
->rating
= rating
;
158 m_sharedData
->comment
= comment
;
159 m_sharedData
->fileRes
= fileRes
;
160 m_sharedData
->files
= files
;
164 MetaDataWidget::MetaDataWidget(QWidget
* parent
) :
169 d
->editComment
= new CommentWidget(this);
170 d
->editComment
->setFocusPolicy(Qt::ClickFocus
);
171 d
->ratingWidget
= new KRatingWidget(this);
172 d
->ratingWidget
->setAlignment( Qt::AlignCenter
);
173 d
->tagWidget
= new Nepomuk::ResourceTaggingWidget(this);
174 connect(d
->ratingWidget
, SIGNAL(ratingChanged(unsigned int)), this, SLOT(slotRatingChanged(unsigned int)));
175 connect(d
->editComment
, SIGNAL(commentChanged(const QString
&)), this, SLOT(slotCommentChanged(const QString
&)));
176 connect( d
->tagWidget
, SIGNAL( tagClicked( const Nepomuk::Tag
& ) ), this, SLOT( slotTagClicked( const Nepomuk::Tag
& ) ) );
178 d
->sharedData
.rating
= 0;
179 d
->loadFilesThread
= new Private::LoadFilesThread(&d
->sharedData
, &d
->mutex
);
180 connect(d
->loadFilesThread
, SIGNAL(finished()), this, SLOT(slotLoadingFinished()));
182 QVBoxLayout
* lay
= new QVBoxLayout(this);
184 lay
->addWidget(d
->ratingWidget
);
185 lay
->addWidget(d
->editComment
);
186 lay
->addWidget( d
->tagWidget
);
193 MetaDataWidget::~MetaDataWidget()
196 delete d
->loadFilesThread
;
202 void MetaDataWidget::setFile(const KUrl
& url
)
210 void MetaDataWidget::setFiles(const KUrl::List
& urls
)
213 d
->loadFilesThread
->setFiles( urls
);
214 d
->loadFilesThread
->start();
221 void MetaDataWidget::slotCommentChanged( const QString
& s
)
224 QMutexLocker
locker( &d
->mutex
);
225 Nepomuk::MassUpdateJob
* job
= Nepomuk::MassUpdateJob::commentResources( d
->sharedData
.files
.values(), s
);
226 connect( job
, SIGNAL( result( KJob
* ) ),
227 this, SLOT( metadataUpdateDone() ) );
228 setEnabled( false ); // no updates during execution
236 void MetaDataWidget::slotRatingChanged(unsigned int rating
)
239 QMutexLocker
locker( &d
->mutex
);
240 Nepomuk::MassUpdateJob
* job
= Nepomuk::MassUpdateJob::rateResources( d
->sharedData
.files
.values(), rating
);
241 connect( job
, SIGNAL( result( KJob
* ) ),
242 this, SLOT( metadataUpdateDone() ) );
243 setEnabled( false ); // no updates during execution
251 void MetaDataWidget::metadataUpdateDone()
257 bool MetaDataWidget::eventFilter(QObject
* obj
, QEvent
* event
)
259 return QWidget::eventFilter(obj
, event
);
263 void MetaDataWidget::slotTagClicked( const Nepomuk::Tag
& tag
)
267 d
->tagWidget
->showTagPopup( QCursor::pos() );
271 void MetaDataWidget::slotLoadingFinished()
274 QMutexLocker
locker( &d
->mutex
);
275 d
->ratingWidget
->setRating( d
->sharedData
.rating
);
276 d
->loadComment( d
->sharedData
.comment
);
277 d
->tagWidget
->setResources( d
->sharedData
.fileRes
);
281 #include "metadatawidget.moc"