2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 using System.Collections.Generic;
22 namespace Tizen.Content.MediaContent
25 /// Provides commands to manage playlists in the database.
27 /// <seealso cref="Playlist"/>
28 public class PlaylistCommand : MediaCommand
31 /// Initializes a new instance of the <see cref="PlaylistCommand"/> class with the specified <see cref="MediaDatabase"/>.
33 /// <param name="database">A <see cref="MediaDatabase"/> that the commands run on.</param>
34 /// <exception cref="ArgumentNullException"><paramref name="database"/> is null.</exception>
35 /// <exception cref="ObjectDisposedException"><paramref name="database"/> has already been disposed of.</exception>
36 public PlaylistCommand(MediaDatabase database) : base(database)
41 /// Retrieves the number of playlists.
43 /// <returns>The number of playlists.</returns>
44 /// <exception cref="InvalidOperationException">The <see cref="MediaDatabase"/> is disconnected.</exception>
45 /// <exception cref="ObjectDisposedException">The <see cref="MediaDatabase"/> has already been disposed of.</exception>
46 /// <exception cref="MediaDatabaseException">An error occurred while executing the command.</exception>
53 /// Retrieves the number of playlists with <see cref="CountArguments"/>.
55 /// <param name="arguments">The criteria to use to filter. This value can be null.</param>
56 /// <returns>The number of playlists.</returns>
57 /// <exception cref="InvalidOperationException">The <see cref="MediaDatabase"/> is disconnected.</exception>
58 /// <exception cref="ObjectDisposedException">The <see cref="MediaDatabase"/> has already been disposed of.</exception>
59 /// <exception cref="MediaDatabaseException">An error occurred while executing the command.</exception>
60 public int Count(CountArguments arguments)
64 return CommandHelper.Count(Interop.Playlist.GetPlaylistCount, arguments);
68 /// Retrieves the play order of the member.
70 /// <param name="playlistId">The playlist id.</param>
71 /// <param name="memberId">The member id of the playlist.</param>
72 /// <returns>The <see cref="MediaDataReader{TRecord}"/> containing the results.</returns>
73 /// <exception cref="InvalidOperationException">The <see cref="MediaDatabase"/> is disconnected.</exception>
74 /// <exception cref="ObjectDisposedException">The <see cref="MediaDatabase"/> has already been disposed of.</exception>
75 /// <exception cref="MediaDatabaseException">An error occurred while executing the command.</exception>
76 /// <exception cref="ArgumentOutOfRangeException">
77 /// <paramref name="playlistId"/> is less than or equal to zero.\n
79 /// <paramref name="memberId"/> is less than or equal to zero.
81 public int GetPlayOrder(int playlistId, int memberId)
87 throw new ArgumentOutOfRangeException(nameof(playlistId), playlistId,
88 "Playlist id can't be less than or equal to zero.");
93 throw new ArgumentOutOfRangeException(nameof(memberId), memberId,
94 "Member id can't be less than or equal to zero.");
97 Interop.Playlist.GetPlayOrder(playlistId, memberId, out var order).ThrowIfError("Failed to query");
103 /// Deletes a playlist from the database.
105 /// <privilege>http://tizen.org/privilege/content.write</privilege>
106 /// <param name="playlistId">The playlist id to delete.</param>
107 /// <returns>true if the matched record was found and deleted, otherwise false.</returns>
108 /// <exception cref="InvalidOperationException">The <see cref="MediaDatabase"/> is disconnected.</exception>
109 /// <exception cref="ObjectDisposedException">The <see cref="MediaDatabase"/> has already been disposed of.</exception>
110 /// <exception cref="MediaDatabaseException">An error occurred while executing the command.</exception>
111 /// <exception cref="ArgumentOutOfRangeException"><paramref name="playlistId"/> is less than or equal to zero.</exception>
112 /// <exception cref="UnauthorizedAccessException">The caller has no required privilege.</exception>
113 public bool Delete(int playlistId)
119 throw new ArgumentOutOfRangeException(nameof(playlistId), playlistId,
120 "Playlist id can't be less than or equal to zero.");
123 if (Select(playlistId) == null)
128 CommandHelper.Delete(Interop.Playlist.Delete, playlistId);
133 /// Inserts a playlist into the database from the specified m3u file.
136 /// If you want to access internal storage, you should add privilege http://tizen.org/privilege/mediastorage.\n
137 /// If you want to access external storage, you should add privilege http://tizen.org/privilege/externalstorage.
139 /// <privilege>http://tizen.org/privilege/content.write</privilege>
140 /// <privilege>http://tizen.org/privilege/mediastorage</privilege>
141 /// <privilege>http://tizen.org/privilege/externalstorage</privilege>
142 /// <param name="name">The name of playlist.</param>
143 /// <param name="path">The path to a m3u file to import.</param>
144 /// <returns>The <see cref="Playlist"/> instance that contains the record information inserted.</returns>
145 /// <exception cref="InvalidOperationException">The <see cref="MediaDatabase"/> is disconnected.</exception>
146 /// <exception cref="ObjectDisposedException">The <see cref="MediaDatabase"/> has already been disposed of.</exception>
147 /// <exception cref="MediaDatabaseException">An error occurred while executing the command.</exception>
148 /// <exception cref="ArgumentNullException">
149 /// <paramref name="name"/> is null.\n
151 /// <paramref name="path"/> is null.
153 /// <exception cref="ArgumentException">
154 /// <paramref name="name"/> is a zero-length string.\n
156 /// <paramref name="path"/> is a zero-length string, contains only white space.
158 /// <exception cref="FileNotFoundException"><paramref name="path"/> does not exists.</exception>
159 /// <exception cref="UnauthorizedAccessException">The caller has no required privilege.</exception>
160 public Playlist InsertFromFile(string name, string path)
166 throw new ArgumentNullException(nameof(name));
169 if (name.Length == 0)
171 throw new ArgumentException("Playlist name can't be an empty string.");
174 ValidationUtil.ValidateNotNullOrEmpty(path, nameof(path));
176 if (File.Exists(path) == false)
178 throw new FileNotFoundException("The specified path does not exists.", path);
181 IntPtr handle = IntPtr.Zero;
182 Interop.Playlist.ImportFromFile(path, name, out handle).ThrowIfError("Failed to insert");
186 return new Playlist(handle);
190 if (handle != IntPtr.Zero)
192 Interop.Playlist.Destroy(handle);
197 /// Exports a playlist to a m3u file.
200 /// If the file already exists in the file system, then it will be overwritten.\n
202 /// If you want to access internal storage, you should add privilege http://tizen.org/privilege/mediastorage.\n
203 /// If you want to access external storage, you should add privilege http://tizen.org/privilege/externalstorage.
205 /// <privilege>http://tizen.org/privilege/mediastorage</privilege>
206 /// <privilege>http://tizen.org/privilege/externalstorage</privilege>
207 /// <param name="playlistId">The playlist id to export.</param>
208 /// <param name="path">The path to a m3u file.</param>
209 /// <exception cref="InvalidOperationException">The <see cref="MediaDatabase"/> is disconnected.</exception>
210 /// <exception cref="ObjectDisposedException">The <see cref="MediaDatabase"/> has already been disposed of.</exception>
211 /// <exception cref="MediaDatabaseException">An error occurred while executing the command.</exception>
212 /// <exception cref="ArgumentNullException"><paramref name="path"/> is null.</exception>
213 /// <exception cref="ArgumentException"><paramref name="path"/> is a zero-length string, contains only white space.</exception>
214 /// <exception cref="ArgumentOutOfRangeException"><paramref name="playlistId"/> is less than or equal to zero.</exception>
215 /// <exception cref="RecordNotFoundException">No matching playlist exists.</exception>
216 /// <exception cref="UnauthorizedAccessException">The caller has no required privilege.</exception>
217 public void ExportToFile(int playlistId, string path)
221 ValidationUtil.ValidateNotNullOrEmpty(path, nameof(path));
225 throw new ArgumentOutOfRangeException(nameof(playlistId), playlistId,
226 "Playlist id can't be less than or equal to zero.");
229 IntPtr handle = IntPtr.Zero;
232 Interop.Playlist.GetPlaylistFromDb(playlistId, out handle).ThrowIfError("Failed to query");
234 if (handle == IntPtr.Zero)
236 throw new RecordNotFoundException("No matching playlist exists.");
239 Interop.Playlist.ExportToFile(handle, path).ThrowIfError("Failed to export");
243 if (handle != IntPtr.Zero)
245 Interop.Playlist.Destroy(handle);
251 /// Inserts a playlist into the database with the specified name.
253 /// <privilege>http://tizen.org/privilege/content.write</privilege>
254 /// <param name="name">The name of playlist.</param>
255 /// <returns>The <see cref="Playlist"/> instance that contains the record information inserted.</returns>
256 /// <exception cref="InvalidOperationException">The <see cref="MediaDatabase"/> is disconnected.</exception>
257 /// <exception cref="ObjectDisposedException">The <see cref="MediaDatabase"/> has already been disposed of.</exception>
258 /// <exception cref="MediaDatabaseException">An error occurred while executing the command.</exception>
259 /// <exception cref="ArgumentNullException"><paramref name="name"/> is null.</exception>
260 /// <exception cref="ArgumentException"><paramref name="name"/> is a zero-length string.</exception>
261 /// <exception cref="UnauthorizedAccessException">The caller has no required privilege.</exception>
262 public Playlist Insert(string name)
264 return Insert(name, null);
268 /// Inserts a playlist into the database with the specified name and thumbnail path.
270 /// <privilege>http://tizen.org/privilege/content.write</privilege>
271 /// <param name="name">The name of playlist.</param>
272 /// <param name="thumbnailPath">The path of thumbnail for playlist. This value can be null.</param>
273 /// <returns>The <see cref="Playlist"/> instance that contains the record information inserted.</returns>
274 /// <exception cref="InvalidOperationException">The <see cref="MediaDatabase"/> is disconnected.</exception>
275 /// <exception cref="ObjectDisposedException">The <see cref="MediaDatabase"/> has already been disposed of.</exception>
276 /// <exception cref="MediaDatabaseException">An error occurred while executing the command.</exception>
277 /// <exception cref="ArgumentNullException"><paramref name="name"/> is null.</exception>
278 /// <exception cref="ArgumentException"><paramref name="name"/> is a zero-length string.</exception>
279 /// <exception cref="UnauthorizedAccessException">The caller has no required privilege.</exception>
280 public Playlist Insert(string name, string thumbnailPath)
286 throw new ArgumentNullException(nameof(name));
289 if (name.Length == 0)
291 throw new ArgumentException("Playlist name can't be an empty string.");
294 IntPtr handle = IntPtr.Zero;
295 Interop.Playlist.Create(out handle).ThrowIfError("Failed to insert");
299 Interop.Playlist.SetName(handle, name).ThrowIfError("Failed to insert");
301 if (thumbnailPath != null)
303 Interop.Playlist.SetThumbnailPath(handle, thumbnailPath).ThrowIfError("Failed to insert");
306 Interop.Playlist.Insert(handle).ThrowIfError("Failed to insert");
307 return new Playlist(handle);
311 if (handle != IntPtr.Zero)
313 Interop.Playlist.Destroy(handle);
319 /// Retrieves the playlists.
321 /// <returns>The <see cref="MediaDataReader{TRecord}"/> containing the results.</returns>
322 /// <exception cref="InvalidOperationException">The <see cref="MediaDatabase"/> is disconnected.</exception>
323 /// <exception cref="ObjectDisposedException">The <see cref="MediaDatabase"/> has already been disposed of.</exception>
324 /// <exception cref="MediaDatabaseException">An error occurred while executing the command.</exception>
325 public MediaDataReader<Playlist> Select()
331 /// Retrieves the playlists with <see cref="SelectArguments"/>.
333 /// <param name="filter">The criteria to use to filter. This value can be null.</param>
334 /// <returns>The <see cref="MediaDataReader{TRecord}"/> containing the results.</returns>
335 /// <exception cref="InvalidOperationException">The <see cref="MediaDatabase"/> is disconnected.</exception>
336 /// <exception cref="ObjectDisposedException">The <see cref="MediaDatabase"/> has already been disposed of.</exception>
337 /// <exception cref="MediaDatabaseException">An error occurred while executing the command.</exception>
338 public MediaDataReader<Playlist> Select(SelectArguments filter)
342 return CommandHelper.Select(filter, Interop.Playlist.ForeachPlaylistFromDb,
343 Playlist.FromHandle);
347 /// Retrieves the playlist with the specified playlist id.
349 /// <param name="playlistId">The playlist id to select.</param>
350 /// <returns>The <see cref="Playlist"/> instance if the matched record was found in the database, otherwise null.</returns>
351 /// <exception cref="InvalidOperationException">The <see cref="MediaDatabase"/> is disconnected.</exception>
352 /// <exception cref="ObjectDisposedException">The <see cref="MediaDatabase"/> has already been disposed of.</exception>
353 /// <exception cref="MediaDatabaseException">An error occurred while executing the command.</exception>
354 /// <exception cref="ArgumentOutOfRangeException"><paramref name="playlistId"/> is less than or equal to zero.</exception>
355 public Playlist Select(int playlistId)
361 throw new ArgumentOutOfRangeException(nameof(playlistId), playlistId,
362 "Playlist id can't be less than or equal to zero.");
365 IntPtr handle = IntPtr.Zero;
369 Interop.Playlist.GetPlaylistFromDb(playlistId, out handle).ThrowIfError("Failed to query");
371 if (handle == IntPtr.Zero)
376 return new Playlist(handle);
380 if (handle != IntPtr.Zero)
382 Interop.Playlist.Destroy(handle);
388 /// Retrieves the number of media info of the playlist.
390 /// <param name="playlistId">The playlist id to count media added to the playlist.</param>
391 /// <returns>The number of media info.</returns>
392 /// <exception cref="InvalidOperationException">The <see cref="MediaDatabase"/> is disconnected.</exception>
393 /// <exception cref="ObjectDisposedException">The <see cref="MediaDatabase"/> has already been disposed of.</exception>
394 /// <exception cref="MediaDatabaseException">An error occurred while executing the command.</exception>
395 /// <exception cref="ArgumentOutOfRangeException"><paramref name="playlistId"/> is less than or equal to zero.</exception>
396 public int CountMember(int playlistId)
398 return CountMember(playlistId, null);
402 /// Retrieves the number of media info of the playlist with <see cref="CountArguments"/>.
404 /// <param name="playlistId">The playlist id to count media added to the playlist.</param>
405 /// <param name="arguments">The criteria to use to filter. This value can be null.</param>
406 /// <returns>The number of media info.</returns>
407 /// <exception cref="InvalidOperationException">The <see cref="MediaDatabase"/> is disconnected.</exception>
408 /// <exception cref="ObjectDisposedException">The <see cref="MediaDatabase"/> has already been disposed of.</exception>
409 /// <exception cref="MediaDatabaseException">An error occurred while executing the command.</exception>
410 /// <exception cref="ArgumentOutOfRangeException"><paramref name="playlistId"/> is less than or equal to zero.</exception>
411 public int CountMember(int playlistId, CountArguments arguments)
417 throw new ArgumentOutOfRangeException(nameof(playlistId), playlistId,
418 "Playlist id can't be less than or equal to zero.");
421 return CommandHelper.Count(Interop.Playlist.GetMediaCountFromDb, playlistId, arguments);
425 private static List<PlaylistMember> GetMembers(int playlistId, SelectArguments arguments)
427 using (var filter = QueryArguments.ToNativeHandle(arguments))
429 Exception caught = null;
430 List<PlaylistMember> list = new List<PlaylistMember>();
432 Interop.Playlist.ForeachMediaFromDb(playlistId, filter, (memberId, mediaInfoHandle, _) =>
436 list.Add(new PlaylistMember(memberId, MediaInfo.FromHandle(mediaInfoHandle)));
445 }).ThrowIfError("Failed to query");
457 /// Retrieves the member id of the media in the playlist.
459 /// <param name="playlistId">The playlist id.</param>
460 /// <param name="mediaId">The media id.</param>
461 /// <returns>The member id if the member was found in the playlist, otherwise -1.</returns>
462 /// <exception cref="InvalidOperationException">The <see cref="MediaDatabase"/> is disconnected.</exception>
463 /// <exception cref="ObjectDisposedException">The <see cref="MediaDatabase"/> has already been disposed of.</exception>
464 /// <exception cref="MediaDatabaseException">An error occurred while executing the command.</exception>
465 /// <exception cref="ArgumentOutOfRangeException"><paramref name="playlistId"/> is less than or equal to zero.</exception>
466 /// <exception cref="ArgumentNullException"><paramref name="mediaId"/> is null.</exception>
467 /// <exception cref="ArgumentException"><paramref name="mediaId"/> is a zero-length string, contains only white space.</exception>
468 public int GetMemberId(int playlistId, string mediaId)
472 ValidationUtil.ValidateNotNullOrEmpty(mediaId, nameof(mediaId));
476 throw new ArgumentOutOfRangeException(nameof(playlistId), playlistId,
477 "Playlist id can't be less than or equal to zero.");
480 var reader = SelectMember(playlistId, new SelectArguments()
482 FilterExpression = $"{MediaInfoColumns.Id}='{mediaId}'"
486 return reader.Current?.MemberId ?? -1;
490 /// Retrieves the media info of the playlist.
492 /// <param name="playlistId">The playlist id to query with.</param>
493 /// <returns>The <see cref="MediaDataReader{TRecord}"/> containing the results.</returns>
494 /// <exception cref="InvalidOperationException">The <see cref="MediaDatabase"/> is disconnected.</exception>
495 /// <exception cref="ObjectDisposedException">The <see cref="MediaDatabase"/> has already been disposed of.</exception>
496 /// <exception cref="MediaDatabaseException">An error occurred while executing the command.</exception>
497 /// <exception cref="ArgumentOutOfRangeException"><paramref name="playlistId"/> is less than or equal to zero.</exception>
498 public MediaDataReader<PlaylistMember> SelectMember(int playlistId)
500 return SelectMember(playlistId, null);
504 /// Retrieves the media info of the playlist with <see cref="SelectArguments"/>.
506 /// <param name="playlistId">The playlist id to query with.</param>
507 /// <param name="filter">The criteria to use to filter. This value can be null.</param>
508 /// <returns>The <see cref="MediaDataReader{TRecord}"/> containing the results.</returns>
509 /// <exception cref="InvalidOperationException">The <see cref="MediaDatabase"/> is disconnected.</exception>
510 /// <exception cref="ObjectDisposedException">The <see cref="MediaDatabase"/> has already been disposed of.</exception>
511 /// <exception cref="MediaDatabaseException">An error occurred while executing the command.</exception>
512 /// <exception cref="ArgumentOutOfRangeException"><paramref name="playlistId"/> is less than or equal to zero.</exception>
513 public MediaDataReader<PlaylistMember> SelectMember(int playlistId, SelectArguments filter)
519 throw new ArgumentOutOfRangeException(nameof(playlistId), playlistId,
520 "Playlist id can't be less than or equal to zero.");
523 return new MediaDataReader<PlaylistMember>(GetMembers(playlistId, filter));
527 /// Updates a playlist with the specified values.
529 /// <privilege>http://tizen.org/privilege/content.write</privilege>
530 /// <param name="playlistId">The playlist id to update.</param>
531 /// <param name="values">The values for update.</param>
532 /// <returns>true if the matched record was found and updated, otherwise false.</returns>
533 /// <remarks>Only values set in <see cref="PlaylistUpdateValues"/> are updated.</remarks>
534 /// <exception cref="InvalidOperationException">The <see cref="MediaDatabase"/> is disconnected.</exception>
535 /// <exception cref="ObjectDisposedException">The <see cref="MediaDatabase"/> has already been disposed of.</exception>
536 /// <exception cref="MediaDatabaseException">An error occurred while executing the command.</exception>
537 /// <exception cref="ArgumentNullException"><paramref name="values"/> is null.</exception>
538 /// <exception cref="ArgumentOutOfRangeException"><paramref name="playlistId"/> is less than or equal to zero.</exception>
539 /// <exception cref="UnauthorizedAccessException">The caller has no required privilege.</exception>
540 public bool Update(int playlistId, PlaylistUpdateValues values)
546 throw new ArgumentOutOfRangeException(nameof(playlistId), playlistId,
547 "Playlist id can't be less than or equal to zero.");
552 throw new ArgumentNullException(nameof(values));
555 if (CommandHelper.Count(
556 Interop.Playlist.GetPlaylistCount, $"{PlaylistColumns.Id}={playlistId}") == 0)
561 Interop.Playlist.Create(out var handle).ThrowIfError("Failed to update");
565 if (values.Name != null)
567 Interop.Playlist.SetName(handle, values.Name).ThrowIfError("Failed to update");
570 if (values.ThumbnailPath != null)
572 Interop.Playlist.SetThumbnailPath(handle, values.ThumbnailPath).ThrowIfError("Failed to update");
575 Interop.Playlist.Update(playlistId, handle).ThrowIfError("Failed to update");
580 if (handle != IntPtr.Zero)
582 Interop.Playlist.Destroy(handle);
588 /// Adds media to a playlist.
590 /// <param name="playlistId">The playlist id that the media will be added to.</param>
591 /// <param name="mediaId">The media id to add to the playlist.</param>
592 /// <returns>true if the matched record was found and updated, otherwise false.</returns>
593 /// <remarks>The invalid media id will be ignored.</remarks>
594 /// <exception cref="InvalidOperationException">The <see cref="MediaDatabase"/> is disconnected.</exception>
595 /// <exception cref="ObjectDisposedException">The <see cref="MediaDatabase"/> has already been disposed of.</exception>
596 /// <exception cref="MediaDatabaseException">An error occurred while executing the command.</exception>
597 /// <exception cref="ArgumentNullException"><paramref name="mediaId"/> is null.</exception>
598 /// <exception cref="ArgumentException"><paramref name="mediaId"/> is a zero-length string, contains only white space.</exception>
599 /// <exception cref="ArgumentOutOfRangeException"><paramref name="playlistId"/> is less than or equal to zero.</exception>
600 public bool AddMember(int playlistId, string mediaId)
602 ValidationUtil.ValidateNotNullOrEmpty(mediaId, nameof(mediaId));
604 return AddMembers(playlistId, new string[] { mediaId });
608 /// Adds a media set to a playlist.
610 /// <param name="playlistId">The playlist id that the media will be added to.</param>
611 /// <param name="mediaIds">The collection of media id to add to the playlist.</param>
612 /// <returns>true if the matched record was found and updated, otherwise false.</returns>
613 /// <remarks>The invalid media ids will be ignored.</remarks>
614 /// <exception cref="InvalidOperationException">The <see cref="MediaDatabase"/> is disconnected.</exception>
615 /// <exception cref="ObjectDisposedException">The <see cref="MediaDatabase"/> has already been disposed of.</exception>
616 /// <exception cref="MediaDatabaseException">An error occurred while executing the command.</exception>
617 /// <exception cref="ArgumentNullException"><paramref name="mediaIds"/> is null.</exception>
618 /// <exception cref="ArgumentException">
619 /// <paramref name="mediaIds"/> has no element.\n
621 /// <paramref name="mediaIds"/> contains null value.\n
623 /// <paramref name="mediaIds"/> contains a zero-length string or white space.\n
625 /// <exception cref="ArgumentOutOfRangeException"><paramref name="playlistId"/> is less than or equal to zero.</exception>
626 public bool AddMembers(int playlistId, IEnumerable<string> mediaIds)
632 throw new ArgumentOutOfRangeException(nameof(playlistId), playlistId,
633 "Playlist id can't be less than or equal to zero.");
636 if (mediaIds == null)
638 throw new ArgumentNullException(nameof(mediaIds));
641 if (mediaIds.Count() == 0)
643 throw new ArgumentException("mediaIds has no element.", nameof(mediaIds));
646 if (CommandHelper.Count(
647 Interop.Playlist.GetPlaylistCount, $"{PlaylistColumns.Id}={playlistId}") == 0)
652 IntPtr handle = IntPtr.Zero;
653 Interop.Playlist.Create(out handle).ThrowIfError("Failed to add member");
657 foreach (var mediaId in mediaIds)
661 throw new ArgumentException("Media id should not be null.", nameof(mediaIds));
664 if (string.IsNullOrWhiteSpace(mediaId))
666 throw new ArgumentException("Media id should not be empty.", nameof(mediaId));
669 Interop.Playlist.AddMedia(handle, mediaId).ThrowIfError("Failed to add member");
672 Interop.Playlist.Update(playlistId, handle).ThrowIfError("Failed to add member");
677 if (handle != IntPtr.Zero)
679 Interop.Playlist.Destroy(handle);
685 /// Removes a member from a playlist.
687 /// <param name="playlistId">The playlist id.</param>
688 /// <param name="memberId">The member id to be removed.</param>
689 /// <returns>true if the matched record was found and updated, otherwise false.</returns>
690 /// <remarks>The invalid id will be ignored.</remarks>
691 /// <exception cref="InvalidOperationException">The <see cref="MediaDatabase"/> is disconnected.</exception>
692 /// <exception cref="ObjectDisposedException">The <see cref="MediaDatabase"/> has already been disposed of.</exception>
693 /// <exception cref="MediaDatabaseException">An error occurred while executing the command.</exception>
694 /// <exception cref="ArgumentOutOfRangeException">
695 /// <paramref name="playlistId"/> is less than or equal to zero.\n
697 /// <paramref name="memberId"/> is less than or equal to zero.\n
699 public bool RemoveMember(int playlistId, int memberId)
703 throw new ArgumentOutOfRangeException(nameof(memberId), memberId,
704 "Member id can't be less than or equal to zero.");
707 return RemoveMembers(playlistId, new int[] { memberId });
711 /// Removes a media set from a playlist.
713 /// <param name="playlistId">The playlist id.</param>
714 /// <param name="memberIds">The collection of member id to remove from to the playlist.</param>
715 /// <returns>true if the matched record was found and updated, otherwise false.</returns>
716 /// <remarks>The invalid ids will be ignored.</remarks>
717 /// <exception cref="InvalidOperationException">The <see cref="MediaDatabase"/> is disconnected.</exception>
718 /// <exception cref="ObjectDisposedException">The <see cref="MediaDatabase"/> has already been disposed of.</exception>
719 /// <exception cref="MediaDatabaseException">An error occurred while executing the command.</exception>
720 /// <exception cref="ArgumentNullException"><paramref name="memberIds"/> is null.</exception>
721 /// <exception cref="ArgumentException">
722 /// <paramref name="memberIds"/> has no element.\n
724 /// <paramref name="memberIds"/> contains a value which is less than or equal to zero.
726 /// <exception cref="ArgumentOutOfRangeException"><paramref name="playlistId"/> is less than or equal to zero.</exception>
727 public bool RemoveMembers(int playlistId, IEnumerable<int> memberIds)
733 throw new ArgumentOutOfRangeException(nameof(playlistId), playlistId,
734 "Playlist id can't be less than or equal to zero.");
737 if (memberIds == null)
739 throw new ArgumentNullException(nameof(memberIds));
742 if (memberIds.Count() == 0)
744 throw new ArgumentException("memberIds has no element.", nameof(memberIds));
747 if (CommandHelper.Count(
748 Interop.Playlist.GetPlaylistCount, $"{PlaylistColumns.Id}={playlistId}") == 0)
753 IntPtr handle = IntPtr.Zero;
754 Interop.Playlist.Create(out handle).ThrowIfError("Failed to add member");
758 foreach (var memberId in memberIds)
762 throw new ArgumentException(nameof(memberIds),
763 "Member id can't be less than or equal to zero.");
766 Interop.Playlist.RemoveMedia(handle, memberId).ThrowIfError("Failed to add member");
769 Interop.Playlist.Update(playlistId, handle).ThrowIfError("Failed to add member");
774 if (handle != IntPtr.Zero)
776 Interop.Playlist.Destroy(handle);
782 /// Updates a play order of a playlist.
784 /// <param name="playlistId">The playlist id.</param>
785 /// <param name="playOrder">The <see cref="PlayOrder"/> to apply.</param>
786 /// <returns>true if the matched record was found and updated, otherwise false.</returns>
787 /// <remarks>The <see cref="PlayOrder.MemberId"/> that is invalid will be ignored.</remarks>
788 /// <exception cref="InvalidOperationException">The <see cref="MediaDatabase"/> is disconnected.</exception>
789 /// <exception cref="ObjectDisposedException">The <see cref="MediaDatabase"/> has already been disposed of.</exception>
790 /// <exception cref="MediaDatabaseException">An error occurred while executing the command.</exception>
791 /// <exception cref="ArgumentNullException"><paramref name="playOrder"/> is null.</exception>
792 /// <exception cref="ArgumentOutOfRangeException"><paramref name="playlistId"/> is less than or equal to zero.</exception>
793 public bool UpdatePlayOrder(int playlistId, PlayOrder playOrder)
795 if (playOrder == null)
797 throw new ArgumentNullException(nameof(playOrder));
799 return UpdatePlayOrders(playlistId, new PlayOrder[] { playOrder });
803 /// Updates play orders of a playlist.
805 /// <param name="playlistId">The playlist id.</param>
806 /// <param name="orders">The collection of <see cref="PlayOrder"/> to apply.</param>
807 /// <returns>true if the matched record was found and updated, otherwise false.</returns>
808 /// <remarks>The <see cref="PlayOrder.MemberId"/> that is invalid will be ignored.</remarks>
809 /// <exception cref="InvalidOperationException">The <see cref="MediaDatabase"/> is disconnected.</exception>
810 /// <exception cref="ObjectDisposedException">The <see cref="MediaDatabase"/> has already been disposed of.</exception>
811 /// <exception cref="MediaDatabaseException">An error occurred while executing the command.</exception>
812 /// <exception cref="ArgumentNullException"><paramref name="orders"/> is null.</exception>
813 /// <exception cref="ArgumentException">
814 /// <paramref name="orders"/> has no element.\n
816 /// <paramref name="orders"/> contains a null value.
818 /// <exception cref="ArgumentOutOfRangeException"><paramref name="playlistId"/> is less than or equal to zero.</exception>
819 public bool UpdatePlayOrders(int playlistId, IEnumerable<PlayOrder> orders)
825 throw new ArgumentOutOfRangeException(nameof(playlistId), playlistId,
826 "Playlist id can't be less than or equal to zero.");
831 throw new ArgumentNullException(nameof(orders));
834 if (orders.Count() == 0)
836 throw new ArgumentException("memberIds has no element.", nameof(orders));
839 if (CommandHelper.Count(
840 Interop.Playlist.GetPlaylistCount, $"{PlaylistColumns.Id}={playlistId}") == 0)
845 IntPtr handle = IntPtr.Zero;
846 Interop.Playlist.Create(out handle).ThrowIfError("Failed to add member");
850 foreach (var order in orders)
854 throw new ArgumentException(nameof(orders),
855 "orders should not contain null value.");
857 Interop.Playlist.SetPlayOrder(handle, order.MemberId, order.Value).ThrowIfError("Failed to add member");
860 Interop.Playlist.Update(playlistId, handle).ThrowIfError("Failed to add member");
865 if (handle != IntPtr.Zero)
867 Interop.Playlist.Destroy(handle);