/* * This file Copyright (C) 2009-2014 Mnemosyne LLC * * It may be used under the GNU GPL versions 2 or 3 * or any future license endorsed by Mnemosyne LLC. * * $Id$ */ #include #ifdef _WIN32 #include #include #endif #include #include #include #include #include #include #include #include #include #include #if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0) #include #include #endif #include #include // tr_formatter #include "utils.h" /*** **** ***/ #if defined(_WIN32) && QT_VERSION >= QT_VERSION_CHECK(5, 0, 0) // Should be in QtWinExtras soon, but for now let's import it manually extern QPixmap qt_pixmapFromWinHICON(HICON icon); #endif QString Utils::remoteFileChooser (QWidget * parent, const QString& title, const QString& myPath, bool dir, bool local) { QString path; if (local) { if (dir) path = QFileDialog::getExistingDirectory (parent, title, myPath); else path = QFileDialog::getOpenFileName (parent, title, myPath); } else { path = QInputDialog::getText (parent, title, tr ("Enter a location:"), QLineEdit::Normal, myPath, NULL); } return path; } void Utils::toStderr (const QString& str) { std::cerr << qPrintable(str) << std::endl; } #ifdef _WIN32 namespace { void addAssociatedFileIcon (const QFileInfo& fileInfo, UINT iconSize, QIcon& icon) { QString const pixmapCacheKey = QLatin1String ("tr_file_ext_") + QString::number (iconSize) + "_" + fileInfo.suffix (); QPixmap pixmap; if (!QPixmapCache::find (pixmapCacheKey, &pixmap)) { const QString filename = fileInfo.fileName (); SHFILEINFO shellFileInfo; if (::SHGetFileInfoW (reinterpret_cast (filename.utf16 ()), FILE_ATTRIBUTE_NORMAL, &shellFileInfo, sizeof(shellFileInfo), SHGFI_ICON | iconSize | SHGFI_USEFILEATTRIBUTES) != 0) { if (shellFileInfo.hIcon != NULL) { #if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0) pixmap = qt_pixmapFromWinHICON (shellFileInfo.hIcon); #else pixmap = QPixmap::fromWinHICON (shellFileInfo.hIcon); #endif ::DestroyIcon (shellFileInfo.hIcon); } } QPixmapCache::insert (pixmapCacheKey, pixmap); } if (!pixmap.isNull ()) icon.addPixmap (pixmap); } } // namespace #endif #include QIcon Utils::guessMimeIcon (const QString& filename) { static const QIcon fallback = qApp->style ()->standardIcon (QStyle::SP_FileIcon); #ifdef _WIN32 QIcon icon; if (!filename.isEmpty ()) { const QFileInfo fileInfo (filename); addAssociatedFileIcon (fileInfo, SHGFI_SMALLICON, icon); addAssociatedFileIcon (fileInfo, 0, icon); addAssociatedFileIcon (fileInfo, SHGFI_LARGEICON, icon); } if (!icon.isNull ()) return icon; #elif QT_VERSION >= QT_VERSION_CHECK(5, 0, 0) QMimeDatabase mimeDb; QMimeType mimeType = mimeDb.mimeTypeForFile (filename, QMimeDatabase::MatchExtension); if (mimeType.isValid ()) return QIcon::fromTheme (mimeType.iconName (), QIcon::fromTheme (mimeType.genericIconName (), fallback)); #else enum { DISK, DOCUMENT, PICTURE, VIDEO, ARCHIVE, AUDIO, APP, TYPE_COUNT }; static QIcon fileIcons[TYPE_COUNT]; static QSet suffixes[TYPE_COUNT]; if (fileIcons[0].isNull ()) { suffixes[DISK] << QString::fromLatin1("iso"); fileIcons[DISK]= QIcon::fromTheme (QString::fromLatin1("media-optical"), fallback); const char * doc_types[] = { "abw", "csv", "doc", "dvi", "htm", "html", "ini", "log", "odp", "ods", "odt", "pdf", "ppt", "ps", "rtf", "tex", "txt", "xml" }; for (int i=0, n=sizeof(doc_types)/sizeof(doc_types[0]); i