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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
|
#! /usr/bin/python
# -*- coding: UTF-8 -*-
"""
Copyright (C) 2005-07 Adolfo González Blázquez <code@infinicode.org>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
If you find any bugs or have any suggestions email: code@infinicode.org
"""
import pygtk
pygtk.require("2.0")
import gtk
import gobject
import pynotify
import os
import sys
import signal
import getopt
import time
from cameramonitor import preferences
from cameramonitor import utils
from cameramonitor import camglobals as glob
from cameramonitor import autostart
class CameraMonitor:
# Init method
def __init__(self):
if utils.pid_check(): sys.exit(0)
else: utils.pid_save()
self.video_device = ''
self.timeout = 0
self.notify = True
self.autostart = False
self.prefs = preferences.CameraMonitorPreferences('nested')
self.video_device = self.prefs.preferences_get_video_device()
self.timeout = self.prefs.preferences_get_timeout()
self.notify = self.prefs.preferences_get_notify()
self.autostart = self.prefs.preferences_get_autostart()
if self.autostart: autostart.create_autostart_file()
else: autostart.delete_autostart_file()
self.state = False
pynotify.init(glob.name)
self.create_icon()
self.menu = self.create_menu()
self.timeout_source = gobject.timeout_add (self.timeout, self.timeout_callback)
# Checks if camera is on
def camera_on(self):
#out = os.popen('lsof /dev/video1', 'r')
#text = out.read()
text = os.popen3('fuser '+self.video_device, 'r')[1].read()
return text != ""
# Callback: Timeout callback
def timeout_callback(self):
self.video_device = self.prefs.preferences_get_video_device()
self.timeout = self.prefs.preferences_get_timeout()
self.notify = self.prefs.preferences_get_notify()
timeout = self.timeout * 1000
if self.state != self.camera_on():
if self.camera_on():
self.tray.set_visible(True)
gtk.gdk.flush()
if self.notify: self.create_notify("Camera is On\nVideo device: %s" % self.video_device)
self.state = True
else:
if self.notify: self.create_notify("Camera is Off")
gtk.gdk.flush()
time.sleep(2)
self.tray.set_visible(False)
self.state = False
if timeout != self.timeout:
self.timeout_source = gobject.timeout_add (self.timeout*1000, self.timeout_callback)
return False
else:
return True
# Callback: exit
def exit_callback(self, widget):
gtk.main_quit()
utils.pid_clean()
return
# Callback: Icon clicked
def on_popup_menu(self, status, button, time):
self.menu.popup(None, None, gtk.status_icon_position_menu, button, time, self.tray)
# Create tray icon
def create_icon(self):
self.tray = gtk.StatusIcon()
self.tray.set_from_file(glob.image)
self.tray.set_tooltip('Camera On')
self.tray.connect('popup-menu', self.on_popup_menu)
self.tray.set_visible(self.camera_on())
# Displays notification
def create_notify(self, msg):
n = pynotify.Notification(glob.name_long, msg)
p = gtk.gdk.pixbuf_new_from_file(glob.image)
n.set_icon_from_pixbuf(p)
# Attach the notification to status icon if possible
try:
n.attach_to_status_icon(self.tray)
except:
pass
n.show()
# Creates the Contextual Menu
def create_menu(self):
menu = gtk.Menu()
pref = gtk.ImageMenuItem(stock_id=gtk.STOCK_PREFERENCES, accel_group=None)
pref.connect("activate", self.prefs.preferences_show)
about = gtk.ImageMenuItem(stock_id=gtk.STOCK_ABOUT, accel_group=None)
about.connect("activate", self.about_show)
sep = gtk.SeparatorMenuItem()
quit = gtk.ImageMenuItem(stock_id=gtk.STOCK_QUIT, accel_group=None)
quit.connect("activate", self.exit_callback)
menu.add(pref)
menu.add(about)
menu.add(sep)
menu.add(quit)
menu.show_all()
return menu
# Creates about dialog
def about_show(self, widget):
about = gtk.AboutDialog()
about.set_name(glob.name_long + ' '+ glob.version)
about.set_authors(glob.authors)
about.set_comments(glob.description)
about.set_logo(gtk.gdk.pixbuf_new_from_file(glob.logo))
about.set_license(glob.license)
about.set_wrap_license(True)
about.set_copyright(glob.copyright)
def openHomePage(widget,url,url2):
import webbrowser
webbrowser.open_new(url)
gtk.about_dialog_set_url_hook(openHomePage,glob.website)
about.set_website(glob.website)
about.run()
about.destroy()
# Handle signals 2, 3 and 15
def signal_handler(sig, frame):
utils.pid_clean()
gtk.main_quit()
# Prints application usage from command line
def command_usage():
print "Usage: %s [options]" % os.path.basename(sys.argv[0])
print "\nApplication options:"
print " -h, --help Displays this dialog"
print " -p, --prefs Display the preferences dialog"
sys.exit(0)
# Parses options passed by command line and acts
def parse_commandline_options():
try:
opts, args = getopt.getopt(sys.argv[1:],"hp", ["help","prefs"])
except:
print "Invalid options\n"
command_usage()
sys.exit(2)
for o, a in opts:
if o in ("-h", "--help"):
command_usage()
elif o in ("-p", "--prefs"):
command_preferences()
sys.exit(0)
# Display Preferences dialog, instead of running the app
def command_preferences():
pid = utils.pid_read()
if not utils.pid_running(pid): utils.display_dialog_warning("Camera Monitor is not running!")
prefs = preferences.CameraMonitorPreferences('alone')
prefs.preferences_show(None)
gtk.main()
# Main method
def main():
parse_commandline_options()
# Handle signals 2, 3 and 15
signal.signal(2, signal_handler)
signal.signal(3, signal_handler)
signal.signal(15, signal_handler)
camera = CameraMonitor()
gtk.main()
if __name__ == "__main__":
main()
|