]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/information/nepomukmassupdatejob.cpp
Group classes into folders, Dolphin is too big in the meantime for having a flat...
[dolphin.git] / src / panels / information / nepomukmassupdatejob.cpp
1 /***************************************************************************
2 * Copyright (C) 2008 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 "nepomukmassupdatejob.h"
21
22 #include <klocale.h>
23 #include <kdebug.h>
24
25 #include <nepomuk/tag.h>
26 #include <nepomuk/tools.h>
27
28
29 Nepomuk::MassUpdateJob::MassUpdateJob( QObject* parent )
30 : KJob( parent ),
31 m_index( -1 )
32 {
33 kDebug();
34 setCapabilities( Killable|Suspendable );
35 connect( &m_processTimer, SIGNAL( timeout() ),
36 this, SLOT( slotNext() ) );
37 }
38
39
40 Nepomuk::MassUpdateJob::~MassUpdateJob()
41 {
42 kDebug();
43 }
44
45
46 void Nepomuk::MassUpdateJob::setFiles( const KUrl::List& urls )
47 {
48 m_resources.clear();
49 foreach( const KUrl &url, urls ) {
50 m_resources.append( Resource( url ) );
51 }
52 setTotalAmount( KJob::Files, m_resources.count() );
53 }
54
55
56 void Nepomuk::MassUpdateJob::setResources( const QList<Nepomuk::Resource>& rl )
57 {
58 m_resources = rl;
59 setTotalAmount( KJob::Files, m_resources.count() );
60 }
61
62
63 void Nepomuk::MassUpdateJob::setProperties( const QList<QPair<QUrl,Nepomuk::Variant> >& props )
64 {
65 m_properties = props;
66 }
67
68
69 void Nepomuk::MassUpdateJob::start()
70 {
71 if ( m_index < 0 ) {
72 kDebug();
73 emit description( this,
74 i18nc("@info:progress", "Changing annotations") );
75 m_index = 0;
76 m_processTimer.start();
77 }
78 else {
79 kDebug() << "Job has already been started";
80 }
81 }
82
83
84 bool Nepomuk::MassUpdateJob::doKill()
85 {
86 if ( m_index > 0 ) {
87 m_processTimer.stop();
88 m_index = -1;
89 return true;
90 }
91 else {
92 return false;
93 }
94 }
95
96
97 bool Nepomuk::MassUpdateJob::doSuspend()
98 {
99 m_processTimer.stop();
100 return true;
101 }
102
103
104 bool Nepomuk::MassUpdateJob::doResume()
105 {
106 if ( m_index > 0 ) {
107 m_processTimer.start();
108 return true;
109 }
110 else {
111 return false;
112 }
113 }
114
115
116 void Nepomuk::MassUpdateJob::slotNext()
117 {
118 if ( !isSuspended() ) {
119 if ( m_index < m_resources.count() ) {
120 Nepomuk::Resource& res = m_resources[m_index];
121 for ( int i = 0; i < m_properties.count(); ++i ) {
122 res.setProperty( m_properties[i].first, m_properties[i].second );
123 }
124 ++m_index;
125 setProcessedAmount( KJob::Files, m_index );
126 }
127 else if ( m_index >= m_resources.count() ) {
128 kDebug() << "done";
129 m_index = -1;
130 m_processTimer.stop();
131 emitResult();
132 }
133 }
134 }
135
136
137 Nepomuk::MassUpdateJob* Nepomuk::MassUpdateJob::tagResources( const QList<Nepomuk::Resource>& rl, const QList<Nepomuk::Tag>& tags )
138 {
139 Nepomuk::MassUpdateJob* job = new Nepomuk::MassUpdateJob();
140 job->setResources( rl );
141 job->setProperties( QList<QPair<QUrl,Nepomuk::Variant> >() << qMakePair( QUrl( Nepomuk::Resource::tagUri() ), Nepomuk::Variant( convertResourceList<Tag>( tags ) ) ) );
142 return job;
143 }
144
145
146 Nepomuk::MassUpdateJob* Nepomuk::MassUpdateJob::rateResources( const QList<Nepomuk::Resource>& rl, int rating )
147 {
148 Nepomuk::MassUpdateJob* job = new Nepomuk::MassUpdateJob();
149 job->setResources( rl );
150 job->setProperties( QList<QPair<QUrl,Nepomuk::Variant> >() << qMakePair( QUrl( Nepomuk::Resource::ratingUri() ), Nepomuk::Variant( rating ) ) );
151 return job;
152 }
153
154
155 Nepomuk::MassUpdateJob* Nepomuk::MassUpdateJob::commentResources( const QList<Nepomuk::Resource>& rl, const QString& comment )
156 {
157 Nepomuk::MassUpdateJob* job = new Nepomuk::MassUpdateJob();
158 job->setResources( rl );
159 job->setProperties( QList<QPair<QUrl,Nepomuk::Variant> >() << qMakePair( QUrl( Nepomuk::Resource::descriptionUri() ), Nepomuk::Variant( comment ) ) );
160 return job;
161 }
162
163 #include "nepomukmassupdatejob.moc"