2 * Copyright 2007-2009 Stephan Aßmus <superstippi@gmx.de>.
3 * All rights reserved. Distributed under the terms of the MIT License.
6 #include "MovePLItemsCommand.h"
18 #undef B_TRANSLATION_CONTEXT
19 #define B_TRANSLATION_CONTEXT "MediaPlayer-MovePLItemsCmd"
25 MovePLItemsCommand::MovePLItemsCommand(Playlist
* playlist
,
26 const int32
* indices
, int32 count
, int32 toIndex
)
30 fItems(count
> 0 ? new (nothrow
) PlaylistItem
*[count
] : NULL
),
31 fIndices(count
> 0 ? new (nothrow
) int32
[count
] : NULL
),
35 if (!indices
|| !fPlaylist
|| !fItems
|| !fIndices
) {
36 // indicate a bad object state
42 memset(fItems
, 0, sizeof(PlaylistItem
*) * fCount
);
43 memcpy(fIndices
, indices
, fCount
* sizeof(int32
));
45 // init original entry indices and
46 // adjust toIndex compensating for items that
47 // are removed before that index
48 int32 itemsBeforeIndex
= 0;
49 for (int32 i
= 0; i
< fCount
; i
++) {
50 fItems
[i
] = fPlaylist
->ItemAt(fIndices
[i
]);
51 if (fItems
[i
] == NULL
) {
52 // indicate a bad object state
57 if (fIndices
[i
] < fToIndex
)
60 fToIndex
-= itemsBeforeIndex
;
64 MovePLItemsCommand::~MovePLItemsCommand()
72 MovePLItemsCommand::InitCheck()
77 // analyse the move, don't return B_OK in case
78 // the container state does not change...
80 int32 index
= fIndices
[0];
81 // NOTE: fIndices == NULL if fCount < 1
83 if (index
!= fToIndex
) {
84 // a change is guaranteed
88 // the insertion index is the same as the index of the first
89 // moved item, a change only occures if the indices of the
90 // moved items is not contiguous
91 bool isContiguous
= true;
92 for (int32 i
= 1; i
< fCount
; i
++) {
93 if (fIndices
[i
] != index
+ 1) {
100 // the container state will not change because of the move
109 MovePLItemsCommand::Perform()
111 BAutolock
_(fPlaylist
);
115 // remember currently playling item in case we move it
116 PlaylistItem
* current
= fPlaylist
->ItemAt(fPlaylist
->CurrentItemIndex());
118 // remove refs from playlist
119 for (int32 i
= 0; i
< fCount
; i
++) {
120 // "- i" to account for the items already removed
121 fPlaylist
->RemoveItem(fIndices
[i
] - i
, false);
124 // add refs to playlist at the insertion index
125 int32 index
= fToIndex
;
126 for (int32 i
= 0; i
< fCount
; i
++) {
127 if (!fPlaylist
->AddItem(fItems
[i
], index
++)) {
135 // take care about currently played item
137 fPlaylist
->SetCurrentItemIndex(fPlaylist
->IndexOf(current
), false);
144 MovePLItemsCommand::Undo()
146 BAutolock
_(fPlaylist
);
150 // remember currently playling item in case we move it
151 PlaylistItem
* current
= fPlaylist
->ItemAt(fPlaylist
->CurrentItemIndex());
153 // remove refs from playlist
154 int32 index
= fToIndex
;
155 for (int32 i
= 0; i
< fCount
; i
++) {
156 fPlaylist
->RemoveItem(index
++, false);
159 // add ref to playlist at remembered indices
160 for (int32 i
= 0; i
< fCount
; i
++) {
161 if (!fPlaylist
->AddItem(fItems
[i
], fIndices
[i
])) {
169 // take care about currently played item
171 fPlaylist
->SetCurrentItemIndex(fPlaylist
->IndexOf(current
), false);
178 MovePLItemsCommand::GetName(BString
& name
)
181 name
<< B_TRANSLATE("Move Entries");
183 name
<< B_TRANSLATE("Move Entry");