~osomon/unity-2d/drag-from-dash

« back to all changes in this revision

Viewing changes to libunity-2d-private/Unity2d/dragitemwithurl.cpp

  • Committer: Olivier Tilloy
  • Date: 2011-05-18 15:47:16 UTC
  • Revision ID: olivier.tilloy@canonical.com-20110518154716-nzzmh153m0wew002
Made DragItem a base class with empty mime data by default.
DragItemWithUrl subclasses it and defines a 'url' property that knows how to convert application:// URLs into the corresponding file:// URLs.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2011 Canonical, Ltd.
 
3
 *
 
4
 * Authors:
 
5
 *  Olivier Tilloy <olivier.tilloy@canonical.com>
 
6
 *
 
7
 * This program is free software; you can redistribute it and/or modify
 
8
 * it under the terms of the GNU General Public License as published by
 
9
 * the Free Software Foundation; version 3.
 
10
 *
 
11
 * This program is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
 */
 
19
 
 
20
#include "dragitemwithurl.h"
 
21
 
 
22
// libunity-2d
 
23
#include <gscopedpointer.h>
 
24
 
 
25
// GIO
 
26
#undef signals
 
27
extern "C" {
 
28
    #include <gio/gdesktopappinfo.h>
 
29
}
 
30
 
 
31
// Qt
 
32
#include <QMimeData>
 
33
 
 
34
DeclarativeDragItemWithUrl::DeclarativeDragItemWithUrl(QDeclarativeItem* parent)
 
35
    : DeclarativeDragItem(parent)
 
36
{
 
37
}
 
38
 
 
39
DeclarativeDragItemWithUrl::~DeclarativeDragItemWithUrl()
 
40
{
 
41
}
 
42
 
 
43
const QString& DeclarativeDragItemWithUrl::url() const
 
44
{
 
45
    return m_url;
 
46
}
 
47
 
 
48
void DeclarativeDragItemWithUrl::setUrl(const QString& url)
 
49
{
 
50
    if (url != m_url) {
 
51
        m_url = url;
 
52
        Q_EMIT urlChanged(m_url);
 
53
    }
 
54
}
 
55
 
 
56
QMimeData* DeclarativeDragItemWithUrl::mimeData() const
 
57
{
 
58
    QMimeData* data = new QMimeData;
 
59
    if (!m_url.isEmpty()) {
 
60
        QList<QUrl> urls;
 
61
        urls.append(decodeUri(m_url));
 
62
        data->setUrls(urls);
 
63
    }
 
64
    return data;
 
65
}
 
66
 
 
67
QUrl DeclarativeDragItemWithUrl::decodeUri(const QString& uri)
 
68
{
 
69
    if (uri.startsWith("application://")) {
 
70
        QString desktopFileName = uri.mid(14);
 
71
        QByteArray bytes = desktopFileName.toUtf8();
 
72
        GObjectScopedPointer<GDesktopAppInfo> appInfo(g_desktop_app_info_new(bytes.constData()));
 
73
        if (appInfo.isNull()) {
 
74
            return QUrl(uri);
 
75
        }
 
76
        QString filePath = QString::fromUtf8(g_desktop_app_info_get_filename(appInfo.data()));
 
77
        return QUrl("file://" + filePath);
 
78
    } else {
 
79
        return QUrl(uri);
 
80
    }
 
81
}
 
82