1# Copyright (c) 2006-2008, The RubyCocoa Project.
2# Copyright (c) 2001-2006, FUJIMOTO Hisakuni.
3# All Rights Reserved.
4#
5# RubyCocoa is free software, covered under either the Ruby's license or the 
6# LGPL. See the COPYRIGHT file for more information.
7
8# String additions.
9class String
10  
11  def nsencoding
12    warn "#{caller[0]}: nsencoding is now deprecated and its use is discouraged, please use 'NKF.guess(rbstr)' instead."
13    OSX::NSString.guess_nsencoding(self)
14  end
15	
16  def to_nsstring
17    warn "#{caller[0]}: to_nsstring is now deprecated and its use is discouraged, please use to_ns instead."
18    OSX::NSString.stringWithRubyString(self)
19  end
20  
21  def to_nsmutablestring
22    warn "#{caller[0]}: to_nsmutablestring is now deprecated and its use is discouraged, please use to_ns instead."
23    OSX::NSMutableString.stringWithRubyString(self)
24  end
25  
26  def to_sel
27    s = self.dup
28    s.instance_variable_set(:@__is_sel__, true)
29    return s
30  end
31
32end
33
34# Property list API.
35module OSX
36  def load_plist(data)
37    nsdata = data.to_s.to_ns.dataUsingEncoding(OSX::NSUTF8StringEncoding)
38    obj, error = OSX::NSPropertyListSerialization.objc_send \
39      :propertyListFromData, nsdata,
40      :mutabilityOption, OSX::NSPropertyListImmutable,
41      :format, nil,
42      :errorDescription
43    raise error.to_s if obj.nil?
44    obj.respond_to?(:to_ruby) ? obj.to_ruby : obj
45  end
46  module_function :load_plist
47
48  def object_to_plist(object, format=nil)
49    format ||= OSX::NSPropertyListXMLFormat_v1_0
50    data, error = OSX::NSPropertyListSerialization.objc_send \
51      :dataFromPropertyList, object,
52      :format, format,
53      :errorDescription
54    raise error.to_s if data.nil?
55    case format
56      when OSX::NSPropertyListXMLFormat_v1_0, 
57           OSX::NSPropertyListOpenStepFormat
58        OSX::NSString.alloc.initWithData_encoding(data, 
59          OSX::NSUTF8StringEncoding).to_s
60      else
61        data.bytes.bytestr(data.length)
62    end
63  end
64  module_function :object_to_plist
65end
66
67[Array, Hash, String, Numeric, TrueClass, FalseClass, Time].each do |k| 
68  k.class_eval do
69    def to_plist(format=nil)
70      OSX.object_to_plist(self, format)
71    end
72    def to_ns
73      OSX.rbobj_to_nsobj(self)
74    end
75  end
76end
77
78# Pascal strings API.
79class Array
80  def pack_as_pstring
81    len = self[0]
82    self[1..-1].pack("C#{len}")
83  end
84end
85
86class String
87  def unpack_as_pstring
88    ary = [self.length]
89    ary.concat(self.unpack('C*'))
90    return ary
91  end
92end
93
94class Thread
95  class << self
96    alias :pre_rubycocoa_new :new
97    
98    # Override Thread.new to prevent threads being created if there isn't 
99    # runtime support for it
100    def new(*args,&block)
101      unless defined? @_rubycocoa_threads_allowed then
102        # If user has explicilty disabled thread support, also disable the 
103        # check (for debugging/testing only)
104        @_rubycocoa_threads_allowed = ENV['RUBYCOCOA_THREAD_HOOK_DISABLE'] || 
105          OSX::RBRuntime.isRubyThreadingSupported?
106      end
107      if !@_rubycocoa_threads_allowed then
108        warn "#{caller[0]}: Ruby threads cannot be used in RubyCocoa without patches to the Ruby interpreter"
109      end
110      pre_rubycocoa_new(*args,&block)
111    end
112  end
113end
114