[vlc-commits] macosx: Factorize code for adding dropped file as subtitle

David Fuhrmann git at videolan.org
Sun Oct 9 19:24:00 CEST 2016


vlc | branch: master | David Fuhrmann <dfuhrmann at videolan.org> | Sun Oct  9 19:12:05 2016 +0200| [9302334702df00b385988b701d47b8e614aedd51] | committer: David Fuhrmann

macosx: Factorize code for adding dropped file as subtitle

Adds dedicated method for trying to set the given file as subtitle.
Deduplicate code from three places.

Also removes superfluous add to recent media list.

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

 modules/gui/macosx/VLCCoreInteraction.m | 21 +--------------------
 modules/gui/macosx/VLCMain.m            | 22 +++-------------------
 modules/gui/macosx/VLCPlaylist.h        | 10 ++++++++++
 modules/gui/macosx/VLCPlaylist.m        | 22 ++++++++++++++++++++++
 modules/gui/macosx/applescript.m        | 21 +++------------------
 5 files changed, 39 insertions(+), 57 deletions(-)

diff --git a/modules/gui/macosx/VLCCoreInteraction.m b/modules/gui/macosx/VLCCoreInteraction.m
index d20fe2c..7727539 100644
--- a/modules/gui/macosx/VLCCoreInteraction.m
+++ b/modules/gui/macosx/VLCCoreInteraction.m
@@ -637,26 +637,7 @@ static int BossCallback(vlc_object_t *p_this, const char *psz_var,
     if (items.count == 0)
         return NO;
 
-    // Try to add file as a subtitle
-    input_thread_t *p_input = pl_CurrentInput(getIntf());
-    if (items.count == 1 && p_input) {
-        NSString *url = [[items firstObject] valueForKey:@"ITEM_URL"];
-        char *path = vlc_uri2path([url UTF8String]);
-
-        if (path) {
-            int i_result = input_AddSubtitleOSD(p_input, path, true, true);
-            free(path);
-            if (i_result == VLC_SUCCESS) {
-                vlc_object_release(p_input);
-                return YES;
-            }
-        }
-    }
-
-    if (p_input)
-        vlc_object_release(p_input);
-
-    [[[VLCMain sharedInstance] playlist] addPlaylistItems:items];
+    [[[VLCMain sharedInstance] playlist] addPlaylistItems:items tryAsSubtitle:YES];
     return YES;
 }
 
diff --git a/modules/gui/macosx/VLCMain.m b/modules/gui/macosx/VLCMain.m
index aba9bcb..ccf73fb 100644
--- a/modules/gui/macosx/VLCMain.m
+++ b/modules/gui/macosx/VLCMain.m
@@ -441,35 +441,19 @@ static VLCMain *sharedInstance = nil;
         }
     }
 
-    char *psz_uri = vlc_path2uri([[o_names firstObject] UTF8String], NULL);
-
-    // try to add file as subtitle
-    if ([o_names count] == 1 && psz_uri) {
-        input_thread_t * p_input = pl_CurrentInput(getIntf());
-        if (p_input) {
-            int i_result = input_AddSubtitleOSD(p_input, [[o_names firstObject] UTF8String], true, true);
-            vlc_object_release(p_input);
-            if (i_result == VLC_SUCCESS) {
-                free(psz_uri);
-                return;
-            }
-        }
-    }
-    free(psz_uri);
-
     NSArray *o_sorted_names = [o_names sortedArrayUsingSelector: @selector(caseInsensitiveCompare:)];
     NSMutableArray *o_result = [NSMutableArray arrayWithCapacity: [o_sorted_names count]];
     for (NSUInteger i = 0; i < [o_sorted_names count]; i++) {
-        psz_uri = vlc_path2uri([[o_sorted_names objectAtIndex:i] UTF8String], "file");
+        char *psz_uri = vlc_path2uri([[o_sorted_names objectAtIndex:i] UTF8String], "file");
         if (!psz_uri)
             continue;
 
         NSDictionary *o_dic = [NSDictionary dictionaryWithObject:toNSStr(psz_uri) forKey:@"ITEM_URL"];
-        free(psz_uri);
         [o_result addObject: o_dic];
+        free(psz_uri);
     }
 
-    [[[VLCMain sharedInstance] playlist] addPlaylistItems:o_result];
+    [[[VLCMain sharedInstance] playlist] addPlaylistItems:o_result tryAsSubtitle:YES];
 }
 
 /* When user click in the Dock icon our double click in the finder */
diff --git a/modules/gui/macosx/VLCPlaylist.h b/modules/gui/macosx/VLCPlaylist.h
index ec8a89f..8f7b902 100644
--- a/modules/gui/macosx/VLCPlaylist.h
+++ b/modules/gui/macosx/VLCPlaylist.h
@@ -83,10 +83,20 @@
 
 /**
  * Simplified version to add new items at the end of the current playlist
+ * @param o_array array of items. Each item is a Dictionary with meta info.
  */
 - (void)addPlaylistItems:(NSArray*)o_array;
 
 /**
+ * Add new items to playlist, with the possibility to check if an item can be added
+ * to the currently playing media as subtitle.
+ *
+ * @param array array of items. Each item is a Dictionary with meta info.
+ * @param isSubtitle if YES, method tries to add the item as a subtitle
+ */
+- (void)addPlaylistItems:(NSArray*)array tryAsSubtitle:(BOOL)isSubtitle;
+
+/**
  * Adds new items to the playlist, at specified parent node and index.
  * @param o_array array of items. Each item is a Dictionary with meta info.
  * @param i_plItemId parent playlist node id, -1 for default playlist
diff --git a/modules/gui/macosx/VLCPlaylist.m b/modules/gui/macosx/VLCPlaylist.m
index c4e0972..efa0e37 100644
--- a/modules/gui/macosx/VLCPlaylist.m
+++ b/modules/gui/macosx/VLCPlaylist.m
@@ -665,6 +665,28 @@
     [self addPlaylistItems:array withParentItemId:i_plItemId atPos:-1 startPlayback:b_autoplay];
 }
 
+- (void)addPlaylistItems:(NSArray*)array tryAsSubtitle:(BOOL)isSubtitle
+{
+    input_thread_t *p_input = pl_CurrentInput(getIntf());
+    if (isSubtitle && array.count == 1 && p_input) {
+        char *path = vlc_uri2path([[[array firstObject] objectForKey:@"ITEM_URL"] UTF8String]);
+
+        if (path) {
+            int i_result = input_AddSubtitleOSD(p_input, path, true, true);
+            free(path);
+            if (i_result == VLC_SUCCESS) {
+                vlc_object_release(p_input);
+                return;
+            }
+        }
+    }
+
+    if (p_input)
+        vlc_object_release(p_input);
+
+    [self addPlaylistItems:array];
+}
+
 - (void)addPlaylistItems:(NSArray*)array withParentItemId:(int)i_plItemId atPos:(int)i_position startPlayback:(BOOL)b_start
 {
     playlist_t * p_playlist = pl_Get(getIntf());
diff --git a/modules/gui/macosx/applescript.m b/modules/gui/macosx/applescript.m
index a16a170..8a5a124 100644
--- a/modules/gui/macosx/applescript.m
+++ b/modules/gui/macosx/applescript.m
@@ -42,26 +42,11 @@
 
     if ([o_command isEqualToString:@"GetURL"] || [o_command isEqualToString:@"OpenURL"]) {
         if (o_urlString) {
-            NSURL * o_url = [NSURL fileURLWithPath: o_urlString];
-            if (o_url != nil)
-                [[NSDocumentController sharedDocumentController] noteNewRecentDocumentURL: o_url];
-
-            input_thread_t * p_input = pl_CurrentInput(getIntf());
-            BOOL b_returned = NO;
-
-            if (p_input) {
-                b_returned = input_AddSubtitle(p_input, [o_urlString UTF8String], true);
-                vlc_object_release(p_input);
-                if (!b_returned)
-                    return nil;
-            }
 
-            NSDictionary *o_dic;
-            NSArray *o_array;
-            o_dic = [NSDictionary dictionaryWithObject:o_urlString forKey:@"ITEM_URL"];
-            o_array = [NSArray arrayWithObject: o_dic];
+            NSDictionary *o_dic = [NSDictionary dictionaryWithObject:o_urlString forKey:@"ITEM_URL"];
+            NSArray* item = [NSArray arrayWithObject:o_dic];
 
-            [[[VLCMain sharedInstance] playlist] addPlaylistItems:o_array];
+            [[[VLCMain sharedInstance] playlist] addPlaylistItems:item tryAsSubtitle:YES];
         }
     }
     return nil;



More information about the vlc-commits mailing list