2
Copyright (c) 2007 Tobias Koenig <tokoe@kde.org>
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.
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.
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
20
#include "kabcitemeditor.h"
22
#include <QtGui/QGridLayout>
23
#include <QtGui/QLabel>
24
#include <QtGui/QLineEdit>
25
#include <QtGui/QMessageBox>
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>
37
#include "ui_kabcitemeditor.h"
38
#include "waitingoverlay.h"
40
using namespace Akonadi;
42
class KABCItemEditor::Private
45
Private( KABCItemEditor *parent )
46
: mParent( parent ), mMonitor( 0 )
55
void fetchDone( KJob* );
56
void storeDone( KJob* );
57
void itemChanged( const Akonadi::Item &item, const QSet<QByteArray>& );
59
void loadContact( const KABC::Addressee &addr );
60
void storeContact( KABC::Addressee &addr );
63
KABCItemEditor *mParent;
64
KABCItemEditor::Mode mMode;
67
Collection mDefaultCollection;
68
Ui::KABCItemEditor gui;
71
void KABCItemEditor::Private::fetchDone( KJob *job )
76
ItemFetchJob *fetchJob = qobject_cast<ItemFetchJob*>( job );
80
if ( fetchJob->items().isEmpty() )
83
mItem = fetchJob->items().first();
85
const KABC::Addressee addr = mItem.payload<KABC::Addressee>();
89
void KABCItemEditor::Private::storeDone( KJob *job )
92
emit mParent->error( job->errorString() );
96
if ( mMode == EditMode )
97
emit mParent->contactStored( mItem );
98
else if ( mMode == CreateMode )
99
emit mParent->contactStored( static_cast<ItemCreateJob*>( job )->item() );
102
void KABCItemEditor::Private::itemChanged( const Item&, const QSet<QByteArray>& )
104
QMessageBox dlg( mParent );
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 );
110
if ( dlg.exec() == QMessageBox::AcceptRole ) {
111
ItemFetchJob *job = new ItemFetchJob( mItem );
112
job->fetchScope().fetchFullPayload();
114
mParent->connect( job, SIGNAL( result( KJob* ) ), mParent, SLOT( fetchDone( KJob* ) ) );
118
void KABCItemEditor::Private::loadContact( const KABC::Addressee &addr )
121
gui.mGivenName->setText( addr.givenName() );
122
gui.mFamilyName->setText( addr.familyName() );
123
gui.mFormattedName->setText( addr.formattedName() );
124
gui.mNickName->setText( addr.nickName() );
127
const QStringList emails = addr.emails();
128
if ( emails.count() > 0 )
129
gui.mEMail->setText( emails.at( 0 ) );
131
gui.mEMail->setText( QString() );
133
if ( emails.count() > 1 )
134
gui.mAdditionalEMail->setText( emails.at( 1 ) );
136
gui.mAdditionalEMail->setText( QString() );
138
gui.mHomepage->setText( addr.url().url() );
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() );
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() );
156
gui.mTitle->setText( addr.title() );
157
gui.mDepartment->setText( addr.department() );
158
gui.mOrganization->setText( addr.organization() );
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() );
170
gui.mNote->setPlainText( addr.note() );
173
void KABCItemEditor::Private::storeContact( KABC::Addressee &addr )
176
addr.setGivenName( gui.mGivenName->text() );
177
addr.setFamilyName( gui.mFamilyName->text() );
178
if ( !gui.mFormattedName->text().isEmpty() )
179
addr.setFormattedName( gui.mFormattedName->text() );
181
addr.setFormattedName( QString::fromLatin1( "%1 %2" ).arg( addr.givenName() ).arg( addr.familyName() ) );
182
addr.setNickName( gui.mNickName->text() );
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() );
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 ) );
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 ) );
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 ) );
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 ) );
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 ) );
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() );
228
addr.insertAddress( homeAddress );
232
addr.setTitle( gui.mTitle->text() );
233
addr.setDepartment( gui.mDepartment->text() );
234
addr.setOrganization( gui.mOrganization->text() );
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() );
249
addr.insertAddress( workAddress );
255
addr.setNote( gui.mNote->toPlainText() );
258
void KABCItemEditor::Private::setupMonitor()
261
mMonitor = new Monitor;
262
mMonitor->ignoreSession( Session::defaultSession() );
264
connect( mMonitor, SIGNAL( itemChanged( const Akonadi::Item&, const QSet<QByteArray>& ) ),
265
mParent, SLOT( itemChanged( const Akonadi::Item&, const QSet<QByteArray>& ) ) );
269
KABCItemEditor::KABCItemEditor( Mode mode, QWidget *parent )
270
: QWidget( parent ), d( new Private( this ) )
273
d->gui.setupUi( this );
277
KABCItemEditor::~KABCItemEditor()
282
void KABCItemEditor::loadContact( const Akonadi::Item &item )
284
if ( d->mMode == CreateMode )
285
Q_ASSERT_X( false, "KABCItemEditor::loadContact", "You are calling loadContact in CreateMode!" );
287
ItemFetchJob *job = new ItemFetchJob( item );
288
job->fetchScope().fetchFullPayload();
290
connect( job, SIGNAL( result( KJob* ) ), SLOT( fetchDone( KJob* ) ) );
293
d->mMonitor->setItemMonitored( item );
295
new WaitingOverlay( job, this );
298
void KABCItemEditor::saveContact()
300
if ( d->mMode == EditMode ) {
301
if ( !d->mItem.isValid() )
304
KABC::Addressee addr = d->mItem.payload<KABC::Addressee>();
306
d->storeContact( addr );
308
d->mItem.setPayload<KABC::Addressee>( addr );
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!" );
315
KABC::Addressee addr;
316
d->storeContact( addr );
319
item.setPayload<KABC::Addressee>( addr );
320
item.setMimeType( KABC::Addressee::mimeType() );
322
ItemCreateJob *job = new ItemCreateJob( item, d->mDefaultCollection );
323
connect( job, SIGNAL( result( KJob* ) ), SLOT( storeDone( KJob* ) ) );
327
void KABCItemEditor::setDefaultCollection( const Akonadi::Collection &collection )
329
d->mDefaultCollection = collection;
332
#include "kabcitemeditor.moc"