]> cloud.milkyroute.net Git - dolphin.git/blob - src/metadatawidget.cpp
consider the protocol and directory capabilities for file actions like Rename, Delete...
[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 <QtGui/QLabel>
32 #include <QtGui/QGridLayout>
33 #include <QtGui/QTextEdit>
34
35 #ifdef HAVE_NEPOMUK
36 #include "nepomukmassupdatejob.h"
37 #include <nepomuk/kmetadatatagwidget.h>
38 #include <nepomuk/resourcemanager.h>
39 #include <nepomuk/resource.h>
40 #include <nepomuk/variant.h>
41 #include <nepomuk/kratingwidget.h>
42 #include <Soprano/Vocabulary/Xesam>
43 #include "tagcloud/resourcetaggingwidget.h"
44 #endif
45
46
47 bool MetaDataWidget::metaDataAvailable()
48 {
49 #ifdef HAVE_NEPOMUK
50 return !Nepomuk::ResourceManager::instance()->init();
51 #else
52 return false;
53 #endif
54 }
55
56
57 class MetaDataWidget::Private
58 {
59 public:
60 #ifdef HAVE_NEPOMUK
61 void loadComment(const QString& comment);
62
63 QMap<KUrl, Nepomuk::Resource> files;
64
65 CommentWidget* editComment;
66 KRatingWidget* ratingWidget;
67 Nepomuk::ResourceTaggingWidget* tagWidget;
68 #endif
69 };
70
71 #ifdef HAVE_NEPOMUK
72 void MetaDataWidget::Private::loadComment(const QString& comment)
73 {
74 editComment->setComment( comment );
75 }
76 #endif
77
78
79 MetaDataWidget::MetaDataWidget(QWidget* parent) :
80 QWidget(parent)
81 {
82 #ifdef HAVE_NEPOMUK
83 d = new Private;
84 d->editComment = new CommentWidget(this);
85 d->editComment->setFocusPolicy(Qt::ClickFocus);
86 d->ratingWidget = new KRatingWidget(this);
87 d->ratingWidget->setAlignment( Qt::AlignCenter );
88 d->tagWidget = new Nepomuk::ResourceTaggingWidget(this);
89 connect(d->ratingWidget, SIGNAL(ratingChanged(unsigned int)), this, SLOT(slotRatingChanged(unsigned int)));
90 connect(d->editComment, SIGNAL(commentChanged(const QString&)), this, SLOT(slotCommentChanged(const QString&)));
91 connect( d->tagWidget, SIGNAL( tagClicked( const Nepomuk::Tag& ) ), this, SLOT( slotTagClicked( const Nepomuk::Tag& ) ) );
92
93 QVBoxLayout* lay = new QVBoxLayout(this);
94 lay->setMargin(0);
95 lay->addWidget(d->ratingWidget);
96 lay->addWidget(d->editComment);
97 QHBoxLayout* hbox = new QHBoxLayout;
98 lay->addWidget( d->tagWidget );
99 #else
100 d = 0;
101 #endif
102 }
103
104
105 MetaDataWidget::~MetaDataWidget()
106 {
107 delete d;
108 }
109
110
111 void MetaDataWidget::setFile(const KUrl& url)
112 {
113 kDebug() << url;
114 KUrl::List urls;
115 urls.append( url );
116 setFiles( urls );
117 }
118
119
120 void MetaDataWidget::setFiles(const KUrl::List& urls)
121 {
122 #ifdef HAVE_NEPOMUK
123 d->files.clear();
124 bool first = true;
125 QList<Nepomuk::Resource> fileRes;
126 Q_FOREACH( KUrl url, urls ) {
127 Nepomuk::Resource file( url, Soprano::Vocabulary::Xesam::File() );
128 d->files.insert( url, file );
129 fileRes.append( file );
130
131 if ( !first &&
132 d->ratingWidget->rating() != file.rating() ) {
133 d->ratingWidget->setRating( 0 ); // reset rating
134 }
135 else if ( first ) {
136 d->ratingWidget->setRating( (qint32)(file.rating()) );
137 }
138
139 if ( !first &&
140 d->editComment->comment() != file.description() ) {
141 d->loadComment( QString() );
142 }
143 else if ( first ) {
144 d->loadComment( file.description() );
145 }
146 first = false;
147 }
148 d->tagWidget->setResources( fileRes );
149 #endif
150 }
151
152
153 void MetaDataWidget::slotCommentChanged( const QString& s )
154 {
155 #ifdef HAVE_NEPOMUK
156 Nepomuk::MassUpdateJob* job = Nepomuk::MassUpdateJob::commentResources( d->files.values(), s );
157 connect( job, SIGNAL( result( KJob* ) ),
158 this, SLOT( metadataUpdateDone() ) );
159 setEnabled( false ); // no updates during execution
160 job->start();
161 #endif
162 }
163
164
165 void MetaDataWidget::slotRatingChanged(unsigned int rating)
166 {
167 #ifdef HAVE_NEPOMUK
168 Nepomuk::MassUpdateJob* job = Nepomuk::MassUpdateJob::rateResources( d->files.values(), rating );
169 connect( job, SIGNAL( result( KJob* ) ),
170 this, SLOT( metadataUpdateDone() ) );
171 setEnabled( false ); // no updates during execution
172 job->start();
173 #endif
174 }
175
176
177 void MetaDataWidget::metadataUpdateDone()
178 {
179 setEnabled( true );
180 }
181
182
183 bool MetaDataWidget::eventFilter(QObject* obj, QEvent* event)
184 {
185 return QWidget::eventFilter(obj, event);
186 }
187
188
189 void MetaDataWidget::slotTagClicked( const Nepomuk::Tag& tag )
190 {
191 // FIXME
192 #ifdef HAVE_NEPOMUK
193 KMessageBox::information( this, "FIXME: connect me to the dolphinmodel: tags:/" + tag.genericLabel() );
194 #endif
195 }
196
197 #include "metadatawidget.moc"