2
* Copyright 2013 Canonical Ltd.
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU Lesser General Public License as published by
6
* the Free Software Foundation; version 2.1.
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU Lesser General Public License for more details.
13
* You should have received a copy of the GNU Lesser General Public License
14
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16
* Author: Juhapekka Piiroinen <juhapekka.piiroinen@canonical.com>
19
#include "ubuntupackagingwidget.h"
20
#include "ui_ubuntupackagingwidget.h"
22
#include <projectexplorer/projectexplorer.h>
23
#include <projectexplorer/project.h>
24
#include <projectexplorer/session.h>
25
#include <coreplugin/actionmanager/actionmanager.h>
26
#include <coreplugin/actionmanager/actioncontainer.h>
28
#include <QFileDialog>
29
#include <QJsonDocument>
30
#include <QListWidgetItem>
33
#include <QMessageBox>
34
#include <QScriptEngine>
36
using namespace Ubuntu::Internal;
38
UbuntuPackagingWidget::UbuntuPackagingWidget(QWidget *parent) :
40
ui(new Ui::UbuntuPackagingWidget)
45
ui->tabWidget->setCurrentIndex(0);
46
ui->stackedWidget->setCurrentIndex(1);
47
ui->listWidget->setContextMenuPolicy(Qt::CustomContextMenu);
49
connect(&m_bzr,SIGNAL(initializedChanged()),SLOT(bzrChanged()));
50
connect(&m_manifest,SIGNAL(loaded()),SLOT(reload()));
51
connect(&m_apparmor,SIGNAL(loaded()),SLOT(reload()));
56
void UbuntuPackagingWidget::setAvailable(bool available) {
58
ui->stackedWidget_2->setCurrentWidget(ui->pageAvailable);
60
ui->stackedWidget_2->setCurrentWidget(ui->pageNotAvailable);
64
UbuntuPackagingWidget::~UbuntuPackagingWidget()
66
save((ui->tabWidget->currentWidget() == ui->tabSimple));
71
void UbuntuPackagingWidget::openManifestForProject() {
73
ProjectExplorer::ProjectExplorerPlugin* projectExplorerInstance = ProjectExplorer::ProjectExplorerPlugin::instance();
74
//ProjectExplorer::SessionManager* sessionManager = projectExplorerInstance->session();
75
ProjectExplorer::Project* startupProject = projectExplorerInstance->startupProject();
78
m_projectName = startupProject->displayName();
79
QString fileName = QString(QLatin1String("%0/manifest.json")).arg(startupProject->projectDirectory());
80
QString no_underscore_displayName = startupProject->displayName();
81
m_projectName=no_underscore_displayName;
82
no_underscore_displayName.replace(QLatin1String("_"),QLatin1String("-"));
83
if (no_underscore_displayName != startupProject->displayName()) {
84
m_manifest.nameDashReplaced();
87
QString fileAppArmorName = QString(QLatin1String("%0/%1.json")).arg(startupProject->projectDirectory()).arg(no_underscore_displayName);
88
if (QFile(fileName).exists()==false) {
89
m_manifest.setFileName(fileName);
90
on_pushButtonReset_clicked();
92
load_manifest(fileName);
94
if (QFile(fileAppArmorName).exists()==false) {
95
m_apparmor.setFileName(fileAppArmorName);
96
on_pushButtonReset_clicked();
98
load_apparmor(fileAppArmorName);
100
ui->stackedWidget->setCurrentIndex(0);
102
m_projectName.clear();
103
ui->stackedWidget->setCurrentIndex(1);
107
void UbuntuPackagingWidget::bzrChanged() {
108
// left empty on purpose
109
m_manifest.setMaintainer(m_bzr.whoami());
110
QString userName = m_bzr.launchpadId();
111
if (userName.isEmpty()) userName = QLatin1String("username");
112
m_manifest.setName(QString(QLatin1String("com.ubuntu.developer.%0.%1")).arg(userName).arg(m_projectName));
116
void UbuntuPackagingWidget::on_pushButtonReset_clicked() {
117
QString fileName = m_manifest.fileName();
118
QString fileAppArmorName = m_apparmor.fileName();
119
load_manifest(QLatin1String(":/ubuntu/manifest.json.template"));
120
load_apparmor(QLatin1String(":/ubuntu/myapp.json.template"));
121
m_apparmor.setFileName(fileAppArmorName);
122
m_manifest.setFileName(fileName);
123
m_manifest.setMaintainer(m_bzr.whoami());
124
QString userName = m_bzr.launchpadId();
125
if (userName.isEmpty()) userName = QLatin1String("username");
126
m_manifest.setName(QString(QLatin1String("com.ubuntu.developer.%0.%1")).arg(userName).arg(m_projectName));
130
void UbuntuPackagingWidget::save(bool bSaveSimple) {
131
switch (m_previous_tab) {
133
m_manifest.setName(ui->lineEdit_name->text());
134
m_manifest.setMaintainer(ui->lineEdit_maintainer->text());
135
m_manifest.setTitle(ui->lineEdit_title->text());
137
for (int i=0; i<ui->listWidget->count(); i++) {
138
items.append(ui->listWidget->item(i)->text());
140
m_apparmor.setPolicyGroups(m_projectName,items);
145
m_manifest.setRaw(ui->plainTextEditJson->toPlainText());
149
m_apparmor.setRaw(ui->plainTextEditAppArmorJson->toPlainText());
157
void UbuntuPackagingWidget::load_manifest(QString fileName) {
158
m_manifest.load(fileName,m_projectName);
159
m_manifest.setMaintainer(m_bzr.whoami());
160
QString userName = m_bzr.launchpadId();
161
if (userName.isEmpty()) userName = QLatin1String("username");
162
m_projectName.replace(QLatin1String("_"),QLatin1String("-"));
164
m_manifest.setName(QString(QLatin1String("com.ubuntu.developer.%0.%1")).arg(userName).arg(m_projectName));
167
void UbuntuPackagingWidget::load_apparmor(QString fileAppArmorName) {
168
m_apparmor.load(fileAppArmorName,m_projectName);
173
void UbuntuPackagingWidget::reload() {
174
ui->lineEdit_maintainer->setText(m_manifest.maintainer());
175
ui->lineEdit_name->setText(m_manifest.name());
176
ui->lineEdit_title->setText(m_manifest.title());
178
QStringList policyGroups = m_apparmor.policyGroups(m_projectName);
180
ui->listWidget->clear();
181
foreach( QString policy, policyGroups) {
182
QListWidgetItem* item = new QListWidgetItem(policy);
183
item->setFlags(item->flags() | Qt::ItemIsEditable);
184
ui->listWidget->addItem(item);
187
ui->plainTextEditJson->setPlainText(m_manifest.raw());
188
ui->plainTextEditAppArmorJson->setPlainText(m_apparmor.raw());
192
void UbuntuPackagingWidget::on_tabWidget_currentChanged(int tab) {
193
save((ui->tabWidget->currentWidget() != ui->tabSimple));
194
m_previous_tab = tab;
198
void UbuntuPackagingWidget::on_pushButtonReload_clicked() {
203
void UbuntuPackagingWidget::on_listWidget_customContextMenuRequested(QPoint p) {
204
if (ui->listWidget->selectedItems().count()==0) { return; }
207
contextMenu.addAction(QLatin1String("Remove"));
208
QAction* selectedItem = contextMenu.exec(ui->listWidget->mapToGlobal(p));
210
delete ui->listWidget->currentItem();
214
void UbuntuPackagingWidget::on_pushButton_addpolicy_clicked() {
215
QListWidgetItem* item = new QListWidgetItem(ui->lineEdit_policy->text());
216
item->setFlags(item->flags() | Qt::ItemIsEditable);
217
ui->listWidget->addItem(item);
218
ui->lineEdit_policy->clear();
221
void UbuntuPackagingWidget::on_pushButtonClickPackage_clicked() {
223
save((ui->tabWidget->currentWidget() == ui->tabSimple));
225
Core::Command *cmd = Core::ActionManager::instance()->command(Core::Id("Ubuntu.Build.click.build"));
227
cmd->action()->trigger();