Improve metadata_editor_remove_picture() API 86/232886/9
authorhj kim <backto.kim@samsung.com>
Fri, 8 May 2020 06:28:47 +0000 (15:28 +0900)
committerhj kim <backto.kim@samsung.com>
Tue, 12 May 2020 09:44:31 +0000 (18:44 +0900)
Change-Id: Ib957753653b501d8c6b543a7e789b523db199e7a

src/metadata_editor.cpp

index 83c250e..28bbe59 100755 (executable)
@@ -1631,6 +1631,92 @@ extern "C" int metadata_editor_append_picture(metadata_editor_h metadata, const
        return ret;
 }
 
+static int __remove_APIC(TagLib::ID3v2::Tag* tag, int index)
+{
+       metadata_editor_retvm_if(tag == NULL, METADATA_EDITOR_ERROR_OPERATION_FAILED, "Error. ID3v2 tag was not created. Can not proceed metadata updating");
+
+       TagLib::ID3v2::FrameList lst = tag->frameListMap()["APIC"];
+       metadata_editor_retvm_if(lst.isEmpty(), METADATA_EDITOR_ERROR_OPERATION_FAILED, "No pictures in file");
+
+       if ((index < 0) || ((uint)index >= lst.size())) {
+               metadata_editor_error("Index of picture is out of range");
+               return METADATA_EDITOR_ERROR_INVALID_PARAMETER;
+       }
+
+       tag->removeFrame(lst[index]);
+
+       return METADATA_EDITOR_ERROR_NONE;
+}
+static int __remove_mp3_picture(metadata_editor_s* metadata, int index)
+{
+       return __remove_APIC(dynamic_cast<TagLib::MPEG::File*>(metadata->file)->ID3v2Tag(true), index);
+}
+
+static int __remove_mp4_picture(metadata_editor_s* metadata, int index)
+{
+       auto tag = dynamic_cast<TagLib::MP4::Tag*>(metadata->file->tag());
+       metadata_editor_retvm_if(tag == NULL, METADATA_EDITOR_ERROR_OPERATION_FAILED, "Error. tag not exist.");
+
+       if (!(tag->contains("covr"))) {
+               metadata_editor_error("No item <covr> in file. No pictures in file");
+               return METADATA_EDITOR_ERROR_OPERATION_FAILED;
+       }
+
+       TagLib::MP4::CoverArtList lst = tag->item("covr").toCoverArtList();
+
+       // Check if the index is in range of CoverArtList Item
+       if ((index < 0) || ((uint)index >= lst.size())) {
+               metadata_editor_error("Index of picture is out of range");
+               return METADATA_EDITOR_ERROR_INVALID_PARAMETER;
+       }
+
+       metadata_editor_info("The picture number %d will be deleted", index);
+       int i = 0;
+       for (TagLib::MP4::CoverArtList::Iterator picIt = lst.begin(); picIt != lst.end(); ++picIt, ++i) {
+               if (i != index) continue;
+               lst.erase(picIt);
+                       break;
+       }
+
+       tag->setItem("covr", lst);      //?? FIX ME!
+
+       return METADATA_EDITOR_ERROR_NONE;
+}
+
+#if 0
+static int __remove_flac_picture(metadata_editor_s* metadata, int index)
+{
+       TagLib::FLAC::File* _file = (TagLib::FLAC::File*) metadata->file;
+       TagLib::List<TagLib::FLAC::Picture*> lst = _file->pictureList();
+
+       if (lst.isEmpty()) {
+               metadata_editor_error("No pictures in file. Nothing to delete");
+               return METADATA_EDITOR_ERROR_OPERATION_FAILED;
+       }
+
+       // Check if the index is in range of CoverArtList Item
+       if ((index < 0) || ((uint)index >= lst.size())) {
+               metadata_editor_error("Index of picture is out of range");
+               return METADATA_EDITOR_ERROR_INVALID_PARAMETER;
+       }
+
+       metadata_editor_info("The picture number %d will be deleted", index);
+       int i = 0;
+       for (TagLib::List<TagLib::FLAC::Picture*>::Iterator picIt = lst.begin(); picIt != lst.end(); ++picIt, ++i) {
+               if (i != index) continue;
+               _file->removePicture(*picIt, true);
+               break;
+       }
+
+       return METADATA_EDITOR_ERROR_NONE;
+}
+
+static int __remove_wav_picture(metadata_editor_s* metadata, int index)
+{
+       return __remove_APIC(dynamic_cast<TagLib::ID3v2::Tag*>(metadata->file->tag()), index);
+}
+#endif
+
 // *** This function is used to delete picture with specified index *** //
 extern "C" int metadata_editor_remove_picture(metadata_editor_h metadata, int index) {
        int ret = METADATA_EDITOR_ERROR_NONE;
@@ -1639,114 +1725,18 @@ extern "C" int metadata_editor_remove_picture(metadata_editor_h metadata, int in
        ret = __check_metadata_set_parameter(_metadata);
        metadata_editor_retvm_if(ret != METADATA_EDITOR_ERROR_NONE, ret, "fail to __check_metadata_set_parameter() [%d]", ret);
 
-       switch (_metadata->filetype) {                                  // Process the file according to the specified file type
-               case METADATA_EDITOR_FORMAT_MP3: {
-                       // Bring the pointer to actual file type and make tags pointers
-                       TagLib::MPEG::File* _file = (TagLib::MPEG::File*)_metadata->file;
-                       TagLib::ID3v2::Tag* tag2 = _file->ID3v2Tag(true);
-                       // Check if the valid tag pointer exists
-                       metadata_editor_retvm_if(tag2 == NULL, METADATA_EDITOR_ERROR_OPERATION_FAILED, "Error. ID3v2 tag was not created. Can not proceed metadata updating");
-                       TagLib::ID3v2::FrameList lst = tag2->frameListMap()["APIC"];
-                       // Check if there are pictures in the tag
-                       metadata_editor_retvm_if(lst.isEmpty(), METADATA_EDITOR_ERROR_OPERATION_FAILED, "No pictures in file");
-
-                       // Check if index is correct or not
-                       metadata_editor_retvm_if((index < 0) || (lst.size() <= (uint)index), METADATA_EDITOR_ERROR_INVALID_PARAMETER, "Index of picture is out of range");
-                       metadata_editor_info("Removing of picture number %d", index);
-                       int i = 0;
-                       // Among all frames we must choose that one with specified index. "i" will be counter
-                       for (TagLib::ID3v2::FrameList::Iterator it = lst.begin(); it != lst.end(); ++it, ++i) {
-                               if (i != index) continue;
-                               tag2->removeFrame(*it);
-                               break;
-                       }
-                       return METADATA_EDITOR_ERROR_NONE;
-               }
-               case METADATA_EDITOR_FORMAT_MP4: {
-                       TagLib::MP4::File* _file = (TagLib::MP4::File*) _metadata->file;
-                       TagLib::MP4::Tag* tag = _file->tag();
-                       metadata_editor_retvm_if(tag == NULL, METADATA_EDITOR_ERROR_OPERATION_FAILED, "Error. tag not exist.");
-
-                       // Get map of items directly from tag and launch a search of specific item
-                       TagLib::MP4::ItemListMap& itemMap = tag->itemListMap();
-                       TagLib::MP4::ItemListMap::ConstIterator it = itemMap.find("covr");
-                       if (it != itemMap.end()) {                                                                      // Item was found
-                               TagLib::MP4::CoverArtList lst = it->second.toCoverArtList();
-                               // Check if the index is in range of CoverArtList Item
-                               if ((index < 0) || ((uint)index >= lst.size())) {                                       // it is not
-                                       metadata_editor_error("Index of picture is out of range");
-                                       return METADATA_EDITOR_ERROR_INVALID_PARAMETER;
-                               } else {                                                                                // index is in range
-                                       metadata_editor_info("The picture number %d will be deleted", index);
-                                       int i = 0;
-                                       for (TagLib::MP4::CoverArtList::Iterator picIt = lst.begin(); picIt != lst.end(); ++picIt, ++i) {
-                                               if (i != index) continue;
-                                               lst.erase(picIt);
-                                               break;
-                                       }
-                                       itemMap.insert("covr", TagLib::MP4::Item(lst));
-                                       return METADATA_EDITOR_ERROR_NONE;
-                               }
-                       } else {                                // Item was not found
-                               metadata_editor_error("The item <covr> does not exist. Nothing to delete");
-                               return METADATA_EDITOR_ERROR_OPERATION_FAILED;
-                       }
-               }
+       switch (_metadata->filetype) {
+               case METADATA_EDITOR_FORMAT_MP3:
+                       return __remove_mp3_picture(_metadata, index);
+
+               case METADATA_EDITOR_FORMAT_MP4:
+                       return __remove_mp4_picture(_metadata, index);
 #if 0
-               case METADATA_EDITOR_FORMAT_FLAC: {
-                       TagLib::FLAC::File* _file = (TagLib::FLAC::File*) _metadata->file;
-                       TagLib::List<TagLib::FLAC::Picture*> lst = _file->pictureList();
-                       if (lst.isEmpty()) {
-                               metadata_editor_error("No pictures in file. Nothing to delete");
-                               return METADATA_EDITOR_ERROR_OPERATION_FAILED;
-                       }
-                       // Check if the index is in range of CoverArtList Item
-                       if ((index < 0) || ((uint)index >= lst.size())) {                                               // it is not
-                               metadata_editor_error("Index of picture is out of range");
-                               return METADATA_EDITOR_ERROR_INVALID_PARAMETER;
-                       } else {                                                                                        // index is in range
-                               metadata_editor_info("The picture number %d will be deleted", index);
-                               int i = 0;
-                               for (TagLib::List<TagLib::FLAC::Picture*>::Iterator picIt = lst.begin(); picIt != lst.end(); ++picIt, ++i) {
-                                       if (i != index) continue;
-                                       _file->removePicture(*picIt, true);
-                                       break;
-                               }
-                               return METADATA_EDITOR_ERROR_NONE;
-                       }
-               }
-               case METADATA_EDITOR_FORMAT_WAV: {
-                       // Bring the pointer to actual file type and make tags pointers
-                       TagLib::RIFF::WAV::File* _file = (TagLib::RIFF::WAV::File*)_metadata->file;
-                       TagLib::ID3v2::Tag* tag2 = _file->tag();
-                       // Check if the valid tag pointer exists
-                       if (tag2 == NULL) {
-                               metadata_editor_error("Error. ID3v2 tag does not exist. Can not remove picture");
-                               return METADATA_EDITOR_ERROR_OPERATION_FAILED;
-                       }
-                       TagLib::ID3v2::FrameList lst = tag2->frameListMap()["APIC"];
-                       // Check if there are pictures in the tag
-                       if (lst.isEmpty()) {
-                               metadata_editor_error("No pictures in file");
-                               return METADATA_EDITOR_ERROR_OPERATION_FAILED;
-                       } else {                // pictures exist in file
-                               // Check if index is correct or not
-                               if ((index < 0) || (lst.size() <= (uint)index)) {
-                                       metadata_editor_error("Index of picture is out of range");
-                                       return METADATA_EDITOR_ERROR_INVALID_PARAMETER;
-                               } else {                // everything is correct - begin extraction
-                                       metadata_editor_info("Removing of picture number %d", index);
-                                       int i = 0;
-                                       // Among all frames we must choose that one with specified index. "i" will be counter
-                                       for (TagLib::ID3v2::FrameList::Iterator it = lst.begin(); it != lst.end(); ++it, ++i) {
-                                               if (i != index) continue;
-                                               tag2->removeFrame(*it);
-                                               break;
-                                       }
-                                       return METADATA_EDITOR_ERROR_NONE;
-                               }
-                       }
-               }
+               case METADATA_EDITOR_FORMAT_FLAC:
+                       return __remove_flac_picture(_metadata, index);
+
+               case METADATA_EDITOR_FORMAT_WAV:
+                       return __remove_wav_picture(_metadata, index);
 #endif
                default:
                        metadata_editor_error("Wrong file type");