~ubuntu-branches/ubuntu/karmic/kdepim/karmic-backports

« back to all changes in this revision

Viewing changes to akonadi/kabc/kabcitemeditor.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Christian Mangold
  • Date: 2009-07-10 06:34:50 UTC
  • mfrom: (1.1.40 upstream)
  • Revision ID: james.westby@ubuntu.com-20090710063450-neojgew2fh0n3y0u
Tags: 4:4.2.96-0ubuntu1
* New upstream release
* Bump kde build-deps to 4.2.96

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
    Copyright (c) 2007 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 "kabcitemeditor.h"
21
 
 
22
 
#include <QtGui/QGridLayout>
23
 
#include <QtGui/QLabel>
24
 
#include <QtGui/QLineEdit>
25
 
#include <QtGui/QMessageBox>
26
 
 
27
 
#include <kabc/addressee.h>
28
 
#include <kabc/phonenumber.h>
29
 
#include <akonadi/itemcreatejob.h>
30
 
#include <akonadi/itemfetchjob.h>
31
 
#include <akonadi/itemfetchscope.h>
32
 
#include <akonadi/itemmodifyjob.h>
33
 
#include <akonadi/monitor.h>
34
 
#include <akonadi/session.h>
35
 
#include <klocale.h>
36
 
 
37
 
#include "ui_kabcitemeditor.h"
38
 
#include "waitingoverlay.h"
39
 
 
40
 
using namespace Akonadi;
41
 
 
42
 
class KABCItemEditor::Private
43
 
{
44
 
  public:
45
 
    Private( KABCItemEditor *parent )
46
 
      : mParent( parent ), mMonitor( 0 )
47
 
    {
48
 
    }
49
 
 
50
 
    ~Private()
51
 
    {
52
 
      delete mMonitor;
53
 
    }
54
 
 
55
 
    void fetchDone( KJob* );
56
 
    void storeDone( KJob* );
57
 
    void itemChanged( const Akonadi::Item &item, const QSet<QByteArray>& );
58
 
 
59
 
    void loadContact( const KABC::Addressee &addr );
60
 
    void storeContact( KABC::Addressee &addr );
61
 
    void setupMonitor();
62
 
 
63
 
    KABCItemEditor *mParent;
64
 
    KABCItemEditor::Mode mMode;
65
 
    Item mItem;
66
 
    Monitor *mMonitor;
67
 
    Collection mDefaultCollection;
68
 
    Ui::KABCItemEditor gui;
69
 
};
70
 
 
71
 
void KABCItemEditor::Private::fetchDone( KJob *job )
72
 
{
73
 
  if ( job->error() )
74
 
    return;
75
 
 
76
 
  ItemFetchJob *fetchJob = qobject_cast<ItemFetchJob*>( job );
77
 
  if ( !fetchJob )
78
 
    return;
79
 
 
80
 
  if ( fetchJob->items().isEmpty() )
81
 
    return;
82
 
 
83
 
  mItem = fetchJob->items().first();
84
 
 
85
 
  const KABC::Addressee addr = mItem.payload<KABC::Addressee>();
86
 
  loadContact( addr );
87
 
}
88
 
 
89
 
void KABCItemEditor::Private::storeDone( KJob *job )
90
 
{
91
 
  if ( job->error() ) {
92
 
    emit mParent->error( job->errorString() );
93
 
    return;
94
 
  }
95
 
 
96
 
  if ( mMode == EditMode )
97
 
    emit mParent->contactStored( mItem );
98
 
  else if ( mMode == CreateMode )
99
 
    emit mParent->contactStored( static_cast<ItemCreateJob*>( job )->item() );
100
 
}
101
 
 
102
 
void KABCItemEditor::Private::itemChanged( const Item&, const QSet<QByteArray>& )
103
 
{
104
 
  QMessageBox dlg( mParent );
105
 
 
106
 
  dlg.setInformativeText( QLatin1String( "The contact has been changed by anyone else\nWhat shall be done?" ) );
107
 
  dlg.addButton( i18n( "Take over changes" ), QMessageBox::AcceptRole );
108
 
  dlg.addButton( i18n( "Ignore and Overwrite changes" ), QMessageBox::RejectRole );
109
 
 
110
 
  if ( dlg.exec() == QMessageBox::AcceptRole ) {
111
 
    ItemFetchJob *job = new ItemFetchJob( mItem );
112
 
    job->fetchScope().fetchFullPayload();
113
 
 
114
 
    mParent->connect( job, SIGNAL( result( KJob* ) ), mParent, SLOT( fetchDone( KJob* ) ) );
115
 
  }
116
 
}
117
 
 
118
 
void KABCItemEditor::Private::loadContact( const KABC::Addressee &addr )
119
 
{
120
 
  // Names
121
 
  gui.mGivenName->setText( addr.givenName() );
122
 
  gui.mFamilyName->setText( addr.familyName() );
123
 
  gui.mFormattedName->setText( addr.formattedName() );
124
 
  gui.mNickName->setText( addr.nickName() );
125
 
 
126
 
  // Internet
127
 
  const QStringList emails = addr.emails();
128
 
  if ( emails.count() > 0 )
129
 
    gui.mEMail->setText( emails.at( 0 ) );
130
 
  else
131
 
    gui.mEMail->setText( QString() );
132
 
 
133
 
  if ( emails.count() > 1 )
134
 
    gui.mAdditionalEMail->setText( emails.at( 1 ) );
135
 
  else
136
 
    gui.mAdditionalEMail->setText( QString() );
137
 
 
138
 
  gui.mHomepage->setText( addr.url().url() );
139
 
 
140
 
  // Phones
141
 
  gui.mWorkPhone->setText( addr.phoneNumber( KABC::PhoneNumber::Work ).number() );
142
 
  gui.mHomePhone->setText( addr.phoneNumber( KABC::PhoneNumber::Home ).number() );
143
 
  gui.mMobilePhone->setText( addr.phoneNumber( KABC::PhoneNumber::Cell ).number() );
144
 
  gui.mFaxPhone->setText( addr.phoneNumber( KABC::PhoneNumber::Fax ).number() );
145
 
  gui.mPagerPhone->setText( addr.phoneNumber( KABC::PhoneNumber::Pager ).number() );
146
 
 
147
 
  // Address Home
148
 
  KABC::Address homeAddress = addr.address( KABC::Address::Home );
149
 
  gui.mHomeStreet->setText( homeAddress.street() );
150
 
  gui.mHomeCity->setText( homeAddress.locality() );
151
 
  gui.mHomeState->setText( homeAddress.region() );
152
 
  gui.mHomePostalCode->setText( homeAddress.postalCode() );
153
 
  gui.mHomeCountry->setText( homeAddress.country() );
154
 
 
155
 
  // Address Work
156
 
  gui.mTitle->setText( addr.title() );
157
 
  gui.mDepartment->setText( addr.department() );
158
 
  gui.mOrganization->setText( addr.organization() );
159
 
 
160
 
  KABC::Address workAddress = addr.address( KABC::Address::Work );
161
 
  gui.mWorkStreet->setText( workAddress.street() );
162
 
  gui.mWorkCity->setText( workAddress.locality() );
163
 
  gui.mWorkState->setText( workAddress.region() );
164
 
  gui.mWorkPostalCode->setText( workAddress.postalCode() );
165
 
  gui.mWorkCountry->setText( workAddress.country() );
166
 
 
167
 
  // Customs
168
 
 
169
 
  // Notes
170
 
  gui.mNote->setPlainText( addr.note() );
171
 
}
172
 
 
173
 
void KABCItemEditor::Private::storeContact( KABC::Addressee &addr )
174
 
{
175
 
  // Names
176
 
  addr.setGivenName( gui.mGivenName->text() );
177
 
  addr.setFamilyName( gui.mFamilyName->text() );
178
 
  if ( !gui.mFormattedName->text().isEmpty() )
179
 
    addr.setFormattedName( gui.mFormattedName->text() );
180
 
  else
181
 
    addr.setFormattedName( QString::fromLatin1( "%1 %2" ).arg( addr.givenName() ).arg( addr.familyName() ) );
182
 
  addr.setNickName( gui.mNickName->text() );
183
 
 
184
 
  // Internet
185
 
  QStringList emails;
186
 
  if ( !gui.mEMail->text().isEmpty() )
187
 
    emails.append( gui.mEMail->text() );
188
 
  if ( !gui.mAdditionalEMail->text().isEmpty() )
189
 
    emails.append( gui.mAdditionalEMail->text() );
190
 
  addr.setEmails( emails );
191
 
  addr.setUrl( gui.mHomepage->text() );
192
 
 
193
 
  // Phones
194
 
  addr.removePhoneNumber( addr.phoneNumber( KABC::PhoneNumber::Work ) );
195
 
  if ( !gui.mWorkPhone->text().isEmpty() )
196
 
    addr.insertPhoneNumber( KABC::PhoneNumber( gui.mWorkPhone->text(), KABC::PhoneNumber::Work ) );
197
 
 
198
 
  addr.removePhoneNumber( addr.phoneNumber( KABC::PhoneNumber::Home ) );
199
 
  if ( !gui.mHomePhone->text().isEmpty() )
200
 
    addr.insertPhoneNumber( KABC::PhoneNumber( gui.mHomePhone->text(), KABC::PhoneNumber::Home ) );
201
 
 
202
 
  addr.removePhoneNumber( addr.phoneNumber( KABC::PhoneNumber::Cell ) );
203
 
  if ( !gui.mMobilePhone->text().isEmpty() )
204
 
    addr.insertPhoneNumber( KABC::PhoneNumber( gui.mMobilePhone->text(), KABC::PhoneNumber::Cell ) );
205
 
 
206
 
  addr.removePhoneNumber( addr.phoneNumber( KABC::PhoneNumber::Fax ) );
207
 
  if ( !gui.mFaxPhone->text().isEmpty() )
208
 
    addr.insertPhoneNumber( KABC::PhoneNumber( gui.mFaxPhone->text(), KABC::PhoneNumber::Fax ) );
209
 
 
210
 
  addr.removePhoneNumber( addr.phoneNumber( KABC::PhoneNumber::Pager ) );
211
 
  if ( !gui.mPagerPhone->text().isEmpty() )
212
 
    addr.insertPhoneNumber( KABC::PhoneNumber( gui.mPagerPhone->text(), KABC::PhoneNumber::Pager ) );
213
 
 
214
 
  // Address Home
215
 
  addr.removeAddress( addr.address( KABC::Address::Home ) );
216
 
  if ( !(gui.mHomeStreet->text().isEmpty() &&
217
 
         gui.mHomeCity->text().isEmpty() &&
218
 
         gui.mHomeState->text().isEmpty() &&
219
 
         gui.mHomePostalCode->text().isEmpty() &&
220
 
         gui.mHomeCountry->text().isEmpty()) ) {
221
 
    KABC::Address homeAddress( KABC::Address::Home );
222
 
    homeAddress.setStreet( gui.mHomeStreet->text() );
223
 
    homeAddress.setLocality( gui.mHomeCity->text() );
224
 
    homeAddress.setRegion( gui.mHomeState->text() );
225
 
    homeAddress.setPostalCode( gui.mHomePostalCode->text() );
226
 
    homeAddress.setCountry( gui.mHomeCountry->text() );
227
 
 
228
 
    addr.insertAddress( homeAddress );
229
 
  }
230
 
 
231
 
  // Address Work
232
 
  addr.setTitle( gui.mTitle->text() );
233
 
  addr.setDepartment( gui.mDepartment->text() );
234
 
  addr.setOrganization( gui.mOrganization->text() );
235
 
 
236
 
  addr.removeAddress( addr.address( KABC::Address::Work ) );
237
 
  if ( !(gui.mWorkStreet->text().isEmpty() &&
238
 
         gui.mWorkCity->text().isEmpty() &&
239
 
         gui.mWorkState->text().isEmpty() &&
240
 
         gui.mWorkPostalCode->text().isEmpty() &&
241
 
         gui.mWorkCountry->text().isEmpty()) ) {
242
 
    KABC::Address workAddress( KABC::Address::Work );
243
 
    workAddress.setStreet( gui.mWorkStreet->text() );
244
 
    workAddress.setLocality( gui.mWorkCity->text() );
245
 
    workAddress.setRegion( gui.mWorkState->text() );
246
 
    workAddress.setPostalCode( gui.mWorkPostalCode->text() );
247
 
    workAddress.setCountry( gui.mWorkCountry->text() );
248
 
 
249
 
    addr.insertAddress( workAddress );
250
 
  }
251
 
 
252
 
  // Customs
253
 
 
254
 
  // Notes
255
 
  addr.setNote( gui.mNote->toPlainText() );
256
 
}
257
 
 
258
 
void KABCItemEditor::Private::setupMonitor()
259
 
{
260
 
  delete mMonitor;
261
 
  mMonitor = new Monitor;
262
 
  mMonitor->ignoreSession( Session::defaultSession() );
263
 
 
264
 
  connect( mMonitor, SIGNAL( itemChanged( const Akonadi::Item&, const QSet<QByteArray>& ) ),
265
 
           mParent, SLOT( itemChanged( const Akonadi::Item&, const QSet<QByteArray>& ) ) );
266
 
}
267
 
 
268
 
 
269
 
KABCItemEditor::KABCItemEditor( Mode mode, QWidget *parent )
270
 
  : QWidget( parent ), d( new Private( this ) )
271
 
{
272
 
  d->mMode = mode;
273
 
  d->gui.setupUi( this );
274
 
}
275
 
 
276
 
 
277
 
KABCItemEditor::~KABCItemEditor()
278
 
{
279
 
  delete d;
280
 
}
281
 
 
282
 
void KABCItemEditor::loadContact( const Akonadi::Item &item )
283
 
{
284
 
  if ( d->mMode == CreateMode )
285
 
    Q_ASSERT_X( false, "KABCItemEditor::loadContact", "You are calling loadContact in CreateMode!" );
286
 
 
287
 
  ItemFetchJob *job = new ItemFetchJob( item );
288
 
  job->fetchScope().fetchFullPayload();
289
 
 
290
 
  connect( job, SIGNAL( result( KJob* ) ), SLOT( fetchDone( KJob* ) ) );
291
 
 
292
 
  d->setupMonitor();
293
 
  d->mMonitor->setItemMonitored( item );
294
 
 
295
 
  new WaitingOverlay( job, this );
296
 
}
297
 
 
298
 
void KABCItemEditor::saveContact()
299
 
{
300
 
  if ( d->mMode == EditMode ) {
301
 
    if ( !d->mItem.isValid() )
302
 
      return;
303
 
 
304
 
    KABC::Addressee addr = d->mItem.payload<KABC::Addressee>();
305
 
 
306
 
    d->storeContact( addr );
307
 
 
308
 
    d->mItem.setPayload<KABC::Addressee>( addr );
309
 
 
310
 
    ItemModifyJob *job = new ItemModifyJob( d->mItem );
311
 
    connect( job, SIGNAL( result( KJob* ) ), SLOT( storeDone( KJob* ) ) );
312
 
  } else if ( d->mMode == CreateMode ) {
313
 
    Q_ASSERT_X( d->mDefaultCollection.isValid(), "KABCItemEditor::saveContact", "Using invalid default collection for saving!" );
314
 
 
315
 
    KABC::Addressee addr;
316
 
    d->storeContact( addr );
317
 
 
318
 
    Item item;
319
 
    item.setPayload<KABC::Addressee>( addr );
320
 
    item.setMimeType( KABC::Addressee::mimeType() );
321
 
 
322
 
    ItemCreateJob *job = new ItemCreateJob( item, d->mDefaultCollection );
323
 
    connect( job, SIGNAL( result( KJob* ) ), SLOT( storeDone( KJob* ) ) );
324
 
  }
325
 
}
326
 
 
327
 
void KABCItemEditor::setDefaultCollection( const Akonadi::Collection &collection )
328
 
{
329
 
  d->mDefaultCollection = collection;
330
 
}
331
 
 
332
 
#include "kabcitemeditor.moc"