File: gnomeplugin.py

package info (click to toggle)
backintime 0.9.26-4
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 1,992 kB
  • ctags: 447
  • sloc: python: 4,542; xml: 526; sh: 185; makefile: 20
file content (184 lines) | stat: -rw-r--r-- 4,735 bytes parent folder | download
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
#    Back In Time
#    Copyright (C) 2008-2009 Oprea Dan
#
#    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.


import os
import pluginmanager
import tools
import logger
import threading
import time


if len( os.getenv( 'DISPLAY', '' ) ) == 0:
	os.putenv( 'DISPLAY', ':0.0' )


class GnomePlugin( pluginmanager.Plugin ):

	class Systray( threading.Thread ):
		def __init__( self, snapshots ):
			import pygtk
			pygtk.require("2.0")
			import gtk
			import gnome

			threading.Thread.__init__( self )
			self.stop_flag = False
			self.snapshots = snapshots
			self.config = self.snapshots.config

			gnome_props = { gnome.PARAM_APP_DATADIR : '/usr/share' }
			gnome.program_init( 'backintime', self.config.VERSION, properties = gnome_props )

		def start( self ):
			self.stop_flag = False
			threading.Thread.start( self )

		def stop( self ):
			self.stop_flag = True
			try:
				self.join()
			except:
				pass

		def show_notification( self, icon ):
			import pynotify
			self.notification.set_timeout( pynotify.EXPIRES_NEVER )
			self.notification.show()

		def run( self ):
			import gtk
			import pynotify

			logger.info( '[GnomePlugin.Systray.run]' )

			gtk.gdk.threads_init()
			display = gtk.gdk.display_get_default()
			
			if display is None:
				logger.info( '[GnomePlugin.Systray.run] display KO' )
				return

			status_icon = None
			try:
				status_icon = gtk.StatusIcon()
			except:
				pass

			if status_icon is None:
				logger.info( '[GnomePlugin.Systray.run] no status_icon' )
				return

			attach_notification = True
			last_message = None
			first_error = self.config.is_notify_enabled()

			status_icon.set_from_stock( gtk.STOCK_SAVE )
			status_icon.set_visible( True )

			pynotify.init( self.config.APP_NAME )
			self.notification = pynotify.Notification( self.config.APP_NAME, '', '' )
			self.notification.set_urgency( pynotify.URGENCY_NORMAL )
			self.notification.set_timeout( pynotify.EXPIRES_NEVER )

			status_icon.connect('activate', self.show_notification )

			logger.info( '[GnomePlugin.Systray.run] begin loop' )

			while True:
				gtk.main_iteration( False )
				if self.stop_flag:
					break

				if not gtk.events_pending():
					if attach_notification:
						attach_notification = False
						self.notification.attach_to_status_icon( status_icon )

					message = self.snapshots.get_take_snapshot_message()
					if message is None and last_message is None:
						message = ( 0, _('Working...') )

					if not message is None:
						if message != last_message:
							last_message = message

							urgency = pynotify.URGENCY_NORMAL
							icon_name = gtk.STOCK_SAVE
							status_icon_blinking = False
							if last_message[0] != 0:
								urgency = pynotify.URGENCY_CRITICAL
								icon_name = 'dialog-error'
								status_icon_blinking = True

							status_icon.set_blinking( status_icon_blinking )
							status_icon.set_tooltip( last_message[1] )

							self.notification.update( self.config.APP_NAME, last_message[1], icon_name )
							self.notification.set_urgency( urgency )
						
							if last_message[0] != 0 and first_error:
								first_error = False
								self.notification.set_timeout( 10000 )
								self.notification.show()

					time.sleep( 0.2 )
			
			status_icon.set_visible( False )
			gtk.main_iteration( False )

			self.notification.close()
			pynotify.uninit()
			
			logger.info( '[GnomePlugin.Systray.run] end loop' )

	def __init__( self ):
		return

	def init( self, snapshots ):
		if not tools.process_exists( 'gnome-settings-daemon' ):
			return False

		if not tools.check_x_server():
			return False

		self.systray = None
		try:
			self.systray = GnomePlugin.Systray( snapshots )
		except:
			self.systray = None

		return True

	def is_gui( self ):
		return True

	def on_process_begins( self ):
		if not self.systray is None:
			self.systray.start()

	def on_process_ends( self ):
		if not self.systray is None:
			self.systray.stop()

	def on_error( self, code, message ):
		return

	def on_new_snapshot( self, snapshot_id, snapshot_path ):
		return