Fix crash if key bindings specified in profile cannot be found. Improve
[personal-kdebase.git] / apps / kdepasswd / kcm / chfacedlg.cpp
blobfb3902e306b9623817192d6456877e0ca1e4f37c
1 /**
2 * Copyright 2003 Braden MacDonald <bradenm_k@shaw.ca>
3 * Copyright 2003 Ravikiran Rajagopal <ravi@ee.eng.ohio-state.edu>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
19 * Please see the README
23 /**
24 * @file UserInfo's Dialog for changing your face.
25 * @author Braden MacDonald
28 #include "chfacedlg.h"
30 #include <QtGui/QLayout>
31 #include <QtGui/QLabel>
32 #include <QtGui/QPixmap>
33 #include <QtGui/QImage>
34 #include <QtGui/QPushButton>
35 #include <QtCore/QDir>
36 #include <QtGui/QCheckBox>
38 #include <klocale.h>
39 #include <kfiledialog.h>
40 #include <kimagefilepreview.h>
41 #include <kimageio.h>
42 #include <kmessagebox.h>
43 #include <konq_operations.h>
44 #include <kurl.h>
46 #include "settings.h" // KConfigXT
50 /**
51 * TODO: It would be nice if the widget were in a .ui
53 ChFaceDlg::ChFaceDlg(const QString& picsdir, QWidget *parent)
54 : KDialog( parent )
56 setCaption( i18n("Change your Face") );
57 setButtons( Ok|Cancel );
58 setDefaultButton( Ok );
59 showButtonSeparator( true );
61 QWidget *faceDlg = new QWidget;
62 ui.setupUi(faceDlg);
64 setMainWidget(faceDlg);
66 connect( ui.m_FacesWidget, SIGNAL( currentItemChanged( QListWidgetItem *, QListWidgetItem * ) ), SLOT( slotFaceWidgetSelectionChanged( QListWidgetItem * ) ) );
68 connect( ui.m_FacesWidget, SIGNAL( doubleClicked( const QModelIndex & ) ), SLOT(accept()) );
69 connect( this, SIGNAL(okClicked()), this, SLOT(accept()));
71 connect( ui.browseBtn, SIGNAL( clicked() ), SLOT( slotGetCustomImage() ) );
73 #if 0
74 QPushButton *acquireBtn = new QPushButton( i18n("&Acquire Image..."), page );
75 acquireBtn->setEnabled( false );
76 morePics->addWidget( acquireBtn );
77 #endif
79 // Filling the icon view
80 QDir facesDir( picsdir );
81 if ( facesDir.exists() )
83 const QStringList picslist = facesDir.entryList( QDir::Files );
84 for ( QStringList::const_iterator it = picslist.constBegin(); it != picslist.constEnd(); ++it )
85 new QListWidgetItem( QIcon( picsdir + *it ), (*it).section(".",0,0), ui.m_FacesWidget );
87 facesDir.setPath( KCFGUserAccount::userFaceDir() );
88 if ( facesDir.exists() )
90 const QStringList picslist = facesDir.entryList( QDir::Files );
91 for ( QStringList::const_iterator it = picslist.constBegin(); it != picslist.constEnd(); ++it )
92 new QListWidgetItem( QIcon( KCFGUserAccount::userFaceDir() + *it ),
93 '/'+(*it) == KCFGUserAccount::customFaceFile() ?
94 i18n("(Custom)") : (*it).section(".",0,0),
95 ui.m_FacesWidget );
99 enableButtonOk( false );
100 //connect( this, SIGNAL( okClicked() ), SLOT( slotSaveCustomImage() ) );
102 resize( 420, 400 );
105 void ChFaceDlg::addCustomPixmap( const QString &imPath, bool saveCopy )
107 QImage pix( imPath );
108 // TODO: save pix to TMPDIR/userinfo-tmp,
109 // then scale and copy *that* to ~/.faces
111 if (pix.isNull())
113 KMessageBox::sorry( this, i18n("There was an error loading the image.") );
114 return;
116 if ( (pix.width() > KCFGUserAccount::faceSize())
117 || (pix.height() > KCFGUserAccount::faceSize()) )
118 pix = pix.scaled( KCFGUserAccount::faceSize(), KCFGUserAccount::faceSize(), Qt::KeepAspectRatio );// Should be no bigger than certain size.
120 if ( saveCopy )
122 // If we should save a copy:
123 QDir userfaces( KCFGUserAccount::userFaceDir() );
124 if ( !userfaces.exists( ) )
125 userfaces.mkdir( userfaces.absolutePath() );
127 pix.save( userfaces.absolutePath() + "/.userinfo-tmp" , "PNG" );
128 KonqOperations::copy( this, KonqOperations::COPY, KUrl::List( KUrl( userfaces.absolutePath() + "/.userinfo-tmp" ) ), KUrl( userfaces.absolutePath() + '/' + QFileInfo(imPath).fileName().section(".",0,0) ) );
129 #if 0
130 if ( !pix.save( userfaces.absolutePath() + '/' + imPath , "PNG" ) )
131 KMessageBox::sorry(this, i18n("There was an error saving the image:\n%1", userfaces.absolutePath() ) );
132 #endif
135 QListWidgetItem* newface = new QListWidgetItem( QIcon(QPixmap::fromImage(pix)), QFileInfo(imPath).fileName().section(".",0,0), ui.m_FacesWidget );
136 ui.m_FacesWidget->scrollToItem( newface );
137 ui.m_FacesWidget->setCurrentItem( newface );
140 void ChFaceDlg::slotGetCustomImage( )
142 QCheckBox* checkWidget = new QCheckBox( i18n("&Save copy in custom faces folder for future use"), 0 );
144 KFileDialog dlg( QDir::homePath(), KImageIO::pattern( KImageIO::Reading ),
145 this, checkWidget);
147 dlg.setOperationMode( KFileDialog::Opening );
148 dlg.setCaption( i18n("Choose Image") );
149 dlg.setMode( KFile::File | KFile::LocalOnly );
151 KImageFilePreview *ip = new KImageFilePreview( &dlg );
152 dlg.setPreviewWidget( ip );
153 if (dlg.exec() == QDialog::Accepted)
154 addCustomPixmap( dlg.selectedFile(), checkWidget->isChecked() );
157 #if 0
158 void ChFaceDlg::slotSaveCustomImage()
160 if ( m_FacesWidget->currentItem()->key() == USER_CUSTOM_KEY)
162 QDir userfaces( QDir::homePath() + USER_FACES_DIR );
163 if ( !userfaces.exists( ) )
164 userfaces.mkdir( userfaces.absolutePath() );
166 if ( !m_FacesWidget->currentItem()->pixmap()->save( userfaces.absolutePath() + USER_CUSTOM_FILE , "PNG" ) )
167 KMessageBox::sorry(this, i18n("There was an error saving the image:\n%1", userfaces.absolutePath() ) );
170 #endif
172 #include "chfacedlg.moc"