Example #1
0
File: ui.py Project: Vendar/hostman
class HostListModel(QtCore.QAbstractListModel):
    UserRole_HostEntry = QtCore.Qt.UserRole+1
    def __init__(self,parent=None):
        super(HostListModel,self).__init__(parent)
        if platform.system() == "Windows":
            self.hostfile = HostFile("C:/WINDOWS/system32/drivers/etc/hosts")
        else:
            self.hostfile = HostFile("/etc/hosts")
        
    def rowCount(self,parent=QtCore.QModelIndex()):
        return len(self.hostfile.entries)
 
    def data(self,index,role=QtCore.Qt.DisplayRole):
        if not index.isValid() or index.row()>=len(self.hostfile.entries):
            return QtCore.QVariant()
        if role == QtCore.Qt.DisplayRole:
            return self.hostfile.entries.values()[index.row()].toexpression()
        elif role == QtCore.Qt.ForegroundRole:
            if self.hostfile.entries.values()[index.row()].enabled:
                return QtGui.QColor(0,0,0)
            else:
                return QtGui.QColor(255,0,0)
        elif role == QtCore.Qt.ToolTipRole:
            if self.hostfile.entries.values()[index.row()].enabled:
                return "Enabled"
            else:
                return "Disabled"
        elif role == HostListModel.UserRole_HostEntry:
            return self.hostfile.entries.values()[index.row()]
        return QtCore.QVariant()

    def switch_entry(self,idx):
        if idx.row()>=0 and idx.row()<len(self.hostfile.entries):
            self.hostfile.entries.values()[idx.row()].switch()
            self.hostfile.save()
            self.dataChanged.emit(idx,idx)
Example #2
0
File: ui.py Project: Vendar/hostman
 def __init__(self,parent=None):
     super(HostListModel,self).__init__(parent)
     if platform.system() == "Windows":
         self.hostfile = HostFile("C:/WINDOWS/system32/drivers/etc/hosts")
     else:
         self.hostfile = HostFile("/etc/hosts")