#!/usr/bin/python
# -*- coding: utf-8 -*-

# --------------------------------------------------------------------------- #
#                                                                             #
#    Password Generator                                                       #
#    Copyright (C) diSabler <dsy@dsy.name>                                    #
#                                                                             #
#    This program is free software: you can redistribute it and/or modify     #
#    it under the terms of the GNU General Public License as published by     #
#    the Free Software Foundation, either version 3 of the License, or        #
#    (at your option) any later version.                                      #
#                                                                             #
#    This program is distributed in the hope that it will be useful,          #
#    but WITHOUT ANY WARRANTY; without even the implied warranty of           #
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            #
#    GNU General Public License for more details.                             #
#                                                                             #
#    You should have received a copy of the GNU General Public License        #
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.    #
#                                                                             #
# --------------------------------------------------------------------------- #

# execute in npp: cmd /K python "$(FULL_CURRENT_PATH)"

from PyQt4 import QtGui, QtCore, Qt
import random
import sys
import base64

# Base64 data
app_logo ='''
iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAB
JAAAASQBIAXRoQAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAAAMqSURBVFiFxZdN
aFNLGIafyUlI2vyo2NQ2jUqyKElpiy5U1IV/6b0Lae3ShbgQCxfusjst0oXgQgIiLpTaxp0LhVJ0ZZS6
UexG7b2lEAQxoYE0SUWx8Z7DPT1x0bT+NKednP74wsDAmXm/Z+abmTMjrsEODfqBPqCRrVEeGHJC3F4J
fnmLAi+pEbisAcpxuA+4rbj4gkGEovC/qiKsgUTs1Djt3kCAruvXCcdiuBsXu86l07x7/JjnAwNonz7V
YtcoBqEsjdvby5lEAtf27VW/F3M5Hp4/Ty6ZlJ4RaQBvSwt/T0/j9PkAyGQyjI6Oomka3d3dRKNRAD7O
zXGlrY2GfF4KQjkOgzIAvffu0bRvHwDJZJJTJ07wfmwM7elTnt29iyscJtLRQV19PXooxPsHD3BI+ErN
gL2ujkulEk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0
DOmjNADlaJnWa0ualT5Kk9WyNGh8zjnkDwOH1C7upAvsJiiC3JzfesICuNKvPWNto7D+zxXNZhPLsjwv
sG8xDGM7XIGPLzbg4yvtBxopZlXG8xCuAAAAAElFTkSuQmCC'''

two_en = ['qa', 'qe', 'qi', 'qo', 'vu', 'wu', 'xo', 'xu', 'yu', 'zi', 'zu']

MIN_MEDIUM_SIZE = 6
MAX_MEDIUM_SIZE = 32

MIN_STRONG_SIZE = 6
MAX_STRONG_SIZE = 32

class MainWindow(QtGui.QMainWindow):
	def __init__(self, parent=None):
		QtGui.QMainWindow.__init__(self, parent)

		m_ico = QtGui.QPixmap()
		m_ico.loadFromData(base64.b64decode(app_logo))
		self.setWindowIcon(Qt.QIcon(m_ico))
		self.setWindowTitle('Password Generator')
		self.resize(360, 100)
		self.window = Qt.QWidget()
		self.layout_main = Qt.QGridLayout()
		self.layout_main.setContentsMargins(3, 3, 3, 3)
		self.layout_main.setSpacing(3)
		self.window.setLayout(self.layout_main)

		lbl1 = QtGui.QLabel()
		m_ico = QtGui.QPixmap()
		m_ico.loadFromData(base64.b64decode(pass_easy))
		lbl1.setPixmap(QtGui.QPixmap(m_ico))
		self.textPasswd1 = Qt.QLineEdit()
		self.textPasswd1 = Qt.QLineEdit()
		self.textPasswd1Len = Qt.QLineEdit()
		self.textPasswd1Len.setText('4')
		self.textPasswd1Len.setFixedWidth(25)
		self.textPasswd1Len.setValidator(Qt.QIntValidator(2,5))
		self.textPasswd1Len.textChanged.connect(self.get_passwd_simple)
		self.get_passwd_simple()
		self.layout_main.addWidget(lbl1,0,0)
		self.layout_main.addWidget(self.textPasswd1,0,1)
		self.layout_main.addWidget(self.textPasswd1Len,0,2)
		m_ico = QtGui.QPixmap()
		m_ico.loadFromData(base64.b64decode(refresh_icon))
		new_act1 = QtGui.QPushButton()
		new_act1.setIcon(QtGui.QIcon(m_ico))
		new_act1.setShortcut('Ctrl+1')
		self.layout_main.addWidget(new_act1,0,3)
		self.connect(new_act1, QtCore.SIGNAL('clicked()'), self.get_passwd_simple)

		lbl2 = QtGui.QLabel()
		m_ico = QtGui.QPixmap()
		m_ico.loadFromData(base64.b64decode(pass_medium))
		lbl2.setPixmap(QtGui.QPixmap(m_ico))
		self.textPasswd2 = Qt.QLineEdit()
		self.textPasswd2Len = Qt.QLineEdit()
		self.textPasswd2Len.setText('12')
		self.textPasswd2Len.setFixedWidth(25)
		self.textPasswd2Len.setValidator(Qt.QIntValidator(MIN_MEDIUM_SIZE,MAX_MEDIUM_SIZE))
		self.textPasswd2Len.textChanged.connect(self.get_passwd_medium)
		self.get_passwd_medium()
		self.layout_main.addWidget(lbl2,1,0)
		self.layout_main.addWidget(self.textPasswd2,1,1)
		self.layout_main.addWidget(self.textPasswd2Len,1,2)
		m_ico = QtGui.QPixmap()
		m_ico.loadFromData(base64.b64decode(refresh_icon))
		new_act2 = QtGui.QPushButton()
		new_act2.setIcon(QtGui.QIcon(m_ico))
		new_act2.setShortcut('Ctrl+2')
		self.layout_main.addWidget(new_act2,1,3)
		self.connect(new_act2, QtCore.SIGNAL('clicked()'), self.get_passwd_medium)

		lbl3 = QtGui.QLabel()
		m_ico = QtGui.QPixmap()
		m_ico.loadFromData(base64.b64decode(pass_strong))
		lbl3.setPixmap(QtGui.QPixmap(m_ico))
		self.textPasswd3 = Qt.QLineEdit()
		self.textPasswd3Len = Qt.QLineEdit()
		self.textPasswd3Len.setText('24')
		self.textPasswd3Len.setFixedWidth(25)
		self.textPasswd3Len.setValidator(Qt.QIntValidator(MIN_STRONG_SIZE,MAX_STRONG_SIZE))
		self.textPasswd3Len.textChanged.connect(self.get_passwd_strong)
		self.get_passwd_strong()
		self.layout_main.addWidget(lbl3,2,0)
		self.layout_main.addWidget(self.textPasswd3,2,1)
		self.layout_main.addWidget(self.textPasswd3Len,2,2)
		m_ico = QtGui.QPixmap()
		m_ico.loadFromData(base64.b64decode(refresh_icon))
		new_act3 = QtGui.QPushButton()
		new_act3.setIcon(QtGui.QIcon(m_ico))
		new_act3.setShortcut('Ctrl+3')
		self.layout_main.addWidget(new_act3,2,3)
		self.connect(new_act3, QtCore.SIGNAL('clicked()'), self.get_passwd_strong)

		m_ico = QtGui.QPixmap()
		m_ico.loadFromData(base64.b64decode(exit_icon))
		quit_act = QtGui.QPushButton()
		quit_act.setIcon(QtGui.QIcon(m_ico))
		quit_act.setShortcut('Ctrl+Q')
		self.layout_main.addWidget(quit_act,3,0,1,4)
		self.connect(quit_act, QtCore.SIGNAL('clicked()'), QtGui.qApp, QtCore.SLOT('quit()'))
		self.setCentralWidget(self.window)

	def get_passwd_simple(self):
		L1 = ['a','e','i','o','u']
		L2 = [chr(t) for t in range(ord('a'),ord('z')+1) if chr(t) not in L1]
		PASS = ''
		T = self.textPasswd1Len.text()
		if T: LEN = int(T)
		else: return
		if LEN < 2: return

		for t in xrange(0,LEN):
			TWO = two_en[0]
			while TWO in two_en:
				LIT1 = random.choice(L2)
				LIT2 = random.choice(L1)
				TWO = LIT1 + LIT2
			L1.remove(LIT2)
			L2.remove(LIT1)
			PASS += TWO
		self.textPasswd1.setText(PASS)

	def get_passwd_medium(self):
		RND1 = [chr(t) for t in range(ord('a'),ord('z')+1) if chr(t) not in 'ilo']
		RND2 = [chr(t) for t in range(ord('A'),ord('Z')+1) if chr(t) not in 'ILO']
		RND3 = [chr(t) for t in range(ord('2'),ord('9')+1)]

		T = self.textPasswd2Len.text()
		if T: LEN = int(T)
		else: return
		if LEN < MIN_MEDIUM_SIZE: return

		RND_T = [RND1,RND2,RND3]
		IDX = 0
		PRE = []

		for t in xrange(0,LEN):
			PRE += [random.choice(RND_T[IDX])]
			IDX += 1
			if IDX >= len(RND_T): IDX = 0
		REZ = ''
		while PRE:
			REZ += PRE.pop(random.choice(xrange(0,len(PRE))))

		self.textPasswd2.setText(REZ)

	def get_passwd_strong(self):
		RND1 = [chr(t) for t in range(ord('a'),ord('z')+1) if chr(t) not in 'ilo']
		RND2 = [chr(t) for t in range(ord('A'),ord('Z')+1) if chr(t) not in 'ILO']
		RND3 = [chr(t) for t in range(ord('2'),ord('9')+1)]
		RND4 = list('!@#$%^&*')

		T = self.textPasswd3Len.text()
		if T: LEN = int(T)
		else: return
		if LEN < MIN_STRONG_SIZE: return

		RND_T = [RND1,RND2,RND3,RND4]
		IDX = 0
		PRE = []

		for t in xrange(0,LEN):
			PRE += [random.choice(RND_T[IDX])]
			IDX += 1
			if IDX >= len(RND_T): IDX = 0
		REZ = ''
		while PRE:
			REZ += PRE.pop(random.choice(xrange(0,len(PRE))))

		self.textPasswd3.setText(REZ)

if __name__ == "__main__" :
	app = QtGui.QApplication(sys.argv)
	main = MainWindow()
	main.show()
	sys.exit(app.exec_())

# The end is near!