~ubuntu-branches/ubuntu/quantal/kde-runtime/quantal

« back to all changes in this revision

Viewing changes to nepomuk/services/storage/datamanagementcommand.cpp

  • Committer: Package Import Robot
  • Author(s): Philip Muškovac
  • Date: 2012-06-03 21:50:00 UTC
  • mto: This revision was merged to the branch mainline in revision 21.
  • Revision ID: package-import@ubuntu.com-20120603215000-vn7oarsq0ynrydj5
Tags: upstream-4.8.80
Import upstream version 4.8.80

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
   This file is part of the Nepomuk KDE project.
3
 
   Copyright (C) 2010 Sebastian Trueg <trueg@kde.org>
4
 
 
5
 
   This library is free software; you can redistribute it and/or
6
 
   modify it under the terms of the GNU Lesser General Public
7
 
   License as published by the Free Software Foundation; either
8
 
   version 2.1 of the License, or (at your option) version 3, or any
9
 
   later version accepted by the membership of KDE e.V. (or its
10
 
   successor approved by the membership of KDE e.V.), which shall
11
 
   act as a proxy defined in Section 6 of version 3 of the license.
12
 
 
13
 
   This library is distributed in the hope that it will be useful,
14
 
   but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
 
   Lesser General Public License for more details.
17
 
 
18
 
   You should have received a copy of the GNU Lesser General Public
19
 
   License along with this library.  If not, see <http://www.gnu.org/licenses/>.
20
 
*/
21
 
 
22
 
#include "datamanagementcommand.h"
23
 
#include "datamanagementmodel.h"
24
 
 
25
 
#include <Soprano/Error/Error>
26
 
#include <Soprano/Error/ErrorCode>
27
 
 
28
 
#include <QtDBus/QDBusConnection>
29
 
 
30
 
#include <QtCore/QStringList>
31
 
#include <QtCore/QEventLoop>
32
 
 
33
 
#include <KUrl>
34
 
 
35
 
 
36
 
namespace {
37
 
QDBusError::ErrorType convertSopranoErrorCode(int code)
38
 
{
39
 
    switch(code) {
40
 
    case Soprano::Error::ErrorInvalidArgument:
41
 
        return QDBusError::InvalidArgs;
42
 
    default:
43
 
        return QDBusError::Failed;
44
 
    }
45
 
}
46
 
}
47
 
 
48
 
 
49
 
Nepomuk::DataManagementCommand::DataManagementCommand(DataManagementModel* model, const QDBusMessage& msg)
50
 
    : QRunnable(),
51
 
      m_model(model),
52
 
      m_msg(msg)
53
 
{
54
 
}
55
 
 
56
 
Nepomuk::DataManagementCommand::~DataManagementCommand()
57
 
{
58
 
}
59
 
 
60
 
void Nepomuk::DataManagementCommand::run()
61
 
{
62
 
    QVariant result = runCommand();
63
 
    Soprano::Error::Error error = model()->lastError();
64
 
    if(error) {
65
 
        // send error reply
66
 
        QDBusConnection::sessionBus().send(m_msg.createErrorReply(convertSopranoErrorCode(error.code()), error.message()));
67
 
    }
68
 
    else {
69
 
        // encode result (ie. convert QUrl to QString)
70
 
        if(result.isValid()) {
71
 
            if(result.type() == QVariant::Url) {
72
 
                result = encodeUrl(result.toUrl());
73
 
            }
74
 
            QDBusConnection::sessionBus().send(m_msg.createReply(result));
75
 
        }
76
 
        else {
77
 
            QDBusConnection::sessionBus().send(m_msg.createReply());
78
 
        }
79
 
    }
80
 
 
81
 
    //
82
 
    // DBus requires event handling for signals to be emitted properly.
83
 
    // (for example the Soprano statement signals which are emitted a
84
 
    // lot during command execution.)
85
 
    // Otherwise memory will fill up with queued DBus message objects.
86
 
    // Instead of executing an event loop we avoid all the hassle and
87
 
    // simply handle all events here.
88
 
    //
89
 
    QEventLoop loop;
90
 
    loop.processEvents();
91
 
}
92
 
 
93
 
 
94
 
// static
95
 
QUrl Nepomuk::decodeUrl(const QString& urlsString)
96
 
{
97
 
    // we use the power of KUrl to automatically convert file paths to file:/ URLs
98
 
    return KUrl(urlsString);
99
 
}
100
 
 
101
 
// static
102
 
QList<QUrl> Nepomuk::decodeUrls(const QStringList& urlStrings)
103
 
{
104
 
    QList<QUrl> urls;
105
 
    Q_FOREACH(const QString& urlString, urlStrings) {
106
 
        urls << decodeUrl(urlString);
107
 
    }
108
 
    return urls;
109
 
}
110
 
 
111
 
// static
112
 
QString Nepomuk::encodeUrl(const QUrl& url)
113
 
{
114
 
    return QString::fromAscii(url.toEncoded());
115
 
}