~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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
    Copyright (c) 2009 Stephen Kelly <steveire@gmail.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 "dragdropmanager_p.h"
#include "specialcollectionattribute_p.h"
#include "collectionutils_p.h"

#include <QtGui/QApplication>
#include <QtGui/QDropEvent>
#include <QtGui/QMenu>

#include <KDE/KIcon>
#include <KDE/KLocale>
#include <KDE/KUrl>

#include "akonadi/collection.h"
#include "akonadi/entitytreemodel.h"

using namespace Akonadi;

DragDropManager::DragDropManager( QAbstractItemView *view )
  : mShowDropActionMenu( true ), mIsManualSortingActive( false ), m_view( view )
{
}

Akonadi::Collection DragDropManager::currentDropTarget( QDropEvent *event ) const
{
  const QModelIndex index = m_view->indexAt( event->pos() );
  Collection collection = m_view->model()->data( index, EntityTreeModel::CollectionRole ).value<Collection>();
  if ( !collection.isValid() ) {
    const Item item = m_view->model()->data( index, EntityTreeModel::ItemRole ).value<Item>();
    if ( item.isValid() )
      collection = m_view->model()->data( index.parent(), EntityTreeModel::CollectionRole ).value<Collection>();
  }

  return collection;
}

bool DragDropManager::dropAllowed( QDragMoveEvent *event ) const
{
  // Check if the collection under the cursor accepts this data type
  const Collection targetCollection = currentDropTarget( event );
  if ( targetCollection.isValid() ) {
    const QStringList supportedContentTypes = targetCollection.contentMimeTypes();

    const QMimeData *data = event->mimeData();
    const KUrl::List urls = KUrl::List::fromMimeData( data );
    foreach ( const KUrl &url, urls ) {
      const Collection collection = Collection::fromUrl( url );
      if ( collection.isValid() ) {
        if ( !supportedContentTypes.contains( Collection::mimeType() ) )
          break;

        // Check if we don't try to drop on one of the children
        if ( hasAncestor( m_view->indexAt( event->pos() ), collection.id() ) )
          break;
      } else { // This is an item.
        const QString type = url.queryItems()[ QString::fromLatin1( "type" ) ];
        if ( !supportedContentTypes.contains( type ) )
          break;
      }

      return true;
    }
  }

  return false;
}

bool DragDropManager::hasAncestor( const QModelIndex &_index, Collection::Id parentId ) const
{
  QModelIndex index( _index );
  while ( index.isValid() ) {
    if ( m_view->model()->data( index, EntityTreeModel::CollectionIdRole ).toLongLong() == parentId )
      return true;

    index = index.parent();
  }

  return false;
}

bool DragDropManager::processDropEvent( QDropEvent *event, bool &menuCanceled, bool dropOnItem )
{
  const Collection targetCollection = currentDropTarget( event );
  if ( !targetCollection.isValid() )
    return false;

  if ( !mIsManualSortingActive && !dropOnItem )
  {
    return false;
  }

  const QStringList supportedContentTypes = targetCollection.contentMimeTypes();
  
  const QMimeData *data = event->mimeData();
  const KUrl::List urls = KUrl::List::fromMimeData( data );
  foreach ( const KUrl &url, urls ) {
    const Collection collection = Collection::fromUrl( url );
    if( !collection.isValid() ) {
      if ( !dropOnItem ) {
        return false;
      }
    }
  } 
    
  int actionCount = 0;
  Qt::DropAction defaultAction;
  // TODO check if the source supports moving

  bool moveAllowed, copyAllowed, linkAllowed;
  moveAllowed = copyAllowed = linkAllowed = false;

  if ( (targetCollection.rights() & (Collection::CanCreateCollection | Collection::CanCreateItem))
       && (event->possibleActions() & Qt::MoveAction) ) {
    moveAllowed = true;
  }
  if ( (targetCollection.rights() & (Collection::CanCreateCollection | Collection::CanCreateItem))
        && (event->possibleActions() & Qt::CopyAction) ) {
    copyAllowed = true;
  }

  if ( (targetCollection.rights() & Collection::CanLinkItem) && (event->possibleActions() & Qt::LinkAction) ) {
    linkAllowed = true;
  }

  if ( mIsManualSortingActive && !dropOnItem ) {
    moveAllowed = true;
    copyAllowed = false;
    linkAllowed = false;
  }
    
  if ( !moveAllowed && !copyAllowed && !linkAllowed ) {
    kDebug() << "Cannot drop here:" << event->possibleActions() << m_view->model()->supportedDragActions() << m_view->model()->supportedDropActions();
    return false;
  }

  // first check whether the user pressed a modifier key to select a specific action
  if ( (QApplication::keyboardModifiers() & Qt::ControlModifier) &&
       (QApplication::keyboardModifiers() & Qt::ShiftModifier) ) {
    if ( linkAllowed ) {
      defaultAction = Qt::LinkAction;
      actionCount = 1;
    } else
      return false;
  } else if ( (QApplication::keyboardModifiers() & Qt::ControlModifier) ) {
    if ( copyAllowed ) {
      defaultAction = Qt::CopyAction;
      actionCount = 1;
    } else
      return false;
  } else if ( (QApplication::keyboardModifiers() & Qt::ShiftModifier) ) {
    if ( moveAllowed ) {
      defaultAction = Qt::MoveAction;
      actionCount = 1;
    } else
      return false;
  }

  if ( actionCount == 1 ) {
    kDebug() << "Selecting drop action" << defaultAction << ", there are no other possibilities";
    event->setDropAction( defaultAction );
    return true;
  }

  if ( !mShowDropActionMenu ) {
    if ( moveAllowed )
      defaultAction = Qt::MoveAction;
    else if ( copyAllowed )
      defaultAction = Qt::CopyAction;
    else if ( linkAllowed )
      defaultAction = Qt::LinkAction;
    else
      return false;
    event->setDropAction( defaultAction );
    return true;
  }

  // otherwise show up a menu to allow the user to select an action
  QMenu popup( m_view );
  QAction* moveDropAction = 0;
  QAction* copyDropAction = 0;
  QAction* linkAction = 0;
  QString sequence;

  if ( moveAllowed ) {
    sequence = QKeySequence( Qt::ShiftModifier ).toString();
    sequence.chop( 1 ); // chop superfluous '+'
    moveDropAction = popup.addAction( KIcon( QString::fromLatin1( "go-jump" ) ), i18n( "&Move Here" ) + QLatin1Char( '\t' ) + sequence );
  }

  if ( copyAllowed ) {
    sequence = QKeySequence( Qt::ControlModifier ).toString();
    sequence.chop( 1 ); // chop superfluous '+'
    copyDropAction = popup.addAction( KIcon( QString::fromLatin1( "edit-copy" ) ), i18n( "&Copy Here" ) + QLatin1Char( '\t' ) + sequence );
  }

  if ( linkAllowed ) {
    sequence = QKeySequence( Qt::ControlModifier + Qt::ShiftModifier ).toString();
    sequence.chop( 1 ); // chop superfluous '+'
    linkAction = popup.addAction( KIcon( QLatin1String( "edit-link" ) ), i18n( "&Link Here" ) + QLatin1Char( '\t' ) + sequence );
  }

  popup.addSeparator();
  popup.addAction( KIcon( QString::fromLatin1( "process-stop" ) ), i18n( "C&ancel" ) + QLatin1Char( '\t' ) + QKeySequence( Qt::Key_Escape ).toString() );

  QAction *activatedAction = popup.exec( QCursor::pos() );
  if ( !activatedAction ) {
    menuCanceled = true;
    return false;
  } else if ( activatedAction == moveDropAction ) {
    event->setDropAction( Qt::MoveAction );
  } else if ( activatedAction == copyDropAction ) {
    event->setDropAction( Qt::CopyAction );
  } else if ( activatedAction == linkAction ) {
    event->setDropAction( Qt::LinkAction );
  } else {
    menuCanceled = true;
    return false;
  }
  return true;
}

void DragDropManager::startDrag( Qt::DropActions supportedActions )
{
  QModelIndexList indexes;
  bool sourceDeletable = true;
  foreach ( const QModelIndex &index, m_view->selectionModel()->selectedRows() ) {
    if ( !m_view->model()->flags( index ).testFlag( Qt::ItemIsDragEnabled ) )
      continue;

    if ( sourceDeletable ) {
      Collection source = index.data( EntityTreeModel::CollectionRole ).value<Collection>();
      if ( !source.isValid() ) {
        // index points to an item
        source = index.data( EntityTreeModel::ParentCollectionRole ).value<Collection>();
        sourceDeletable = source.rights() & Collection::CanDeleteItem;
      } else {
        // index points to a collection
        sourceDeletable = ( source.rights() & Collection::CanDeleteCollection ) && !source.hasAttribute<SpecialCollectionAttribute>() && !CollectionUtils::isVirtual( source );
      }
    }
    indexes.append( index );
  }

  if ( indexes.isEmpty() )
    return;

  QMimeData *mimeData = m_view->model()->mimeData( indexes );
  if ( !mimeData )
    return;

  QDrag *drag = new QDrag( m_view );
  drag->setMimeData( mimeData );
  if ( indexes.size() > 1 ) {
    drag->setPixmap( KIcon( QLatin1String( "document-multiple" ) ).pixmap( QSize( 22, 22 ) ) );
  } else {
    QPixmap pixmap = indexes.first().data( Qt::DecorationRole ).value<QIcon>().pixmap( QSize( 22, 22 ) );
    if ( pixmap.isNull() )
      pixmap = KIcon( QLatin1String( "text-plain" ) ).pixmap( QSize( 22, 22 ) );
    drag->setPixmap( pixmap );
  }

  if ( !sourceDeletable )
    supportedActions &= ~Qt::MoveAction;

  Qt::DropAction defaultAction = Qt::IgnoreAction;
  if ( (QApplication::keyboardModifiers() & Qt::ControlModifier) &&
       (QApplication::keyboardModifiers() & Qt::ShiftModifier) ) {
    defaultAction = Qt::LinkAction;
  } else if ( (QApplication::keyboardModifiers() & Qt::ControlModifier) ) {
    defaultAction = Qt::CopyAction;
  } else if ( (QApplication::keyboardModifiers() & Qt::ShiftModifier) ) {
    defaultAction = Qt::MoveAction;
  }

  drag->exec( supportedActions, defaultAction );
}

bool DragDropManager::showDropActionMenu() const
{
  return mShowDropActionMenu;
}

void DragDropManager::setShowDropActionMenu( bool show )
{
  mShowDropActionMenu = show;
}

bool DragDropManager::isManualSortingActive() const
{
  return mIsManualSortingActive;
}

void DragDropManager::setManualSortingActive(bool active)
{
  mIsManualSortingActive = active;
}