~kripkenstein/topshelf/main

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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
#!/usr/bin/env python
#------------------------------------------------------------------------------------
#
# TopShelf - A 'Currently Important Files' Applet
#
# Copyright (C) 2007  Alon Zakai ('Kripken')
#
# 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 sys
import pickle
import os.path
import subprocess
import mimetypes
import urllib

import gobject
import gtk, gtk.gdk
import pygtk
pygtk.require('2.0')

import gnomeapplet

## Global constants

OPEN_APP = "gnome-open"
HELP_APP = "yelp"

HELP_FILE = "/usr/share/doc/topshelf/topshelf.docbook"

## Global utilities

def popup_warning(window, message):
	dlg = gtk.MessageDialog(parent         = window,
	                        flags          = gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
	                        buttons        = gtk.BUTTONS_OK,
	                        message_format = message,
	                        type           = gtk.MESSAGE_WARNING)

	window.present() # Ensure we are on top, for our losing keep_above to not hide us
	window.set_keep_above(False)
	result = dlg.run()
	dlg.destroy()
	window.set_keep_above(True)

def popup_question(window, message):
	dlg = gtk.MessageDialog(parent         = window,
	                        flags          = gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
	                        buttons        = gtk.BUTTONS_YES_NO,
	                        message_format = message,
	                        type           = gtk.MESSAGE_QUESTION)

	window.present() # Ensure we are on top, for our losing keep_above to not hide us
	window.set_keep_above(False)
	result = dlg.run()
	dlg.destroy()
	window.set_keep_above(True)

	if result == gtk.RESPONSE_YES:
		return True
	elif result == gtk.RESPONSE_NO:
		return False
	elif result == gtk.RESPONSE_DELETE_EVENT:
		return False
	else:
		return False

def try_load_icon(icon_theme, icon_name, icon_size):
	try:
		return icon_theme.load_icon(icon_name, icon_size, 0)
	except gobject.GError:
		return None

def guess_mime_icon(filename):
	ICON_SIZE = 24

	icon_theme = gtk.icon_theme_get_default()

	if os.path.isdir(to_filename(filename)):
		ret = try_load_icon(icon_theme, "gnome-fs-directory", ICON_SIZE)
		if ret is None:
			ret = try_load_icon(icon_theme, "gtk-directory", ICON_SIZE)
		return ret

	filename = mimetypes.guess_type(os.path.basename(filename))[0]

	if filename is None:
		return try_load_icon(icon_theme, "unknown", ICON_SIZE)

	ret = try_load_icon(icon_theme, filename, ICON_SIZE)

	filename = filename.replace("/", "-")

	if ret is None:
		ret = try_load_icon(icon_theme, filename, ICON_SIZE)

	if ret is None:
		ret = try_load_icon(icon_theme, "gnome-mime-" + filename, ICON_SIZE)

	if ret is None:
		ret = try_load_icon(icon_theme, "gnome-mime-" + filename[0:filename.index("-")], ICON_SIZE)

	if ret is None:
		ret = try_load_icon(icon_theme, "unknown", ICON_SIZE)

	return ret

def to_filename(uri_or_something):
	return uri_or_something.replace("file://", "")


### Persistent data

PERSISTENT_FILE = os.path.join(os.path.expanduser("~"), ".topshelf.conf")

# Tests whether persistent data is 0.1, and hence needs migration
def test_point1(data):
	try:
		temp = data.data
		return False
	except AttributeError:
		print "Migration needed to 0.2"
		return True
	
# Migrates persistent data from 0.1 to 0.2
def migrate_point1_to_point2(old):
	print "Migrating to 0.2;", len(old.current_files), "files transferred"
	new = ModernPersistentData()
	new.set(current_files, old.current_files)
	new.set('w',           old.w)
	new.set('h',           old.h)
	return new

class PersistentData: ## Legacy!
	def __init__(self):
		self.current_files = []
		self.w = -1
		self.h = -1

current_files = "current_files" # A constant, really...

DEFAULT_VALUES = { current_files: [],
                   'x': 400,   # For standalone app
                   'y': 300,   # For standalone app
                   'w': 600,
                   'h': 350  }

class ModernPersistentData:
	def __init__(self):
		self.data = { }

	def get(self, key):
		try:
			return self.data[key]
		except KeyError:
			try:
				self.data[key] = DEFAULT_VALUES[key]
				return self.data[key]
			except KeyError:
				print "Fatal error: Key with no default value in ModernPersistentData"

	def set(self, key, value):
		self.data[key] = value


### Data for a single file

NORMAL_BG_COLOR  = "black"
DISABLE_BG_COLOR = "#FF0000"

class FileData:
	def __init__(self, filename, position, short_filename=None, mime_icon=None, color=None):
		self.filename  = filename
		self.position  = position

	def listify(self):
		return (self.filename, self.position, os.path.basename(self.filename), guess_mime_icon(self.filename), NORMAL_BG_COLOR)


### Main class

# full-path, position, short-filename, mime-type-icon, color
FILE_STORE_FULLPATH  = 0
FILE_STORE_POSITION  = 1
FILE_STORE_SHORTNAME = 2
FILE_STORE_MIME_ICON = 3
FILE_STORE_COLOR     = 4

class TopShelf:
	def __init__(self, applet, standalone_window=None):
		self.applet = applet
		self.applet.ts = self # Needed for standalone

		self.standalone_window = standalone_window

		self.window = None

		self.pressed  = False
		self.resizing = False

		self.panel_pb     = gtk.gdk.pixbuf_new_from_file("/usr/share/pixmaps/topshelf-24.png")
		self.panel_off_pb = gtk.gdk.pixbuf_new_from_file("/usr/share/pixmaps/topshelf-24-off.png")

		self.panel_image = gtk.Image()
		self.panel_image.set_from_pixbuf(self.panel_pb)
		self.panel_image.set_tooltip_text("TopShelf")
		self.eventbox = gtk.EventBox()
		self.eventbox.connect("button-press-event", self.do_button_press)
		self.eventbox.drag_dest_set(gtk.DEST_DEFAULT_ALL, [('text/uri-list', 0, 81)], gtk.gdk.ACTION_LINK)
		self.eventbox.connect("drag-data-received", self.do_drag_data_received)
		self.eventbox.connect("key-press-event", self.do_key_press)

		self.eventbox.add(self.panel_image)
		self.applet.add(self.eventbox)
		self.applet.show_all()

		# full-path, position, short-filename, mime-type-icon, color
		self.files_store = gtk.ListStore(str, int, str, gtk.gdk.Pixbuf, str)

		try:
			self.unpickle()
		except IOError:
			self.persistent_data = ModernPersistentData()

		if self.standalone_window is not None:
			self.standalone_window.move(self.persistent_data.get('x'), self.persistent_data.get('y'))
			self.standalone_window.connect("configure-event", self.standalone_window_configure_event)
			self.standalone_window.set_keep_above(True)

	# Events

	def do_button_toggle(self, param1=None):
		if not self.pressed:
			self.show_window()
		else:
			self.hide_window()

	def do_button_press(self, btn, event):
		if event.type == gtk.gdk.BUTTON_PRESS and event.button == 1:
			if not self.resizing:
				self.do_button_toggle()
			else:
				self.do_stop_resizing()
		elif event.type == gtk.gdk.BUTTON_PRESS and event.button == 3:
			btn.emit_stop_by_name("button_press_event")
			propxml="""
				<popup name="button3">
				<menuitem name="Item 3" verb="Help"  label="_Help" pixtype="stock" pixname="gtk-help"/>
				<menuitem name="Item 4" verb="About" label="_About" pixtype="stock" pixname="gtk-about"/>
				</popup>"""
			verbs = [("About", self.do_about),
			         ("Help",  self.do_help)  ]
			self.applet.setup_menu(propxml, verbs, None)

	def standalone_window_configure_event(self, param1=None, param2=None):
		x, y = self.standalone_window.get_position()
		if x != self.persistent_data.get('x') or y != self.persistent_data.get('y'):
			self.persistent_data.set('x', x)
			self.persistent_data.set('y', y)
			self.pickle() # This might thrash, if we get many many events at once. TODO.


	# Actions

	def do_about(self, applet, menu_action):
		self.hide_window() # So we are not concealed!

		logo_pb = gtk.gdk.pixbuf_new_from_file("/usr/share/pixmaps/topshelf-64.png")

		dlg = gtk.AboutDialog()
		dlg.set_icon(logo_pb)

		dlg.set_name("TopShelf")
		dlg.set_program_name("TopShelf")
		dlg.set_version("0.2 - Development")
		dlg.set_comments("Currently Important Files Applet")
		dlg.set_authors(["Alon Zakai (Kripken)"])
		dlg.set_license("TopShelf is free software; you can redistribute it and/or\nmodify it under the terms of the GNU General Public License\nas published by the Free Software Foundation; either version\n2 of the License, or (at your option) any later version.")
		dlg.set_copyright("(c) 2007 Alon Zakai ('Kripken')")
		dlg.set_website("http://launchpad.net/topshelf")
		dlg.set_logo(logo_pb)

		dlg.run()
		dlg.destroy()

		return False

	def do_open(self, param1=None, param2=None, param3=None):
		model, iter = self.get_selected()
		if iter is not None:
			filename = model.get_value(iter, 0)
			ret = subprocess.call(OPEN_APP + " '" + filename + "'", shell=True)
			self.hide_window() # After opening, we hide... for convenience
			if ret != 0:
				popup_warning(self.window, "The file '" + filename + "' could not be opened due to an error.")

		return False

	def do_open_folder(self, param1=None, param2=None, param3=None):
		model, iter = self.get_selected()
		if iter is not None:
			dirname = os.path.dirname(model.get_value(iter, 0))
			ret = subprocess.call(OPEN_APP + " '" + dirname + "'", shell=True)
			self.hide_window() # After opening, we hide... for convenience
			if ret != 0:
				popup_warning(self.window, "The directory '" + dirname + "' could not be opened due to an error.")

		return False

	def do_move_up(self, param1=None):
		model, iter = self.get_selected()
		if iter is not None:
			old_val = model.get(iter, 1)[0]
			new_val = old_val - 1
			self.swap_file_position(model, iter, old_val, new_val)

		return False

	def do_move_down(self, param1=None):
		model, iter = self.get_selected()
		if iter is not None:
			old_val = model.get(iter, 1)[0]
			new_val = old_val + 1
			self.swap_file_position(model, iter, old_val, new_val)

		return False

	def do_add(self, wgt):
		self.hide_window() # So we are not concealed
		chooser = gtk.FileChooserDialog("Select a file to add to TopShelf:",
		                                self.window,
		                                gtk.FILE_CHOOSER_ACTION_OPEN,
		                                buttons=(gtk.STOCK_CANCEL,gtk.RESPONSE_CANCEL,gtk.STOCK_OPEN,gtk.RESPONSE_OK))

# 	    chooser.set_current_folder(...)
# 	    chooser.set_select_multiple(True)

		response = chooser.run()
		filename = chooser.get_filenames() if response == gtk.RESPONSE_OK else None
		chooser.destroy()

		self.show_window()

		if filename is not None:
			self.add_file(filename[0])

		return False

	def do_remove(self, wgt):
		model, iter = self.get_selected()
		if iter is not None:
			if popup_question(self.window,
			                  "Are you sure you want to remove the file '" + model.get(iter, 2)[0] +
			                  "' from the list? (The actual file will not be touched)"):
				old_position = model.get(iter, 1)[0]
				self.files_store.remove(iter)
				self.remove_file_position(model, old_position)

				self.pickle()

		return False

	def do_drag_data_received(self, widget, drag_context, x, y, selection_data, info, timestamp):
		uris = selection_data.data.strip().split()
		for uri in uris:
			filename = urllib.url2pathname(uri)
			self.add_file(filename)

	def do_package(self, param1=None):
		if self.get_n_files() == 0:
			return

		chooser = gtk.FileChooserDialog("Select a filename to save the zipped files as:",
		                                self.window,
		                                gtk.FILE_CHOOSER_ACTION_SAVE,
		                                buttons=(gtk.STOCK_CANCEL,gtk.RESPONSE_CANCEL,gtk.STOCK_SAVE,gtk.RESPONSE_OK))

		response = chooser.run()
		package_name = chooser.get_filenames()[0] if response == gtk.RESPONSE_OK else None
		chooser.destroy()

		if package_name is None:
			return

		file_names = ""
		iter = self.files_store.get_iter_first()
		while iter is not None:
			file_names = file_names + " '" + urllib.url2pathname(self.files_store.get(iter, 0)[0]) + "' "
			iter = self.files_store.iter_next(iter)

		file_names = to_filename(file_names)

		command = "zip '" + package_name + "' " + file_names
		subprocess.call(command, shell=True)

		return False

	def do_help(self, param1=None, param2=None, param3=None):
		self.hide_window()
		ret = subprocess.call(HELP_APP + " '" + HELP_FILE + "'", shell=True)
		if ret != 0:
			popup_warning(self.window, "The help file could not be opened due to an error.")

		return False

	def do_key_press(self, widget, event):
		if event.keyval == 65307:
			self.hide_window()
		elif event.keyval == 65535:
			self.do_remove(None)


	# Persistency

	def unpickle(self):
		pickle_file = open(PERSISTENT_FILE, 'rb')
		self.persistent_data = pickle.load(pickle_file)
		pickle_file.close()

		# Migrate, if needed
		if test_point1(self.persistent_data):
			self.persistent_data = migrate_point1_to_point2(self.persistent_data)

		for file_data in self.persistent_data.get(current_files):
			self.files_store.insert(0, file_data.listify())

	def pickle(self):
		self.persistent_data.set(current_files, [])

		iter = self.files_store.get_iter_first()
		while iter is not None:
			self.persistent_data.get(current_files).append(FileData(*self.files_store.get(iter, *range(self.files_store.get_n_columns()))))
			iter = self.files_store.iter_next(iter)

		pickle_file = open(PERSISTENT_FILE, 'wb')
		pickle.dump(self.persistent_data, pickle_file)
		pickle_file.close()

	# File management

	def add_file(self, filename):
		self.files_store.insert(0, FileData(filename, self.get_n_files()).listify())

		self.pickle()

	def get_file_iter_by_filename(self, filename):
		class Ret:
			def __init__(self):
				self.val = None

			def search(self, m, p, i, filename):
				if m.get(i, 0)[0] == filename:
					self.val = i

		ret = Ret()
		self.files_store.foreach(ret.search, (filename))
		return ret.val

	# Window functions

	def show_window(self):
		if self.window is None:
			self.create_window()

		if not self.resizing:
			self.panel_image.set_from_pixbuf(self.panel_off_pb)
		self.pressed = True

		# These four commands - present, keep-above, present, position - are very odd. But they work, in THIS order.
		self.window.present()
		self.window.set_keep_above(True) # Force it!
		self.window.present()
		self.position_window() # Needed here because of odd behavior otherwise (after adding, hide, show, it moved)

		self.check_files_thread() # One manual operation, then later as a background thread
#		gobject.timeout_add(2000, self.check_files_thread) No thread for now, since it wastes CPU. Later, add an option to do this.

	def hide_window(self, param1=False, param2=False):
		if self.window is None:
			return True

		self.window.hide()

		self.panel_image.set_from_pixbuf(self.panel_pb)
		self.pressed = False

		return True # Stop delete events

	def create_window(self):
		self.window = gtk.Window()

		self.window.set_title("TopShelf")
		self.window.connect("delete-event",       self.hide_window)
		self.window.connect("window-state-event", self.window_state_event)
		self.window.connect("configure-event",    self.configure_event)

		self.window.connect("key-press-event",    self.do_key_press)

		self.window.set_keep_above(True)
		self.window.resize(self.persistent_data.get('w'), self.persistent_data.get('h'))

		self.window.drag_dest_set(gtk.DEST_DEFAULT_ALL, [('text/uri-list', 0, 81)], gtk.gdk.ACTION_LINK)
		self.window.connect("drag-data-received", self.do_drag_data_received)

		# Main view

		self.treeview = gtk.TreeView()
		self.treeview.set_model(self.files_store)
		col = self.append_column(self.treeview, gtk.TreeViewColumn("#",    gtk.CellRendererText(),   text  =1), 1)
		tmp = self.append_column(self.treeview, gtk.TreeViewColumn("",     gtk.CellRendererPixbuf(), pixbuf=3))
		tmp = self.append_column(self.treeview, gtk.TreeViewColumn("File", gtk.CellRendererText(),   text  =2), 2,
		                         foreground_id=FILE_STORE_COLOR)

		col.clicked() # Set default sort
		self.treeview.connect("row-activated", self.do_open)

		scrolled_window = gtk.ScrolledWindow()
		scrolled_window.add(self.treeview)
		scrolled_window.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)

		# Toolbar & buttons on bottom

		toolbar = gtk.Toolbar()

		self.add_stock_button    (toolbar, gtk.STOCK_OPEN,                            self.do_open       , "Open file")
		self.add_altstock_button (toolbar, gtk.STOCK_DIRECTORY,      "Open Folder",   self.do_open_folder, "Open folder containing file")
		self.add_stock_button    (toolbar, gtk.STOCK_GO_UP,                           self.do_move_up    , "Move file up in order")
		self.add_stock_button    (toolbar, gtk.STOCK_GO_DOWN,                         self.do_move_down  , "Move file down in order")
		self.add_stock_button    (toolbar, gtk.STOCK_ADD,                             self.do_add        , "Add a file to the list")
		self.add_stock_button    (toolbar, gtk.STOCK_REMOVE,                          self.do_remove     , "Remove file from the list")
#		self.add_pixbuf_button   (toolbar, guess_mime_icon("a.zip"), "Package Files", self.do_package)
#		self.add_stock_button    (toolbar, gtk.STOCK_HELP,                            self.do_help       , "Help")

		# Main vbox

		vbox = gtk.VBox()

#		label = gtk.Label("<b>TopShelf: Currently Important Files</b>")
#		label.set_use_markup(True)
#		vbox.add(label)
#		vbox.child_set_property(label, "expand", False)
#		vbox.child_set_property(label, "border", 2)

		vbox.add(scrolled_window)
		vbox.add(toolbar)
		vbox.child_set_property(toolbar, "expand", False)

		# Frames

		inner_frame = gtk.Frame()
		inner_frame.set_shadow_type(gtk.SHADOW_IN)
		inner_frame.set_border_width(self.panel_image.get_allocation().height/8)

		outer_frame = gtk.Frame()
		outer_frame.set_shadow_type(gtk.SHADOW_OUT)

		inner_frame.add(vbox)
		outer_frame.add(inner_frame)

		# Finalize main window

		self.window.add(outer_frame)
		self.position_window()
		self.window.show_all()

	def window_state_event(self, widget, event):
		if event.changed_mask & gtk.gdk.WINDOW_STATE_ICONIFIED:
			self.hide_window()
			self.window.deiconify()

	def configure_event(self, widget, event):
		w, h = self.window.get_size()

		if w != self.persistent_data.get('w') or h != self.persistent_data.get('h'):
			self.persistent_data.set('w', w)
			self.persistent_data.set('h', h)

			self.pickle()

	def add_button(self, toolbar, image, text, callback, tooltip):
		btn = gtk.ToolButton()
		btn.set_icon_widget(image)
		btn.set_label(text)
		btn.connect("clicked", callback)
		btn.set_tooltip_text(tooltip)
		toolbar.insert(btn, -1)

	def add_altstock_button(self, toolbar, icon_id, text, callback, tooltip):
		icon = gtk.Image()
		icon.set_from_stock(icon_id, gtk.ICON_SIZE_LARGE_TOOLBAR)
		self.add_button(toolbar, icon, text, callback, tooltip)

#	def add_pixbuf_button(self, toolbar, pixbuf, text, callback, ):
#		icon = gtk.Image()
#		icon.set_from_pixbuf(pixbuf)
#		self.add_button(toolbar, icon, text, callback)

	def add_stock_button(self, toolbar, stock_id, callback, tooltip):
		btn = gtk.ToolButton(stock_id)
		btn.connect("clicked", callback)
		btn.set_tooltip_text(tooltip)
		toolbar.insert(btn, -1)

	def append_column(self, treeview, column, sort_id=None, foreground_id=None):
		renderer = column.get_cell_renderers()[0]
		if sort_id is not None:
			column.set_sort_column_id(sort_id)
		if foreground_id is not None:
			column.add_attribute(renderer, "foreground", foreground_id)
		treeview.append_column(column)
		return column

	def position_window(self):

		# This function was converted from a C function in clock.c in the GNOME
		# panel applet 'clock', which is GPL 2+ (like this code). Thanks to the
		# GNOME authors,
		#       Miguel de Icaza
 		#       Frederico Mena
		#       Stuart Parmenter
		#       Alexander Larsson
		#       George Lebl
		#       Gediminas Paulauskas
		#       Mark McLoughlin

		x,y = self.panel_image.window.get_origin()

		w,h = self.window.get_size()

		button_w = self.panel_image.get_allocation().width
		button_h = self.panel_image.get_allocation().height

		screen = self.window.get_screen()

		n = screen.get_n_monitors()
		found_monitor = False
		monitor = None
		for i in range(n):
			monitor = screen.get_monitor_geometry(i)
			if (x >= monitor.x and x <= monitor.x + monitor.width and
			    y >= monitor.y and y <= monitor.y + monitor.height):
				found_monitor = True
				break

		if not found_monitor:
			monitor = gtk.gdk.Rectangle()
			monitor.x = 0
			monitor.y = 0
			monitor.width  = screen.get_width()
			monitor.height = screen.get_height()

		if x > monitor.width/2:
			x -= w
			x += button_w

		if y < monitor.height/2:
			y += button_h
		else:
			y -= h

		self.window.move(x, y)


	# Threads

	def check_files_thread(self):
		if self.pressed:
			iter = self.files_store.get_iter_first()
			while iter is not None:
				filename = to_filename(self.files_store.get(iter, 0)[0])
				if os.path.exists(filename):
					self.files_store.set(iter, FILE_STORE_COLOR, NORMAL_BG_COLOR)
				else:
					self.files_store.set(iter, FILE_STORE_COLOR, DISABLE_BG_COLOR)
				iter = self.files_store.iter_next(iter)
			return True
		else:
			return False

	# Utilities

	def get_selected(self):
		return self.treeview.get_selection().get_selected()

	def get_n_files(self):
		ret = self.files_store.iter_n_children(None)
		return ret

	def swap_file_position(self, model, iter, old_val, new_val):
		if old_val >= 0 and new_val >= 0 and old_val < self.get_n_files() and new_val < self.get_n_files():
			model.foreach(lambda m, p, i, vs: None if m.get(i, 1)[0] != vs[1] else m.set(i, 1, vs[0]),
			              (old_val, new_val))
			model.set(iter, 1, new_val)

		self.pickle()

	def remove_file_position(self, model, old_position):
		model.foreach(lambda m, p, i, op: m.set(i, 1, m.get(i, 1)[0]-1) if m.get(i, 1)[0] > op else None,
		              old_position)


### Factory

def factory(applet, iid, standalone_window=None):
	ts = TopShelf(applet, standalone_window)

	return True


### Main

if len(sys.argv) == 1:
	# Run standalone
	main_window = gtk.Window(gtk.WINDOW_TOPLEVEL)
	main_window.set_title("TopShelf")
	main_window.connect("destroy", gtk.main_quit)

	applet = gnomeapplet.Applet()
	factory(applet, None, standalone_window=main_window)
	applet.reparent(main_window)
	main_window.show_all()
	gtk.main()

	sys.exit()

if __name__ == '__main__':
	gnomeapplet.bonobo_factory("OAFIID:Gnome_Panel_TopShelf_Factory", gnomeapplet.Applet.__gtype__, "A current files manager", "1.0", factory)