~ubuntu-branches/ubuntu/oneiric/kdepim/oneiric-updates

« back to all changes in this revision

Viewing changes to mobile/lib/favoritescontroller.cpp

  • Committer: Package Import Robot
  • Author(s): Philip Muškovac
  • Date: 2011-06-28 19:33:24 UTC
  • mfrom: (0.2.13) (0.1.13 sid)
  • Revision ID: package-import@ubuntu.com-20110628193324-8yvjs8sdv9rdoo6c
Tags: 4:4.7.0-0ubuntu1
* New upstream release
  - update install files
  - add missing kdepim-doc package to control file
  - Fix Vcs lines
  - kontact breaks/replaces korganizer << 4:4.6.80
  - tighten the dependency of kdepim-dev on libkdepim4 to fix lintian error

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    Copyright (C) 2010 Klarälvdalens Datakonsult AB,
 
3
        a KDAB Group company, info@kdab.net,
 
4
        author Tobias Koenig <tokoe@kdab.com>
 
5
 
 
6
    This library is free software; you can redistribute it and/or modify it
 
7
    under the terms of the GNU Library General Public License as published by
 
8
    the Free Software Foundation; either version 2 of the License, or (at your
 
9
    option) any later version.
 
10
 
 
11
    This library is distributed in the hope that it will be useful, but WITHOUT
 
12
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 
13
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
 
14
    License for more details.
 
15
 
 
16
    You should have received a copy of the GNU Library General Public License
 
17
    along with this library; see the file COPYING.LIB.  If not, write to the
 
18
    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 
19
    02110-1301, USA.
 
20
*/
 
21
 
 
22
#include "favoritescontroller.h"
 
23
 
 
24
#include "favoriteslistmodel.h"
 
25
 
 
26
#include <akonadi/etmviewstatesaver.h>
 
27
#include <kconfiggroup.h>
 
28
#include <klocale.h>
 
29
#include <kmessagebox.h>
 
30
 
 
31
#include <QtCore/QAbstractItemModel>
 
32
#include <QtGui/QAction>
 
33
#include <QtGui/QItemSelectionModel>
 
34
 
 
35
static const char * const sFavoritePrefix = "Favorite_";
 
36
static const int sFavoritePrefixLength = 9;
 
37
 
 
38
class FavoritesController::Private
 
39
{
 
40
  public:
 
41
    QStringList rereadFavoritesList() const;
 
42
 
 
43
    void selectionChanged();
 
44
    void removeFavorite();
 
45
    void moveUpFavorite();
 
46
    void moveDownFavorite();
 
47
 
 
48
    KSharedConfig::Ptr mConfig;
 
49
    FavoritesListModel *mModel;
 
50
    QItemSelectionModel *mSelectionModel;
 
51
    QItemSelectionModel *mCollectionSelectionModel;
 
52
    QAction *mRemoveAction;
 
53
    QAction *mMoveUpAction;
 
54
    QAction *mMoveDownAction;
 
55
};
 
56
 
 
57
QStringList FavoritesController::Private::rereadFavoritesList() const
 
58
{
 
59
  QStringList names;
 
60
  foreach ( const QString &group, mConfig->groupList() ) {
 
61
    if ( group.startsWith( sFavoritePrefix ) )
 
62
      names.append( QString( group ).remove( 0, sFavoritePrefixLength ) );
 
63
  }
 
64
 
 
65
  return names;
 
66
}
 
67
 
 
68
void FavoritesController::Private::selectionChanged()
 
69
{
 
70
  const bool favoriteSelected = mSelectionModel->hasSelection();
 
71
 
 
72
  if ( favoriteSelected ) {
 
73
    mRemoveAction->setEnabled( true );
 
74
 
 
75
    const QModelIndex index = mSelectionModel->selectedRows().first();
 
76
    mMoveUpAction->setEnabled( index.row() != 0 );
 
77
    mMoveDownAction->setEnabled( index.row() != (mModel->rowCount() - 1) );
 
78
  } else {
 
79
    mRemoveAction->setEnabled( false );
 
80
    mMoveUpAction->setEnabled( false );
 
81
    mMoveDownAction->setEnabled( false );
 
82
  }
 
83
}
 
84
 
 
85
void FavoritesController::Private::removeFavorite()
 
86
{
 
87
  if ( !mSelectionModel->hasSelection() )
 
88
    return;
 
89
 
 
90
  const QModelIndex index = mSelectionModel->selectedRows().first();
 
91
 
 
92
  const int result = KMessageBox::questionYesNo( 0, i18n( "Do you really want to remove favorite <b>%1</b>?",
 
93
                                                          index.data( Qt::DisplayRole ).toString() ),
 
94
                                                    i18n( "Remove Favorite" ) );
 
95
  if ( result == KMessageBox::No )
 
96
    return;
 
97
 
 
98
  mModel->removeItem( index.row() );
 
99
}
 
100
 
 
101
void FavoritesController::Private::moveUpFavorite()
 
102
{
 
103
  if ( !mSelectionModel->hasSelection() )
 
104
    return;
 
105
 
 
106
  const QModelIndex index = mSelectionModel->selectedRows().first();
 
107
  mModel->moveUp( index.row() );
 
108
}
 
109
 
 
110
void FavoritesController::Private::moveDownFavorite()
 
111
{
 
112
  if ( !mSelectionModel->hasSelection() )
 
113
    return;
 
114
 
 
115
  const QModelIndex index = mSelectionModel->selectedRows().first();
 
116
  mModel->moveDown( index.row() );
 
117
}
 
118
 
 
119
 
 
120
FavoritesController::FavoritesController( const KSharedConfig::Ptr &config, QObject *parent )
 
121
  : QObject( parent ), d( new Private )
 
122
{
 
123
  d->mConfig = config;
 
124
  d->mModel = new FavoritesListModel( config, this );
 
125
  d->mSelectionModel = new QItemSelectionModel( d->mModel );
 
126
  d->mCollectionSelectionModel = 0;
 
127
 
 
128
  d->mRemoveAction = new QAction( i18n( "Remove" ), this );
 
129
  d->mMoveUpAction = new QAction( i18n( "Move Up" ), this );
 
130
  d->mMoveDownAction = new QAction( i18n( "Move Down" ), this );
 
131
 
 
132
  connect( d->mSelectionModel, SIGNAL( selectionChanged( const QItemSelection&, const QItemSelection& ) ),
 
133
           this, SLOT( selectionChanged() ) );
 
134
 
 
135
  connect( d->mRemoveAction, SIGNAL( triggered( bool ) ), SLOT( removeFavorite() ) );
 
136
  connect( d->mMoveUpAction, SIGNAL( triggered( bool ) ), SLOT( moveUpFavorite() ) );
 
137
  connect( d->mMoveDownAction, SIGNAL( triggered( bool ) ), SLOT( moveDownFavorite() ) );
 
138
 
 
139
  d->selectionChanged();
 
140
}
 
141
 
 
142
FavoritesController::~FavoritesController()
 
143
{
 
144
  delete d;
 
145
}
 
146
 
 
147
QAbstractItemModel* FavoritesController::model() const
 
148
{
 
149
  return d->mModel;
 
150
}
 
151
 
 
152
QItemSelectionModel* FavoritesController::selectionModel() const
 
153
{
 
154
  return d->mSelectionModel;
 
155
}
 
156
 
 
157
void FavoritesController::setCollectionSelectionModel( QItemSelectionModel *model )
 
158
{
 
159
  d->mCollectionSelectionModel = model;
 
160
}
 
161
 
 
162
QAction* FavoritesController::removeAction() const
 
163
{
 
164
  return d->mRemoveAction;
 
165
}
 
166
 
 
167
QAction* FavoritesController::moveUpAction() const
 
168
{
 
169
  return d->mMoveUpAction;
 
170
}
 
171
 
 
172
QAction* FavoritesController::moveDownAction() const
 
173
{
 
174
  return d->mMoveDownAction;
 
175
}
 
176
 
 
177
void FavoritesController::loadFavorite( const QString &name ) const
 
178
{
 
179
  Q_ASSERT( d->mCollectionSelectionModel );
 
180
 
 
181
  Akonadi::ETMViewStateSaver *saver = new Akonadi::ETMViewStateSaver;
 
182
  saver->setSelectionModel( d->mCollectionSelectionModel );
 
183
 
 
184
  KConfigGroup group( d->mConfig, sFavoritePrefix + name );
 
185
  if ( !group.isValid() ) {
 
186
    delete saver;
 
187
    return;
 
188
  }
 
189
 
 
190
  saver->restoreState( group );
 
191
}
 
192
 
 
193
void FavoritesController::saveFavorite( const QString &name )
 
194
{
 
195
  Q_ASSERT( d->mCollectionSelectionModel );
 
196
 
 
197
  Akonadi::ETMViewStateSaver saver;
 
198
  saver.setSelectionModel( d->mCollectionSelectionModel );
 
199
 
 
200
  KConfigGroup group( d->mConfig, sFavoritePrefix + name );
 
201
  saver.saveState( group );
 
202
  group.sync();
 
203
  d->mModel->setStringList( d->rereadFavoritesList() );
 
204
}
 
205
 
 
206
#include "favoritescontroller.moc"