summaryrefslogtreecommitdiff
path: root/linux/traycommand/traycommand
blob: 7bd862ae02d83d67ac6da0d20cea45ff8d6a0f0d (plain)
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
#!/usr/bin/env ruby

begin
	require 'gtk2'
rescue LoadError
	$stderr << "########################################################\n"
	$stderr << "# In order to run this programm you need ruby-gnome2!  #\n"
	$stderr << "########################################################\n"
	raise
end

begin
	require 'gconf2'
rescue LoadError
	$stderr << "########################################################\n"
	$stderr << "# In order to run this programm you need ruby-gconf2!  #\n"
	$stderr << "########################################################\n"
	raise
end

require 'yaml'
require 'pp'
require 'optparse'
require 'ostruct'

TRAYCOMMANDVERSION = [0,1]

Gtk.init
$options = OpenStruct.new
	
optpars = OptionParser.new { |opts|
	
	opts.banner = "Usage: #{File.basename($0)} [options]"
	
	opts.separator "Options:"
	
	opts.on("--configfile=file", "Path to the configfile.") {|string|
		$options.configfile = string
	}	

	opts.on_tail("--version", "Show version and exit") {
		puts "traycommand #{TRAYCOMMANDVERSION.join('.')}"
		puts "written by Benjamin Kellermann <Benjamin.Kellermann@gmx.de>"
		exit
	}
}

begin
	optpars.parse!
	$options.configfile ||= "layoutswitch.yaml"
rescue => e
	puts e
	puts optpars
	exit
end

############################################################################
# init the things from the configfile
############################################################################
unless File.exist?($options.configfile) and $config = YAML::load_file($options.configfile)
	$config = []
	2.times{ 
		$config << {"command"  => "/path/to/command.sh",
								"icon"     => "/path/to/icon.{png|ico|jpg|...}",
								"menutext" => "Some Useful text"}
	}
	File.open($options.configfile, 'w') do |out|
		out << $config.to_yaml
	end
	puts "Created configfile template, exiting now"
	exit
end

############################################################################
# init the tray and GTK stuff
############################################################################
currentconfig = 0
$sicon = Gtk::StatusIcon.new
$sicon.pixbuf = Gdk::Pixbuf.new(File.expand_path($config[currentconfig]["icon"]))

# Add a menu
menu = Gtk::Menu.new

$config.each_with_index{|citem,i|
	item = Gtk::MenuItem.new(citem["menutext"])
	item.signal_connect("activate"){
		`#{citem["command"]}`
		$sicon.pixbuf = Gdk::Pixbuf.new(File.expand_path(citem["icon"]))
		currentconfig = i
	}
	menu.append(item.show)
}
$sicon.signal_connect("activate"){
	nextconf = (currentconfig+1) % $config.size
	`#{$config[nextconf]["command"]}`
	$sicon.pixbuf = Gdk::Pixbuf.new(File.expand_path($config[nextconf]["icon"]))
	currentconfig = nextconf
}




############################################################################
# set the about dialog
############################################################################
aboutitem = Gtk::ImageMenuItem.new(Gtk::Stock::ABOUT)
aboutitem.signal_connect("activate") {
			Gtk::AboutDialog.set_email_hook {|about, link|
		`xdg-open mailto:#{link}`
	}
	Gtk::AboutDialog.set_url_hook {|about, link|
		`xdg-open #{link}`
	}

	a = Gtk::AboutDialog.new
	a.authors   = ["Benjamin Kellermann <Benjamin.Kellermann@gmx.de>"]
	a.comments  = "This is an applet to execute commands fastly."
	a.copyright = "Copyright (C) 2009 Benjamin Kellermann"
	a.license   = "This program is licenced under CC-by-sa"
	a.logo_icon_name = "gtk-about"
	a.name      = "keyspeedapplet"
	a.version   = TRAYCOMMANDVERSION.join(".")
	a.website   = "http://www.eigenheimstrasse.de/~ben/traycommand/"
	a.website_label = "Download Website"
	a.signal_connect('response') { a.destroy }
	a.show
}
menu.append(aboutitem.show)

# add an exit button
quit = Gtk::ImageMenuItem.new(Gtk::Stock::QUIT)
quit.signal_connect("activate") { Gtk.main_quit }
menu.append(Gtk::SeparatorMenuItem.new.show)
menu.append(quit.show)

$sicon.signal_connect('popup-menu') do |w,e,time|
	menu.popup(nil, nil, e, time) 
end

pid = fork {
	Signal.trap("USR1") {
		$sicon.activate
	}
	Gtk.main
}
File.open("/tmp/traycommand-#{$options.configfile}.pid","w"){|f|
	f << pid
}

Process.waitpid(pid)