# 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. import os import deliLoader import deliExport import setOnStartup import prefHandler import menuBuilder import getProxy from Foundation import * from AppKit import * from PyObjCTools import NibClassBuilder class delibarAppDelegate(NibClassBuilder.AutoBaseClass): def buildMenu(self, store): pref = prefHandler.prefHandler() sortFreq = {0:1, 1:2, 2:5} m = menuBuilder.menuBuilder.alloc().init() m.setMenu(self.dMenu) m.deleteMenu() try: m.buildRecentMenu(store) m.buildBundleMenu(store) m.buildTagMenu(store, pref.sortFreq, sortFreq[pref.useMin]) except Exception, e: print "buildMenu exception ->" print Exception, e def openurl(self, sender): cmd = "open '" + sender.toolTip() + "'" os.system(cmd) def htmlExp_(self): try: l = deliLoader.loader() store = l.getLocal() except Exception, e: print Exception, type(e) self.errorMsg("Data not found", "You must gater some valid data first") self.app.activateIgnoringOtherApps_(1) panel = NSSavePanel.savePanel() panel.setCanCreateDirectories_(1) panel.setMessage_("Select file:") res = panel.runModalForDirectory_file_("~/", "DeliciousLink.html") if res == 1: exportPath = panel.filename() try: deliExport.exporter(store, exportPath) except Exception, e: print Exception, type(e) self.errorMsg("Error", "Invalid directory") def mReload_(self, silent=0): pref = prefHandler.prefHandler() try: l = deliLoader.loader() if pref.proxy == "": proxy = None store = l.getNet(pref.username, pref.password, pref.proxy) self.buildMenu(store) NSLog("reload done") except Exception, e: if silent == 0: if hasattr(e, 'code'): if e.code == 401: self.errorMsg("Invalid login", "Wrong user/password, retry maybe better luck") elif e.code == 500: self.errorMsg("Del.icio.us server error", "Delicious server error, retry within few minute") else: print Exception, e self.errorMsg("Error", "Check user/password, or your internet connection") else: print Exception, e def checkPref(self): """ Check if the user has already set the pref, otherwise will open the pref panel """ pref = prefHandler.prefHandler() if pref.username != "": l = deliLoader.loader() store = l.getLocal() self.buildMenu(store) # setting timer self.setTimerReload(pref.timer) else: self.openPrefPanel_() def openPrefPanel_(self): """ open and prepare the preference panel """ pref = prefHandler.prefHandler() if pref.username != "" and pref.password != "": self.userField.setStringValue_(pref.username) self.pwdField.setStringValue_(pref.password) if pref.proxy != "": self.proxyField.setStringValue_(pref.proxy) ison = setOnStartup.isOnStartUp("delibar") if ison == 1: self.startButt.setState_(1) if pref.timer >= 0: self.timerField.setStringValue_(str(pref.timer)) self.minimumPost.selectItemAtIndex_(pref.useMin) self.tagSort.setState_(pref.sortFreq) self.app.activateIgnoringOtherApps_(1) self.prefWin.makeKeyAndOrderFront_(self) def prefOk_(self): pref = prefHandler.prefHandler() pref.username = self.userField.stringValue() pref.password = self.pwdField.stringValue() pref.proxy = self.proxyField.stringValue() pref.timer = int(self.timerField.stringValue()) pref.sortFreq = self.tagSort.state() pref.useMin = self.minimumPost.indexOfSelectedItem() pref.savePref() self.prefWin.close() # Setting reload timer self.setTimerReload(pref.timer) self.mReload_() def prefCanc_(self): self.prefWin.close() def errorMsg(self, title, msg): self.app.activateIgnoringOtherApps_(1) alert = NSAlert.alertWithMessageText_defaultButton_alternateButton_otherButton_informativeTextWithFormat_(title, None, None, None, msg) alert.runModal() def infoPanel_(self): self.app.activateIgnoringOtherApps_(1) self.app.orderFrontStandardAboutPanel(self) def openStart_(self, sender): if sender.state() == 0: setOnStartup.setAppOnStartUp("delibar", 0) else: setOnStartup.setAppOnStartUp("delibar", 1) def getSysProxy_(self, sender): proxy = getProxy.getProxy() if proxy is None: NSLog("No system proxy found") return else: self.proxyField.setStringValue_(proxy) def setTimerReload(self, minute): if minute > 0: minute = minute * 60 if self.reloadTimer is not None: self.reloadTimer.invalidate() self.reloadTimer = NSTimer.scheduledTimerWithTimeInterval_target_selector_userInfo_repeats_(minute, self, self.autoReload, None, 1) def autoReload(self, sender): try: self.mReload_(silent=1) except Exception: NSLog("Auto reload Failed, because ") print Exception def stepperControl_(self, sender): self.timerField.setStringValue_(sender.intValue()) def applicationWillFinishLaunching_(self, notification): self.plainMenu = 0 self.reloadTimer = None # Get global application self.app = NSApplication.sharedApplication() # Put Menu' into system bar statusbar = NSStatusBar.systemStatusBar() #self.baritem =statusbar.statusItemWithLength_(NSSquareStatusItemLength) self.baritem =statusbar.statusItemWithLength_(25.0) self.baritem.retain() #imageicon = NSImage.alloc().initByReferencingFile_("delicious.gif") imageicon = NSImage.alloc().initByReferencingFile_("deli.png") self.baritem.setImage_(imageicon) self.baritem.setHighlightMode_(1) self.baritem.setMenu_(self.dMenu) #runLoop = NSRunLoop.currentRunLoop() #myTimer = [[NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:@selector(onTimer:) userInfo:nil repeats:YES] retain]; #reloadTimer = NSTimer.scheduledTimerWithTimeInterval_target_selector_userInfo_repeats_(10, self, self.printa, None, 1) # Check'n set pref self.checkPref()