]>
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
),
121 MetaDataWidget::Private::LoadFilesThread::~LoadFilesThread()
123 // this thread may very well be deleted during execution. We need
124 // to protect it from crashes here
129 void MetaDataWidget::Private::LoadFilesThread::loadFiles(const KUrl::List
& urls
)
131 QMutexLocker
locker( m_mutex
);
137 void MetaDataWidget::Private::LoadFilesThread::run()
139 QMutexLocker
locker( m_mutex
);
140 const KUrl::List urls
= m_urls
;
144 QList
<Nepomuk::Resource
> fileRes
;
145 QMap
<KUrl
, Nepomuk::Resource
> files
;
146 unsigned int rating
= 0;
148 Q_FOREACH( const KUrl
&url
, urls
) {
151 Nepomuk::Resource
file( url
, Soprano::Vocabulary::Xesam::File() );
152 files
.insert( url
, file
);
153 fileRes
.append( file
);
155 if ( !first
&& rating
!= file
.rating() ) {
156 rating
= 0; // reset rating
159 rating
= file
.rating();
162 if ( !first
&& comment
!= file
.description() ) {
166 comment
= file
.description();
172 m_sharedData
->rating
= rating
;
173 m_sharedData
->comment
= comment
;
174 m_sharedData
->fileRes
= fileRes
;
175 m_sharedData
->files
= files
;
179 MetaDataWidget::MetaDataWidget(QWidget
* parent
) :
184 d
->editComment
= new CommentWidget(this);
185 d
->editComment
->setFocusPolicy(Qt::ClickFocus
);
186 d
->ratingWidget
= new KRatingWidget(this);
187 d
->ratingWidget
->setAlignment( Qt::AlignCenter
);
188 d
->tagWidget
= new Nepomuk::ResourceTaggingWidget(this);
189 connect(d
->ratingWidget
, SIGNAL(ratingChanged(unsigned int)), this, SLOT(slotRatingChanged(unsigned int)));
190 connect(d
->editComment
, SIGNAL(commentChanged(const QString
&)), this, SLOT(slotCommentChanged(const QString
&)));
191 connect( d
->tagWidget
, SIGNAL( tagClicked( const Nepomuk::Tag
& ) ), this, SLOT( slotTagClicked( const Nepomuk::Tag
& ) ) );
193 d
->sharedData
.rating
= 0;
194 d
->loadFilesThread
= new Private::LoadFilesThread(&d
->sharedData
, &d
->mutex
);
195 connect(d
->loadFilesThread
, SIGNAL(finished()), this, SLOT(slotLoadingFinished()));
197 QVBoxLayout
* lay
= new QVBoxLayout(this);
199 lay
->addWidget(d
->ratingWidget
);
200 lay
->addWidget(d
->editComment
);
201 lay
->addWidget( d
->tagWidget
);
208 MetaDataWidget::~MetaDataWidget()
211 delete d
->loadFilesThread
;
216 void MetaDataWidget::setRatingVisible(bool visible
)
219 d
->ratingWidget
->setVisible(visible
);
226 bool MetaDataWidget::isRatingVisible() const
229 return d
->ratingWidget
->isVisible();
236 void MetaDataWidget::setCommentVisible(bool visible
)
239 d
->editComment
->setVisible(visible
);
246 bool MetaDataWidget::isCommentVisible() const
249 return d
->editComment
->isVisible();
256 void MetaDataWidget::setTagsVisible(bool visible
)
259 d
->tagWidget
->setVisible(visible
);
266 bool MetaDataWidget::areTagsVisible() const
269 return d
->tagWidget
->isVisible();
276 void MetaDataWidget::setFile(const KUrl
& url
)
284 void MetaDataWidget::setFiles(const KUrl::List
& urls
)
287 d
->loadFilesThread
->loadFiles( urls
);
294 void MetaDataWidget::slotCommentChanged( const QString
& s
)
297 QMutexLocker
locker( &d
->mutex
);
298 Nepomuk::MassUpdateJob
* job
= Nepomuk::MassUpdateJob::commentResources( d
->sharedData
.files
.values(), s
);
299 connect( job
, SIGNAL( result( KJob
* ) ),
300 this, SLOT( metadataUpdateDone() ) );
301 setEnabled( false ); // no updates during execution
309 void MetaDataWidget::slotRatingChanged(unsigned int rating
)
312 QMutexLocker
locker( &d
->mutex
);
313 Nepomuk::MassUpdateJob
* job
= Nepomuk::MassUpdateJob::rateResources( d
->sharedData
.files
.values(), rating
);
314 connect( job
, SIGNAL( result( KJob
* ) ),
315 this, SLOT( metadataUpdateDone() ) );
316 setEnabled( false ); // no updates during execution
324 void MetaDataWidget::metadataUpdateDone()
330 bool MetaDataWidget::eventFilter(QObject
* obj
, QEvent
* event
)
332 return QWidget::eventFilter(obj
, event
);
336 void MetaDataWidget::slotTagClicked( const Nepomuk::Tag
& tag
)
340 d
->tagWidget
->showTagPopup( QCursor::pos() );
344 void MetaDataWidget::slotLoadingFinished()
347 QMutexLocker
locker( &d
->mutex
);
348 d
->ratingWidget
->setRating( d
->sharedData
.rating
);
349 d
->loadComment( d
->sharedData
.comment
);
350 d
->tagWidget
->setResources( d
->sharedData
.fileRes
);
354 #include "metadatawidget.moc"