summaryrefslogtreecommitdiffstats
path: root/kaddressbook/distributionlistpicker.cpp
blob: aa59fe217e9eafaa77b2ee71b108ea57f0a74f98 (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
/*
    This file is part of KAddressBook.
    Copyright (c) 2007 Klaralvdalens Datakonsult AB <frank@kdab.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 "distributionlistpicker.h"
#include "config.h"

#ifdef TDEPIM_NEW_DISTRLISTS
#include <libtdepim/distributionlist.h>
#endif

#include <kabc/addressbook.h>

#include <kapplication.h>
#include <kinputdialog.h>
#include <klistbox.h>
#include <klocale.h>
#include <kmessagebox.h>

#include <tqlabel.h>
#include <tqlayout.h>
#include <tqpushbutton.h>
#include <tqregexp.h>
#include <tqvalidator.h>

KPIM::DistributionListPickerDialog::DistributionListPickerDialog( KABC::AddressBook* book, TQWidget* parent ) : KDialogBase( parent, 0, true, TQString(), Ok | Cancel | User1 ), m_book( book )
{
    Q_ASSERT( m_book );
    setModal( true );
    enableButton( Ok, false );
    setButtonText( User1, i18n( "Add New Distribution List" ) );
    TQWidget* main = new TQWidget( this );
    TQGridLayout* tqlayout = new TQGridLayout( main );
    tqlayout->setSpacing( KDialog::spacingHint() );
    m_label = new TQLabel( main );
    tqlayout->addWidget( m_label, 0, 0 );
    m_listBox = new KListBox( main );
    tqlayout->addWidget( m_listBox, 1, 0 );
    connect( m_listBox, TQT_SIGNAL( highlighted( const TQString& ) ),
             this, TQT_SLOT( entrySelected( const TQString& ) ) ); 
    connect( m_listBox, TQT_SIGNAL( selected( const TQString& ) ),
             this, TQT_SLOT( entrySelected( const TQString& ) ) ); 
    setMainWidget( main );
#ifdef TDEPIM_NEW_DISTRLISTS
    typedef TQValueList<KPIM::DistributionList> DistListList;
    const DistListList lists = KPIM::DistributionList::allDistributionLists( m_book );
    for ( DistListList::ConstIterator it = lists.begin(); it != lists.end(); ++it )
    {
        m_listBox->insertItem ( (*it).name() );
    }
#endif
}

void KPIM::DistributionListPickerDialog::entrySelected( const TQString& name )
{
    actionButton( Ok )->setEnabled( !name.isNull() );
}

void KPIM::DistributionListPickerDialog::setLabelText( const TQString& text )
{
    m_label->setText( text );
}

void KPIM::DistributionListPickerDialog::slotUser1()
{
#ifdef TDEPIM_NEW_DISTRLISTS
    const TQValueList<KPIM::DistributionList> lists = KPIM::DistributionList::allDistributionLists( m_book );
    TQStringList listNames;
    for ( TQValueList<KPIM::DistributionList>::ConstIterator it = lists.begin(); it != lists.end(); ++it ) 
    {
        listNames += (*it).name();
    }

    bool validName = false;
    do
    {
        TQRegExpValidator validator( TQRegExp( "\\S+.*" ), 0 );
        const TQString name = KInputDialog::getText( i18n( "Enter Name" ), i18n( "Enter a name for the new distribution list:" ), TQString(), 0, this, 0, &validator ).stripWhiteSpace();
        if ( name.isEmpty() )
            return;

        validName = !listNames.contains( name );

        if ( validName )
        {
            KPIM::DistributionList list;
            list.setName( name );
            list.setUid( KApplication::randomString( 10 ) );
            m_book->insertAddressee( list );

            m_listBox->insertItem( name );
            TQListBoxItem* item = m_listBox->findItem( name );
            m_listBox->setSelected( item, true );
        }
        else
        {
            KMessageBox::error( this, i18n( "A distribution list with the the name %1 already exists. Please choose another name" ).arg( name ), i18n( "Name Exists" ) );
        }
    }
    while ( !validName );
#endif
}


void KPIM::DistributionListPickerDialog::slotOk()
{
    TQListBoxItem* item = m_listBox->selectedItem();
    m_selectedDistributionList = item ? item->text() : TQString();
    KDialogBase::slotOk();
}

void KPIM::DistributionListPickerDialog::slotCancel()
{
    m_selectedDistributionList = TQString();
    KDialogBase::slotCancel();
}

TQString KPIM::DistributionListPickerDialog::selectedDistributionList() const
{
    return m_selectedDistributionList;
}

#include "distributionlistpicker.moc"