29 #include <tqdatetime.h>
33 #include <tqpainter.h>
34 #include <tqtextstream.h>
39 #include <kabc/ldifconverter.h>
40 #include <kabc/vcardconverter.h>
41 #include <kpixmapsplitter.h>
42 #include <kstandarddirs.h>
43 #include <kglobalsettings.h>
45 #include "ldifvcardcreator.h"
49 ThumbCreator *new_creator()
51 KGlobal::locale()->insertCatalogue(
"kaddressbook" );
52 return new VCard_LDIFCreator;
56 VCard_LDIFCreator::VCard_LDIFCreator()
61 VCard_LDIFCreator::~VCard_LDIFCreator()
67 bool VCard_LDIFCreator::readContents(
const TQString &path )
71 if ( !file.open( IO_ReadOnly ) )
78 #if defined(KABC_VCARD_ENCODING_FIX)
79 const TQByteArray data = file.readAll();
80 const TQString contents( data );
81 const TQCString contentsRaw( data.data(), data.size() );
83 TQString contents = file.readAll();
88 KABC::AddresseeList addrList;
90 KABC::VCardConverter converter;
92 #if defined(KABC_VCARD_ENCODING_FIX)
93 addrList = converter.parseVCardsRaw( contentsRaw );
95 addrList = converter.parseVCards( contents );
97 if ( addrList.count() == 0 )
98 if ( !KABC::LDIFConverter::LDIFToAddressee( contents, addrList ) )
100 if ( addrList.count()>1 ) {
102 name = i18n(
"One contact found:",
"%n contacts found:", addrList.count());
103 unsigned int no, linenr;
104 for (linenr=no=0; linenr<30 && no<addrList.count(); ++no) {
106 info = addr.formattedName().simplifyWhiteSpace();
108 info = addr.givenName() +
" " + addr.familyName();
109 info = info.simplifyWhiteSpace();
120 addr = addrList[ 0 ];
123 name = addr.formattedName().simplifyWhiteSpace();
124 if ( name.isEmpty() )
125 name = addr.givenName() +
" " + addr.familyName();
126 name = name.simplifyWhiteSpace();
129 KABC::PhoneNumber::List pnList = addr.phoneNumbers();
130 TQStringList phoneNumbers;
131 for (
unsigned int no=0; no<pnList.count(); ++no) {
132 TQString pn = pnList[no].number().simplifyWhiteSpace();
133 if (!pn.isEmpty() && !phoneNumbers.contains(pn))
134 phoneNumbers.append(pn);
136 if ( !phoneNumbers.isEmpty() )
137 text += phoneNumbers.join(
"\n") +
"\n";
139 info = addr.organization().simplifyWhiteSpace();
140 if ( !info.isEmpty() )
144 KABC::Address address = addr.address(KABC::Address::Work);
145 if (address.isEmpty())
146 address = addr.address(KABC::Address::Home);
147 if (address.isEmpty())
148 address = addr.address(KABC::Address::Pref);
149 info = address.formattedAddress();
150 if ( !info.isEmpty() )
157 bool VCard_LDIFCreator::createImageSmall()
159 text = name +
"\n" + text;
162 mSplitter =
new KPixmapSplitter;
163 TQString pixmap = locate(
"data",
"konqueror/pics/thumbnailfont_7x4.png" );
164 if ( pixmap.isEmpty() ) {
167 kdWarning() <<
"VCard_LDIFCreator: Font image \"thumbnailfont_7x4.png\" not found!\n";
170 mSplitter->setPixmap( TQPixmap( pixmap ) );
171 mSplitter->setItemSize( TQSize( 4, 7 ) );
174 TQSize chSize = mSplitter->itemSize();
175 int xOffset = chSize.width();
176 int yOffset = chSize.height();
179 int canvasWidth = pixmapSize.width() - 2 * xborder;
180 int canvasHeight = pixmapSize.height() - 2 * yborder;
181 int numCharsPerLine = (int) (canvasWidth / chSize.width());
182 int numLines = (int) (canvasHeight / chSize.height());
186 int rest = mPixmap.width() - (numCharsPerLine * chSize.width());
187 xborder = TQMAX( xborder, rest / 2 );
188 rest = mPixmap.height() - (numLines * chSize.height());
189 yborder = TQMAX( yborder, rest / 2 );
192 int x = xborder, y = yborder;
193 int posNewLine = mPixmap.width() - (chSize.width() + xborder);
194 int posLastLine = mPixmap.height() - (chSize.height() + yborder);
195 bool newLine =
false;
196 Q_ASSERT( posNewLine > 0 );
197 const TQPixmap *fontPixmap = &(mSplitter->pixmap());
199 for ( uint i = 0; i < text.length(); i++ ) {
200 if ( x > posNewLine || newLine ) {
204 if ( y > posLastLine )
210 int pos = text.find(
'\n', i );
219 TQChar ch = text.at( i );
223 }
else if ( ch ==
'\r' && text.at(i+1) ==
'\n' ) {
229 rect = mSplitter->coordinates( ch );
230 if ( !rect.isEmpty() )
231 bitBlt( &mPixmap, TQPoint(x,y), fontPixmap, rect, TQt::CopyROP );
239 bool VCard_LDIFCreator::createImageBig()
241 TQFont normalFont( KGlobalSettings::generalFont() );
242 TQFont titleFont( normalFont );
243 titleFont.setBold(
true);
245 titleFont.setItalic(
true);
247 TQPainter painter(&mPixmap);
248 painter.setFont(titleFont);
249 TQFontMetrics fm(painter.fontMetrics());
252 painter.setClipRect(2, 2, pixmapSize.width()-4, pixmapSize.height()-4);
253 TQPoint p(5, fm.height()+2);
254 painter.drawText(p, name);
258 painter.setFont(normalFont);
259 fm = painter.fontMetrics();
261 const TQStringList list( TQStringList::split(
'\n', text) );
262 for ( TQStringList::ConstIterator it = list.begin();
263 p.y()<=pixmapSize.height() && it != list.end(); ++it ) {
264 p.setY( p.y() + fm.height() );
265 painter.drawText(p, *it);
271 bool VCard_LDIFCreator::create(
const TQString &path,
int width,
int height, TQImage &img)
273 if ( !readContents(path) )
277 pixmapSize = TQSize( width, height );
278 if (height * 3 > width * 4)
279 pixmapSize.setHeight( width * 4 / 3 );
281 pixmapSize.setWidth( height * 3 / 4 );
283 if ( pixmapSize != mPixmap.size() )
284 mPixmap.resize( pixmapSize );
286 mPixmap.fill( TQColor( 245, 245, 245 ) );
289 xborder = 1 + pixmapSize.width()/16;
290 yborder = 1 + pixmapSize.height()/16;
294 ok = createImageBig();
296 ok = createImageSmall();
300 img = mPixmap.convertToImage();
304 ThumbCreator::Flags VCard_LDIFCreator::flags()
const
306 return (Flags)(DrawFrame | BlendIcon);