# Created by Matteo Rattotti on 13/03/06. # Copyright (c) 2006 Matteo Rattotti. All rights reserved. # Contact matteo.rattotti@gmail.com for any problem # 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 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. from Foundation import * from AppKit import * import delibarAppDelegate class menuBuilder(NSObject): def setMenu(self, menu): self.plainMenu = 0 self.menu = menu def deleteMenu(self): c = self.menu.itemArray() num = self.menu.numberOfItems() index = 0 for a in c: if index > 7: self.menu.removeItem_(a) index = index + 1 def createBookmark(self, post): urlIcon = NSImage.alloc().initByReferencingFile_("url.png") d = post['desc'] if len(d) > 45: post['desc'] = post['desc'][:21] + " ... " + post['desc'][-21:] title = post['desc'].capitalize() subitem = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_(title, delibarAppDelegate.delibarAppDelegate.openurl, "") subitem.setImage_(urlIcon) subitem.setToolTip_(post['url']) return subitem def buildRecentMenu(self, store): recentIcon = NSImage.alloc().initByReferencingFile_("recent.png") recentMItem = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_("recent posts:", None, "") recentMItem.setImage_(recentIcon) recentSubMenu = NSMenu.alloc().initWithTitle_("recent posts:") recentMItem.setSubmenu_(recentSubMenu) self.menu.addItem_(recentMItem) for url in store.returnRecent(): subI = self.createBookmark(url) recentSubMenu.addItem_(subI) menuSep = NSMenuItem.separatorItem() self.menu.addItem_(menuSep) def buildBundleMenu(self, store): if len(store.returnBundles()) == 0: return bundleLabel = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_("Bundles:", None, "") self.menu.addItem_(bundleLabel) dirIcon = NSImage.alloc().initByReferencingFile_("folder.png") bundleIcon = NSImage.alloc().initByReferencingFile_("bundle.png") #dirIcon = NSImage.alloc().initByReferencingFile_("folder2.png") bundles = store.returnBundles() for b in bundles: # Setting each bundle menu' menuItem = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_(b, None, "") menuItem.setImage_(bundleIcon) self.menu.addItem_(menuItem) subm = NSMenu.alloc().initWithTitle_(b) menuItem.setSubmenu_(subm) # Filling bundle menu' with proper tag, and setting tag as menu for tag in bundles[b]: tagItem = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_(tag, None, "") tagItem.setImage_(dirIcon) subm.addItem_(tagItem) tagMenu = NSMenu.alloc().initWithTitle_(tag) tagItem.setSubmenu_(tagMenu) # Creating bookmark in tag menu for url in store.__getattr__(tag): subI = self.createBookmark(url) tagMenu.addItem_(subI) menuSep = NSMenuItem.separatorItem() self.menu.addItem_(menuSep) def buildTagMenu(self, store, sortByFreq = 0, useMin=0): tagLabel = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_("Tags:", None, "") self.menu.addItem_(tagLabel) # initialize icon for folder and url dirIcon = NSImage.alloc().initByReferencingFile_("folder.png") #dirIcon = NSImage.alloc().initByReferencingFile_("folder2.png") urlIcon = NSImage.alloc().initByReferencingFile_("url.png") tagList = [] for t in store.tag: tagList.append(t) # Sort tag if sortByFreq != 0: # Argh this is a fucking workaround def freqSorter(x,y): if len(store.__getattr__(x)) > len(store.__getattr__(y)): return -1 elif len(store.__getattr__(x)) < len(store.__getattr__(y)): return 1 else: return 0 # end of fucking workaround tagList.sort(freqSorter) else: tagList.sort() # Also this is very shitty if useMin >0: nT = [] for t in tagList: #print t, len(store.__getattr__(t)) if len(store.__getattr__(t)) >= useMin: nT.append(t) tagList = nT #print tagList # Must rework on code ;-) for tag in tagList: menuItem = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_(tag, None, "") menuItem.setToolTip_("N.Post: " + str(len(store.__getattr__(tag)))) menuItem.setImage_(dirIcon) self.menu.addItem_(menuItem) subm = NSMenu.alloc().initWithTitle_(tag) if self.plainMenu == 0: for post in store.__getattr__(tag): tlist = post['tag'].split(" ") if len(tlist) > 1: for mtag in tlist: if mtag != tag: mtag = mtag subitem = self.createBookmark(post) #NSMenuItem.alloc().initWithTitle_action_keyEquivalent_(post['desc'].capitalize(), delibarAppDelegate.delibarAppDelegate.openurl, "") subitem.setImage_(urlIcon) subitem.setToolTip_(post['url']) try: tmenu = subm.itemWithTitle_(mtag).submenu() tmenu.addItem_(subitem) except Exception, e: #print Exception #print e mItem = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_(mtag, None, "") mItem.setImage_(dirIcon) mSubm = NSMenu.alloc().initWithTitle_(mtag) mSubm.addItem_(subitem) mItem.setSubmenu_(mSubm) subm.addItem_(mItem) else: subitem = self.createBookmark(post) #NSMenuItem.alloc().initWithTitle_action_keyEquivalent_(post['desc'].capitalize(), delibarAppDelegate.delibarAppDelegate.openurl, "") subitem.setImage_(urlIcon) subitem.setToolTip_(post['url']) subm.addItem_(subitem) b = [] for a in subm.itemArray(): b.append(a.title()) finSubm = NSMenu.alloc().initWithTitle_(tag) b.sort() for item in b: t = subm.itemWithTitle_(item) subm.removeItem_(t) finSubm.addItem_(t) menuItem.setSubmenu_(finSubm) else: for post in store.__getattr__(tag): subitem = self.createBookmark(post) #NSMenuItem.alloc().initWithTitle_action_keyEquivalent_(post['desc'], delibarAppDelegate.delibarAppDelegate.openurl, "") subitem.setImage_(urlIcon) subitem.setToolTip_(post['url']) subm.addItem_(subitem) menuItem.setSubmenu_(subm)