2 * Copyright 2008 David Edmundson <kde@davidedmundson.co.uk>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Library General Public License as
6 * published by the Free Software Foundation; either version 2, or
7 * (at your option) any later version.
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
14 * You should have received a copy of the GNU Library General Public
15 * License 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.
20 #include "placesrunner.h"
26 PlacesRunner::PlacesRunner(QObject
* parent
, const QVariantList
&args
)
27 : Plasma::AbstractRunner(parent
, args
)
30 setObjectName("Places");
31 m_filePlaces
= new KFilePlacesModel(this);
32 connect(m_filePlaces
, SIGNAL(setupDone(QModelIndex
, bool)), SLOT(setupComplete(QModelIndex
, bool)));
35 PlacesRunner::~PlacesRunner()
39 void PlacesRunner::match(Plasma::RunnerContext
&context
)
41 const QString term
= context
.query();
42 QList
<Plasma::QueryMatch
> matches
;
44 if (term
.length() < 3)
47 for (int i
= 0; i
<= m_filePlaces
->rowCount();i
++) {
48 QModelIndex current_index
= m_filePlaces
->index(i
, 0);
49 Plasma::QueryMatch::Type type
= Plasma::QueryMatch::NoMatch
;
52 if (m_filePlaces
->text(current_index
).toLower() == term
.toLower()) {
53 type
= Plasma::QueryMatch::ExactMatch
;
55 } else if (m_filePlaces
->text(current_index
).contains(term
, Qt::CaseInsensitive
)) {
56 type
= Plasma::QueryMatch::PossibleMatch
;
60 if (type
!= Plasma::QueryMatch::NoMatch
) {
61 Plasma::QueryMatch
match(this);
63 match
.setRelevance(relevance
);
64 match
.setIcon(KIcon(m_filePlaces
->icon(current_index
)));
65 match
.setText(m_filePlaces
->text(current_index
));
67 //if we have to mount it set the device udi instead of the URL, as we can't open it directly
68 if (m_filePlaces
->isDevice(current_index
) && m_filePlaces
->setupNeeded(current_index
)) {
69 match
.setData(m_filePlaces
->deviceForIndex(current_index
).udi());
71 match
.setData(m_filePlaces
->url(current_index
));
76 context
.addMatches(term
, matches
);
80 void PlacesRunner::run(const Plasma::RunnerContext
&context
, const Plasma::QueryMatch
&action
)
83 //I don't just pass the model index because the list could change before the user clicks on it, which would make everything go wrong. Ideally we don't want things to go wrong.
84 if (action
.data().canConvert
<KUrl
>()) {
85 new KRun(action
.data().value
<KUrl
>().url(), 0);
86 } else if (action
.data().canConvert
<QString
>()) {
87 //search our list for the device with the same udi, then set it up (mount it).
88 QString deviceUdi
= action
.data().toString();
90 for (int i
= 0; i
<= m_filePlaces
->rowCount();i
++) {
91 QModelIndex current_index
= m_filePlaces
->index(i
, 0);
92 if (m_filePlaces
->isDevice(current_index
) && m_filePlaces
->deviceForIndex(current_index
).udi() == deviceUdi
) {
93 m_filePlaces
->requestSetup(current_index
);
100 //if a device needed mounting, this slot gets called when it's finished.
101 void PlacesRunner::setupComplete(QModelIndex index
, bool success
)
104 new KRun(m_filePlaces
->url(index
), 0);
108 #include "placesrunner.moc"