]> cloud.milkyroute.net Git - dolphin.git/blob - src/metadatawidget.cpp
assure that the zoom slider/capacity bar gets hidden correctly if the width is too...
[dolphin.git] / src / metadatawidget.cpp
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 ***************************************************************************/
19
20 #include "metadatawidget.h"
21
22 #include "commentwidget.h"
23
24 #include <config-nepomuk.h>
25
26 #include <klocale.h>
27 #include <KDebug>
28 #include <KMessageBox>
29
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>
37
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 "tagcloud/resourcetaggingwidget.h"
47 #endif
48
49
50 bool MetaDataWidget::metaDataAvailable()
51 {
52 #ifdef HAVE_NEPOMUK
53 return !Nepomuk::ResourceManager::instance()->init();
54 #else
55 return false;
56 #endif
57 }
58
59
60 class MetaDataWidget::Private
61 {
62 public:
63 #ifdef HAVE_NEPOMUK
64 void loadComment(const QString& comment);
65
66 CommentWidget* editComment;
67 KRatingWidget* ratingWidget;
68 Nepomuk::ResourceTaggingWidget* tagWidget;
69
70 // shared data between the GUI-thread and
71 // the loader-thread (see LoadFilesThread):
72 QMutex mutex;
73 struct SharedData
74 {
75 int rating;
76 QString comment;
77 QList<Nepomuk::Resource> fileRes;
78 QMap<KUrl, Nepomuk::Resource> files;
79 } sharedData;
80
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.
85 */
86 class LoadFilesThread : public QThread
87 {
88 public:
89 LoadFilesThread(SharedData* sharedData, QMutex* mutex);
90 void setFiles(const KUrl::List& urls);
91 virtual void run();
92
93 private:
94 SharedData* m_sharedData;
95 QMutex* m_mutex;
96 KUrl::List m_urls;
97 };
98
99 LoadFilesThread* loadFilesThread;
100 #endif
101 };
102
103 #ifdef HAVE_NEPOMUK
104 void MetaDataWidget::Private::loadComment(const QString& comment)
105 {
106 editComment->setComment( comment );
107 }
108 #endif
109
110 MetaDataWidget::Private::LoadFilesThread::LoadFilesThread(
111 MetaDataWidget::Private::SharedData* sharedData,
112 QMutex* mutex) :
113 m_sharedData(sharedData),
114 m_mutex(mutex),
115 m_urls()
116 {
117 }
118
119 void MetaDataWidget::Private::LoadFilesThread::setFiles(const KUrl::List& urls)
120 {
121 QMutexLocker locker( m_mutex );
122 m_urls = urls;
123 }
124
125 void MetaDataWidget::Private::LoadFilesThread::run()
126 {
127 QMutexLocker locker( m_mutex );
128 const KUrl::List urls = m_urls;
129 locker.unlock();
130
131 bool first = true;
132 QList<Nepomuk::Resource> fileRes;
133 QMap<KUrl, Nepomuk::Resource> files;
134 unsigned int rating = 0;
135 QString comment;
136 Q_FOREACH( const KUrl &url, urls ) {
137 Nepomuk::Resource file( url, Soprano::Vocabulary::Xesam::File() );
138 files.insert( url, file );
139 fileRes.append( file );
140
141 if ( !first && rating != file.rating() ) {
142 rating = 0; // reset rating
143 }
144 else if ( first ) {
145 rating = file.rating();
146 }
147
148 if ( !first && comment != file.description() ) {
149 comment.clear();
150 }
151 else if ( first ) {
152 comment = file.description();
153 }
154 first = false;
155 }
156
157 locker.relock();
158 m_sharedData->rating = rating;
159 m_sharedData->comment = comment;
160 m_sharedData->fileRes = fileRes;
161 m_sharedData->files = files;
162 }
163
164 MetaDataWidget::MetaDataWidget(QWidget* parent) :
165 QWidget(parent)
166 {
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& ) ) );
177
178 d->sharedData.rating = 0;
179 d->loadFilesThread = new Private::LoadFilesThread(&d->sharedData, &d->mutex);
180 connect(d->loadFilesThread, SIGNAL(finished()), this, SLOT(slotLoadingFinished()));
181
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
190 }
191
192
193 MetaDataWidget::~MetaDataWidget()
194 {
195 delete d->loadFilesThread;
196 delete d;
197 }
198
199
200 void MetaDataWidget::setFile(const KUrl& url)
201 {
202 kDebug() << url;
203 KUrl::List urls;
204 urls.append( url );
205 setFiles( urls );
206 }
207
208 void MetaDataWidget::setFiles(const KUrl::List& urls)
209 {
210 #ifdef HAVE_NEPOMUK
211 d->loadFilesThread->setFiles( urls );
212 d->loadFilesThread->start();
213 #endif
214 }
215
216
217 void MetaDataWidget::slotCommentChanged( const QString& s )
218 {
219 #ifdef HAVE_NEPOMUK
220 QMutexLocker locker( &d->mutex );
221 Nepomuk::MassUpdateJob* job = Nepomuk::MassUpdateJob::commentResources( d->sharedData.files.values(), s );
222 connect( job, SIGNAL( result( KJob* ) ),
223 this, SLOT( metadataUpdateDone() ) );
224 setEnabled( false ); // no updates during execution
225 job->start();
226 #endif
227 }
228
229
230 void MetaDataWidget::slotRatingChanged(unsigned int rating)
231 {
232 #ifdef HAVE_NEPOMUK
233 QMutexLocker locker( &d->mutex );
234 Nepomuk::MassUpdateJob* job = Nepomuk::MassUpdateJob::rateResources( d->sharedData.files.values(), rating );
235 connect( job, SIGNAL( result( KJob* ) ),
236 this, SLOT( metadataUpdateDone() ) );
237 setEnabled( false ); // no updates during execution
238 job->start();
239 #endif
240 }
241
242
243 void MetaDataWidget::metadataUpdateDone()
244 {
245 setEnabled( true );
246 }
247
248
249 bool MetaDataWidget::eventFilter(QObject* obj, QEvent* event)
250 {
251 return QWidget::eventFilter(obj, event);
252 }
253
254
255 void MetaDataWidget::slotTagClicked( const Nepomuk::Tag& tag )
256 {
257 Q_UNUSED( tag );
258 #ifdef HAVE_NEPOMUK
259 d->tagWidget->showTagPopup( QCursor::pos() );
260 #endif
261 }
262
263 void MetaDataWidget::slotLoadingFinished()
264 {
265 QMutexLocker locker( &d->mutex );
266 d->ratingWidget->setRating( d->sharedData.rating );
267 d->loadComment( d->sharedData.comment );
268 d->tagWidget->setResources( d->sharedData.fileRes );
269 }
270
271 #include "metadatawidget.moc"