25 #include "profiledialog.h"
26 #include "profilemanager.h"
28 #include <tdefiledialog.h>
29 #include <tdelistview.h>
30 #include <tdelocale.h>
31 #include <tdemessagebox.h>
34 #include <tqpushbutton.h>
37 Kontact::ProfileDialog::ProfileDialog( TQWidget* parent, WFlags flags ) : KDialogBase( parent, 0, true, TQString(), KDialogBase::Ok|KDialogBase::Close )
40 setCaption( i18n(
"Configure Profiles") );
41 setButtonOK( i18n(
"Load Profile") );
43 TQWidget* mainWidget =
new TQWidget(
this );
45 TQHBoxLayout* horizontalLayout =
new TQHBoxLayout( mainWidget );
46 horizontalLayout->setSpacing( 5 );
48 m_list =
new TDEListView( mainWidget );
49 m_list->addColumn( i18n(
"Name") );
50 m_list->addColumn( i18n(
"Description") );
51 m_list->setSelectionMode( TQListView::Single );
52 m_list->setItemsRenameable(
true );
53 m_list->setRenameable( NameColumn,
true );
54 m_list->setRenameable( DescriptionColumn,
true );
56 connect( m_list, TQ_SIGNAL( selectionChanged() ),
57 this, TQ_SLOT( listSelectionChanged() ) );
58 connect( m_list, TQ_SIGNAL( itemRenamed( TQListViewItem*,
const TQString&,
int ) ),
59 this, TQ_SLOT( listItemRenamed( TQListViewItem*,
const TQString&,
int ) ) );
60 horizontalLayout->addWidget( m_list );
62 TQVBoxLayout* buttonLayout =
new TQVBoxLayout( horizontalLayout );
63 buttonLayout->setSpacing( 5 );
65 m_newProfileButton =
new TQPushButton( mainWidget );
66 m_newProfileButton->setText( i18n(
"New Profile") );
67 connect( m_newProfileButton, TQ_SIGNAL( clicked() ),
68 this, TQ_SLOT( addNewProfile() ) );
69 buttonLayout->addWidget( m_newProfileButton );
71 m_deleteProfileButton =
new TQPushButton( mainWidget );
72 m_deleteProfileButton->setText( i18n(
"Delete Profile") );
73 m_deleteProfileButton->setEnabled(
false );
74 connect( m_deleteProfileButton, TQ_SIGNAL( clicked() ),
75 this, TQ_SLOT( deleteSelectedProfile() ) );
76 buttonLayout->addWidget( m_deleteProfileButton );
78 m_saveProfileButton =
new TQPushButton( mainWidget );
79 m_saveProfileButton->setText( i18n(
"Save Profile") );
80 m_saveProfileButton->setEnabled(
false );
81 connect( m_saveProfileButton, TQ_SIGNAL( clicked() ),
82 this, TQ_SLOT( saveToSelectedProfile() ) );
83 buttonLayout->addWidget( m_saveProfileButton );
85 buttonLayout->addStretch();
87 m_importProfileButton =
new TQPushButton( mainWidget );
88 m_importProfileButton->setText( i18n(
"Import Profile") );
89 connect( m_importProfileButton, TQ_SIGNAL( clicked() ),
90 this, TQ_SLOT( importProfile() ) );
91 buttonLayout->addWidget( m_importProfileButton );
93 m_exportProfileButton =
new TQPushButton( mainWidget );
94 m_exportProfileButton->setText( i18n(
"Export Profile") );
95 m_exportProfileButton->setEnabled(
false );
96 connect( m_exportProfileButton, TQ_SIGNAL( clicked() ),
97 this, TQ_SLOT( exportSelectedProfile() ) );
98 buttonLayout->addWidget( m_exportProfileButton );
100 setMainWidget( mainWidget );
102 connect( Kontact::ProfileManager::self(), TQ_SIGNAL( profileAdded(
const TQString& ) ),
103 this, TQ_SLOT( profileAdded(
const TQString& ) ) );
104 connect( Kontact::ProfileManager::self(), TQ_SIGNAL( profileRemoved(
const TQString& ) ),
105 this, TQ_SLOT( profileRemoved(
const TQString& ) ) );
106 connect( Kontact::ProfileManager::self(), TQ_SIGNAL( profileLoaded(
const TQString& ) ),
107 this, TQ_SLOT( profileLoaded(
const TQString& ) ) );
108 connect( Kontact::ProfileManager::self(), TQ_SIGNAL( profileUpdated(
const TQString& ) ),
109 this, TQ_SLOT( profileUpdated(
const TQString& ) ) );
111 const TQValueList<Kontact::Profile> profiles = Kontact::ProfileManager::self()->profiles();
112 for ( TQValueList<Kontact::Profile>::ConstIterator it = profiles.begin(), end = profiles.end(); it != end; ++it )
114 profileAdded( (*it).id() );
119 void Kontact::ProfileDialog::slotOk()
121 loadSelectedProfile();
122 KDialogBase::slotOk();
125 TQString Kontact::ProfileDialog::selectedProfile()
const
127 return m_itemToProfile[m_list->selectedItem()];
130 void Kontact::ProfileDialog::loadSelectedProfile()
132 const Kontact::Profile profile = Kontact::ProfileManager::self()->profileById( selectedProfile() );
133 if ( profile.isNull() )
135 Kontact::ProfileManager::self()->loadProfile( profile.id() );
138 void Kontact::ProfileDialog::profileLoaded(
const TQString&
id )
140 const Kontact::Profile profile = Kontact::ProfileManager::self()->profileById(
id );
141 if ( profile.isNull() )
143 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") );
146 void Kontact::ProfileDialog::saveToSelectedProfile()
148 const Kontact::Profile profile = Kontact::ProfileManager::self()->profileById( selectedProfile() );
149 if ( profile.isNull() )
151 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() ) )
153 Kontact::ProfileManager::self()->saveToProfile( profile.id() );
156 void Kontact::ProfileDialog::deleteSelectedProfile()
158 const Kontact::Profile profile = Kontact::ProfileManager::self()->profileById( selectedProfile() );
159 if ( profile.isNull() )
161 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() ) )
163 Kontact::ProfileManager::self()->removeProfile( profile );
166 void Kontact::ProfileDialog::exportSelectedProfile()
168 const TQString
id = selectedProfile();
169 const Kontact::Profile profile = Kontact::ProfileManager::self()->profileById(
id );
170 if ( profile.isNull() )
172 const TQString path = KFileDialog::getExistingDirectory( TQString(),
this, i18n(
"Select Profile Folder") );
175 const Kontact::ProfileManager::ExportError error = Kontact::ProfileManager::self()->exportProfileToDirectory(
id, path );
176 if ( error == Kontact::ProfileManager::SuccessfulExport )
178 KMessageBox::information(
this, i18n(
"The profile \"%1\" was successfully exported.").arg( profile.name() ), i18n(
"Profile Exported") );
186 void Kontact::ProfileDialog::importProfile()
188 const TQString path = KFileDialog::getExistingDirectory( TQString(),
this, i18n(
"Select Profile Folder") );
191 const Kontact::ProfileManager::ImportError error = Kontact::ProfileManager::self()->importProfileFromDirectory( path );
192 if ( error != Kontact::ProfileManager::SuccessfulImport )
198 void Kontact::ProfileDialog::profileAdded(
const TQString&
id )
200 Q_ASSERT( !m_profileToItem[
id] );
201 const Kontact::Profile profile = Kontact::ProfileManager::self()->profileById(
id );
202 Q_ASSERT( !profile.isNull() );
203 TQListViewItem*
const item =
new TQListViewItem( m_list );
204 m_profileToItem[id] = item;
205 m_itemToProfile[item] = id;
206 profileUpdated(
id );
209 void Kontact::ProfileDialog::profileRemoved(
const TQString&
id )
211 TQListViewItem* item = m_profileToItem[id];
213 m_profileToItem.remove(
id );
214 m_itemToProfile.remove( item );
218 void Kontact::ProfileDialog::profileUpdated(
const TQString&
id )
220 TQListViewItem* item = m_profileToItem[id];
222 const Kontact::Profile profile = Kontact::ProfileManager::self()->profileById(
id );
223 Q_ASSERT( !profile.isNull() );
224 item->setText( NameColumn, profile.name() );
225 item->setText( DescriptionColumn, profile.description() );
228 void Kontact::ProfileDialog::addNewProfile()
230 Kontact::Profile profile( Kontact::ProfileManager::self()->generateNewId(),
true );
231 profile.setName( i18n(
"New profile") );
232 profile.setDescription( i18n(
"Enter description") );
233 Kontact::ProfileManager::self()->addProfile( profile );
236 void Kontact::ProfileDialog::listItemRenamed( TQListViewItem* item,
const TQString& text,
int col )
238 Kontact::Profile profile = Kontact::ProfileManager::self()->profileById( m_itemToProfile[item] );
239 Q_ASSERT( !profile.isNull() );
243 profile.setName( text );
244 Kontact::ProfileManager::self()->updateProfile( profile );
246 case DescriptionColumn:
247 profile.setDescription( text );
248 Kontact::ProfileManager::self()->updateProfile( profile );
253 void Kontact::ProfileDialog::updateButtonState()
255 const bool hasSelection = m_list->selectedItem() != 0;
256 m_deleteProfileButton->setEnabled( hasSelection );
257 m_saveProfileButton->setEnabled( hasSelection);
258 actionButton( KDialogBase::Ok )->setEnabled( hasSelection );
259 m_exportProfileButton->setEnabled( hasSelection );
262 void Kontact::ProfileDialog::listSelectionChanged()
267 #include "profiledialog.moc"