2 * Copyright 2007-2009 Stephan Aßmus <superstippi@gmx.de>.
3 * All rights reserved. Distributed under the terms of the MIT License.
7 #include "ImportPLItemsCommand.h"
17 #include "PlaylistItem.h"
20 #undef B_TRANSLATION_CONTEXT
21 #define B_TRANSLATION_CONTEXT "MediaPlayer-ImportPLItemsCmd"
27 ImportPLItemsCommand::ImportPLItemsCommand(Playlist
* playlist
,
28 const BMessage
* refsMessage
, int32 toIndex
)
48 temp
.AppendItems(refsMessage
);
50 fNewCount
= temp
.CountItems();
54 fNewItems
= new (nothrow
) PlaylistItem
*[fNewCount
];
57 memset(fNewItems
, 0, fNewCount
* sizeof(PlaylistItem
*));
61 for (int32 i
= 0; i
< fNewCount
; i
++) {
62 if (!Playlist::ExtraMediaExists(playlist
, temp
.ItemAtFast(i
))) {
63 fNewItems
[added
] = temp
.ItemAtFast(i
)->Clone();
64 if (fNewItems
[added
] == NULL
) {
65 // indicate bad object init
66 _CleanUp(fNewItems
, fNewCount
, true);
74 fPlaylingIndex
= fPlaylist
->CurrentItemIndex();
76 if (fToIndex
== APPEND_INDEX_REPLACE_PLAYLIST
) {
77 fOldCount
= fPlaylist
->CountItems();
79 fOldItems
= new (nothrow
) PlaylistItem
*[fOldCount
];
81 // indicate bad object init
82 _CleanUp(fNewItems
, fNewCount
, true);
84 memset(fOldItems
, 0, fOldCount
* sizeof(PlaylistItem
*));
88 for (int32 i
= 0; i
< fOldCount
; i
++) {
89 fOldItems
[i
] = fPlaylist
->ItemAtFast(i
)->Clone();
90 if (fOldItems
[i
] == NULL
) {
91 // indicate bad object init
92 _CleanUp(fNewItems
, fNewCount
, true);
99 ImportPLItemsCommand::~ImportPLItemsCommand()
101 _CleanUp(fOldItems
, fOldCount
, fItemsAdded
);
102 _CleanUp(fNewItems
, fNewCount
, !fItemsAdded
);
107 ImportPLItemsCommand::InitCheck()
109 if (!fPlaylist
|| !fNewItems
)
116 ImportPLItemsCommand::Perform()
118 BAutolock
_(fPlaylist
);
122 if (fToIndex
== APPEND_INDEX_APPEND_LAST
)
123 fToIndex
= fPlaylist
->CountItems();
125 int32 index
= fToIndex
;
126 if (fToIndex
== APPEND_INDEX_REPLACE_PLAYLIST
) {
127 fPlaylist
->MakeEmpty(false);
131 bool startPlaying
= fPlaylist
->CountItems() == 0;
133 // add refs to playlist at the insertion index
134 for (int32 i
= 0; i
< fNewCount
; i
++) {
135 if (!fPlaylist
->AddItem(fNewItems
[i
], index
++))
141 fPlaylist
->SetCurrentItemIndex(0);
149 ImportPLItemsCommand::Undo()
151 BAutolock
_(fPlaylist
);
155 if (fToIndex
== APPEND_INDEX_REPLACE_PLAYLIST
) {
156 // remove new items from playlist and restore old refs
157 fPlaylist
->MakeEmpty(false);
158 for (int32 i
= 0; i
< fOldCount
; i
++) {
159 if (!fPlaylist
->AddItem(fOldItems
[i
], i
))
162 // Restore previously playing item
163 if (fPlaylingIndex
>= 0)
164 fPlaylist
->SetCurrentItemIndex(fPlaylingIndex
, false);
166 // remove new items from playlist
167 for (int32 i
= 0; i
< fNewCount
; i
++) {
168 fPlaylist
->RemoveItem(fToIndex
);
177 ImportPLItemsCommand::GetName(BString
& name
)
180 name
<< B_TRANSLATE("Import Entries");
182 name
<< B_TRANSLATE("Import Entry");