~ubuntu-branches/ubuntu/precise/gwenview/precise-proposed

« back to all changes in this revision

Viewing changes to lib/urlutils.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2011-12-15 14:17:54 UTC
  • mto: This revision was merged to the branch mainline in revision 12.
  • Revision ID: package-import@ubuntu.com-20111215141754-z043hyx69dulbggf
Tags: upstream-4.7.90
Import upstream version 4.7.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// vim: set tabstop=4 shiftwidth=4 noexpandtab:
 
1
// vim: set tabstop=4 shiftwidth=4 expandtab:
2
2
/*
3
3
Gwenview: an image viewer
4
4
Copyright 2008 Aurélien Gâteau <agateau@kde.org>
37
37
#include <archiveutils.h>
38
38
#include <mimetypeutils.h>
39
39
 
40
 
namespace Gwenview {
41
 
 
42
 
namespace UrlUtils {
43
 
 
44
 
bool urlIsFastLocalFile(const KUrl& url) {
45
 
        if (!url.isLocalFile()) {
46
 
                return false;
47
 
        }
48
 
 
49
 
        KMountPoint::List list = KMountPoint::currentMountPoints();
50
 
        KMountPoint::Ptr mountPoint = list.findByPath(url.toLocalFile());
51
 
        if (!mountPoint) {
52
 
                // We couldn't find a mount point for the url. We are probably in a
53
 
                // chroot. Assume everything is fast then.
54
 
                return true;
55
 
        }
56
 
 
57
 
        return !mountPoint->probablySlow();
58
 
}
59
 
 
60
 
 
61
 
bool urlIsDirectory(const KUrl& url) {
62
 
        if( url.fileName(KUrl::ObeyTrailingSlash).isEmpty()) {
63
 
                return true; // file:/somewhere/<nothing here>
64
 
        }
65
 
 
66
 
        // Do direct stat instead of using KIO if the file is local (faster)
67
 
        if (UrlUtils::urlIsFastLocalFile(url)) {
68
 
                KDE_struct_stat buff;
69
 
                if ( KDE_stat( QFile::encodeName(url.toLocalFile()), &buff ) == 0 )  {
70
 
                        return S_ISDIR( buff.st_mode );
71
 
                }
72
 
        }
73
 
 
74
 
        QWidgetList list = QApplication::topLevelWidgets();
75
 
        QWidget* parent;
76
 
        if (list.count() > 0) {
77
 
                parent = list[0];
78
 
        } else {
79
 
                parent = 0;
80
 
        }
81
 
        KIO::UDSEntry entry;
82
 
        if( KIO::NetAccess::stat( url, entry, parent)) {
83
 
                return entry.isDir();
84
 
        }
85
 
        return false;
86
 
}
87
 
 
88
 
 
89
 
KUrl fixUserEnteredUrl(const KUrl& in) {
90
 
        if (!in.isRelative() && !in.isLocalFile()) {
91
 
                return in;
92
 
        }
93
 
 
94
 
        QFileInfo info(in.toLocalFile());
95
 
        QString path = info.absoluteFilePath();
96
 
 
97
 
        KUrl out = KUrl::fromPath(path);
98
 
        QString mimeType = MimeTypeUtils::urlMimeType(out);
99
 
 
100
 
        const QString protocol = ArchiveUtils::protocolForMimeType(mimeType);
101
 
        if (!protocol.isEmpty()) {
102
 
                out.setProtocol(protocol);
103
 
        }
104
 
        return out;
105
 
}
106
 
 
 
40
namespace Gwenview
 
41
{
 
42
 
 
43
namespace UrlUtils
 
44
{
 
45
 
 
46
bool urlIsFastLocalFile(const KUrl& url)
 
47
{
 
48
    if (!url.isLocalFile()) {
 
49
        return false;
 
50
    }
 
51
 
 
52
    KMountPoint::List list = KMountPoint::currentMountPoints();
 
53
    KMountPoint::Ptr mountPoint = list.findByPath(url.toLocalFile());
 
54
    if (!mountPoint) {
 
55
        // We couldn't find a mount point for the url. We are probably in a
 
56
        // chroot. Assume everything is fast then.
 
57
        return true;
 
58
    }
 
59
 
 
60
    return !mountPoint->probablySlow();
 
61
}
 
62
 
 
63
bool urlIsDirectory(const KUrl& url)
 
64
{
 
65
    if (url.fileName(KUrl::ObeyTrailingSlash).isEmpty()) {
 
66
        return true; // file:/somewhere/<nothing here>
 
67
    }
 
68
 
 
69
    // Do direct stat instead of using KIO if the file is local (faster)
 
70
    if (UrlUtils::urlIsFastLocalFile(url)) {
 
71
        KDE_struct_stat buff;
 
72
        if (KDE_stat(QFile::encodeName(url.toLocalFile()), &buff) == 0)  {
 
73
            return S_ISDIR(buff.st_mode);
 
74
        }
 
75
    }
 
76
 
 
77
    QWidgetList list = QApplication::topLevelWidgets();
 
78
    QWidget* parent;
 
79
    if (list.count() > 0) {
 
80
        parent = list[0];
 
81
    } else {
 
82
        parent = 0;
 
83
    }
 
84
    KIO::UDSEntry entry;
 
85
    if (KIO::NetAccess::stat(url, entry, parent)) {
 
86
        return entry.isDir();
 
87
    }
 
88
    return false;
 
89
}
 
90
 
 
91
KUrl fixUserEnteredUrl(const KUrl& in)
 
92
{
 
93
    if (!in.isRelative() && !in.isLocalFile()) {
 
94
        return in;
 
95
    }
 
96
 
 
97
    QFileInfo info(in.toLocalFile());
 
98
    QString path = info.absoluteFilePath();
 
99
 
 
100
    KUrl out = KUrl::fromPath(path);
 
101
    QString mimeType = MimeTypeUtils::urlMimeType(out);
 
102
 
 
103
    const QString protocol = ArchiveUtils::protocolForMimeType(mimeType);
 
104
    if (!protocol.isEmpty()) {
 
105
        out.setProtocol(protocol);
 
106
    }
 
107
    return out;
 
108
}
107
109
 
108
110
} // namespace
109
111