kontact

profiledialog.cpp
1/*
2 This file is part of KDE Kontact.
3
4 Copyright (c) 2007 Frank Osterfeld <frank.osterfeld@kdemail.net>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
20 As a special exception, permission is given to link this program
21 with any edition of TQt, and distribute the resulting executable,
22 without including the source code for TQt in the source distribution.
23*/
24
25#include "profiledialog.h"
26#include "profilemanager.h"
27
28#include <tdefiledialog.h>
29#include <tdelistview.h>
30#include <tdelocale.h>
31#include <tdemessagebox.h>
32
33#include <tqlayout.h>
34#include <tqpushbutton.h>
35#include <tqstring.h>
36
37Kontact::ProfileDialog::ProfileDialog( TQWidget* parent, WFlags flags ) : KDialogBase( parent, /*name=*/0, /*modal=*/true, /*caption=*/TQString(), /*buttonMask=*/KDialogBase::Ok|KDialogBase::Close )
38{
39 setWFlags( flags );
40 setCaption( i18n("Configure Profiles") );
41 setButtonOK( i18n("Load Profile") );
42
43 TQWidget* mainWidget = new TQWidget( this );
44
45 TQHBoxLayout* horizontalLayout = new TQHBoxLayout( mainWidget );
46 horizontalLayout->setSpacing( 5 );
47
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 );
55
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 );
61
62 TQVBoxLayout* buttonLayout = new TQVBoxLayout( horizontalLayout );
63 buttonLayout->setSpacing( 5 );
64
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 );
70
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 );
77
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 );
84
85 buttonLayout->addStretch();
86
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 );
92
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 );
99
100 setMainWidget( mainWidget );
101
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& ) ) );
110
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 )
113 {
114 profileAdded( (*it).id() );
115 }
116 updateButtonState();
117}
118
119void Kontact::ProfileDialog::slotOk()
120{
121 loadSelectedProfile();
122 KDialogBase::slotOk();
123}
124
125TQString Kontact::ProfileDialog::selectedProfile() const
126{
127 return m_itemToProfile[m_list->selectedItem()];
128}
129
130void Kontact::ProfileDialog::loadSelectedProfile()
131{
132 const Kontact::Profile profile = Kontact::ProfileManager::self()->profileById( selectedProfile() );
133 if ( profile.isNull() )
134 return;
135 Kontact::ProfileManager::self()->loadProfile( profile.id() );
136}
137
138void Kontact::ProfileDialog::profileLoaded( const TQString& id )
139{
140 const Kontact::Profile profile = Kontact::ProfileManager::self()->profileById( id );
141 if ( profile.isNull() )
142 return;
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") );
144}
145
146void Kontact::ProfileDialog::saveToSelectedProfile()
147{
148 const Kontact::Profile profile = Kontact::ProfileManager::self()->profileById( selectedProfile() );
149 if ( profile.isNull() )
150 return;
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() ) )
152 return;
153 Kontact::ProfileManager::self()->saveToProfile( profile.id() );
154}
155
156void Kontact::ProfileDialog::deleteSelectedProfile()
157{
158 const Kontact::Profile profile = Kontact::ProfileManager::self()->profileById( selectedProfile() );
159 if ( profile.isNull() )
160 return;
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() ) )
162 return;
163 Kontact::ProfileManager::self()->removeProfile( profile );
164}
165
166void Kontact::ProfileDialog::exportSelectedProfile()
167{
168 const TQString id = selectedProfile();
169 const Kontact::Profile profile = Kontact::ProfileManager::self()->profileById( id );
170 if ( profile.isNull() )
171 return;
172 const TQString path = KFileDialog::getExistingDirectory( TQString(), this, i18n("Select Profile Folder") );
173 if ( path.isNull() )
174 return;
175 const Kontact::ProfileManager::ExportError error = Kontact::ProfileManager::self()->exportProfileToDirectory( id, path );
176 if ( error == Kontact::ProfileManager::SuccessfulExport )
177 {
178 KMessageBox::information( this, i18n("The profile \"%1\" was successfully exported.").arg( profile.name() ), i18n("Profile Exported") );
179 }
180 else
181 {
182 // TODO print error
183 }
184}
185
186void Kontact::ProfileDialog::importProfile()
187{
188 const TQString path = KFileDialog::getExistingDirectory( TQString(), this, i18n("Select Profile Folder") );
189 if ( path.isNull() )
190 return;
191 const Kontact::ProfileManager::ImportError error = Kontact::ProfileManager::self()->importProfileFromDirectory( path );
192 if ( error != Kontact::ProfileManager::SuccessfulImport )
193 {
194 // TODO print error
195 }
196}
197
198void Kontact::ProfileDialog::profileAdded( const TQString& id )
199{
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 );
207}
208
209void Kontact::ProfileDialog::profileRemoved( const TQString& id )
210{
211 TQListViewItem* item = m_profileToItem[id];
212 Q_ASSERT( item );
213 m_profileToItem.remove( id );
214 m_itemToProfile.remove( item );
215 delete item;
216}
217
218void Kontact::ProfileDialog::profileUpdated( const TQString& id )
219{
220 TQListViewItem* item = m_profileToItem[id];
221 Q_ASSERT( item );
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() );
226}
227
228void Kontact::ProfileDialog::addNewProfile()
229{
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 );
234}
235
236void Kontact::ProfileDialog::listItemRenamed( TQListViewItem* item, const TQString& text, int col )
237{
238 Kontact::Profile profile = Kontact::ProfileManager::self()->profileById( m_itemToProfile[item] );
239 Q_ASSERT( !profile.isNull() );
240 switch ( col )
241 {
242 case NameColumn:
243 profile.setName( text );
244 Kontact::ProfileManager::self()->updateProfile( profile );
245 break;
246 case DescriptionColumn:
247 profile.setDescription( text );
248 Kontact::ProfileManager::self()->updateProfile( profile );
249 break;
250 }
251}
252
253void Kontact::ProfileDialog::updateButtonState()
254{
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 );
260}
261
262void Kontact::ProfileDialog::listSelectionChanged()
263{
264 updateButtonState();
265}
266
267#include "profiledialog.moc"