]>
cloud.milkyroute.net Git - dolphin.git/blob - src/panels/information/metadatawidget.cpp
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
);
91 void loadFiles(const KUrl::List
& urls
);
95 SharedData
* m_sharedData
;
101 LoadFilesThread
* loadFilesThread
;
106 void MetaDataWidget::Private::loadComment(const QString
& comment
)
108 editComment
->setComment( comment
);
111 MetaDataWidget::Private::LoadFilesThread::LoadFilesThread(
112 MetaDataWidget::Private::SharedData
* sharedData
,
114 m_sharedData(sharedData
),
120 MetaDataWidget::Private::LoadFilesThread::~LoadFilesThread()
122 // this thread may very well be deleted during execution. We need
123 // to protect it from crashes here
128 void MetaDataWidget::Private::LoadFilesThread::loadFiles(const KUrl::List
& urls
)
130 QMutexLocker
locker( m_mutex
);
136 void MetaDataWidget::Private::LoadFilesThread::run()
138 QMutexLocker
locker( m_mutex
);
139 const KUrl::List urls
= m_urls
;
143 QList
<Nepomuk::Resource
> fileRes
;
144 QMap
<KUrl
, Nepomuk::Resource
> files
;
145 unsigned int rating
= 0;
147 Q_FOREACH( const KUrl
&url
, urls
) {
150 Nepomuk::Resource
file( url
, Soprano::Vocabulary::Xesam::File() );
151 files
.insert( url
, file
);
152 fileRes
.append( file
);
154 if ( !first
&& rating
!= file
.rating() ) {
155 rating
= 0; // reset rating
158 rating
= file
.rating();
161 if ( !first
&& comment
!= file
.description() ) {
165 comment
= file
.description();
171 m_sharedData
->rating
= rating
;
172 m_sharedData
->comment
= comment
;
173 m_sharedData
->fileRes
= fileRes
;
174 m_sharedData
->files
= files
;
178 MetaDataWidget::MetaDataWidget(QWidget
* parent
) :
183 d
->editComment
= new CommentWidget(this);
184 d
->editComment
->setFocusPolicy(Qt::ClickFocus
);
185 d
->ratingWidget
= new KRatingWidget(this);
186 d
->ratingWidget
->setAlignment( Qt::AlignCenter
);
187 d
->tagWidget
= new Nepomuk::ResourceTaggingWidget(this);
188 connect(d
->ratingWidget
, SIGNAL(ratingChanged(unsigned int)), this, SLOT(slotRatingChanged(unsigned int)));
189 connect(d
->editComment
, SIGNAL(commentChanged(const QString
&)), this, SLOT(slotCommentChanged(const QString
&)));
190 connect( d
->tagWidget
, SIGNAL( tagClicked( const Nepomuk::Tag
& ) ), this, SLOT( slotTagClicked( const Nepomuk::Tag
& ) ) );
192 d
->sharedData
.rating
= 0;
193 d
->loadFilesThread
= new Private::LoadFilesThread(&d
->sharedData
, &d
->mutex
);
194 connect(d
->loadFilesThread
, SIGNAL(finished()), this, SLOT(slotLoadingFinished()));
196 QVBoxLayout
* lay
= new QVBoxLayout(this);
198 lay
->addWidget(d
->ratingWidget
);
199 lay
->addWidget(d
->editComment
);
200 lay
->addWidget( d
->tagWidget
);
207 MetaDataWidget::~MetaDataWidget()
210 delete d
->loadFilesThread
;
215 void MetaDataWidget::setRatingVisible(bool visible
)
218 d
->ratingWidget
->setVisible(visible
);
225 bool MetaDataWidget::isRatingVisible() const
228 return d
->ratingWidget
->isVisible();
235 void MetaDataWidget::setCommentVisible(bool visible
)
238 d
->editComment
->setVisible(visible
);
245 bool MetaDataWidget::isCommentVisible() const
248 return d
->editComment
->isVisible();
255 void MetaDataWidget::setTagsVisible(bool visible
)
258 d
->tagWidget
->setVisible(visible
);
265 bool MetaDataWidget::areTagsVisible() const
268 return d
->tagWidget
->isVisible();
275 void MetaDataWidget::setFile(const KUrl
& url
)
283 void MetaDataWidget::setFiles(const KUrl::List
& urls
)
286 d
->loadFilesThread
->loadFiles( urls
);
293 void MetaDataWidget::slotCommentChanged( const QString
& s
)
296 QMutexLocker
locker( &d
->mutex
);
297 Nepomuk::MassUpdateJob
* job
= Nepomuk::MassUpdateJob::commentResources( d
->sharedData
.files
.values(), s
);
298 connect( job
, SIGNAL( result( KJob
* ) ),
299 this, SLOT( metadataUpdateDone() ) );
300 setEnabled( false ); // no updates during execution
308 void MetaDataWidget::slotRatingChanged(unsigned int rating
)
311 QMutexLocker
locker( &d
->mutex
);
312 Nepomuk::MassUpdateJob
* job
= Nepomuk::MassUpdateJob::rateResources( d
->sharedData
.files
.values(), rating
);
313 connect( job
, SIGNAL( result( KJob
* ) ),
314 this, SLOT( metadataUpdateDone() ) );
315 setEnabled( false ); // no updates during execution
323 void MetaDataWidget::metadataUpdateDone()
329 bool MetaDataWidget::eventFilter(QObject
* obj
, QEvent
* event
)
331 return QWidget::eventFilter(obj
, event
);
335 void MetaDataWidget::slotTagClicked( const Nepomuk::Tag
& tag
)
339 d
->tagWidget
->showTagPopup( QCursor::pos() );
343 void MetaDataWidget::slotLoadingFinished()
346 QMutexLocker
locker( &d
->mutex
);
347 d
->ratingWidget
->setRating( d
->sharedData
.rating
);
348 d
->loadComment( d
->sharedData
.comment
);
349 d
->tagWidget
->setResources( d
->sharedData
.fileRes
);
353 #include "metadatawidget.moc"