public
Description: Watches the ether for the handy *jour scripts.
Homepage:
Clone URL: git://github.com/lachie/starjour.git
Click here to lend your support to: starjour and make a donation at www.pledgie.com !
starjour / BonjourWatcher.rb
100644 157 lines (122 sloc) 3.956 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
require 'osx/cocoa'
 
class BonjourWatcher < OSX::NSObject
  include OSX
  
  kvc_accessor :servicekinds
  kvc_array_accessor :servicekinds
  
  kvc_accessor :status
  
  ib_outlet :detail_view
  ib_outlet :outline_view
  
  def init
if super_init
@servicekinds = NSMutableArray.alloc.init
      @selectedItems = NSMutableArray.alloc.init
return self
end
end
 
def awakeFromNib
self.status = 'setting up...'
 
setup_known_services
load_rc
 
browse
self.status = ''
 
@outline_view.expandItem_expandChildren(nil,true)
end
 
def setup_known_services
    service('pastejour', PasteJour)
    service('http' , AppJour )
    service('git' , GitJour )
    service('rubygems' , GemJour )
    
    # service('git' , GitJour )
    # service('rubygems' , GemJour )
    # service('http' , AppJour )
    
    # service('git') do |service|
    # [ "gitjour", "git clone #{service.name}", lambda {|msg|} ]
    # end
    #
    # service('pastejour') do |service|
    # (from, to) = *service.name.split('-')
    #
    # message = "Paste from #{from}"
    # message << " to #{to}" if to
    #
    # ['pastejour', message, lambda {|msg| system("/opt/local/bin/pastejour '#{msg[:service].name}' | pbcopy"); 'copied to pasteboard'}]
    # end
    #
    # service('rubygems') do |service|
    # ['gemjour', "gem server #{service.name}", lambda {|msg|}]
    # end
    #
    # service('http') do |service|
    # ['appjour', "appjour #{service.name}", lambda {|msg|}]
    # end
  end
  
  
  def load_rc
    rcfile = "#{ENV['HOME']}/.jourrc"
    if File.exist?(rcfile)
      instance_eval(File.read(rcfile))
    end
  end
  
  def service(name,klass)
    srv = ServiceKind.alloc.initWithName_andClass(name,klass)
    insertObject_inServicekindsAtIndex(srv, @servicekinds.size)
  end
 
def browse
servicekinds.each do |srv|
browser = NSNetServiceBrowser.alloc.init
browser.delegate = self
   browser.searchForServicesOfType_inDomain(srv.service_type, "")
   end
  end
  
  
  # def selectedItems; @selectedItems end
  def selectedItems=(selectedItems)
    @selectedItems = selectedItems
    
    if !selectedItems or selectedItems.length < 1
      @detail_view.subviews.each {|sv| sv.removeFromSuperview}
      return
    end
    
    path = selectedItems.first
    if path.length == 2
      srv_index = path.indexAtPosition(0)
      jour_index = path.indexAtPosition(1)
      
      srv = @servicekinds[srv_index]
      jour = srv.children[jour_index]
      
      puts "jour.text: #{jour.text}"
      update_view(jour)
    end
 
  end
  kvc_accessor :selectedItems
  
  def update_view(jour)
    return unless jour.view?
    
    @detail_view.subviews.each {|sv| sv.removeFromSuperview}
    @detail_view.addSubview jour.view
    jour.view.frame = @detail_view.bounds
  end
 
def netServiceBrowserWillSearch(ns)
puts "starting search"
end
 
def netServiceBrowser_didNotSearch(nsb,errorDict)
puts "didn't start searching due to error #{errorDict.to_ruby.inspect}"
end
 
def netServiceBrowser_didFindService_moreComing(nsb,service,more)
    ServiceKind.found(service)
@outline_view.expandItem_expandChildren(nil,true)
end
 
def netServiceBrowser_didRemoveService_moreComing(nsb,service,more)
puts "lost service #{service.name}"
ServiceKind.lost(service)
@outline_view.expandItem_expandChildren(nil,true)
end
  
  
  # olv delegate
  def outlineView_isGroupItem(outlineView,item)
    item.representedObject.is_a?(ServiceKind)
  end
  
  def outlineView_shouldSelectItem(outlineView,item)
    !item.representedObject.is_a?(ServiceKind)
  end
  
  def outlineView_shouldCollapseItem(outline_view,item)
    item.representedObject.is_a?(ServiceKind)
  end
  
  def pcOutlineView_shouldShowDisclosureTriangleForItem(outline_view,item)
    item.representedObject.is_a?(ServiceKind)
  end
    
end