]>
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>
27 #include <KMessageBox>
29 #include <QtCore/QEvent>
30 #include <QtCore/QMutex>
31 #include <QtCore/QMutexLocker>
32 #include <QtCore/QThread>
33 #include <QtGui/QLabel>
34 #include <QtGui/QGridLayout>
35 #include <QtGui/QTextEdit>
38 #include "nepomukmassupdatejob.h"
39 #include <nepomuk/kmetadatatagwidget.h>
40 #include <nepomuk/resourcemanager.h>
41 #include <nepomuk/resource.h>
42 #include <nepomuk/variant.h>
43 #include <nepomuk/kratingwidget.h>
44 #include <Soprano/Vocabulary/Xesam>
45 #include "resourcetaggingwidget.h"
49 bool MetaDataWidget::metaDataAvailable()
52 return !Nepomuk::ResourceManager::instance()->init();
59 class MetaDataWidget::Private
63 CommentWidget
* commentWidget
;
64 KRatingWidget
* ratingWidget
;
65 Nepomuk::ResourceTaggingWidget
* tagWidget
;
67 // shared data between the GUI-thread and
68 // the loader-thread (see LoadFilesThread):
74 QList
<Nepomuk::Resource
> fileRes
;
75 QMap
<KUrl
, Nepomuk::Resource
> files
;
79 * Loads the meta data of files and writes
80 * the result into a shared data pool that
81 * can be used by the widgets in the GUI thread.
83 class LoadFilesThread
: public QThread
86 LoadFilesThread(SharedData
* sharedData
, QMutex
* mutex
);
88 void loadFiles(const KUrl::List
& urls
);
92 SharedData
* m_sharedData
;
98 LoadFilesThread
* loadFilesThread
;
104 MetaDataWidget::Private::LoadFilesThread::LoadFilesThread(
105 MetaDataWidget::Private::SharedData
* sharedData
,
107 m_sharedData(sharedData
),
114 MetaDataWidget::Private::LoadFilesThread::~LoadFilesThread()
116 // this thread may very well be deleted during execution. We need
117 // to protect it from crashes here
122 void MetaDataWidget::Private::LoadFilesThread::loadFiles(const KUrl::List
& urls
)
124 QMutexLocker
locker( m_mutex
);
130 void MetaDataWidget::Private::LoadFilesThread::run()
132 QMutexLocker
locker( m_mutex
);
133 const KUrl::List urls
= m_urls
;
137 QList
<Nepomuk::Resource
> fileRes
;
138 QMap
<KUrl
, Nepomuk::Resource
> files
;
139 unsigned int rating
= 0;
141 Q_FOREACH( const KUrl
&url
, urls
) {
144 Nepomuk::Resource
file( url
, Soprano::Vocabulary::Xesam::File() );
145 files
.insert( url
, file
);
146 fileRes
.append( file
);
148 if ( !first
&& rating
!= file
.rating() ) {
149 rating
= 0; // reset rating
152 rating
= file
.rating();
155 if ( !first
&& comment
!= file
.description() ) {
159 comment
= file
.description();
165 m_sharedData
->rating
= rating
;
166 m_sharedData
->comment
= comment
;
167 m_sharedData
->fileRes
= fileRes
;
168 m_sharedData
->files
= files
;
172 MetaDataWidget::MetaDataWidget(QWidget
* parent
) :
177 d
->commentWidget
= new CommentWidget(this);
178 d
->commentWidget
->setFocusPolicy(Qt::ClickFocus
);
179 d
->ratingWidget
= new KRatingWidget(this);
180 d
->ratingWidget
->setAlignment( Qt::AlignCenter
);
181 d
->tagWidget
= new Nepomuk::ResourceTaggingWidget(this);
182 connect(d
->ratingWidget
, SIGNAL(ratingChanged(unsigned int)), this, SLOT(slotRatingChanged(unsigned int)));
183 connect(d
->commentWidget
, SIGNAL(commentChanged(const QString
&)), this, SLOT(slotCommentChanged(const QString
&)));
184 connect(d
->tagWidget
, SIGNAL(tagClicked(const Nepomuk::Tag
&)), this, SLOT(slotTagClicked( const Nepomuk::Tag
&)));
186 d
->sharedData
.rating
= 0;
187 d
->loadFilesThread
= new Private::LoadFilesThread(&d
->sharedData
, &d
->mutex
);
188 connect(d
->loadFilesThread
, SIGNAL(finished()), this, SLOT(slotLoadingFinished()));
190 QVBoxLayout
* lay
= new QVBoxLayout(this);
192 lay
->addWidget(d
->ratingWidget
);
193 lay
->addWidget(d
->commentWidget
);
194 lay
->addWidget( d
->tagWidget
);
201 MetaDataWidget::~MetaDataWidget()
204 delete d
->loadFilesThread
;
209 void MetaDataWidget::setRatingVisible(bool visible
)
212 d
->ratingWidget
->setVisible(visible
);
219 bool MetaDataWidget::isRatingVisible() const
222 return d
->ratingWidget
->isVisible();
229 void MetaDataWidget::setCommentVisible(bool visible
)
232 d
->commentWidget
->setVisible(visible
);
239 bool MetaDataWidget::isCommentVisible() const
242 return d
->commentWidget
->isVisible();
249 void MetaDataWidget::setTagsVisible(bool visible
)
252 d
->tagWidget
->setVisible(visible
);
259 bool MetaDataWidget::areTagsVisible() const
262 return d
->tagWidget
->isVisible();
269 void MetaDataWidget::setFile(const KUrl
& url
)
276 void MetaDataWidget::setFiles(const KUrl::List
& urls
)
279 // Assure that the currently edited text is stored before
280 // loading the meta data for new files.
281 const QString currentComment
= d
->commentWidget
->editorText();
282 if ( currentComment
!= d
->commentWidget
->comment() ) {
283 slotCommentChanged( currentComment
);
286 d
->loadFilesThread
->loadFiles( urls
);
293 void MetaDataWidget::slotCommentChanged( const QString
& s
)
296 disconnect(d
->commentWidget
, SIGNAL(commentChanged(const QString
&)), this, SLOT(slotCommentChanged(const QString
&)));
298 QMutexLocker
locker( &d
->mutex
);
299 Nepomuk::MassUpdateJob
* job
= Nepomuk::MassUpdateJob::commentResources( d
->sharedData
.files
.values(), s
);
300 connect( job
, SIGNAL( result( KJob
* ) ),
301 this, SLOT( metadataUpdateDone() ) );
302 setEnabled( false ); // no updates during execution
305 connect(d
->commentWidget
, SIGNAL(commentChanged(const QString
&)), this, SLOT(slotCommentChanged(const QString
&)));
312 void MetaDataWidget::slotRatingChanged(unsigned int rating
)
315 QMutexLocker
locker( &d
->mutex
);
316 Nepomuk::MassUpdateJob
* job
= Nepomuk::MassUpdateJob::rateResources( d
->sharedData
.files
.values(), rating
);
317 connect( job
, SIGNAL( result( KJob
* ) ),
318 this, SLOT( metadataUpdateDone() ) );
319 setEnabled( false ); // no updates during execution
327 void MetaDataWidget::metadataUpdateDone()
333 bool MetaDataWidget::eventFilter(QObject
* obj
, QEvent
* event
)
335 return QWidget::eventFilter(obj
, event
);
339 void MetaDataWidget::slotTagClicked( const Nepomuk::Tag
& tag
)
343 d
->tagWidget
->showTagPopup( QCursor::pos() );
347 void MetaDataWidget::slotLoadingFinished()
350 QMutexLocker
locker( &d
->mutex
);
351 d
->ratingWidget
->setRating( d
->sharedData
.rating
);
352 d
->commentWidget
->setComment( d
->sharedData
.comment
);
353 d
->tagWidget
->setResources( d
->sharedData
.fileRes
);
357 #include "metadatawidget.moc"