~zeller-benjamin/qtcreator-plugin-ubuntu/qtc41-beta

« back to all changes in this revision

Viewing changes to ubuntupackagingwidget.cpp

  • Committer: Juhapekka Piiroinen
  • Date: 2013-09-04 15:30:00 UTC
  • mto: (23.1.14 binary-plugin)
  • mto: This revision was merged to the branch mainline in revision 24.
  • Revision ID: juhapekka.piiroinen@canonical.com-20130904153000-r4lhfhrjlwmop277
Added cordova plugin from ubuntu-qtcreator-plugins.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright 2013 Canonical Ltd.
3
 
 *
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.
7
 
 *
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.
12
 
 *
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/>.
15
 
 *
16
 
 * Author: Juhapekka Piiroinen <juhapekka.piiroinen@canonical.com>
17
 
 */
18
 
 
19
 
#include "ubuntupackagingwidget.h"
20
 
#include "ui_ubuntupackagingwidget.h"
21
 
 
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>
27
 
 
28
 
#include <QFileDialog>
29
 
#include <QJsonDocument>
30
 
#include <QListWidgetItem>
31
 
 
32
 
#include <QMenu>
33
 
#include <QMessageBox>
34
 
#include <QScriptEngine>
35
 
 
36
 
using namespace Ubuntu::Internal;
37
 
 
38
 
UbuntuPackagingWidget::UbuntuPackagingWidget(QWidget *parent) :
39
 
    QWidget(parent),
40
 
    ui(new Ui::UbuntuPackagingWidget)
41
 
{
42
 
    m_previous_tab = 0;
43
 
    ui->setupUi(this);
44
 
 
45
 
    ui->tabWidget->setCurrentIndex(0);
46
 
    ui->stackedWidget->setCurrentIndex(1);
47
 
    ui->listWidget->setContextMenuPolicy(Qt::CustomContextMenu);
48
 
 
49
 
    connect(&m_bzr,SIGNAL(initializedChanged()),SLOT(bzrChanged()));
50
 
    connect(&m_manifest,SIGNAL(loaded()),SLOT(reload()));
51
 
    connect(&m_apparmor,SIGNAL(loaded()),SLOT(reload()));
52
 
 
53
 
    m_bzr.initialize();
54
 
}
55
 
 
56
 
void UbuntuPackagingWidget::setAvailable(bool available) {
57
 
    if (available) {
58
 
        ui->stackedWidget_2->setCurrentWidget(ui->pageAvailable);
59
 
    } else {
60
 
        ui->stackedWidget_2->setCurrentWidget(ui->pageNotAvailable);
61
 
    }
62
 
}
63
 
 
64
 
UbuntuPackagingWidget::~UbuntuPackagingWidget()
65
 
{
66
 
    save((ui->tabWidget->currentWidget() == ui->tabSimple));
67
 
 
68
 
    delete ui;
69
 
}
70
 
 
71
 
void UbuntuPackagingWidget::openManifestForProject() {
72
 
 
73
 
    ProjectExplorer::ProjectExplorerPlugin* projectExplorerInstance = ProjectExplorer::ProjectExplorerPlugin::instance();
74
 
    //ProjectExplorer::SessionManager* sessionManager = projectExplorerInstance->session();
75
 
    ProjectExplorer::Project* startupProject = projectExplorerInstance->startupProject();
76
 
 
77
 
    if (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();
85
 
                
86
 
        }
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();
91
 
        } else {
92
 
            load_manifest(fileName);
93
 
        }
94
 
        if (QFile(fileAppArmorName).exists()==false) {
95
 
            m_apparmor.setFileName(fileAppArmorName);
96
 
            on_pushButtonReset_clicked();
97
 
        } else {
98
 
            load_apparmor(fileAppArmorName);
99
 
        }
100
 
        ui->stackedWidget->setCurrentIndex(0);
101
 
    } else {
102
 
        m_projectName.clear();
103
 
        ui->stackedWidget->setCurrentIndex(1);
104
 
    }
105
 
}
106
 
 
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));
113
 
    reload();
114
 
}
115
 
 
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));
127
 
    reload();
128
 
}
129
 
 
130
 
void UbuntuPackagingWidget::save(bool bSaveSimple) {
131
 
    switch (m_previous_tab) {
132
 
      case 0:{
133
 
                m_manifest.setName(ui->lineEdit_name->text());
134
 
                m_manifest.setMaintainer(ui->lineEdit_maintainer->text());
135
 
                m_manifest.setTitle(ui->lineEdit_title->text());
136
 
                QStringList items;
137
 
                for (int i=0; i<ui->listWidget->count(); i++) {
138
 
                   items.append(ui->listWidget->item(i)->text());
139
 
                }
140
 
                m_apparmor.setPolicyGroups(m_projectName,items);
141
 
 
142
 
                break;
143
 
        }
144
 
      case 1:{
145
 
                m_manifest.setRaw(ui->plainTextEditJson->toPlainText());
146
 
                break;
147
 
        }
148
 
      case 2:{
149
 
                m_apparmor.setRaw(ui->plainTextEditAppArmorJson->toPlainText());
150
 
                break;
151
 
        }
152
 
    }
153
 
    m_manifest.save();
154
 
    m_apparmor.save();
155
 
}
156
 
 
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("-"));
163
 
 
164
 
    m_manifest.setName(QString(QLatin1String("com.ubuntu.developer.%0.%1")).arg(userName).arg(m_projectName));
165
 
}
166
 
 
167
 
void UbuntuPackagingWidget::load_apparmor(QString fileAppArmorName) {
168
 
    m_apparmor.load(fileAppArmorName,m_projectName);
169
 
}
170
 
 
171
 
 
172
 
 
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());
177
 
 
178
 
    QStringList policyGroups = m_apparmor.policyGroups(m_projectName);
179
 
 
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);
185
 
    }
186
 
 
187
 
    ui->plainTextEditJson->setPlainText(m_manifest.raw());
188
 
    ui->plainTextEditAppArmorJson->setPlainText(m_apparmor.raw());
189
 
 
190
 
}
191
 
 
192
 
void UbuntuPackagingWidget::on_tabWidget_currentChanged(int tab) {
193
 
    save((ui->tabWidget->currentWidget() != ui->tabSimple));
194
 
    m_previous_tab = tab;
195
 
    reload();
196
 
}
197
 
 
198
 
void UbuntuPackagingWidget::on_pushButtonReload_clicked() {
199
 
    m_manifest.reload();
200
 
    m_apparmor.reload();
201
 
}
202
 
 
203
 
void UbuntuPackagingWidget::on_listWidget_customContextMenuRequested(QPoint p) {
204
 
    if (ui->listWidget->selectedItems().count()==0) { return; }
205
 
 
206
 
    QMenu contextMenu;
207
 
    contextMenu.addAction(QLatin1String("Remove"));
208
 
    QAction* selectedItem = contextMenu.exec(ui->listWidget->mapToGlobal(p));
209
 
    if (selectedItem) {
210
 
        delete ui->listWidget->currentItem();
211
 
    }
212
 
}
213
 
 
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();
219
 
}
220
 
 
221
 
void UbuntuPackagingWidget::on_pushButtonClickPackage_clicked() {
222
 
 
223
 
    save((ui->tabWidget->currentWidget() == ui->tabSimple));
224
 
 
225
 
    Core::Command *cmd = Core::ActionManager::instance()->command(Core::Id("Ubuntu.Build.click.build"));
226
 
    if (cmd) {
227
 
        cmd->action()->trigger();
228
 
    }
229
 
}