xfaceconfigurator.cpp
00001 /* 00002 This file is part of KMail. 00003 00004 Copyright (c) 2004 Jakob Schr�er <js@camaya.net> 00005 00006 This library is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU Library General Public 00008 License as published by the Free Software Foundation; either 00009 version 2 of the License, or (at your option) any later version. 00010 00011 This library is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 Library General Public License for more details. 00015 00016 You should have received a copy of the GNU Library General Public License 00017 along with this library; see the file COPYING.LIB. If not, write to 00018 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00019 Boston, MA 02110-1301, USA. 00020 00021 In addition, as a special exception, the copyright holders give 00022 permission to link the code of this program with any edition of 00023 the TQt library by Trolltech AS, Norway (or with modified versions 00024 of TQt that use the same license as TQt), and distribute linked 00025 combinations including the two. You must obey the GNU General 00026 Public License in all respects for all of the code used other than 00027 TQt. If you modify this file, you may extend this exception to 00028 your version of the file, but you are not obligated to do so. If 00029 you do not wish to do so, delete this exception statement from 00030 your version. 00031 */ 00032 00033 #ifdef HAVE_CONFIG_H 00034 #include <config.h> 00035 #endif 00036 00037 #include "xfaceconfigurator.h" 00038 00039 #include <kactivelabel.h> 00040 #include <kdialog.h> 00041 #include <kfiledialog.h> 00042 #include <kglobalsettings.h> 00043 #include <kimageio.h> 00044 #include <klocale.h> 00045 #include <kmessagebox.h> 00046 #include <kurl.h> 00047 #include <kio/netaccess.h> 00048 using namespace KIO; 00049 #include <kxface.h> 00050 using namespace KPIM; 00051 #include <kabc/stdaddressbook.h> 00052 #include <kabc/addressee.h> 00053 using namespace KABC; 00054 00055 #include <tqbitmap.h> 00056 #include <tqcheckbox.h> 00057 #include <tqcombobox.h> 00058 #include <tqimage.h> 00059 #include <tqlabel.h> 00060 #include <tqlayout.h> 00061 #include <tqpushbutton.h> 00062 #include <tqwhatsthis.h> 00063 #include <tqwidgetstack.h> 00064 00065 00066 // #include <assert.h> 00067 00068 using namespace KMail; 00069 using namespace KPIM; 00070 00071 namespace KMail { 00072 00073 XFaceConfigurator::XFaceConfigurator( TQWidget * parent, const char * name ) 00074 : TQWidget( parent, name ) 00075 { 00076 // tmp. vars: 00077 TQLabel * label; 00078 TQLabel * label1; 00079 KActiveLabel * label2; 00080 TQWidget * page; 00081 TQVBoxLayout * vlay; 00082 TQHBoxLayout * hlay; 00083 TQVBoxLayout * page_vlay; 00084 TQPushButton * mFromFileBtn; 00085 TQPushButton * mFromAddrbkBtn; 00086 00087 vlay = new TQVBoxLayout( this, 0, KDialog::spacingHint(), "main layout" ); 00088 hlay = new TQHBoxLayout( vlay ); 00089 00090 // "enable X-Face" checkbox: 00091 mEnableCheck = new TQCheckBox( i18n("&Send picture with every message"), this ); 00092 TQWhatsThis::add( mEnableCheck, 00093 i18n( "Check this box if you want KMail to add a so-called X-Face header to messages " 00094 "written with this identity. An X-Face is a small (48x48 pixels) black and " 00095 "white image that some mail clients are able to display." ) ); 00096 hlay->addWidget( mEnableCheck, TQt::AlignLeft | TQt::AlignVCenter ); 00097 00098 mXFaceLabel = new TQLabel( this ); 00099 TQWhatsThis::add(mXFaceLabel, 00100 i18n( "This is a preview of the picture selected/entered below." ) ); 00101 mXFaceLabel->setFixedSize(48, 48); 00102 mXFaceLabel->setFrameShape( TQFrame::Box ); 00103 hlay->addWidget( mXFaceLabel ); 00104 00105 // label1 = new TQLabel( "X-Face:", this ); 00106 // vlay->addWidget( label1 ); 00107 00108 // "obtain X-Face from" combo and label: 00109 hlay = new TQHBoxLayout( vlay ); // inherits spacing 00110 mSourceCombo = new TQComboBox( false, this ); 00111 TQWhatsThis::add(mSourceCombo, 00112 i18n("Click on the widgets below to obtain help on the input methods.")); 00113 mSourceCombo->setEnabled( false ); // since !mEnableCheck->isChecked() 00114 mSourceCombo->insertStringList( TQStringList() 00115 << i18n( "continuation of \"obtain picture from\"", 00116 "External Source" ) 00117 << i18n( "continuation of \"obtain picture from\"", 00118 "Input Field Below" ) ); 00119 label = new TQLabel( mSourceCombo, 00120 i18n("Obtain pic&ture from:"), this ); 00121 label->setEnabled( false ); // since !mEnableCheck->isChecked() 00122 hlay->addWidget( label ); 00123 hlay->addWidget( mSourceCombo, 1 ); 00124 00125 // widget stack that is controlled by the source combo: 00126 TQWidgetStack * widgetStack = new TQWidgetStack( this ); 00127 widgetStack->setEnabled( false ); // since !mEnableCheck->isChecked() 00128 vlay->addWidget( widgetStack, 1 ); 00129 connect( mSourceCombo, TQT_SIGNAL(highlighted(int)), 00130 widgetStack, TQT_SLOT(raiseWidget(int)) ); 00131 connect( mEnableCheck, TQT_SIGNAL(toggled(bool)), 00132 mSourceCombo, TQT_SLOT(setEnabled(bool)) ); 00133 connect( mEnableCheck, TQT_SIGNAL(toggled(bool)), 00134 widgetStack, TQT_SLOT(setEnabled(bool)) ); 00135 connect( mEnableCheck, TQT_SIGNAL(toggled(bool)), 00136 label, TQT_SLOT(setEnabled(bool)) ); 00137 // The focus might be still in the widget that is disabled 00138 connect( mEnableCheck, TQT_SIGNAL(clicked()), 00139 mEnableCheck, TQT_SLOT(setFocus()) ); 00140 00141 int pageno = 0; 00142 // page 0: create X-Face from image file or address book entry 00143 page = new TQWidget( widgetStack ); 00144 widgetStack->addWidget( page, pageno ); // force sequential numbers (play safe) 00145 page_vlay = new TQVBoxLayout( page, 0, KDialog::spacingHint() ); 00146 hlay = new TQHBoxLayout( page_vlay ); // inherits spacing 00147 mFromFileBtn = new TQPushButton( i18n("Select File..."), page ); 00148 TQWhatsThis::add( mFromFileBtn, 00149 i18n("Use this to select an image file to create the picture from. " 00150 "The image should be of high contrast and nearly quadratic shape. " 00151 "A light background helps improve the result." ) ); 00152 mFromFileBtn->setAutoDefault( false ); 00153 page_vlay->addWidget( mFromFileBtn, 1 ); 00154 connect( mFromFileBtn, TQT_SIGNAL(released()), 00155 this, TQT_SLOT(slotSelectFile()) ); 00156 mFromAddrbkBtn = new TQPushButton( i18n("Set From Address Book"), page ); 00157 TQWhatsThis::add( mFromAddrbkBtn, 00158 i18n( "You can use a scaled-down version of the picture " 00159 "you have set in your address book entry." ) ); 00160 mFromAddrbkBtn->setAutoDefault( false ); 00161 page_vlay->addWidget( mFromAddrbkBtn, 1 ); 00162 connect( mFromAddrbkBtn, TQT_SIGNAL(released()), 00163 this, TQT_SLOT(slotSelectFromAddressbook()) ); 00164 label1 = new TQLabel( i18n("<qt>KMail can send a small (48x48 pixels), low-quality, " 00165 "monochrome picture with every message. " 00166 "For example, this could be a picture of you or a glyph. " 00167 "It is shown in the recipient's mail client (if supported)." ), page ); 00168 label1->setAlignment( TQLabel::WordBreak | TQLabel::AlignVCenter ); 00169 page_vlay->addWidget( label1 ); 00170 00171 widgetStack->raiseWidget( 0 ); // since mSourceCombo->currentItem() == 0 00172 00173 // page 1: input field for direct entering 00174 ++pageno; 00175 page = new TQWidget( widgetStack ); 00176 widgetStack->addWidget( page, pageno ); 00177 page_vlay = new TQVBoxLayout( page, 0, KDialog::spacingHint() ); 00178 mTextEdit = new TQTextEdit( page ); 00179 page_vlay->addWidget( mTextEdit ); 00180 TQWhatsThis::add( mTextEdit, i18n( "Use this field to enter an arbitrary X-Face string." ) ); 00181 mTextEdit->setFont( KGlobalSettings::fixedFont() ); 00182 mTextEdit->setWrapPolicy( TQTextEdit::Anywhere ); 00183 mTextEdit->setTextFormat( TQt::PlainText ); 00184 label2 = new KActiveLabel( i18n("Examples are available at <a href=\"http://www.xs4all.nl/~ace/X-Faces/\">http://www.xs4all.nl/~ace/X-Faces/</a>."), page ); 00185 page_vlay->addWidget( label2 ); 00186 00187 00188 connect(mTextEdit, TQT_SIGNAL(textChanged()), this, TQT_SLOT(slotUpdateXFace())); 00189 } 00190 00191 XFaceConfigurator::~XFaceConfigurator() { 00192 00193 } 00194 00195 bool XFaceConfigurator::isXFaceEnabled() const { 00196 return mEnableCheck->isChecked(); 00197 } 00198 00199 void XFaceConfigurator::setXFaceEnabled( bool enable ) { 00200 mEnableCheck->setChecked( enable ); 00201 } 00202 00203 TQString XFaceConfigurator::xface() const { 00204 return mTextEdit->text(); 00205 } 00206 00207 void XFaceConfigurator::setXFace( const TQString & text ) { 00208 mTextEdit->setText( text ); 00209 } 00210 00211 void XFaceConfigurator::setXfaceFromFile( const KURL &url ) 00212 { 00213 TQString tmpFile; 00214 if( KIO::NetAccess::download( url, tmpFile, this ) ) 00215 { 00216 KXFace xf; 00217 mTextEdit->setText( xf.fromImage( TQImage( tmpFile ) ) ); 00218 KIO::NetAccess::removeTempFile( tmpFile ); 00219 } else { 00220 KMessageBox::error(this, KIO::NetAccess::lastErrorString() ); 00221 } 00222 } 00223 00224 void XFaceConfigurator::slotSelectFile() 00225 { 00226 TQStringList mimeTypes = KImageIO::mimeTypes (KImageIO::Reading); 00227 TQString filter = mimeTypes.join (" "); 00228 KURL url = KFileDialog::getOpenURL( TQString(), filter, this, TQString() ); 00229 if ( !url.isEmpty() ) 00230 setXfaceFromFile( url ); 00231 } 00232 00233 void XFaceConfigurator::slotSelectFromAddressbook() 00234 { 00235 StdAddressBook *ab = StdAddressBook::self( true ); 00236 Addressee me = ab->whoAmI(); 00237 if ( !me.isEmpty() ) 00238 { 00239 if ( me.photo().isIntern() ) 00240 { 00241 TQImage photo = me.photo().data(); 00242 if ( !photo.isNull() ) 00243 { 00244 KXFace xf; 00245 mTextEdit->setText( xf.fromImage( photo ) ); 00246 } 00247 else 00248 KMessageBox::information( this, i18n("No picture set for your address book entry."), i18n("No Picture") ); 00249 00250 } 00251 else 00252 { 00253 KURL url = me.photo().url(); 00254 if( !url.isEmpty() ) 00255 setXfaceFromFile( url ); 00256 else 00257 KMessageBox::information( this, i18n("No picture set for your address book entry."), i18n("No Picture") ); 00258 } 00259 } 00260 else 00261 KMessageBox::information( this, i18n("You do not have your own contact defined in the address book."), i18n("No Picture") ); 00262 } 00263 00264 void XFaceConfigurator::slotUpdateXFace() 00265 { 00266 TQString str = mTextEdit->text(); 00267 if ( !str.isEmpty() ) 00268 { 00269 if ( str.startsWith("x-face:", false) ) 00270 { 00271 str = str.remove("x-face:", false); 00272 mTextEdit->setText(str); 00273 } 00274 KXFace xf; 00275 TQPixmap p( 48, 48, true ); 00276 p.convertFromImage( xf.toImage(str) ); 00277 mXFaceLabel->setPixmap( p ); 00278 } 00279 else 00280 mXFaceLabel->setPixmap( 0L ); 00281 } 00282 00283 } // namespace KMail 00284 00285 #include "xfaceconfigurator.moc"