summaryrefslogtreecommitdiffstats
path: root/programconfigurator.cpp
blob: 435a828eb4620ad2d3f33d14541f06dd0f8428a6 (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
  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
  2 of the License, or (at your option) any later version.
*/

#include <QPushButton>
#include <QComboBox>
#include <QLineEdit>
#include <QLabel>
#include <QGridLayout>
#include <QHBoxLayout>
#include <QCompleter>
#include <QSettings>
#include <QDirModel>
#include <QMessageBox>
#include <QRegExp>
#include <QFileInfo>

#include "programconfigurator.h"

ProgramConfigurator::ProgramConfigurator(const QString &prefix, const QString &description, QWidget *parent) : QWidget(parent), mPrefix(prefix), mDescription(description){
	QGridLayout *mainLayout = new QGridLayout;
	QLabel *l1 = new QLabel(QString(tr("Select %1")).arg(mDescription));
	mProgramSelector = new QComboBox;
	mainLayout->addWidget(l1, 0, 0);
	mainLayout->addWidget(mProgramSelector, 0, 1);
	QLabel *l2 = new QLabel(QString(tr("%1 name")).arg(mDescription));
	mNameEdit = new QLineEdit;
	mainLayout->addWidget(l2, 1, 0);
	mainLayout->addWidget(mNameEdit, 1, 1);
	QLabel *l3 = new QLabel(QString(tr("%1 path")).arg(mDescription));
	mPathEdit = new QLineEdit;
	QCompleter *fsCompleter = new QCompleter(this);
	QDirModel *dirModel = new QDirModel(this);
	fsCompleter->setModel(dirModel);
	fsCompleter->setCompletionMode(QCompleter::PopupCompletion);
	mPathEdit->setCompleter(fsCompleter);
	mainLayout->addWidget(l3, 2, 0);
	mainLayout->addWidget(mPathEdit, 2, 1);
	QLabel *l4 = new QLabel(QString(tr("%2 arguments")).arg(mDescription));
	mArgumentsEdit = new QLineEdit;
	mainLayout->addWidget(l4, 3, 0);
	mainLayout->addWidget(mArgumentsEdit, 3, 1);
	mDefaultLabel = new QLabel;
	mainLayout->addWidget(mDefaultLabel, 4, 0, 1, 2, Qt::AlignCenter);
	mAdd = new QPushButton(tr("Add program"));
	mRemove = new QPushButton(tr("Remove program"));
	mUpdate = new QPushButton(tr("Upadate"));
	mDefault = new QPushButton(tr("Set as default"));
	QHBoxLayout *buttonLayout = new QHBoxLayout;
	buttonLayout->addStretch();
	buttonLayout->addWidget(mAdd);
	buttonLayout->addWidget(mRemove);
	buttonLayout->addWidget(mUpdate);
	buttonLayout->addWidget(mDefault);
	buttonLayout->addStretch();
	mainLayout->addLayout(buttonLayout, 5, 0, 1, 2, Qt::AlignCenter);
	QVBoxLayout *stretchLayout = new QVBoxLayout;
	stretchLayout->addStretch();
	mainLayout->addLayout(stretchLayout, 6, 0, 1, 2);
	connect(mProgramSelector, SIGNAL(currentIndexChanged(QString)), this, SLOT(programChanged(QString)));
	connect(mAdd, SIGNAL(clicked()), this, SLOT(addProgram()));
	connect(mRemove, SIGNAL(clicked()), this, SLOT(removeProgram()));
	connect(mUpdate, SIGNAL(clicked()), this, SLOT(updateProgram()));
	connect(mDefault, SIGNAL(clicked()), this, SLOT(setDefault()));
	readSettings();
	setLayout(mainLayout);
}

void ProgramConfigurator::writeSettings(){
	QString setString = QString("programs_%1").arg(mPrefix);
	QString defaultString = QString("%1/default").arg(setString);
	QString dataString = QString("%1/data").arg(setString);
	QSettings s;
	s.setValue(defaultString, mDefautProg);
	s.setValue(dataString, mData);
}

void ProgramConfigurator::programChanged(const QString &program){
	QHash<QString, QVariant> data = mData.value(program).toHash();
	if(data.isEmpty()){
		return;
	}
	mNameEdit->setText(program);
	mPathEdit->setText(data.value("path").toString());
	QStringList args = data.value("args").toStringList();
	mArgumentsEdit->setText(args.join(" "));
}

void ProgramConfigurator::addProgram(){
	QString name = mNameEdit->text();
	if(mData.keys().contains(name)){
		QString message = QString(tr("Already have a program with name %1.")).arg(name);
		QMessageBox::critical(this, tr("Error"), message);
		return;
	}
	QString prog = mPathEdit->text();
	if(!isValid(prog)){
		return;
	}
	QHash<QString, QVariant> newData;
	newData.insert("path", prog);
	newData.insert("args", args(mArgumentsEdit->text()));
	mData.insert(name, newData);
	mProgramSelector->addItem(name);
	int newIdx = mProgramSelector->findText(name);
	if(newIdx != -1){
		mProgramSelector->setCurrentIndex(newIdx);
	}
}

void ProgramConfigurator::removeProgram(){
	QString name = mProgramSelector->currentText();
	if(mData.keys().contains(name)){
		QString message = QString(tr("Really remove %1 from program list?")).arg(name);
		int retval = QMessageBox::question(this, tr("Question"), message, QMessageBox::Yes | QMessageBox::No);
		if(retval == QMessageBox::Yes){
			mData.remove(name);
		}
	}
	int idx = mProgramSelector->findText(name);
	if(idx != -1){
		mProgramSelector->removeItem(idx);
	}
}

void ProgramConfigurator::updateProgram(){
	QString name = mProgramSelector->currentText();
	QHash<QString, QVariant> data = mData.value(name).toHash();
	if(data.isEmpty()){
		return;
	}
	QString path = mPathEdit->text();
	if(!isValid(path)){
		return;
	}
	QStringList arguments = args(mArgumentsEdit->text());
	QHash<QString, QVariant> newData;
	newData.insert("path", path);
	newData.insert("args", arguments);
	mData.remove(name);
	mData.insert(name, newData);
}

void ProgramConfigurator::setDefault(){
	mDefautProg = mProgramSelector->currentText();
	QString defaultLbl;
	if(mDefautProg.isEmpty()){
		defaultLbl = QString(tr("Default is [None]"));
	}else{
		defaultLbl = QString(tr("Default is %1")).arg(mDefautProg);
	}
	mDefaultLabel->setText(defaultLbl);
}

void ProgramConfigurator::readSettings(){
	QString settingsPrefix = QString("programs_%1").arg(mPrefix);
	QString defaultString = QString("%1/default").arg(settingsPrefix);
	QString dataString = QString("%1/data").arg(settingsPrefix);
	QSettings s;
	mData = s.value(dataString).toHash();
	QStringList progs;
	foreach(QString p, mData.keys()){
		progs << p;
	}
	mProgramSelector->addItems(progs);
	mDefautProg = s.value(defaultString, tr("[None]")).toString();
	int idx = mProgramSelector->findText(mDefautProg);
	if(idx != -1){
		mProgramSelector->setCurrentIndex(idx);
	}
	setDefault();
}

bool ProgramConfigurator::isValid(const QString &program){
	QFileInfo progInfo(program);
	if(!progInfo.isExecutable()){
		QString message = QString(tr("%1 is not executable.")).arg(program);
		QMessageBox::critical(this, tr("Error"), message);
		return false;
	}
	if(!progInfo.exists()){
		QString message = QString(tr("Cannot find %1.")).arg(program);
		QMessageBox::critical(this, tr("Error"), message);
		return false;
	}
	return true;
}

QStringList ProgramConfigurator::args(const QString &argString){
	if(argString.isEmpty()){
		return QStringList();
	}
	QRegExp splitter("\\s+");
	return argString.split(splitter, QString::SkipEmptyParts);
}