]> git.sesse.net Git - vlc/blobdiff - modules/gui/macosx/StringUtility.m
macosx: robustify media key trap handling
[vlc] / modules / gui / macosx / StringUtility.m
index dc9f2cdcd3bc411e5093cee7ecabb6a0e1698598..37301e9b81da614ad9960a1c37f81c20b58202d1 100644 (file)
@@ -1,7 +1,7 @@
 /*****************************************************************************
  * StringUtility.m: MacOS X interface module
  *****************************************************************************
- * Copyright (C) 2002-2012 VLC authors and VideoLAN
+ * Copyright (C) 2002-2014 VLC authors and VideoLAN
  * $Id$
  *
  * Authors: Jon Lech Johansen <jon-vl@nanocrew.net>
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
-#import <vlc_input.h>
+#import "intf.h"
+#import "StringUtility.h"
+#import "CompatibilityFixes.h"
+
 #import <vlc_keys.h>
 #import <vlc_strings.h>
 
-#import "StringUtility.h"
-#import "intf.h"
-
 @implementation VLCStringUtility
 
 static VLCStringUtility *_o_sharedInstance = nil;
@@ -55,25 +55,23 @@ static VLCStringUtility *_o_sharedInstance = nil;
 
 - (NSString *)localizedString:(const char *)psz
 {
-    NSString * o_str = nil;
+    NSString * stringObject = nil;
 
     if (psz != NULL) {
-        o_str = [NSString stringWithCString: _(psz) encoding:NSUTF8StringEncoding];
+        stringObject = [NSString stringWithCString: _(psz) encoding:NSUTF8StringEncoding];
 
-        if (o_str == NULL) {
+        if (stringObject == NULL) {
             msg_Err(VLCIntf, "could not translate: %s", psz);
-            return(@"");
+            return @"";
         }
-    } else {
-        msg_Warn(VLCIntf, "can't translate empty strings");
-        return(@"");
-    }
+    } else
+        return @"";
 
-    return(o_str);
+    return stringObject;
 }
 
 /* i_width is in pixels */
-- (NSString *)wrapString: (NSString *)o_in_string toWidth: (int) i_width
+- (NSString *)wrapString:(NSString *)o_in_string toWidth:(int)i_width
 {
     NSMutableString *o_wrapped;
     NSString *o_out_string;
@@ -118,12 +116,12 @@ static VLCStringUtility *_o_sharedInstance = nil;
 - (NSString *)getCurrentTimeAsString:(input_thread_t *)p_input negative:(BOOL)b_negative
 {
     assert(p_input != nil);
-    
+
     vlc_value_t time;
     char psz_time[MSTRTIME_MAX_SIZE];
-    
+
     var_Get(p_input, "time", &time);
-    
+
     mtime_t dur = input_item_GetDuration(input_GetItem(p_input));
     if (b_negative && dur > 0) {
         mtime_t remaining = 0;
@@ -134,6 +132,27 @@ static VLCStringUtility *_o_sharedInstance = nil;
         return [NSString stringWithUTF8String:secstotimestr(psz_time, (time.i_time / 1000000))];
 }
 
+- (NSString *)stringForTime:(long long int)time
+{
+    if (time > 0) {
+        long long positiveDuration = llabs(time);
+        if (positiveDuration > 3600)
+            return [NSString stringWithFormat:@"%s%01ld:%02ld:%02ld",
+                    time < 0 ? "-" : "",
+                    (long) (positiveDuration / 3600),
+                    (long)((positiveDuration / 60) % 60),
+                    (long) (positiveDuration % 60)];
+        else
+            return [NSString stringWithFormat:@"%s%02ld:%02ld",
+                    time < 0 ? "-" : "",
+                    (long)((positiveDuration / 60) % 60),
+                    (long) (positiveDuration % 60)];
+    } else {
+        // Return a string that represents an undefined time.
+        return @"--:--";
+    }
+}
+
 #pragma mark -
 #pragma mark Key Shortcuts
 
@@ -188,6 +207,12 @@ unsigned int CocoaKeyToVLC(unichar i_key)
     return (unsigned int)i_key;
 }
 
+/* takes a good old const c string and converts it to NSString without UTF8 loss */
+
+NSString *toNSStr(const char *str) {
+    return str != NULL ? [NSString stringWithUTF8String:str] : @"";
+}
+
 /*
  * Converts VLC key string to a prettified version, for hotkey settings.
  * The returned string adapts similar how its done within the cocoa framework when setting this
@@ -368,5 +393,16 @@ unsigned int CocoaKeyToVLC(unichar i_key)
     return returnStr;
 }
 
-
 @end
+
+NSImage *imageFromRes(NSString *o_id)
+{
+    NSString *result = @"";
+    if (OSX_YOSEMITE) {
+        result = [result stringByAppendingString:@"ys-"];
+    }
+
+    result = [result stringByAppendingString:o_id];
+
+    return [NSImage imageNamed:result];
+}