def openComposer(to='', cc='', bcc='', subject='', message=''): interfaces = [('kmail', 'default'), ('kontact', 'KMailIface')] client = DCOPClient() client.attach() done = False for app, part in interfaces: obj = DCOPObj(app, client, part) try: obj.openComposer(to, cc, bcc, qstr(subject), qstr(message), False, KURL()) done = True break except TypeError: pass return done
def getLocalWallpaper(): "Returns local wallpaper using dcop" # Create a dcop object: client = DCOPClient() if not client.attach(): return None # Get Wallpaper: background = DCOPObj("kdesktop", client, "KBackgroundIface") ok, wallpaper = background.currentWallpaper(0) if ok: return wallpaper else: return None
def ConnectKMail(): # Run KMail: if not os.system("kmail") == 0: raise Exception, "KMail cannot be started" # Create a dcop object: client = DCOPClient() if not client.attach(): raise Exception, "Cannot connected to KMail" # Keep this window on top: kmail = DCOPObj("kmail", client, "kmail-mainwindow#1") kmail.lower() # Return KMailIface kmail = DCOPObj("kmail", client, "KMailIface") return kmail
def setWallpaper(path): "Changes current wallpaper with the new one" # Copy file to wallpapers dir: wallpapersdir = os.path.expanduser("~/.kde/share/wallpapers") if not (os.path.isdir(wallpapersdir)): os.makedirs(wallpapersdir) newpath = os.path.join(wallpapersdir, os.path.basename(path)) shutil.copyfile(path, newpath) # Create a dcop object: client = DCOPClient() if not client.attach(): raise WallpaperError, "Wallpaper cannot be changed" # Set Wallpaper: background = DCOPObj("kdesktop", client, "KBackgroundIface") ok, wallpaper = background.setWallpaper(QString(unicode(newpath)), 6) # 6: Scaled if not ok: raise WallpaperError, "Wallpaper cannot be changed"
def addMessage(folder, message, kmail=None): "Adds a message to kmail with dcop interface" if not kmail: # Create a dcop object: client = DCOPClient() if not client.attach(): raise Exception, "Message cannot be added" kmail = DCOPObj("kmail", client, "KMailIface") # Add Message: ok, status = kmail.dcopAddMessage(QString(folder), message, "") if not ok: raise DCOPError, "Can not connect to kmail with DCOP" elif status == -4: raise DuplicateMessage, "Message in %s cannot be added: duplicate message" % folder elif status == -2: raise MailError, "Message in %s cannot be added: cannot add message to folder" % folder elif status == -1: raise MailError, "Message in %s cannot be added: cannot make folder" % folder elif status == 0: raise MailError, "Message in %s cannot be added: error while adding message" % folder elif status != 1: raise MailError, "Message in %s cannot be added, status: %d" % (folder, status) else: return True
class autoEqualizer( QApplication): """ The main application, also sets up the Qt event loop """ def __init__( self, args, mode ): QApplication.__init__( self, args ) # create a new DCOP-Client self.client = DCOPClient() # Select what mode we want to run in # 1 => shortStatusMessage # 2 => popupMessage self.mode = mode # connect the client to the local DCOP-Server if self.client.attach() is not True: os.popen( "kdialog --sorry 'Could not connect to local DCOP server. Something weird happened.'" ) sys.exit(1) # create a DCOP-Application-Object to talk to Amarok self.amarok = DCOPApp('amarok', self.client) debug( "Started.\n" ) # Start separate thread for reading data from stdin self.stdinReader = threading.Thread( target = self.readStdin ) self.stdinReader.start() self.readSettings() def osCommands(self, command): # Execute the command and return the exit value # Once the extraction problem is root-caused, we can fix this easily. os.environ['__autoEqualizer_command'] = command try: old_environ = os.environ['LANG'] except KeyError: old_environ = "C" os.environ['LANG'] = "C" #if os.system(command '> __autoEqualizer_output 2>&1') != 0: # debug("Couldn't execute the command using the dcopy command interface also.") def saveState(self, sessionmanager): # script is started by amarok, not by KDE's session manager debug("We're in saveState. We should be avoiding session starts with this in place.\n") sessionmanager.setRestartHint(QSessionManager.RestartNever) def readSettings( self ): """ Reads settings from configuration file """ try: foovar = config.get( "General", "foo" ) except: debug( "No config file found, using defaults.\n" ) ############################################################################ # Stdin-Reader Thread ############################################################################ def readStdin( self ): """ Reads incoming notifications from stdin """ while True: # Read data from stdin. Will block until data arrives. line = sys.stdin.readline() debug ("Line is %s.\n" % (line) ) if line: qApp.postEvent( self, Notification(line) ) else: break ############################################################################ # Notification Handling ############################################################################ def customEvent( self, notification ): """ Handles notifications """ string = QString(notification.string) debug( "Received notification: " + str( string ) + "\n" ) if string.contains( "configure" ): self.configure() if string.contains( "engineStateChange: play" ): debug("Play event triggered.\n") self.engineStatePlay() if string.contains( "engineStateChange: idle" ): self.engineStateIdle() if string.contains( "engineStateChange: pause" ): self.engineStatePause() if string.contains( "engineStateChange: empty" ): self.engineStatePause() if string.contains( "trackChange" ): debug("Track change event occured.\n") self.trackChange() # Notification callbacks. Implement these functions to react to specific notification # events from Amarok: def configure( self ): debug( "configuration" ) self.dia = ConfigDialog() self.dia.show() self.connect( self.dia, SIGNAL( "destroyed()" ), self.readSettings ) def engineStatePlay( self ): """ Called when Engine state changes to Play """ debug ("Enable the equalizer to On") self.equalizerState() def engineStateIdle( self ): """ Called when Engine state changes to Idle """ pass def engineStatePause( self ): """ Called when Engine state changes to Pause """ pass def engineStateEmpty( self ): """ Called when Engine state changes to Empty """ pass def trackChange( self ): """ Called when a new track starts """ debug ("Track Change event called.\n") self.setEqualizer() def getGenre(self): # get the Genre from the current song. retval, genre = self.amarok.player.genre() genre = genre.__str__() if retval is not True: debug("I couldn't get the genre using the library. Is Amarok running?") #TODO: debug("Will try using the dcop commandline interface") else: return genre def setEqualizer(self): # set the equalizer accordingly # TODO: It would be good to have a list of preset equalizers # and match them self.genre = self.getGenre() retval, success = self.amarok.player.setEqualizerPreset(self.genre) if retval is not True: debug("I couldn't get the equalizer preset. Is Amarok running?") else: if self.mode == 1: self.amarok.playlist.shortStatusMessage("Activated equalizer preset -> %s" % (self.genre) ) debug ("Activated equalizer preset -> %s\n" % (self.genre) ) elif self.mode == 2: self.amarok.playlist.popupMessage("Activated equalizer preset -> %s" % (self.genre) ) debug ("Activated equalizer preset -> %s\n" % (self.genre) ) else: # Default Mode self.amarok.playlist.popupMessage("Activated equalizer preset -> %s" % (self.genre) ) debug ("Activated equalizer preset -> %s\n" % (self.genre) ) def equalizerState(self): # check if the equalizer is on or not # FIXME: Currently, it looks like dcopext has a bug # even though I try to set the equalizer to on, it doesn't # so for now we will check if the equalizer is on or not and # enable it using the dcop command retval, equalizerState = self.amarok.player.equalizerEnabled() if not equalizerState: os.system( "dcop amarok player setEqualizerEnabled True" ) debug("Enable the Equalizer.")