|  | @@ -0,0 +1,150 @@
 | 
		
	
		
			
			|  | 1 | +@** Typica User Management.
 | 
		
	
		
			
			|  | 2 | +
 | 
		
	
		
			
			|  | 3 | +\noindent Starting in version 1.8, the concepts of database user and \pn{} user
 | 
		
	
		
			
			|  | 4 | +are separated. This means that there must be controls for creating new users
 | 
		
	
		
			
			|  | 5 | +and for selecting the user to log in as. Other management interfaces can be
 | 
		
	
		
			
			|  | 6 | +implemented in configuration scripts.
 | 
		
	
		
			
			|  | 7 | +
 | 
		
	
		
			
			|  | 8 | +@* User Creation.
 | 
		
	
		
			
			|  | 9 | +
 | 
		
	
		
			
			|  | 10 | +\noindent The first time \pn{} is started with a database connection and a
 | 
		
	
		
			
			|  | 11 | +multi-user aware configuration there will not be any user records in the
 | 
		
	
		
			
			|  | 12 | +database. An interface for adding new users is provided.
 | 
		
	
		
			
			|  | 13 | +
 | 
		
	
		
			
			|  | 14 | +@<Class declarations@>=
 | 
		
	
		
			
			|  | 15 | +class NewTypicaUser: public QDialog
 | 
		
	
		
			
			|  | 16 | +{
 | 
		
	
		
			
			|  | 17 | +	Q_OBJECT
 | 
		
	
		
			
			|  | 18 | +	public:
 | 
		
	
		
			
			|  | 19 | +		NewTypicaUser();
 | 
		
	
		
			
			|  | 20 | +	public slots:
 | 
		
	
		
			
			|  | 21 | +		void createAndReset();
 | 
		
	
		
			
			|  | 22 | +		void createAndClose();
 | 
		
	
		
			
			|  | 23 | +		void validate();
 | 
		
	
		
			
			|  | 24 | +		void cancelValidate();
 | 
		
	
		
			
			|  | 25 | +	private:
 | 
		
	
		
			
			|  | 26 | +		void createNewUser();
 | 
		
	
		
			
			|  | 27 | +		QLineEdit *userField;
 | 
		
	
		
			
			|  | 28 | +		QLineEdit *passwordField;
 | 
		
	
		
			
			|  | 29 | +		QCheckBox *autoLogin;
 | 
		
	
		
			
			|  | 30 | +		QPushButton *saveAndCloseButton;
 | 
		
	
		
			
			|  | 31 | +		QPushButton *saveAndNewButton;
 | 
		
	
		
			
			|  | 32 | +		QPushButton *cancelButton;
 | 
		
	
		
			
			|  | 33 | +};
 | 
		
	
		
			
			|  | 34 | +
 | 
		
	
		
			
			|  | 35 | +@ The constructor sets up the dialog.
 | 
		
	
		
			
			|  | 36 | +
 | 
		
	
		
			
			|  | 37 | +@<NewTypicaUser implementation@>=
 | 
		
	
		
			
			|  | 38 | +NewTypicaUser::NewTypicaUser() : QDialog(),
 | 
		
	
		
			
			|  | 39 | +	userField(new QLineEdit), passwordField(new QLineEdit),
 | 
		
	
		
			
			|  | 40 | +	autoLogin(new QCheckBox(tr("Log in automatically"))),
 | 
		
	
		
			
			|  | 41 | +	saveAndCloseButton(new QPushButton(tr("Save and Close"))),
 | 
		
	
		
			
			|  | 42 | +	saveAndNewButton(new QPushButton(tr("Save and Create Another"))),
 | 
		
	
		
			
			|  | 43 | +	cancelButton(new QPushButton(tr("Cancel")))
 | 
		
	
		
			
			|  | 44 | +{
 | 
		
	
		
			
			|  | 45 | +	setModal(true);
 | 
		
	
		
			
			|  | 46 | +	QVBoxLayout *mainLayout = new QVBoxLayout;
 | 
		
	
		
			
			|  | 47 | +	QFormLayout *form = new QFormLayout;
 | 
		
	
		
			
			|  | 48 | +	QHBoxLayout *buttons = new QHBoxLayout;
 | 
		
	
		
			
			|  | 49 | +	form->addRow(tr("Name:"), userField);
 | 
		
	
		
			
			|  | 50 | +	passwordField->setEchoMode(QLineEdit::Password);
 | 
		
	
		
			
			|  | 51 | +	form->addRow(tr("Password:"), passwordField);
 | 
		
	
		
			
			|  | 52 | +	form->addRow(autoLogin);
 | 
		
	
		
			
			|  | 53 | +	buttons->addWidget(cancelButton);
 | 
		
	
		
			
			|  | 54 | +	buttons->addStretch();
 | 
		
	
		
			
			|  | 55 | +	buttons->addWidget(saveAndNewButton);
 | 
		
	
		
			
			|  | 56 | +	buttons->addWidget(saveAndCloseButton);
 | 
		
	
		
			
			|  | 57 | +	mainLayout->addLayout(form);
 | 
		
	
		
			
			|  | 58 | +	mainLayout->addLayout(buttons);
 | 
		
	
		
			
			|  | 59 | +	setLayout(mainLayout);
 | 
		
	
		
			
			|  | 60 | +	setWindowTitle(tr("Create New User"));
 | 
		
	
		
			
			|  | 61 | +	connect(cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
 | 
		
	
		
			
			|  | 62 | +	connect(saveAndCloseButton, SIGNAL(clicked()), this, SLOT(createAndClose()));
 | 
		
	
		
			
			|  | 63 | +	connect(saveAndNewButton, SIGNAL(clicked()), this, SLOT(createAndReset()));
 | 
		
	
		
			
			|  | 64 | +	connect(userField, SIGNAL(textChanged(QString)), this, SLOT(validate()));
 | 
		
	
		
			
			|  | 65 | +}
 | 
		
	
		
			
			|  | 66 | +
 | 
		
	
		
			
			|  | 67 | +@ Slots handle basic operation.
 | 
		
	
		
			
			|  | 68 | +
 | 
		
	
		
			
			|  | 69 | +@<NewTypicaUser implementation@>=
 | 
		
	
		
			
			|  | 70 | +void NewTypicaUser::createAndReset()
 | 
		
	
		
			
			|  | 71 | +{
 | 
		
	
		
			
			|  | 72 | +	createNewUser();
 | 
		
	
		
			
			|  | 73 | +	userField->setText("");
 | 
		
	
		
			
			|  | 74 | +	passwordField->setText("");
 | 
		
	
		
			
			|  | 75 | +	autoLogin->setChecked(false);
 | 
		
	
		
			
			|  | 76 | +}
 | 
		
	
		
			
			|  | 77 | +
 | 
		
	
		
			
			|  | 78 | +void NewTypicaUser::createAndClose()
 | 
		
	
		
			
			|  | 79 | +{
 | 
		
	
		
			
			|  | 80 | +	createNewUser();
 | 
		
	
		
			
			|  | 81 | +	accept();
 | 
		
	
		
			
			|  | 82 | +}
 | 
		
	
		
			
			|  | 83 | +
 | 
		
	
		
			
			|  | 84 | +void NewTypicaUser::createNewUser()
 | 
		
	
		
			
			|  | 85 | +{
 | 
		
	
		
			
			|  | 86 | +	SqlQueryConnection h;
 | 
		
	
		
			
			|  | 87 | +	QSqlQuery *dbquery = h.operator->();
 | 
		
	
		
			
			|  | 88 | +	dbquery->prepare("INSERT INTO typica_users (name, password, active, auto_login) VALUES (:name, :password, true, :auto)");
 | 
		
	
		
			
			|  | 89 | +	dbquery->bindValue(":name", userField->text());
 | 
		
	
		
			
			|  | 90 | +	dbquery->bindValue(":password", passwordField->text());
 | 
		
	
		
			
			|  | 91 | +	dbquery->bindValue(":auto", autoLogin->isChecked());
 | 
		
	
		
			
			|  | 92 | +	dbquery->exec();
 | 
		
	
		
			
			|  | 93 | +	cancelButton->setEnabled(true);
 | 
		
	
		
			
			|  | 94 | +}
 | 
		
	
		
			
			|  | 95 | +
 | 
		
	
		
			
			|  | 96 | +void NewTypicaUser::validate()
 | 
		
	
		
			
			|  | 97 | +{
 | 
		
	
		
			
			|  | 98 | +	if(!userField->text().isEmpty())
 | 
		
	
		
			
			|  | 99 | +	{
 | 
		
	
		
			
			|  | 100 | +		saveAndCloseButton->setEnabled(true);
 | 
		
	
		
			
			|  | 101 | +		saveAndNewButton->setEnabled(true);
 | 
		
	
		
			
			|  | 102 | +	}
 | 
		
	
		
			
			|  | 103 | +	else
 | 
		
	
		
			
			|  | 104 | +	{
 | 
		
	
		
			
			|  | 105 | +		saveAndCloseButton->setEnabled(false);
 | 
		
	
		
			
			|  | 106 | +		saveAndNewButton->setEnabled(false);
 | 
		
	
		
			
			|  | 107 | +	}
 | 
		
	
		
			
			|  | 108 | +}
 | 
		
	
		
			
			|  | 109 | +
 | 
		
	
		
			
			|  | 110 | +void NewTypicaUser::cancelValidate()
 | 
		
	
		
			
			|  | 111 | +{
 | 
		
	
		
			
			|  | 112 | +	SqlQueryConnection h;
 | 
		
	
		
			
			|  | 113 | +	QSqlQuery *dbquery = h.operator->();
 | 
		
	
		
			
			|  | 114 | +	dbquery->exec("SELECT count(1) FROM typica_users");
 | 
		
	
		
			
			|  | 115 | +	if(dbquery->next())
 | 
		
	
		
			
			|  | 116 | +	{
 | 
		
	
		
			
			|  | 117 | +		if(dbquery->value(0).toInt() > 0)
 | 
		
	
		
			
			|  | 118 | +		{
 | 
		
	
		
			
			|  | 119 | +			cancelButton->setEnabled(true);
 | 
		
	
		
			
			|  | 120 | +			return;
 | 
		
	
		
			
			|  | 121 | +		}
 | 
		
	
		
			
			|  | 122 | +	}
 | 
		
	
		
			
			|  | 123 | +	cancelButton->setEnabled(false);
 | 
		
	
		
			
			|  | 124 | +}
 | 
		
	
		
			
			|  | 125 | +
 | 
		
	
		
			
			|  | 126 | +@ This is exposted to the host environment in the usual way.
 | 
		
	
		
			
			|  | 127 | +
 | 
		
	
		
			
			|  | 128 | +@<Set up the scripting engine@>=
 | 
		
	
		
			
			|  | 129 | +constructor = engine->newFunction(constructNewTypicaUser);
 | 
		
	
		
			
			|  | 130 | +value = engine->newQMetaObject(&NewTypicaUser::staticMetaObject, constructor);
 | 
		
	
		
			
			|  | 131 | +engine->globalObject().setProperty("NewTypicaUser", value);
 | 
		
	
		
			
			|  | 132 | +
 | 
		
	
		
			
			|  | 133 | +@ The constructor is trivial.
 | 
		
	
		
			
			|  | 134 | +
 | 
		
	
		
			
			|  | 135 | +@<Functions for scripting@>=
 | 
		
	
		
			
			|  | 136 | +QScriptValue constructNewTypicaUser(QScriptContext *, QScriptEngine *engine)
 | 
		
	
		
			
			|  | 137 | +{
 | 
		
	
		
			
			|  | 138 | +	QScriptValue object = engine->newQObject(new NewTypicaUser);
 | 
		
	
		
			
			|  | 139 | +	return object;
 | 
		
	
		
			
			|  | 140 | +}
 | 
		
	
		
			
			|  | 141 | +
 | 
		
	
		
			
			|  | 142 | +@ A function prototype is required.
 | 
		
	
		
			
			|  | 143 | +
 | 
		
	
		
			
			|  | 144 | +@<Function prototypes for scripting@>=
 | 
		
	
		
			
			|  | 145 | +QScriptValue constructNewTypicaUser(QScriptContext *context, QScriptEngine *engine);
 | 
		
	
		
			
			|  | 146 | +
 | 
		
	
		
			
			|  | 147 | +@ Add class implementation to generated source file.
 | 
		
	
		
			
			|  | 148 | +
 | 
		
	
		
			
			|  | 149 | +@<Class implementations@>=
 | 
		
	
		
			
			|  | 150 | +@<NewTypicaUser implementation@>
 |