mattmatt / macistrano

A RubyCocoa client for Webistrano (http://labs.peritor.com/webistrano)

This URL has Read+Write access

mattmatt (author)
Mon Mar 02 01:34:32 -0800 2009
commit  e6f8aec619365bc623200aa858becdd0803b9ab2
tree    ce6027fc1062f6a4da0359529db13bc20da2c335
parent  570f08e4912443d002cf238eb958ef1a4ba95c2c
macistrano / controller / preferences_controller.rb
100644 174 lines (148 sloc) 4.655 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
require 'osx/cocoa'
 
class PreferencesController < OSX::NSWindowController
  attr_accessor :hosts
  include OSX
  include NotificationHub
 
  notify :host_version_accepted, :when => :host_version_acceptable
  notify :host_version_not_accepted, :when => :host_version_inacceptable
  notify :host_credentials_invalid, :when => :host_credentials_invalid
  notify :host_check_failed, :when => :host_check_failed
  
  ib_outlet :preferences_window
  ib_outlet :table_view
  ib_outlet :new_host_sheet
  ib_outlet :spinner
  ib_outlet :host_field
  ib_outlet :username_field
  ib_outlet :password_field
  
  def init
    init_hosts
    self
  end
 
  def register_defaults
    appDefaults = NSMutableDictionary.dictionary
    appDefaults.setObject_forKey([], "hosts")
    NSUserDefaults.standardUserDefaults.registerDefaults appDefaults
  end
  
  def init_hosts
    register_defaults
    configured_hosts = NSUserDefaults.standardUserDefaults.arrayForKey("hosts")
    @hosts ||= []
    configured_hosts.each do |data|
      host = Host.alloc.init
      host.url = data[0]
      host.username = data[1]
      Keychain.find_password host
      @hosts << host
    end
    fetchPasswords
  end
  
  def fetchPasswords
    @hosts.each do |host|
      Keychain.find_password host
    end
  end
  
  def showPreferences
    NSApp.activateIgnoringOtherApps true
    self.showWindow(self)
    @preferences_window.makeKeyAndOrderFront(self)
  end
  
  def numberOfRowsInTableView(table_view)
    @hosts.size
  end
  
  def tableView_objectValueForTableColumn_row(table_view, column, row)
    @hosts[row].url
  end
  
  ib_action :add do
    NSApp.beginSheet_modalForWindow_modalDelegate_didEndSelector_contextInfo(@new_host_sheet, @preferences_window, nil, nil, nil)
    NSApp.endSheet @new_host_sheet
  end
  
  ib_action :addFromSheet do
    @spinner.startAnimation(self)
    @spinner.setHidden(false)
    host = Host.new
    host.url = @host_field.stringValue
    host.username = @username_field.stringValue
    host.password = @password_field.stringValue.to_s
    host.schedule_version_check
  end
 
  def host_version_accepted(notification)
    # ignore messages to instances not connected to the nib. weird side effect of having an instance
    # of the controller in the project_controller
    return if @host_field.nil?
    host = notification.object
    add_host host
    @new_host_sheet.orderOut self
    @table_view.reloadData
    host.find_projects
  end
  
  def host_check_failed(notification)
    return if @host_field.nil?
    host = notification.object
    show_alert("There was an error trying to fetch the data for the host, are you sure the URL is correct?", "Please have a good look at the data and try again.")
    reset_spinner
  end
  
  def host_version_not_accepted(notification)
    return if @host_field.nil?
    host = notification.object
    show_alert("The Webistrano version you're running is not suitable for use with Macistrano", "You need at least version #{Host::ACCEPT_VERSION.join(".")}.")
    reset_spinner
  end
 
  def host_credentials_invalid(notification)
    return if @host_field.nil?
    show_alert("The specified credentials are invalid.", "Please check username and password and try again.")
    reset_spinner
  end
  
  def show_alert(message, extended)
    alert = NSAlert.alloc.init
    alert.addButtonWithTitle "OK"
    alert.setMessageText message
    alert.setInformativeText extended
    alert.setAlertStyle NSWarningAlertStyle
    alert.runModal
    alert.release
  end
  
  def reset_spinner
    @spinner.stopAnimation(self)
    @spinner.setHidden(true)
  end
  
  ib_action :cancelSheet do
    closeSheet
    reset_fields
    reset_spinner
  end
 
  ib_action :removeHost do
    unless @table_view.selectedRow < 0
      host = @hosts[@table_view.selectedRow]
      Keychain.remove_password host
      @hosts.delete_at(@table_view.selectedRow)
      save_hosts_to_preferences
      notify_host_removed host
      @table_view.reloadData
    end
  end
  
  def closeSheet
    @new_host_sheet.orderOut self
  end
  
  def add_host host
    @hosts << host
    Keychain.add_password host
    save_hosts_to_preferences
    reset_fields
    reset_spinner
  end
  
  def save_hosts_to_preferences
    NSUserDefaults.standardUserDefaults.setObject_forKey(hosts_as_list, "hosts")
    NSUserDefaults.standardUserDefaults.synchronize
  end
  
  def hosts_as_list
    @hosts.collect do |host|
      [host.url, host.username]
    end
  end
  
  def reset_fields
    @host_field.setStringValue ""
    @new_host_sheet.makeFirstResponder @host_field
    @password_field.setStringValue ""
    @username_field.setStringValue ""
  end
end