mintmenu/usr/lib/linuxmint/mintMenu/plugins/recent.py

227 lines
7.6 KiB
Python
Raw Permalink Normal View History

2014-03-28 15:44:13 +00:00
#!/usr/bin/env python
2013-03-08 01:53:03 +00:00
import gi
gi.require_version("Gtk", "2.0")
from gi.repository import Gtk, Pango
2009-07-27 11:45:34 +01:00
import os
2013-03-08 01:53:03 +00:00
from easygsettings import EasyGSettings
2009-07-27 11:45:34 +01:00
from execute import Execute
from easyfiles import *
from easybuttons import *
class pluginclass:
2011-03-30 19:02:13 +01:00
"""This is the main class for the plugin"""
"""It MUST be named pluginclass"""
2009-07-27 11:45:34 +01:00
2011-03-30 19:02:13 +01:00
def __init__( self, mintMenuWin, toggleButton, de ):
2009-07-27 11:45:34 +01:00
2011-03-30 19:02:13 +01:00
self.Win = mintMenuWin
self.toggleButton = toggleButton
self.de = de
2009-07-27 11:45:34 +01:00
2013-03-08 01:53:03 +00:00
self.builder = Gtk.Builder()
2011-03-30 19:02:13 +01:00
#The Glade file for the plugin
2013-03-08 01:53:03 +00:00
self.builder.add_from_file (os.path.join( os.path.dirname( __file__ ), "recent.glade" ))
2009-07-27 11:45:34 +01:00
2011-03-30 19:02:13 +01:00
#Set 'window' property for the plugin (Must be the root widget)
2013-03-08 01:53:03 +00:00
self.window = self.builder.get_object( "window1" )
2011-03-30 19:02:13 +01:00
#Set 'heading' property for plugin
self.heading = _("Recent documents")
#This should be the first item added to the window in glade
2013-03-08 01:53:03 +00:00
self.content_holder = self.builder.get_object( "eventbox1" )
2011-03-30 19:02:13 +01:00
2013-05-21 16:24:01 +01:00
self.recentBox = self.builder.get_object("RecentBox")
self.recentVBox = self.builder.get_object( "vbox1" )
2011-03-30 19:02:13 +01:00
#Specify plugin width
self.width = 250
2009-07-27 11:45:34 +01:00
2011-03-30 19:02:13 +01:00
#Plugin icon
self.icon = 'mate-folder.png'
2009-07-27 11:45:34 +01:00
2013-03-08 01:53:03 +00:00
self.settings = EasyGSettings ("com.linuxmint.mintmenu.plugins.recent")
self.settings.notifyAdd( 'height', self.RegenPlugin )
self.settings.notifyAdd( 'width', self.RegenPlugin )
self.settings.notifyAdd( 'num-recent-docs', self.RegenPlugin )
self.settings.notifyAdd( 'recent-font-size', self.RegenPlugin )
2009-07-27 11:45:34 +01:00
2011-03-30 19:02:13 +01:00
self.FileList=[]
2013-03-08 01:53:03 +00:00
self.RecManagerInstance = Gtk.RecentManager.get_default()
2013-05-21 18:15:39 +01:00
self.recentManagerId = self.RecManagerInstance.connect("changed", self.DoRecent)
2011-03-30 19:02:13 +01:00
self.RegenPlugin()
2013-03-08 01:53:03 +00:00
self.builder.get_object( "RecentTabs" ).set_current_page(1)
2011-03-30 19:02:13 +01:00
#Connect event handlers
2013-03-08 01:53:03 +00:00
self.builder.get_object("ClrBtn").connect("clicked", self.clrmenu)
2009-07-27 11:45:34 +01:00
2011-03-30 19:02:13 +01:00
def wake (self) :
pass
2013-05-21 18:15:39 +01:00
def destroy( self ):
self.recentBox.destroy()
self.recentVBox.destroy()
self.builder.get_object( "RecentTabs" ).destroy()
self.builder.get_object("ClrBtn").destroy()
self.content_holder.destroy()
self.settings.notifyRemoveAll()
if self.recentManagerId:
self.RecManagerInstance.disconnect(self.recentManagerId)
2011-03-30 19:02:13 +01:00
def RegenPlugin( self, *args, **kargs ):
2013-03-08 01:53:03 +00:00
self.GetGSettingsEntries()
2011-03-30 19:02:13 +01:00
2013-03-08 01:53:03 +00:00
def GetGSettingsEntries( self ):
self.recenth = self.settings.get( 'int', 'height' )
self.recentw = self.settings.get( 'int', 'width' )
self.numentries = self.settings.get( 'int', 'num-recent-docs' )
self.recentfontsize = self.settings.get( 'int', 'recent-font-size' )
2011-03-30 19:02:13 +01:00
# Hide vertical dotted separator
2013-03-08 01:53:03 +00:00
self.hideseparator = self.settings.get( "bool", "hide-separator" )
2011-03-30 19:02:13 +01:00
# Plugin icon
2013-03-08 01:53:03 +00:00
self.icon = self.settings.get( "string", 'icon' )
2011-03-30 19:02:13 +01:00
# Allow plugin to be minimized to the left plugin pane
2013-03-08 01:53:03 +00:00
self.sticky = self.settings.get( "bool", "sticky" )
self.minimized = self.settings.get( "bool", "minimized" )
2011-03-30 19:02:13 +01:00
self.RebuildPlugin()
def SetHidden( self, state ):
if state == True:
2013-03-08 01:53:03 +00:00
self.settings.set( "bool", "minimized", True )
2011-03-30 19:02:13 +01:00
else:
2013-03-08 01:53:03 +00:00
self.settings.set( "bool", "minimized", False )
2011-03-30 19:02:13 +01:00
def RebuildPlugin(self):
self.content_holder.set_size_request(self.recentw, self.recenth )
self.DoRecent()
def DoRecent( self, *args, **kargs ):
2013-05-21 16:24:01 +01:00
for i in self.recentBox.get_children():
2011-03-30 19:02:13 +01:00
i.destroy()
2013-05-21 16:24:01 +01:00
self.recentVBox.set_size_request( self.recentw, self.recenth )
if len( self.recentBox.get_children() ) < self.numentries:
n=len( self.recentBox.get_children() )-1
2011-03-30 19:02:13 +01:00
else:
n=self.numentries-1
while n >= 0:
2013-05-21 16:24:01 +01:00
self.recentBox.remove( self.recentBox.get_children()[n] )
2011-03-30 19:02:13 +01:00
n-=1
self.FileList, self.IconList = self.GetRecent()
loc = 0
for Name in self.FileList:
if Name != None:
self.AddRecentBtn( Name, self.IconList[loc] )
loc = loc + 1
return True
def clrmenu(self, *args, **kargs):
self.RecManagerInstance.purge_items()
self.DoRecent()
return
def AddRecentBtn( self, Name, RecentImage ):
DispName=os.path.basename( Name )
2013-03-08 01:53:03 +00:00
AButton = Gtk.Button( "", "ok", True )
2011-03-30 19:02:13 +01:00
AButton.remove( AButton.get_children()[0] )
AButton.set_size_request( 200, -1 )
2013-03-08 01:53:03 +00:00
AButton.set_relief( Gtk.ReliefStyle.NONE )
2011-03-30 19:02:13 +01:00
AButton.connect( "clicked", self.callback, Name )
2013-03-08 01:53:03 +00:00
Align1 = Gtk.Alignment()
Align1.set( 0, 0.5, 0, 0)
2011-03-30 19:02:13 +01:00
Align1.set_padding( 0, 0, 0, 0 )
2013-03-08 01:53:03 +00:00
HBox1 = Gtk.HBox( False, 5 )
VBox1 = Gtk.VBox( False, 2 )
2011-03-30 19:02:13 +01:00
VBox1.show()
2013-03-08 01:53:03 +00:00
req = Gtk.Requisition()
AButton.size_request(req)
2011-03-30 19:02:13 +01:00
2013-03-08 01:53:03 +00:00
Label1 = Gtk.Label( DispName )
Label1.set_size_request( req.width-20, -1 )
Label1.set_ellipsize( Pango.EllipsizeMode.END )
2011-03-30 19:02:13 +01:00
Label1.show()
VBox1.add( Label1 )
2013-03-08 01:53:03 +00:00
ButtonIcon = Gtk.Image()
2011-03-30 19:02:13 +01:00
ButtonIcon.set_from_pixbuf(RecentImage)
HBox1.add( ButtonIcon )
ButtonIcon.show()
HBox1.add( VBox1 )
HBox1.show()
Align1.add( HBox1 )
Align1.show()
AButton.add( Align1 )
AButton.show()
2013-05-21 16:24:01 +01:00
self.recentBox.pack_start( AButton, False, True, 0 )
2011-03-30 19:02:13 +01:00
def callback(self, widget, filename=None):
self.Win.hide()
2012-10-21 19:45:49 +01:00
x = os.system("gvfs-open \""+filename+"\"")
2011-03-30 19:02:13 +01:00
if x == 256:
2013-03-08 01:53:03 +00:00
dia = Gtk.Dialog('File not found!',
2011-03-30 19:02:13 +01:00
None, #the toplevel wgt of your app
2013-03-08 01:53:03 +00:00
Gtk.DialogFlags.MODAL | Gtk.DialogFlags.DESTROY_WITH_PARENT, #binary flags or'ed together
2011-03-30 19:02:13 +01:00
("Ok", 77))
2013-03-08 01:53:03 +00:00
dia.vbox.pack_start(Gtk.Label('The location or file could not be found!'), False, False, 0)
2011-03-30 19:02:13 +01:00
dia.vbox.show_all()
dia.show()
result = dia.run()
if result == 77:
dia.destroy()
def GetRecent(self, *args, **kargs):
FileString=[]
IconString=[]
RecentInfo=self.RecManagerInstance.get_items()
# print RecentInfo[0].get_icon(gtk.ICON_SIZE_MENU)
count=0
MaxEntries=self.numentries
if self.numentries == -1:
MaxEntries=len(RecentInfo)
for items in RecentInfo:
FileString.append(items.get_uri_display())
2013-03-08 01:53:03 +00:00
IconString.append(items.get_icon(Gtk.IconSize.MENU))
2011-03-30 19:02:13 +01:00
count+=1
if count >= MaxEntries:
break
return FileString, IconString
def ButtonClicked( self, widget, event, Exec ):
self.press_x = event.x
self.press_y = event.y
self.Exec = Exec
def ButtonReleased( self, w, ev, ev2 ):
if ev.button == 1:
if not hasattr( self, "press_x" ) or \
not w.drag_check_threshold( int( self.press_x ),
int( self.press_y ),
int( ev.x ),
int( ev.y ) ):
if self.Win.pinmenu == False:
self.Win.wTree.get_widget( "window1" ).hide()
if "applications" in self.Win.plugins:
self.Win.plugins["applications"].wTree.get_widget( "entry1" ).grab_focus()
Execute( w, self.Exec )
def do_plugin(self):
self.DoRecent()