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 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
|
/***************************************************************************
* Copyright 2007 Alexander Dymo <adymo@kdevelop.org> *
* Copyright 2011 Aleix Pol Gonzalez <aleixpol@kde.org> *
* *
* 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 "projectselectionpage.h"
#include "debug.h"
#include <QDir>
#include <QFileDialog>
#include <KConfig>
#include <KConfigGroup>
#include <KLineEdit>
#include <KLocalizedString>
#include <KMessageBox>
#include <KNS3/DownloadDialog>
#include <interfaces/icore.h>
#include <interfaces/iprojectcontroller.h>
#include <language/codegen/templatepreviewicon.h>
#include <util/scopeddialog.h>
#include "ui_projectselectionpage.h"
#include "projecttemplatesmodel.h"
using namespace KDevelop;
ProjectSelectionPage::ProjectSelectionPage(ProjectTemplatesModel *templatesModel, AppWizardDialog *wizardDialog)
: AppWizardPageWidget(wizardDialog), m_templatesModel(templatesModel)
{
ui = new Ui::ProjectSelectionPage();
ui->setupUi(this);
ui->descriptionContent->setBackgroundRole(QPalette::Base);
ui->descriptionContent->setForegroundRole(QPalette::Text);
ui->locationUrl->setMode(KFile::Directory | KFile::ExistingOnly | KFile::LocalOnly );
ui->locationUrl->setUrl(KDevelop::ICore::self()->projectController()->projectsBaseDirectory());
ui->locationValidWidget->hide();
ui->locationValidWidget->setMessageType(KMessageWidget::Error);
ui->locationValidWidget->setCloseButtonVisible(false);
connect( ui->locationUrl->lineEdit(), &KLineEdit::textEdited,
this, &ProjectSelectionPage::urlEdited);
connect( ui->locationUrl, &KUrlRequester::urlSelected,
this, &ProjectSelectionPage::urlEdited);
connect( ui->projectNameEdit, &QLineEdit::textEdited,
this, &ProjectSelectionPage::nameChanged );
ui->listView->setLevels(2);
ui->listView->setHeaderLabels(QStringList{
i18nc("@title:column", "Category"),
i18nc("@title:column", "Project Type")
});
ui->listView->setModel(templatesModel);
ui->listView->setLastLevelViewMode(MultiLevelListView::DirectChildren);
connect (ui->listView, &MultiLevelListView::currentIndexChanged, this, &ProjectSelectionPage::typeChanged);
typeChanged(ui->listView->currentIndex());
connect( ui->templateType, QOverload<int>::of(&QComboBox::currentIndexChanged),
this, &ProjectSelectionPage::templateChanged );
auto* getMoreButton = new QPushButton(i18nc("@action:button", "Get More Templates"), ui->listView);
getMoreButton->setIcon(QIcon::fromTheme(QStringLiteral("get-hot-new-stuff")));
connect (getMoreButton, &QPushButton::clicked,
this, &ProjectSelectionPage::moreTemplatesClicked);
ui->listView->addWidget(0, getMoreButton);
auto* loadButton = new QPushButton(ui->listView);
loadButton->setText(i18nc("@action:button", "Load Template from File"));
loadButton->setIcon(QIcon::fromTheme(QStringLiteral("application-x-archive")));
connect (loadButton, &QPushButton::clicked, this, &ProjectSelectionPage::loadFileClicked);
ui->listView->addWidget(0, loadButton);
m_wizardDialog = wizardDialog;
}
void ProjectSelectionPage::nameChanged()
{
validateData();
emit locationChanged( location() );
}
ProjectSelectionPage::~ProjectSelectionPage()
{
delete ui;
}
void ProjectSelectionPage::typeChanged(const QModelIndex& idx)
{
if (!idx.model())
{
qCDebug(PLUGIN_APPWIZARD) << "Index with no model";
return;
}
int children = idx.model()->rowCount(idx);
ui->templateType->setVisible(children);
ui->templateType->setEnabled(children > 1);
if (children) {
ui->templateType->setModel(m_templatesModel);
ui->templateType->setRootModelIndex(idx);
ui->templateType->setCurrentIndex(0);
itemChanged(idx.model()->index(0, 0, idx));
} else {
itemChanged(idx);
}
}
void ProjectSelectionPage::templateChanged(int current)
{
QModelIndex idx=m_templatesModel->index(current, 0, ui->templateType->rootModelIndex());
itemChanged(idx);
}
void ProjectSelectionPage::itemChanged( const QModelIndex& current)
{
TemplatePreviewIcon icon = current.data(KDevelop::TemplatesModel::PreviewIconRole).value<TemplatePreviewIcon>();
QPixmap pixmap = icon.pixmap();
ui->icon->setPixmap(pixmap);
ui->icon->setFixedHeight(pixmap.height());
// header name is either from this index directly or the parents if we show the combo box
const QVariant headerData = ui->templateType->isVisible()
? current.parent().data()
: current.data();
ui->header->setText(QStringLiteral("<h1>%1</h1>").arg(headerData.toString().trimmed()));
ui->description->setText(current.data(KDevelop::TemplatesModel::CommentRole).toString());
validateData();
ui->propertiesBox->setEnabled(true);
}
QString ProjectSelectionPage::selectedTemplate()
{
QStandardItem *item = currentItem();
if (item)
return item->data().toString();
else
return QString();
}
QUrl ProjectSelectionPage::location()
{
QUrl url = ui->locationUrl->url().adjusted(QUrl::StripTrailingSlash);
url.setPath(url.path() + QLatin1Char('/') + QString::fromUtf8(encodedProjectName()));
return url;
}
QString ProjectSelectionPage::projectName()
{
return ui->projectNameEdit->text();
}
void ProjectSelectionPage::urlEdited()
{
validateData();
emit locationChanged( location() );
}
void ProjectSelectionPage::validateData()
{
QUrl url = ui->locationUrl->url();
if( !url.isLocalFile() || url.isEmpty() )
{
ui->locationValidWidget->setText( i18n("Invalid location") );
ui->locationValidWidget->animatedShow();
emit invalid();
return;
}
if (projectName().isEmpty()) {
ui->locationValidWidget->setText( i18n("Empty project name") );
ui->locationValidWidget->animatedShow();
emit invalid();
return;
}
if (!projectName().isEmpty()) {
QString projectName = this->projectName();
QString templatefile = m_wizardDialog->appInfo().appTemplate;
// Read template file
KConfig config(templatefile);
KConfigGroup configgroup(&config, "General");
QString pattern = configgroup.readEntry( "ValidProjectName" , "^[a-zA-Z][a-zA-Z0-9_]+$" );
// Validation
int pos = 0;
QRegExp regex( pattern );
QRegExpValidator validator( regex );
if( validator.validate(projectName, pos) == QValidator::Invalid )
{
ui->locationValidWidget->setText( i18n("Invalid project name") );
ui->locationValidWidget->animatedShow();
emit invalid();
return;
}
}
QDir tDir(url.toLocalFile());
while (!tDir.exists() && !tDir.isRoot()) {
if (!tDir.cdUp()) {
break;
}
}
if (tDir.exists())
{
QFileInfo tFileInfo(tDir.absolutePath());
if (!tFileInfo.isWritable() || !tFileInfo.isExecutable())
{
ui->locationValidWidget->setText( i18n("Unable to create subdirectories, "
"missing permissions on: %1", tDir.absolutePath()) );
ui->locationValidWidget->animatedShow();
emit invalid();
return;
}
}
QStandardItem* item = currentItem();
if( item && !item->hasChildren() )
{
ui->locationValidWidget->animatedHide();
emit valid();
} else
{
ui->locationValidWidget->setText( i18n("Invalid project template, please choose a leaf item") );
ui->locationValidWidget->animatedShow();
emit invalid();
return;
}
// Check for non-empty target directory. Not an error, but need to display a warning.
url.setPath(url.path() + QLatin1Char('/') + QString::fromUtf8(encodedProjectName()));
QFileInfo fi( url.toLocalFile() );
if( fi.exists() && fi.isDir() )
{
if( !QDir( fi.absoluteFilePath()).entryList( QDir::NoDotAndDotDot | QDir::AllEntries ).isEmpty() )
{
ui->locationValidWidget->setText( i18n("Path already exists and contains files. Open it as a project.") );
ui->locationValidWidget->animatedShow();
emit invalid();
return;
}
}
}
QByteArray ProjectSelectionPage::encodedProjectName()
{
// : < > * ? / \ | " are invalid on windows
QByteArray tEncodedName = projectName().toUtf8();
for (int i = 0; i < tEncodedName.size(); ++i)
{
QChar tChar(QLatin1Char(tEncodedName.at(i)));
if (tChar.isDigit() || tChar.isSpace() || tChar.isLetter() || tChar == QLatin1Char('%')) {
continue;
}
QByteArray tReplace = QUrl::toPercentEncoding( tChar );
tEncodedName.replace( tEncodedName.at( i ) ,tReplace );
i = i + tReplace.size() - 1;
}
return tEncodedName;
}
QStandardItem* ProjectSelectionPage::currentItem() const
{
QStandardItem* item = m_templatesModel->itemFromIndex( ui->listView->currentIndex() );
if ( item && item->hasChildren() )
{
const int current = ui->templateType->currentIndex();
const QModelIndex idx = m_templatesModel->index( current, 0, ui->templateType->rootModelIndex() );
item = m_templatesModel->itemFromIndex(idx);
}
return item;
}
bool ProjectSelectionPage::shouldContinue()
{
QFileInfo fi(location().toLocalFile());
if (fi.exists() && fi.isDir())
{
if (!QDir(fi.absoluteFilePath()).entryList(QDir::NoDotAndDotDot | QDir::AllEntries).isEmpty())
{
int res = KMessageBox::questionYesNo(this, i18n("The specified path already exists and contains files. "
"Are you sure you want to proceed?"));
return res == KMessageBox::Yes;
}
}
return true;
}
void ProjectSelectionPage::loadFileClicked()
{
const QStringList supportedMimeTypes {
QStringLiteral("application/x-desktop"),
QStringLiteral("application/x-bzip-compressed-tar"),
QStringLiteral("application/zip")
};
ScopedDialog<QFileDialog> fileDialog(this, i18nc("@title:window", "Load Template from File"));
fileDialog->setMimeTypeFilters(supportedMimeTypes);
fileDialog->setFileMode(QFileDialog::ExistingFiles);
if (!fileDialog->exec()) {
return;
}
const auto& fileNames = fileDialog->selectedFiles();
for (const auto& fileName : fileNames) {
QString destination = m_templatesModel->loadTemplateFile(fileName);
QModelIndexList indexes = m_templatesModel->templateIndexes(destination);
if (indexes.size() > 2)
{
ui->listView->setCurrentIndex(indexes.at(1));
ui->templateType->setCurrentIndex(indexes.at(2).row());
}
}
}
void ProjectSelectionPage::moreTemplatesClicked()
{
ScopedDialog<KNS3::DownloadDialog> dialog(QStringLiteral("kdevappwizard.knsrc"), this);
if (!dialog->exec())
return;
const auto entries = dialog->changedEntries();
if (entries.isEmpty()) {
return;
}
m_templatesModel->refresh();
bool updated = false;
for (const KNS3::Entry& entry : entries) {
if (!entry.installedFiles().isEmpty())
{
updated = true;
setCurrentTemplate(entry.installedFiles().at(0));
break;
}
}
if (!updated)
{
ui->listView->setCurrentIndex(QModelIndex());
}
}
void ProjectSelectionPage::setCurrentTemplate (const QString& fileName)
{
QModelIndexList indexes = m_templatesModel->templateIndexes(fileName);
if (indexes.size() > 1)
{
ui->listView->setCurrentIndex(indexes.at(1));
}
if (indexes.size() > 2)
{
ui->templateType->setCurrentIndex(indexes.at(2).row());
}
}
|