summaryrefslogtreecommitdiffstats
path: root/kontact/src/profiledialog.cpp
blob: c58377806d50ca6da037d9f84dba66a10e5f5b7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
    This file is part of KDE Kontact.

    Copyright (c) 2007 Frank Osterfeld <frank.osterfeld@kdemail.net>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

    As a special exception, permission is given to link this program
    with any edition of TQt, and distribute the resulting executable,
    without including the source code for TQt in the source distribution.
*/

#include "profiledialog.h"
#include "profilemanager.h"

#include <kfiledialog.h>
#include <klistview.h>
#include <klocale.h>
#include <kmessagebox.h>

#include <tqlayout.h>
#include <tqpushbutton.h>
#include <tqstring.h>

Kontact::ProfileDialog::ProfileDialog( TQWidget* parent, WFlags flags ) : KDialogBase( parent, /*name=*/0, /*modal=*/true, /*caption=*/TQString(), /*buttonMask=*/KDialogBase::Ok|KDialogBase::Close )
{
    setWFlags( flags );
    setCaption( i18n("Configure Profiles") );
    setButtonOK( i18n("Load Profile") );

    TQWidget* mainWidget = new TQWidget( this );

    TQHBoxLayout* horizontalLayout = new TQHBoxLayout( mainWidget );
    horizontalLayout->setSpacing( 5 );

    m_list = new KListView( mainWidget );
    m_list->addColumn( i18n("Name") );
    m_list->addColumn( i18n("Description") );
    m_list->setSelectionMode( TQListView::Single );
    m_list->setItemsRenameable( true );
    m_list->setRenameable( NameColumn, true );
    m_list->setRenameable( DescriptionColumn, true );

    connect( m_list, TQT_SIGNAL( selectionChanged() ), 
             this, TQT_SLOT( listSelectionChanged() ) );
    connect( m_list, TQT_SIGNAL( itemRenamed( TQListViewItem*, const TQString&, int ) ),
             this, TQT_SLOT( listItemRenamed( TQListViewItem*, const TQString&, int ) ) );
    horizontalLayout->addWidget( m_list );

    TQVBoxLayout* buttonLayout = new TQVBoxLayout( horizontalLayout );
    buttonLayout->setSpacing( 5 );

    m_newProfileButton = new TQPushButton( mainWidget );
    m_newProfileButton->setText( i18n("New Profile") );
    connect( m_newProfileButton, TQT_SIGNAL( clicked() ),
             this, TQT_SLOT( addNewProfile() ) );
    buttonLayout->addWidget( m_newProfileButton );

    m_deleteProfileButton = new TQPushButton( mainWidget );
    m_deleteProfileButton->setText( i18n("Delete Profile") );
    m_deleteProfileButton->setEnabled( false );
    connect( m_deleteProfileButton, TQT_SIGNAL( clicked() ),
             this, TQT_SLOT( deleteSelectedProfile() ) );
    buttonLayout->addWidget( m_deleteProfileButton );

    m_saveProfileButton = new TQPushButton( mainWidget );
    m_saveProfileButton->setText( i18n("Save Profile") );
    m_saveProfileButton->setEnabled( false );
    connect( m_saveProfileButton, TQT_SIGNAL( clicked() ),
             this, TQT_SLOT( saveToSelectedProfile() ) );
    buttonLayout->addWidget( m_saveProfileButton );

    buttonLayout->addStretch();

    m_importProfileButton = new TQPushButton( mainWidget );
    m_importProfileButton->setText( i18n("Import Profile") );
    connect( m_importProfileButton, TQT_SIGNAL( clicked() ),
             this, TQT_SLOT( importProfile() ) );
    buttonLayout->addWidget( m_importProfileButton );

    m_exportProfileButton = new TQPushButton( mainWidget );
    m_exportProfileButton->setText( i18n("Export Profile") );
    m_exportProfileButton->setEnabled( false );
    connect( m_exportProfileButton, TQT_SIGNAL( clicked() ),
             this, TQT_SLOT( exportSelectedProfile() ) );
    buttonLayout->addWidget( m_exportProfileButton );

    setMainWidget( mainWidget );

    connect( Kontact::ProfileManager::self(), TQT_SIGNAL( profileAdded( const TQString& ) ), 
             this, TQT_SLOT( profileAdded( const TQString& ) ) );
    connect( Kontact::ProfileManager::self(), TQT_SIGNAL( profileRemoved( const TQString& ) ), 
             this, TQT_SLOT( profileRemoved( const TQString& ) ) );
    connect( Kontact::ProfileManager::self(), TQT_SIGNAL( profileLoaded( const TQString& ) ), 
             this, TQT_SLOT( profileLoaded( const TQString& ) ) );
    connect( Kontact::ProfileManager::self(), TQT_SIGNAL( profileUpdated( const TQString& ) ), 
             this, TQT_SLOT( profileUpdated( const TQString& ) ) );

    const TQValueList<Kontact::Profile> profiles = Kontact::ProfileManager::self()->profiles();
    for ( TQValueList<Kontact::Profile>::ConstIterator it = profiles.begin(), end = profiles.end(); it != end; ++it )
    {
        profileAdded( (*it).id() );
    }
    updateButtonState();
}

void Kontact::ProfileDialog::slotOk()
{
    loadSelectedProfile();
    KDialogBase::slotOk();
}

TQString Kontact::ProfileDialog::selectedProfile() const
{
    return m_itemToProfile[m_list->selectedItem()];
}

void Kontact::ProfileDialog::loadSelectedProfile()
{
    const Kontact::Profile profile = Kontact::ProfileManager::self()->profileById( selectedProfile() );
    if ( profile.isNull() )
        return;
    Kontact::ProfileManager::self()->loadProfile( profile.id() );
}

void Kontact::ProfileDialog::profileLoaded( const TQString& id )
{
    const Kontact::Profile profile = Kontact::ProfileManager::self()->profileById( id );
    if ( profile.isNull() )
        return;
    KMessageBox::information( this, i18n("The profile \"%1\" was successfully loaded. Some profile settings require a restart to get activated.").tqarg( profile.name() ), i18n("Profile Loaded") );
}

void Kontact::ProfileDialog::saveToSelectedProfile()
{
    const Kontact::Profile profile = Kontact::ProfileManager::self()->profileById( selectedProfile() );
    if ( profile.isNull() )
        return;
    if ( KMessageBox::Yes != KMessageBox::warningYesNo( this, i18n("The profile \"%1\" will be overwritten with the current settings. Are you sure?").tqarg( profile.name() ), i18n("Save to Profile"), KStdGuiItem::overwrite(), KStdGuiItem::cancel() ) )
        return;
    Kontact::ProfileManager::self()->saveToProfile( profile.id() );
}

void Kontact::ProfileDialog::deleteSelectedProfile()
{
    const Kontact::Profile profile = Kontact::ProfileManager::self()->profileById( selectedProfile() );
    if ( profile.isNull() )
        return;
    if ( KMessageBox::Yes != KMessageBox::warningYesNo( this, i18n("Do you really want to delete the profile \"%1\"? All profile settings will be lost!").tqarg( profile.name() ), i18n("Delete Profile"), KStdGuiItem::del(), KStdGuiItem::cancel() ) )
        return;
    Kontact::ProfileManager::self()->removeProfile( profile );
}

void Kontact::ProfileDialog::exportSelectedProfile()
{
    const TQString id = selectedProfile();
    const Kontact::Profile profile = Kontact::ProfileManager::self()->profileById( id );
    if ( profile.isNull() )
        return;
    const TQString path = KFileDialog::getExistingDirectory( TQString(), this, i18n("Select Profile Folder") );
    if ( path.isNull() )
        return;
    const Kontact::ProfileManager::ExportError error = Kontact::ProfileManager::self()->exportProfileToDirectory( id, path );
    if ( error == Kontact::ProfileManager::SuccessfulExport )
    {
        KMessageBox::information( this, i18n("The profile \"%1\" was successfully exported.").tqarg( profile.name() ), i18n("Profile Exported") );
    }
    else
    {
        // TODO print error
    }
}

void Kontact::ProfileDialog::importProfile()
{
    const TQString path = KFileDialog::getExistingDirectory( TQString(), this, i18n("Select Profile Folder") );
    if ( path.isNull() )
        return;
    const Kontact::ProfileManager::ImportError error = Kontact::ProfileManager::self()->importProfileFromDirectory( path );
    if ( error != Kontact::ProfileManager::SuccessfulImport )
    {
        // TODO print error
    }
}

void Kontact::ProfileDialog::profileAdded( const TQString& id )
{
    Q_ASSERT( !m_profileToItem[id] );
    const Kontact::Profile profile = Kontact::ProfileManager::self()->profileById( id );
    Q_ASSERT( !profile.isNull() );
    TQListViewItem* const item = new TQListViewItem( m_list );
    m_profileToItem[id] = item;
    m_itemToProfile[item] = id;
    profileUpdated( id );
}

void Kontact::ProfileDialog::profileRemoved( const TQString& id )
{
    TQListViewItem* item = m_profileToItem[id];
    Q_ASSERT( item );
    m_profileToItem.remove( id );
    m_itemToProfile.remove( item );
    delete item;
}

void Kontact::ProfileDialog::profileUpdated( const TQString& id )
{
    TQListViewItem* item = m_profileToItem[id];
    Q_ASSERT( item );
    const Kontact::Profile profile = Kontact::ProfileManager::self()->profileById( id );
    Q_ASSERT( !profile.isNull() );
    item->setText( NameColumn, profile.name() );
    item->setText( DescriptionColumn, profile.description() );
}

void Kontact::ProfileDialog::addNewProfile()
{
    Kontact::Profile profile( Kontact::ProfileManager::self()->generateNewId(), true );
    profile.setName( i18n("New profile") );
    profile.setDescription( i18n("Enter description") );
    Kontact::ProfileManager::self()->addProfile( profile );
}

void Kontact::ProfileDialog::listItemRenamed( TQListViewItem* item, const TQString& text, int col )
{
    Kontact::Profile profile = Kontact::ProfileManager::self()->profileById( m_itemToProfile[item] );
    Q_ASSERT( !profile.isNull() );
    switch ( col )
    {
        case NameColumn:
            profile.setName( text );
            Kontact::ProfileManager::self()->updateProfile( profile );
            break;
        case DescriptionColumn:
            profile.setDescription( text );
            Kontact::ProfileManager::self()->updateProfile( profile );
            break;
    }
}

void Kontact::ProfileDialog::updateButtonState()
{
    const bool hasSelection = m_list->selectedItem() != 0;
    m_deleteProfileButton->setEnabled( hasSelection );
    m_saveProfileButton->setEnabled( hasSelection);
    actionButton( KDialogBase::Ok )->setEnabled( hasSelection );
    m_exportProfileButton->setEnabled( hasSelection );
}

void Kontact::ProfileDialog::listSelectionChanged()
{
    updateButtonState();
}

#include "profiledialog.moc"