KNewStuff
ktranslatable.cpp
Go to the documentation of this file.00001 /* 00002 This file is part of KNewStuff2. 00003 Copyright (c) 2006, 2007 Josef Spillner <spillner@kde.org> 00004 Copyright (c) 2008 Jeremy Whiting <jeremy@scitools.com> 00005 00006 This library is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU Library General Public 00008 License as published by the Free Software Foundation; either 00009 version 2 of the License, or (at your option) any later version. 00010 00011 This library is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 Library General Public License for more details. 00015 00016 You should have received a copy of the GNU Library General Public License 00017 along with this library; see the file COPYING.LIB. If not, write to 00018 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00019 Boston, MA 02110-1301, USA. 00020 */ 00021 #include "ktranslatable.h" 00022 00023 #include <kglobal.h> 00024 #include <klocale.h> 00025 00026 #include <QtCore/QMutableStringListIterator> 00027 00028 using namespace KNS; 00029 00030 KTranslatable::KTranslatable() 00031 : d(0) 00032 { 00033 } 00034 00035 KTranslatable::KTranslatable(const KTranslatable& other) 00036 : d(0) 00037 { 00038 m_strings = other.m_strings; 00039 } 00040 00041 KTranslatable& KTranslatable::operator=(const KTranslatable & other) 00042 { 00043 if (this == &other) { 00044 return *this; 00045 } 00046 m_strings = other.m_strings; 00047 return *this; 00048 } 00049 00050 KTranslatable::~KTranslatable() 00051 { 00052 // delete d; 00053 } 00054 00055 KTranslatable::KTranslatable(const QString& string) 00056 : d(0) 00057 { 00058 m_strings[QString()] = string; 00059 } 00060 00061 void KTranslatable::addString(const QString& lang, const QString& string) 00062 { 00063 m_strings[lang] = string; 00064 } 00065 00066 QString KTranslatable::representation() const 00067 { 00068 if (m_strings.isEmpty()) return QString(); 00069 00070 const QStringList langs = KGlobal::locale()->languageList(); 00071 for (QStringList::ConstIterator it = langs.begin(); it != langs.end(); ++it) 00072 if (m_strings.contains(*it)) return m_strings[*it]; 00073 00074 if (m_strings.contains(QString())) return m_strings[QString()]; 00075 // NOTE: this could be the source of crashes I've seen occasionally 00076 else return *(m_strings.begin()); 00077 } 00078 00079 QString KTranslatable::language() const 00080 { 00081 if (m_strings.isEmpty()) return QString(); 00082 00083 const QStringList langs = KGlobal::locale()->languageList(); 00084 for (QStringList::ConstIterator it = langs.begin(); it != langs.end(); ++it) 00085 if (m_strings.contains(*it)) return (*it); 00086 00087 if (m_strings.contains(QString())) return QString(); 00088 else return m_strings.begin().key(); 00089 } 00090 00091 QString KTranslatable::translated(const QString& lang) const 00092 { 00093 if (m_strings.contains(lang)) 00094 return m_strings[lang]; 00095 return QString(); 00096 } 00097 00098 QStringList KTranslatable::languages() const 00099 { 00100 return m_strings.keys(); 00101 } 00102 00103 QStringList KTranslatable::strings() const 00104 { 00105 return m_strings.values(); 00106 } 00107 00108 QMap<QString, QString> KTranslatable::stringmap() const 00109 { 00110 return m_strings; 00111 } 00112 00113 bool KTranslatable::isTranslated() const 00114 { 00115 return m_strings.count() > 1; 00116 } 00117 00118 bool KTranslatable::isEmpty() const 00119 { 00120 return m_strings.isEmpty(); 00121 } 00122