kcatalogue.cpp
00001 /* This file is part of the KDE libraries 00002 Copyright (c) 2001 Hans Petter Bieker <bieker@kde.org> 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License as published by the Free Software Foundation; either 00007 version 2 of the License, or (at your option) any later version. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 Library General Public License for more details. 00013 00014 You should have received a copy of the GNU Library General Public License 00015 along with this library; see the file COPYING.LIB. If not, write to 00016 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00017 Boston, MA 02110-1301, USA. 00018 */ 00019 00020 #include <config.h> 00021 00022 #include <tqfile.h> 00023 00024 #include <kdebug.h> 00025 00026 #include "kcatalogue.h" 00027 #include "kstandarddirs.h" 00028 00029 char *k_nl_find_msg(struct kde_loaded_l10nfile *domain_file, 00030 const char *msgid); 00031 void k_nl_unload_domain (struct loaded_domain *domain); 00032 00033 #ifndef KDE_USE_FINAL // with --enable-final, we're getting this from libintl.cpp 00034 struct kde_loaded_l10nfile 00035 { 00036 const char *filename; 00037 int decided; 00038 00039 const void *data; 00040 00041 kde_loaded_l10nfile() : filename(0), decided(0), data(0) {} 00042 }; 00043 #endif 00044 00045 class KCataloguePrivate 00046 { 00047 public: 00048 TQString name; 00049 TQString language; 00050 int pluralType; 00051 00052 kde_loaded_l10nfile domain; 00053 }; 00054 00055 KCatalogue::KCatalogue(const TQString & name, const TQString & language ) 00056 : d( new KCataloguePrivate ) 00057 { 00058 d->name = name; 00059 d->language = language; 00060 // at the moment we do not know more. To find out the plural type we first have to look into 00061 // kdelibs.mo for the language. And for this we already need a catalog object. So this data 00062 // has to be set after we have the first catalog objects. 00063 d->pluralType = -1; 00064 00065 TQString path = TQString::fromLatin1("%1/LC_MESSAGES/%2.mo") 00066 .arg( d->language ) 00067 .arg( d->name ); 00068 00069 TQString fileName = locate( "locale", path ); 00070 if (fileName.isEmpty()) 00071 fileName = locate( "locale-bundle", path ); 00072 00073 setFileName( fileName ); 00074 00075 } 00076 00077 KCatalogue::KCatalogue(const KCatalogue & rhs) 00078 : d( new KCataloguePrivate ) 00079 { 00080 *this = rhs; 00081 } 00082 00083 KCatalogue & KCatalogue::operator=(const KCatalogue & rhs) 00084 { 00085 d->name = rhs.d->name; 00086 d->language = rhs.d->language; 00087 d->pluralType = rhs.d->pluralType; 00088 setFileName( rhs.fileName() ); 00089 00090 return *this; 00091 } 00092 00093 KCatalogue::~KCatalogue() 00094 { 00095 doUnload(); 00096 00097 delete d; 00098 } 00099 00100 TQString KCatalogue::name() const 00101 { 00102 return d->name; 00103 } 00104 00105 TQString KCatalogue::language() const 00106 { 00107 return d->language; 00108 } 00109 00110 void KCatalogue::setPluralType( int pluralType) 00111 { 00112 d->pluralType = pluralType; 00113 } 00114 00115 int KCatalogue::pluralType() const 00116 { 00117 return d->pluralType; 00118 } 00119 00120 00121 void KCatalogue::setFileName( const TQString & fileName ) 00122 { 00123 // nothing to do if the file name is already the same 00124 if ( this->fileName() == fileName ) return; 00125 00126 doUnload(); 00127 00128 TQCString newFileName = TQFile::encodeName( fileName ); 00129 00130 if ( !fileName.isEmpty() ) 00131 { 00132 // set file name 00133 char *filename = new char[ newFileName.length() + 1 ]; 00134 ::qstrcpy( filename, newFileName ); 00135 d->domain.filename = filename; 00136 } 00137 } 00138 00139 TQString KCatalogue::fileName() const 00140 { 00141 return TQFile::decodeName( d->domain.filename ); 00142 } 00143 00144 const char * KCatalogue::translate(const char * msgid) const 00145 { 00146 return ::k_nl_find_msg( &d->domain, msgid ); 00147 } 00148 00149 void KCatalogue::doUnload() 00150 { 00151 // use gettext's unloader 00152 if ( d->domain.data ) 00153 ::k_nl_unload_domain( (struct loaded_domain *)d->domain.data ); 00154 d->domain.data = 0; 00155 00156 // free name 00157 delete [] const_cast<char *>(d->domain.filename); 00158 d->domain.filename = 0; 00159 00160 d->domain.decided = 0; 00161 }