[vlc-commits] macosx: Refactor and update drag handling in ConvertAndSave Window

Marvin Scholz git at videolan.org
Tue Feb 27 02:26:47 CET 2018


vlc | branch: master | Marvin Scholz <epirat07 at gmail.com> | Tue Feb 27 02:12:05 2018 +0100| [b95be9484810b309834a78a56b42c14dfb105f69] | committer: Marvin Scholz

macosx: Refactor and update drag handling in ConvertAndSave Window

Fixes a crash when dragging a playlist item onto the drag and drop area
of the Convert an Save window, as it tried to access the no longer
present playlist item pointerValue.
Instead, the playlist item id has to be used to get the playlist item.

Additionally refactor the way too complex and hard to follow
draggedItems iteration code.

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=b95be9484810b309834a78a56b42c14dfb105f69
---

 .../gui/macosx/VLCConvertAndSaveWindowController.m | 77 ++++++++++++----------
 1 file changed, 41 insertions(+), 36 deletions(-)

diff --git a/modules/gui/macosx/VLCConvertAndSaveWindowController.m b/modules/gui/macosx/VLCConvertAndSaveWindowController.m
index 6d3755408f..1ff6e1b19b 100644
--- a/modules/gui/macosx/VLCConvertAndSaveWindowController.m
+++ b/modules/gui/macosx/VLCConvertAndSaveWindowController.m
@@ -608,44 +608,49 @@
     NSString *desired_type = [paste availableTypeFromArray: types];
     NSData *carried_data = [paste dataForType: desired_type];
 
-    if (carried_data) {
-        if ([desired_type isEqualToString:NSFilenamesPboardType]) {
-            NSArray *values = [[paste propertyListForType: NSFilenamesPboardType] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
-
-            if ([values count] > 0) {
-                [self setMRL: toNSStr(vlc_path2uri([[values firstObject] UTF8String], NULL))];
-                [self updateOKButton];
-                [self updateDropView];
-                return YES;
-            }
-        } else if ([desired_type isEqualToString:@"VLCPlaylistItemPboardType"]) {
-            NSArray * array = [[[VLCMain sharedInstance] playlist] draggedItems];
-            NSUInteger count = [array count];
-            if (count > 0) {
-                playlist_t * p_playlist = pl_Get(getIntf());
-                playlist_item_t * p_item = NULL;
-
-                PL_LOCK;
-                /* let's look for the first proper input item */
-                for (NSUInteger x = 0; x < count; x++) {
-                    p_item = [[array objectAtIndex:x] pointerValue];
-                    if (p_item) {
-                        if (p_item->p_input) {
-                            if (p_item->p_input->psz_uri != nil) {
-                                [self setMRL: toNSStr(p_item->p_input->psz_uri)];
-                                [self updateDropView];
-                                [self updateOKButton];
-
-                                PL_UNLOCK;
-
-                                return YES;
-                            }
-                        }
-                    }
-                }
-                PL_UNLOCK;
+    if (carried_data == nil)
+        return NO;
+
+    if ([desired_type isEqualToString:NSFilenamesPboardType]) {
+        NSArray *values = [[paste propertyListForType: NSFilenamesPboardType] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
+
+        if ([values count] > 0) {
+            [self setMRL: toNSStr(vlc_path2uri([[values firstObject] UTF8String], NULL))];
+            [self updateOKButton];
+            [self updateDropView];
+            return YES;
+        }
+    } else if ([desired_type isEqualToString:@"VLCPlaylistItemPboardType"]) {
+        NSArray *draggedItems = [[[VLCMain sharedInstance] playlist] draggedItems];
+
+        // Return early to prevent unnecessary playlist access/locking
+        if ([draggedItems count] <= 0) {
+            return NO;
+        }
+
+        playlist_t *p_playlist = pl_Get(getIntf());
+        playlist_item_t *p_item = NULL;
+
+        PL_LOCK;
+        for (VLCPLItem *draggedItem in draggedItems) {
+            p_item = playlist_ItemGetById(p_playlist, [draggedItem plItemId]);
+
+            // Check if the item is usable
+            if (!p_item || !p_item->p_input || !p_item->p_input->psz_uri) {
+                // Item not usable, reset it.
+                p_item = NULL;
+                continue;
             }
+
+            // First usable item found
+            [self setMRL: toNSStr(p_item->p_input->psz_uri)];
+            [self updateDropView];
+            [self updateOKButton];
+            break;
         }
+        PL_UNLOCK;
+
+        return (p_item != NULL) ? YES : NO;
     }
     return NO;
 }



More information about the vlc-commits mailing list