kaddressbook Library API Documentation

ldifvcardcreator.cpp

00001 /* 00002 This file is part of KAddressBook. 00003 Copyright (C) 2003 Helge Deller <deller@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 version 2 License as published by the Free Software Foundation. 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., 59 Temple Place - Suite 330, 00017 Boston, MA 02111-1307, USA. 00018 */ 00019 00020 /* 00021 * - ldifvcardthumbnail - 00022 * 00023 * kioslave which generates tumbnails for vCard and LDIF files. 00024 * The thumbnails are used e.g. by Konqueror or in the file selection 00025 * dialog. 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 // read file contents 00068 QFile file( path ); 00069 if ( !file.open( IO_ReadOnly ) ) 00070 return false; 00071 00072 QString info; 00073 text.truncate(0); 00074 00075 // read the file 00076 QTextStream t( &file ); 00077 t.setEncoding( QTextStream::Latin1 ); 00078 QString contents = t.read(); 00079 file.close(); 00080 00081 // convert the file contents to a KABC::Addressee address 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 // create an overview (list of all names) 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 // create card for _one_ contact 00110 addr = addrList[ 0 ]; 00111 00112 // prepare the text 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 // get an address 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(); // the size of one char 00163 int xOffset = chSize.width(); 00164 int yOffset = chSize.height(); 00165 00166 // calculate a better border so that the text is centered 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 // render the information 00173 QRect rect; 00174 int rest = mPixmap.width() - (numCharsPerLine * chSize.width()); 00175 xborder = QMAX( xborder, rest / 2 ); // center horizontally 00176 rest = mPixmap.height() - (numLines * chSize.height()); 00177 yborder = QMAX( yborder, rest / 2 ); // center vertically 00178 // end centering 00179 00180 int x = xborder, y = yborder; // where to paint the characters 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 ) { // start a new line? 00189 x = xborder; 00190 y += yOffset; 00191 00192 if ( y > posLastLine ) // more text than space 00193 break; 00194 00195 // after starting a new line, we also jump to the next 00196 // physical newline in the file if we don't come from one 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 // check for newlines in the text (unix,dos) 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++; // skip the next character (\n) as well 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; // next character 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 // titleFont.setUnderline(true); 00233 titleFont.setItalic(true); 00234 00235 QPainter painter(&mPixmap); 00236 painter.setFont(titleFont); 00237 QFontMetrics fm(painter.fontMetrics()); 00238 00239 // draw contact name 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 // draw contact information 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 // resize the image if necessary 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 ) ); // light-grey background 00275 00276 // one pixel for the rectangle, the rest. whitespace 00277 xborder = 1 + pixmapSize.width()/16; // minimum x-border 00278 yborder = 1 + pixmapSize.height()/16; // minimum y-border 00279 00280 bool ok; 00281 if ( width >= 150 /*pixel*/ ) 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 }
KDE Logo
This file is part of the documentation for kaddressbook Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Jul 28 23:58:08 2004 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003