File indexing completed on 2024-05-05 16:08:28

0001 /*****************************************************************************
0002  * Copyright (C) 2010 by Peter Penz <peter.penz@gmx.at>                      *
0003  *                                                                           *
0004  * This library is free software; you can redistribute it and/or             *
0005  * modify it under the terms of the GNU Library General Public               *
0006  * License as published by the Free Software Foundation; either              *
0007  * version 2 of the License, or (at your option) any later version.          *
0008  *                                                                           *
0009  * This library is distributed in the hope that it will be useful,           *
0010  * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
0011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         *
0012  * Library General Public License for more details.                          *
0013  *                                                                           *
0014  * You should have received a copy of the GNU Library General Public License *
0015  * along with this library; see the file COPYING.LIB.  If not, write to      *
0016  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,      *
0017  * Boston, MA 02110-1301, USA.                                               *
0018  *****************************************************************************/
0019 
0020 #include "knfotranslator_p.h"
0021 #include <klocalizedstring.h>
0022 
0023 #include <QUrl>
0024 
0025 #include <config-kdelibs4support.h>
0026 #if ! KIO_NO_NEPOMUK
0027 #define DISABLE_NEPOMUK_LEGACY
0028 #include "property.h"
0029 #endif
0030 
0031 struct TranslationItem {
0032     const char *const key;
0033     const char *const context;
0034     const char *const value;
0035 };
0036 
0037 // TODO: a lot of NFOs are missing yet
0038 static const TranslationItem g_translations[] = {
0039     { "http://www.semanticdesktop.org/ontologies/2007/01/19/nie#comment", I18N_NOOP2_NOSTRIP("@label", "Comment") },
0040     { "http://www.semanticdesktop.org/ontologies/2007/01/19/nie#contentCreated", I18N_NOOP2_NOSTRIP("@label creation date", "Created") },
0041     { "http://www.semanticdesktop.org/ontologies/2007/01/19/nie#contentSize", I18N_NOOP2_NOSTRIP("@label file content size", "Size") },
0042     { "http://www.semanticdesktop.org/ontologies/2007/01/19/nie#depends", I18N_NOOP2_NOSTRIP("@label file depends from", "Depends") },
0043     { "http://www.semanticdesktop.org/ontologies/2007/01/19/nie#description", I18N_NOOP2_NOSTRIP("@label", "Description") },
0044     { "http://www.semanticdesktop.org/ontologies/2007/01/19/nie#generator", I18N_NOOP2_NOSTRIP("@label Software used to generate content", "Generator") },
0045     { "http://www.semanticdesktop.org/ontologies/2007/01/19/nie#hasPart", I18N_NOOP2_NOSTRIP("@label see http://www.semanticdesktop.org/ontologies/2007/01/19/nie#hasPart", "Has Part") },
0046     { "http://www.semanticdesktop.org/ontologies/2007/01/19/nie#hasLogicalPart", I18N_NOOP2_NOSTRIP("@label see http://www.semanticdesktop.org/ontologies/2007/01/19/nie#hasLogicalPart", "Has Logical Part") },
0047     { "http://www.semanticdesktop.org/ontologies/2007/01/19/nie#isPartOf", I18N_NOOP2_NOSTRIP("@label parent directory", "Part of") },
0048     { "http://www.semanticdesktop.org/ontologies/2007/01/19/nie#keyword", I18N_NOOP2_NOSTRIP("@label", "Keyword") },
0049     { "http://www.semanticdesktop.org/ontologies/2007/01/19/nie#lastModified", I18N_NOOP2_NOSTRIP("@label modified date of file", "Modified") },
0050     { "http://www.semanticdesktop.org/ontologies/2007/01/19/nie#mimeType", I18N_NOOP2_NOSTRIP("@label", "MIME Type") },
0051     { "http://www.semanticdesktop.org/ontologies/2007/01/19/nie#plainTextContent", I18N_NOOP2_NOSTRIP("@label", "Content") },
0052     { "http://www.semanticdesktop.org/ontologies/2007/01/19/nie#relatedTo", I18N_NOOP2_NOSTRIP("@label", "Related To") },
0053     { "http://www.semanticdesktop.org/ontologies/2007/01/19/nie#subject", I18N_NOOP2_NOSTRIP("@label", "Subject") },
0054     { "http://www.semanticdesktop.org/ontologies/2007/01/19/nie#title", I18N_NOOP2_NOSTRIP("@label music title", "Title") },
0055     { "http://www.semanticdesktop.org/ontologies/2007/01/19/nie#url", I18N_NOOP2_NOSTRIP("@label file URL", "File Location") },
0056     { "http://www.semanticdesktop.org/ontologies/2007/03/22/nco#creator", I18N_NOOP2_NOSTRIP("@label", "Creator") },
0057     { "http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#averageBitrate", I18N_NOOP2_NOSTRIP("@label", "Average Bitrate") },
0058     { "http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#channels", I18N_NOOP2_NOSTRIP("@label", "Channels") },
0059     { "http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#characterCount", I18N_NOOP2_NOSTRIP("@label number of characters", "Characters") },
0060     { "http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#codec",  I18N_NOOP2_NOSTRIP("@label", "Codec") },
0061     { "http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#colorDepth", I18N_NOOP2_NOSTRIP("@label", "Color Depth") },
0062     { "http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#duration", I18N_NOOP2_NOSTRIP("@label", "Duration") },
0063     { "http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#fileName", I18N_NOOP2_NOSTRIP("@label", "Filename") },
0064     { "http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#hasHash", I18N_NOOP2_NOSTRIP("@label", "Hash") },
0065     { "http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#height", I18N_NOOP2_NOSTRIP("@label", "Height") },
0066     { "http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#interlaceMode", I18N_NOOP2_NOSTRIP("@label", "Interlace Mode") },
0067     { "http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#lineCount", I18N_NOOP2_NOSTRIP("@label number of lines", "Lines") },
0068     { "http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#programmingLanguage", I18N_NOOP2_NOSTRIP("@label", "Programming Language") },
0069     { "http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#sampleRate", I18N_NOOP2_NOSTRIP("@label", "Sample Rate") },
0070     { "http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#width", I18N_NOOP2_NOSTRIP("@label", "Width") },
0071     { "http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#wordCount", I18N_NOOP2_NOSTRIP("@label number of words", "Words") },
0072     { "http://www.semanticdesktop.org/ontologies/2007/05/10/nexif#apertureValue", I18N_NOOP2_NOSTRIP("@label EXIF aperture value", "Aperture") },
0073     { "http://www.semanticdesktop.org/ontologies/2007/05/10/nexif#exposureBiasValue", I18N_NOOP2_NOSTRIP("@label EXIF", "Exposure Bias Value") },
0074     { "http://www.semanticdesktop.org/ontologies/2007/05/10/nexif#exposureTime", I18N_NOOP2_NOSTRIP("@label EXIF", "Exposure Time") },
0075     { "http://www.semanticdesktop.org/ontologies/2007/05/10/nexif#flash", I18N_NOOP2_NOSTRIP("@label EXIF", "Flash") },
0076     { "http://www.semanticdesktop.org/ontologies/2007/05/10/nexif#focalLength", I18N_NOOP2_NOSTRIP("@label EXIF", "Focal Length") },
0077     { "http://www.semanticdesktop.org/ontologies/2007/05/10/nexif#focalLengthIn35mmFilm", I18N_NOOP2_NOSTRIP("@label EXIF", "Focal Length 35 mm") },
0078     { "http://www.semanticdesktop.org/ontologies/2007/05/10/nexif#isoSpeedRatings", I18N_NOOP2_NOSTRIP("@label EXIF", "ISO Speed Ratings") },
0079     { "http://www.semanticdesktop.org/ontologies/2007/05/10/nexif#make", I18N_NOOP2_NOSTRIP("@label EXIF", "Make") },
0080     { "http://www.semanticdesktop.org/ontologies/2007/05/10/nexif#meteringMode", I18N_NOOP2_NOSTRIP("@label EXIF", "Metering Mode") },
0081     { "http://www.semanticdesktop.org/ontologies/2007/05/10/nexif#model", I18N_NOOP2_NOSTRIP("@label EXIF", "Model") },
0082     { "http://www.semanticdesktop.org/ontologies/2007/05/10/nexif#orientation", I18N_NOOP2_NOSTRIP("@label EXIF", "Orientation") },
0083     { "http://www.semanticdesktop.org/ontologies/2007/05/10/nexif#whiteBalance", I18N_NOOP2_NOSTRIP("@label EXIF", "White Balance") },
0084     { "http://www.semanticdesktop.org/ontologies/2009/02/19/nmm#director",  I18N_NOOP2_NOSTRIP("@label video director", "Director") },
0085     { "http://www.semanticdesktop.org/ontologies/2009/02/19/nmm#genre",  I18N_NOOP2_NOSTRIP("@label music genre", "Genre") },
0086     { "http://www.semanticdesktop.org/ontologies/2009/02/19/nmm#musicAlbum", I18N_NOOP2_NOSTRIP("@label music album", "Album") },
0087     { "http://www.semanticdesktop.org/ontologies/2009/02/19/nmm#performer", I18N_NOOP2_NOSTRIP("@label", "Performer") },
0088     { "http://www.semanticdesktop.org/ontologies/2009/02/19/nmm#releaseDate", I18N_NOOP2_NOSTRIP("@label", "Release Date") },
0089     { "http://www.semanticdesktop.org/ontologies/2009/02/19/nmm#trackNumber", I18N_NOOP2_NOSTRIP("@label music track number", "Track") },
0090     { "http://www.semanticdesktop.org/ontologies/2007/08/15/nao#created", I18N_NOOP2_NOSTRIP("@label resource created time", "Resource Created")},
0091     { "http://www.semanticdesktop.org/ontologies/2007/08/15/nao#hasSubResource", I18N_NOOP2_NOSTRIP("@label", "Sub Resource")},
0092     { "http://www.semanticdesktop.org/ontologies/2007/08/15/nao#lastModified", I18N_NOOP2_NOSTRIP("@label resource last modified", "Resource Modified")},
0093     { "http://www.semanticdesktop.org/ontologies/2007/08/15/nao#numericRating", I18N_NOOP2_NOSTRIP("@label", "Numeric Rating")},
0094     { "http://www.semanticdesktop.org/ontologies/2010/04/30/ndo#copiedFrom", I18N_NOOP2_NOSTRIP("@label", "Copied From")},
0095     { "http://www.semanticdesktop.org/ontologies/2010/01/25/nuao#firstUsage", I18N_NOOP2_NOSTRIP("@label", "First Usage")},
0096     { "http://www.semanticdesktop.org/ontologies/2010/01/25/nuao#lastUsage", I18N_NOOP2_NOSTRIP("@label", "Last Usage")},
0097     { "http://www.semanticdesktop.org/ontologies/2010/01/25/nuao#usageCount", I18N_NOOP2_NOSTRIP("@label", "Usage Count")},
0098     { "http://nepomuk.kde.org/ontologies/2010/11/29/kext#unixFileGroup", I18N_NOOP2_NOSTRIP("@label", "Unix File Group")},
0099     { "http://nepomuk.kde.org/ontologies/2010/11/29/kext#unixFileMode", I18N_NOOP2_NOSTRIP("@label", "Unix File Mode")},
0100     { "http://nepomuk.kde.org/ontologies/2010/11/29/kext#unixFileOwner", I18N_NOOP2_NOSTRIP("@label", "Unix File Owner")},
0101     { "http://www.w3.org/1999/02/22-rdf-syntax-ns#type", I18N_NOOP2_NOSTRIP("@label file type", "Type") },
0102     { "translation.fuzzy", I18N_NOOP2_NOSTRIP("@label Number of fuzzy translations", "Fuzzy Translations") },
0103     { "translation.last_translator", I18N_NOOP2_NOSTRIP("@label Name of last translator", "Last Translator") },
0104     { "translation.obsolete", I18N_NOOP2_NOSTRIP("@label Number of obsolete translations", "Obsolete Translations") },
0105     { "translation.source_date", I18N_NOOP2_NOSTRIP("@label", "Translation Source Date") },
0106     { "translation.total", I18N_NOOP2_NOSTRIP("@label Number of total translations", "Total Translations") },
0107     { "translation.translated", I18N_NOOP2_NOSTRIP("@label Number of translated strings", "Translated") },
0108     { "translation.translation_date", I18N_NOOP2_NOSTRIP("@label", "Translation Date") },
0109     { "translation.untranslated", I18N_NOOP2_NOSTRIP("@label Number of untranslated strings", "Untranslated") },
0110     { nullptr, nullptr, nullptr } // mandatory last entry
0111 };
0112 
0113 class KNfoTranslatorSingleton
0114 {
0115 public:
0116     KNfoTranslator instance;
0117 };
0118 Q_GLOBAL_STATIC(KNfoTranslatorSingleton, s_nfoTranslator)
0119 
0120 KNfoTranslator &KNfoTranslator::instance()
0121 {
0122     return s_nfoTranslator()->instance;
0123 }
0124 
0125 QString KNfoTranslator::translation(const QUrl &uri) const
0126 {
0127     const QString key = uri.toString();
0128     if (m_hash.contains(key)) {
0129         return m_hash.value(key);
0130     }
0131 
0132     // fallback if the URI is not translated
0133 #if ! KIO_NO_NEPOMUK
0134     const QString label = Nepomuk::Types::Property(uri).label();
0135 #else
0136     QString label;
0137     const int index = key.indexOf(QChar('#'));
0138     if (index >= 0) {
0139         label = key.right(key.size() - index - 1);
0140     }
0141 #endif
0142     QString tunedLabel;
0143     const int labelLength = label.length();
0144     if (labelLength > 0) {
0145         tunedLabel.reserve(labelLength);
0146         tunedLabel = label[0].toUpper();
0147         for (int i = 1; i < labelLength; ++i) {
0148             if (label[i].isUpper() && !label[i - 1].isSpace() && !label[i - 1].isUpper()) {
0149                 tunedLabel += ' ';
0150                 tunedLabel += label[i].toLower();
0151             } else {
0152                 tunedLabel += label[i];
0153             }
0154         }
0155     }
0156     return tunedLabel;
0157 }
0158 
0159 KNfoTranslator::KNfoTranslator() :
0160     m_hash()
0161 {
0162     const TranslationItem *item = &g_translations[0];
0163     while (item->key != nullptr) {
0164         m_hash.insert(item->key, i18nc(item->context, item->value));
0165         ++item;
0166     }
0167 }
0168 
0169 KNfoTranslator::~KNfoTranslator()
0170 {
0171 }