aboutsummaryrefslogtreecommitdiffstats
path: root/plasma/contents/code/main.py
blob: 45f7fec5275328f702312ed371ecafcdf72dcf4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# -*- coding: utf-8 -*-
# Net_monitor plasma interface
#
# Copyright, (C) Eugeni Dodonov <eugeni@mandriva.com>, 2011
#

from PyQt4.QtCore import Qt
from PyKDE4.plasma import Plasma
from PyKDE4 import plasmascript

# localization
import gettext
try:
    gettext.install("net_monitor")
except IOError:
    _ = str

from net_monitor import Monitor
 
class NetMonitor(plasmascript.Applet):
    def __init__(self,parent,args=None):
        plasmascript.Applet.__init__(self,parent)

        self.monitor = Monitor()
        self.ifaces = self.monitor.readnet()

        self.enabled_ifaces = []
        self.wireless_ifaces = filter(self.monitor.has_wireless, self.ifaces.keys())

        sorted_ifaces = self.ifaces.keys()
        sorted_ifaces.sort()

        net=self.monitor.readnet()

        for iface in sorted_ifaces:
            device_exists, data_in, data_out = self.monitor.get_traffic(iface,net)
            self.ifaces[iface] = {'data_in': 0,
                              'data_out': 0,
                              'total_in': 0,
                              'total_out': 0,
                              'graph': None,
                              'histogram': [],
                              'address': "",
                              }
            if self.monitor.has_network_accounting(iface):
                self.enabled_ifaces.append(iface)

        self.refresh_connections()

    def refresh_connections(self):
        """Updates connections"""
        for proto in ["tcp", "udp"]:
            connections = self.monitor.get_connections(proto=proto)
            for loc_addr, loc_port, rem_addr, rem_port, status in connections:
                print "%s - %s - %s - %s - %s - %s" % (proto, loc_addr, loc_port, rem_addr, rem_port, status)


    def init(self):
        self.setHasConfigurationInterface(False)
        self.resize(125, 125)
        self.setAspectRatioMode(Plasma.Square)

    def paintInterface(self, painter, option, rect):
        painter.save()
        painter.setPen(Qt.black)
        painter.drawText(rect, Qt.AlignVCenter | Qt.AlignHCenter, "Hello net_monitor!")
        painter.restore()
 
def CreateApplet(parent):
    return NetMonitor(parent)