• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

kaddressbook

  • sources
  • kde-4.12
  • kdepim
  • kaddressbook
  • xxport
  • csv
templateselectiondialog.cpp
Go to the documentation of this file.
1 /*
2  This file is part of KAddressBook.
3  Copyright (c) 2009 Tobias Koenig <tokoe@kde.org>
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License along
16  with this program; if not, write to the Free Software Foundation, Inc.,
17  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19 
20 #include "templateselectiondialog.h"
21 
22 #include <KConfig>
23 #include <KLocale>
24 #include <KMessageBox>
25 #include <KPushButton>
26 #include <KStandardDirs>
27 #include <KVBox>
28 
29 #include <QtCore/QAbstractTableModel>
30 #include <QtCore/QFile>
31 #include <QtCore/QFileInfo>
32 #include <QLabel>
33 #include <QListView>
34 #include <QMouseEvent>
35 #include <QStyledItemDelegate>
36 
37 typedef struct {
38  QString displayName;
39  QString fileName;
40  bool isDeletable;
41 } TemplateInfo;
42 
43 class TemplatesModel : public QAbstractTableModel
44 {
45  public:
46  TemplatesModel( QObject *parent = 0 )
47  : QAbstractTableModel( parent )
48  {
49  update();
50  }
51 
52  virtual int rowCount( const QModelIndex &parent = QModelIndex() ) const
53  {
54  if ( !parent.isValid() ) {
55  return mTemplates.count();
56  } else {
57  return 0;
58  }
59  }
60 
61  virtual int columnCount( const QModelIndex &parent = QModelIndex() ) const
62  {
63  if ( !parent.isValid() ) {
64  return 2;
65  } else {
66  return 0;
67  }
68  }
69 
70  virtual QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const
71  {
72  if ( !index.isValid() || index.row() >= mTemplates.count() || index.column() >= 2 ) {
73  return QVariant();
74  }
75 
76  if ( role == Qt::DisplayRole ) {
77  if ( index.column() == 0 ) {
78  return mTemplates[ index.row() ].displayName;
79  } else {
80  return mTemplates[ index.row() ].fileName;
81  }
82  }
83 
84  if ( role == Qt::UserRole ) {
85  return mTemplates[ index.row() ].isDeletable;
86  }
87 
88  return QVariant();
89  }
90 
91  virtual bool removeRows( int row, int count, const QModelIndex &parent = QModelIndex() )
92  {
93  if ( parent.isValid() || row < 0 || row >= mTemplates.count() ) {
94  return false;
95  }
96 
97  beginRemoveRows( parent, row, row + count - 1 );
98  for ( int i = 0; i < count; ++i ) {
99  if ( !QFile::remove( mTemplates[ row ].fileName ) ) {
100  return false;
101  }
102  mTemplates.removeAt( row );
103  }
104 
105  endRemoveRows();
106  return true;
107  }
108 
109  void update()
110  {
111  beginResetModel();
112  mTemplates.clear();
113  const QStringList files =
114  KGlobal::dirs()->findAllResources( "data", QLatin1String("kaddressbook/csv-templates/*.desktop"),
115  KStandardDirs::Recursive | KStandardDirs::NoDuplicates );
116  for ( int i = 0; i < files.count(); ++i ) {
117  KConfig config( files.at( i ), KConfig::SimpleConfig );
118 
119  if ( !config.hasGroup( "csv column map" ) ) {
120  continue;
121  }
122 
123  KConfigGroup group( &config, "Misc" );
124  TemplateInfo info;
125  info.displayName = group.readEntry( "Name" );
126  info.fileName = files.at( i );
127 
128  const QFileInfo fileInfo( info.fileName );
129  info.isDeletable = QFileInfo( fileInfo.absolutePath() ).isWritable();
130 
131  mTemplates.append( info );
132  }
133  endResetModel();
134  }
135 
136  bool templatesAvailable() const
137  {
138  return !mTemplates.isEmpty();
139  }
140 
141  private:
142  QList<TemplateInfo> mTemplates;
143 };
144 
145 class TemplateSelectionDelegate : public QStyledItemDelegate
146 {
147  public:
148  explicit TemplateSelectionDelegate( QObject *parent = 0 )
149  : QStyledItemDelegate( parent ), mIcon( QLatin1String("list-remove") )
150  {
151  }
152 
153  void paint( QPainter *painter, const QStyleOptionViewItem &option,
154  const QModelIndex &index ) const
155  {
156  QStyledItemDelegate::paint( painter, option, index );
157 
158  if ( index.data( Qt::UserRole ).toBool() ) {
159  mIcon.paint( painter, option.rect, Qt::AlignRight );
160  }
161  }
162 
163  QSize sizeHint( const QStyleOptionViewItem &option, const QModelIndex &index ) const
164  {
165  QSize hint = QStyledItemDelegate::sizeHint( option, index );
166 
167  if ( index.data( Qt::UserRole ).toBool() ) {
168  hint.setWidth( hint.width() + 16 );
169  }
170 
171  return hint;
172  }
173 
174  bool editorEvent( QEvent *event, QAbstractItemModel *model,
175  const QStyleOptionViewItem &option, const QModelIndex &index )
176  {
177  if ( event->type() == QEvent::MouseButtonRelease && index.data( Qt::UserRole ).toBool() ) {
178  const QMouseEvent *mouseEvent = static_cast<QMouseEvent*>( event );
179  QRect buttonRect = option.rect;
180  buttonRect.setLeft( buttonRect.right() - 16 );
181 
182  if ( buttonRect.contains( mouseEvent->pos() ) ) {
183  const QString templateName = index.data( Qt::DisplayRole ).toString();
184  if ( KMessageBox::questionYesNo(
185  0,
186  i18nc( "@label", "Do you really want to delete template '%1'?",
187  templateName ) ) == KMessageBox::Yes ) {
188  model->removeRows( index.row(), 1 );
189  return true;
190  }
191  }
192  }
193 
194  return QStyledItemDelegate::editorEvent( event, model, option, index );
195  }
196 
197  private:
198  KIcon mIcon;
199 };
200 
201 TemplateSelectionDialog::TemplateSelectionDialog( QWidget *parent )
202  : KDialog( parent )
203 {
204  setCaption( i18nc( "@title:window", "Template Selection" ) );
205  setButtons( Ok | Cancel );
206 
207  KVBox *wdg = new KVBox( this );
208  setMainWidget( wdg );
209 
210  new QLabel( i18nc( "@info", "Please select a template, that matches the CSV file:" ), wdg );
211 
212  mView = new QListView( wdg );
213 
214  mView->setModel( new TemplatesModel( this ) );
215  mView->setItemDelegate( new TemplateSelectionDelegate( this ) );
216 
217  button( Ok )->setEnabled( false );
218  connect( mView->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
219  this, SLOT(updateButtons()) );
220 }
221 
222 bool TemplateSelectionDialog::templatesAvailable() const
223 {
224  return static_cast<TemplatesModel*>( mView->model() )->templatesAvailable();
225 }
226 
227 QString TemplateSelectionDialog::selectedTemplate() const
228 {
229  const QModelIndex rowIndex = mView->currentIndex();
230  const QModelIndex index = mView->model()->index( rowIndex.row(), 1 );
231 
232  return index.data( Qt::DisplayRole ).toString();
233 }
234 
235 void TemplateSelectionDialog::updateButtons()
236 {
237  button( Ok )->setEnabled( mView->currentIndex().isValid() );
238 }
239 
240 #include "templateselectiondialog.moc"
KDialog
QObject
templateselectiondialog.h
TemplateSelectionDialog::TemplateSelectionDialog
TemplateSelectionDialog(QWidget *parent=0)
Definition: templateselectiondialog.cpp:201
TemplateSelectionDialog::templatesAvailable
bool templatesAvailable() const
Definition: templateselectiondialog.cpp:222
TemplateSelectionDialog::selectedTemplate
QString selectedTemplate() const
Definition: templateselectiondialog.cpp:227
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:55:51 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kaddressbook

Skip menu "kaddressbook"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal