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

mailcommon

  • sources
  • kde-4.12
  • kdepim
  • mailcommon
  • snippets
snippetsmanager.cpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 2010 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
3  Author Tobias Koenig <tokoe@kdab.com>
4 
5  This library is free software; you can redistribute it and/or modify it
6  under the terms of the GNU Library General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or (at your
8  option) any later version.
9 
10  This library is distributed in the hope that it will be useful, but WITHOUT
11  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13  License for more details.
14 
15  You should have received a copy of the GNU Library General Public License
16  along with this library; see the file COPYING.LIB. If not, write to the
17  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18  02110-1301, USA.
19 */
20 
21 #include "snippetsmanager.h"
22 
23 #include "snippetdialog_p.h"
24 #include "snippetsmodel_p.h"
25 #include "snippetvariabledialog_p.h"
26 
27 #include <KAction>
28 #include <KActionCollection>
29 #include <KDebug>
30 #include <KLocale>
31 #include <KMessageBox>
32 
33 #include <QAction>
34 #include <QPointer>
35 #include <QItemSelectionModel>
36 
37 using namespace MailCommon;
38 
39 class SnippetsManager::Private
40 {
41 public:
42  Private( SnippetsManager *qq, QWidget * parent )
43  : q( qq ), mEditor( 0 ), mParent( parent ), mDirty( false )
44  {
45  }
46 
47  QModelIndex currentGroupIndex() const;
48 
49  void selectionChanged();
50  void dndDone();
51  void addSnippet();
52  void editSnippet();
53  void deleteSnippet();
54 
55  void addSnippetGroup();
56  void editSnippetGroup();
57  void deleteSnippetGroup();
58 
59  void insertSelectedSnippet();
60  void insertActionSnippet();
61 
62  void updateActionCollection( const QString &oldName, const QString &newName,
63  const QKeySequence &keySequence, const QString &text );
64 
65  QString replaceVariables( const QString &text );
66  QModelIndex createGroup( const QString &groupName );
67  void createSnippet( const QModelIndex &groupIndex, const QString &snippetName,
68  const QString &snippetText, const QString &snippetKeySequence );
69 
70  void load();
71  void loadFromOldFormat( const KConfigGroup &group );
72  void save();
73 
74  SnippetsManager *q;
75  SnippetsModel *mModel;
76  QItemSelectionModel *mSelectionModel;
77  KActionCollection *mActionCollection;
78  QObject *mEditor;
79  QByteArray mEditorInsertMethod;
80  QMap<QString, QString> mSavedVariables;
81 
82  QAction *mAddSnippetAction;
83  QAction *mEditSnippetAction;
84  QAction *mDeleteSnippetAction;
85  QAction *mAddSnippetGroupAction;
86  QAction *mEditSnippetGroupAction;
87  QAction *mDeleteSnippetGroupAction;
88  QAction *mInsertSnippetAction;
89  QWidget *mParent;
90  bool mDirty;
91 };
92 
93 QModelIndex SnippetsManager::Private::currentGroupIndex() const
94 {
95  if ( mSelectionModel->selectedIndexes().isEmpty() ) {
96  return QModelIndex();
97  }
98 
99  const QModelIndex index = mSelectionModel->selectedIndexes().first();
100  if ( index.data( SnippetsModel::IsGroupRole ).toBool() ) {
101  return index;
102  } else {
103  return mModel->parent( index );
104  }
105 }
106 
107 void SnippetsManager::Private::selectionChanged()
108 {
109  const bool itemSelected = !mSelectionModel->selectedIndexes().isEmpty();
110 
111  if ( itemSelected ) {
112  const QModelIndex index = mSelectionModel->selectedIndexes().first();
113  const bool isGroup = index.data( SnippetsModel::IsGroupRole ).toBool();
114  if ( isGroup ) {
115  mEditSnippetAction->setEnabled( false );
116  mDeleteSnippetAction->setEnabled( false );
117  mEditSnippetGroupAction->setEnabled( true );
118  mDeleteSnippetGroupAction->setEnabled( true );
119  mInsertSnippetAction->setEnabled( false );
120  } else {
121  mEditSnippetAction->setEnabled( true );
122  mDeleteSnippetAction->setEnabled( true );
123  mEditSnippetGroupAction->setEnabled( false );
124  mDeleteSnippetGroupAction->setEnabled( false );
125  mInsertSnippetAction->setEnabled( true );
126  }
127  } else {
128  mEditSnippetAction->setEnabled( false );
129  mDeleteSnippetAction->setEnabled( false );
130  mEditSnippetGroupAction->setEnabled( false );
131  mDeleteSnippetGroupAction->setEnabled( false );
132  mInsertSnippetAction->setEnabled( false );
133  }
134 }
135 
136 void SnippetsManager::Private::addSnippet()
137 {
138  const bool noGroupAvailable = ( mModel->rowCount() == 0 );
139 
140  if ( noGroupAvailable ) {
141  // create a 'General' snippet group
142  if ( !mModel->insertRow( mModel->rowCount(), QModelIndex() ) ) {
143  return;
144  }
145 
146  const QModelIndex groupIndex = mModel->index( mModel->rowCount() - 1, 0, QModelIndex() );
147  mModel->setData( groupIndex, i18n( "General" ), SnippetsModel::NameRole );
148 
149  mSelectionModel->select( groupIndex, QItemSelectionModel::ClearAndSelect );
150  }
151 
152  QPointer<SnippetDialog> dlg = new SnippetDialog( mActionCollection, false, mParent );
153  dlg->setWindowTitle( i18nc( "@title:window", "Add Snippet" ) );
154  dlg->setGroupModel( mModel );
155  dlg->setGroupIndex( currentGroupIndex() );
156 
157  if ( dlg->exec() ) {
158  const QModelIndex groupIndex = dlg->groupIndex();
159 
160  if ( !mModel->insertRow( mModel->rowCount( groupIndex ), groupIndex ) ) {
161  delete dlg;
162  return;
163  }
164 
165  const QModelIndex index = mModel->index( mModel->rowCount( groupIndex ) - 1, 0, groupIndex );
166  mModel->setData( index, dlg->name(), SnippetsModel::NameRole );
167  mModel->setData( index, dlg->text(), SnippetsModel::TextRole );
168  mModel->setData( index, dlg->keySequence().toString(), SnippetsModel::KeySequenceRole );
169 
170  updateActionCollection( QString(), dlg->name(), dlg->keySequence(), dlg->text() );
171  mDirty = true;
172  }
173  delete dlg;
174 }
175 
176 void SnippetsManager::Private::dndDone()
177 {
178  mDirty = true;
179 }
180 
181 void SnippetsManager::Private::editSnippet()
182 {
183  QModelIndex index = mSelectionModel->selectedIndexes().first();
184  if ( !index.isValid() || index.data( SnippetsModel::IsGroupRole ).toBool() ) {
185  return;
186  }
187 
188  const QModelIndex oldGroupIndex = currentGroupIndex();
189 
190  const QString oldSnippetName = index.data( SnippetsModel::NameRole ).toString();
191 
192  QPointer<SnippetDialog> dlg = new SnippetDialog( mActionCollection, false, mParent );
193  dlg->setWindowTitle( i18nc( "@title:window", "Edit Snippet" ) );
194  dlg->setGroupModel( mModel );
195  dlg->setGroupIndex( oldGroupIndex );
196  dlg->setName( oldSnippetName );
197  dlg->setText( index.data( SnippetsModel::TextRole ).toString() );
198  dlg->setKeySequence(
199  QKeySequence::fromString(
200  index.data( SnippetsModel::KeySequenceRole ).toString() ) );
201 
202  if ( dlg->exec() ) {
203 
204  const QModelIndex newGroupIndex = dlg->groupIndex();
205 
206  if ( oldGroupIndex != newGroupIndex ) {
207  mModel->removeRow( index.row(), oldGroupIndex );
208  mModel->insertRow( mModel->rowCount( newGroupIndex ), newGroupIndex );
209 
210  index = mModel->index( mModel->rowCount( newGroupIndex ) - 1, 0, newGroupIndex );
211  }
212 
213  mModel->setData( index, dlg->name(), SnippetsModel::NameRole );
214  mModel->setData( index, dlg->text(), SnippetsModel::TextRole );
215  mModel->setData( index, dlg->keySequence().toString(), SnippetsModel::KeySequenceRole );
216 
217  updateActionCollection( oldSnippetName, dlg->name(), dlg->keySequence(), dlg->text() );
218  mDirty = true;
219  }
220  delete dlg;
221 }
222 
223 void SnippetsManager::Private::deleteSnippet()
224 {
225  const QModelIndex index = mSelectionModel->selectedIndexes().first();
226 
227  const QString snippetName = index.data( SnippetsModel::NameRole ).toString();
228 
229  if ( KMessageBox::warningContinueCancel(
230  0,
231  i18nc( "@info",
232  "Do you really want to remove snippet \"%1\"?<nl/>"
233  "<warning>There is no way to undo the removal.</warning>", snippetName ),
234  QString(),
235  KStandardGuiItem::remove() ) == KMessageBox::Cancel ) {
236  return;
237  }
238 
239  mModel->removeRow( index.row(), currentGroupIndex() );
240 
241  updateActionCollection( snippetName, QString(), QKeySequence(), QString() );
242  mDirty = true;
243 }
244 
245 void SnippetsManager::Private::addSnippetGroup()
246 {
247  QPointer<SnippetDialog> dlg = new SnippetDialog( mActionCollection, true, mParent );
248  dlg->setWindowTitle( i18nc( "@title:window", "Add Group" ) );
249 
250  if ( dlg->exec() ) {
251 
252  if ( !mModel->insertRow( mModel->rowCount(), QModelIndex() ) ) {
253  kDebug() << "unable to insert row";
254  delete dlg;
255  return;
256  }
257 
258  const QModelIndex groupIndex = mModel->index( mModel->rowCount() - 1, 0, QModelIndex() );
259  mModel->setData( groupIndex, dlg->name(), SnippetsModel::NameRole );
260  mDirty = true;
261  }
262  delete dlg;
263 }
264 
265 void SnippetsManager::Private::editSnippetGroup()
266 {
267  const QModelIndex groupIndex = currentGroupIndex();
268  if ( !groupIndex.isValid() || !groupIndex.data( SnippetsModel::IsGroupRole ).toBool() ) {
269  return;
270  }
271 
272  QPointer<SnippetDialog> dlg = new SnippetDialog( mActionCollection, true, mParent );
273  dlg->setWindowTitle( i18nc( "@title:window", "Edit Group" ) );
274  const QString oldGroupName = groupIndex.data( SnippetsModel::NameRole ).toString();
275  dlg->setName( oldGroupName );
276 
277  if ( dlg->exec() ) {
278  if (oldGroupName == dlg->name()) {
279  delete dlg;
280  return;
281  }
282 
283  mModel->setData( groupIndex, dlg->name(), SnippetsModel::NameRole );
284  mDirty = true;
285  }
286  delete dlg;
287 }
288 
289 void SnippetsManager::Private::deleteSnippetGroup()
290 {
291  const QModelIndex groupIndex = currentGroupIndex();
292  if ( !groupIndex.isValid() ) {
293  return;
294  }
295 
296  const QString groupName = groupIndex.data( SnippetsModel::NameRole ).toString();
297 
298  if ( mModel->rowCount( groupIndex ) > 0 ) {
299  if ( KMessageBox::warningContinueCancel(
300  0,
301  i18nc( "@info",
302  "Do you really want to remove group \"%1\" along with all its snippets?<nl/>"
303  "<warning>There is no way to undo the removal.</warning>", groupName ),
304  QString(),
305  KStandardGuiItem::remove() ) == KMessageBox::Cancel ) {
306  return;
307  }
308  } else {
309  if ( KMessageBox::warningContinueCancel(
310  0,
311  i18nc( "@info",
312  "Do you really want to remove group \"%1\"?", groupName ),
313  QString(),
314  KStandardGuiItem::remove() ) == KMessageBox::Cancel ) {
315  return;
316  }
317  }
318 
319  mModel->removeRow( groupIndex.row(), QModelIndex() );
320  mDirty = true;
321 }
322 
323 void SnippetsManager::Private::insertSelectedSnippet()
324 {
325  if ( !mEditor ) {
326  return;
327  }
328 
329  if ( !mSelectionModel->hasSelection() ) {
330  return;
331  }
332 
333  const QModelIndex index = mSelectionModel->selectedIndexes().first();
334  if ( index.data( SnippetsModel::IsGroupRole ).toBool() ) {
335  return;
336  }
337 
338  const QString text = replaceVariables( index.data( SnippetsModel::TextRole ).toString() );
339  QMetaObject::invokeMethod( mEditor, mEditorInsertMethod, Qt::DirectConnection,
340  Q_ARG( QString, text ) );
341 }
342 
343 void SnippetsManager::Private::insertActionSnippet()
344 {
345  if ( !mEditor ) {
346  return;
347  }
348 
349  QAction *action = qobject_cast<QAction*>( q->sender() );
350  if ( !action ) {
351  return;
352  }
353 
354  const QString text = replaceVariables( action->property( "snippetText" ).toString() );
355  QMetaObject::invokeMethod( mEditor, mEditorInsertMethod, Qt::DirectConnection,
356  Q_ARG( QString, text ) );
357 }
358 
359 void SnippetsManager::Private::updateActionCollection( const QString &oldName,
360  const QString &newName,
361  const QKeySequence &keySequence,
362  const QString &text )
363 {
364  // remove previous action in case that the name changed
365  if ( !oldName.isEmpty() ) {
366  const QString actionName = i18nc( "@action", "Snippet %1", oldName );
367  const QString normalizedName = QString( actionName ).replace( QLatin1Char(' '), QLatin1Char('_') );
368 
369  QAction *action = mActionCollection->action( normalizedName );
370  if ( action ) {
371  mActionCollection->removeAction( action );
372  }
373  }
374 
375  if ( !newName.isEmpty() ) {
376  const QString actionName = i18nc( "@action", "Snippet %1", newName );
377  const QString normalizedName = QString( actionName ).replace( QLatin1Char(' '), QLatin1Char('_') );
378 
379  KAction *action =
380  mActionCollection->addAction( normalizedName, q, SLOT(insertActionSnippet()) );
381  action->setProperty( "snippetText", text );
382  action->setText( actionName );
383  action->setShortcut( keySequence );
384  }
385 }
386 
387 QString SnippetsManager::Private::replaceVariables( const QString &text )
388 {
389  QString result = text;
390  QString variableName;
391  QString variableValue;
392  QMap<QString, QString> localVariables( mSavedVariables );
393  int iFound = -1;
394  int iEnd = -1;
395 
396  do {
397  //find the next variable by this QRegExp
398  iFound = text.indexOf( QRegExp( QLatin1String("\\$[A-Za-z-_0-9\\s]*\\$") ), iEnd + 1 );
399  if ( iFound >= 0 ) {
400  iEnd = text.indexOf( QLatin1Char('$'), iFound + 1 ) + 1;
401 
402  variableName = text.mid( iFound, iEnd - iFound );
403 
404  if ( variableName != QLatin1String( "$$" ) ) { // if not double-delimiter
405  if ( !localVariables.contains( variableName ) ) { // and not already in map
406 
407  QPointer<SnippetVariableDialog> dlg = new SnippetVariableDialog( variableName, &mSavedVariables, mParent );
408  if ( dlg->exec() ) {
409 
410  if ( dlg->saveVariableIsChecked() ) {
411  mDirty = true;
412  }
413  variableValue = dlg->variableValue();
414  } else {
415  delete dlg;
416  return QString();
417  }
418  delete dlg;
419  } else {
420  variableValue = localVariables.value( variableName );
421  }
422  } else {
423  variableValue = QLatin1Char('$'); //if double-delimiter -> replace by single character
424  }
425 
426  result.replace( variableName, variableValue );
427  localVariables[ variableName ] = variableValue;
428  }
429  } while ( iFound != -1 );
430 
431  return result;
432 }
433 
434 QModelIndex SnippetsManager::Private::createGroup( const QString &groupName )
435 {
436  mModel->insertRow( mModel->rowCount(), QModelIndex() );
437  const QModelIndex groupIndex = mModel->index( mModel->rowCount() - 1, 0, QModelIndex() );
438  mModel->setData( groupIndex, groupName, SnippetsModel::NameRole );
439  return groupIndex;
440 }
441 
442 void SnippetsManager::Private::createSnippet( const QModelIndex &groupIndex,
443  const QString &snippetName,
444  const QString &snippetText,
445  const QString &snippetKeySequence )
446 {
447 
448  mModel->insertRow( mModel->rowCount( groupIndex ), groupIndex );
449  const QModelIndex index = mModel->index( mModel->rowCount( groupIndex ) - 1, 0, groupIndex );
450 
451  mModel->setData( index, snippetName, SnippetsModel::NameRole );
452  mModel->setData( index, snippetText, SnippetsModel::TextRole );
453  mModel->setData( index, snippetKeySequence, SnippetsModel::KeySequenceRole );
454 
455  updateActionCollection( QString(),
456  snippetName,
457  QKeySequence::fromString( snippetKeySequence ),
458  snippetText );
459 }
460 
461 void SnippetsManager::Private::loadFromOldFormat( const KConfigGroup &group )
462 {
463  //Code from kmail1
464 
465  //if entry doesn't get found, this will return -1 which we will need a bit later
466  int iCount = group.readEntry( "snippetGroupCount", -1 );
467  QMap< int, QModelIndex> listGroup;
468  for ( int i=0; i<iCount; ++i ) { //read the group-list
469  const QString strNameVal =
470  group.readEntry( QString::fromLatin1( "snippetGroupName_%1" ).arg( i ), QString() );
471 
472  const int iIdVal =
473  group.readEntry( QString::fromLatin1( "snippetGroupId_%1" ).arg( i ), -1 );
474 
475  //kDebug() << "Read group " << " " << iIdVal;
476 
477  if ( !strNameVal.isEmpty() && iIdVal != -1 ) {
478  // create group
479  const QModelIndex groupIndex = createGroup( strNameVal );
480  listGroup.insert( iIdVal, groupIndex );
481  }
482  }
483 
484  /* Check if the snippetGroupCount property has been found
485  if iCount is -1 this means, that the user has his snippets
486  stored without groups.
487  Should only happen with an empty config file.
488  */
489 
490  if ( iCount != -1 ) {
491  iCount = group.readEntry( "snippetCount", 0 );
492  for ( int i=0; i<iCount; ++i ) { //read the snippet-list
493  const QString snippetName =
494  group.readEntry( QString::fromLatin1( "snippetName_%1" ).arg( i ), QString() );
495 
496  const QString snippetText =
497  group.readEntry( QString::fromLatin1( "snippetText_%1" ).arg( i ), QString() );
498 
499  const int iParentVal =
500  group.readEntry( QString::fromLatin1( "snippetParent_%1" ).arg( i ), -1 );
501 
502  if ( !snippetText.isEmpty() &&
503  !snippetName.isEmpty() &&
504  iParentVal != -1 ) {
505  const QString snippetKeySequence =
506  group.readEntry( QString::fromLatin1( "snippetShortcut_%1" ).arg( i ), QString() );
507 
508  const QModelIndex groupIndex = listGroup.value( iParentVal );
509  createSnippet( groupIndex, snippetName, snippetText, snippetKeySequence );
510  }
511  }
512  }
513  iCount = group.readEntry( "snippetSavedCount", 0 );
514 
515  for ( int i=1; i<=iCount; ++i ) { //read the saved-values and store in QMap
516  const QString variableKey =
517  group.readEntry( QString::fromLatin1( "snippetSavedName_%1" ).arg( i ), QString() );
518 
519  const QString variableValue =
520  group.readEntry( QString::fromLatin1( "snippetSavedVal_%1" ).arg( i ), QString() );
521 
522  mSavedVariables.insert( variableKey, variableValue );
523  }
524  mDirty = true;
525 }
526 
527 void SnippetsManager::Private::load()
528 {
529  const KSharedConfig::Ptr config =
530  KSharedConfig::openConfig( QLatin1String("kmailsnippetrc"), KConfig::NoGlobals );
531 
532  const KConfigGroup snippetPartGroup = config->group( "SnippetPart" );
533 
534  //Old format has this entry not new format
535  if ( snippetPartGroup.hasKey( "snippetCount" ) ) {
536  loadFromOldFormat( snippetPartGroup );
537  } else {
538  const int groupCount = snippetPartGroup.readEntry( "snippetGroupCount", 0 );
539 
540  for ( int i = 0; i < groupCount; ++i ) {
541  const KConfigGroup group =
542  config->group( QString::fromLatin1( "SnippetGroup_%1" ).arg ( i ) );
543 
544  const QString groupName = group.readEntry( "Name" );
545 
546  // create group
547  QModelIndex groupIndex = createGroup( groupName );
548 
549  const int snippetCount = group.readEntry( "snippetCount", 0 );
550  for ( int j = 0; j < snippetCount; ++j ) {
551  const QString snippetName =
552  group.readEntry( QString::fromLatin1( "snippetName_%1" ).arg( j ), QString() );
553 
554  const QString snippetText =
555  group.readEntry( QString::fromLatin1( "snippetText_%1" ).arg( j ), QString() );
556 
557  const QString snippetKeySequence =
558  group.readEntry( QString::fromLatin1( "snippetKeySequence_%1" ).arg( j ), QString() );
559 
560  createSnippet( groupIndex, snippetName, snippetText, snippetKeySequence );
561  }
562  }
563 
564  mSavedVariables.clear();
565  const KConfigGroup group = config->group( "SavedVariablesPart" );
566  const int variablesCount = group.readEntry( "variablesCount", 0 );
567 
568  for ( int i = 0; i < variablesCount; ++i ) {
569  const QString variableKey =
570  group.readEntry( QString::fromLatin1( "variableName_%1" ).arg( i ), QString() );
571 
572  const QString variableValue =
573  group.readEntry( QString::fromLatin1( "variableValue_%1" ).arg( i ), QString() );
574 
575  mSavedVariables.insert( variableKey, variableValue );
576  }
577  }
578 }
579 
580 void SnippetsManager::Private::save()
581 {
582  if ( !mDirty ) {
583  return;
584  }
585 
586  KSharedConfig::Ptr config = KSharedConfig::openConfig( QLatin1String("kmailsnippetrc"), KConfig::NoGlobals );
587 
588  // clear everything
589  foreach ( const QString &group, config->groupList() ) {
590  config->deleteGroup( group );
591  }
592 
593  // write number of snippet groups
594  KConfigGroup group = config->group( "SnippetPart" );
595 
596  const int groupCount = mModel->rowCount();
597  group.writeEntry( "snippetGroupCount", groupCount );
598 
599  for ( int i = 0; i < groupCount; ++i ) {
600  const QModelIndex groupIndex = mModel->index( i, 0, QModelIndex() );
601  const QString groupName = groupIndex.data( SnippetsModel::NameRole ).toString();
602 
603  KConfigGroup group = config->group( QString::fromLatin1( "SnippetGroup_%1" ).arg ( i ) );
604  group.writeEntry( "Name", groupName );
605 
606  const int snippetCount = mModel->rowCount( groupIndex );
607 
608  group.writeEntry( "snippetCount", snippetCount );
609  for ( int j = 0; j < snippetCount; ++j ) {
610  const QModelIndex index = mModel->index( j, 0, groupIndex );
611 
612  const QString snippetName = index.data( SnippetsModel::NameRole ).toString();
613  const QString snippetText = index.data( SnippetsModel::TextRole ).toString();
614  const QString snippetKeySequence = index.data( SnippetsModel::KeySequenceRole ).toString();
615 
616  group.writeEntry( QString::fromLatin1( "snippetName_%1" ).arg( j ), snippetName );
617  group.writeEntry( QString::fromLatin1( "snippetText_%1" ).arg( j ), snippetText );
618  group.writeEntry( QString::fromLatin1( "snippetKeySequence_%1" ).arg( j ),
619  snippetKeySequence );
620  }
621  }
622 
623  {
624  KConfigGroup group = config->group( "SavedVariablesPart" );
625 
626  const int variablesCount = mSavedVariables.count();
627  group.writeEntry( "variablesCount", variablesCount );
628 
629  int counter = 0;
630  QMapIterator<QString, QString> it( mSavedVariables );
631  while ( it.hasNext() ) {
632  it.next();
633  group.writeEntry( QString::fromLatin1( "variableName_%1" ).arg( counter ), it.key() );
634  group.writeEntry( QString::fromLatin1( "variableValue_%1" ).arg( counter ), it.value() );
635  counter++;
636  }
637  }
638 
639  config->sync();
640  mDirty = false;
641 }
642 
643 SnippetsManager::SnippetsManager( KActionCollection *actionCollection,
644  QObject *parent, QWidget *widget )
645  : QObject( parent ), d( new Private( this, widget ) )
646 {
647  d->mModel = new SnippetsModel( this );
648  d->mSelectionModel = new QItemSelectionModel( d->mModel );
649  d->mActionCollection = actionCollection;
650 
651  d->mAddSnippetAction = new QAction( i18n( "Add Snippet..." ), this );
652  d->mEditSnippetAction = new QAction( i18n( "Edit Snippet..." ), this );
653  d->mEditSnippetAction->setIcon( KIcon( QLatin1String("document-properties") ) );
654  d->mDeleteSnippetAction = new QAction( i18n( "Remove Snippet" ), this );
655  d->mDeleteSnippetAction->setIcon( KIcon( QLatin1String("edit-delete") ) );
656 
657  d->mAddSnippetGroupAction = new QAction( i18n( "Add Group..." ), this );
658  d->mEditSnippetGroupAction = new QAction( i18n( "Rename Group..." ), this );
659  d->mEditSnippetGroupAction->setIcon( KIcon( QLatin1String("edit-rename") ) );
660  d->mDeleteSnippetGroupAction = new QAction( i18n( "Remove Group" ), this );
661  d->mDeleteSnippetGroupAction->setIcon( KIcon( QLatin1String("edit-delete") ) );
662 
663  d->mInsertSnippetAction = new QAction( i18n( "Insert Snippet" ), this );
664 
665  connect( d->mSelectionModel, SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
666  this, SLOT(selectionChanged()) );
667  connect( d->mModel, SIGNAL(dndDone()), SLOT(dndDone()) );
668 
669  connect( d->mAddSnippetAction, SIGNAL(triggered(bool)), SLOT(addSnippet()) );
670  connect( d->mEditSnippetAction, SIGNAL(triggered(bool)), SLOT(editSnippet()) );
671  connect( d->mDeleteSnippetAction, SIGNAL(triggered(bool)), SLOT(deleteSnippet()) );
672 
673  connect( d->mAddSnippetGroupAction, SIGNAL(triggered(bool)), SLOT(addSnippetGroup()) );
674  connect( d->mEditSnippetGroupAction, SIGNAL(triggered(bool)), SLOT(editSnippetGroup()) );
675  connect( d->mDeleteSnippetGroupAction, SIGNAL(triggered(bool)), SLOT(deleteSnippetGroup()) );
676 
677  connect( d->mInsertSnippetAction, SIGNAL(triggered(bool)), SLOT(insertSelectedSnippet()) );
678 
679  d->selectionChanged();
680 
681  d->load();
682 }
683 
684 SnippetsManager::~SnippetsManager()
685 {
686  d->save();
687  delete d;
688 }
689 
690 void SnippetsManager::setEditor( QObject *editor, const char *insertSnippetMethod,
691  const char *dropSignal )
692 {
693  d->mEditor = editor;
694  d->mEditorInsertMethod = insertSnippetMethod;
695 
696  if ( dropSignal ) {
697  const int index =
698  editor->metaObject()->indexOfSignal(
699  QMetaObject::normalizedSignature( dropSignal + 1 ).data() ); // skip the leading '2'
700  if ( index != -1 ) {
701  connect( editor, dropSignal, this, SLOT(insertSelectedSnippet()) );
702  }
703  }
704 }
705 
706 QAbstractItemModel *SnippetsManager::model() const
707 {
708  return d->mModel;
709 }
710 
711 QItemSelectionModel *SnippetsManager::selectionModel() const
712 {
713  return d->mSelectionModel;
714 }
715 
716 QAction *SnippetsManager::addSnippetAction() const
717 {
718  return d->mAddSnippetAction;
719 }
720 
721 QAction *SnippetsManager::editSnippetAction() const
722 {
723  return d->mEditSnippetAction;
724 }
725 
726 QAction *SnippetsManager::deleteSnippetAction() const
727 {
728  return d->mDeleteSnippetAction;
729 }
730 
731 QAction *SnippetsManager::addSnippetGroupAction() const
732 {
733  return d->mAddSnippetGroupAction;
734 }
735 
736 QAction *SnippetsManager::editSnippetGroupAction() const
737 {
738  return d->mEditSnippetGroupAction;
739 }
740 
741 QAction *SnippetsManager::deleteSnippetGroupAction() const
742 {
743  return d->mDeleteSnippetGroupAction;
744 }
745 
746 QAction *SnippetsManager::insertSnippetAction() const
747 {
748  return d->mInsertSnippetAction;
749 }
750 
751 bool SnippetsManager::snippetGroupSelected() const
752 {
753  if ( d->mSelectionModel->selectedIndexes().isEmpty() ) {
754  return false;
755  }
756 
757  return d->mSelectionModel->selectedIndexes().first().data( SnippetsModel::IsGroupRole ).toBool();
758 }
759 
760 QString SnippetsManager::selectedName() const
761 {
762  if ( d->mSelectionModel->selectedIndexes().isEmpty() ) {
763  return QString();
764  }
765 
766  return d->mSelectionModel->selectedIndexes().first().data( SnippetsModel::NameRole ).toString();
767 }
768 
769 #include "snippetsmanager.moc"
MailCommon::SnippetsModel::IsGroupRole
Returns whether the index represents a group.
Definition: snippetsmodel_p.h:35
snippetsmodel_p.h
MailCommon::SnippetsModel::KeySequenceRole
The key sequence to activate a snippet.
Definition: snippetsmodel_p.h:38
text
const char * text
Definition: mdnadvicedialog.cpp:52
MailCommon::SnippetsManager::SnippetsManager
SnippetsManager(KActionCollection *actionCollection, QObject *parent=0, QWidget *widget=0)
Creates a new snippets manager.
Definition: snippetsmanager.cpp:643
QWidget
snippetsmanager.h
QObject
MailCommon::SnippetsManager::setEditor
void setEditor(QObject *editor, const char *insertSnippetMethod, const char *dropSignal)
Sets the editor object the snippet manager will act on.
Definition: snippetsmanager.cpp:690
MailCommon::SnippetsManager::insertSnippetAction
QAction * insertSnippetAction() const
Returns the action that handles inserting a snippet into the editor.
MailCommon::SnippetsManager::model
QAbstractItemModel * model() const
Returns the model that represents the snippets.
MailCommon::SnippetsModel::NameRole
The name of a snippet or group.
Definition: snippetsmodel_p.h:36
MailCommon::SnippetsManager::deleteSnippetGroupAction
QAction * deleteSnippetGroupAction() const
Returns the action that handles deleting the currently selected snippet group.
snippetvariabledialog_p.h
MailCommon::SnippetsManager::selectionModel
QItemSelectionModel * selectionModel() const
Returns the selection model that is used by the manager to select the snippet or snippet group to wor...
MailCommon::SnippetsManager::~SnippetsManager
~SnippetsManager()
Destroys the snippets manager.
Definition: snippetsmanager.cpp:684
QAbstractItemModel
snippetdialog_p.h
MailCommon::SnippetsManager::deleteSnippetAction
QAction * deleteSnippetAction() const
Returns the action that handles deleting the currently selected snippet.
MailCommon::SnippetsModel
Definition: snippetsmodel_p.h:30
MailCommon::SnippetsManager::editSnippetGroupAction
QAction * editSnippetGroupAction() const
Returns the action that handles editing the currently selected snippet group.
MailCommon::SnippetsManager::selectedName
QString selectedName() const
Returns the name of the currently selected snippet or snippet group.
MailCommon::SnippetVariableDialog
Definition: snippetvariabledialog_p.h:31
MailCommon::SnippetsManager::snippetGroupSelected
bool snippetGroupSelected() const
Returns whether the currently selected item is a snippet group.
MailCommon::SnippetsManager
Definition: snippetsmanager.h:36
MailCommon::SnippetsModel::TextRole
The text of a snippet.
Definition: snippetsmodel_p.h:37
MailCommon::SnippetsManager::editSnippetAction
QAction * editSnippetAction() const
Returns the action that handles editing the currently selected snippet.
MailCommon::SnippetsManager::addSnippetAction
QAction * addSnippetAction() const
Returns the action that handles adding new snippets.
MailCommon::SnippetsManager::addSnippetGroupAction
QAction * addSnippetGroupAction() const
Returns the action that handles adding new snippet groups.
SnippetDialog
Definition: snippetdialog_p.h:28
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:55:15 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

mailcommon

Skip menu "mailcommon"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

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