def background_process(self, cmd, stdout=False, stderr=False, env=None): '''Add a new background process and begin running it.''' process = KProcess() #if env: # process.setEnv() process.setNextOpenMode(QIODevice.ReadOnly|QIODevice.Text) if stdout and stderr: process.setOutputChannelMode(KProcess.MergedChannels) else: if stdout: process.setOutputChannelMode(KProcess.OnlyStdoutChannel) elif stderr: process.setOutputChannelMode(KProcess.OnlyStderrChannel) process.setProgram(cmd) process.start() self.__process = process
class RedshiftApplet(plasmascript.Applet): def __init__(self, parent, args=None): plasmascript.Applet.__init__(self, parent) self.parent = parent def init(self): self.iconStopped = KIcon(ICON_STOPPED) self.iconRunning = KIcon(ICON_RUNNING) self.setHasConfigurationInterface(True) self.process = KProcess() self.dontStart = False self.restart = False # Set size of Plasmoid self.resize(100, 100) self.setMinimumSize(10, 10) self.setAspectRatioMode(Plasma.KeepAspectRatio) self.setBackgroundHints(Plasma.Applet.DefaultBackground) self.theme = Plasma.Svg(self) self.theme.setImagePath(THEME) self.button = Plasma.IconWidget(self.parent) self.button.setIcon(self.iconStopped) self.layout = QGraphicsGridLayout(self.applet) self.layout.setContentsMargins(0, 0, 0, 0) self.layout.addItem(self.button, 0, 0) # Set the tooltip self.tooltip = Plasma.ToolTipContent() self.tooltip.setMainText(i18n("Redshift")) self.tooltip.setSubText(i18n("Click to toggle it on")) self.tooltip.setImage(self.iconStopped) Plasma.ToolTipManager.self().setContent(self.applet, self.tooltip) # Connect signals and slots self.button.clicked.connect(self.toggle) self.appletDestroyed.connect(self.destroy) self.process.finished.connect(self.toggle) # Load configuration self.configChanged() # Set the default latitude and longitude values in the # configuration file, so that the config dialog can read them if not (self.latitude or self.longitude): self.latitude = KSystemTimeZones.local().latitude() self.longitude = KSystemTimeZones.local().longitude() cfgGeneral = self.config().group("General") cfgGeneral.writeEntry("latitude", self.latitude) cfgGeneral.writeEntry("longitude", self.longitude) # Autolaunch if self.autolaunch: print ("Autostarting Redshift") self.toggle() def configChanged(self): # Read the values from the configuration file, called automatically when configuration is changed cfgGeneral = self.config().group("General") self.latitude = cfgGeneral.readEntry("latitude", 0).toFloat()[0] self.longitude = cfgGeneral.readEntry("longitude", 0).toFloat()[0] self.nighttemp = cfgGeneral.readEntry("nightTemp", DEFAULT_NIGHT).toInt()[0] self.daytemp = cfgGeneral.readEntry("dayTemp", DEFAULT_DAY).toInt()[0] self.smooth = cfgGeneral.readEntry("smooth", True).toBool() self.autolaunch = cfgGeneral.readEntry("autolaunch", False).toBool() gammaR = cfgGeneral.readEntry("gammaR", 1.00).toFloat()[0] gammaG = cfgGeneral.readEntry("gammaG", 1.00).toFloat()[0] gammaB = cfgGeneral.readEntry("gammaB", 1.00).toFloat()[0] self.gamma = str("%.2f:%.2f:%.2f" % (gammaR, gammaG, gammaB)) self.restartRedshift() def toggleStatus(self): if self.button.icon().name() == self.iconRunning.name(): self.button.setIcon(self.iconStopped) self.tooltip.setImage(self.iconStopped) self.tooltip.setSubText(i18n("Click to toggle it on")) else: self.button.setIcon(self.iconRunning) self.tooltip.setImage(self.iconRunning) self.tooltip.setSubText(i18n("Click to toggle it off")) Plasma.ToolTipManager.self().setContent(self.applet, self.tooltip) def toggle(self): # Toggle Redshift on/off if self.process.state() == QProcess.Running: os.kill(int(self.process.pid()), signal.SIGUSR1) elif not self.dontStart: self.startRedshift() if not self.restart: self.toggleStatus() self.restart = False self.dontStart = False def startRedshift(self): """Start redshift if all conditions are satisfied Wait for any previous instance of Redshift launched by the applet to terminate, if redshift is launched outside the applet, notify the user and don't start Redshift """ self.process.waitForFinished() if not subprocess.call(["pidof", "redshift"]): KNotification.event( KNotification.Notification, i18n("Redshift"), i18n("Another instance of Redshift is running. It must be closed before you can use this applet."), KIcon(ICON_PLASMOID).pixmap(QSize(32, 32)), ) return print ( "Starting Redshift with latitude %.1f, longitude %.1f, day temperature %d, night temperature %d, gamma ramp %s, smooth transition = %s" % ( self.latitude, self.longitude, self.daytemp, self.nighttemp, self.gamma, ("yes" if self.smooth else "no"), ) ) self.process.setShellCommand( "%s -c /dev/null -l %.1f:%.1f -t %d:%d -g %s %s" % ( "redshift", self.latitude, self.longitude, self.daytemp, self.nighttemp, self.gamma, ("-r" if not self.smooth else ""), ) ) self.process.start() def restartRedshift(self): """Terminate the redshift process and eventually restart it If Redshift is not running no signal is emitted so toggle is not called If Redshift is running, it is terminated, a signal is emitted and toggle is called: if Redshift was not active the flag dontStart is setted and no new process is started if Redshift was active it is started """ print "Stopping Redshift" if self.process.state() == QProcess.Running: self.restart = True if self.button.icon().name() == self.iconStopped.name(): self.dontStart = True self.process.terminate() def destroy(self): # Kill redshift and clear any gamma correction self.process.kill() self.process.waitForFinished() subprocess.call(["redshift", "-c", "/dev/null", "-x"])
class FortuneCookie(plasmascript.Applet): # The all important label : This is the only control in our plasmoid label = Plasma.Label mytimer = QTimer process = KProcess fortunecommand="fortune" fortunecookies="fortune-ml" offensive="-o" #Every half an hour mytimeout = 1800000 #process = def __init__(self,parent,args=None): plasmascript.Applet.__init__(self,parent) def init(self): #There is no configuration dialogue for this applet self.setHasConfigurationInterface(False) #This applet will always retain its Initial aspect ratio self.setAspectRatioMode(Plasma.KeepAspectRatio) #Get the current theme and use the default background ("widgets/background") and background hints self.theme = Plasma.Svg(self) self.theme.setImagePath("widgets/background") self.setBackgroundHints(Plasma.Applet.DefaultBackground) #The applet layout is horizontal self.layout = QGraphicsLinearLayout(Qt.Vertical, self.applet) #Create a new label self.label = Plasma.Label(self.applet) # Add the label to the layout so it will be displayed self.layout.addItem(self.label) self.setLayout(self.layout) # setup the process self.process=KProcess(self) self.process.setShellCommand(self.fortunecommand + " " + self.fortunecookies) self.process.setOutputChannelMode(KProcess.MergedChannels) # When the command outputs something , get it QObject.connect( self.process, SIGNAL("readyReadStandardOutput()"), self.gotsomeoutput ); #setup timer self.mytimer=QTimer(self) # We don't want this to timeout all by itself.. self.mytimer.setSingleShot(True) # When the timer times out, execute this QObject.connect(self.mytimer,SIGNAL("timeout()"), self.TimeOut) # start the timer self.process.start() self.mytimer.start(self.mytimeout) # Set the default applet size self.resize(180,130) def gotsomeoutput(self): outputstring = str(self.process.readAllStandardOutput()) self.label.setText(unicode(outputstring,"utf-8")) self.mytimer.start(self.mytimeout) def TimeOut(self): # execute the fortune command and get its output if self.process.state() == QProcess.NotRunning: self.process.start() else: return # On mouse click , change the quote def mousePressEvent(self, event): print "Test" self.mytimer.start(0)