kilobyte is kB not kb
[kdepim.git] / akonadiconsole / collectionattributespage.cpp
blobbe753144e948fef6eac4407fcb13dc050bb2df01
1 /*
2 Copyright (c) 2008 Volker Krause <vkrause@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
17 02110-1301, USA.
20 #include "collectionattributespage.h"
22 #include <akonadi/attributefactory.h>
23 #include <akonadi/collection.h>
25 #include <kdebug.h>
26 #include <klocale.h>
28 #include <QStandardItemModel>
30 using namespace Akonadi;
32 CollectionAttributePage::CollectionAttributePage(QWidget * parent) :
33 CollectionPropertiesPage( parent ),
34 mModel( 0 )
36 setPageTitle( i18n( "Attributes" ) );
37 ui.setupUi( this );
39 connect( ui.addButton, SIGNAL(clicked()), SLOT(addAttribute()) );
40 connect( ui.deleteButton, SIGNAL(clicked()), SLOT(delAttribute()) );
43 void CollectionAttributePage::load(const Collection & col)
45 Attribute::List list = col.attributes();
46 mModel = new QStandardItemModel( list.count(), 2 );
47 QStringList labels;
48 labels << i18n( "Attribute" ) << i18n( "Value" );
49 mModel->setHorizontalHeaderLabels( labels );
51 for ( int i = 0; i < list.count(); ++i ) {
52 QModelIndex index = mModel->index( i, 0 );
53 Q_ASSERT( index.isValid() );
54 mModel->setData( index, QString::fromLatin1( list[i]->type() ) );
55 mModel->item( i, 0 )->setEditable( false );
56 index = mModel->index( i, 1 );
57 Q_ASSERT( index.isValid() );
58 mModel->setData( index, QString::fromLatin1( list[i]->serialized() ) );
59 mModel->itemFromIndex( index )->setFlags( Qt::ItemIsEditable | mModel->flags( index ) );
61 ui.attrView->setModel( mModel );
62 connect( mModel, SIGNAL(itemChanged(QStandardItem*)), SLOT(attributeChanged(QStandardItem*)) );
65 void CollectionAttributePage::save(Collection & col)
67 foreach ( const QString &del, mDeleted )
68 col.removeAttribute( del.toLatin1() );
69 for ( int i = 0; i < mModel->rowCount(); ++i ) {
70 const QModelIndex typeIndex = mModel->index( i, 0 );
71 Q_ASSERT( typeIndex.isValid() );
72 if ( !mChanged.contains( typeIndex.data().toString() ) )
73 continue;
74 const QModelIndex valueIndex = mModel->index( i, 1 );
75 Q_ASSERT( valueIndex.isValid() );
76 Attribute* attr = AttributeFactory::createAttribute( mModel->data( typeIndex ).toString().toLatin1() );
77 Q_ASSERT( attr );
78 attr->deserialize( mModel->data( valueIndex ).toString().toLatin1() );
79 col.addAttribute( attr );
83 void CollectionAttributePage::addAttribute()
85 if ( ui.attrName->text().isEmpty() )
86 return;
87 const QString attr = ui.attrName->text();
88 mChanged.insert( attr );
89 mDeleted.remove( attr );
90 const int row = mModel->rowCount();
91 mModel->insertRow( row );
92 QModelIndex index = mModel->index( row, 0 );
93 Q_ASSERT( index.isValid() );
94 mModel->setData( index, attr );
95 ui.attrName->clear();
98 void CollectionAttributePage::delAttribute()
100 QModelIndexList selection = ui.attrView->selectionModel()->selectedRows();
101 if ( selection.count() != 1 )
102 return;
103 const QString attr = selection.first().data().toString();
104 mChanged.remove( attr );
105 mDeleted.insert( attr );
106 mModel->removeRow( selection.first().row() );
109 void CollectionAttributePage::attributeChanged( QStandardItem *item )
111 const QString attr = mModel->data( mModel->index( item->row(), 0 ) ).toString();
112 mDeleted.remove( attr );
113 mChanged.insert( attr );
116 #include "collectionattributespage.moc"