add more spacing
[personal-kdebase.git] / apps / dolphin / src / panels / information / metadatawidget.cpp
blob44a4c029f16e0d1292f82247af8b2fafc63b3080
1 /***************************************************************************
2 * Copyright (C) 2007 by Sebastian Trueg <trueg@kde.org> *
3 * *
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. *
8 * *
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. *
13 * *
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>
26 #include <klocale.h>
27 #include <KDebug>
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>
38 #ifdef HAVE_NEPOMUK
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"
47 #endif
50 bool MetaDataWidget::metaDataAvailable()
52 #ifdef HAVE_NEPOMUK
53 return !Nepomuk::ResourceManager::instance()->init();
54 #else
55 return false;
56 #endif
60 class MetaDataWidget::Private
62 public:
63 #ifdef HAVE_NEPOMUK
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):
72 QMutex mutex;
73 struct SharedData
75 int rating;
76 QString comment;
77 QList<Nepomuk::Resource> fileRes;
78 QMap<KUrl, Nepomuk::Resource> files;
79 } sharedData;
81 /**
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
88 public:
89 LoadFilesThread(SharedData* sharedData, QMutex* mutex);
90 void setFiles(const KUrl::List& urls);
91 virtual void run();
93 private:
94 SharedData* m_sharedData;
95 QMutex* m_mutex;
96 KUrl::List m_urls;
99 LoadFilesThread* loadFilesThread;
100 #endif
103 #ifdef HAVE_NEPOMUK
104 void MetaDataWidget::Private::loadComment(const QString& comment)
106 editComment->setComment( comment );
109 MetaDataWidget::Private::LoadFilesThread::LoadFilesThread(
110 MetaDataWidget::Private::SharedData* sharedData,
111 QMutex* mutex) :
112 m_sharedData(sharedData),
113 m_mutex(mutex),
114 m_urls()
118 void MetaDataWidget::Private::LoadFilesThread::setFiles(const KUrl::List& urls)
120 QMutexLocker locker( m_mutex );
121 m_urls = urls;
124 void MetaDataWidget::Private::LoadFilesThread::run()
126 QMutexLocker locker( m_mutex );
127 const KUrl::List urls = m_urls;
128 locker.unlock();
130 bool first = true;
131 QList<Nepomuk::Resource> fileRes;
132 QMap<KUrl, Nepomuk::Resource> files;
133 unsigned int rating = 0;
134 QString comment;
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
143 else if ( first ) {
144 rating = file.rating();
147 if ( !first && comment != file.description() ) {
148 comment.clear();
150 else if ( first ) {
151 comment = file.description();
153 first = false;
156 locker.relock();
157 m_sharedData->rating = rating;
158 m_sharedData->comment = comment;
159 m_sharedData->fileRes = fileRes;
160 m_sharedData->files = files;
162 #endif
164 MetaDataWidget::MetaDataWidget(QWidget* parent) :
165 QWidget(parent)
167 #ifdef HAVE_NEPOMUK
168 d = new Private;
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);
183 lay->setMargin(0);
184 lay->addWidget(d->ratingWidget);
185 lay->addWidget(d->editComment);
186 lay->addWidget( d->tagWidget );
187 #else
188 d = 0;
189 #endif
193 MetaDataWidget::~MetaDataWidget()
195 #ifdef HAVE_NEPOMUK
196 delete d->loadFilesThread;
197 #endif
198 delete d;
202 void MetaDataWidget::setFile(const KUrl& url)
204 kDebug() << url;
205 KUrl::List urls;
206 urls.append( url );
207 setFiles( urls );
210 void MetaDataWidget::setFiles(const KUrl::List& urls)
212 #ifdef HAVE_NEPOMUK
213 d->loadFilesThread->setFiles( urls );
214 d->loadFilesThread->start();
215 #else
216 Q_UNUSED( urls );
217 #endif
221 void MetaDataWidget::slotCommentChanged( const QString& s )
223 #ifdef HAVE_NEPOMUK
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
229 job->start();
230 #else
231 Q_UNUSED( s );
232 #endif
236 void MetaDataWidget::slotRatingChanged(unsigned int rating)
238 #ifdef HAVE_NEPOMUK
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
244 job->start();
245 #else
246 Q_UNUSED( rating );
247 #endif
251 void MetaDataWidget::metadataUpdateDone()
253 setEnabled( true );
257 bool MetaDataWidget::eventFilter(QObject* obj, QEvent* event)
259 return QWidget::eventFilter(obj, event);
263 void MetaDataWidget::slotTagClicked( const Nepomuk::Tag& tag )
265 Q_UNUSED( tag );
266 #ifdef HAVE_NEPOMUK
267 d->tagWidget->showTagPopup( QCursor::pos() );
268 #endif
271 void MetaDataWidget::slotLoadingFinished()
273 #ifdef HAVE_NEPOMUK
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 );
278 #endif
281 #include "metadatawidget.moc"