~ubuntu-branches/ubuntu/quantal/kdepimlibs/quantal-proposed

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
    Copyright (C) 2010 Klarälvdalens Datakonsult AB,
        a KDAB Group company, info@kdab.net,
        author Stephen Kelly <stephen@kdab.com>

    This library is free software; you can redistribute it and/or modify it
    under the terms of the GNU Library General Public License as published by
    the Free Software Foundation; either version 2 of the License, or (at your
    option) any later version.

    This library is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
    License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to the
    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
*/


#include "entityorderproxymodel.h"

#include <QMimeData>

#include <KDE/KConfigGroup>

#include "collection.h"
#include "item.h"
#include "entitytreemodel.h"

namespace Akonadi
{

class EntityOrderProxyModelPrivate
{
public:
  EntityOrderProxyModelPrivate( EntityOrderProxyModel *qq )
    : q_ptr( qq )
  {

  }

  void saveOrder( const QModelIndex &index );

  KConfigGroup m_orderConfig;

  Q_DECLARE_PUBLIC(EntityOrderProxyModel)
  EntityOrderProxyModel * const q_ptr;

};

}

using namespace Akonadi;

EntityOrderProxyModel::EntityOrderProxyModel( QObject* parent )
  : QSortFilterProxyModel(parent), d_ptr( new EntityOrderProxyModelPrivate( this ) )
{
  setDynamicSortFilter(true);
}

EntityOrderProxyModel::~EntityOrderProxyModel()
{
  delete d_ptr;
}

void EntityOrderProxyModel::setOrderConfig( KConfigGroup& configGroup )
{
  Q_D( EntityOrderProxyModel );
  layoutAboutToBeChanged();
  d->m_orderConfig = configGroup;
  layoutChanged();
}

bool EntityOrderProxyModel::lessThan( const QModelIndex& left, const QModelIndex& right ) const
{
  Q_D( const EntityOrderProxyModel );

  if ( !d->m_orderConfig.isValid() )
    return QSortFilterProxyModel::lessThan( left, right );

  Collection col = left.data( EntityTreeModel::ParentCollectionRole ).value<Collection>();

  if ( !d->m_orderConfig.hasKey( QString::number( col.id() ) ) )
    return QSortFilterProxyModel::lessThan( left, right );

  const QStringList list = d->m_orderConfig.readEntry( QString::number( col.id() ), QStringList() );

  if ( list.isEmpty() )
    return QSortFilterProxyModel::lessThan( left, right );

  const QString leftValue = configString( left );
  const QString rightValue = configString( right );

  const int leftPosition = list.indexOf( leftValue );
  const int rightPosition = list.indexOf( rightValue );

  if ( leftPosition < 0 || rightPosition < 0 )
    return QSortFilterProxyModel::lessThan( left, right );

  return leftPosition < rightPosition;
}

bool EntityOrderProxyModel::dropMimeData( const QMimeData* data, Qt::DropAction action, int row, int column, const QModelIndex& parent )
{
  Q_D( EntityOrderProxyModel );

  if ( !d->m_orderConfig.isValid() )
    return QSortFilterProxyModel::dropMimeData( data, action, row, column, parent );

  if ( !data->hasFormat( QLatin1String( "text/uri-list" ) ) )
    return QSortFilterProxyModel::dropMimeData( data, action, row, column, parent );

  if ( row == -1 )
    return QSortFilterProxyModel::dropMimeData( data, action, row, column, parent );

  bool containsMove = false;

  const KUrl::List urls = KUrl::List::fromMimeData( data );

  Collection parentCol;

  if (parent.isValid())
    parentCol = parent.data( EntityTreeModel::CollectionRole ).value<Collection>();
  else
  {
    if (!hasChildren(parent))
      return QSortFilterProxyModel::dropMimeData( data, action, row, column, parent );

    const QModelIndex targetIndex = index( 0, column, parent );

    parentCol = targetIndex.data( EntityTreeModel::ParentCollectionRole ).value<Collection>();
  }

  QStringList droppedList;
  foreach ( const KUrl &url, urls ) {
    Collection col = Collection::fromUrl( url );

    if ( !col.isValid() )
    {
      Item item = Item::fromUrl( url );
      if ( !item.isValid() )
        continue;

      const QModelIndexList list = EntityTreeModel::modelIndexesForItem( this, item );
      if ( list.isEmpty() )
        continue;

      if ( !containsMove && list.first().data( EntityTreeModel::ParentCollectionRole ).value<Collection>().id() != parentCol.id() )
        containsMove = true;

      droppedList << configString( list.first() );
    } else {
      const QModelIndex idx = EntityTreeModel::modelIndexForCollection( this, col );
      if ( !idx.isValid() )
        continue;

      if ( !containsMove && idx.data( EntityTreeModel::ParentCollectionRole ).value<Collection>().id() != parentCol.id() )
        containsMove = true;

      droppedList << configString( idx );
    }
  }

  QStringList existingList;
  if ( d->m_orderConfig.hasKey( QString::number( parentCol.id() ) ) ) {
    existingList = d->m_orderConfig.readEntry( QString::number( parentCol.id() ), QStringList() );
  } else {
    const QModelIndex sourceIndex = mapToSource( parent );
    const int rowCount = sourceModel()->rowCount( sourceIndex );
    for (int row = 0; row < rowCount; ++row) {
      static const int column = 0;
      const QModelIndex idx = sourceModel()->index( row, column, sourceIndex );
      existingList.append( configString( idx ) );
    }
  }
  const int numberOfDroppedElement( droppedList.size() );
  for ( int i = 0; i < numberOfDroppedElement; ++i )
  {
    const QString droppedItem = droppedList.at( i );
    const int existingIndex = existingList.indexOf( droppedItem );
    existingList.removeAt( existingIndex );
    existingList.insert( row + i - (existingIndex > row ? 0 : 1), droppedList.at( i ) );
  }

  d->m_orderConfig.writeEntry( QString::number( parentCol.id() ), existingList );

  if ( containsMove )
  {
    bool result = QSortFilterProxyModel::dropMimeData( data, action, row, column, parent );
    invalidate();
    return result;
  }
  invalidate();
  return true;
}

QModelIndexList EntityOrderProxyModel::match( const QModelIndex& start, int role, const QVariant& value, int hits, Qt::MatchFlags flags ) const
{
  if ( role < Qt::UserRole )
    return QSortFilterProxyModel::match( start, role, value, hits, flags );

  QModelIndexList list;
  QModelIndex proxyIndex;
  foreach ( const QModelIndex &idx, sourceModel()->match( mapToSource( start ), role, value, hits, flags ) ) {
    proxyIndex = mapFromSource( idx );
    if ( proxyIndex.isValid() )
      list << proxyIndex;
  }

  return list;
}

void EntityOrderProxyModelPrivate::saveOrder( const QModelIndex &parent )
{
  Q_Q( const EntityOrderProxyModel );
  int rowCount = q->rowCount( parent );

  if ( rowCount == 0 )
    return;

  static const int column = 0;
  QModelIndex childIndex = q->index( 0, column, parent );

  QString parentKey = q->parentConfigString( childIndex );

  if ( parentKey.isEmpty() )
    return;

  QStringList list;

  list << q->configString( childIndex );
  saveOrder( childIndex );

  for ( int row = 1; row < rowCount; ++row )
  {
    childIndex = q->index( row, column, parent );
    list << q->configString( childIndex );
    saveOrder( childIndex );
  }

  m_orderConfig.writeEntry( parentKey, list );
}

QString EntityOrderProxyModel::parentConfigString( const QModelIndex& index ) const
{
  const Collection col = index.data( EntityTreeModel::ParentCollectionRole ).value<Collection>();

  Q_ASSERT( col.isValid() );
  if ( !col.isValid() )
    return QString();

  return QString::number( col.id() );
}

QString EntityOrderProxyModel::configString( const QModelIndex& index ) const
{
  Entity::Id eId = index.data( EntityTreeModel::ItemIdRole ).toLongLong();
  if ( eId != -1 )
  {
    return QString::fromLatin1( "i" ) + QString::number( eId );
  }
  eId = index.data( EntityTreeModel::CollectionIdRole ).toLongLong();
  if ( eId != -1 )
  {
    return QString::fromLatin1( "c" ) + QString::number( eId );
  }
  Q_ASSERT( !"Invalid entity" );
  return QString();
}

void EntityOrderProxyModel::saveOrder()
{
  Q_D( EntityOrderProxyModel );
  d->saveOrder( QModelIndex() );
  d->m_orderConfig.sync();
}

void EntityOrderProxyModel::clearOrder( const QModelIndex& parent )
{
  Q_D( EntityOrderProxyModel );

  const QString parentKey = parentConfigString( index( 0, 0, parent ) );

  if ( parentKey.isEmpty() )
    return;

  d->m_orderConfig.deleteEntry( parentKey );
  invalidate();
}

void EntityOrderProxyModel::clearTreeOrder()
{
  Q_D( EntityOrderProxyModel );
  d->m_orderConfig.deleteGroup();
  invalidate();
}