summaryrefslogtreecommitdiffstats
path: root/src/gui/getprofilenameform.cpp
blob: 854b9c2c1eac15c740ba97b70d76608f3cd33b11 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include "getprofilenameform.h"
#include <QDir>
#include <QMessageBox>
#include <QRegExpValidator>
#include "user.h"
#include "protocol.h"

/*
 *  Constructs a GetProfileNameForm which is a child of 'parent', with the
 *  name 'name' and widget flags set to 'f'
 *
 *  The dialog will by default be modeless, unless you set 'modal' to
 *  true to construct a modal dialog.
 */
GetProfileNameForm::GetProfileNameForm(QWidget* parent)
    : QDialog(parent)
{
	setupUi(this);
	init();
}

/*
 *  Destroys the object and frees any allocated resources
 */
GetProfileNameForm::~GetProfileNameForm()
{
    // no need to delete child widgets, Qt does it all for us
}

void GetProfileNameForm::init()
{
	// Letters, digits, underscore, minus, "@" and period
	QRegExp rxFilenameChars("[\\w\\-@\\.]*");

	// Set validators
	// USER
	profileLineEdit->setValidator(new QRegExpValidator(rxFilenameChars, this));

	// Focus seems to default to OK button, despite tab order
	profileLineEdit->setFocus();
}

void GetProfileNameForm::validate()
{
	QString profileName = profileLineEdit->text();

	if (profileName.isEmpty()) return;

	// Check for anything that was let through by the validator
	QRegExp rxFilename("^[\\w\\-][\\w\\-@\\.]*$");
	if (!rxFilename.exactMatch(profileName)) {
		QMessageBox::critical(this, PRODUCT_NAME,
			tr("Invalid profile name: %1").arg(profileName)
			// While this would be better suited as informativeText,
			// Qt wouldn't take it into account when sizing the box
			// (see http://apocalyptech.com/linux/qt/qmessagebox/)
			+ QString("\n\n") + tr(
				"Allowed characters are: letters, digits, '-', '_', '.' and '@'.\n"
				"Furthermore, the initial character cannot be '.' or '@'."
			));

		return;
	}

	// Find the .twinkle directory in HOME
	QDir d = QDir::home();
	if (!d.cd(USER_DIR)) {
		QMessageBox::critical(this, PRODUCT_NAME,
			tr("Cannot find .twinkle directory in your home directory."));
		reject();
	}

	QString filename = profileName;
	filename.append(USER_FILE_EXT);
	QString fullname = d.filePath(filename);
	if (QFile::exists(fullname)) {
		QMessageBox::warning(this, PRODUCT_NAME,
			tr("Profile already exists."));
		return;
	}

	accept();
}

QString GetProfileNameForm:: getProfileName()
{
	return profileLineEdit->text();
}

// Execute a dialog to get a name for a new profile
int GetProfileNameForm::execNewName()
{
	profileTextLabel->setText(tr("Enter a name for your profile:"));
	return exec();
}

// Execute this dialog to get a new name for an existing profile
int GetProfileNameForm::execRename(const QString &oldName)
{
	QString s = tr("Rename profile '%1' to:").arg(oldName);
	profileTextLabel->setText(s);
	return exec();
}