| 1 | #!/usr/bin/env ruby |
|---|
| 2 | |
|---|
| 3 | begin |
|---|
| 4 | require 'gtk2' |
|---|
| 5 | rescue LoadError |
|---|
| 6 | $stderr << "########################################################\n" |
|---|
| 7 | $stderr << "# In order to run this programm you need ruby-gnome2! #\n" |
|---|
| 8 | $stderr << "########################################################\n" |
|---|
| 9 | raise |
|---|
| 10 | end |
|---|
| 11 | |
|---|
| 12 | begin |
|---|
| 13 | require 'gconf2' |
|---|
| 14 | rescue LoadError |
|---|
| 15 | $stderr << "########################################################\n" |
|---|
| 16 | $stderr << "# In order to run this programm you need ruby-gconf2! #\n" |
|---|
| 17 | $stderr << "########################################################\n" |
|---|
| 18 | raise |
|---|
| 19 | end |
|---|
| 20 | |
|---|
| 21 | require 'yaml' |
|---|
| 22 | require 'pp' |
|---|
| 23 | require 'optparse' |
|---|
| 24 | require 'ostruct' |
|---|
| 25 | |
|---|
| 26 | TRAYCOMMANDVERSION = [0,1] |
|---|
| 27 | |
|---|
| 28 | Gtk.init |
|---|
| 29 | $options = OpenStruct.new |
|---|
| 30 | |
|---|
| 31 | optpars = OptionParser.new { |opts| |
|---|
| 32 | |
|---|
| 33 | opts.banner = "Usage: #{File.basename($0)} [options]" |
|---|
| 34 | |
|---|
| 35 | opts.separator "Options:" |
|---|
| 36 | |
|---|
| 37 | opts.on("--configfile=file", "Path to the configfile.") {|string| |
|---|
| 38 | $options.configfile = string |
|---|
| 39 | } |
|---|
| 40 | |
|---|
| 41 | opts.on_tail("--version", "Show version and exit") { |
|---|
| 42 | puts "traycommand #{TRAYCOMMANDVERSION.join('.')}" |
|---|
| 43 | puts "written by Benjamin Kellermann <Benjamin.Kellermann@gmx.de>" |
|---|
| 44 | exit |
|---|
| 45 | } |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | begin |
|---|
| 49 | optpars.parse! |
|---|
| 50 | $options.configfile ||= "layoutswitch.yaml" |
|---|
| 51 | rescue => e |
|---|
| 52 | puts e |
|---|
| 53 | puts optpars |
|---|
| 54 | exit |
|---|
| 55 | end |
|---|
| 56 | |
|---|
| 57 | ############################################################################ |
|---|
| 58 | # init the things from the configfile |
|---|
| 59 | ############################################################################ |
|---|
| 60 | unless File.exist?($options.configfile) and $config = YAML::load_file($options.configfile) |
|---|
| 61 | $config = [] |
|---|
| 62 | 2.times{ |
|---|
| 63 | $config << {"command" => "/path/to/command.sh", |
|---|
| 64 | "icon" => "/path/to/icon.{png|ico|jpg|...}", |
|---|
| 65 | "menutext" => "Some Useful text"} |
|---|
| 66 | } |
|---|
| 67 | File.open($options.configfile, 'w') do |out| |
|---|
| 68 | out << $config.to_yaml |
|---|
| 69 | end |
|---|
| 70 | puts "Created configfile template, exiting now" |
|---|
| 71 | exit |
|---|
| 72 | end |
|---|
| 73 | |
|---|
| 74 | ############################################################################ |
|---|
| 75 | # init the tray and GTK stuff |
|---|
| 76 | ############################################################################ |
|---|
| 77 | currentconfig = 0 |
|---|
| 78 | $sicon = Gtk::StatusIcon.new |
|---|
| 79 | $sicon.pixbuf = Gdk::Pixbuf.new(File.expand_path($config[currentconfig]["icon"])) |
|---|
| 80 | |
|---|
| 81 | # Add a menu |
|---|
| 82 | menu = Gtk::Menu.new |
|---|
| 83 | |
|---|
| 84 | $config.each_with_index{|citem,i| |
|---|
| 85 | item = Gtk::MenuItem.new(citem["menutext"]) |
|---|
| 86 | item.signal_connect("activate"){ |
|---|
| 87 | `#{citem["command"]}` |
|---|
| 88 | $sicon.pixbuf = Gdk::Pixbuf.new(File.expand_path(citem["icon"])) |
|---|
| 89 | currentconfig = i |
|---|
| 90 | } |
|---|
| 91 | menu.append(item.show) |
|---|
| 92 | } |
|---|
| 93 | $sicon.signal_connect("activate"){ |
|---|
| 94 | nextconf = (currentconfig+1) % $config.size |
|---|
| 95 | `#{$config[nextconf]["command"]}` |
|---|
| 96 | $sicon.pixbuf = Gdk::Pixbuf.new(File.expand_path($config[nextconf]["icon"])) |
|---|
| 97 | currentconfig = nextconf |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | |
|---|
| 103 | ############################################################################ |
|---|
| 104 | # set the about dialog |
|---|
| 105 | ############################################################################ |
|---|
| 106 | aboutitem = Gtk::ImageMenuItem.new(Gtk::Stock::ABOUT) |
|---|
| 107 | aboutitem.signal_connect("activate") { |
|---|
| 108 | Gtk::AboutDialog.set_email_hook {|about, link| |
|---|
| 109 | `xdg-open mailto:#{link}` |
|---|
| 110 | } |
|---|
| 111 | Gtk::AboutDialog.set_url_hook {|about, link| |
|---|
| 112 | `xdg-open #{link}` |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | a = Gtk::AboutDialog.new |
|---|
| 116 | a.authors = ["Benjamin Kellermann <Benjamin.Kellermann@gmx.de>"] |
|---|
| 117 | a.comments = "This is an applet to execute commands fastly." |
|---|
| 118 | a.copyright = "Copyright (C) 2009 Benjamin Kellermann" |
|---|
| 119 | a.license = "This program is licenced under CC-by-sa" |
|---|
| 120 | a.logo_icon_name = "gtk-about" |
|---|
| 121 | a.name = "keyspeedapplet" |
|---|
| 122 | a.version = TRAYCOMMANDVERSION.join(".") |
|---|
| 123 | a.website = "http://www.eigenheimstrasse.de/~ben/traycommand/" |
|---|
| 124 | a.website_label = "Download Website" |
|---|
| 125 | a.signal_connect('response') { a.destroy } |
|---|
| 126 | a.show |
|---|
| 127 | } |
|---|
| 128 | menu.append(aboutitem.show) |
|---|
| 129 | |
|---|
| 130 | # add an exit button |
|---|
| 131 | quit = Gtk::ImageMenuItem.new(Gtk::Stock::QUIT) |
|---|
| 132 | quit.signal_connect("activate") { Gtk.main_quit } |
|---|
| 133 | menu.append(Gtk::SeparatorMenuItem.new.show) |
|---|
| 134 | menu.append(quit.show) |
|---|
| 135 | |
|---|
| 136 | $sicon.signal_connect('popup-menu') do |w,e,time| |
|---|
| 137 | menu.popup(nil, nil, e, time) |
|---|
| 138 | end |
|---|
| 139 | |
|---|
| 140 | pid = fork { |
|---|
| 141 | Signal.trap("USR1") { |
|---|
| 142 | $sicon.activate |
|---|
| 143 | } |
|---|
| 144 | Gtk.main |
|---|
| 145 | } |
|---|
| 146 | File.open("/tmp/traycommand-#{$options.configfile}.pid","w"){|f| |
|---|
| 147 | f << pid |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | Process.waitpid(pid) |
|---|