29 #include <tqdatetime.h>
33 #include <tqpainter.h>
34 #include <tqtextstream.h>
37 #include <tdeglobal.h>
38 #include <tdelocale.h>
39 #include <tdeabc/ldifconverter.h>
40 #include <tdeabc/vcardconverter.h>
41 #include <kpixmapsplitter.h>
42 #include <kstandarddirs.h>
43 #include <tdeglobalsettings.h>
45 #include "ldifvcardcreator.h"
49 ThumbCreator *new_creator()
51 TDEGlobal::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 TQTextStream s( &file );
84 s.setEncoding( TQTextStream::UnicodeUTF8 );
85 TQString contents = s.read();
90 TDEABC::AddresseeList addrList;
91 TDEABC::Addressee addr;
92 TDEABC::VCardConverter converter;
94 #if defined(KABC_VCARD_ENCODING_FIX)
95 addrList = converter.parseVCardsRaw( contentsRaw );
97 addrList = converter.parseVCards( contents );
99 if ( addrList.count() == 0 )
100 if ( !TDEABC::LDIFConverter::LDIFToAddressee( contents, addrList ) )
102 if ( addrList.count()>1 ) {
104 name = i18n(
"One contact found:",
"%n contacts found:", addrList.count());
105 unsigned int no, linenr;
106 for (linenr=no=0; linenr<30 && no<addrList.count(); ++no) {
108 info = addr.formattedName().simplifyWhiteSpace();
110 info = addr.givenName() +
" " + addr.familyName();
111 info = info.simplifyWhiteSpace();
122 addr = addrList[ 0 ];
125 name = addr.formattedName().simplifyWhiteSpace();
126 if ( name.isEmpty() )
127 name = addr.givenName() +
" " + addr.familyName();
128 name = name.simplifyWhiteSpace();
131 TDEABC::PhoneNumber::List pnList = addr.phoneNumbers();
132 TQStringList phoneNumbers;
133 for (
unsigned int no=0; no<pnList.count(); ++no) {
134 TQString pn = pnList[no].number().simplifyWhiteSpace();
135 if (!pn.isEmpty() && !phoneNumbers.contains(pn))
136 phoneNumbers.append(pn);
138 if ( !phoneNumbers.isEmpty() )
139 text += phoneNumbers.join(
"\n") +
"\n";
141 info = addr.organization().simplifyWhiteSpace();
142 if ( !info.isEmpty() )
146 TDEABC::Address address = addr.address(TDEABC::Address::Work);
147 if (address.isEmpty())
148 address = addr.address(TDEABC::Address::Home);
149 if (address.isEmpty())
150 address = addr.address(TDEABC::Address::Pref);
151 info = address.formattedAddress();
152 if ( !info.isEmpty() )
159 bool VCard_LDIFCreator::createImageSmall()
161 text = name +
"\n" + text;
164 mSplitter =
new KPixmapSplitter;
165 TQString pixmap = locate(
"data",
"konqueror/pics/thumbnailfont_7x4.png" );
166 if ( pixmap.isEmpty() ) {
169 kdWarning() <<
"VCard_LDIFCreator: Font image \"thumbnailfont_7x4.png\" not found!\n";
172 mSplitter->setPixmap( TQPixmap( pixmap ) );
173 mSplitter->setItemSize( TQSize( 4, 7 ) );
176 TQSize chSize = mSplitter->itemSize();
177 int xOffset = chSize.width();
178 int yOffset = chSize.height();
181 int canvasWidth = pixmapSize.width() - 2 * xborder;
182 int canvasHeight = pixmapSize.height() - 2 * yborder;
183 int numCharsPerLine = (int) (canvasWidth / chSize.width());
184 int numLines = (int) (canvasHeight / chSize.height());
188 int rest = mPixmap.width() - (numCharsPerLine * chSize.width());
189 xborder = TQMAX( xborder, rest / 2 );
190 rest = mPixmap.height() - (numLines * chSize.height());
191 yborder = TQMAX( yborder, rest / 2 );
194 int x = xborder, y = yborder;
195 int posNewLine = mPixmap.width() - (chSize.width() + xborder);
196 int posLastLine = mPixmap.height() - (chSize.height() + yborder);
197 bool newLine =
false;
198 Q_ASSERT( posNewLine > 0 );
199 const TQPixmap *fontPixmap = &(mSplitter->pixmap());
201 for ( uint i = 0; i < text.length(); i++ ) {
202 if ( x > posNewLine || newLine ) {
206 if ( y > posLastLine )
212 int pos = text.find(
'\n', i );
221 TQChar ch = text.at( i );
225 }
else if ( ch ==
'\r' && text.at(i+1) ==
'\n' ) {
231 rect = mSplitter->coordinates( ch );
232 if ( !rect.isEmpty() )
233 bitBlt( &mPixmap, TQPoint(x,y), fontPixmap, rect, TQt::CopyROP );
241 bool VCard_LDIFCreator::createImageBig()
243 TQFont normalFont( TDEGlobalSettings::generalFont() );
244 TQFont titleFont( normalFont );
245 titleFont.setBold(
true);
247 titleFont.setItalic(
true);
249 TQPainter painter(&mPixmap);
250 painter.setFont(titleFont);
251 TQFontMetrics fm(painter.fontMetrics());
254 painter.setClipRect(2, 2, pixmapSize.width()-4, pixmapSize.height()-4);
255 TQPoint p(5, fm.height()+2);
256 painter.drawText(p, name);
260 painter.setFont(normalFont);
261 fm = painter.fontMetrics();
263 const TQStringList list( TQStringList::split(
'\n', text) );
264 for ( TQStringList::ConstIterator it = list.begin();
265 p.y()<=pixmapSize.height() && it != list.end(); ++it ) {
266 p.setY( p.y() + fm.height() );
267 painter.drawText(p, *it);
273 bool VCard_LDIFCreator::create(
const TQString &path,
int width,
int height, TQImage &img)
275 if ( !readContents(path) )
279 pixmapSize = TQSize( width, height );
280 if (height * 3 > width * 4)
281 pixmapSize.setHeight( width * 4 / 3 );
283 pixmapSize.setWidth( height * 3 / 4 );
285 if ( pixmapSize != mPixmap.size() )
286 mPixmap.resize( pixmapSize );
288 mPixmap.fill( TQColor( 245, 245, 245 ) );
291 xborder = 1 + pixmapSize.width()/16;
292 yborder = 1 + pixmapSize.height()/16;
296 ok = createImageBig();
298 ok = createImageSmall();
302 img = mPixmap.convertToImage();
306 ThumbCreator::Flags VCard_LDIFCreator::flags()
const
308 return (Flags)(DrawFrame | BlendIcon);