public
Description: A bandwidth-quota monitor for TPG users that sits in your OS X menubar.
Homepage: http://www.bjeanes.com/
Clone URL: git://github.com/bjeanes/tipi.git
tipi / AppController.rb
100644 82 lines (64 sloc) 2.076 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
#
# AppController.rb
# Tipi
#
# Created by Bodaniel Jeanes on 15/04/08.
# Copyright (c) 2008 bjeanes.com. All rights reserved.
#
 
require 'osx/cocoa'
 
class AppController < OSX::NSObject
  include OSX
  
  ib_outlets :statusMenu, :window, :username, :password, :interval,
    :onpeak, :offpeak
  
  ib_action :check_usage do |sender|
    @timer.fire
  end
  
  ib_action :save_prefs do |sender|
    @timer.fire
    @window.close
  end
  
  def awakeFromNib
    NSLog("Getting Status Bar")
    @bar = NSStatusBar.systemStatusBar
    @status_item = @bar.statusItemWithLength_(NSVariableStatusItemLength)
    NSLog("Setting status item icon")
    
    # statusImage = OSX::NSImage.alloc.initWithContentsOfFile_(OSX::NSBundle.mainBundle.pathForResource_ofType_("icon","gif"))
    
    @status_item.setTitle("Checking...")
    @status_item.setMenu(@statusMenu)
    @status_item.setToolTip("Click for more information")
    @status_item.setHighlightMode(true)
 
    interval = user_interval_value || (60.0 * 5.0)
    @timer = NSTimer.scheduledTimerWithTimeInterval_target_selector_userInfo_repeats_(interval, self, :tick!, nil, true)
    @timer.fire # get an initial pull of data
  end
  
  def tick!
    NSLog("Checking Usage...")
    # NSLog(@onpeak.stringValue)
    
    @tipi ||= Tipi.new
    @tipi.set_auth(@username.stringValue.to_s, @password.stringValue.to_s)
    tipi = @tipi.check_usage
 
    if tipi.nil?
      @status_item.setTitle("Invalid Credentials")
    else
      NSLog(tipi.inspect)
      
      on = (tipi[:on].to_f / 1000).round.to_s
      off = (tipi[:off].to_f / 1000).round.to_s
      
      @status_item.setTitle((tipi[:current] == :on ? on : off) + " GB")
      
      # tpg uses metric calculations (1000 instead of 1024)
      @onpeak.title = "On-peak: #{on} GB (#{tipi[:pon]})"
      @offpeak.title = "Off-peak: #{off} GB (#{tipi[:poff]})"
    end
  end
  
  def dealloc
    @status_item.release
    @status_item = nil
  end
  
  private
  def user_interval_value
    if val = @interval.floatValue
      val * 60.0
    else
      nil
    end
  end
end