ldifvcardcreator.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include <qdatetime.h>
00030 #include <qfile.h>
00031 #include <qpixmap.h>
00032 #include <qimage.h>
00033 #include <qpainter.h>
00034 #include <qtextstream.h>
00035
00036 #include <kdebug.h>
00037 #include <klocale.h>
00038 #include <kabc/ldifconverter.h>
00039 #include <kabc/vcardconverter.h>
00040 #include <kpixmapsplitter.h>
00041 #include <kstandarddirs.h>
00042 #include <kglobalsettings.h>
00043
00044 #include "ldifvcardcreator.h"
00045
00046 extern "C"
00047 {
00048 ThumbCreator *new_creator()
00049 {
00050 return new VCard_LDIFCreator;
00051 }
00052 }
00053
00054 VCard_LDIFCreator::VCard_LDIFCreator()
00055 : mSplitter( 0 )
00056 {
00057 }
00058
00059 VCard_LDIFCreator::~VCard_LDIFCreator()
00060 {
00061 delete mSplitter;
00062 }
00063
00064
00065 bool VCard_LDIFCreator::readContents( const QString &path )
00066 {
00067
00068 QFile file( path );
00069 if ( !file.open( IO_ReadOnly ) )
00070 return false;
00071
00072 QString info;
00073 text.truncate(0);
00074
00075
00076 QTextStream t( &file );
00077 t.setEncoding( QTextStream::Latin1 );
00078 QString contents = t.read();
00079 file.close();
00080
00081
00082 KABC::AddresseeList addrList;
00083 KABC::Addressee addr;
00084 KABC::VCardConverter converter;
00085
00086 addrList = converter.parseVCards( contents );
00087 if ( addrList.count() == 0 )
00088 if ( !KABC::LDIFConverter::LDIFToAddressee( contents, addrList ) )
00089 return false;
00090 if ( addrList.count()>1 ) {
00091
00092 name = i18n("One contact found:", "%n contacts found:", addrList.count());
00093 unsigned int no, linenr;
00094 for (linenr=no=0; linenr<30 && no<addrList.count(); ++no) {
00095 addr = addrList[no];
00096 info = addr.formattedName().simplifyWhiteSpace();
00097 if (info.isEmpty())
00098 info = addr.givenName() + " " + addr.familyName();
00099 info = info.simplifyWhiteSpace();
00100 if (info.isEmpty())
00101 continue;
00102 text.append(info);
00103 text.append("\n");
00104 ++linenr;
00105 }
00106 return true;
00107 }
00108
00109
00110 addr = addrList[ 0 ];
00111
00112
00113 name = addr.formattedName().simplifyWhiteSpace();
00114 if ( name.isEmpty() )
00115 name = addr.givenName() + " " + addr.familyName();
00116 name = name.simplifyWhiteSpace();
00117
00118
00119 KABC::PhoneNumber::List pnList = addr.phoneNumbers();
00120 QStringList phoneNumbers;
00121 for (unsigned int no=0; no<pnList.count(); ++no) {
00122 QString pn = pnList[no].number().simplifyWhiteSpace();
00123 if (!pn.isEmpty() && !phoneNumbers.contains(pn))
00124 phoneNumbers.append(pn);
00125 }
00126 if ( !phoneNumbers.isEmpty() )
00127 text += phoneNumbers.join("\n") + "\n";
00128
00129 info = addr.organization().simplifyWhiteSpace();
00130 if ( !info.isEmpty() )
00131 text += info + "\n";
00132
00133
00134 KABC::Address address = addr.address(KABC::Address::Work);
00135 if (address.isEmpty())
00136 address = addr.address(KABC::Address::Home);
00137 if (address.isEmpty())
00138 address = addr.address(KABC::Address::Pref);
00139 info = address.formattedAddress();
00140 if ( !info.isEmpty() )
00141 text += info + "\n";
00142
00143 return true;
00144 }
00145
00146
00147 bool VCard_LDIFCreator::createImageSmall()
00148 {
00149 text = name + "\n" + text;
00150
00151 if ( !mSplitter ) {
00152 mSplitter = new KPixmapSplitter;
00153 QString pixmap = locate( "data", "konqueror/pics/thumbnailfont_7x4.png" );
00154 if ( pixmap.isEmpty() ) {
00155 kdWarning() << "VCard_LDIFCreator: Font image \"thumbnailfont_7x4.png\" not found!\n";
00156 return false;
00157 }
00158 mSplitter->setPixmap( QPixmap( pixmap ) );
00159 mSplitter->setItemSize( QSize( 4, 7 ) );
00160 }
00161
00162 QSize chSize = mSplitter->itemSize();
00163 int xOffset = chSize.width();
00164 int yOffset = chSize.height();
00165
00166
00167 int canvasWidth = pixmapSize.width() - 2 * xborder;
00168 int canvasHeight = pixmapSize.height() - 2 * yborder;
00169 int numCharsPerLine = (int) (canvasWidth / chSize.width());
00170 int numLines = (int) (canvasHeight / chSize.height());
00171
00172
00173 QRect rect;
00174 int rest = mPixmap.width() - (numCharsPerLine * chSize.width());
00175 xborder = QMAX( xborder, rest / 2 );
00176 rest = mPixmap.height() - (numLines * chSize.height());
00177 yborder = QMAX( yborder, rest / 2 );
00178
00179
00180 int x = xborder, y = yborder;
00181 int posNewLine = mPixmap.width() - (chSize.width() + xborder);
00182 int posLastLine = mPixmap.height() - (chSize.height() + yborder);
00183 bool newLine = false;
00184 Q_ASSERT( posNewLine > 0 );
00185 const QPixmap *fontPixmap = &(mSplitter->pixmap());
00186
00187 for ( uint i = 0; i < text.length(); i++ ) {
00188 if ( x > posNewLine || newLine ) {
00189 x = xborder;
00190 y += yOffset;
00191
00192 if ( y > posLastLine )
00193 break;
00194
00195
00196
00197 if ( !newLine ) {
00198 int pos = text.find( '\n', i );
00199 if ( pos > (int) i )
00200 i = pos +1;
00201 }
00202
00203 newLine = false;
00204 }
00205
00206
00207 QChar ch = text.at( i );
00208 if ( ch == '\n' ) {
00209 newLine = true;
00210 continue;
00211 } else if ( ch == '\r' && text.at(i+1) == '\n' ) {
00212 newLine = true;
00213 i++;
00214 continue;
00215 }
00216
00217 rect = mSplitter->coordinates( ch );
00218 if ( !rect.isEmpty() )
00219 bitBlt( &mPixmap, QPoint(x,y), fontPixmap, rect, Qt::CopyROP );
00220
00221 x += xOffset;
00222 }
00223
00224 return true;
00225 }
00226
00227 bool VCard_LDIFCreator::createImageBig()
00228 {
00229 QFont normalFont( KGlobalSettings::generalFont() );
00230 QFont titleFont( normalFont );
00231 titleFont.setBold(true);
00232
00233 titleFont.setItalic(true);
00234
00235 QPainter painter(&mPixmap);
00236 painter.setFont(titleFont);
00237 QFontMetrics fm(painter.fontMetrics());
00238
00239
00240 painter.setClipRect(2, 2, pixmapSize.width()-4, pixmapSize.height()-4);
00241 QPoint p(5, fm.height()+2);
00242 painter.drawText(p, name);
00243 p.setY( 3*p.y()/2 );
00244
00245
00246 painter.setFont(normalFont);
00247 fm = painter.fontMetrics();
00248
00249 QStringList list = QStringList::split('\n', text);
00250 for ( QStringList::Iterator it = list.begin();
00251 p.y()<=pixmapSize.height() && it != list.end(); ++it ) {
00252 p.setY( p.y() + fm.height() );
00253 painter.drawText(p, *it);
00254 }
00255
00256 return true;
00257 }
00258
00259 bool VCard_LDIFCreator::create(const QString &path, int width, int height, QImage &img)
00260 {
00261 if ( !readContents(path) )
00262 return false;
00263
00264
00265 pixmapSize = QSize( width, height );
00266 if (height * 3 > width * 4)
00267 pixmapSize.setHeight( width * 4 / 3 );
00268 else
00269 pixmapSize.setWidth( height * 3 / 4 );
00270
00271 if ( pixmapSize != mPixmap.size() )
00272 mPixmap.resize( pixmapSize );
00273
00274 mPixmap.fill( QColor( 245, 245, 245 ) );
00275
00276
00277 xborder = 1 + pixmapSize.width()/16;
00278 yborder = 1 + pixmapSize.height()/16;
00279
00280 bool ok;
00281 if ( width >= 150 )
00282 ok = createImageBig();
00283 else
00284 ok = createImageSmall();
00285 if (!ok)
00286 return false;
00287
00288 img = mPixmap.convertToImage();
00289 return true;
00290 }
00291
00292 ThumbCreator::Flags VCard_LDIFCreator::flags() const
00293 {
00294 return (Flags)(DrawFrame | BlendIcon);
00295 }
This file is part of the documentation for kaddressbook Library Version 3.2.2.