root/linux/traycommand/traycommand @ 2275

Revision 2275, 4.0 KB (checked in by martin_r, 3 years ago)

gconf2 in fehlermeldung verpackt und programm ohne parameter aufrufbar gemacht

  • 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
12begin
13        require 'gconf2'
14rescue LoadError
15        $stderr << "########################################################\n"
16        $stderr << "# In order to run this programm you need ruby-gconf2!  #\n"
17        $stderr << "########################################################\n"
18        raise
19end
20
21require 'yaml'
22require 'pp'
23require 'optparse'
24require 'ostruct'
25
26TRAYCOMMANDVERSION = [0,1]
27
28Gtk.init
29$options = OpenStruct.new
30       
31optpars = 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
48begin
49        optpars.parse!
50        $options.configfile ||= "layoutswitch.yaml"
51rescue => e
52        puts e
53        puts optpars
54        exit
55end
56
57############################################################################
58# init the things from the configfile
59############################################################################
60unless 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
72end
73
74############################################################################
75# init the tray and GTK stuff
76############################################################################
77currentconfig = 0
78$sicon = Gtk::StatusIcon.new
79$sicon.pixbuf = Gdk::Pixbuf.new(File.expand_path($config[currentconfig]["icon"]))
80
81# Add a menu
82menu = 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############################################################################
106aboutitem = Gtk::ImageMenuItem.new(Gtk::Stock::ABOUT)
107aboutitem.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}
128menu.append(aboutitem.show)
129
130# add an exit button
131quit = Gtk::ImageMenuItem.new(Gtk::Stock::QUIT)
132quit.signal_connect("activate") { Gtk.main_quit }
133menu.append(Gtk::SeparatorMenuItem.new.show)
134menu.append(quit.show)
135
136$sicon.signal_connect('popup-menu') do |w,e,time|
137        menu.popup(nil, nil, e, time) 
138end
139
140pid = fork {
141        Signal.trap("USR1") {
142                $sicon.activate
143        }
144        Gtk.main
145}
146File.open("/tmp/traycommand-#{$options.configfile}.pid","w"){|f|
147        f << pid
148}
149
150Process.waitpid(pid)
Note: See TracBrowser for help on using the browser.