public
Description: A RubyCocoa Gmail Notifier for Mac OS X
Homepage: http://ashchan.com/projects/gmail-notifr
Clone URL: git://github.com/ashchan/gmail-notifr.git
Click here to lend your support to: gmail-notifr and make a donation at www.pledgie.com !
gmail-notifr / GNAccount.rb
100644 113 lines (89 sloc) 2.375 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
#
# GNAccount.rb
# Gmail Notifr
#
# Created by James Chan on 1/3/09.
# Copyright (c) 2009 ashchan.com. All rights reserved.
#
 
require 'osx/cocoa'
 
# a normal gmail account, or a google hosted email account
class GNAccount < OSX::NSObject
 
  attr_accessor :guid, :username, :password, :interval, :enabled, :sound, :growl
  Properties = [:guid, :username, :interval, :enabled, :sound, :growl]
    
  MIN_INTERVAL = 1
  MAX_INTERVAL = 900
  DEFAULT_INTERVAL = 30
 
  def init
    self.password = GNKeychain.sharedInstance.get_password(@username)
    super_init
  end
 
  def initWithNameIntervalEnabledGrowlSound(username, interval, enabled, growl, sound)
    self.username = username
    self.interval = interval || DEFAULT_INTERVAL
    self.enabled = enabled
    self.growl = growl
    self.sound = sound || GNSound::SOUND_NONE
    
    init
  end
  
  def initWithCoder(coder)
    Properties.each do |prop|
      val = coder.decodeObjectForKey(prop)
      self.send("#{prop.to_s}=", val)
    end
    
    init
  end
  
  def encodeWithCoder(coder)
    Properties.each do |prop|
      val = self.send(prop)
      coder.encodeObject_forKey(val, prop)
    end
  end
  
  def description
    "<#{self.class}: #{username}(#{guid}), enabled? : #{enabled?}\ninterval: #{interval}, sound: #{sound}, growl: #{growl}>"
  end
  
  alias inspect to_s
  alias enabled? enabled
  
  def enabled=(val)
    @enabled = val
    @enabled = false if val == 0
  end
  
  def interval=(val)
    @interval = val.to_i
    @interval = DEFAULT_INTERVAL unless @interval.between?(MIN_INTERVAL, MAX_INTERVAL)
  end
  
  def growl=(val)
    @growl = val
    @growl = false if val == 0
  end
  
  def username=(new_username)
    @old_username ||= @username
    @username = new_username
  end
  
  def password=(new_password)
    @old_password ||= @password
    @password = new_password
  end
  
  def username_changed?
    @old_username && @old_username != @username
  end
  
  def password_changed?
    @old_password && @old_password != @password
  end
  
  def changed?
    username_changed? || password_changed? #todo
  end
  
  def new?
    @guid.nil?
  end
  
  def gen_guid
    self.guid = `uuidgen`.strip
  end
  
  def save
    if new?
      gen_guid
      GNPreferences.sharedInstance.addAccount(self)
    else
      GNPreferences.sharedInstance.saveAccount(self)
    end
  end
end