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