| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2020 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the QtGui module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU Lesser General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | #include "qabstractfileiconprovider.h" |
| 41 | |
| 42 | #include <qguiapplication.h> |
| 43 | #include <qicon.h> |
| 44 | #include <qmimedatabase.h> |
| 45 | |
| 46 | |
| 47 | #include <private/qabstractfileiconprovider_p.h> |
| 48 | #include <private/qfilesystementry_p.h> |
| 49 | |
| 50 | QT_BEGIN_NAMESPACE |
| 51 | |
| 52 | QAbstractFileIconProviderPrivate::QAbstractFileIconProviderPrivate(QAbstractFileIconProvider *q) |
| 53 | : q_ptr(q) |
| 54 | {} |
| 55 | |
| 56 | QAbstractFileIconProviderPrivate::~QAbstractFileIconProviderPrivate() = default; |
| 57 | |
| 58 | /*! |
| 59 | \class QAbstractFileIconProvider |
| 60 | |
| 61 | \inmodule QtGui |
| 62 | \since 6.0 |
| 63 | |
| 64 | \brief The QAbstractFileIconProvider class provides file icons for the QFileSystemModel class. |
| 65 | */ |
| 66 | |
| 67 | /*! |
| 68 | \enum QAbstractFileIconProvider::IconType |
| 69 | |
| 70 | \value Computer The icon used for the computing device as a whole |
| 71 | \value Desktop The icon for the special "Desktop" directory of the user |
| 72 | \value Trashcan The icon for the user's "Trash" place in the desktop's file manager |
| 73 | \value Network The icon for the “Network Servers” place in the desktop's file manager, |
| 74 | and workgroups within the network |
| 75 | \value Drive The icon used for disk drives |
| 76 | \value Folder The standard folder icon used to represent directories on local filesystems |
| 77 | \value File The icon used for generic text file types |
| 78 | */ |
| 79 | |
| 80 | /*! |
| 81 | \enum QAbstractFileIconProvider::Option |
| 82 | |
| 83 | \value DontUseCustomDirectoryIcons Always use the default directory icon. |
| 84 | Some platforms allow the user to set a different icon. Custom icon lookup |
| 85 | cause a big performance impact over network or removable drives. |
| 86 | */ |
| 87 | |
| 88 | /*! |
| 89 | Constructs a file icon provider. |
| 90 | */ |
| 91 | QAbstractFileIconProvider::QAbstractFileIconProvider() |
| 92 | : d_ptr(new QAbstractFileIconProviderPrivate(this)) |
| 93 | { |
| 94 | } |
| 95 | |
| 96 | /*! |
| 97 | \internal |
| 98 | */ |
| 99 | QAbstractFileIconProvider::QAbstractFileIconProvider(QAbstractFileIconProviderPrivate &dd) |
| 100 | : d_ptr(&dd) |
| 101 | {} |
| 102 | |
| 103 | /*! |
| 104 | Destroys the file icon provider. |
| 105 | */ |
| 106 | |
| 107 | QAbstractFileIconProvider::~QAbstractFileIconProvider() = default; |
| 108 | |
| 109 | |
| 110 | /*! |
| 111 | Sets \a options that affect the icon provider. |
| 112 | \sa options() |
| 113 | */ |
| 114 | |
| 115 | void QAbstractFileIconProvider::setOptions(QAbstractFileIconProvider::Options options) |
| 116 | { |
| 117 | Q_D(QAbstractFileIconProvider); |
| 118 | d->options = options; |
| 119 | } |
| 120 | |
| 121 | /*! |
| 122 | Returns all the options that affect the icon provider. |
| 123 | By default, all options are disabled. |
| 124 | \sa setOptions() |
| 125 | */ |
| 126 | |
| 127 | QAbstractFileIconProvider::Options QAbstractFileIconProvider::options() const |
| 128 | { |
| 129 | Q_D(const QAbstractFileIconProvider); |
| 130 | return d->options; |
| 131 | } |
| 132 | |
| 133 | /*! |
| 134 | Returns an icon set for the given \a type, using the current |
| 135 | icon theme. |
| 136 | |
| 137 | \sa QIcon::fromTheme |
| 138 | */ |
| 139 | |
| 140 | QIcon QAbstractFileIconProvider::icon(IconType type) const |
| 141 | { |
| 142 | Q_UNUSED(type); |
| 143 | switch (type) { |
| 144 | case Computer: |
| 145 | return QIcon::fromTheme(QLatin1String("computer" )); |
| 146 | case Desktop: |
| 147 | return QIcon::fromTheme(QLatin1String("user-desktop" )); |
| 148 | case Trashcan: |
| 149 | return QIcon::fromTheme(QLatin1String("user-trash" )); |
| 150 | case Network: |
| 151 | return QIcon::fromTheme(QLatin1String("network-workgroup" )); |
| 152 | case Drive: |
| 153 | return QIcon::fromTheme(QLatin1String("drive-harddisk" )); |
| 154 | case Folder: |
| 155 | return QIcon::fromTheme(QLatin1String("folder" )); |
| 156 | case File: |
| 157 | return QIcon::fromTheme(QLatin1String("text-x-generic" )); |
| 158 | // no default on purpose; we want warnings when the type enum is extended |
| 159 | } |
| 160 | return QIcon::fromTheme(QLatin1String("text-x-generic" )); |
| 161 | } |
| 162 | |
| 163 | /*! |
| 164 | Returns an icon for the file described by \a info, using the |
| 165 | current icon theme. |
| 166 | |
| 167 | \sa QIcon::fromTheme |
| 168 | */ |
| 169 | |
| 170 | QIcon QAbstractFileIconProvider::icon(const QFileInfo &info) const |
| 171 | { |
| 172 | Q_D(const QAbstractFileIconProvider); |
| 173 | if (info.isRoot()) |
| 174 | return icon(Drive); |
| 175 | if (info.isDir()) |
| 176 | return icon(Folder); |
| 177 | return QIcon::fromTheme(d->mimeDatabase.mimeTypeForFile(info).iconName()); |
| 178 | } |
| 179 | |
| 180 | /*! |
| 181 | Returns the type of the file described by \a info. |
| 182 | */ |
| 183 | |
| 184 | QString QAbstractFileIconProvider::type(const QFileInfo &info) const |
| 185 | { |
| 186 | Q_D(const QAbstractFileIconProvider); |
| 187 | if (QFileSystemEntry::isRootPath(info.absoluteFilePath())) |
| 188 | return QGuiApplication::translate("QAbstractFileIconProvider" , "Drive" ); |
| 189 | if (info.isFile()) { |
| 190 | const QMimeType mimeType = d->mimeDatabase.mimeTypeForFile(info); |
| 191 | return mimeType.comment().isEmpty() ? mimeType.name() : mimeType.comment(); |
| 192 | } |
| 193 | |
| 194 | if (info.isDir()) |
| 195 | #ifdef Q_OS_WIN |
| 196 | return QGuiApplication::translate("QAbstractFileIconProvider" , "File Folder" , "Match Windows Explorer" ); |
| 197 | #else |
| 198 | return QGuiApplication::translate("QAbstractFileIconProvider" , "Folder" , "All other platforms" ); |
| 199 | #endif |
| 200 | // Windows - "File Folder" |
| 201 | // macOS - "Folder" |
| 202 | // Konqueror - "Folder" |
| 203 | // Nautilus - "folder" |
| 204 | |
| 205 | if (info.isSymLink()) |
| 206 | #ifdef Q_OS_MACOS |
| 207 | return QGuiApplication::translate("QAbstractFileIconProvider" , "Alias" , "macOS Finder" ); |
| 208 | #else |
| 209 | return QGuiApplication::translate("QAbstractFileIconProvider" , "Shortcut" , "All other platforms" ); |
| 210 | #endif |
| 211 | // macOS - "Alias" |
| 212 | // Windows - "Shortcut" |
| 213 | // Konqueror - "Folder" or "TXT File" i.e. what it is pointing to |
| 214 | // Nautilus - "link to folder" or "link to object file", same as Konqueror |
| 215 | |
| 216 | return QGuiApplication::translate("QAbstractFileIconProvider" , "Unknown" ); |
| 217 | } |
| 218 | |
| 219 | QT_END_NAMESPACE |
| 220 | |