kabc
phonenumber.cpp
00001 /* 00002 This file is part of libkabc. 00003 Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org> 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Library General Public 00007 License as published by the Free Software Foundation; either 00008 version 2 of the License, or (at your option) any later version. 00009 00010 This library is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Library General Public License for more details. 00014 00015 You should have received a copy of the GNU Library General Public License 00016 along with this library; see the file COPYING.LIB. If not, write to 00017 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00018 Boston, MA 02110-1301, USA. 00019 */ 00020 00021 #include "phonenumber.h" 00022 00023 #include <klocale.h> 00024 #include <krandom.h> 00025 00026 #include <QtCore/QDataStream> 00027 #include <QtCore/QSharedData> 00028 00029 using namespace KABC; 00030 00031 static QString cleanupNumber( const QString &input ) 00032 { 00033 return input.simplified(); 00034 } 00035 00036 class PhoneNumber::Private : public QSharedData 00037 { 00038 public: 00039 Private( Type type ) 00040 : mId( KRandom::randomString( 8 ) ), mType( type ) 00041 { 00042 } 00043 00044 Private( const Private &other ) 00045 : QSharedData( other ) 00046 { 00047 mId = other.mId; 00048 mType = other.mType; 00049 mNumber = other.mNumber; 00050 } 00051 00052 QString mId; 00053 Type mType; 00054 QString mNumber; 00055 }; 00056 00057 PhoneNumber::PhoneNumber() 00058 : d( new Private( Home ) ) 00059 { 00060 } 00061 00062 PhoneNumber::PhoneNumber( const QString &number, Type type ) 00063 : d( new Private( type ) ) 00064 { 00065 d->mNumber = cleanupNumber( number ); 00066 } 00067 00068 PhoneNumber::PhoneNumber( const PhoneNumber &other ) 00069 : d( other.d ) 00070 { 00071 } 00072 00073 PhoneNumber::~PhoneNumber() 00074 { 00075 } 00076 00077 bool PhoneNumber::operator==( const PhoneNumber &other ) const 00078 { 00079 if ( d->mId != other.d->mId ) { 00080 return false; 00081 } 00082 00083 if ( d->mNumber != other.d->mNumber ) { 00084 return false; 00085 } 00086 00087 if ( d->mType != other.d->mType ) { 00088 return false; 00089 } 00090 00091 return true; 00092 } 00093 00094 bool PhoneNumber::operator!=( const PhoneNumber &other ) const 00095 { 00096 return !( other == *this ); 00097 } 00098 00099 PhoneNumber &PhoneNumber::operator=( const PhoneNumber &other ) 00100 { 00101 if ( this != &other ) { 00102 d = other.d; 00103 } 00104 00105 return *this; 00106 } 00107 00108 bool PhoneNumber::isEmpty() const 00109 { 00110 return d->mNumber.isEmpty(); 00111 } 00112 00113 void PhoneNumber::setId( const QString &id ) 00114 { 00115 d->mId = id; 00116 } 00117 00118 QString PhoneNumber::id() const 00119 { 00120 return d->mId; 00121 } 00122 00123 void PhoneNumber::setNumber( const QString &number ) 00124 { 00125 d->mNumber = cleanupNumber( number ); 00126 } 00127 00128 QString PhoneNumber::number() const 00129 { 00130 return d->mNumber; 00131 } 00132 00133 void PhoneNumber::setType( Type type ) 00134 { 00135 d->mType = type; 00136 } 00137 00138 PhoneNumber::Type PhoneNumber::type() const 00139 { 00140 return d->mType; 00141 } 00142 00143 QString PhoneNumber::typeLabel() const 00144 { 00145 return typeLabel( type() ); 00146 } 00147 00148 PhoneNumber::TypeList PhoneNumber::typeList() 00149 { 00150 static TypeList list; 00151 00152 if ( list.isEmpty() ) { 00153 list << Home << Work << Msg << Pref << Voice << Fax << Cell << Video 00154 << Bbs << Modem << Car << Isdn << Pcs << Pager; 00155 } 00156 00157 return list; 00158 } 00159 00160 QString PhoneNumber::typeFlagLabel( TypeFlag type ) 00161 { 00162 switch ( type ) { 00163 case Home: 00164 return i18nc( "Home phone", "Home" ); 00165 break; 00166 case Work: 00167 return i18nc( "Work phone", "Work" ); 00168 break; 00169 case Msg: 00170 return i18n( "Messenger" ); 00171 break; 00172 case Pref: 00173 return i18nc( "Preferred phone", "Preferred" ); 00174 break; 00175 case Voice: 00176 return i18n( "Voice" ); 00177 break; 00178 case Fax: 00179 return i18n( "Fax" ); 00180 break; 00181 case Cell: 00182 return i18nc( "Mobile Phone", "Mobile" ); 00183 break; 00184 case Video: 00185 return i18nc( "Video phone", "Video" ); 00186 break; 00187 case Bbs: 00188 return i18n( "Mailbox" ); 00189 break; 00190 case Modem: 00191 return i18n( "Modem" ); 00192 break; 00193 case Car: 00194 return i18nc( "Car Phone", "Car" ); 00195 break; 00196 case Isdn: 00197 return i18n( "ISDN" ); 00198 break; 00199 case Pcs: 00200 return i18n( "PCS" ); 00201 break; 00202 case Pager: 00203 return i18n( "Pager" ); 00204 break; 00205 default: 00206 return i18nc( "another type of phone", "Other" ); 00207 } 00208 } 00209 00210 QString PhoneNumber::typeLabel( Type type ) 00211 { 00212 QString label; 00213 bool first = true; 00214 00215 // special cases 00216 // Pref stand alone -> Preferred Number 00217 // Home+Fax or Work+Fax -> combine as initial string 00218 if ( type == Pref ) { 00219 return i18n( "Preferred Number" ); 00220 } 00221 00222 if ( type & Fax ) { 00223 if ( type & Home ) { 00224 label = i18n( "Home Fax" ); 00225 first = false; 00226 type &= ~Fax; 00227 type &= ~Home; 00228 } else if ( type & Work ) { 00229 label = i18n( "Work Fax" ); 00230 first = false; 00231 type &= ~Fax; 00232 type &= ~Work; 00233 } 00234 } 00235 00236 const TypeList list = typeList(); 00237 00238 TypeList::ConstIterator it; 00239 for ( it = list.begin(); it != list.end(); ++it ) { 00240 // these are actually flags 00241 const TypeFlag flag = static_cast<TypeFlag>( static_cast<int>( *it ) ); 00242 if ( type & flag ) { 00243 if ( !first ) { 00244 label.append( QLatin1Char( '/' ) ); 00245 } 00246 00247 label.append( typeFlagLabel( flag ) ); 00248 00249 if ( first ) { 00250 first = false; 00251 } 00252 } 00253 } 00254 00255 return label; 00256 } 00257 00258 QString PhoneNumber::toString() const 00259 { 00260 QString str; 00261 00262 str += QString::fromLatin1( "PhoneNumber {\n" ); 00263 str += QString::fromLatin1( " Id: %1\n" ).arg( d->mId ); 00264 str += QString::fromLatin1( " Type: %1\n" ).arg( typeLabel( d->mType ) ); 00265 str += QString::fromLatin1( " Number: %1\n" ).arg( d->mNumber ); 00266 str += QString::fromLatin1( "}\n" ); 00267 00268 return str; 00269 } 00270 00271 QDataStream &KABC::operator<<( QDataStream &s, const PhoneNumber &phone ) 00272 { 00273 return s << phone.d->mId << (uint)phone.d->mType << phone.d->mNumber; 00274 } 00275 00276 QDataStream &KABC::operator>>( QDataStream &s, PhoneNumber &phone ) 00277 { 00278 uint type; 00279 s >> phone.d->mId >> type >> phone.d->mNumber; 00280 phone.d->mType = PhoneNumber::Type( type ); 00281 00282 return s; 00283 }
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Thu May 10 2012 22:20:25 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006
Documentation copyright © 1996-2012 The KDE developers.
Generated on Thu May 10 2012 22:20:25 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006
KDE's Doxygen guidelines are available online.