~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
/*
    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 "etmviewstatesaver.h"

#include <QtCore/QModelIndex>
#include <QtGui/QItemSelection>
#include <QtGui/QTreeView>

#include "entitytreemodel.h"

using namespace Akonadi;

ETMViewStateSaver::ETMViewStateSaver(QObject* parent)
  : KViewStateSaver(parent)
{
}

QModelIndex ETMViewStateSaver::indexFromConfigString(const QAbstractItemModel *model, const QString& key) const
{
  if ( key.startsWith( QLatin1Char( 'x' ) ) )
    return QModelIndex();

  Entity::Id id = key.mid( 1 ).toLongLong();
  if ( id < 0 )
    return QModelIndex();

  if ( key.startsWith( QLatin1Char( 'c' ) ) )
  {
    QModelIndex idx = EntityTreeModel::modelIndexForCollection( model, Collection( id ) );
    if ( !idx.isValid() )
    {
      return QModelIndex();
    }
    return idx;
  }
  else if ( key.startsWith( QLatin1Char( 'i' ) ) )
  {
    QModelIndexList list = EntityTreeModel::modelIndexesForItem( model, Item( id ) );
    if ( list.isEmpty() )
    {
      return QModelIndex();
    }
    return list.first();
   }
  return QModelIndex();
}

QString ETMViewStateSaver::indexToConfigString(const QModelIndex& index) const
{
  if ( !index.isValid() )
    return QLatin1String( "x-1" );
  const Collection c = index.data( EntityTreeModel::CollectionRole ).value<Collection>();
  if ( c.isValid() )
    return QString::fromLatin1( "c%1" ).arg( c.id() );
  Entity::Id id = index.data( EntityTreeModel::ItemIdRole ).value<Entity::Id>();
  if ( id >= 0 )
    return QString::fromLatin1( "i%1" ).arg( id );
  return QString();
}

void ETMViewStateSaver::selectCollections(const Akonadi::Collection::List& list)
{
  QStringList colStrings;
  foreach(const Collection &col, list)
    colStrings << QString::fromLatin1( "c%1" ).arg( col.id() );
  restoreSelection(colStrings);
}

void ETMViewStateSaver::selectCollections(const QList< Collection::Id >& list)
{
  QStringList colStrings;
  foreach(const Collection::Id &colId, list)
    colStrings << QString::fromLatin1( "c%1" ).arg( colId );
  restoreSelection(colStrings);
}

void ETMViewStateSaver::selectItems(const Akonadi::Item::List& list)
{
  QStringList itemStrings;
  foreach(const Item &item, list)
    itemStrings << QString::fromLatin1( "i%1" ).arg( item.id() );
  restoreSelection(itemStrings);
}

void ETMViewStateSaver::selectItems(const QList< Item::Id >& list)
{
  QStringList itemStrings;
  foreach(const Item::Id &itemId, list)
    itemStrings << QString::fromLatin1( "i%1" ).arg( itemId );
  restoreSelection(itemStrings);
}