kontact

profiledialog.cpp

00001 /*
00002     This file is part of KDE Kontact.
00003 
00004     Copyright (c) 2007 Frank Osterfeld <frank.osterfeld@kdemail.net>
00005 
00006     This program is free software; you can redistribute it and/or modify
00007     it under the terms of the GNU General Public License as published by
00008     the Free Software Foundation; either version 2 of the License, or
00009     (at your option) any later version.
00010 
00011     This program 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
00014     GNU General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License
00017     along with this program; if not, write to the Free Software
00018     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019 
00020     As a special exception, permission is given to link this program
00021     with any edition of Qt, and distribute the resulting executable,
00022     without including the source code for Qt in the source distribution.
00023 */
00024 
00025 #include "profiledialog.h"
00026 #include "profilemanager.h"
00027 
00028 #include <kfiledialog.h>
00029 #include <klistview.h>
00030 #include <klocale.h>
00031 #include <kmessagebox.h>
00032 
00033 #include <qlayout.h>
00034 #include <qpushbutton.h>
00035 #include <qstring.h>
00036 
00037 Kontact::ProfileDialog::ProfileDialog( QWidget* parent, WFlags flags ) : KDialogBase( parent, /*name=*/0, /*modal=*/true, /*caption=*/QString(), /*buttonMask=*/KDialogBase::Ok|KDialogBase::Close )
00038 {
00039     setWFlags( flags );
00040     setCaption( i18n("Configure Profiles") );
00041     setButtonOK( i18n("Load Profile") );
00042 
00043     QWidget* mainWidget = new QWidget( this );
00044 
00045     QHBoxLayout* horizontalLayout = new QHBoxLayout( mainWidget );
00046     horizontalLayout->setSpacing( 5 );
00047 
00048     m_list = new KListView( mainWidget );
00049     m_list->addColumn( i18n("Name") );
00050     m_list->addColumn( i18n("Description") );
00051     m_list->setSelectionMode( QListView::Single );
00052     m_list->setItemsRenameable( true );
00053     m_list->setRenameable( NameColumn, true );
00054     m_list->setRenameable( DescriptionColumn, true );
00055 
00056     connect( m_list, SIGNAL( selectionChanged() ), 
00057              this, SLOT( listSelectionChanged() ) );
00058     connect( m_list, SIGNAL( itemRenamed( QListViewItem*, const QString&, int ) ),
00059              this, SLOT( listItemRenamed( QListViewItem*, const QString&, int ) ) );
00060     horizontalLayout->addWidget( m_list );
00061 
00062     QVBoxLayout* buttonLayout = new QVBoxLayout( horizontalLayout );
00063     buttonLayout->setSpacing( 5 );
00064 
00065     m_newProfileButton = new QPushButton( mainWidget );
00066     m_newProfileButton->setText( i18n("New Profile") );
00067     connect( m_newProfileButton, SIGNAL( clicked() ),
00068              this, SLOT( addNewProfile() ) );
00069     buttonLayout->addWidget( m_newProfileButton );
00070 
00071     m_deleteProfileButton = new QPushButton( mainWidget );
00072     m_deleteProfileButton->setText( i18n("Delete Profile") );
00073     m_deleteProfileButton->setEnabled( false );
00074     connect( m_deleteProfileButton, SIGNAL( clicked() ),
00075              this, SLOT( deleteSelectedProfile() ) );
00076     buttonLayout->addWidget( m_deleteProfileButton );
00077 
00078     m_saveProfileButton = new QPushButton( mainWidget );
00079     m_saveProfileButton->setText( i18n("Save Profile") );
00080     m_saveProfileButton->setEnabled( false );
00081     connect( m_saveProfileButton, SIGNAL( clicked() ),
00082              this, SLOT( saveToSelectedProfile() ) );
00083     buttonLayout->addWidget( m_saveProfileButton );
00084 
00085     buttonLayout->addStretch();
00086 
00087     m_importProfileButton = new QPushButton( mainWidget );
00088     m_importProfileButton->setText( i18n("Import Profile") );
00089     connect( m_importProfileButton, SIGNAL( clicked() ),
00090              this, SLOT( importProfile() ) );
00091     buttonLayout->addWidget( m_importProfileButton );
00092 
00093     m_exportProfileButton = new QPushButton( mainWidget );
00094     m_exportProfileButton->setText( i18n("Export Profile") );
00095     m_exportProfileButton->setEnabled( false );
00096     connect( m_exportProfileButton, SIGNAL( clicked() ),
00097              this, SLOT( exportSelectedProfile() ) );
00098     buttonLayout->addWidget( m_exportProfileButton );
00099 
00100     setMainWidget( mainWidget );
00101 
00102     connect( Kontact::ProfileManager::self(), SIGNAL( profileAdded( const QString& ) ), 
00103              this, SLOT( profileAdded( const QString& ) ) );
00104     connect( Kontact::ProfileManager::self(), SIGNAL( profileRemoved( const QString& ) ), 
00105              this, SLOT( profileRemoved( const QString& ) ) );
00106     connect( Kontact::ProfileManager::self(), SIGNAL( profileLoaded( const QString& ) ), 
00107              this, SLOT( profileLoaded( const QString& ) ) );
00108     connect( Kontact::ProfileManager::self(), SIGNAL( profileUpdated( const QString& ) ), 
00109              this, SLOT( profileUpdated( const QString& ) ) );
00110 
00111     const QValueList<Kontact::Profile> profiles = Kontact::ProfileManager::self()->profiles();
00112     for ( QValueList<Kontact::Profile>::ConstIterator it = profiles.begin(), end = profiles.end(); it != end; ++it )
00113     {
00114         profileAdded( (*it).id() );
00115     }
00116     updateButtonState();
00117 }
00118 
00119 void Kontact::ProfileDialog::slotOk()
00120 {
00121     loadSelectedProfile();
00122     KDialogBase::slotOk();
00123 }
00124 
00125 QString Kontact::ProfileDialog::selectedProfile() const
00126 {
00127     return m_itemToProfile[m_list->selectedItem()];
00128 }
00129 
00130 void Kontact::ProfileDialog::loadSelectedProfile()
00131 {
00132     const Kontact::Profile profile = Kontact::ProfileManager::self()->profileById( selectedProfile() );
00133     if ( profile.isNull() )
00134         return;
00135     Kontact::ProfileManager::self()->loadProfile( profile.id() );
00136 }
00137 
00138 void Kontact::ProfileDialog::profileLoaded( const QString& id )
00139 {
00140     const Kontact::Profile profile = Kontact::ProfileManager::self()->profileById( id );
00141     if ( profile.isNull() )
00142         return;
00143     KMessageBox::information( this, i18n("The profile \"%1\" was successfully loaded. Some profile settings require a restart to get activated.").arg( profile.name() ), i18n("Profile Loaded") );
00144 }
00145 
00146 void Kontact::ProfileDialog::saveToSelectedProfile()
00147 {
00148     const Kontact::Profile profile = Kontact::ProfileManager::self()->profileById( selectedProfile() );
00149     if ( profile.isNull() )
00150         return;
00151     if ( KMessageBox::Yes != KMessageBox::warningYesNo( this, i18n("The profile \"%1\" will be overwritten with the current settings. Are you sure?").arg( profile.name() ), i18n("Save to Profile"), KStdGuiItem::overwrite(), KStdGuiItem::cancel() ) )
00152         return;
00153     Kontact::ProfileManager::self()->saveToProfile( profile.id() );
00154 }
00155 
00156 void Kontact::ProfileDialog::deleteSelectedProfile()
00157 {
00158     const Kontact::Profile profile = Kontact::ProfileManager::self()->profileById( selectedProfile() );
00159     if ( profile.isNull() )
00160         return;
00161     if ( KMessageBox::Yes != KMessageBox::warningYesNo( this, i18n("Do you really want to delete the profile \"%1\"? All profile settings will be lost!").arg( profile.name() ), i18n("Delete Profile"), KStdGuiItem::del(), KStdGuiItem::cancel() ) )
00162         return;
00163     Kontact::ProfileManager::self()->removeProfile( profile );
00164 }
00165 
00166 void Kontact::ProfileDialog::exportSelectedProfile()
00167 {
00168     const QString id = selectedProfile();
00169     const Kontact::Profile profile = Kontact::ProfileManager::self()->profileById( id );
00170     if ( profile.isNull() )
00171         return;
00172     const QString path = KFileDialog::getExistingDirectory( QString(), this, i18n("Select Profile Folder") );
00173     if ( path.isNull() )
00174         return;
00175     const Kontact::ProfileManager::ExportError error = Kontact::ProfileManager::self()->exportProfileToDirectory( id, path );
00176     if ( error == Kontact::ProfileManager::SuccessfulExport )
00177     {
00178         KMessageBox::information( this, i18n("The profile \"%1\" was successfully exported.").arg( profile.name() ), i18n("Profile Exported") );
00179     }
00180     else
00181     {
00182         // TODO print error
00183     }
00184 }
00185 
00186 void Kontact::ProfileDialog::importProfile()
00187 {
00188     const QString path = KFileDialog::getExistingDirectory( QString(), this, i18n("Select Profile Folder") );
00189     if ( path.isNull() )
00190         return;
00191     const Kontact::ProfileManager::ImportError error = Kontact::ProfileManager::self()->importProfileFromDirectory( path );
00192     if ( error != Kontact::ProfileManager::SuccessfulImport )
00193     {
00194         // TODO print error
00195     }
00196 }
00197 
00198 void Kontact::ProfileDialog::profileAdded( const QString& id )
00199 {
00200     Q_ASSERT( !m_profileToItem[id] );
00201     const Kontact::Profile profile = Kontact::ProfileManager::self()->profileById( id );
00202     Q_ASSERT( !profile.isNull() );
00203     QListViewItem* const item = new QListViewItem( m_list );
00204     m_profileToItem[id] = item;
00205     m_itemToProfile[item] = id;
00206     profileUpdated( id );
00207 }
00208 
00209 void Kontact::ProfileDialog::profileRemoved( const QString& id )
00210 {
00211     QListViewItem* item = m_profileToItem[id];
00212     Q_ASSERT( item );
00213     m_profileToItem.remove( id );
00214     m_itemToProfile.remove( item );
00215     delete item;
00216 }
00217 
00218 void Kontact::ProfileDialog::profileUpdated( const QString& id )
00219 {
00220     QListViewItem* item = m_profileToItem[id];
00221     Q_ASSERT( item );
00222     const Kontact::Profile profile = Kontact::ProfileManager::self()->profileById( id );
00223     Q_ASSERT( !profile.isNull() );
00224     item->setText( NameColumn, profile.name() );
00225     item->setText( DescriptionColumn, profile.description() );
00226 }
00227 
00228 void Kontact::ProfileDialog::addNewProfile()
00229 {
00230     Kontact::Profile profile( Kontact::ProfileManager::self()->generateNewId(), true );
00231     profile.setName( i18n("New profile") );
00232     profile.setDescription( i18n("Enter description") );
00233     Kontact::ProfileManager::self()->addProfile( profile );
00234 }
00235 
00236 void Kontact::ProfileDialog::listItemRenamed( QListViewItem* item, const QString& text, int col )
00237 {
00238     Kontact::Profile profile = Kontact::ProfileManager::self()->profileById( m_itemToProfile[item] );
00239     Q_ASSERT( !profile.isNull() );
00240     switch ( col )
00241     {
00242         case NameColumn:
00243             profile.setName( text );
00244             Kontact::ProfileManager::self()->updateProfile( profile );
00245             break;
00246         case DescriptionColumn:
00247             profile.setDescription( text );
00248             Kontact::ProfileManager::self()->updateProfile( profile );
00249             break;
00250     }
00251 }
00252 
00253 void Kontact::ProfileDialog::updateButtonState()
00254 {
00255     const bool hasSelection = m_list->selectedItem() != 0;
00256     m_deleteProfileButton->setEnabled( hasSelection );
00257     m_saveProfileButton->setEnabled( hasSelection);
00258     actionButton( KDialogBase::Ok )->setEnabled( hasSelection );
00259     m_exportProfileButton->setEnabled( hasSelection );
00260 }
00261 
00262 void Kontact::ProfileDialog::listSelectionChanged()
00263 {
00264     updateButtonState();
00265 }
00266 
00267 #include "profiledialog.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys