root/linux/traycommand/traycommand @ 1883

Revision 1883, 3.7 KB (checked in by ben, 4 years ago)

Tool um kommandos über trayicon auszuführen hinzugefügt

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