~ubuntu-branches/ubuntu/oneiric/kdepim/oneiric-updates

« back to all changes in this revision

Viewing changes to mobile/contacts/editorlocation.cpp

  • Committer: Package Import Robot
  • Author(s): Philip Muškovac
  • Date: 2011-06-28 19:33:24 UTC
  • mfrom: (0.2.13) (0.1.13 sid)
  • Revision ID: package-import@ubuntu.com-20110628193324-8yvjs8sdv9rdoo6c
Tags: 4:4.7.0-0ubuntu1
* New upstream release
  - update install files
  - add missing kdepim-doc package to control file
  - Fix Vcs lines
  - kontact breaks/replaces korganizer << 4:4.6.80
  - tighten the dependency of kdepim-dev on libkdepim4 to fix lintian error

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    Copyright (c) 2010 Tobias Koenig <tokoe@kde.org>
 
3
 
 
4
    This library is free software; you can redistribute it and/or modify it
 
5
    under the terms of the GNU Library General Public License as published by
 
6
    the Free Software Foundation; either version 2 of the License, or (at your
 
7
    option) any later version.
 
8
 
 
9
    This library is distributed in the hope that it will be useful, but WITHOUT
 
10
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 
11
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
 
12
    License for more details.
 
13
 
 
14
    You should have received a copy of the GNU Library General Public License
 
15
    along with this library; see the file COPYING.LIB.  If not, write to the
 
16
    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 
17
    02110-1301, USA.
 
18
*/
 
19
 
 
20
#include "editorlocation.h"
 
21
 
 
22
#include "locationmodel.h"
 
23
#include "ui_editorlocation.h"
 
24
 
 
25
#include <KDE/KABC/Addressee>
 
26
#include <KDE/KMessageBox>
 
27
 
 
28
#include <QtGui/QCheckBox>
 
29
#include <QtGui/QDataWidgetMapper>
 
30
#include <QtGui/QGroupBox>
 
31
 
 
32
class AddressTypeDialog : public KDialog
 
33
{
 
34
  public:
 
35
    AddressTypeDialog( QWidget *parent = 0 )
 
36
      : KDialog( parent)
 
37
    {
 
38
      setCaption( i18nc( "street/postal", "New Address" ) );
 
39
 
 
40
      QWidget *page = new QWidget(this);
 
41
      setMainWidget( page );
 
42
      QVBoxLayout *layout = new QVBoxLayout( page );
 
43
      layout->setSpacing( KDialog::spacingHint() );
 
44
      layout->setMargin( 0 );
 
45
 
 
46
      QGroupBox *box  = new QGroupBox( i18nc( "street/postal", "Address Types" ), page );
 
47
      layout->addWidget( box );
 
48
      mGroup = new QButtonGroup( box );
 
49
      mGroup->setExclusive ( false );
 
50
 
 
51
      QGridLayout *buttonLayout = new QGridLayout( box );
 
52
 
 
53
      mTypeList = KABC::Address::typeList();
 
54
      mTypeList.removeAll( KABC::Address::Pref );
 
55
 
 
56
      KABC::Address::TypeList::ConstIterator it;
 
57
      int i = 0;
 
58
      int row = 0;
 
59
      for ( it = mTypeList.constBegin(); it != mTypeList.constEnd(); ++it, ++i ) {
 
60
        QCheckBox *checkBox = new QCheckBox( KABC::Address::typeLabel( *it ), box );
 
61
        buttonLayout->addWidget( checkBox, row, i % 3 );
 
62
 
 
63
        if ( i % 3 == 2 )
 
64
            ++row;
 
65
 
 
66
        mGroup->addButton( checkBox );
 
67
      }
 
68
    }
 
69
 
 
70
    KABC::Address::Type type() const
 
71
    {
 
72
      KABC::Address::Type type;
 
73
      for ( int i = 0; i < mGroup->buttons().count(); ++i ) {
 
74
        QCheckBox *box = dynamic_cast<QCheckBox*>( mGroup->buttons().at( i ) );
 
75
        if ( box && box->isChecked() )
 
76
          type |= mTypeList[ i ];
 
77
      }
 
78
 
 
79
      return type;
 
80
    }
 
81
 
 
82
  private:
 
83
    QButtonGroup *mGroup;
 
84
 
 
85
    KABC::Address::TypeList mTypeList;
 
86
};
 
87
 
 
88
 
 
89
class EditorLocation::Private
 
90
{
 
91
  EditorLocation *const q;
 
92
 
 
93
  public:
 
94
    explicit Private( EditorLocation *parent ) : q( parent )
 
95
    {
 
96
      mUi.setupUi( parent );
 
97
      mModel = new LocationModel( q );
 
98
 
 
99
      mUi.addressSelectionCombo->setModel( mModel );
 
100
      mUi.addressSelectionCombo->setModelColumn( 0 );
 
101
 
 
102
      mMapper = new QDataWidgetMapper( q );
 
103
      mMapper->setModel( mModel );
 
104
      mMapper->addMapping( mUi.streetLineEdit, 1, "plainText" );
 
105
      mMapper->addMapping( mUi.postOfficeBoxLineEdit, 2 );
 
106
      mMapper->addMapping( mUi.localityLineEdit, 3 );
 
107
      mMapper->addMapping( mUi.regionLineEdit, 4 );
 
108
      mMapper->addMapping( mUi.postalCodeLineEdit, 5 );
 
109
      mMapper->addMapping( mUi.countryLineEdit, 6 );
 
110
      mMapper->addMapping( mUi.editLabelLineEdit, 7 );
 
111
      mMapper->toFirst();
 
112
 
 
113
      q->connect( mUi.addressSelectionCombo, SIGNAL( activated( int ) ),
 
114
                  mMapper, SLOT( setCurrentIndex( int ) ) );
 
115
      q->connect( mUi.addAddressButton, SIGNAL( clicked() ),
 
116
                  SLOT( addAddress() ) );
 
117
      q->connect( mUi.deleteAddressButton, SIGNAL( clicked() ),
 
118
                  SLOT( removeAddress() ) );
 
119
      q->connect( mModel, SIGNAL( rowsInserted( const QModelIndex&, int, int ) ),
 
120
                  SLOT( addressCountChanged() ) );
 
121
      q->connect( mModel, SIGNAL( rowsRemoved( const QModelIndex&, int, int ) ),
 
122
                  SLOT( addressCountChanged() ) );
 
123
 
 
124
      addressCountChanged();
 
125
    }
 
126
 
 
127
    void addAddress()
 
128
    {
 
129
      AddressTypeDialog dlg;
 
130
      if ( !dlg.exec() )
 
131
        return;
 
132
 
 
133
      const KABC::Address::Type addressType = dlg.type();
 
134
 
 
135
      if ( mModel->insertRows( 0, 1 ) ) {
 
136
        mModel->setData( mModel->index( 0, 0 ), QVariant::fromValue( static_cast<int>( addressType ) ) );
 
137
        mUi.addressSelectionCombo->setCurrentIndex( 0 );
 
138
        mMapper->setCurrentIndex( 0 );
 
139
      }
 
140
    }
 
141
 
 
142
    void removeAddress()
 
143
    {
 
144
      const int answer = KMessageBox::questionYesNo( 0, i18n( "Do you really want to delete this address?" ),
 
145
                                                        i18n( "Delete Address" ),
 
146
                                                        KGuiItem( i18n("Delete") ) );
 
147
      if ( answer == KMessageBox::No )
 
148
        return;
 
149
 
 
150
      const int index = mMapper->currentIndex();
 
151
      mModel->removeRows( index, 1 );
 
152
      if ( index >= mModel->rowCount() )
 
153
        mMapper->setCurrentIndex( mModel->rowCount() - 1 );
 
154
      else
 
155
        mMapper->setCurrentIndex( index );
 
156
 
 
157
      if ( mModel->rowCount() == 0 ) {
 
158
        // We have to cleanup the fields ourself in this case,
 
159
        // QDataWidgetMapper does not handle a non-existing index
 
160
        for ( int column = 1; column < 8; ++column ) {
 
161
          QLineEdit *lineEdit = qobject_cast<QLineEdit*>( mMapper->mappedWidgetAt( column ) );
 
162
          if ( lineEdit )
 
163
            lineEdit->clear();
 
164
          else {
 
165
            QTextEdit *textEdit = qobject_cast<QTextEdit*>( mMapper->mappedWidgetAt( column ) );
 
166
            if ( textEdit )
 
167
              textEdit->clear();
 
168
          }
 
169
        }
 
170
      }
 
171
    }
 
172
 
 
173
    void addressCountChanged()
 
174
    {
 
175
      const bool enabled = (mModel->rowCount() > 0);
 
176
 
 
177
      mUi.addressSelectionCombo->setEnabled( enabled );
 
178
      mUi.deleteAddressButton->setEnabled( enabled );
 
179
      for ( int column = 1; column < 8; ++column ) {
 
180
        QWidget *widget = mMapper->mappedWidgetAt( column );
 
181
        if ( widget )
 
182
          widget->setEnabled( enabled );
 
183
      }
 
184
    }
 
185
 
 
186
  public:
 
187
    Ui::EditorLocation mUi;
 
188
 
 
189
    KABC::Addressee mContact;
 
190
    LocationModel *mModel;
 
191
    QDataWidgetMapper *mMapper;
 
192
};
 
193
 
 
194
 
 
195
EditorLocation::EditorLocation( QWidget *parent )
 
196
  : EditorBase( parent ), d( new Private( this ) )
 
197
{
 
198
}
 
199
 
 
200
EditorLocation::~EditorLocation()
 
201
{
 
202
  delete d;
 
203
}
 
204
 
 
205
void EditorLocation::loadContact( const KABC::Addressee &contact, const Akonadi::ContactMetaData& )
 
206
{
 
207
  d->mModel->setLocations( contact.addresses() );
 
208
  d->mUi.addressSelectionCombo->setCurrentIndex( 0 );
 
209
  d->mMapper->setCurrentIndex( 0 );
 
210
  d->addressCountChanged();
 
211
}
 
212
 
 
213
void EditorLocation::saveContact( KABC::Addressee &contact, Akonadi::ContactMetaData& ) const
 
214
{
 
215
  const KABC::Address::List oldAddresses = contact.addresses();
 
216
  foreach ( const KABC::Address &oldAddress, oldAddresses )
 
217
    contact.removeAddress( oldAddress );
 
218
 
 
219
  foreach ( const KABC::Address &newAddress, d->mModel->locations() )
 
220
    contact.insertAddress( newAddress );
 
221
}
 
222
 
 
223
#include "editorlocation.moc"