SearchResultView.py :  » Network » Luma » luma-2.4 » lib » luma » base » utils » gui » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Network » Luma 
Luma » luma 2.4 » lib » luma » base » utils » gui » SearchResultView.py
# -*- coding: utf-8 -*-

###########################################################################
#    Copyright (C) 2003 by Wido Depping
#    <widod@users.sourceforge.net>
#
# Copyright: See COPYING file that comes with this distribution
#
###########################################################################

import ldap
from qt import *
import base64
import re
import os.path

from base.utils.gui.SearchResultViewDesign import SearchResultViewDesign
from base.utils.gui.AdvancedObjectWidget import AdvancedObjectWidget
from base.backend.ServerList import ServerList
import environment
from base.utils import isBinaryAttribute,encodeBase64
from base.backend.LumaConnection import LumaConnection
from base.utils import escapeSpecialChars
from base.utils import explodeDN
from base.utils import getSortedDnList
from base.utils.gui.LumaErrorDialog import LumaErrorDialog
from base.utils.gui.DeleteDialog import DeleteDialog
from base.utils.gui.ExportDialog import ExportDialog
from base.utils import getSortedDnList




class SearchResultView(SearchResultViewDesign):

    def __init__(self, parent=None, name=None, fl=0):
        SearchResultViewDesign.__init__(self,parent,name,fl)

        self.SERVER = None
        self.RESULT = {}
        self.childWidgets = []
        self.childsToClean = []
        self.FILTER_COLUMN_POS = {}
        self.FILTER_LIST = []
        
        self.iconPath = os.path.join(environment.lumaInstallationPrefix, "share", "luma", "icons")
        delIconFile = os.path.join(self.iconPath, "deleteEntry.png")
        exportIconFile = os.path.join(self.iconPath, "exportLdif.png")
        
        self.popupMenu = QPopupMenu()
        self.popupMenu.insertItem(QIconSet(QPixmap(exportIconFile)), self.trUtf8("Export selected"), self.exportItems)
        self.popupMenu.insertSeparator()
        self.popupMenu.insertItem(QIconSet(QPixmap(delIconFile)), self.trUtf8("Delete selected"), self.deleteItems)
        
        self.connect(self.resultListView, SIGNAL("rightButtonPressed(QListViewItem*, const QPoint&, int)"), self.showPopup)
        
        self.resultListView.setSorting(-1, False) 

###############################################################################

    def showEntry(self, listItem):
        """ Show the double-clicked listitem in an own window.
        """
        
        normalDN = unicode(listItem.text(0)).encode('utf-8')
        tmpList = explodeDN(normalDN)
        newList = map(escapeSpecialChars, tmpList)
        normalDN = ",".join(newList)
        
        dataObject = self.RESULT[normalDN]
        serverMeta = dataObject.getServerMeta()
        connection = LumaConnection(serverMeta)
        
        bindSuccess, exceptionObject = connection.bind()
        
        if not bindSuccess:
                dialog = LumaErrorDialog()
                errorMsg = self.trUtf8("Could not bind to server.<br><br>Reason: ")
                errorMsg.append(str(exceptionObject))
                dialog.setErrorMessage(errorMsg)
                dialog.exec_loop()
                return
                
        success, resultList, exceptionObject = connection.search(dataObject.getDN(), ldap.SCOPE_BASE)
        connection.unbind()
        
        if success and (len(resultList) > 0):
            floatingWidget = ChildWindow(None, normalDN)
            widget = AdvancedObjectWidget(floatingWidget, normalDN, 0)
    
            floatingWidget.setCentralWidget(widget)
            widget.buildToolBar(floatingWidget)
        
            widget.initView(resultList[0])
        
            # needed if window is closed. gets deletetd from the list
            self.connect(floatingWidget, PYSIGNAL("child_closed"), self.cleanChildren)
        
            widget.setCaption(listItem.text(0))
            floatingWidget.resize(500, 400)
            floatingWidget.show()
        
            # don't loose reference. normally window will disappear if function is completed
            self.childWidgets.append(floatingWidget)
            
        # Couldn't load selected item
        else:
            dialog = LumaErrorDialog()
            errorMsg = self.trUtf8("Could not access entry.<br><br>Reason: ")
            errorMsg.append(str(exceptionObject))
            dialog.setErrorMessage(errorMsg)
            dialog.exec_loop()
        
###############################################################################

    def cleanChildren(self, child):
        """ Delete window of closed ldap item.
        """
        
        index = self.childWidgets.index(child)
        del self.childWidgets[index]

###############################################################################

    def setResult(self, serverMeta=None, resultData=None, filterList=None):
        self.serverMeta = serverMeta
        self.SERVER = serverMeta.name
        self.rawResultList = resultData
        self.processData(resultData)
        self.FILTER_LIST = filterList
        self.displayResults()

###############################################################################

    def processData(self, data):
        self.RESULT = {}
        if not(data == None):
            for x in data:
                self.RESULT[x.getDN()] = x

###############################################################################

    def displayResults(self):
        self.resultListView.clear()
        
        while self.resultListView.columns():
            self.resultListView.removeColumn(0)
            
        self.FILTER_COLUMN_POS = {}
        position = self.resultListView.addColumn("dn")
        self.FILTER_COLUMN_POS["dn"] = position
        
        for x in self.FILTER_LIST:
            position = self.resultListView.addColumn(x)
            self.FILTER_COLUMN_POS[x] = position
            
        tmpList = self.RESULT.keys()
        tmpList = getSortedDnList(tmpList)
        for x in tmpList[::-1]:
            dataObject = self.RESULT[x]
            listItem = QListViewItem(self.resultListView, dataObject.getDN())
            listItem.setText(0, dataObject.getPrettyDN())
            
            for x in self.FILTER_COLUMN_POS.keys():
                if x == 'dn':
                    continue
                    
                if x.lower() == "objectclass":
                    valueList = dataObject.getObjectClasses()
                    listItem.setText(self.FILTER_COLUMN_POS[x], ",".join(valueList))
                        
                if dataObject.hasAttribute(x):
                    if dataObject.isAttributeBinary(x):
                        listItem.setText(self.FILTER_COLUMN_POS[x], self.trUtf8("<u>Binary value</u>"))
                    else:
                        valueList = dataObject.getAttributeValueList(x)
                        listItem.setText(self.FILTER_COLUMN_POS[x], ",".join(valueList))
            
            self.resultListView.insertItem(listItem)
            
        self.resultListView.setColumnWidth(0, 250)
        self.resultListView.triggerUpdate()


###############################################################################

    def eventFilter(self, object, event):
        if (event.type() == QEvent.Close):
            self.childsToClean.append(object)
            
        return 0

###############################################################################

    def deleteItems(self):
        itemList = self.getSelectedItems()
        
        dnList = []
        itemDict = {}
        for x in itemList:
            # create the real string of the dn.
            tmpString = unicode(x.text(0)).encode('utf-8')
            tmpList = explodeDN(tmpString)
            newList = map(escapeSpecialChars, tmpList)
            tmpString = ",".join(newList)
            
            dnList.append(tmpString)
            itemDict[tmpString] = x
            
            
        deleteList = map(lambda x: self.RESULT[x], dnList)
        deleteDialog = DeleteDialog()
        deleteDialog.initData(deleteList)
        deleteDialog.exec_loop()
        
        realDeletedEntries = deleteDialog.getDeletedItems()
        
        for x in realDeletedEntries:
            self.resultListView.takeItem(itemDict[x])
            del self.RESULT[x]

###############################################################################
    
    def showPopup(self, tmpItem=None, point=None, itemId=None):
        if not (tmpItem == None):
            self.popupMenu.exec_loop(point)

###############################################################################

    def exportItems(self):
        itemList = self.getSelectedItems()
        
        # create list of encoded dns
        dnList = []
        for x in itemList:
            # create the real string of the dn.
            normalDN = unicode(x.text(0)).encode('utf-8')
            tmpList = explodeDN(normalDN)
            newList = map(escapeSpecialChars, tmpList)
            normalDN = ",".join(newList)
            
            dnList.append(normalDN)
         
        exportList = map(lambda x: self.RESULT[x], dnList)
        dialog = ExportDialog()
        dialog.initData(exportList)
        dialog.exec_loop()
        
###############################################################################

    def getSelectedItems(self):
        tmpList = []
        
        child = self.resultListView.firstChild()
        if child.isSelected():
                tmpList.append(child)
        
        while child.nextSibling():
            child = child.nextSibling()
            if child.isSelected():
                tmpList.append(child)
                
        return tmpList
    
###############################################################################

class ChildWindow(QMainWindow):
    
    def __init__(self, parent = None, name= None):
        QMainWindow.__init__(self, parent, name)
        
        
    def closeEvent(self, event):
        self.emit(PYSIGNAL("child_closed"), (self,))
        self.deleteLater()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.