#!/usr/bin/env python2

import sys, os
from glob import glob
from re import sub
from qt import *

def QMIN(x, y):
	if y > x:
		return y
	#if y > x
	return x
#def QMIN(x, y)

class IceIconView(QIconView):
	def __init__(self, parent = None, name = None):
		QIconView.__init__(self, parent, name)
	#def __init__(self, parent = None, name = None)

	def dragObject(self):
		return QTextDrag(str(self.currentItem().text()), self)
	#def dragObject(self)

#class IceIconView(QIconView)

class IceIconCvt(QWidget):
	def __init__(self,parent = None,name = None):
		QWidget.__init__(self,parent,name)

		if name == None:
			self.setName("IceIconCvt")

		self.setCaption(self.trUtf8("IceWM's Icons' Convertor"))
		self.resize(600, 400)
		topLayout = QVBoxLayout(self,0,6,"topLayout")

		hLayout1 = QHBox(self, "hLayout1")
		topLayout.addWidget(hLayout1)
		
		self.TextLabel2 = QLabel(self.trUtf8("Browsed icons:"), hLayout1, "TextLabel2")

		self.edIconDir = QLineEdit(hLayout1, "edIconDir")
		QToolTip.add(self.edIconDir,self.trUtf8("Browsed directory"))

		self.pbBrowse = QPushButton(self.trUtf8("..."), hLayout1,"pbBrowse")
		QToolTip.add(self.pbBrowse,self.trUtf8("Press this key for browsing directorys with icons"))
		
		hLayout2 = QHBox(self, "hLayout2")
		topLayout.addWidget(hLayout2)
		
		self.ivIcons = IceIconView(hLayout2,"ivIcons")
		self.ivIcons.setSorting(1)
		QToolTip.add(self.ivIcons,self.trUtf8("Availabel icons"))

		vLayout1 = QVBox(hLayout2, "vLayout1")

		self.btReload = QPushButton(self.trUtf8("Reload"), vLayout1,"btReload")
		QToolTip.add(self.btReload,self.trUtf8("Reloads the icons in browsed directory"))

		self.btCopy = QPushButton(vLayout1,"btCopy")
		self.btCopy.setText(self.trUtf8("Copy"))
		QToolTip.add(self.btCopy,self.trUtf8("Copys selected icon into IceWM's icons' directory"))

		self.TextLabel9 = QLabel(vLayout1,"TextLabel9")
		self.TextLabel9.setText(self.trUtf8(" Press \'Copy\' button \n"
											" or drag-and-drop image \n"
											" from browsed dir to IceWM's dir \n"
											" to convert and copy \n"
											" selected icon into \n"
											" IceWM's icons' dir. "))
		self.TextLabel9.setAlignment(QLabel.AlignCenter)

		self.TextLabel3 = QLabel(self.trUtf8("IceWM's icons:"), self,"TextLabel3")
		self.TextLabel3.setAlignment(QLabel.AlignCenter)
		topLayout.addWidget(self.TextLabel3)

		hLayout3 = QHBox(self, "hLayout3")
		topLayout.addWidget(hLayout3)

		self.ivIceIcons = QIconView(hLayout3,"ivIceIcons")
		self.ivIceIcons.setSorting(1)
		QToolTip.add(self.ivIceIcons,self.trUtf8("IceWM's icons"))

		vLayout2 = QVBox(hLayout3, "vLayout2")

		self.pbDel = QPushButton(self.trUtf8("Delete"), vLayout2,"pbDel")
		QToolTip.add(self.pbDel,self.trUtf8("Deletes the selected IceWM's icon"))

		self.pbExit = QPushButton(self.trUtf8("Exit"), vLayout2,"pbExit")
		self.TextLabel8 = QLabel(vLayout2,"TextLabel8")
		self.TextLabel8.setFrameShadow(QLabel.Raised)
		self.TextLabel8.setText(self.trUtf8("IceWM's Icons'\n"
											"Convertor 1.0\n"
											"Copyright (c) 2003\n"
											"by Vadim A. Khohlov"))
		self.TextLabel8.setAlignment(QLabel.AlignCenter)
		self.TextLabel8.setFrameStyle(QFrame.StyledPanel | QFrame.Raised)

		self.connect(self.pbBrowse,SIGNAL("clicked()"),self.slotBrowse)
		self.connect(self.ivIceIcons, SIGNAL("dropped(QDropEvent*,"
				"const QValueList<QIconDragItem>&)"), self.slotDropEvent)
		self.connect(self.btReload,SIGNAL("clicked()"),self.slotReload)
		self.connect(self.btCopy,SIGNAL("clicked()"),self.slotCopy)
		self.connect(self.pbExit,SIGNAL("clicked()"),self.close)
		self.connect(self.pbDel,SIGNAL("clicked()"),self.slotDel)

		self.iconDirName = ""
		
		if os.getuid() == 0:
			res = QMessageBox.information(self, self.trUtf8("Information"),
				self.trUtf8("You are root.\n"
				"Press \'Ok\' button for working with global IceWM's icon's dir\n"
				"and \'Cancel\' button for working with your private IceWM's dir."),
				QMessageBox.Ok, QMessageBox.Cancel)
		#if os.getuid() == 0
		else:
			res = 2		
		#else
		
		if res == 1:
			#You should correct this dir on your IceWM's global dir.
			self.iceIconDir = "/usr/local/share/icewm/icons/"
		#if res
		else:
			if os.environ.has_key("ICEWM_CFGDIR") == 1:
				self.iceIconDir = os.environ["ICEWM_CFGDIR"] + "/icons/"
			else:
				self.iceIconDir = os.environ["HOME"] + "/.icewm/icons/"
			#else if os.environ.has_key("ICEWM_CFGDIR") == 1
		#else if res == 1
		
		self.loadIceIcons()

	def convertIcon(self, iName):
		iiName = sub("\..*", "", str(iName))
		sname = QInputDialog.getText(self.trUtf8("IceWM's Icons' Convertor"),
				self.trUtf8("Enter icon name without size and extention:"),
				QLineEdit.Normal, iiName)
		if sname[1] == 0:
			return
		#if sname[1] == 0
		iconName = str(self.iconDirName) + str(iName)
		
		img = QImage(iconName)

		iceImg = img.scale(32, 32)
		iconName = str(self.iceIconDir) + str(sname[0]) + "_32x32.xpm"
		iceImg.save(iconName, "XPM")
		iceImg = img.scale(16, 16)
		iconName = str(self.iceIconDir) + str(sname[0]) + "_16x16.xpm"
		iceImg.save(iconName, "XPM")

		self.loadIceIcons()
	#def convertIcon(self, iName)

	def loadIceIcons(self):
		self.ivIceIcons.clear()
		for file in glob(self.iceIconDir + "*_32x32.xpm"):
			iconName = sub("^.*/", "", file)
			iconName = sub('_32x32\.xpm$', "", iconName)
			QIconViewItem(self.ivIceIcons, iconName, QPixmap(file))
		#for file in glob(self.iceIconDir + "*_32x32.xpm")
	#def loadIceIcons(self)

	def loadIcons(self, dirName, iconView):
		iconView.clear()
		for file in os.listdir(str(dirName)):
			icon = QPixmap(str(dirName) + str(file))
			if icon.isNull() != 1:
				QIconViewItem(iconView, file, icon)
			#if !icon.siNull()
		#for file in os.listdir("%s" % dirName)
	#def loadIcons(self, dir, iconView)

	def slotBrowse(self):
		dirName = QFileDialog.getExistingDirectory(QString.null, self,
			"get existing dir", "Choose a directory", 0)
		if dirName.isEmpty():
			return
		#if dirName != ""
		self.iconDirName = dirName
		self.edIconDir.setText(dirName)
		self.loadIcons(dirName, self.ivIcons)
	#def slotBrowse(self):
	
	def slotReload(self):
		if self.iconDirName != "":
			self.loadIcons(self.iconDirName, self.ivIcons)
		#if self.iconDirName != ""
	#def slotReload(self):
	
	def slotCopy(self):
		curIcon = self.ivIcons.currentItem()
		if curIcon == None:
			QMessageBox.warning(self, self.trUtf8("Error!"),
				self.trUtf8("You should select an icon"),
				QMessageBox.Ok, QMessageBox.NoButton)
			return
		#if curIcon == None
		self.convertIcon(curIcon.text())
	#def slotCopy(self):

	def slotDel(self):
		curIcon = self.ivIceIcons.currentItem()
		if curIcon == None:
			QMessageBox.warning(self, self.trUtf8("Error!"),
				self.trUtf8("You should select an icon"),
				QMessageBox.Ok, QMessageBox.NoButton)
			return
		#if curIcon == None
		iconName = str(self.iceIconDir) + str(curIcon.text())
		os.remove(iconName + "_32x32.xpm")
		os.remove(iconName + "_16x16.xpm")
		self.loadIceIcons()
	#def slotDel(self):

	def slotDropEvent(self, dropEvt, lst):
		iconName = QString()
		if QTextDrag.decode(dropEvt, iconName):
			self.convertIcon(iconName)
		#if QtextDrag.decode(dropEvt, iconName) == 1
	#def slotDropEvent(self, QDropEvent evt, QValueList lst)

a = QApplication(sys.argv)

iceIconCvtApp = IceIconCvt()
a.setMainWidget(iceIconCvtApp)
iceIconCvtApp.show()
a.exec_loop()
