ExportDialog.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 » ExportDialog.py
# -*- coding: utf-8 -*-

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

from qt import *
import os
import os.path
import dsml
import StringIO
import ldap

from base.utils.gui.ExportDialogDesign import ExportDialogDesign
import environment
from base.utils import getSortedDnList,stripSpecialChars,explodeDN
from base.backend.LumaConnection import LumaConnection


class ExportDialog(ExportDialogDesign):

    def __init__(self,parent = None,name = None,modal = 0,fl = 0):
        ExportDialogDesign.__init__(self,parent,name,modal,fl)
        
        self.iconPath = os.path.join(environment.lumaInstallationPrefix, "share", "luma", "icons")
        self.exportIcon = QPixmap(os.path.join(self.iconPath, "export_big.png"))
        self.iconLabel.setPixmap(self.exportIcon)
        folderPixmap = QPixmap(os.path.join(self.iconPath, "folder.png"))
        self.fileButton.setPixmap(folderPixmap)
        
        self.fileLabel.setText("")
        self.resultLabel.setText("")
        
        self.okIcon = QPixmap(os.path.join(self.iconPath, "ok.png"))
        self.failureIcon = QPixmap(os.path.join(self.iconPath, "no.png"))
        
        self.itemView.setColumnText(0, "")
        self.itemView.setColumnWidth(0, 32)
        self.itemView.setSorting(-1, False) 
        
        self.startButton.setEnabled(False)
        
        # Dictionary of items which should be exported.
        # Key is the prettyDN; value is as list with the normal dn 
        # and the QListViewItem
        self.exportDictionary = {}
        
        # Filename where to export the entries
        self.fileName = None
        
###############################################################################

    def initData(self, itemList):
        """ Initializaion of the dialog with data.
        """
        
        itemList.sort()
        
        for x in itemList[::-1]:
            prettyDN = x.getPrettyDN()
            
            tmpItem = QListViewItem(self.itemView)
            tmpItem.setText(1, prettyDN)
            self.exportDictionary[prettyDN] = [x, tmpItem]
            
###############################################################################

    def showFileDialog(self):
        tmpFileName = QFileDialog.getSaveFileName(\
                        QString.null,
                        "LDIF Files (*.ldif);; DSML Files (*.dsml);; All files (*)",
                        None, None,
                        self.trUtf8("Select file for exporting"),
                        None, 1)

                            
        self.fileName = unicode(tmpFileName).strip()
        self.fileEdit.setText(self.fileName)
        
###############################################################################

    def updateFileName(self, tmpString):
        self.fileName = unicode(tmpString).strip()
        
        enable = True
        
        # Check the given filename
        self.fileLabel.setText("")
        if os.path.isdir(self.fileName):
            self.fileLabel.setText(self.trUtf8("Given file is a directory. Please check the filename."))
            enable = False
        else:
            try:
                if os.path.isfile(self.fileName) or os.path.islink(self.fileName):
                    open(self.fileName, "r")
                else:
                    fileHandler = open(self.fileName, "w")
                    fileHandler.close()
                    os.remove(self.fileName)
            except IOError, e:
                self.fileLabel.setText(self.trUtf8("Can't open file. Please check file system permissions."))
                enable = False
                
        if enable:
            self.fileEdit.unsetPalette()
        else:
            self.fileEdit.setPaletteBackgroundColor(Qt.red)
        
        self.startButton.setEnabled(enable)
        
###############################################################################

    def removeItems(self):
        """ Remove the currently selected items from the list of items to 
        be exported.
        """
        
        selectedList = []
        
        listIterator = QListViewItemIterator(self.itemView)
        while listIterator.current():
            item = listIterator.current()
            if item.isSelected():
                selectedList.append(item)
            listIterator += 1
        
        for x in selectedList:
            name = unicode(x.text(1))
            self.itemView.takeItem(self.exportDictionary[name][1])
            del self.exportDictionary[name]

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

    def exportItems(self):
        environment.setBusy(True)
        
        itemList = map(lambda x: self.exportDictionary[x][0], self.exportDictionary.keys())
        itemList.sort()
        
        allExported = True
        
        try:
            fileHandler = open(self.fileName, "w")
                
            format = str(self.formatBox.currentText())
            
            if format == "DSML":
                tmpString = StringIO.StringIO()
                dsmlWriter = dsml.DSMLWriter(tmpString)
                dsmlWriter.writeHeader()
                fileHandler.write(tmpString.getvalue())
            
            for x in itemList:
                try:
                    if format == "LDIF":
                        fileHandler.write(x.convertToLdif())
                    elif format == "DSML":
                        fileHandler.write(x.convertToDsml())
            
                    self.displayItemStatus(self.exportDictionary[x.getPrettyDN()][1], True, None)
                except IOError, e:
                    self.displayItemStatus(self.exportDictionary[x.getPrettyDN()][1], False, None)
                    allExported = False
            
            if format == "DSML": 
                tmpString = StringIO.StringIO()
                dsmlWriter = dsml.DSMLWriter(tmpString)
                dsmlWriter.writeFooter()
                fileHandler.write(tmpString.getvalue())
            
            fileHandler.close()
            
            if not allExported:
                self.resultLabel.setText(self.trUtf8("Could not export all entires. Please check messages."))
            else:
                self.resultLabel.setText(self.trUtf8("All items exported successfully."))
                
        except IOError, e:
            self.resultLabel.setText(self.trUtf8("Can't open file. Please check file system permissions."))

        environment.setBusy(False)
        
        if allExported:
            self.accept()
        
        

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

    def displayItemStatus(self, listItem, success, exceptionObject):
        if success:
            listItem.setPixmap(0, self.okIcon)
            listItem.setText(2, self.trUtf8("Item exported successfully."))
        else:
            listItem.setPixmap(0, self.failureIcon)
            listItem.setText(2, str(exceptionObject))
            
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.